symtable.c revision c6660cf4d68ec17fef0a49aca2cf441c124a88d4
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, 22c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou void *key, int lineno) 23cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton{ 24c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PySTEntryObject *ste = NULL; 25c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *k; 26c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 27c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou k = PyLong_FromVoidPtr(key); 28c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (k == NULL) 29c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto fail; 30c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste = PyObject_New(PySTEntryObject, &PySTEntry_Type); 31c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste == NULL) 32c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto fail; 33c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_table = st; 34c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_id = k; 35c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 36c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_name = name; 37c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_INCREF(name); 38c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 39c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_symbols = NULL; 40c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_varnames = NULL; 41c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_children = NULL; 42c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 43c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_symbols = PyDict_New(); 44c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_symbols == NULL) 45c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto fail; 46c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 47c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_varnames = PyList_New(0); 48c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_varnames == NULL) 49c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto fail; 50c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 51c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_children = PyList_New(0); 52c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_children == NULL) 53c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto fail; 54c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 55c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_type = block; 56c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_unoptimized = 0; 57c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_nested = 0; 58c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_free = 0; 59c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_varargs = 0; 60c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_varkeywords = 0; 61c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_opt_lineno = 0; 62c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_lineno = lineno; 63c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 64c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur != NULL && 65c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (st->st_cur->ste_nested || 66c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_type == FunctionBlock)) 67c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_nested = 1; 68c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_child_free = 0; 69c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_generator = 0; 70c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_returns_value = 0; 71c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 72c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0) 73c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto fail; 74c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 75c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return ste; 76cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton fail: 77c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(ste); 78c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return NULL; 79cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton} 80cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 81cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonstatic PyObject * 823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonste_repr(PySTEntryObject *ste) 83cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton{ 84c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou char buf[256]; 85cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 86c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyOS_snprintf(buf, sizeof(buf), 87c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "<symtable entry %.100s(%ld), line %d>", 88c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyString_AS_STRING(ste->ste_name), 89c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyInt_AS_LONG(ste->ste_id), ste->ste_lineno); 90c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return PyString_FromString(buf); 91cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton} 92cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 93cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonstatic void 943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonste_dealloc(PySTEntryObject *ste) 95cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton{ 96c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_table = NULL; 97c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(ste->ste_id); 98c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(ste->ste_name); 99c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(ste->ste_symbols); 100c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(ste->ste_varnames); 101c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(ste->ste_children); 102c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject_Del(ste); 103cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton} 104cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 1053e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define OFF(x) offsetof(PySTEntryObject, x) 106cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 1076f7993765ac0989b5d13084240797913627a31d8Guido van Rossumstatic PyMemberDef ste_memberlist[] = { 108c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"id", T_OBJECT, OFF(ste_id), READONLY}, 109c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"name", T_OBJECT, OFF(ste_name), READONLY}, 110c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"symbols", T_OBJECT, OFF(ste_symbols), READONLY}, 111c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"varnames", T_OBJECT, OFF(ste_varnames), READONLY}, 112c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"children", T_OBJECT, OFF(ste_children), READONLY}, 113c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"optimized",T_INT, OFF(ste_unoptimized), READONLY}, 114c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"nested", T_INT, OFF(ste_nested), READONLY}, 115c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"type", T_INT, OFF(ste_type), READONLY}, 116c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"lineno", T_INT, OFF(ste_lineno), READONLY}, 117c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {NULL} 118cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton}; 119cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 1203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPyTypeObject PySTEntry_Type = { 121c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyVarObject_HEAD_INIT(&PyType_Type, 0) 122c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "symtable entry", 123c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou sizeof(PySTEntryObject), 124c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, 125c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (destructor)ste_dealloc, /* tp_dealloc */ 126c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_print */ 127c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_getattr */ 128c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_setattr */ 129c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_compare */ 130c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (reprfunc)ste_repr, /* tp_repr */ 131c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_as_number */ 132c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_as_sequence */ 133c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_as_mapping */ 134c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_hash */ 135c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_call */ 136c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_str */ 137c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject_GenericGetAttr, /* tp_getattro */ 138c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_setattro */ 139c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_as_buffer */ 140c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_TPFLAGS_DEFAULT, /* tp_flags */ 141c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_doc */ 142c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_traverse */ 143c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_clear */ 144c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_richcompare */ 145c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_weaklistoffset */ 146c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_iter */ 147c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_iternext */ 148c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_methods */ 149c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste_memberlist, /* tp_members */ 150c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_getset */ 151c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_base */ 152c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_dict */ 153c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_descr_get */ 154c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_descr_set */ 155c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_dictoffset */ 156c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_init */ 157c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_alloc */ 158c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 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); 163c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int symtable_enter_block(struct symtable *st, identifier name, 164c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou _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); 169b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottistatic int symtable_visit_setcomp(struct symtable *st, expr_ty e); 170b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottistatic int symtable_visit_dictcomp(struct symtable *st, expr_ty e); 1713e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_arguments(struct symtable *st, arguments_ty); 1723e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty); 1733e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_alias(struct symtable *st, alias_ty); 1743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_comprehension(struct symtable *st, comprehension_ty); 1753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_keyword(struct symtable *st, keyword_ty); 1763e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_slice(struct symtable *st, slice_ty); 1773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_params(struct symtable *st, asdl_seq *args, int top); 1783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_params_nested(struct symtable *st, asdl_seq *args); 1793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_implicit_arg(struct symtable *st, int pos); 1803e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 182b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottistatic identifier top = NULL, lambda = NULL, genexpr = NULL, setcomp = NULL, 183c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou dictcomp = NULL; 1843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1853e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define GET_IDENTIFIER(VAR) \ 186c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR))) 1873e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1883e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define DUPLICATE_ARGUMENT \ 1893e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton"duplicate argument '%s' in function definition" 1903e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic struct symtable * 1923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_new(void) 1933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 194c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou struct symtable *st; 195c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 196c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable)); 197c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st == NULL) 198c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return NULL; 199c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 200c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_filename = NULL; 201c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_symbols = NULL; 202c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 203c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if ((st->st_stack = PyList_New(0)) == NULL) 204c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto fail; 205c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if ((st->st_symbols = PyDict_New()) == NULL) 206c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto fail; 207c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur = NULL; 208c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_private = NULL; 209c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return st; 2103e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton fail: 211c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PySymtable_Free(st); 212c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return NULL; 2133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 2143e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 2153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstruct symtable * 2163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future) 2173e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 218c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou struct symtable *st = symtable_new(); 219c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou asdl_seq *seq; 220c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i; 221c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 222c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st == NULL) 223c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return st; 224c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_filename = filename; 225c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_future = future; 226c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!GET_IDENTIFIER(top) || 227c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) { 228c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PySymtable_Free(st); 229c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return NULL; 230c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 231c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 232c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_top = st->st_cur; 233c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_unoptimized = OPT_TOPLEVEL; 234c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Any other top-level initialization? */ 235c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou switch (mod->kind) { 236c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Module_kind: 237c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou seq = mod->v.Module.body; 238c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = 0; i < asdl_seq_LEN(seq); i++) 239c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_stmt(st, 240c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (stmt_ty)asdl_seq_GET(seq, i))) 241c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 242c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 243c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Expression_kind: 244c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_expr(st, mod->v.Expression.body)) 245c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 246c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 247c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Interactive_kind: 248c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou seq = mod->v.Interactive.body; 249c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = 0; i < asdl_seq_LEN(seq); i++) 250c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_stmt(st, 251c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (stmt_ty)asdl_seq_GET(seq, i))) 252c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 253c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 254c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Suite_kind: 255c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SetString(PyExc_RuntimeError, 256c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "this compiler does not handle Suites"); 257c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 258c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 259c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_exit_block(st, (void *)mod)) { 260c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PySymtable_Free(st); 261c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return NULL; 262c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 263c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (symtable_analyze(st)) 264c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return st; 265c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PySymtable_Free(st); 266c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return NULL; 2673e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton error: 268c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (void) symtable_exit_block(st, (void *)mod); 269c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PySymtable_Free(st); 270c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return NULL; 2713e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 2723e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 2733e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonvoid 2743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPySymtable_Free(struct symtable *st) 2753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 276c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(st->st_symbols); 277c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(st->st_stack); 278c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyMem_Free((void *)st); 2793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 2803e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 2813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPySTEntryObject * 2823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPySymtable_Lookup(struct symtable *st, void *key) 2833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 284c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *k, *v; 285c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 286c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou k = PyLong_FromVoidPtr(key); 287c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (k == NULL) 288c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return NULL; 289c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou v = PyDict_GetItem(st->st_symbols, k); 290c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (v) { 291c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(PySTEntry_Check(v)); 292c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_INCREF(v); 293c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 294c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou else { 295c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SetString(PyExc_KeyError, 296c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "unknown symbol table entry"); 297c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 298c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 299c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(k); 300c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return (PySTEntryObject *)v; 3013e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 3023e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 303c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrouint 3043e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPyST_GetScope(PySTEntryObject *ste, PyObject *name) 3053e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 306c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *v = PyDict_GetItem(ste->ste_symbols, name); 307c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!v) 308c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 309c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(PyInt_Check(v)); 310c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK; 3113e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 3123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3143e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* Analyze raw symbol information to determine scope of each name. 3153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton The next several functions are helpers for PySymtable_Analyze(), 317c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou which determines whether a name is local, global, or free. In addition, 3183e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton it determines which local variables are cell variables; they provide 319c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou bindings that are used for free variables in enclosed blocks. 3203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 321c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou There are also two kinds of free variables, implicit and explicit. An 3223e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton explicit global is declared with the global statement. An implicit 3233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton global is a free variable for which the compiler has found no binding 3243e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton in an enclosing function scope. The implicit global is either a global 3253e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton or a builtin. Python's module and class blocks use the xxx_NAME opcodes 3263e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton to handle these names to implement slightly odd semantics. In such a 3273e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton block, the name is treated as global until it is assigned to; then it 3283e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton is treated as a local. 3293e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3303e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton The symbol table requires two passes to determine the scope of each name. 331c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou The first pass collects raw facts from the AST: the name is a parameter 3323e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton here, the name is used by not defined here, etc. The second pass analyzes 3333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton these facts during a pass over the PySTEntryObjects created during pass 1. 3343e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton When a function is entered during the second pass, the parent passes 336c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou the set of all name bindings visible to its children. These bindings 3373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton are used to determine if the variable is free or an implicit global. 3383e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton After doing the local analysis, it analyzes each of its child blocks 339c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou using an updated set of name bindings. 3403e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 341c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou The children update the free variable set. If a local variable is free 342c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou in a child, the variable is marked as a cell. The current function must 343c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou provide runtime storage for the variable that may outlive the function's 3443e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton frame. Cell variables are removed from the free set before the analyze 3453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton function returns to its parent. 346c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 3473e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton The sets of bound and free variables are implemented as dictionaries 3483e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton mapping strings to None. 3493e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 3503e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3513e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define SET_SCOPE(DICT, NAME, I) { \ 352c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *o = PyInt_FromLong(I); \ 353c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!o) \ 354c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; \ 355c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem((DICT), (NAME), o) < 0) { \ 356c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(o); \ 357c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; \ 358c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } \ 359c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(o); \ 3603e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 3613e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3623e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* Decide on scope of name, given flags. 3633e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 36488f1c042150466f94784526e14842eb694a54735Jeremy Hylton The namespace dictionaries may be modified to record information 36588f1c042150466f94784526e14842eb694a54735Jeremy Hylton about the new name. For example, a new global will add an entry to 36688f1c042150466f94784526e14842eb694a54735Jeremy Hylton global. A name that was global can be changed to local. 3673e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 3683e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 369c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 37046b7bda9bcd0fb11878a154234c3064e19e35f3cNeal Norwitzanalyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags, 371c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *bound, PyObject *local, PyObject *free, 372c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *global) 3733e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 374c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (flags & DEF_GLOBAL) { 375c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (flags & DEF_PARAM) { 376c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_Format(PyExc_SyntaxError, 377c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "name '%s' is local and global", 378c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyString_AS_STRING(name)); 379c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SyntaxLocation(ste->ste_table->st_filename, 380c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_lineno); 381c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 382c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 383c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 384c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou SET_SCOPE(dict, name, GLOBAL_EXPLICIT); 385c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(global, name, Py_None) < 0) 386c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 387c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (bound && PyDict_GetItem(bound, name)) { 388c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_DelItem(bound, name) < 0) 389c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 390c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 391c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 392c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 393c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (flags & DEF_BOUND) { 394c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou SET_SCOPE(dict, name, LOCAL); 395c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(local, name, Py_None) < 0) 396c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 397c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_GetItem(global, name)) { 398c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_DelItem(global, name) < 0) 399c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 400c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 401c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 402c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 403c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* If an enclosing block has a binding for this name, it 404c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou is a free variable rather than a global variable. 405c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Note that having a non-NULL bound implies that the block 406c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou is nested. 407c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 408c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (bound && PyDict_GetItem(bound, name)) { 409c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou SET_SCOPE(dict, name, FREE); 410c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_free = 1; 411c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(free, name, Py_None) < 0) 412c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 413c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 414c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 415c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* If a parent has a global statement, then call it global 416c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou explicit? It could also be global implicit. 417c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 418c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou else if (global && PyDict_GetItem(global, name)) { 419c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou SET_SCOPE(dict, name, GLOBAL_IMPLICIT); 420c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 421c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 422c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou else { 423c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_nested) 424c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_free = 1; 425c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou SET_SCOPE(dict, name, GLOBAL_IMPLICIT); 426c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 427c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 428c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Should never get here. */ 429c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_Format(PyExc_SystemError, "failed to set scope for %s", 430c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyString_AS_STRING(name)); 431c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 4323e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 4333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4343e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#undef SET_SCOPE 4353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4363e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* If a name is defined in free and also in locals, then this block 4373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton provides the binding for the free variable. The name should be 4383e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton marked CELL in this block and removed from the free list. 4393e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4403e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Note that the current block's free variables are included in free. 4413e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton That's safe because no name can be free and local in the same scope. 4423e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 4433e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4443e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 4453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonanalyze_cells(PyObject *scope, PyObject *free) 4463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 447c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *name, *v, *w; 448c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int success = 0; 449c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_ssize_t pos = 0; 450c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 451c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou w = PyInt_FromLong(CELL); 452c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!w) 453c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 454c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou while (PyDict_Next(scope, &pos, &name, &v)) { 455c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou long flags; 456c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(PyInt_Check(v)); 457c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou flags = PyInt_AS_LONG(v); 458c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (flags != LOCAL) 459c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou continue; 460c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!PyDict_GetItem(free, name)) 461c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou continue; 462c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Replace LOCAL with CELL for this name, and remove 463c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou from free. It is safe to replace the value of name 464c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou in the dict, because it will not cause a resize. 465c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 466c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(scope, name, w) < 0) 467c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 468c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!PyDict_DelItem(free, name) < 0) 469c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 470c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 471c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou success = 1; 4723e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton error: 473c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(w); 474c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return success; 4753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 4763e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* Check for illegal statements in unoptimized namespaces */ 4783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 4793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltoncheck_unoptimized(const PySTEntryObject* ste) { 480c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou char buf[300]; 481c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou const char* trailer; 482c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 483c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized 484c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou || !(ste->ste_free || ste->ste_child_free)) 485c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 486c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 487c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou trailer = (ste->ste_child_free ? 488c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "contains a nested function with free variables" : 489c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "is a nested function"); 490c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 491c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou switch (ste->ste_unoptimized) { 492c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case OPT_TOPLEVEL: /* exec / import * at top-level is fine */ 493c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case OPT_EXEC: /* qualified exec is fine */ 494c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 495c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case OPT_IMPORT_STAR: 496c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyOS_snprintf(buf, sizeof(buf), 497c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "import * is not allowed in function '%.100s' " 498c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "because it %s", 499c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyString_AS_STRING(ste->ste_name), trailer); 500c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 501c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case OPT_BARE_EXEC: 502c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyOS_snprintf(buf, sizeof(buf), 503c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "unqualified exec is not allowed in function " 504c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "'%.100s' it %s", 505c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyString_AS_STRING(ste->ste_name), trailer); 506c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 507c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou default: 508c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyOS_snprintf(buf, sizeof(buf), 509c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "function '%.100s' uses import * and bare exec, " 510c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "which are illegal because it %s", 511c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyString_AS_STRING(ste->ste_name), trailer); 512c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 513c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 514c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 515c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SetString(PyExc_SyntaxError, buf); 516c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SyntaxLocation(ste->ste_table->st_filename, 517c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_opt_lineno); 518c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 5193e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 5203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 521c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou/* Enter the final scope information into the st_symbols dict. 522c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou * 5233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton * All arguments are dicts. Modifies symbols, others are read-only. 5243e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 5253e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 526c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrouupdate_symbols(PyObject *symbols, PyObject *scope, 527019aec618a24ce0743ddee50a47d7b7413a81fdaAnthony Baxter PyObject *bound, PyObject *free, int classflag) 5283e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 529c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *name, *v, *u, *w, *free_value = NULL; 530c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_ssize_t pos = 0; 531c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 532c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou while (PyDict_Next(symbols, &pos, &name, &v)) { 533c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou long i, flags; 534c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(PyInt_Check(v)); 535c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou flags = PyInt_AS_LONG(v); 536c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou w = PyDict_GetItem(scope, name); 537c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(w && PyInt_Check(w)); 538c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou i = PyInt_AS_LONG(w); 539c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou flags |= (i << SCOPE_OFF); 540c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou u = PyInt_FromLong(flags); 541c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!u) 542c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 543c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(symbols, name, u) < 0) { 544c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(u); 545c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 546c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 547c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(u); 548c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 549c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 550c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou free_value = PyInt_FromLong(FREE << SCOPE_OFF); 551c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!free_value) 552c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 553c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 554c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* add a free variable when it's only use is for creating a closure */ 555c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou pos = 0; 556c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou while (PyDict_Next(free, &pos, &name, &v)) { 557c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *o = PyDict_GetItem(symbols, name); 558c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 559c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (o) { 560c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* It could be a free variable in a method of 561c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou the class that has the same name as a local 562c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou or global in the class scope. 563c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 564c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (classflag && 565c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) { 566c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS; 567c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou o = PyInt_FromLong(i); 568c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!o) { 569c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(free_value); 570c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 571c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 572c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(symbols, name, o) < 0) { 573c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(o); 574c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(free_value); 575c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 576c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 577c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(o); 578c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 579c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* else it's not free, probably a cell */ 580c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou continue; 5813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 582c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!PyDict_GetItem(bound, name)) 583c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou continue; /* it's a global */ 584c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 585c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(symbols, name, free_value) < 0) { 586c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(free_value); 587c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 588c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 589c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 590c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(free_value); 591c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 592c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou} 5933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 5943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* Make final symbol table decisions for block of ste. 59588f1c042150466f94784526e14842eb694a54735Jeremy Hylton 5963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Arguments: 5973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste -- current symtable entry (input/output) 59888f1c042150466f94784526e14842eb694a54735Jeremy Hylton bound -- set of variables bound in enclosing scopes (input). bound 59988f1c042150466f94784526e14842eb694a54735Jeremy Hylton is NULL for module blocks. 6003e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton free -- set of free variables in enclosed scopes (output) 6013e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton globals -- set of declared global variables in enclosing scopes (input) 60288f1c042150466f94784526e14842eb694a54735Jeremy Hylton 60388f1c042150466f94784526e14842eb694a54735Jeremy Hylton The implementation uses two mutually recursive functions, 60488f1c042150466f94784526e14842eb694a54735Jeremy Hylton analyze_block() and analyze_child_block(). analyze_block() is 60588f1c042150466f94784526e14842eb694a54735Jeremy Hylton responsible for analyzing the individual names defined in a block. 60688f1c042150466f94784526e14842eb694a54735Jeremy Hylton analyze_child_block() prepares temporary namespace dictionaries 60788f1c042150466f94784526e14842eb694a54735Jeremy Hylton used to evaluated nested blocks. 60888f1c042150466f94784526e14842eb694a54735Jeremy Hylton 60988f1c042150466f94784526e14842eb694a54735Jeremy Hylton The two functions exist because a child block should see the name 61088f1c042150466f94784526e14842eb694a54735Jeremy Hylton bindings of its enclosing blocks, but those bindings should not 61188f1c042150466f94784526e14842eb694a54735Jeremy Hylton propagate back to a parent block. 6123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 6133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 6143e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 615c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrouanalyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free, 616c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *global, PyObject* child_free); 61788f1c042150466f94784526e14842eb694a54735Jeremy Hylton 61888f1c042150466f94784526e14842eb694a54735Jeremy Hyltonstatic int 619c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrouanalyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free, 620c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *global) 6213e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 622c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *name, *v, *local = NULL, *scope = NULL; 623c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *newbound = NULL, *newglobal = NULL; 624c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *newfree = NULL, *allfree = NULL; 625c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i, success = 0; 626c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_ssize_t pos = 0; 627c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 628c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou local = PyDict_New(); /* collect new names bound in block */ 629c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!local) 630c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 631c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou scope = PyDict_New(); /* collect scopes defined for each name */ 632c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!scope) 633c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 634c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 635c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Allocate new global and bound variable dictionaries. These 636c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou dictionaries hold the names visible in nested blocks. For 637c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ClassBlocks, the bound and global names are initialized 638c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou before analyzing names, because class bindings aren't 639c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou visible in methods. For other blocks, they are initialized 640c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou after names are analyzed. 641c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 642c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 643c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* TODO(jhylton): Package these dicts in a struct so that we 644c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou can write reasonable helper functions? 645c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 646c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou newglobal = PyDict_New(); 647c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!newglobal) 648c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 649c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou newbound = PyDict_New(); 650c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!newbound) 651c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 652c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou newfree = PyDict_New(); 653c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!newfree) 654c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 655c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 656c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_type == ClassBlock) { 657c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(newglobal, global) < 0) 658c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 659c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (bound) 660c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(newbound, bound) < 0) 661c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 662c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 663c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 664c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { 665c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou long flags = PyInt_AS_LONG(v); 666c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!analyze_name(ste, scope, name, flags, 667c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou bound, local, free, global)) 668c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 669c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 670c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 671c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_type != ClassBlock) { 672c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_type == FunctionBlock) { 673c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(newbound, local) < 0) 674c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 675c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 676c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (bound) { 677c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(newbound, bound) < 0) 678c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 679c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 680c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(newglobal, global) < 0) 681c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 682c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 683c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 684c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Recursively call analyze_block() on each child block. 685c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 686c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou newbound, newglobal now contain the names visible in 687c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou nested blocks. The free variables in the children will 688c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou be collected in allfree. 689c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 690c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou allfree = PyDict_New(); 691c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!allfree) 692c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 693c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { 694c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *c = PyList_GET_ITEM(ste->ste_children, i); 695c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PySTEntryObject* entry; 696c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(c && PySTEntry_Check(c)); 697c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou entry = (PySTEntryObject*)c; 698c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!analyze_child_block(entry, newbound, newfree, newglobal, 699c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou allfree)) 700c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 701c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (entry->ste_free || entry->ste_child_free) 702c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_child_free = 1; 703c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 704c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 705c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(newfree, allfree) < 0) 706c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 707c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree)) 708c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 709c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!update_symbols(ste->ste_symbols, scope, bound, newfree, 710c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_type == ClassBlock)) 711c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 712c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!check_unoptimized(ste)) 713c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 714c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 715c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(free, newfree) < 0) 716c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 717c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou success = 1; 7183e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton error: 719c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(local); 720c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(scope); 721c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(newbound); 722c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(newglobal); 723c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(newfree); 724c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(allfree); 725c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!success) 726c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(PyErr_Occurred()); 727c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return success; 7283e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 7293e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 7303e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 731c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrouanalyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free, 732c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *global, PyObject* child_free) 73388f1c042150466f94784526e14842eb694a54735Jeremy Hylton{ 734c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL; 735c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 736c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Copy the bound and global dictionaries. 737c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 738c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou These dictionary are used by all blocks enclosed by the 739c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou current block. The analyze_block() call modifies these 740c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou dictionaries. 741c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 742c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 743c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou temp_bound = PyDict_New(); 744c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!temp_bound) 745c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 746c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(temp_bound, bound) < 0) 747c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 748c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou temp_free = PyDict_New(); 749c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!temp_free) 750c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 751c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(temp_free, free) < 0) 752c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 753c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou temp_global = PyDict_New(); 754c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!temp_global) 755c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 756c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(temp_global, global) < 0) 757c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 758c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 759c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!analyze_block(entry, temp_bound, temp_free, temp_global)) 760c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 761c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(child_free, temp_free) < 0) 762c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 763c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(temp_bound); 764c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(temp_free); 765c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(temp_global); 766c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 76788f1c042150466f94784526e14842eb694a54735Jeremy Hylton error: 768c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(temp_bound); 769c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(temp_free); 770c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(temp_global); 771c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 77288f1c042150466f94784526e14842eb694a54735Jeremy Hylton} 77388f1c042150466f94784526e14842eb694a54735Jeremy Hylton 77488f1c042150466f94784526e14842eb694a54735Jeremy Hyltonstatic int 7753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_analyze(struct symtable *st) 7763e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 777c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *free, *global; 778c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int r; 779c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 780c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou free = PyDict_New(); 781c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!free) 782c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 783c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou global = PyDict_New(); 784c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!global) { 785c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(free); 786c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 787c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 788c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou r = analyze_block(st->st_top, NULL, free, global); 789c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(free); 790c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(global); 791c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return r; 7923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 7933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 7943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 7953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 7965d0ad50f5acf84f2e8a1ca5c6951f333aef0e25aNeal Norwitzsymtable_warn(struct symtable *st, char *msg, int lineno) 7973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 798c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename, 799c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou lineno, NULL, NULL) < 0) { 800c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { 801c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SetString(PyExc_SyntaxError, msg); 802c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SyntaxLocation(st->st_filename, 803c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_lineno); 804c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 805c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 806c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 807c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 8083e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 8093e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 810e0d12eb85cff367497090b1fdc58582edc0d4c99Benjamin Peterson/* symtable_enter_block() gets a reference via ste_new. 8113e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton This reference is released when the block is exited, via the DECREF 8123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton in symtable_exit_block(). 8133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 8143e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 8153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 8163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_exit_block(struct symtable *st, void *ast) 8173e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 818c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_ssize_t end; 819c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 820c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_CLEAR(st->st_cur); 821c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou end = PyList_GET_SIZE(st->st_stack) - 1; 822c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (end >= 0) { 823c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, 824c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou end); 825c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur == NULL) 826c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 827c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_INCREF(st->st_cur); 828c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PySequence_DelItem(st->st_stack, end) < 0) 829c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 830c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 831c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 8323e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 8333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 8343e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 835c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrousymtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block, 836c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou void *ast, int lineno) 8373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 838c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PySTEntryObject *prev = NULL; 839c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 840c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur) { 841c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou prev = st->st_cur; 842c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { 843c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 844c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 845c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(st->st_cur); 846c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 847c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur = ste_new(st, name, block, ast, lineno); 848c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur == NULL) 849c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 850c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (name == GET_IDENTIFIER(top)) 851c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_global = st->st_cur->ste_symbols; 852c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (prev) { 853c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyList_Append(prev->ste_children, 854c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (PyObject *)st->st_cur) < 0) { 855c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 856c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 857c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 858c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 8593e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 8603e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 86146b7bda9bcd0fb11878a154234c3064e19e35f3cNeal Norwitzstatic long 8623e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_lookup(struct symtable *st, PyObject *name) 8633e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 864c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *o; 865c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *mangled = _Py_Mangle(st->st_private, name); 866c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!mangled) 867c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 868c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou o = PyDict_GetItem(st->st_cur->ste_symbols, mangled); 869c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(mangled); 870c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!o) 871c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 872c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return PyInt_AsLong(o); 8733e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 8743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 8753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 876c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrousymtable_add_def(struct symtable *st, PyObject *name, int flag) 8773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 878c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *o; 879c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *dict; 880c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou long val; 881c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *mangled = _Py_Mangle(st->st_private, name); 882c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 883c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!mangled) 884c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 885c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou dict = st->st_cur->ste_symbols; 886c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if ((o = PyDict_GetItem(dict, mangled))) { 887c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou val = PyInt_AS_LONG(o); 888c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { 889c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Is it better to use 'mangled' or 'name' here? */ 890c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, 891c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyString_AsString(name)); 892c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SyntaxLocation(st->st_filename, 893c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_lineno); 894c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 895c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 896c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou val |= flag; 897c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } else 898c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou val = flag; 899c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou o = PyInt_FromLong(val); 900c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (o == NULL) 901c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 902c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(dict, mangled, o) < 0) { 903c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(o); 904c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 905c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 906c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(o); 907c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 908c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (flag & DEF_PARAM) { 909c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0) 910c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 911c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } else if (flag & DEF_GLOBAL) { 912c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* XXX need to update DEF_GLOBAL for other flags too; 913c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou perhaps only DEF_FREE_GLOBAL */ 914c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou val = flag; 915c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if ((o = PyDict_GetItem(st->st_global, mangled))) { 916c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou val |= PyInt_AS_LONG(o); 917c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 918c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou o = PyInt_FromLong(val); 9193e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (o == NULL) 920c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 921c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(st->st_global, mangled, o) < 0) { 922c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(o); 923c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 924c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 925c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(o); 926c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 927c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(mangled); 928c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 9298b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer 9308b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauererror: 931c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(mangled); 932c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 9333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 9343e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 9353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument. 9363e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton They use the ASDL name to synthesize the name of the C type and the visit 937c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou function. 938c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 9393e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is 9403e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton useful if the first node in the sequence requires special treatment. 9413e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 9423e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 9433e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define VISIT(ST, TYPE, V) \ 944c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_ ## TYPE((ST), (V))) \ 945c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 946b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 947b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz#define VISIT_IN_BLOCK(ST, TYPE, V, S) \ 948c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_ ## TYPE((ST), (V))) { \ 949c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou symtable_exit_block((ST), (S)); \ 950c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; \ 951c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 952b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 9533e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define VISIT_SEQ(ST, TYPE, SEQ) { \ 954c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i; \ 955c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou asdl_seq *seq = (SEQ); /* avoid variable capture */ \ 956c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = 0; i < asdl_seq_LEN(seq); i++) { \ 957c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ 958c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_ ## TYPE((ST), elt)) \ 959c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; \ 960c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } \ 9613e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 962b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 963b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \ 964c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i; \ 965c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou asdl_seq *seq = (SEQ); /* avoid variable capture */ \ 966c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = 0; i < asdl_seq_LEN(seq); i++) { \ 967c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ 968c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_ ## TYPE((ST), elt)) { \ 969c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou symtable_exit_block((ST), (S)); \ 970c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; \ 971c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } \ 972c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } \ 973b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz} 974b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 9753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \ 976c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i; \ 977c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou asdl_seq *seq = (SEQ); /* avoid variable capture */ \ 978c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = (START); i < asdl_seq_LEN(seq); i++) { \ 979c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ 980c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_ ## TYPE((ST), elt)) \ 981c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; \ 982c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } \ 9833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 984b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 985b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \ 986c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i; \ 987c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou asdl_seq *seq = (SEQ); /* avoid variable capture */ \ 988c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = (START); i < asdl_seq_LEN(seq); i++) { \ 989c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ 990c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_ ## TYPE((ST), elt)) { \ 991c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou symtable_exit_block((ST), (S)); \ 992c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; \ 993c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } \ 994c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } \ 995b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz} 996b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 9973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 9983e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_stmt(struct symtable *st, stmt_ty s) 9993e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1000c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou switch (s->kind) { 1001c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case FunctionDef_kind: 1002c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL)) 1003c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1004c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.FunctionDef.args->defaults) 1005c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults); 1006c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.FunctionDef.decorator_list) 1007c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list); 1008c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_enter_block(st, s->v.FunctionDef.name, 1009c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou FunctionBlock, (void *)s, s->lineno)) 1010c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1011c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s); 1012c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s); 1013c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_exit_block(st, s)) 1014c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1015c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1016c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case ClassDef_kind: { 1017c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *tmp; 1018c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL)) 1019c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1020c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, s->v.ClassDef.bases); 1021c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.ClassDef.decorator_list) 1022c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list); 1023c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock, 1024c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (void *)s, s->lineno)) 1025c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1026c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou tmp = st->st_private; 1027c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_private = s->v.ClassDef.name; 1028c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s); 1029c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_private = tmp; 1030c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_exit_block(st, s)) 1031c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1032c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1033c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1034c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Return_kind: 1035c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Return.value) { 1036c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Return.value); 1037c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_returns_value = 1; 1038c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur->ste_generator) { 1039c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SetString(PyExc_SyntaxError, 1040c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou RETURN_VAL_IN_GENERATOR); 1041c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SyntaxLocation(st->st_filename, 1042c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou s->lineno); 1043c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1044c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1045c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1046c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1047c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Delete_kind: 1048c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, s->v.Delete.targets); 1049c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1050c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Assign_kind: 1051c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, s->v.Assign.targets); 1052c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Assign.value); 1053c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1054c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case AugAssign_kind: 1055c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.AugAssign.target); 1056c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.AugAssign.value); 1057c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1058c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Print_kind: 1059c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Print.dest) 1060c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Print.dest); 1061c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, s->v.Print.values); 1062c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1063c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case For_kind: 1064c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.For.target); 1065c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.For.iter); 1066c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.For.body); 1067c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.For.orelse) 1068c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.For.orelse); 1069c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1070c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case While_kind: 1071c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.While.test); 1072c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.While.body); 1073c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.While.orelse) 1074c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.While.orelse); 1075c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1076c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case If_kind: 1077c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* XXX if 0: and lookup_yield() hacks */ 1078c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.If.test); 1079c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.If.body); 1080c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.If.orelse) 1081c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.If.orelse); 1082c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1083c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Raise_kind: 1084c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Raise.type) { 1085c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Raise.type); 1086c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Raise.inst) { 1087c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Raise.inst); 1088c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Raise.tback) 1089c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Raise.tback); 1090c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1091c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1092c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1093c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case TryExcept_kind: 1094c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.TryExcept.body); 1095c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.TryExcept.orelse); 1096c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers); 1097c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1098c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case TryFinally_kind: 1099c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.TryFinally.body); 1100c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody); 1101c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1102c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Assert_kind: 1103c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Assert.test); 1104c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Assert.msg) 1105c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Assert.msg); 1106c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1107c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Import_kind: 1108c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, alias, s->v.Import.names); 1109c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* XXX Don't have the lineno available inside 1110c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou visit_alias */ 1111c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) 1112c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_opt_lineno = s->lineno; 1113c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1114c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case ImportFrom_kind: 1115c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, alias, s->v.ImportFrom.names); 1116c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* XXX Don't have the lineno available inside 1117c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou visit_alias */ 1118c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) 1119c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_opt_lineno = s->lineno; 1120c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1121c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Exec_kind: 1122c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Exec.body); 1123c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!st->st_cur->ste_opt_lineno) 1124c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_opt_lineno = s->lineno; 1125c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Exec.globals) { 1126c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_unoptimized |= OPT_EXEC; 1127c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Exec.globals); 1128c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Exec.locals) 1129c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Exec.locals); 1130c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } else { 1131c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_unoptimized |= OPT_BARE_EXEC; 1132c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1133c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1134c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Global_kind: { 1135c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i; 1136c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou asdl_seq *seq = s->v.Global.names; 1137c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = 0; i < asdl_seq_LEN(seq); i++) { 1138c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou identifier name = (identifier)asdl_seq_GET(seq, i); 1139c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou char *c_name = PyString_AS_STRING(name); 1140c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou long cur = symtable_lookup(st, name); 1141c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (cur < 0) 1142c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1143c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (cur & (DEF_LOCAL | USE)) { 1144c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou char buf[256]; 1145c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (cur & DEF_LOCAL) 1146c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyOS_snprintf(buf, sizeof(buf), 1147c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou GLOBAL_AFTER_ASSIGN, 1148c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou c_name); 1149c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou else 1150c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyOS_snprintf(buf, sizeof(buf), 1151c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou GLOBAL_AFTER_USE, 1152c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou c_name); 1153c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_warn(st, buf, s->lineno)) 1154c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1155c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1156c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, name, DEF_GLOBAL)) 1157c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1158c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1159c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1160c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1161c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Expr_kind: 1162c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Expr.value); 1163c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1164c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Pass_kind: 1165c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Break_kind: 1166c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Continue_kind: 1167c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* nothing to do here */ 1168c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1169c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case With_kind: 1170c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.With.context_expr); 1171c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.With.optional_vars) { 1172c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.With.optional_vars); 1173c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1174c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.With.body); 1175c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1176c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1177c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 11783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 11793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1180c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 11813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_expr(struct symtable *st, expr_ty e) 11823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1183c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou switch (e->kind) { 1184c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case BoolOp_kind: 1185c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.BoolOp.values); 1186c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1187c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case BinOp_kind: 1188c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.BinOp.left); 1189c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.BinOp.right); 1190c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1191c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case UnaryOp_kind: 1192c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.UnaryOp.operand); 1193c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1194c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Lambda_kind: { 1195c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!GET_IDENTIFIER(lambda)) 1196c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1197c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (e->v.Lambda.args->defaults) 1198c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.Lambda.args->defaults); 1199c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_enter_block(st, lambda, 1200c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou FunctionBlock, (void *)e, e->lineno)) 1201c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1202c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e); 1203c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e); 1204c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_exit_block(st, (void *)e)) 1205c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1206c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1207c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1208c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case IfExp_kind: 1209c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.IfExp.test); 1210c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.IfExp.body); 1211c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.IfExp.orelse); 1212c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1213c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Dict_kind: 1214c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.Dict.keys); 1215c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.Dict.values); 1216c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1217c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Set_kind: 1218c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.Set.elts); 1219c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1220c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case ListComp_kind: 1221c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.ListComp.elt); 1222c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, comprehension, e->v.ListComp.generators); 1223c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1224c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case GeneratorExp_kind: 1225c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_genexp(st, e)) 1226c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1227c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1228c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case SetComp_kind: 1229c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_setcomp(st, e)) 1230c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1231c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1232c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case DictComp_kind: 1233c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_dictcomp(st, e)) 1234c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1235c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1236c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Yield_kind: 1237c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (e->v.Yield.value) 1238c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.Yield.value); 1239c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_generator = 1; 1240c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur->ste_returns_value) { 1241c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SetString(PyExc_SyntaxError, 1242c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou RETURN_VAL_IN_GENERATOR); 1243c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SyntaxLocation(st->st_filename, 1244c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->lineno); 1245c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1246c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1247c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1248c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Compare_kind: 1249c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.Compare.left); 1250c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.Compare.comparators); 1251c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1252c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Call_kind: 1253c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.Call.func); 1254c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.Call.args); 1255c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, keyword, e->v.Call.keywords); 1256c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (e->v.Call.starargs) 1257c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.Call.starargs); 1258c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (e->v.Call.kwargs) 1259c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.Call.kwargs); 1260c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1261c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Repr_kind: 1262c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.Repr.value); 1263c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1264c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Num_kind: 1265c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Str_kind: 1266c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Nothing to do here. */ 1267c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1268c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* The following exprs can be assignment targets. */ 1269c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Attribute_kind: 1270c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.Attribute.value); 1271c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1272c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Subscript_kind: 1273c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.Subscript.value); 1274c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, slice, e->v.Subscript.slice); 1275c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1276c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Name_kind: 1277c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, e->v.Name.id, 1278c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->v.Name.ctx == Load ? USE : DEF_LOCAL)) 1279c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1280c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1281c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* child nodes of List and Tuple will have expr_context set */ 1282c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case List_kind: 1283c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.List.elts); 1284c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1285c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Tuple_kind: 1286c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.Tuple.elts); 1287c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1288c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1289c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 12903e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 12913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 12923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 12933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_implicit_arg(struct symtable *st, int pos) 12943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1295c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *id = PyString_FromFormat(".%d", pos); 1296c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (id == NULL) 1297c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1298c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, id, DEF_PARAM)) { 1299c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(id); 1300c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1301c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1302c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(id); 1303c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 13043e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 13053e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1306c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 13073e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel) 13083e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1309c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i; 1310c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 1311c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* go through all the toplevel arguments first */ 1312c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = 0; i < asdl_seq_LEN(args); i++) { 1313c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou expr_ty arg = (expr_ty)asdl_seq_GET(args, i); 1314c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (arg->kind == Name_kind) { 1315c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(arg->v.Name.ctx == Param || 1316c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (arg->v.Name.ctx == Store && !toplevel)); 1317c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM)) 1318c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1319c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1320c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou else if (arg->kind == Tuple_kind) { 1321c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(arg->v.Tuple.ctx == Store); 1322c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (toplevel) { 1323c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_implicit_arg(st, i)) 1324c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1325c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1326c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1327c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou else { 1328c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SetString(PyExc_SyntaxError, 1329c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "invalid expression in parameter list"); 1330c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SyntaxLocation(st->st_filename, 1331c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_lineno); 1332c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1333c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1334c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1335c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 1336c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!toplevel) { 1337c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_params_nested(st, args)) 1338c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1339c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1340c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 1341c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 13423e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 13433e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 13443e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 13453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_params_nested(struct symtable *st, asdl_seq *args) 13463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1347c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i; 1348c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = 0; i < asdl_seq_LEN(args); i++) { 1349c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou expr_ty arg = (expr_ty)asdl_seq_GET(args, i); 1350c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (arg->kind == Tuple_kind && 1351c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou !symtable_visit_params(st, arg->v.Tuple.elts, 0)) 1352c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1353c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1354c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 1355c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 13563e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 13573e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1358c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 13593e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_arguments(struct symtable *st, arguments_ty a) 13603e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1361c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* skip default arguments inside function block 1362c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou XXX should ast be different? 1363c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 1364c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (a->args && !symtable_visit_params(st, a->args, 1)) 1365c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1366c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (a->vararg) { 1367c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, a->vararg, DEF_PARAM)) 1368c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1369c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_varargs = 1; 1370c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1371c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (a->kwarg) { 1372c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, a->kwarg, DEF_PARAM)) 1373c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1374c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_varkeywords = 1; 1375c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1376c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (a->args && !symtable_visit_params_nested(st, a->args)) 1377c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1378c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 13793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 13803e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 13813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1382c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 13833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh) 13843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1385c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (eh->v.ExceptHandler.type) 1386c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, eh->v.ExceptHandler.type); 1387c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (eh->v.ExceptHandler.name) 1388c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, eh->v.ExceptHandler.name); 1389c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body); 1390c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 13913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 13923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 13933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1394c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 13953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_alias(struct symtable *st, alias_ty a) 13963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1397c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Compute store_name, the name actually bound by the import 1398c6660cf4d68ec17fef0a49aca2cf441c124a88d4Benjamin Peterson operation. It is different than a->name when a->name is a 1399c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou dotted package name (e.g. spam.eggs) 1400c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 1401c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *store_name; 1402c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *name = (a->asname == NULL) ? a->name : a->asname; 1403c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou const char *base = PyString_AS_STRING(name); 1404c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou char *dot = strchr(base, '.'); 1405c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (dot) { 1406c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou store_name = PyString_FromStringAndSize(base, dot - base); 1407c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!store_name) 1408c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1409c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1410c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou else { 1411c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou store_name = name; 1412c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_INCREF(store_name); 1413c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1414c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (strcmp(PyString_AS_STRING(name), "*")) { 1415c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int r = symtable_add_def(st, store_name, DEF_IMPORT); 1416c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(store_name); 1417c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return r; 1418c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1419c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou else { 1420c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur->ste_type != ModuleBlock) { 1421c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int lineno = st->st_cur->ste_lineno; 1422c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) { 1423c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(store_name); 1424c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1425c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1426c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1427c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR; 1428c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(store_name); 1429c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 1430c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 14313e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 14323e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 14333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1434c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 14353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_comprehension(struct symtable *st, comprehension_ty lc) 14363e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1437c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, lc->target); 1438c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, lc->iter); 1439c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, lc->ifs); 1440c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 14413e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 14423e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 14433e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1444c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 14453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_keyword(struct symtable *st, keyword_ty k) 14463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1447c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, k->value); 1448c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 14493e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 14503e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 14513e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1452c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 14533e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_slice(struct symtable *st, slice_ty s) 14543e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1455c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou switch (s->kind) { 1456c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Slice_kind: 1457c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Slice.lower) 1458c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Slice.lower) 1459c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Slice.upper) 1460c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Slice.upper) 1461c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Slice.step) 1462c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Slice.step) 1463c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1464c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case ExtSlice_kind: 1465c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, slice, s->v.ExtSlice.dims) 1466c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1467c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Index_kind: 1468c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Index.value) 1469c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1470c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Ellipsis_kind: 1471c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1472c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1473c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 14743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 14753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1476b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottistatic int 1477b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottisymtable_new_tmpname(struct symtable *st) 1478b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti{ 1479c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou char tmpname[256]; 1480c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou identifier tmp; 1481c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 1482c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", 1483c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ++st->st_cur->ste_tmpname); 1484c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou tmp = PyString_InternFromString(tmpname); 1485c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!tmp) 1486c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1487c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, tmp, DEF_LOCAL)) 1488c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1489c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(tmp); 1490c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 1491b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti} 1492b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti 1493c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 1494b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottisymtable_handle_comprehension(struct symtable *st, expr_ty e, 1495b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti identifier scope_name, asdl_seq *generators, 1496b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti expr_ty elt, expr_ty value) 14973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1498c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int is_generator = (e->kind == GeneratorExp_kind); 1499c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int needs_tmp = !is_generator; 1500c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou comprehension_ty outermost = ((comprehension_ty) 1501c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou asdl_seq_GET(generators, 0)); 1502c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Outermost iterator is evaluated in current scope */ 1503c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, outermost->iter); 1504c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Create comprehension scope for the rest */ 1505c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!scope_name || 1506c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) { 1507c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1508c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1509c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_generator = is_generator; 1510c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Outermost iter is received as an argument */ 1511c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_implicit_arg(st, 0)) { 1512c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou symtable_exit_block(st, (void *)e); 1513c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1514c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1515c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Allocate temporary name if needed */ 1516c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (needs_tmp && !symtable_new_tmpname(st)) { 1517c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou symtable_exit_block(st, (void *)e); 1518c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1519c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1520c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e); 1521c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e); 1522c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension, 1523c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou generators, 1, (void*)e); 1524c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (value) 1525c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_IN_BLOCK(st, expr, value, (void*)e); 1526c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_IN_BLOCK(st, expr, elt, (void*)e); 1527c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return symtable_exit_block(st, (void *)e); 15283e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 1529b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti 1530c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 1531b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottisymtable_visit_genexp(struct symtable *st, expr_ty e) 1532b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti{ 1533c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr), 1534c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->v.GeneratorExp.generators, 1535c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->v.GeneratorExp.elt, NULL); 1536b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti} 1537b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti 1538b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottistatic int 1539b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottisymtable_visit_setcomp(struct symtable *st, expr_ty e) 1540b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti{ 1541c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp), 1542c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->v.SetComp.generators, 1543c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->v.SetComp.elt, NULL); 1544b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti} 1545b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti 1546b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottistatic int 1547b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottisymtable_visit_dictcomp(struct symtable *st, expr_ty e) 1548b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti{ 1549c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp), 1550c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->v.DictComp.generators, 1551c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->v.DictComp.key, 1552c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->v.DictComp.value); 1553b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti} 1554