jpayne@69: #ifndef Py_LIMITED_API jpayne@69: #ifndef Py_SYMTABLE_H jpayne@69: #define Py_SYMTABLE_H jpayne@69: #ifdef __cplusplus jpayne@69: extern "C" { jpayne@69: #endif jpayne@69: jpayne@69: #include "Python-ast.h" /* mod_ty */ jpayne@69: jpayne@69: /* XXX(ncoghlan): This is a weird mix of public names and interpreter internal jpayne@69: * names. jpayne@69: */ jpayne@69: jpayne@69: typedef enum _block_type { FunctionBlock, ClassBlock, ModuleBlock } jpayne@69: _Py_block_ty; jpayne@69: jpayne@69: struct _symtable_entry; jpayne@69: jpayne@69: struct symtable { jpayne@69: PyObject *st_filename; /* name of file being compiled, jpayne@69: decoded from the filesystem encoding */ jpayne@69: struct _symtable_entry *st_cur; /* current symbol table entry */ jpayne@69: struct _symtable_entry *st_top; /* symbol table entry for module */ jpayne@69: PyObject *st_blocks; /* dict: map AST node addresses jpayne@69: * to symbol table entries */ jpayne@69: PyObject *st_stack; /* list: stack of namespace info */ jpayne@69: PyObject *st_global; /* borrowed ref to st_top->ste_symbols */ jpayne@69: int st_nblocks; /* number of blocks used. kept for jpayne@69: consistency with the corresponding jpayne@69: compiler structure */ jpayne@69: PyObject *st_private; /* name of current class or NULL */ jpayne@69: PyFutureFeatures *st_future; /* module's future features that affect jpayne@69: the symbol table */ jpayne@69: int recursion_depth; /* current recursion depth */ jpayne@69: int recursion_limit; /* recursion limit */ jpayne@69: }; jpayne@69: jpayne@69: typedef struct _symtable_entry { jpayne@69: PyObject_HEAD jpayne@69: PyObject *ste_id; /* int: key in ste_table->st_blocks */ jpayne@69: PyObject *ste_symbols; /* dict: variable names to flags */ jpayne@69: PyObject *ste_name; /* string: name of current block */ jpayne@69: PyObject *ste_varnames; /* list of function parameters */ jpayne@69: PyObject *ste_children; /* list of child blocks */ jpayne@69: PyObject *ste_directives;/* locations of global and nonlocal statements */ jpayne@69: _Py_block_ty ste_type; /* module, class, or function */ jpayne@69: int ste_nested; /* true if block is nested */ jpayne@69: unsigned ste_free : 1; /* true if block has free variables */ jpayne@69: unsigned ste_child_free : 1; /* true if a child block has free vars, jpayne@69: including free refs to globals */ jpayne@69: unsigned ste_generator : 1; /* true if namespace is a generator */ jpayne@69: unsigned ste_coroutine : 1; /* true if namespace is a coroutine */ jpayne@69: unsigned ste_comprehension : 1; /* true if namespace is a list comprehension */ jpayne@69: unsigned ste_varargs : 1; /* true if block has varargs */ jpayne@69: unsigned ste_varkeywords : 1; /* true if block has varkeywords */ jpayne@69: unsigned ste_returns_value : 1; /* true if namespace uses return with jpayne@69: an argument */ jpayne@69: unsigned ste_needs_class_closure : 1; /* for class scopes, true if a jpayne@69: closure over __class__ jpayne@69: should be created */ jpayne@69: unsigned ste_comp_iter_target : 1; /* true if visiting comprehension target */ jpayne@69: int ste_comp_iter_expr; /* non-zero if visiting a comprehension range expression */ jpayne@69: int ste_lineno; /* first line of block */ jpayne@69: int ste_col_offset; /* offset of first line of block */ jpayne@69: int ste_opt_lineno; /* lineno of last exec or import * */ jpayne@69: int ste_opt_col_offset; /* offset of last exec or import * */ jpayne@69: struct symtable *ste_table; jpayne@69: } PySTEntryObject; jpayne@69: jpayne@69: PyAPI_DATA(PyTypeObject) PySTEntry_Type; jpayne@69: jpayne@69: #define PySTEntry_Check(op) (Py_TYPE(op) == &PySTEntry_Type) jpayne@69: jpayne@69: PyAPI_FUNC(int) PyST_GetScope(PySTEntryObject *, PyObject *); jpayne@69: jpayne@69: PyAPI_FUNC(struct symtable *) PySymtable_Build( jpayne@69: mod_ty mod, jpayne@69: const char *filename, /* decoded from the filesystem encoding */ jpayne@69: PyFutureFeatures *future); jpayne@69: PyAPI_FUNC(struct symtable *) PySymtable_BuildObject( jpayne@69: mod_ty mod, jpayne@69: PyObject *filename, jpayne@69: PyFutureFeatures *future); jpayne@69: PyAPI_FUNC(PySTEntryObject *) PySymtable_Lookup(struct symtable *, void *); jpayne@69: jpayne@69: PyAPI_FUNC(void) PySymtable_Free(struct symtable *); jpayne@69: jpayne@69: /* Flags for def-use information */ jpayne@69: jpayne@69: #define DEF_GLOBAL 1 /* global stmt */ jpayne@69: #define DEF_LOCAL 2 /* assignment in code block */ jpayne@69: #define DEF_PARAM 2<<1 /* formal parameter */ jpayne@69: #define DEF_NONLOCAL 2<<2 /* nonlocal stmt */ jpayne@69: #define USE 2<<3 /* name is used */ jpayne@69: #define DEF_FREE 2<<4 /* name used but not defined in nested block */ jpayne@69: #define DEF_FREE_CLASS 2<<5 /* free variable from class's method */ jpayne@69: #define DEF_IMPORT 2<<6 /* assignment occurred via import */ jpayne@69: #define DEF_ANNOT 2<<7 /* this name is annotated */ jpayne@69: #define DEF_COMP_ITER 2<<8 /* this name is a comprehension iteration variable */ jpayne@69: jpayne@69: #define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT) jpayne@69: jpayne@69: /* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol jpayne@69: table. GLOBAL is returned from PyST_GetScope() for either of them. jpayne@69: It is stored in ste_symbols at bits 12-15. jpayne@69: */ jpayne@69: #define SCOPE_OFFSET 11 jpayne@69: #define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL) jpayne@69: jpayne@69: #define LOCAL 1 jpayne@69: #define GLOBAL_EXPLICIT 2 jpayne@69: #define GLOBAL_IMPLICIT 3 jpayne@69: #define FREE 4 jpayne@69: #define CELL 5 jpayne@69: jpayne@69: #define GENERATOR 1 jpayne@69: #define GENERATOR_EXPRESSION 2 jpayne@69: jpayne@69: #ifdef __cplusplus jpayne@69: } jpayne@69: #endif jpayne@69: #endif /* !Py_SYMTABLE_H */ jpayne@69: #endif /* !Py_LIMITED_API */