symtable.c revision e0d12eb85cff367497090b1fdc58582edc0d4c99
1cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton#include "Python.h" 23e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#include "Python-ast.h" 33e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#include "code.h" 4cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton#include "symtable.h" 5cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton#include "structmember.h" 6cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 75d0ad50f5acf84f2e8a1ca5c6951f333aef0e25aNeal Norwitz/* error strings used for warnings */ 83e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define GLOBAL_AFTER_ASSIGN \ 93e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton"name '%.400s' is assigned to before global declaration" 102990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton 113e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define GLOBAL_AFTER_USE \ 123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton"name '%.400s' is used prior to global declaration" 132990640d8af1d1fd6ba264ced46e741b9dc091d0Jeremy Hylton 145d0ad50f5acf84f2e8a1ca5c6951f333aef0e25aNeal Norwitz#define IMPORT_STAR_WARNING "import * only allowed at module level" 155d0ad50f5acf84f2e8a1ca5c6951f333aef0e25aNeal Norwitz 16ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl#define RETURN_VAL_IN_GENERATOR \ 17ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl "'return' with argument inside generator" 185d0ad50f5acf84f2e8a1ca5c6951f333aef0e25aNeal Norwitz 19e0d12eb85cff367497090b1fdc58582edc0d4c99Benjamin Peterson 20090b3dde06c7590ed7d789bd01a7d800cc09a348Neal Norwitzstatic PySTEntryObject * 21e0d12eb85cff367497090b1fdc58582edc0d4c99Benjamin Petersonste_new(struct symtable *st, identifier name, _Py_block_ty block, 223e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton void *key, int lineno) 23cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton{ 243e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PySTEntryObject *ste = NULL; 25b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz PyObject *k; 26cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 273e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton k = PyLong_FromVoidPtr(key); 28cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton if (k == NULL) 29cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 305becac576c18541e09e88c536e6ca71257a283f0Neal Norwitz ste = PyObject_New(PySTEntryObject, &PySTEntry_Type); 315becac576c18541e09e88c536e6ca71257a283f0Neal Norwitz if (ste == NULL) 325becac576c18541e09e88c536e6ca71257a283f0Neal Norwitz goto fail; 33cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_table = st; 34cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_id = k; 353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste->ste_tmpname = 0; 36cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste->ste_name = name; 383e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_INCREF(name); 39b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 40b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz ste->ste_symbols = NULL; 41b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz ste->ste_varnames = NULL; 42b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz ste->ste_children = NULL; 43b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 44b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz ste->ste_symbols = PyDict_New(); 45b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz if (ste->ste_symbols == NULL) 46cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 47cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 48b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz ste->ste_varnames = PyList_New(0); 49b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz if (ste->ste_varnames == NULL) 50cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 51cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 52b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz ste->ste_children = PyList_New(0); 53b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz if (ste->ste_children == NULL) 54cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 55cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 563e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste->ste_type = block; 573e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste->ste_unoptimized = 0; 583e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste->ste_nested = 0; 593e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste->ste_free = 0; 603e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste->ste_varargs = 0; 613e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste->ste_varkeywords = 0; 6286424e333f1736591114da975c8d2926ba6f51f5Jeremy Hylton ste->ste_opt_lineno = 0; 634d508adae3101434ae62be1c140e9877673dc257Jeremy Hylton ste->ste_tmpname = 0; 64cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_lineno = lineno; 65cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 663e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (st->st_cur != NULL && 673e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton (st->st_cur->ste_nested || 683e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_cur->ste_type == FunctionBlock)) 69cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_nested = 1; 70cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_child_free = 0; 715ca576ed0a0c697c7e7547adfd0b3af010fd2053Tim Peters ste->ste_generator = 0; 72ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl ste->ste_returns_value = 0; 73cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 74cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0) 75cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton goto fail; 7674b3bc47df9979e54c1f0c7866730eb499705f0eJeremy Hylton 773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return ste; 78cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton fail: 79cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste); 80cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton return NULL; 81cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton} 82cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 83cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonstatic PyObject * 843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonste_repr(PySTEntryObject *ste) 85cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton{ 86cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton char buf[256]; 87cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 884b4ab20f2c07a0e555c0b982089448b5471f6521Barry Warsaw PyOS_snprintf(buf, sizeof(buf), 894b4ab20f2c07a0e555c0b982089448b5471f6521Barry Warsaw "<symtable entry %.100s(%ld), line %d>", 90dd96db63f689e2f0d8ae5a1436b3b3395eec7de5Gregory P. Smith PyString_AS_STRING(ste->ste_name), 913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyInt_AS_LONG(ste->ste_id), ste->ste_lineno); 92dd96db63f689e2f0d8ae5a1436b3b3395eec7de5Gregory P. Smith return PyString_FromString(buf); 93cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton} 94cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 95cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonstatic void 963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonste_dealloc(PySTEntryObject *ste) 97cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton{ 98cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton ste->ste_table = NULL; 99cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste->ste_id); 100cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste->ste_name); 101cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste->ste_symbols); 102cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste->ste_varnames); 103cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_XDECREF(ste->ste_children); 104cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton PyObject_Del(ste); 105cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton} 106cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 1073e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define OFF(x) offsetof(PySTEntryObject, x) 108cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 1096f7993765ac0989b5d13084240797913627a31d8Guido van Rossumstatic PyMemberDef ste_memberlist[] = { 110cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"id", T_OBJECT, OFF(ste_id), READONLY}, 111cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"name", T_OBJECT, OFF(ste_name), READONLY}, 112cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"symbols", T_OBJECT, OFF(ste_symbols), READONLY}, 113cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"varnames", T_OBJECT, OFF(ste_varnames), READONLY}, 114cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"children", T_OBJECT, OFF(ste_children), READONLY}, 115cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"type", T_INT, OFF(ste_type), READONLY}, 116cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {"lineno", T_INT, OFF(ste_lineno), READONLY}, 117cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton {NULL} 118cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton}; 119cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 1203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPyTypeObject PySTEntry_Type = { 1216819210b9e4e5719a6f7f9c1725f8fa70a8936f6Martin v. Löwis PyVarObject_HEAD_INIT(&PyType_Type, 0) 122cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton "symtable entry", 1233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton sizeof(PySTEntryObject), 124cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, 125cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton (destructor)ste_dealloc, /* tp_dealloc */ 126cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_print */ 1276f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_getattr */ 128cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_setattr */ 129cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_compare */ 130cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton (reprfunc)ste_repr, /* tp_repr */ 131cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_as_number */ 132cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_as_sequence */ 133cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_as_mapping */ 134cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_hash */ 135cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_call */ 136cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_str */ 1376f7993765ac0989b5d13084240797913627a31d8Guido van Rossum PyObject_GenericGetAttr, /* tp_getattro */ 138cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_setattro */ 139cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_as_buffer */ 140cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton Py_TPFLAGS_DEFAULT, /* tp_flags */ 141cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 0, /* tp_doc */ 1426f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_traverse */ 1436f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_clear */ 1446f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_richcompare */ 1456f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_weaklistoffset */ 1466f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_iter */ 1476f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_iternext */ 1486f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_methods */ 1496f7993765ac0989b5d13084240797913627a31d8Guido van Rossum ste_memberlist, /* tp_members */ 1506f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_getset */ 1516f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_base */ 1526f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_dict */ 1536f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_descr_get */ 1546f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_descr_set */ 1556f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_dictoffset */ 1566f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_init */ 1576f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_alloc */ 1586f7993765ac0989b5d13084240797913627a31d8Guido van Rossum 0, /* tp_new */ 159cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton}; 1603e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1613e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_analyze(struct symtable *st); 1625d0ad50f5acf84f2e8a1ca5c6951f333aef0e25aNeal Norwitzstatic int symtable_warn(struct symtable *st, char *msg, int lineno); 1633e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_enter_block(struct symtable *st, identifier name, 16462c2fac9a0758f303c0382e459031a2679a056e2Neal Norwitz _Py_block_ty block, void *ast, int lineno); 1653e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_exit_block(struct symtable *st, void *ast); 1663e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_stmt(struct symtable *st, stmt_ty s); 1673e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_expr(struct symtable *st, expr_ty s); 1683e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_genexp(struct symtable *st, expr_ty s); 1693e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_arguments(struct symtable *st, arguments_ty); 1703e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty); 1713e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_alias(struct symtable *st, alias_ty); 1723e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_comprehension(struct symtable *st, comprehension_ty); 1733e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_keyword(struct symtable *st, keyword_ty); 1743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_slice(struct symtable *st, slice_ty); 1753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_params(struct symtable *st, asdl_seq *args, int top); 1763e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_params_nested(struct symtable *st, asdl_seq *args); 1773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_implicit_arg(struct symtable *st, int pos); 1783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 18099b2533539dbc98fe7e5b3ac95e71879afca1c2cNick Coghlanstatic identifier top = NULL, lambda = NULL, genexpr = NULL; 1813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define GET_IDENTIFIER(VAR) \ 183dd96db63f689e2f0d8ae5a1436b3b3395eec7de5Gregory P. Smith ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR))) 1843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1853e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define DUPLICATE_ARGUMENT \ 1863e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton"duplicate argument '%s' in function definition" 1873e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1883e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic struct symtable * 1893e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_new(void) 1903e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton struct symtable *st; 1923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable)); 1943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (st == NULL) 1953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return NULL; 1963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_filename = NULL; 198b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz st->st_symbols = NULL; 199b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 2003e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if ((st->st_stack = PyList_New(0)) == NULL) 2013e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto fail; 2023e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if ((st->st_symbols = PyDict_New()) == NULL) 2033e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto fail; 2043e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_cur = NULL; 2053e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_tmpname = 0; 2063e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_private = NULL; 2073e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return st; 2083e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton fail: 2093e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PySymtable_Free(st); 2103e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return NULL; 2113e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 2123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 2133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstruct symtable * 2143e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future) 2153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 2163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton struct symtable *st = symtable_new(); 2173e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton asdl_seq *seq; 2183e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton int i; 2193e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 2203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (st == NULL) 2213e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return st; 2223e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_filename = filename; 2233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_future = future; 2247605936deeeb2fac75b209895717e7798f7a306fNeal Norwitz if (!GET_IDENTIFIER(top) || 2257605936deeeb2fac75b209895717e7798f7a306fNeal Norwitz !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) { 226d12bd012a6a4729b5a77c1019ca9da4e9d1b7e3eNeal Norwitz PySymtable_Free(st); 227d12bd012a6a4729b5a77c1019ca9da4e9d1b7e3eNeal Norwitz return NULL; 228d12bd012a6a4729b5a77c1019ca9da4e9d1b7e3eNeal Norwitz } 229d12bd012a6a4729b5a77c1019ca9da4e9d1b7e3eNeal Norwitz 2303e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_top = st->st_cur; 2313e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_cur->ste_unoptimized = OPT_TOPLEVEL; 2323e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* Any other top-level initialization? */ 2333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton switch (mod->kind) { 2343e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Module_kind: 2353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton seq = mod->v.Module.body; 2363e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton for (i = 0; i < asdl_seq_LEN(seq); i++) 237019aec618a24ce0743ddee50a47d7b7413a81fdaAnthony Baxter if (!symtable_visit_stmt(st, 238019aec618a24ce0743ddee50a47d7b7413a81fdaAnthony Baxter (stmt_ty)asdl_seq_GET(seq, i))) 2393e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 2403e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 2413e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Expression_kind: 2423e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_visit_expr(st, mod->v.Expression.body)) 2433e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 2443e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 2453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Interactive_kind: 2463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton seq = mod->v.Interactive.body; 2473e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton for (i = 0; i < asdl_seq_LEN(seq); i++) 248019aec618a24ce0743ddee50a47d7b7413a81fdaAnthony Baxter if (!symtable_visit_stmt(st, 249019aec618a24ce0743ddee50a47d7b7413a81fdaAnthony Baxter (stmt_ty)asdl_seq_GET(seq, i))) 2503e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 2513e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 2523e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Suite_kind: 2533e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyErr_SetString(PyExc_RuntimeError, 2543e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton "this compiler does not handle Suites"); 255b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz goto error; 2563e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 257b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz if (!symtable_exit_block(st, (void *)mod)) { 258b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz PySymtable_Free(st); 2593e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return NULL; 260b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz } 2613e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (symtable_analyze(st)) 2623e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return st; 263b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz PySymtable_Free(st); 264b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz return NULL; 2653e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton error: 266b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz (void) symtable_exit_block(st, (void *)mod); 2673e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PySymtable_Free(st); 2683e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return NULL; 2693e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 2703e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 2713e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonvoid 2723e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPySymtable_Free(struct symtable *st) 2733e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 2743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_XDECREF(st->st_symbols); 2753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_XDECREF(st->st_stack); 2763e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyMem_Free((void *)st); 2773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 2783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 2793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPySTEntryObject * 2803e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPySymtable_Lookup(struct symtable *st, void *key) 2813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 2823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *k, *v; 2833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 2843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton k = PyLong_FromVoidPtr(key); 2853e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (k == NULL) 2863e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return NULL; 2873e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton v = PyDict_GetItem(st->st_symbols, k); 2883e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (v) { 2893e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton assert(PySTEntry_Check(v)); 2903e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_INCREF(v); 2913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 2923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton else { 2933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyErr_SetString(PyExc_KeyError, 2943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton "unknown symbol table entry"); 2953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 296b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 297b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz Py_DECREF(k); 298b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz return (PySTEntryObject *)v; 2993e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 3003e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3013e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonint 3023e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPyST_GetScope(PySTEntryObject *ste, PyObject *name) 3033e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 3043e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *v = PyDict_GetItem(ste->ste_symbols, name); 3053e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!v) 3063e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 3073e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton assert(PyInt_Check(v)); 3083e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK; 3093e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 3103e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3113e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* Analyze raw symbol information to determine scope of each name. 3133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3143e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton The next several functions are helpers for PySymtable_Analyze(), 3153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton which determines whether a name is local, global, or free. In addition, 3163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton it determines which local variables are cell variables; they provide 3173e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton bindings that are used for free variables in enclosed blocks. 3183e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3193e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton There are also two kinds of free variables, implicit and explicit. An 3203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton explicit global is declared with the global statement. An implicit 3213e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton global is a free variable for which the compiler has found no binding 3223e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton in an enclosing function scope. The implicit global is either a global 3233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton or a builtin. Python's module and class blocks use the xxx_NAME opcodes 3243e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton to handle these names to implement slightly odd semantics. In such a 3253e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton block, the name is treated as global until it is assigned to; then it 3263e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton is treated as a local. 3273e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3283e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton The symbol table requires two passes to determine the scope of each name. 3293e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton The first pass collects raw facts from the AST: the name is a parameter 3303e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton here, the name is used by not defined here, etc. The second pass analyzes 3313e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton these facts during a pass over the PySTEntryObjects created during pass 1. 3323e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton When a function is entered during the second pass, the parent passes 3343e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton the set of all name bindings visible to its children. These bindings 3353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton are used to determine if the variable is free or an implicit global. 3363e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton After doing the local analysis, it analyzes each of its child blocks 3373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton using an updated set of name bindings. 3383e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3393e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton The children update the free variable set. If a local variable is free 3403e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton in a child, the variable is marked as a cell. The current function must 3413e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton provide runtime storage for the variable that may outlive the function's 3423e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton frame. Cell variables are removed from the free set before the analyze 3433e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton function returns to its parent. 3443e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton The sets of bound and free variables are implemented as dictionaries 3463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton mapping strings to None. 3473e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 3483e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3493e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define SET_SCOPE(DICT, NAME, I) { \ 3503e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *o = PyInt_FromLong(I); \ 3513e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!o) \ 3523e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; \ 353b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz if (PyDict_SetItem((DICT), (NAME), o) < 0) { \ 354b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz Py_DECREF(o); \ 3553e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; \ 356b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz } \ 357b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz Py_DECREF(o); \ 3583e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 3593e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3603e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* Decide on scope of name, given flags. 3613e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3623e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton The dicts passed in as arguments are modified as necessary. 3633e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste is passed so that flags can be updated. 3643e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 3653e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3663e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 36746b7bda9bcd0fb11878a154234c3064e19e35f3cNeal Norwitzanalyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags, 3683e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *bound, PyObject *local, PyObject *free, 3693e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *global) 3703e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 3713e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (flags & DEF_GLOBAL) { 3723e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (flags & DEF_PARAM) { 3733e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyErr_Format(PyExc_SyntaxError, 3743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton "name '%s' is local and global", 375dd96db63f689e2f0d8ae5a1436b3b3395eec7de5Gregory P. Smith PyString_AS_STRING(name)); 3760847332716d538cc500f8495ddae81510d05b205Benjamin Peterson PyErr_SyntaxLocation(ste->ste_table->st_filename, 3770847332716d538cc500f8495ddae81510d05b205Benjamin Peterson ste->ste_lineno); 3780847332716d538cc500f8495ddae81510d05b205Benjamin Peterson 3793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 3803e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 3813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton SET_SCOPE(dict, name, GLOBAL_EXPLICIT); 3823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyDict_SetItem(global, name, Py_None) < 0) 3833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 3843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (bound && PyDict_GetItem(bound, name)) { 3853e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyDict_DelItem(bound, name) < 0) 3863e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 3873e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 3883e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 3893e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 3903e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (flags & DEF_BOUND) { 3913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton SET_SCOPE(dict, name, LOCAL); 3923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyDict_SetItem(local, name, Py_None) < 0) 3933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 3943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyDict_GetItem(global, name)) { 3953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyDict_DelItem(global, name) < 0) 3963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 3973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 3983e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 3993e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 4003e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* If an enclosing block has a binding for this name, it 4013e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton is a free variable rather than a global variable. 4023e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Note that having a non-NULL bound implies that the block 4033e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton is nested. 4043e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton */ 4053e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (bound && PyDict_GetItem(bound, name)) { 4063e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton SET_SCOPE(dict, name, FREE); 4073e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste->ste_free = 1; 4083e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyDict_SetItem(free, name, Py_None) < 0) 4093e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 4103e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 4113e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 4123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* If a parent has a global statement, then call it global 4133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton explicit? It could also be global implicit. 4143e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton */ 4153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton else if (global && PyDict_GetItem(global, name)) { 4163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton SET_SCOPE(dict, name, GLOBAL_EXPLICIT); 4173e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 4183e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 4193e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton else { 4203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (ste->ste_nested) 4213e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste->ste_free = 1; 4223e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton SET_SCOPE(dict, name, GLOBAL_IMPLICIT); 4233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 4243e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 4253e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; /* Can't get here */ 4263e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 4273e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4283e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#undef SET_SCOPE 4293e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4303e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* If a name is defined in free and also in locals, then this block 4313e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton provides the binding for the free variable. The name should be 4323e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton marked CELL in this block and removed from the free list. 4333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4343e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Note that the current block's free variables are included in free. 4353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton That's safe because no name can be free and local in the same scope. 4363e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 4373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4383e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 4393e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonanalyze_cells(PyObject *scope, PyObject *free) 4403e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 4413e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *name, *v, *w; 44218e165558b24d29e7e0ca501842b9236589b012aMartin v. Löwis int success = 0; 44318e165558b24d29e7e0ca501842b9236589b012aMartin v. Löwis Py_ssize_t pos = 0; 4443e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton w = PyInt_FromLong(CELL); 4463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!w) 4473e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 4483e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton while (PyDict_Next(scope, &pos, &name, &v)) { 449d8fe7ab711df2d6380aeff085fe8eda6b268f3ebTim Peters long flags; 4503e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton assert(PyInt_Check(v)); 451d8fe7ab711df2d6380aeff085fe8eda6b268f3ebTim Peters flags = PyInt_AS_LONG(v); 4523e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (flags != LOCAL) 4533e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton continue; 4543e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!PyDict_GetItem(free, name)) 4553e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton continue; 4563e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* Replace LOCAL with CELL for this name, and remove 4573e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton from free. It is safe to replace the value of name 4583e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton in the dict, because it will not cause a resize. 4593e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton */ 4603e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyDict_SetItem(scope, name, w) < 0) 4613e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 4623e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!PyDict_DelItem(free, name) < 0) 4633e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 4643e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 4653e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton success = 1; 4663e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton error: 4673e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(w); 4683e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return success; 4693e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 4703e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4713e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* Check for illegal statements in unoptimized namespaces */ 4723e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 4733e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltoncheck_unoptimized(const PySTEntryObject* ste) { 4743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton char buf[300]; 47531441302171fe882976bcc05f5ded9645cd690afArmin Rigo const char* trailer; 4763e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4772dfcef5c57f35af0e1ba200ba36508be8667001aNeil Schemenauer if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized 4783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton || !(ste->ste_free || ste->ste_child_free)) 4793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 4803e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 48131441302171fe882976bcc05f5ded9645cd690afArmin Rigo trailer = (ste->ste_child_free ? 4823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton "contains a nested function with free variables" : 4833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton "is a nested function"); 4843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4853e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton switch (ste->ste_unoptimized) { 4863e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case OPT_TOPLEVEL: /* exec / import * at top-level is fine */ 4873e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case OPT_EXEC: /* qualified exec is fine */ 4883e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 4893e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case OPT_IMPORT_STAR: 4903e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyOS_snprintf(buf, sizeof(buf), 4913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton "import * is not allowed in function '%.100s' " 4923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton "because it is %s", 493dd96db63f689e2f0d8ae5a1436b3b3395eec7de5Gregory P. Smith PyString_AS_STRING(ste->ste_name), trailer); 4943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 4953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case OPT_BARE_EXEC: 4963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyOS_snprintf(buf, sizeof(buf), 4973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton "unqualified exec is not allowed in function " 4983e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton "'%.100s' it %s", 499dd96db63f689e2f0d8ae5a1436b3b3395eec7de5Gregory P. Smith PyString_AS_STRING(ste->ste_name), trailer); 5003e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 5013e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton default: 5023e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyOS_snprintf(buf, sizeof(buf), 5033e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton "function '%.100s' uses import * and bare exec, " 5043e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton "which are illegal because it %s", 505dd96db63f689e2f0d8ae5a1436b3b3395eec7de5Gregory P. Smith PyString_AS_STRING(ste->ste_name), trailer); 5063e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 5073e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 5083e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 5093e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyErr_SetString(PyExc_SyntaxError, buf); 5103e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyErr_SyntaxLocation(ste->ste_table->st_filename, 5113e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste->ste_opt_lineno); 5123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 5133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 5143e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 5153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* Enter the final scope information into the st_symbols dict. 5163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton * 5173e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton * All arguments are dicts. Modifies symbols, others are read-only. 5183e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 5193e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 5203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonupdate_symbols(PyObject *symbols, PyObject *scope, 521019aec618a24ce0743ddee50a47d7b7413a81fdaAnthony Baxter PyObject *bound, PyObject *free, int classflag) 5223e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 5233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *name, *v, *u, *w, *free_value = NULL; 52418e165558b24d29e7e0ca501842b9236589b012aMartin v. Löwis Py_ssize_t pos = 0; 5253e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 5263e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton while (PyDict_Next(symbols, &pos, &name, &v)) { 52746b7bda9bcd0fb11878a154234c3064e19e35f3cNeal Norwitz long i, flags; 5283e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton assert(PyInt_Check(v)); 5293e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton flags = PyInt_AS_LONG(v); 5303e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton w = PyDict_GetItem(scope, name); 5313e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton assert(w && PyInt_Check(w)); 5323e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton i = PyInt_AS_LONG(w); 5333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton flags |= (i << SCOPE_OFF); 5343e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton u = PyInt_FromLong(flags); 53518b6adf9b2f581da04cf5cd6149b84408763ea6eNeal Norwitz if (!u) 53618b6adf9b2f581da04cf5cd6149b84408763ea6eNeal Norwitz return 0; 5373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyDict_SetItem(symbols, name, u) < 0) { 5383e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(u); 5393e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 5403e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 5413e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(u); 5423e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 5433e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 5443e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton free_value = PyInt_FromLong(FREE << SCOPE_OFF); 5453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!free_value) 5463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 5473e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 5483e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* add a free variable when it's only use is for creating a closure */ 5493e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton pos = 0; 5503e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton while (PyDict_Next(free, &pos, &name, &v)) { 5513e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *o = PyDict_GetItem(symbols, name); 5523e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 5533e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (o) { 5543e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* It could be a free variable in a method of 5553e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton the class that has the same name as a local 5563e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton or global in the class scope. 5573e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton */ 558019aec618a24ce0743ddee50a47d7b7413a81fdaAnthony Baxter if (classflag && 5593e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) { 56046b7bda9bcd0fb11878a154234c3064e19e35f3cNeal Norwitz long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS; 5613e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton o = PyInt_FromLong(i); 5623e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!o) { 5633e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(free_value); 5643e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 5653e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 5663e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyDict_SetItem(symbols, name, o) < 0) { 5673e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(o); 5683e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(free_value); 5693e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 5703e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 571b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz Py_DECREF(o); 5723e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 5733e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* else it's not free, probably a cell */ 5743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton continue; 5753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 5763e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!PyDict_GetItem(bound, name)) 5773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton continue; /* it's a global */ 5783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 5793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyDict_SetItem(symbols, name, free_value) < 0) { 5803e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(free_value); 5813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 5823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 5833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 5843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(free_value); 5853e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 5863e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 5873e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 5883e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* Make final symbol table decisions for block of ste. 5893e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Arguments: 5903e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste -- current symtable entry (input/output) 5913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton bound -- set of variables bound in enclosing scopes (input) 5923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton free -- set of free variables in enclosed scopes (output) 5933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton globals -- set of declared global variables in enclosing scopes (input) 5943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 5953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 5963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 5973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonanalyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free, 5983e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *global) 5993e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 6003e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *name, *v, *local = NULL, *scope = NULL, *newbound = NULL; 6013e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *newglobal = NULL, *newfree = NULL; 60218e165558b24d29e7e0ca501842b9236589b012aMartin v. Löwis int i, success = 0; 60318e165558b24d29e7e0ca501842b9236589b012aMartin v. Löwis Py_ssize_t pos = 0; 6043e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 6053e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton local = PyDict_New(); 6063e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!local) 6073e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 6083e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton scope = PyDict_New(); 6093e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!scope) 6103e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 6113e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton newglobal = PyDict_New(); 6123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!newglobal) 6133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 6143e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton newfree = PyDict_New(); 6153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!newfree) 6163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 6173e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton newbound = PyDict_New(); 6183e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!newbound) 6193e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 6203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 6213e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (ste->ste_type == ClassBlock) { 6223e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* make a copy of globals before calling analyze_name(), 6233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton because global statements in the class have no effect 6243e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton on nested functions. 6253e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton */ 6263e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyDict_Update(newglobal, global) < 0) 6273e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 6283e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (bound) 6293e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyDict_Update(newbound, bound) < 0) 6303e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 6313e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 6323e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 6333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton assert(PySTEntry_Check(ste)); 6343e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton assert(PyDict_Check(ste->ste_symbols)); 6353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { 63646b7bda9bcd0fb11878a154234c3064e19e35f3cNeal Norwitz long flags = PyInt_AS_LONG(v); 6373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!analyze_name(ste, scope, name, flags, bound, local, free, 6383e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton global)) 6393e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 6403e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 6413e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 6423e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (ste->ste_type != ClassBlock) { 6433e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (ste->ste_type == FunctionBlock) { 6443e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyDict_Update(newbound, local) < 0) 6453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 6463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 6473e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (bound) { 6483e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyDict_Update(newbound, bound) < 0) 6493e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 6503e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 6513e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyDict_Update(newglobal, global) < 0) 6523e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 6533e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 6543e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 6553e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* Recursively call analyze_block() on each child block */ 6563e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { 6573e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *c = PyList_GET_ITEM(ste->ste_children, i); 65831441302171fe882976bcc05f5ded9645cd690afArmin Rigo PySTEntryObject* entry; 6593e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton assert(c && PySTEntry_Check(c)); 66031441302171fe882976bcc05f5ded9645cd690afArmin Rigo entry = (PySTEntryObject*)c; 6613e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!analyze_block(entry, newbound, newfree, newglobal)) 6623e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 6633e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (entry->ste_free || entry->ste_child_free) 6643e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste->ste_child_free = 1; 6653e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 6663e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 6673e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree)) 6683e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 6693e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!update_symbols(ste->ste_symbols, scope, bound, newfree, 6703e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste->ste_type == ClassBlock)) 6713e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 6723e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!check_unoptimized(ste)) 6733e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 6743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 6753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyDict_Update(free, newfree) < 0) 6763e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton goto error; 6773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton success = 1; 6783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton error: 6793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_XDECREF(local); 6803e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_XDECREF(scope); 6813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_XDECREF(newbound); 6823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_XDECREF(newglobal); 6833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_XDECREF(newfree); 6843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!success) 6853e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton assert(PyErr_Occurred()); 6863e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return success; 6873e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 6883e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 6893e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 6903e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_analyze(struct symtable *st) 6913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 6923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *free, *global; 6933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton int r; 6943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 6953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton free = PyDict_New(); 6963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!free) 6973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 6983e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton global = PyDict_New(); 6993e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!global) { 700b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz Py_DECREF(free); 7013e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 7023e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 7033e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton r = analyze_block(st->st_top, NULL, free, global); 7043e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(free); 7053e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(global); 7063e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return r; 7073e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 7083e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 7093e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 7103e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 7115d0ad50f5acf84f2e8a1ca5c6951f333aef0e25aNeal Norwitzsymtable_warn(struct symtable *st, char *msg, int lineno) 7123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 7133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename, 7145d0ad50f5acf84f2e8a1ca5c6951f333aef0e25aNeal Norwitz lineno, NULL, NULL) < 0) { 7153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { 7163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyErr_SetString(PyExc_SyntaxError, msg); 7173e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyErr_SyntaxLocation(st->st_filename, 7183e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_cur->ste_lineno); 7193e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 7203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 7213e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 7223e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 7233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 7243e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 725e0d12eb85cff367497090b1fdc58582edc0d4c99Benjamin Peterson/* symtable_enter_block() gets a reference via ste_new. 7263e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton This reference is released when the block is exited, via the DECREF 7273e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton in symtable_exit_block(). 7283e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 7293e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 7303e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 7313e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_exit_block(struct symtable *st, void *ast) 7323e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 733d96ee909934f3855135589e0e8d6ece1912c1f22Martin v. Löwis Py_ssize_t end; 7343e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 735b59d08c2fb407bbb1527b72141e125760863c2baNeal Norwitz Py_CLEAR(st->st_cur); 7363e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton end = PyList_GET_SIZE(st->st_stack) - 1; 7373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (end >= 0) { 7383e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, 7393e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton end); 740d12bd012a6a4729b5a77c1019ca9da4e9d1b7e3eNeal Norwitz if (st->st_cur == NULL) 741d12bd012a6a4729b5a77c1019ca9da4e9d1b7e3eNeal Norwitz return 0; 7423e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_INCREF(st->st_cur); 7433e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PySequence_DelItem(st->st_stack, end) < 0) 7443e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 7453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 7463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 7473e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 7483e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 7493e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 75062c2fac9a0758f303c0382e459031a2679a056e2Neal Norwitzsymtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block, 7513e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton void *ast, int lineno) 7523e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 7533e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PySTEntryObject *prev = NULL; 7543e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 7553e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (st->st_cur) { 7563e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton prev = st->st_cur; 7573e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { 7583e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 7593e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 7603e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(st->st_cur); 7613e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 762e0d12eb85cff367497090b1fdc58582edc0d4c99Benjamin Peterson st->st_cur = ste_new(st, name, block, ast, lineno); 763d12bd012a6a4729b5a77c1019ca9da4e9d1b7e3eNeal Norwitz if (st->st_cur == NULL) 764d12bd012a6a4729b5a77c1019ca9da4e9d1b7e3eNeal Norwitz return 0; 7653e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (name == GET_IDENTIFIER(top)) 7663e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_global = st->st_cur->ste_symbols; 7673e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (prev) { 7683e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (PyList_Append(prev->ste_children, 7693e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton (PyObject *)st->st_cur) < 0) { 7703e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 7713e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 7723e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 7733e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 7743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 7753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 77646b7bda9bcd0fb11878a154234c3064e19e35f3cNeal Norwitzstatic long 7773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_lookup(struct symtable *st, PyObject *name) 7783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 7793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *o; 7808b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer PyObject *mangled = _Py_Mangle(st->st_private, name); 7818b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer if (!mangled) 7828b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer return 0; 7838b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer o = PyDict_GetItem(st->st_cur->ste_symbols, mangled); 7848b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer Py_DECREF(mangled); 7853e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!o) 7863e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 7873e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return PyInt_AsLong(o); 7883e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 7893e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 7903e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 7913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_add_def(struct symtable *st, PyObject *name, int flag) 7923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 7933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *o; 7943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *dict; 79546b7bda9bcd0fb11878a154234c3064e19e35f3cNeal Norwitz long val; 7968b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer PyObject *mangled = _Py_Mangle(st->st_private, name); 7973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 7988b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer if (!mangled) 7998b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer return 0; 8003e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton dict = st->st_cur->ste_symbols; 8018b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer if ((o = PyDict_GetItem(dict, mangled))) { 8023e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton val = PyInt_AS_LONG(o); 8033e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { 8048b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer /* Is it better to use 'mangled' or 'name' here? */ 8053e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, 806dd96db63f689e2f0d8ae5a1436b3b3395eec7de5Gregory P. Smith PyString_AsString(name)); 8073e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyErr_SyntaxLocation(st->st_filename, 8083e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_cur->ste_lineno); 8098b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer goto error; 8103e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 8113e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton val |= flag; 8123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } else 8133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton val = flag; 8143e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton o = PyInt_FromLong(val); 8153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (o == NULL) 8168b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer goto error; 8178b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer if (PyDict_SetItem(dict, mangled, o) < 0) { 8183e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(o); 8198b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer goto error; 8203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 8213e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(o); 8223e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 8233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (flag & DEF_PARAM) { 8248b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0) 8258b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer goto error; 8263e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } else if (flag & DEF_GLOBAL) { 8273e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* XXX need to update DEF_GLOBAL for other flags too; 8283e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton perhaps only DEF_FREE_GLOBAL */ 8293e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton val = flag; 8308b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer if ((o = PyDict_GetItem(st->st_global, mangled))) { 8313e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton val |= PyInt_AS_LONG(o); 8323e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 8333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton o = PyInt_FromLong(val); 8343e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (o == NULL) 8358b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer goto error; 8368b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer if (PyDict_SetItem(st->st_global, mangled, o) < 0) { 8373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(o); 8388b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer goto error; 8393e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 8403e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(o); 8413e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 8424737b2348b197d21deffbf12ff23488918e9b9d4Neal Norwitz Py_DECREF(mangled); 8433e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 8448b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer 8458b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauererror: 8468b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer Py_DECREF(mangled); 8478b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer return 0; 8483e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 8493e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 8503e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument. 8513e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton They use the ASDL name to synthesize the name of the C type and the visit 8523e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton function. 8533e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 8543e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is 8553e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton useful if the first node in the sequence requires special treatment. 8563e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 8573e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 8583e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define VISIT(ST, TYPE, V) \ 8593e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_visit_ ## TYPE((ST), (V))) \ 8603e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 861b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 862b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz#define VISIT_IN_BLOCK(ST, TYPE, V, S) \ 863b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz if (!symtable_visit_ ## TYPE((ST), (V))) { \ 864b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz symtable_exit_block((ST), (S)); \ 865b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz return 0; \ 866b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz } 867b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 8683e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define VISIT_SEQ(ST, TYPE, SEQ) { \ 8693e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton int i; \ 8703e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton asdl_seq *seq = (SEQ); /* avoid variable capture */ \ 8713e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton for (i = 0; i < asdl_seq_LEN(seq); i++) { \ 872019aec618a24ce0743ddee50a47d7b7413a81fdaAnthony Baxter TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ 8733e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_visit_ ## TYPE((ST), elt)) \ 8743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; \ 8753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } \ 8763e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 877b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 878b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \ 879b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz int i; \ 880b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz asdl_seq *seq = (SEQ); /* avoid variable capture */ \ 881b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz for (i = 0; i < asdl_seq_LEN(seq); i++) { \ 882019aec618a24ce0743ddee50a47d7b7413a81fdaAnthony Baxter TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ 883b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz if (!symtable_visit_ ## TYPE((ST), elt)) { \ 884b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz symtable_exit_block((ST), (S)); \ 885b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz return 0; \ 886b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz } \ 887b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz } \ 888b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz} 889b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 8903e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \ 8913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton int i; \ 8923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton asdl_seq *seq = (SEQ); /* avoid variable capture */ \ 8933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton for (i = (START); i < asdl_seq_LEN(seq); i++) { \ 894019aec618a24ce0743ddee50a47d7b7413a81fdaAnthony Baxter TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ 8953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_visit_ ## TYPE((ST), elt)) \ 8963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; \ 8973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } \ 8983e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 899b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 900b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \ 901b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz int i; \ 902b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz asdl_seq *seq = (SEQ); /* avoid variable capture */ \ 903b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz for (i = (START); i < asdl_seq_LEN(seq); i++) { \ 904019aec618a24ce0743ddee50a47d7b7413a81fdaAnthony Baxter TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ 905b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz if (!symtable_visit_ ## TYPE((ST), elt)) { \ 906b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz symtable_exit_block((ST), (S)); \ 907b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz return 0; \ 908b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz } \ 909b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz } \ 910b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz} 911b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 9123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 913c2e20744b2b7811632030470971c31630f0975e2Guido van Rossumsymtable_new_tmpname(struct symtable *st) 914c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum{ 915c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum char tmpname[256]; 916c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum identifier tmp; 917c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum 918c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", 919c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum ++st->st_cur->ste_tmpname); 920dd96db63f689e2f0d8ae5a1436b3b3395eec7de5Gregory P. Smith tmp = PyString_InternFromString(tmpname); 9216f5ff3f3eb7abc2f4750c1319b560f67faf546acNeal Norwitz if (!tmp) 9226f5ff3f3eb7abc2f4750c1319b560f67faf546acNeal Norwitz return 0; 923c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum if (!symtable_add_def(st, tmp, DEF_LOCAL)) 924c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum return 0; 925c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum Py_DECREF(tmp); 926c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum return 1; 927c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum} 928c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum 929c2e20744b2b7811632030470971c31630f0975e2Guido van Rossumstatic int 9303e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_stmt(struct symtable *st, stmt_ty s) 9313e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 9323e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton switch (s->kind) { 9333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case FunctionDef_kind: 9343e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL)) 9353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 9363e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (s->v.FunctionDef.args->defaults) 9373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults); 9385224d28d38eb784f17c2fed3f48368285df6d17aChristian Heimes if (s->v.FunctionDef.decorator_list) 9395224d28d38eb784f17c2fed3f48368285df6d17aChristian Heimes VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list); 9403e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_enter_block(st, s->v.FunctionDef.name, 9413e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton FunctionBlock, (void *)s, s->lineno)) 9423e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 943b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s); 944b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s); 9453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_exit_block(st, s)) 9463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 9473e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 9488b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer case ClassDef_kind: { 9498b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer PyObject *tmp; 9503e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL)) 9513e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 9523e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, expr, s->v.ClassDef.bases); 9535224d28d38eb784f17c2fed3f48368285df6d17aChristian Heimes if (s->v.ClassDef.decorator_list) 9545224d28d38eb784f17c2fed3f48368285df6d17aChristian Heimes VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list); 9553e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock, 9563e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton (void *)s, s->lineno)) 9573e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 9588b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer tmp = st->st_private; 9598b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer st->st_private = s->v.ClassDef.name; 960b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s); 9618b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer st->st_private = tmp; 9623e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_exit_block(st, s)) 9633e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 9643e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 9658b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer } 9663e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Return_kind: 967ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl if (s->v.Return.value) { 9683e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.Return.value); 969ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl st->st_cur->ste_returns_value = 1; 970ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl if (st->st_cur->ste_generator) { 971ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl PyErr_SetString(PyExc_SyntaxError, 972ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl RETURN_VAL_IN_GENERATOR); 973ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl PyErr_SyntaxLocation(st->st_filename, 974ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl s->lineno); 975ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl return 0; 976ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl } 977ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl } 9783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 9793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Delete_kind: 9803e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, expr, s->v.Delete.targets); 9813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 9823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Assign_kind: 9833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, expr, s->v.Assign.targets); 9843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.Assign.value); 9853e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 9863e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case AugAssign_kind: 9873e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.AugAssign.target); 9883e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.AugAssign.value); 9893e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 9903e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Print_kind: 9913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (s->v.Print.dest) 9923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.Print.dest); 9933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, expr, s->v.Print.values); 9943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 9953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case For_kind: 9963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.For.target); 9973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.For.iter); 9983e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, stmt, s->v.For.body); 9993e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (s->v.For.orelse) 10003e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, stmt, s->v.For.orelse); 10013e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 10023e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case While_kind: 10033e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.While.test); 10043e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, stmt, s->v.While.body); 10053e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (s->v.While.orelse) 10063e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, stmt, s->v.While.orelse); 10073e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 10083e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case If_kind: 10093e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* XXX if 0: and lookup_yield() hacks */ 10103e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.If.test); 10113e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, stmt, s->v.If.body); 10123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (s->v.If.orelse) 10133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, stmt, s->v.If.orelse); 10143e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 10153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Raise_kind: 10163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (s->v.Raise.type) { 10173e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.Raise.type); 10183e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (s->v.Raise.inst) { 10193e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.Raise.inst); 10203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (s->v.Raise.tback) 10213e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.Raise.tback); 10223e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 10233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 10243e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 10253e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case TryExcept_kind: 10263e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, stmt, s->v.TryExcept.body); 10273e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, stmt, s->v.TryExcept.orelse); 10283e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers); 10293e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 10303e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case TryFinally_kind: 10313e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, stmt, s->v.TryFinally.body); 10323e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody); 10333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 10343e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Assert_kind: 10353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.Assert.test); 10363e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (s->v.Assert.msg) 10373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.Assert.msg); 10383e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 10393e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Import_kind: 10403e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, alias, s->v.Import.names); 10413e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* XXX Don't have the lineno available inside 10423e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton visit_alias */ 10433e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) 10443e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_cur->ste_opt_lineno = s->lineno; 10453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 10463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case ImportFrom_kind: 10473e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, alias, s->v.ImportFrom.names); 10483e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* XXX Don't have the lineno available inside 10493e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton visit_alias */ 10503e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) 10513e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_cur->ste_opt_lineno = s->lineno; 10523e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 10533e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Exec_kind: 10543e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.Exec.body); 10553e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!st->st_cur->ste_opt_lineno) 10563e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_cur->ste_opt_lineno = s->lineno; 10573e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (s->v.Exec.globals) { 10583e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_cur->ste_unoptimized |= OPT_EXEC; 10593e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.Exec.globals); 10603e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (s->v.Exec.locals) 10613e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.Exec.locals); 10623e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } else { 10633e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_cur->ste_unoptimized |= OPT_BARE_EXEC; 10643e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 10653e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 10663e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Global_kind: { 10673e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton int i; 10683e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton asdl_seq *seq = s->v.Global.names; 10693e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton for (i = 0; i < asdl_seq_LEN(seq); i++) { 1070019aec618a24ce0743ddee50a47d7b7413a81fdaAnthony Baxter identifier name = (identifier)asdl_seq_GET(seq, i); 1071dd96db63f689e2f0d8ae5a1436b3b3395eec7de5Gregory P. Smith char *c_name = PyString_AS_STRING(name); 107246b7bda9bcd0fb11878a154234c3064e19e35f3cNeal Norwitz long cur = symtable_lookup(st, name); 10733e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (cur < 0) 10743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 10753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (cur & (DEF_LOCAL | USE)) { 1076b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz char buf[256]; 10773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (cur & DEF_LOCAL) 10783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyOS_snprintf(buf, sizeof(buf), 10793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton GLOBAL_AFTER_ASSIGN, 10803e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton c_name); 10813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton else 10823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyOS_snprintf(buf, sizeof(buf), 10833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton GLOBAL_AFTER_USE, 10843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton c_name); 10855d0ad50f5acf84f2e8a1ca5c6951f333aef0e25aNeal Norwitz if (!symtable_warn(st, buf, s->lineno)) 10863e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 10873e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 10883e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_add_def(st, name, DEF_GLOBAL)) 10893e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 10903e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 10913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 10923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 10933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Expr_kind: 10943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.Expr.value); 10953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 10963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Pass_kind: 10973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Break_kind: 10983e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Continue_kind: 10993e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* nothing to do here */ 11003e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 1101c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum case With_kind: 1102c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum if (!symtable_new_tmpname(st)) 1103c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum return 0; 1104c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum VISIT(st, expr, s->v.With.context_expr); 1105c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum if (s->v.With.optional_vars) { 1106c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum if (!symtable_new_tmpname(st)) 1107c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum return 0; 1108c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum VISIT(st, expr, s->v.With.optional_vars); 1109c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum } 1110c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum VISIT_SEQ(st, stmt, s->v.With.body); 1111c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum break; 11123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 11133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 11143e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 11153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 11163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 11173e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_expr(struct symtable *st, expr_ty e) 11183e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 11193e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton switch (e->kind) { 11203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case BoolOp_kind: 11213e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, expr, e->v.BoolOp.values); 11223e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 11233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case BinOp_kind: 11243e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, e->v.BinOp.left); 11253e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, e->v.BinOp.right); 11263e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 11273e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case UnaryOp_kind: 11283e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, e->v.UnaryOp.operand); 11293e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 11303e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Lambda_kind: { 11317605936deeeb2fac75b209895717e7798f7a306fNeal Norwitz if (!GET_IDENTIFIER(lambda) || 11327605936deeeb2fac75b209895717e7798f7a306fNeal Norwitz !symtable_add_def(st, lambda, DEF_LOCAL)) 11333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 11343e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (e->v.Lambda.args->defaults) 11353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, expr, e->v.Lambda.args->defaults); 11363e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* XXX how to get line numbers for expressions */ 11377605936deeeb2fac75b209895717e7798f7a306fNeal Norwitz if (!symtable_enter_block(st, lambda, 11383e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton FunctionBlock, (void *)e, 0)) 11393e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 1140b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e); 1141b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e); 11423e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_exit_block(st, (void *)e)) 11433e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 11443e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 11453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 1146dca3b9c797f6dd4b08d590fa2aa1031e22ab598eThomas Wouters case IfExp_kind: 1147dca3b9c797f6dd4b08d590fa2aa1031e22ab598eThomas Wouters VISIT(st, expr, e->v.IfExp.test); 1148dca3b9c797f6dd4b08d590fa2aa1031e22ab598eThomas Wouters VISIT(st, expr, e->v.IfExp.body); 1149dca3b9c797f6dd4b08d590fa2aa1031e22ab598eThomas Wouters VISIT(st, expr, e->v.IfExp.orelse); 1150dca3b9c797f6dd4b08d590fa2aa1031e22ab598eThomas Wouters break; 11513e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Dict_kind: 11523e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, expr, e->v.Dict.keys); 11533e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, expr, e->v.Dict.values); 11543e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 1155c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum case ListComp_kind: 1156c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum if (!symtable_new_tmpname(st)) 11573e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 11583e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, e->v.ListComp.elt); 11593e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, comprehension, e->v.ListComp.generators); 11603e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 1161c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum case GeneratorExp_kind: 1162c2e20744b2b7811632030470971c31630f0975e2Guido van Rossum if (!symtable_visit_genexp(st, e)) 11633e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 11643e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 11653e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Yield_kind: 11663e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (e->v.Yield.value) 11673e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, e->v.Yield.value); 11683e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_cur->ste_generator = 1; 1169ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl if (st->st_cur->ste_returns_value) { 1170ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl PyErr_SetString(PyExc_SyntaxError, 1171ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl RETURN_VAL_IN_GENERATOR); 1172ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl PyErr_SyntaxLocation(st->st_filename, 1173ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl e->lineno); 1174ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl return 0; 1175ddbaa660d3b64a71b4ac9eab64b1bb944ca1276bGeorg Brandl } 11763e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 11773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Compare_kind: 11783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, e->v.Compare.left); 11793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, expr, e->v.Compare.comparators); 11803e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 11813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Call_kind: 11823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, e->v.Call.func); 11833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, expr, e->v.Call.args); 11843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, keyword, e->v.Call.keywords); 11853e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (e->v.Call.starargs) 11863e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, e->v.Call.starargs); 11873e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (e->v.Call.kwargs) 11883e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, e->v.Call.kwargs); 11893e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 11903e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Repr_kind: 11913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, e->v.Repr.value); 11923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 11933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Num_kind: 11943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Str_kind: 11953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* Nothing to do here. */ 11963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 11973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* The following exprs can be assignment targets. */ 11983e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Attribute_kind: 11993e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, e->v.Attribute.value); 12003e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 12013e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Subscript_kind: 12023e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, e->v.Subscript.value); 12033e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, slice, e->v.Subscript.slice); 12043e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 12053e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Name_kind: 12063e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_add_def(st, e->v.Name.id, 12073e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton e->v.Name.ctx == Load ? USE : DEF_LOCAL)) 12083e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 12093e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 12103e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* child nodes of List and Tuple will have expr_context set */ 12113e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case List_kind: 12123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, expr, e->v.List.elts); 12133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 12143e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Tuple_kind: 12153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, expr, e->v.Tuple.elts); 12163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 12173e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 12183e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 12193e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 12203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 12213e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 12223e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_implicit_arg(struct symtable *st, int pos) 12233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1224dd96db63f689e2f0d8ae5a1436b3b3395eec7de5Gregory P. Smith PyObject *id = PyString_FromFormat(".%d", pos); 12253e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (id == NULL) 12263e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 12273e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_add_def(st, id, DEF_PARAM)) { 12283e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(id); 12293e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 12303e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 12313e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(id); 12323e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 12333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 12343e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 12353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 12363e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel) 12373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1238daf595f8a91c1a9f9a8ba5e06be4fa77c66cccf0Neal Norwitz int i; 12393e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 12403e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* go through all the toplevel arguments first */ 12413e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton for (i = 0; i < asdl_seq_LEN(args); i++) { 1242019aec618a24ce0743ddee50a47d7b7413a81fdaAnthony Baxter expr_ty arg = (expr_ty)asdl_seq_GET(args, i); 12433e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (arg->kind == Name_kind) { 12443e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton assert(arg->v.Name.ctx == Param || 12453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton (arg->v.Name.ctx == Store && !toplevel)); 12463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM)) 12473e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 12483e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 12493e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton else if (arg->kind == Tuple_kind) { 12503e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton assert(arg->v.Tuple.ctx == Store); 12513e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (toplevel) { 12523e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_implicit_arg(st, i)) 12533e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 12543e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 12553e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 12563e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton else { 12574737b2348b197d21deffbf12ff23488918e9b9d4Neal Norwitz PyErr_SetString(PyExc_SyntaxError, 12584737b2348b197d21deffbf12ff23488918e9b9d4Neal Norwitz "invalid expression in parameter list"); 12594737b2348b197d21deffbf12ff23488918e9b9d4Neal Norwitz PyErr_SyntaxLocation(st->st_filename, 12604737b2348b197d21deffbf12ff23488918e9b9d4Neal Norwitz st->st_cur->ste_lineno); 12613e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 12623e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 12633e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 12643e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 12653e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!toplevel) { 12663e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_visit_params_nested(st, args)) 12673e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 12683e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 12693e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 12703e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 12713e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 12723e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 12733e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 12743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_params_nested(struct symtable *st, asdl_seq *args) 12753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 12763e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton int i; 12773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton for (i = 0; i < asdl_seq_LEN(args); i++) { 1278019aec618a24ce0743ddee50a47d7b7413a81fdaAnthony Baxter expr_ty arg = (expr_ty)asdl_seq_GET(args, i); 12793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (arg->kind == Tuple_kind && 12803e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton !symtable_visit_params(st, arg->v.Tuple.elts, 0)) 12813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 12823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 12833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 12843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 12853e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 12863e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 12873e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 12883e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_arguments(struct symtable *st, arguments_ty a) 12893e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 12903e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* skip default arguments inside function block 12913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton XXX should ast be different? 12923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton */ 12933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (a->args && !symtable_visit_params(st, a->args, 1)) 12943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 12953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (a->vararg) { 12963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_add_def(st, a->vararg, DEF_PARAM)) 12973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 12983e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_cur->ste_varargs = 1; 12993e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 13003e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (a->kwarg) { 13013e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_add_def(st, a->kwarg, DEF_PARAM)) 13023e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 13033e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_cur->ste_varkeywords = 1; 13043e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 13053e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (a->args && !symtable_visit_params_nested(st, a->args)) 13063e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 13073e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 13083e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 13093e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 13103e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 13113e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 13123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh) 13133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1314a48f3ab8951fa97844f2a4fa368483af968b7b73Georg Brandl if (eh->v.ExceptHandler.type) 1315a48f3ab8951fa97844f2a4fa368483af968b7b73Georg Brandl VISIT(st, expr, eh->v.ExceptHandler.type); 1316a48f3ab8951fa97844f2a4fa368483af968b7b73Georg Brandl if (eh->v.ExceptHandler.name) 1317a48f3ab8951fa97844f2a4fa368483af968b7b73Georg Brandl VISIT(st, expr, eh->v.ExceptHandler.name); 1318a48f3ab8951fa97844f2a4fa368483af968b7b73Georg Brandl VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body); 13193e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 13203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 13213e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 13223e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 13233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 13243e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_alias(struct symtable *st, alias_ty a) 13253e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 13263e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* Compute store_name, the name actually bound by the import 13273e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton operation. It is diferent than a->name when a->name is a 13283e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton dotted package name (e.g. spam.eggs) 13293e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton */ 13303e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *store_name; 13313e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton PyObject *name = (a->asname == NULL) ? a->name : a->asname; 1332dd96db63f689e2f0d8ae5a1436b3b3395eec7de5Gregory P. Smith const char *base = PyString_AS_STRING(name); 13333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton char *dot = strchr(base, '.'); 13346f5ff3f3eb7abc2f4750c1319b560f67faf546acNeal Norwitz if (dot) { 1335dd96db63f689e2f0d8ae5a1436b3b3395eec7de5Gregory P. Smith store_name = PyString_FromStringAndSize(base, dot - base); 13366f5ff3f3eb7abc2f4750c1319b560f67faf546acNeal Norwitz if (!store_name) 13376f5ff3f3eb7abc2f4750c1319b560f67faf546acNeal Norwitz return 0; 13386f5ff3f3eb7abc2f4750c1319b560f67faf546acNeal Norwitz } 13393e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton else { 13403e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton store_name = name; 13413e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_INCREF(store_name); 13423e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 1343dd96db63f689e2f0d8ae5a1436b3b3395eec7de5Gregory P. Smith if (strcmp(PyString_AS_STRING(name), "*")) { 13443e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton int r = symtable_add_def(st, store_name, DEF_IMPORT); 13453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Py_DECREF(store_name); 13463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return r; 13473e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 13483e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton else { 13493e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (st->st_cur->ste_type != ModuleBlock) { 13505d0ad50f5acf84f2e8a1ca5c6951f333aef0e25aNeal Norwitz int lineno = st->st_cur->ste_lineno; 13515d0ad50f5acf84f2e8a1ca5c6951f333aef0e25aNeal Norwitz if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) { 13523715c3e576a182692cf2ad2d390732126f11780dNeal Norwitz Py_DECREF(store_name); 13533e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 13543715c3e576a182692cf2ad2d390732126f11780dNeal Norwitz } 13553e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 13563e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR; 13574737b2348b197d21deffbf12ff23488918e9b9d4Neal Norwitz Py_DECREF(store_name); 13583e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 13593e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 13603e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 13613e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 13623e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 13633e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 13643e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_comprehension(struct symtable *st, comprehension_ty lc) 13653e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 13663e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, lc->target); 13673e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, lc->iter); 13683e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, expr, lc->ifs); 13693e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 13703e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 13713e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 13723e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 13733e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 13743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_keyword(struct symtable *st, keyword_ty k) 13753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 13763e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, k->value); 13773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 13783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 13793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 13803e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 13813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 13823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_slice(struct symtable *st, slice_ty s) 13833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 13843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton switch (s->kind) { 13853e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Slice_kind: 13863e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (s->v.Slice.lower) 13873e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.Slice.lower) 13883e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (s->v.Slice.upper) 13893e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.Slice.upper) 13903e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (s->v.Slice.step) 13913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.Slice.step) 13923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 13933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case ExtSlice_kind: 13943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ(st, slice, s->v.ExtSlice.dims) 13953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 13963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Index_kind: 13973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, s->v.Index.value) 13983e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 13993e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton case Ellipsis_kind: 14003e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton break; 14013e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 14023e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 1; 14033e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 14043e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 14053e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 14063e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_genexp(struct symtable *st, expr_ty e) 14073e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 14083e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton comprehension_ty outermost = ((comprehension_ty) 14093e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton (asdl_seq_GET(e->v.GeneratorExp.generators, 0))); 14103e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* Outermost iterator is evaluated in current scope */ 14113e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT(st, expr, outermost->iter); 14123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* Create generator scope for the rest */ 14137605936deeeb2fac75b209895717e7798f7a306fNeal Norwitz if (!GET_IDENTIFIER(genexpr) || 14147605936deeeb2fac75b209895717e7798f7a306fNeal Norwitz !symtable_enter_block(st, genexpr, FunctionBlock, (void *)e, 0)) { 14153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 14163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 14173e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton st->st_cur->ste_generator = 1; 14183e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton /* Outermost iter is received as an argument */ 14193e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (!symtable_implicit_arg(st, 0)) { 1420b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz symtable_exit_block(st, (void *)e); 14213e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton return 0; 14223e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 1423b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e); 1424b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e); 1425b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension, 1426b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz e->v.GeneratorExp.generators, 1, (void*)e); 1427b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e); 14287605936deeeb2fac75b209895717e7798f7a306fNeal Norwitz return symtable_exit_block(st, (void *)e); 14293e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 1430