symtable.c revision 4db62b1e14ed909d4bd574633a11c45455c76beb
1cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton#include "Python.h" 24db62b1e14ed909d4bd574633a11c45455c76bebJeremy Hylton#include "compile.h" 3cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton#include "symtable.h" 4cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton#include "graminit.h" 5cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton#include "structmember.h" 6cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 7cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy HyltonPyObject * 8cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy HyltonPySymtableEntry_New(struct symtable *st, char *name, int type, int lineno) 9cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton{ 10cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton PySymtableEntryObject *ste = NULL; 11cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton PyObject *k, *v; 12cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 13cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton k = PyInt_FromLong(st->st_nscopes++); 14cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton if (k == NULL) 15cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 16cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton v = PyDict_GetItem(st->st_symbols, k); 1774b3bc47df9979e54c1f0c7866730eb499705f0eJeremy Hylton if (v) /* XXX could check that name, type, lineno match */ { 1874b3bc47df9979e54c1f0c7866730eb499705f0eJeremy Hylton Py_INCREF(v); 1974b3bc47df9979e54c1f0c7866730eb499705f0eJeremy Hylton return v; 2074b3bc47df9979e54c1f0c7866730eb499705f0eJeremy Hylton } 21cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 22cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste = (PySymtableEntryObject *)PyObject_New(PySymtableEntryObject, 23cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton &PySymtableEntry_Type); 24cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_table = st; 25cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_id = k; 26cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 27cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton v = PyString_FromString(name); 28cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton if (v == NULL) 29cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 30cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_name = v; 31cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 32cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton v = PyDict_New(); 33cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton if (v == NULL) 34cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 35cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_symbols = v; 36cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 37cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton v = PyList_New(0); 38cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton if (v == NULL) 39cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 40cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_varnames = v; 41cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 42cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton v = PyList_New(0); 43cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton if (v == NULL) 44cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 45cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_children = v; 46cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 4729906eef3a56c43a4cd68a8706c75844b2384e59Jeremy Hylton ste->ste_optimized = 0; 48cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_lineno = lineno; 49cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton switch (type) { 50cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton case funcdef: 51cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton case lambdef: 52cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_type = TYPE_FUNCTION; 53cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton break; 54cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton case classdef: 55cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_type = TYPE_CLASS; 56cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton break; 57cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton case single_input: 58cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton case eval_input: 59cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton case file_input: 60cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_type = TYPE_MODULE; 61cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton break; 62cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton } 63cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 64cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton if (st->st_cur == NULL) 65cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_nested = 0; 66cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton else if (st->st_cur->ste_nested 67cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton || st->st_cur->ste_type == TYPE_FUNCTION) 68cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_nested = 1; 69cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton else 70cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_nested = 0; 71cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_child_free = 0; 72cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 73cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0) 74cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 7574b3bc47df9979e54c1f0c7866730eb499705f0eJeremy Hylton 76cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton return (PyObject *)ste; 77cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton fail: 78cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste); 79cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton return NULL; 80cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton} 81cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 82cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonstatic PyObject * 83cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonste_repr(PySymtableEntryObject *ste) 84cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton{ 85cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton char buf[256]; 86cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 87cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton sprintf(buf, "<symtable entry %.100s(%ld), line %d>", 88cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton PyString_AS_STRING(ste->ste_name), 89cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton PyInt_AS_LONG(ste->ste_id), 90cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_lineno); 91cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton return PyString_FromString(buf); 92cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton} 93cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 94cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonstatic void 95cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonste_dealloc(PySymtableEntryObject *ste) 96cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton{ 97cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_table = NULL; 98cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste->ste_id); 99cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste->ste_name); 100cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste->ste_symbols); 101cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste->ste_varnames); 102cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste->ste_children); 103cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton PyObject_Del(ste); 104cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton} 105cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 106cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton#define OFF(x) offsetof(PySymtableEntryObject, x) 107cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 108cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonstatic struct memberlist ste_memberlist[] = { 109cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"id", T_OBJECT, OFF(ste_id), READONLY}, 110cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"name", T_OBJECT, OFF(ste_name), READONLY}, 111cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"symbols", T_OBJECT, OFF(ste_symbols), READONLY}, 112cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"varnames", T_OBJECT, OFF(ste_varnames), READONLY}, 113cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"children", T_OBJECT, OFF(ste_children), READONLY}, 114cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"type", T_INT, OFF(ste_type), READONLY}, 115cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"lineno", T_INT, OFF(ste_lineno), READONLY}, 116cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"optimized",T_INT, OFF(ste_optimized), READONLY}, 117cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"nested", T_INT, OFF(ste_nested), READONLY}, 118cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {NULL} 119cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton}; 120cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 121cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonstatic PyObject * 122cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonste_getattr(PySymtableEntryObject *ste, char *name) 123cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton{ 124cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton return PyMember_Get((char *)ste, ste_memberlist, name); 125cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton} 126cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 127cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy HyltonPyTypeObject PySymtableEntry_Type = { 128cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton PyObject_HEAD_INIT(&PyType_Type) 129cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, 130cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton "symtable entry", 131cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton sizeof(PySymtableEntryObject), 132cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, 133cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton (destructor)ste_dealloc, /* tp_dealloc */ 134cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_print */ 135cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton (getattrfunc)ste_getattr, /* tp_getattr */ 136cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_setattr */ 137cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_compare */ 138cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton (reprfunc)ste_repr, /* tp_repr */ 139cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_as_number */ 140cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_as_sequence */ 141cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_as_mapping */ 142cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_hash */ 143cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_call */ 144cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_str */ 145cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_getattro */ 146cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_setattro */ 147cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_as_buffer */ 148cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_TPFLAGS_DEFAULT, /* tp_flags */ 149cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_doc */ 150cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton}; 151