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; 258c1bce00d25ddda20d4e760e5771324865ddae17Christian Heimes PyObject *k = NULL; 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); 31dfaf90da9c4a9a4f7dd7bd716fa0695d32bbd91fChristian Heimes if (ste == NULL) { 32dfaf90da9c4a9a4f7dd7bd716fa0695d32bbd91fChristian Heimes Py_DECREF(k); 33c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto fail; 34dfaf90da9c4a9a4f7dd7bd716fa0695d32bbd91fChristian Heimes } 35c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_table = st; 3665a0141e146aba6e2ae2b82d39d016ab03b8c7e2Christian Heimes ste->ste_id = k; /* ste owns reference to k */ 37c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 38c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_name = name; 39c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_INCREF(name); 40c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 41c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_symbols = NULL; 42c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_varnames = NULL; 43c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_children = NULL; 44c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 45c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_symbols = PyDict_New(); 46c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_symbols == NULL) 47c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto fail; 48c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 49c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_varnames = PyList_New(0); 50c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_varnames == NULL) 51c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto fail; 52c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 53c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_children = PyList_New(0); 54c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_children == NULL) 55c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto fail; 56c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 57c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_type = block; 58c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_unoptimized = 0; 59c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_nested = 0; 60c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_free = 0; 61c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_varargs = 0; 62c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_varkeywords = 0; 63c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_opt_lineno = 0; 64045bbcdc8e1c745e49d3f4b0d79ca8182375bce0Benjamin Peterson ste->ste_tmpname = 0; 65c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_lineno = lineno; 66c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 67c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur != NULL && 68c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (st->st_cur->ste_nested || 69c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_type == FunctionBlock)) 70c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_nested = 1; 71c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_child_free = 0; 72c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_generator = 0; 73c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_returns_value = 0; 74c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 75c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0) 76c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto fail; 77c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 78c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return ste; 79cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton fail: 80c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(ste); 81c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return NULL; 82cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton} 83cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 84cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonstatic PyObject * 853e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonste_repr(PySTEntryObject *ste) 86cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton{ 87c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou char buf[256]; 88cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 89c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyOS_snprintf(buf, sizeof(buf), 90c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "<symtable entry %.100s(%ld), line %d>", 91c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyString_AS_STRING(ste->ste_name), 92c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyInt_AS_LONG(ste->ste_id), ste->ste_lineno); 93c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return PyString_FromString(buf); 94cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton} 95cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 96cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonstatic void 973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonste_dealloc(PySTEntryObject *ste) 98cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton{ 99c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_table = NULL; 100c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(ste->ste_id); 101c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(ste->ste_name); 102c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(ste->ste_symbols); 103c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(ste->ste_varnames); 104c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(ste->ste_children); 105c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject_Del(ste); 106cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton} 107cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 1083e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define OFF(x) offsetof(PySTEntryObject, x) 109cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 1106f7993765ac0989b5d13084240797913627a31d8Guido van Rossumstatic PyMemberDef ste_memberlist[] = { 111c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"id", T_OBJECT, OFF(ste_id), READONLY}, 112c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"name", T_OBJECT, OFF(ste_name), READONLY}, 113c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"symbols", T_OBJECT, OFF(ste_symbols), READONLY}, 114c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"varnames", T_OBJECT, OFF(ste_varnames), READONLY}, 115c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"children", T_OBJECT, OFF(ste_children), READONLY}, 116c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"optimized",T_INT, OFF(ste_unoptimized), READONLY}, 117c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"nested", T_INT, OFF(ste_nested), READONLY}, 118c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"type", T_INT, OFF(ste_type), READONLY}, 119c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {"lineno", T_INT, OFF(ste_lineno), READONLY}, 120c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou {NULL} 121cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton}; 122cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton 1233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPyTypeObject PySTEntry_Type = { 124c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyVarObject_HEAD_INIT(&PyType_Type, 0) 125c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "symtable entry", 126c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou sizeof(PySTEntryObject), 127c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, 128c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (destructor)ste_dealloc, /* tp_dealloc */ 129c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_print */ 130c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_getattr */ 131c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_setattr */ 132c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_compare */ 133c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (reprfunc)ste_repr, /* tp_repr */ 134c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_as_number */ 135c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_as_sequence */ 136c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_as_mapping */ 137c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_hash */ 138c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_call */ 139c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_str */ 140c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject_GenericGetAttr, /* tp_getattro */ 141c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_setattro */ 142c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_as_buffer */ 143c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_TPFLAGS_DEFAULT, /* tp_flags */ 144c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_doc */ 145c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_traverse */ 146c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_clear */ 147c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_richcompare */ 148c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_weaklistoffset */ 149c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_iter */ 150c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_iternext */ 151c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_methods */ 152c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste_memberlist, /* tp_members */ 153c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_getset */ 154c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_base */ 155c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_dict */ 156c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_descr_get */ 157c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_descr_set */ 158c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_dictoffset */ 159c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_init */ 160c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_alloc */ 161c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 0, /* tp_new */ 162cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton}; 1633e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1643e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_analyze(struct symtable *st); 1655d0ad50f5acf84f2e8a1ca5c6951f333aef0e25aNeal Norwitzstatic int symtable_warn(struct symtable *st, char *msg, int lineno); 166c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int symtable_enter_block(struct symtable *st, identifier name, 167c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou _Py_block_ty block, void *ast, int lineno); 1683e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_exit_block(struct symtable *st, void *ast); 1693e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_stmt(struct symtable *st, stmt_ty s); 1703e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_expr(struct symtable *st, expr_ty s); 1713e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_genexp(struct symtable *st, expr_ty s); 172b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottistatic int symtable_visit_setcomp(struct symtable *st, expr_ty e); 173b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottistatic int symtable_visit_dictcomp(struct symtable *st, expr_ty e); 1743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_arguments(struct symtable *st, arguments_ty); 1753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty); 1763e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_alias(struct symtable *st, alias_ty); 1773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_comprehension(struct symtable *st, comprehension_ty); 1783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_keyword(struct symtable *st, keyword_ty); 1793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_slice(struct symtable *st, slice_ty); 1803e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_params(struct symtable *st, asdl_seq *args, int top); 1813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_visit_params_nested(struct symtable *st, asdl_seq *args); 1823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int symtable_implicit_arg(struct symtable *st, int pos); 1833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 185b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottistatic identifier top = NULL, lambda = NULL, genexpr = NULL, setcomp = NULL, 186c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou dictcomp = NULL; 1873e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1883e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define GET_IDENTIFIER(VAR) \ 189c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR))) 1903e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1913e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define DUPLICATE_ARGUMENT \ 1923e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton"duplicate argument '%s' in function definition" 1933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic struct symtable * 1953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_new(void) 1963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 197c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou struct symtable *st; 198c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 199c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable)); 200c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st == NULL) 201c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return NULL; 202c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 203c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_filename = NULL; 204c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_symbols = NULL; 205c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 206c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if ((st->st_stack = PyList_New(0)) == NULL) 207c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto fail; 208c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if ((st->st_symbols = PyDict_New()) == NULL) 209c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto fail; 210c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur = NULL; 211c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_private = NULL; 212c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return st; 2133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton fail: 214c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PySymtable_Free(st); 215c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return NULL; 2163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 2173e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 2183e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstruct symtable * 2193e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future) 2203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 221c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou struct symtable *st = symtable_new(); 222c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou asdl_seq *seq; 223c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i; 224c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 225c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st == NULL) 226c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return st; 227c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_filename = filename; 228c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_future = future; 229c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!GET_IDENTIFIER(top) || 230c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) { 231c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PySymtable_Free(st); 232c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return NULL; 233c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 234c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 235c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_top = st->st_cur; 236c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_unoptimized = OPT_TOPLEVEL; 237c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Any other top-level initialization? */ 238c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou switch (mod->kind) { 239c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Module_kind: 240c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou seq = mod->v.Module.body; 241c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = 0; i < asdl_seq_LEN(seq); i++) 242c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_stmt(st, 243c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (stmt_ty)asdl_seq_GET(seq, i))) 244c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 245c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 246c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Expression_kind: 247c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_expr(st, mod->v.Expression.body)) 248c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 249c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 250c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Interactive_kind: 251c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou seq = mod->v.Interactive.body; 252c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = 0; i < asdl_seq_LEN(seq); i++) 253c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_stmt(st, 254c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (stmt_ty)asdl_seq_GET(seq, i))) 255c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 256c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 257c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Suite_kind: 258c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SetString(PyExc_RuntimeError, 259c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "this compiler does not handle Suites"); 260c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 261c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 262c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_exit_block(st, (void *)mod)) { 263c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PySymtable_Free(st); 264c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return NULL; 265c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 266c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (symtable_analyze(st)) 267c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return st; 268c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PySymtable_Free(st); 269c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return NULL; 2703e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton error: 271c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (void) symtable_exit_block(st, (void *)mod); 272c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PySymtable_Free(st); 273c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return NULL; 2743e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 2753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 2763e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonvoid 2773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPySymtable_Free(struct symtable *st) 2783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 279c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(st->st_symbols); 280c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(st->st_stack); 281c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyMem_Free((void *)st); 2823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 2833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 2843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPySTEntryObject * 2853e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPySymtable_Lookup(struct symtable *st, void *key) 2863e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 287c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *k, *v; 288c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 289c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou k = PyLong_FromVoidPtr(key); 290c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (k == NULL) 291c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return NULL; 292c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou v = PyDict_GetItem(st->st_symbols, k); 293c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (v) { 294c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(PySTEntry_Check(v)); 295c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_INCREF(v); 296c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 297c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou else { 298c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SetString(PyExc_KeyError, 299c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "unknown symbol table entry"); 300c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 301c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 302c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(k); 303c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return (PySTEntryObject *)v; 3043e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 3053e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 306c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrouint 3073e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPyST_GetScope(PySTEntryObject *ste, PyObject *name) 3083e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 309c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *v = PyDict_GetItem(ste->ste_symbols, name); 310c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!v) 311c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 312c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(PyInt_Check(v)); 313c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK; 3143e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 3153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3173e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* Analyze raw symbol information to determine scope of each name. 3183e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3193e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton The next several functions are helpers for PySymtable_Analyze(), 320c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou which determines whether a name is local, global, or free. In addition, 3213e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton it determines which local variables are cell variables; they provide 322c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou bindings that are used for free variables in enclosed blocks. 3233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 324c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou There are also two kinds of free variables, implicit and explicit. An 3253e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton explicit global is declared with the global statement. An implicit 3263e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton global is a free variable for which the compiler has found no binding 3273e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton in an enclosing function scope. The implicit global is either a global 3283e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton or a builtin. Python's module and class blocks use the xxx_NAME opcodes 3293e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton to handle these names to implement slightly odd semantics. In such a 3303e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton block, the name is treated as global until it is assigned to; then it 3313e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton is treated as a local. 3323e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton The symbol table requires two passes to determine the scope of each name. 334c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou The first pass collects raw facts from the AST: the name is a parameter 3353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton here, the name is used by not defined here, etc. The second pass analyzes 3363e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton these facts during a pass over the PySTEntryObjects created during pass 1. 3373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3383e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton When a function is entered during the second pass, the parent passes 339c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou the set of all name bindings visible to its children. These bindings 3403e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton are used to determine if the variable is free or an implicit global. 3413e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton After doing the local analysis, it analyzes each of its child blocks 342c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou using an updated set of name bindings. 3433e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 344c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou The children update the free variable set. If a local variable is free 345c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou in a child, the variable is marked as a cell. The current function must 346c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou provide runtime storage for the variable that may outlive the function's 3473e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton frame. Cell variables are removed from the free set before the analyze 3483e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton function returns to its parent. 349c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 3503e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton The sets of bound and free variables are implemented as dictionaries 3513e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton mapping strings to None. 3523e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 3533e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3543e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define SET_SCOPE(DICT, NAME, I) { \ 355c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *o = PyInt_FromLong(I); \ 356c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!o) \ 357c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; \ 358c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem((DICT), (NAME), o) < 0) { \ 359c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(o); \ 360c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; \ 361c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } \ 362c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(o); \ 3633e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 3643e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 3653e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* Decide on scope of name, given flags. 3663e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 36788f1c042150466f94784526e14842eb694a54735Jeremy Hylton The namespace dictionaries may be modified to record information 36888f1c042150466f94784526e14842eb694a54735Jeremy Hylton about the new name. For example, a new global will add an entry to 36988f1c042150466f94784526e14842eb694a54735Jeremy Hylton global. A name that was global can be changed to local. 3703e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 3713e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 372c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 37346b7bda9bcd0fb11878a154234c3064e19e35f3cNeal Norwitzanalyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags, 374c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *bound, PyObject *local, PyObject *free, 375c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *global) 3763e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 377c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (flags & DEF_GLOBAL) { 378c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (flags & DEF_PARAM) { 379c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_Format(PyExc_SyntaxError, 380c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "name '%s' is local and global", 381c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyString_AS_STRING(name)); 382c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SyntaxLocation(ste->ste_table->st_filename, 383c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_lineno); 384c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 385c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 386c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 387c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou SET_SCOPE(dict, name, GLOBAL_EXPLICIT); 388c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(global, name, Py_None) < 0) 389c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 390c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (bound && PyDict_GetItem(bound, name)) { 391c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_DelItem(bound, name) < 0) 392c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 393c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 394c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 395c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 396c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (flags & DEF_BOUND) { 397c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou SET_SCOPE(dict, name, LOCAL); 398c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(local, name, Py_None) < 0) 399c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 400c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_GetItem(global, name)) { 401c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_DelItem(global, name) < 0) 402c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 403c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 404c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 405c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 406c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* If an enclosing block has a binding for this name, it 407c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou is a free variable rather than a global variable. 408c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Note that having a non-NULL bound implies that the block 409c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou is nested. 410c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 411c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (bound && PyDict_GetItem(bound, name)) { 412c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou SET_SCOPE(dict, name, FREE); 413c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_free = 1; 414c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(free, name, Py_None) < 0) 415c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 416c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 417c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 418c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* If a parent has a global statement, then call it global 419c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou explicit? It could also be global implicit. 420c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 421c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou else if (global && PyDict_GetItem(global, name)) { 422c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou SET_SCOPE(dict, name, GLOBAL_IMPLICIT); 423c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 424c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 425c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou else { 426c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_nested) 427c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_free = 1; 428c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou SET_SCOPE(dict, name, GLOBAL_IMPLICIT); 429c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 430c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 431c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Should never get here. */ 432c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_Format(PyExc_SystemError, "failed to set scope for %s", 433c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyString_AS_STRING(name)); 434c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 4353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 4363e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#undef SET_SCOPE 4383e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4393e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* If a name is defined in free and also in locals, then this block 4403e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton provides the binding for the free variable. The name should be 4413e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton marked CELL in this block and removed from the free list. 4423e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4433e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Note that the current block's free variables are included in free. 4443e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton That's safe because no name can be free and local in the same scope. 4453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 4463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4473e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 4483e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonanalyze_cells(PyObject *scope, PyObject *free) 4493e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 450c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *name, *v, *w; 451c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int success = 0; 452c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_ssize_t pos = 0; 453c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 454c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou w = PyInt_FromLong(CELL); 455c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!w) 456c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 457c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou while (PyDict_Next(scope, &pos, &name, &v)) { 458c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou long flags; 459c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(PyInt_Check(v)); 460c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou flags = PyInt_AS_LONG(v); 461c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (flags != LOCAL) 462c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou continue; 463c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!PyDict_GetItem(free, name)) 464c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou continue; 465c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Replace LOCAL with CELL for this name, and remove 466c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou from free. It is safe to replace the value of name 467c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou in the dict, because it will not cause a resize. 468c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 469c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(scope, name, w) < 0) 470c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 4718363f77795503e5352d983be844f156f4f495458Benjamin Peterson if (PyDict_DelItem(free, name) < 0) 472c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 473c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 474c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou success = 1; 4753e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton error: 476c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(w); 477c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return success; 4783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 4793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 4803e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* Check for illegal statements in unoptimized namespaces */ 4813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 4823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltoncheck_unoptimized(const PySTEntryObject* ste) { 483c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou char buf[300]; 484c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou const char* trailer; 485c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 486c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized 487c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou || !(ste->ste_free || ste->ste_child_free)) 488c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 489c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 490c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou trailer = (ste->ste_child_free ? 491c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "contains a nested function with free variables" : 492c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "is a nested function"); 493c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 494c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou switch (ste->ste_unoptimized) { 495c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case OPT_TOPLEVEL: /* exec / import * at top-level is fine */ 496c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case OPT_EXEC: /* qualified exec is fine */ 497c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 498c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case OPT_IMPORT_STAR: 499c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyOS_snprintf(buf, sizeof(buf), 500c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "import * is not allowed in function '%.100s' " 501c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "because it %s", 502c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyString_AS_STRING(ste->ste_name), trailer); 503c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 504c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case OPT_BARE_EXEC: 505c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyOS_snprintf(buf, sizeof(buf), 506c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "unqualified exec is not allowed in function " 507ee5729dbf978c9d1ee78974b84be9cd02a2bc117Benjamin Peterson "'%.100s' because it %s", 508c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyString_AS_STRING(ste->ste_name), trailer); 509c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 510c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou default: 511c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyOS_snprintf(buf, sizeof(buf), 512c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "function '%.100s' uses import * and bare exec, " 513c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "which are illegal because it %s", 514c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyString_AS_STRING(ste->ste_name), trailer); 515c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 516c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 517c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 518c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SetString(PyExc_SyntaxError, buf); 519c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SyntaxLocation(ste->ste_table->st_filename, 520c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_opt_lineno); 521c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 5223e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 5233e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 524c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou/* Enter the final scope information into the st_symbols dict. 525c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou * 5263e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton * All arguments are dicts. Modifies symbols, others are read-only. 5273e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 5283e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 529c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrouupdate_symbols(PyObject *symbols, PyObject *scope, 530019aec618a24ce0743ddee50a47d7b7413a81fdaAnthony Baxter PyObject *bound, PyObject *free, int classflag) 5313e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 532c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *name, *v, *u, *w, *free_value = NULL; 533c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_ssize_t pos = 0; 534c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 535c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou while (PyDict_Next(symbols, &pos, &name, &v)) { 536c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou long i, flags; 537c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(PyInt_Check(v)); 538c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou flags = PyInt_AS_LONG(v); 539c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou w = PyDict_GetItem(scope, name); 540c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(w && PyInt_Check(w)); 541c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou i = PyInt_AS_LONG(w); 542c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou flags |= (i << SCOPE_OFF); 543c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou u = PyInt_FromLong(flags); 544c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!u) 545c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 546c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(symbols, name, u) < 0) { 547c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(u); 548c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 549c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 550c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(u); 551c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 552c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 553c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou free_value = PyInt_FromLong(FREE << SCOPE_OFF); 554c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!free_value) 555c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 556c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 557c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* add a free variable when it's only use is for creating a closure */ 558c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou pos = 0; 559c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou while (PyDict_Next(free, &pos, &name, &v)) { 560c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *o = PyDict_GetItem(symbols, name); 561c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 562c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (o) { 563c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* It could be a free variable in a method of 564c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou the class that has the same name as a local 565c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou or global in the class scope. 566c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 567c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (classflag && 568c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) { 569c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS; 570c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou o = PyInt_FromLong(i); 571c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!o) { 572c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(free_value); 573c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 574c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 575c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(symbols, name, o) < 0) { 576c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(o); 577c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(free_value); 578c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 579c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 580c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(o); 581c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 582c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* else it's not free, probably a cell */ 583c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou continue; 5843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton } 585c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!PyDict_GetItem(bound, name)) 586c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou continue; /* it's a global */ 587c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 588c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(symbols, name, free_value) < 0) { 589c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(free_value); 590c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 591c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 592c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 593c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(free_value); 594c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 595c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou} 5963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 5973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* Make final symbol table decisions for block of ste. 59888f1c042150466f94784526e14842eb694a54735Jeremy Hylton 5993e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton Arguments: 6003e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton ste -- current symtable entry (input/output) 60188f1c042150466f94784526e14842eb694a54735Jeremy Hylton bound -- set of variables bound in enclosing scopes (input). bound 60288f1c042150466f94784526e14842eb694a54735Jeremy Hylton is NULL for module blocks. 6033e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton free -- set of free variables in enclosed scopes (output) 6043e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton globals -- set of declared global variables in enclosing scopes (input) 60588f1c042150466f94784526e14842eb694a54735Jeremy Hylton 60688f1c042150466f94784526e14842eb694a54735Jeremy Hylton The implementation uses two mutually recursive functions, 60788f1c042150466f94784526e14842eb694a54735Jeremy Hylton analyze_block() and analyze_child_block(). analyze_block() is 60888f1c042150466f94784526e14842eb694a54735Jeremy Hylton responsible for analyzing the individual names defined in a block. 60988f1c042150466f94784526e14842eb694a54735Jeremy Hylton analyze_child_block() prepares temporary namespace dictionaries 61088f1c042150466f94784526e14842eb694a54735Jeremy Hylton used to evaluated nested blocks. 61188f1c042150466f94784526e14842eb694a54735Jeremy Hylton 61288f1c042150466f94784526e14842eb694a54735Jeremy Hylton The two functions exist because a child block should see the name 61388f1c042150466f94784526e14842eb694a54735Jeremy Hylton bindings of its enclosing blocks, but those bindings should not 61488f1c042150466f94784526e14842eb694a54735Jeremy Hylton propagate back to a parent block. 6153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 6163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 6173e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 618c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrouanalyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free, 619c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *global, PyObject* child_free); 62088f1c042150466f94784526e14842eb694a54735Jeremy Hylton 62188f1c042150466f94784526e14842eb694a54735Jeremy Hyltonstatic int 622c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrouanalyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free, 623c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *global) 6243e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 625c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *name, *v, *local = NULL, *scope = NULL; 626c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *newbound = NULL, *newglobal = NULL; 627c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *newfree = NULL, *allfree = NULL; 628c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i, success = 0; 629c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_ssize_t pos = 0; 630c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 631c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou local = PyDict_New(); /* collect new names bound in block */ 632c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!local) 633c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 634c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou scope = PyDict_New(); /* collect scopes defined for each name */ 635c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!scope) 636c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 637c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 638c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Allocate new global and bound variable dictionaries. These 639c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou dictionaries hold the names visible in nested blocks. For 640c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ClassBlocks, the bound and global names are initialized 641c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou before analyzing names, because class bindings aren't 642c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou visible in methods. For other blocks, they are initialized 643c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou after names are analyzed. 644c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 645c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 646c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* TODO(jhylton): Package these dicts in a struct so that we 647c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou can write reasonable helper functions? 648c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 649c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou newglobal = PyDict_New(); 650c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!newglobal) 651c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 652c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou newbound = PyDict_New(); 653c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!newbound) 654c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 655c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou newfree = PyDict_New(); 656c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!newfree) 657c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 658c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 659c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_type == ClassBlock) { 660c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(newglobal, global) < 0) 661c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 662c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (bound) 663c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(newbound, bound) < 0) 664c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 665c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 666c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 667c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { 668c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou long flags = PyInt_AS_LONG(v); 669c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!analyze_name(ste, scope, name, flags, 670c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou bound, local, free, global)) 671c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 672c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 673c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 674c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_type != ClassBlock) { 675c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_type == FunctionBlock) { 676c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(newbound, local) < 0) 677c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 678c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 679c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (bound) { 680c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(newbound, bound) < 0) 681c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 682c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 683c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(newglobal, global) < 0) 684c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 685c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 686c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 687c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Recursively call analyze_block() on each child block. 688c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 689c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou newbound, newglobal now contain the names visible in 690c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou nested blocks. The free variables in the children will 691c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou be collected in allfree. 692c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 693c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou allfree = PyDict_New(); 694c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!allfree) 695c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 696c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { 697c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *c = PyList_GET_ITEM(ste->ste_children, i); 698c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PySTEntryObject* entry; 699c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(c && PySTEntry_Check(c)); 700c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou entry = (PySTEntryObject*)c; 701c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!analyze_child_block(entry, newbound, newfree, newglobal, 702c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou allfree)) 703c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 704c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (entry->ste_free || entry->ste_child_free) 705c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_child_free = 1; 706c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 707c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 708c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(newfree, allfree) < 0) 709c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 710c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree)) 711c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 712c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!update_symbols(ste->ste_symbols, scope, bound, newfree, 713c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ste->ste_type == ClassBlock)) 714c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 715c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!check_unoptimized(ste)) 716c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 717c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 718c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(free, newfree) < 0) 719c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 720c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou success = 1; 7213e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton error: 722c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(local); 723c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(scope); 724c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(newbound); 725c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(newglobal); 726c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(newfree); 727c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(allfree); 728c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!success) 729c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(PyErr_Occurred()); 730c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return success; 7313e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 7323e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 7333e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 734c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrouanalyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free, 735c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *global, PyObject* child_free) 73688f1c042150466f94784526e14842eb694a54735Jeremy Hylton{ 737c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL; 738c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 739c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Copy the bound and global dictionaries. 740c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 741b5f487a0902c82a914cd2398ffd1b5e57715431aMartin Panter These dictionaries are used by all blocks enclosed by the 742c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou current block. The analyze_block() call modifies these 743c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou dictionaries. 744c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 745c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 746c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou temp_bound = PyDict_New(); 747c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!temp_bound) 748c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 749c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(temp_bound, bound) < 0) 750c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 751c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou temp_free = PyDict_New(); 752c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!temp_free) 753c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 754c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(temp_free, free) < 0) 755c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 756c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou temp_global = PyDict_New(); 757c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!temp_global) 758c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 759c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(temp_global, global) < 0) 760c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 761c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 762c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!analyze_block(entry, temp_bound, temp_free, temp_global)) 763c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 764c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_Update(child_free, temp_free) < 0) 765c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 766c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(temp_bound); 767c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(temp_free); 768c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(temp_global); 769c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 77088f1c042150466f94784526e14842eb694a54735Jeremy Hylton error: 771c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(temp_bound); 772c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(temp_free); 773c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_XDECREF(temp_global); 774c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 77588f1c042150466f94784526e14842eb694a54735Jeremy Hylton} 77688f1c042150466f94784526e14842eb694a54735Jeremy Hylton 77788f1c042150466f94784526e14842eb694a54735Jeremy Hyltonstatic int 7783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_analyze(struct symtable *st) 7793e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 780c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *free, *global; 781c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int r; 782c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 783c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou free = PyDict_New(); 784c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!free) 785c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 786c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou global = PyDict_New(); 787c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!global) { 788c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(free); 789c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 790c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 791c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou r = analyze_block(st->st_top, NULL, free, global); 792c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(free); 793c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(global); 794c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return r; 7953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 7963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 7973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 7983e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 7995d0ad50f5acf84f2e8a1ca5c6951f333aef0e25aNeal Norwitzsymtable_warn(struct symtable *st, char *msg, int lineno) 8003e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 801c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename, 802c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou lineno, NULL, NULL) < 0) { 803c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { 804c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SetString(PyExc_SyntaxError, msg); 805c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SyntaxLocation(st->st_filename, 806c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_lineno); 807c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 808c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 809c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 810c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 8113e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 8123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 813e0d12eb85cff367497090b1fdc58582edc0d4c99Benjamin Peterson/* symtable_enter_block() gets a reference via ste_new. 8143e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton This reference is released when the block is exited, via the DECREF 8153e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton in symtable_exit_block(). 8163e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 8173e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 8183e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 8193e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_exit_block(struct symtable *st, void *ast) 8203e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 821c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_ssize_t end; 822c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 823c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_CLEAR(st->st_cur); 824c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou end = PyList_GET_SIZE(st->st_stack) - 1; 825c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (end >= 0) { 826c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, 827c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou end); 828c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur == NULL) 829c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 830c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_INCREF(st->st_cur); 831c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PySequence_DelItem(st->st_stack, end) < 0) 832c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 833c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 834c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 8353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 8363e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 8373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 838c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrousymtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block, 839c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou void *ast, int lineno) 8403e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 841c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PySTEntryObject *prev = NULL; 842c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 843c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur) { 844c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou prev = st->st_cur; 845c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { 846c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 847c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 848c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(st->st_cur); 849c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 850c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur = ste_new(st, name, block, ast, lineno); 851c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur == NULL) 852c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 853f76942d6bf432d6881dc47070002d226e1e15ce9Benjamin Peterson if (block == ModuleBlock) 854c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_global = st->st_cur->ste_symbols; 855c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (prev) { 856c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyList_Append(prev->ste_children, 857c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (PyObject *)st->st_cur) < 0) { 858c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 859c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 860c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 861c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 8623e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 8633e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 86446b7bda9bcd0fb11878a154234c3064e19e35f3cNeal Norwitzstatic long 8653e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_lookup(struct symtable *st, PyObject *name) 8663e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 867c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *o; 868c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *mangled = _Py_Mangle(st->st_private, name); 869c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!mangled) 870c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 871c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou o = PyDict_GetItem(st->st_cur->ste_symbols, mangled); 872c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(mangled); 873c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!o) 874c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 875c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return PyInt_AsLong(o); 8763e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 8773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 8783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 879c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrousymtable_add_def(struct symtable *st, PyObject *name, int flag) 8803e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 881c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *o; 882c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *dict; 883c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou long val; 884c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *mangled = _Py_Mangle(st->st_private, name); 885c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 886c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!mangled) 887c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 888c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou dict = st->st_cur->ste_symbols; 889c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if ((o = PyDict_GetItem(dict, mangled))) { 890c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou val = PyInt_AS_LONG(o); 891c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { 892c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Is it better to use 'mangled' or 'name' here? */ 893c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, 894c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyString_AsString(name)); 895c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SyntaxLocation(st->st_filename, 896c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_lineno); 897c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 898c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 899c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou val |= flag; 900c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } else 901c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou val = flag; 902c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou o = PyInt_FromLong(val); 903c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (o == NULL) 904c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 905c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(dict, mangled, o) < 0) { 906c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(o); 907c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 908c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 909c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(o); 910c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 911c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (flag & DEF_PARAM) { 912c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0) 913c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 914c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } else if (flag & DEF_GLOBAL) { 915c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* XXX need to update DEF_GLOBAL for other flags too; 916c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou perhaps only DEF_FREE_GLOBAL */ 917c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou val = flag; 918c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if ((o = PyDict_GetItem(st->st_global, mangled))) { 919c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou val |= PyInt_AS_LONG(o); 920c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 921c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou o = PyInt_FromLong(val); 9223e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton if (o == NULL) 923c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 924c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (PyDict_SetItem(st->st_global, mangled, o) < 0) { 925c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(o); 926c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou goto error; 927c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 928c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(o); 929c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 930c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(mangled); 931c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 9328b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauer 9338b528b28f15a78c116fc90303194ad8b1476034dNeil Schemenauererror: 934c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(mangled); 935c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 9363e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 9373e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 9383e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument. 9393e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton They use the ASDL name to synthesize the name of the C type and the visit 940c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou function. 941c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 9423e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is 9433e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton useful if the first node in the sequence requires special treatment. 9443e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/ 9453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 9463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define VISIT(ST, TYPE, V) \ 947c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_ ## TYPE((ST), (V))) \ 948c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 949b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 950b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz#define VISIT_IN_BLOCK(ST, TYPE, V, S) \ 951c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_ ## TYPE((ST), (V))) { \ 952c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou symtable_exit_block((ST), (S)); \ 953c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; \ 954c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 955b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 9563e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define VISIT_SEQ(ST, TYPE, SEQ) { \ 957c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i; \ 958c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou asdl_seq *seq = (SEQ); /* avoid variable capture */ \ 959c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = 0; i < asdl_seq_LEN(seq); i++) { \ 960c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ 961c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_ ## TYPE((ST), elt)) \ 962c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; \ 963c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } \ 9643e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 965b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 966b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \ 967c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i; \ 968c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou asdl_seq *seq = (SEQ); /* avoid variable capture */ \ 969c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = 0; i < asdl_seq_LEN(seq); i++) { \ 970c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ 971c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_ ## TYPE((ST), elt)) { \ 972c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou symtable_exit_block((ST), (S)); \ 973c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; \ 974c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } \ 975c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } \ 976b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz} 977b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 9783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \ 979c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i; \ 980c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou asdl_seq *seq = (SEQ); /* avoid variable capture */ \ 981c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = (START); i < asdl_seq_LEN(seq); i++) { \ 982c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ 983c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_ ## TYPE((ST), elt)) \ 984c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; \ 985c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } \ 9863e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 987b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 988b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \ 989c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i; \ 990c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou asdl_seq *seq = (SEQ); /* avoid variable capture */ \ 991c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = (START); i < asdl_seq_LEN(seq); i++) { \ 992c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ 993c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_ ## TYPE((ST), elt)) { \ 994c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou symtable_exit_block((ST), (S)); \ 995c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; \ 996c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } \ 997c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } \ 998b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz} 999b6fc9df8fc34271a881ab954b843872834f5f112Neal Norwitz 10003e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 10013e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_stmt(struct symtable *st, stmt_ty s) 10023e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1003c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou switch (s->kind) { 1004c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case FunctionDef_kind: 1005c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL)) 1006c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1007c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.FunctionDef.args->defaults) 1008c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults); 1009c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.FunctionDef.decorator_list) 1010c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list); 1011c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_enter_block(st, s->v.FunctionDef.name, 1012c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou FunctionBlock, (void *)s, s->lineno)) 1013c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1014c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s); 1015c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s); 1016c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_exit_block(st, s)) 1017c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1018c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1019c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case ClassDef_kind: { 1020c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *tmp; 1021c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL)) 1022c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1023c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, s->v.ClassDef.bases); 1024c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.ClassDef.decorator_list) 1025c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list); 1026c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock, 1027c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (void *)s, s->lineno)) 1028c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1029c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou tmp = st->st_private; 1030c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_private = s->v.ClassDef.name; 1031c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s); 1032c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_private = tmp; 1033c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_exit_block(st, s)) 1034c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1035c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1036c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1037c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Return_kind: 1038c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Return.value) { 1039c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Return.value); 1040c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_returns_value = 1; 1041c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur->ste_generator) { 1042c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SetString(PyExc_SyntaxError, 1043c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou RETURN_VAL_IN_GENERATOR); 1044c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SyntaxLocation(st->st_filename, 1045c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou s->lineno); 1046c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1047c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1048c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1049c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1050c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Delete_kind: 1051c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, s->v.Delete.targets); 1052c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1053c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Assign_kind: 1054c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, s->v.Assign.targets); 1055c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Assign.value); 1056c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1057c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case AugAssign_kind: 1058c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.AugAssign.target); 1059c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.AugAssign.value); 1060c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1061c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Print_kind: 1062c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Print.dest) 1063c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Print.dest); 1064c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, s->v.Print.values); 1065c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1066c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case For_kind: 1067c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.For.target); 1068c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.For.iter); 1069c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.For.body); 1070c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.For.orelse) 1071c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.For.orelse); 1072c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1073c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case While_kind: 1074c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.While.test); 1075c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.While.body); 1076c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.While.orelse) 1077c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.While.orelse); 1078c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1079c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case If_kind: 1080c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* XXX if 0: and lookup_yield() hacks */ 1081c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.If.test); 1082c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.If.body); 1083c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.If.orelse) 1084c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.If.orelse); 1085c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1086c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Raise_kind: 1087c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Raise.type) { 1088c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Raise.type); 1089c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Raise.inst) { 1090c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Raise.inst); 1091c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Raise.tback) 1092c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Raise.tback); 1093c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1094c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1095c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1096c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case TryExcept_kind: 1097c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.TryExcept.body); 1098c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.TryExcept.orelse); 1099c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers); 1100c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1101c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case TryFinally_kind: 1102c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.TryFinally.body); 1103c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody); 1104c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1105c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Assert_kind: 1106c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Assert.test); 1107c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Assert.msg) 1108c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Assert.msg); 1109c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1110c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Import_kind: 1111c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, alias, s->v.Import.names); 1112c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* XXX Don't have the lineno available inside 1113c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou visit_alias */ 1114c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) 1115c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_opt_lineno = s->lineno; 1116c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1117c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case ImportFrom_kind: 1118c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, alias, s->v.ImportFrom.names); 1119c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* XXX Don't have the lineno available inside 1120c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou visit_alias */ 1121c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) 1122c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_opt_lineno = s->lineno; 1123c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1124c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Exec_kind: 1125c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Exec.body); 1126c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!st->st_cur->ste_opt_lineno) 1127c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_opt_lineno = s->lineno; 1128c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Exec.globals) { 1129c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_unoptimized |= OPT_EXEC; 1130c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Exec.globals); 1131c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Exec.locals) 1132c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Exec.locals); 1133c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } else { 1134c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_unoptimized |= OPT_BARE_EXEC; 1135c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1136c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1137c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Global_kind: { 1138c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i; 1139c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou asdl_seq *seq = s->v.Global.names; 1140c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = 0; i < asdl_seq_LEN(seq); i++) { 1141c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou identifier name = (identifier)asdl_seq_GET(seq, i); 1142c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou char *c_name = PyString_AS_STRING(name); 1143c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou long cur = symtable_lookup(st, name); 1144c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (cur < 0) 1145c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1146c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (cur & (DEF_LOCAL | USE)) { 1147c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou char buf[256]; 1148c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (cur & DEF_LOCAL) 1149c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyOS_snprintf(buf, sizeof(buf), 1150c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou GLOBAL_AFTER_ASSIGN, 1151c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou c_name); 1152c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou else 1153c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyOS_snprintf(buf, sizeof(buf), 1154c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou GLOBAL_AFTER_USE, 1155c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou c_name); 1156c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_warn(st, buf, s->lineno)) 1157c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1158c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1159c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, name, DEF_GLOBAL)) 1160c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1161c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1162c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1163c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1164c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Expr_kind: 1165c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Expr.value); 1166c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1167c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Pass_kind: 1168c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Break_kind: 1169c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Continue_kind: 1170c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* nothing to do here */ 1171c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1172c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case With_kind: 1173c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.With.context_expr); 1174c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.With.optional_vars) { 1175c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.With.optional_vars); 1176c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1177c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, s->v.With.body); 1178c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1179c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1180c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 11813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 11823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1183c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 11843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_expr(struct symtable *st, expr_ty e) 11853e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1186c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou switch (e->kind) { 1187c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case BoolOp_kind: 1188c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.BoolOp.values); 1189c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1190c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case BinOp_kind: 1191c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.BinOp.left); 1192c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.BinOp.right); 1193c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1194c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case UnaryOp_kind: 1195c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.UnaryOp.operand); 1196c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1197c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Lambda_kind: { 1198c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!GET_IDENTIFIER(lambda)) 1199c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1200c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (e->v.Lambda.args->defaults) 1201c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.Lambda.args->defaults); 1202c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_enter_block(st, lambda, 1203c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou FunctionBlock, (void *)e, e->lineno)) 1204c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1205c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e); 1206c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e); 1207c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_exit_block(st, (void *)e)) 1208c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1209c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1210c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1211c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case IfExp_kind: 1212c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.IfExp.test); 1213c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.IfExp.body); 1214c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.IfExp.orelse); 1215c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1216c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Dict_kind: 1217c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.Dict.keys); 1218c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.Dict.values); 1219c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1220c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Set_kind: 1221c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.Set.elts); 1222c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1223c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case ListComp_kind: 1224c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.ListComp.elt); 1225c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, comprehension, e->v.ListComp.generators); 1226c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1227c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case GeneratorExp_kind: 1228c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_genexp(st, e)) 1229c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1230c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1231c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case SetComp_kind: 1232c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_setcomp(st, e)) 1233c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1234c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1235c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case DictComp_kind: 1236c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_dictcomp(st, e)) 1237c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1238c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1239c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Yield_kind: 1240c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (e->v.Yield.value) 1241c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.Yield.value); 1242c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_generator = 1; 1243c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur->ste_returns_value) { 1244c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SetString(PyExc_SyntaxError, 1245c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou RETURN_VAL_IN_GENERATOR); 1246c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SyntaxLocation(st->st_filename, 1247c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->lineno); 1248c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1249c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1250c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1251c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Compare_kind: 1252c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.Compare.left); 1253c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.Compare.comparators); 1254c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1255c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Call_kind: 1256c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.Call.func); 1257c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.Call.args); 1258c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, keyword, e->v.Call.keywords); 1259c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (e->v.Call.starargs) 1260c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.Call.starargs); 1261c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (e->v.Call.kwargs) 1262c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.Call.kwargs); 1263c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1264c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Repr_kind: 1265c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.Repr.value); 1266c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1267c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Num_kind: 1268c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Str_kind: 1269c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Nothing to do here. */ 1270c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1271c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* The following exprs can be assignment targets. */ 1272c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Attribute_kind: 1273c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.Attribute.value); 1274c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1275c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Subscript_kind: 1276c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, e->v.Subscript.value); 1277c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, slice, e->v.Subscript.slice); 1278c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1279c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Name_kind: 1280c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, e->v.Name.id, 1281c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->v.Name.ctx == Load ? USE : DEF_LOCAL)) 1282c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1283c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1284c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* child nodes of List and Tuple will have expr_context set */ 1285c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case List_kind: 1286c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.List.elts); 1287c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1288c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Tuple_kind: 1289c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, e->v.Tuple.elts); 1290c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1291c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1292c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 12933e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 12943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 12953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 12963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_implicit_arg(struct symtable *st, int pos) 12973e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1298c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *id = PyString_FromFormat(".%d", pos); 1299c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (id == NULL) 1300c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1301c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, id, DEF_PARAM)) { 1302c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(id); 1303c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1304c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1305c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(id); 1306c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 13073e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 13083e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1309c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 13103e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel) 13113e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1312c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i; 1313c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 1314c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* go through all the toplevel arguments first */ 1315c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = 0; i < asdl_seq_LEN(args); i++) { 1316c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou expr_ty arg = (expr_ty)asdl_seq_GET(args, i); 1317c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (arg->kind == Name_kind) { 1318c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(arg->v.Name.ctx == Param || 1319c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou (arg->v.Name.ctx == Store && !toplevel)); 1320c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM)) 1321c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1322c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1323c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou else if (arg->kind == Tuple_kind) { 1324c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou assert(arg->v.Tuple.ctx == Store); 1325c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (toplevel) { 1326c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_implicit_arg(st, i)) 1327c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1328c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1329c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1330c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou else { 1331c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SetString(PyExc_SyntaxError, 1332c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou "invalid expression in parameter list"); 1333c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyErr_SyntaxLocation(st->st_filename, 1334c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_lineno); 1335c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1336c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1337c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1338c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 1339c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!toplevel) { 1340c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_visit_params_nested(st, args)) 1341c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1342c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1343c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 1344c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 13453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 13463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 13473e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonstatic int 13483e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_params_nested(struct symtable *st, asdl_seq *args) 13493e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1350c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int i; 1351c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou for (i = 0; i < asdl_seq_LEN(args); i++) { 1352c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou expr_ty arg = (expr_ty)asdl_seq_GET(args, i); 1353c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (arg->kind == Tuple_kind && 1354c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou !symtable_visit_params(st, arg->v.Tuple.elts, 0)) 1355c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1356c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1357c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 1358c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 13593e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 13603e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1361c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 13623e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_arguments(struct symtable *st, arguments_ty a) 13633e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1364c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* skip default arguments inside function block 1365c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou XXX should ast be different? 1366c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 1367c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (a->args && !symtable_visit_params(st, a->args, 1)) 1368c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1369c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (a->vararg) { 1370c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, a->vararg, DEF_PARAM)) 1371c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1372c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_varargs = 1; 1373c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1374c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (a->kwarg) { 1375c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, a->kwarg, DEF_PARAM)) 1376c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1377c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_varkeywords = 1; 1378c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1379c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (a->args && !symtable_visit_params_nested(st, a->args)) 1380c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1381c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 13823e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 13833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 13843e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1385c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 13863e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh) 13873e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1388c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (eh->v.ExceptHandler.type) 1389c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, eh->v.ExceptHandler.type); 1390c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (eh->v.ExceptHandler.name) 1391c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, eh->v.ExceptHandler.name); 1392c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body); 1393c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 13943e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 13953e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 13963e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1397c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 13983e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_alias(struct symtable *st, alias_ty a) 13993e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1400c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Compute store_name, the name actually bound by the import 1401c6660cf4d68ec17fef0a49aca2cf441c124a88d4Benjamin Peterson operation. It is different than a->name when a->name is a 1402c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou dotted package name (e.g. spam.eggs) 1403c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou */ 1404c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *store_name; 1405c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyObject *name = (a->asname == NULL) ? a->name : a->asname; 1406c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou const char *base = PyString_AS_STRING(name); 1407c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou char *dot = strchr(base, '.'); 1408c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (dot) { 1409c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou store_name = PyString_FromStringAndSize(base, dot - base); 1410c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!store_name) 1411c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1412c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1413c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou else { 1414c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou store_name = name; 1415c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_INCREF(store_name); 1416c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1417c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (strcmp(PyString_AS_STRING(name), "*")) { 1418c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int r = symtable_add_def(st, store_name, DEF_IMPORT); 1419c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(store_name); 1420c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return r; 1421c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1422c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou else { 1423c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (st->st_cur->ste_type != ModuleBlock) { 14245f75502a2ed490071514ab0361e1b406a7405de1Martin Panter int lineno = st->st_cur->ste_lineno; 14255f75502a2ed490071514ab0361e1b406a7405de1Martin Panter if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) { 14265f75502a2ed490071514ab0361e1b406a7405de1Martin Panter Py_DECREF(store_name); 14275f75502a2ed490071514ab0361e1b406a7405de1Martin Panter return 0; 14285f75502a2ed490071514ab0361e1b406a7405de1Martin Panter } 1429c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1430c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR; 1431c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(store_name); 1432c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 1433c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 14343e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 14353e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 14363e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1437c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 14383e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_comprehension(struct symtable *st, comprehension_ty lc) 14393e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1440c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, lc->target); 1441c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, lc->iter); 1442c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, expr, lc->ifs); 1443c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 14443e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 14453e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 14463e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1447c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 14483e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_keyword(struct symtable *st, keyword_ty k) 14493e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1450c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, k->value); 1451c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 14523e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 14533e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 14543e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1455c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 14563e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltonsymtable_visit_slice(struct symtable *st, slice_ty s) 14573e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1458c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou switch (s->kind) { 1459c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Slice_kind: 1460c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Slice.lower) 1461c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Slice.lower) 1462c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Slice.upper) 1463c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Slice.upper) 1464c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (s->v.Slice.step) 1465c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Slice.step) 1466c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1467c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case ExtSlice_kind: 1468c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ(st, slice, s->v.ExtSlice.dims) 1469c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1470c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Index_kind: 1471c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, s->v.Index.value) 1472c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1473c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou case Ellipsis_kind: 1474c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou break; 1475c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1476c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 14773e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 14783e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton 1479b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottistatic int 1480b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottisymtable_new_tmpname(struct symtable *st) 1481b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti{ 1482c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou char tmpname[256]; 1483c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou identifier tmp; 1484c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou 1485c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", 1486c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou ++st->st_cur->ste_tmpname); 1487c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou tmp = PyString_InternFromString(tmpname); 1488c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!tmp) 1489c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1490c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_add_def(st, tmp, DEF_LOCAL)) 1491c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1492c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou Py_DECREF(tmp); 1493c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 1; 1494b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti} 1495b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti 1496c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 1497b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottisymtable_handle_comprehension(struct symtable *st, expr_ty e, 1498b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti identifier scope_name, asdl_seq *generators, 1499b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti expr_ty elt, expr_ty value) 15003e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton{ 1501c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int is_generator = (e->kind == GeneratorExp_kind); 1502c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou int needs_tmp = !is_generator; 1503c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou comprehension_ty outermost = ((comprehension_ty) 1504c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou asdl_seq_GET(generators, 0)); 1505c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Outermost iterator is evaluated in current scope */ 1506c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT(st, expr, outermost->iter); 1507c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Create comprehension scope for the rest */ 1508c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!scope_name || 1509c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) { 1510c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1511c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1512c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou st->st_cur->ste_generator = is_generator; 1513c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Outermost iter is received as an argument */ 1514c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (!symtable_implicit_arg(st, 0)) { 1515c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou symtable_exit_block(st, (void *)e); 1516c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1517c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1518c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou /* Allocate temporary name if needed */ 1519c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (needs_tmp && !symtable_new_tmpname(st)) { 1520c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou symtable_exit_block(st, (void *)e); 1521c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return 0; 1522c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou } 1523c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e); 1524c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e); 1525c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension, 1526c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou generators, 1, (void*)e); 1527c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou if (value) 1528c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_IN_BLOCK(st, expr, value, (void*)e); 1529c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou VISIT_IN_BLOCK(st, expr, elt, (void*)e); 1530c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return symtable_exit_block(st, (void *)e); 15313e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} 1532b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti 1533c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitroustatic int 1534b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottisymtable_visit_genexp(struct symtable *st, expr_ty e) 1535b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti{ 1536c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr), 1537c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->v.GeneratorExp.generators, 1538c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->v.GeneratorExp.elt, NULL); 1539b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti} 1540b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti 1541b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottistatic int 1542b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottisymtable_visit_setcomp(struct symtable *st, expr_ty e) 1543b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti{ 1544c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp), 1545c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->v.SetComp.generators, 1546c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->v.SetComp.elt, NULL); 1547b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti} 1548b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti 1549b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottistatic int 1550b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalottisymtable_visit_dictcomp(struct symtable *st, expr_ty e) 1551b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti{ 1552c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp), 1553c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->v.DictComp.generators, 1554c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->v.DictComp.key, 1555c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou e->v.DictComp.value); 1556b646547bb45fe1df6abefd94f892c633798d91d2Alexandre Vassalotti} 1557