symtable.c revision 2990640d8af1d1fd6ba264ced46e741b9dc091d0
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 72990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton/* The compiler uses this function to load a PySymtableEntry object 82990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton for a code block. Each block is loaded twice, once during the 92990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton symbol table pass and once during the code gen pass. Entries 102990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton created during the first pass are cached for the second pass, using 112990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton the st_symbols dictionary. 122990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton 132990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton The cache is keyed by st_nscopes. Each code block node in a 142990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton module's parse tree can be assigned a unique id based on the order 152990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton in which the nodes are visited by the compiler. This strategy 162990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton works so long as the symbol table and codegen passes visit the same 172990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton nodes in the same order. 182990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton*/ 192990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton 202990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton 21cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy HyltonPyObject * 22cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy HyltonPySymtableEntry_New(struct symtable *st, char *name, int type, int lineno) 23cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton{ 24cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton PySymtableEntryObject *ste = NULL; 25cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton PyObject *k, *v; 26cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 27cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton k = PyInt_FromLong(st->st_nscopes++); 28cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton if (k == NULL) 29cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 30cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton v = PyDict_GetItem(st->st_symbols, k); 312990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton if (v) { 320f2d4b84a60e6064640f09c8010dedeaa9453f14Tim Peters Py_DECREF(k); 3374b3bc47df9979e54c1f0c7866730eb499705f0eJeremy Hylton Py_INCREF(v); 3474b3bc47df9979e54c1f0c7866730eb499705f0eJeremy Hylton return v; 3574b3bc47df9979e54c1f0c7866730eb499705f0eJeremy Hylton } 36cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 37cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste = (PySymtableEntryObject *)PyObject_New(PySymtableEntryObject, 38cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton &PySymtableEntry_Type); 39cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_table = st; 40cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_id = k; 41cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 42cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton v = PyString_FromString(name); 43cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton if (v == NULL) 44cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 45cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_name = v; 46cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 47cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton v = PyDict_New(); 48cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton if (v == NULL) 49cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 50cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_symbols = v; 51cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 52cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton v = PyList_New(0); 53cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton if (v == NULL) 54cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 55cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_varnames = v; 56cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 57cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton v = PyList_New(0); 58cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton if (v == NULL) 59cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 60cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_children = v; 61cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 6229906eef3a56c43a4cd68a8706c75844b2384e59Jeremy Hylton ste->ste_optimized = 0; 6386424e333f1736591114da975c8d2926ba6f51f5Jeremy Hylton ste->ste_opt_lineno = 0; 64cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_lineno = lineno; 65cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton switch (type) { 66cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton case funcdef: 67cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton case lambdef: 68cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_type = TYPE_FUNCTION; 69cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton break; 70cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton case classdef: 71cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_type = TYPE_CLASS; 72cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton break; 73cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton case single_input: 74cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton case eval_input: 75cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton case file_input: 76cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_type = TYPE_MODULE; 77cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton break; 78cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton } 79cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 80cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton if (st->st_cur == NULL) 81cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_nested = 0; 82cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton else if (st->st_cur->ste_nested 83cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton || st->st_cur->ste_type == TYPE_FUNCTION) 84cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_nested = 1; 85cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton else 86cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_nested = 0; 87cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_child_free = 0; 885ca576ed0a0c697c7e7547adfd0b3af010fd2053Tim Peters ste->ste_generator = 0; 89cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 90cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0) 91cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 9274b3bc47df9979e54c1f0c7866730eb499705f0eJeremy Hylton 93cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton return (PyObject *)ste; 94cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton fail: 95cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste); 96cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton return NULL; 97cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton} 98cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 99cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonstatic PyObject * 100cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonste_repr(PySymtableEntryObject *ste) 101cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton{ 102cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton char buf[256]; 103cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 1044b4ab20f2c07a0e555c0b982089448b5471f6521Barry Warsaw PyOS_snprintf(buf, sizeof(buf), 1054b4ab20f2c07a0e555c0b982089448b5471f6521Barry Warsaw "<symtable entry %.100s(%ld), line %d>", 1064b4ab20f2c07a0e555c0b982089448b5471f6521Barry Warsaw PyString_AS_STRING(ste->ste_name), 1074b4ab20f2c07a0e555c0b982089448b5471f6521Barry Warsaw PyInt_AS_LONG(ste->ste_id), 1084b4ab20f2c07a0e555c0b982089448b5471f6521Barry Warsaw ste->ste_lineno); 109cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton return PyString_FromString(buf); 110cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton} 111cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 112cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonstatic void 113cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonste_dealloc(PySymtableEntryObject *ste) 114cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton{ 115cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_table = NULL; 116cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste->ste_id); 117cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste->ste_name); 118cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste->ste_symbols); 119cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste->ste_varnames); 120cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste->ste_children); 121cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton PyObject_Del(ste); 122cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton} 123cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 124cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton#define OFF(x) offsetof(PySymtableEntryObject, x) 125cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 1266f7993765ac0989b5d13084240797913627a31d8Guido van Rossumstatic PyMemberDef ste_memberlist[] = { 127cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"id", T_OBJECT, OFF(ste_id), READONLY}, 128cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"name", T_OBJECT, OFF(ste_name), READONLY}, 129cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"symbols", T_OBJECT, OFF(ste_symbols), READONLY}, 130cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"varnames", T_OBJECT, OFF(ste_varnames), READONLY}, 131cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"children", T_OBJECT, OFF(ste_children), READONLY}, 132cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"type", T_INT, OFF(ste_type), READONLY}, 133cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"lineno", T_INT, OFF(ste_lineno), READONLY}, 134cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"optimized",T_INT, OFF(ste_optimized), READONLY}, 135cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"nested", T_INT, OFF(ste_nested), READONLY}, 136cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {NULL} 137cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton}; 138cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 139cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy HyltonPyTypeObject PySymtableEntry_Type = { 140cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton PyObject_HEAD_INIT(&PyType_Type) 141cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, 142cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton "symtable entry", 143cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton sizeof(PySymtableEntryObject), 144cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, 145cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton (destructor)ste_dealloc, /* tp_dealloc */ 146cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_print */ 1476f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_getattr */ 148cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_setattr */ 149cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_compare */ 150cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton (reprfunc)ste_repr, /* tp_repr */ 151cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_as_number */ 152cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_as_sequence */ 153cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_as_mapping */ 154cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_hash */ 155cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_call */ 156cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_str */ 1576f7993765ac0989b5d13084240797913627a31d8Guido van Rossum PyObject_GenericGetAttr, /* tp_getattro */ 158cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_setattro */ 159cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_as_buffer */ 160cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_TPFLAGS_DEFAULT, /* tp_flags */ 161cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_doc */ 1626f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_traverse */ 1636f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_clear */ 1646f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_richcompare */ 1656f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_weaklistoffset */ 1666f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_iter */ 1676f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_iternext */ 1686f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_methods */ 1696f7993765ac0989b5d13084240797913627a31d8Guido van Rossum ste_memberlist, /* tp_members */ 1706f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_getset */ 1716f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_base */ 1726f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_dict */ 1736f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_descr_get */ 1746f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_descr_set */ 1756f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_dictoffset */ 1766f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_init */ 1776f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_alloc */ 1786f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_new */ 179cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton}; 180