7 /* A symbol table is constructed each time PyNode_Compile() is
8 called. The table walks the entire parse tree and identifies each
9 use or definition of a variable.
11 The symbol table contains a dictionary for each code block in a
12 module: The symbol dictionary for the block. They keys of these
13 dictionaries are the name of all variables used or defined in the
14 block; the integer values are used to store several flags,
15 e.g. DEF_PARAM indicates that a variable is a parameter to a
19 struct _symtable_entry
;
22 int st_pass
; /* pass == 1 or 2 */
23 int st_nested_scopes
; /* true if nested scopes are enabled */
24 char *st_filename
; /* name of file being compiled */
25 struct _symtable_entry
*st_cur
; /* current symbol table entry */
26 PyObject
*st_symbols
; /* dictionary of symbol table entries */
27 PyObject
*st_stack
; /* stack of namespace info */
28 PyObject
*st_global
; /* borrowed ref to MODULE in st_symbols */
29 int st_nscopes
; /* number of scopes */
30 int st_errors
; /* number of errors */
31 char *st_private
; /* name of current class or NULL */
32 int st_tmpname
; /* temporary name counter */
33 PyFutureFeatures
*st_future
; /* module's future features */
36 typedef struct _symtable_entry
{
38 PyObject
*ste_id
; /* int: key in st_symbols) */
39 PyObject
*ste_symbols
; /* dict: name to flags) */
40 PyObject
*ste_name
; /* string: name of scope */
41 PyObject
*ste_varnames
; /* list of variable names */
42 PyObject
*ste_children
; /* list of child ids */
43 int ste_type
; /* module, class, or function */
44 int ste_lineno
; /* first line of scope */
45 int ste_optimized
; /* true if namespace can't be optimized */
46 int ste_nested
; /* true if scope is nested */
47 int ste_child_free
; /* true if a child scope has free variables,
48 including free refs to globals */
49 int ste_opt_lineno
; /* lineno of last exec or import * */
50 struct symtable
*ste_table
;
51 } PySymtableEntryObject
;
53 extern DL_IMPORT(PyTypeObject
) PySymtableEntry_Type
;
55 #define PySymtableEntry_Check(op) ((op)->ob_type == &PySymtableEntry_Type)
57 extern DL_IMPORT(PyObject
*) PySymtableEntry_New(struct symtable
*,
60 DL_IMPORT(struct symtable
*) PyNode_CompileSymtable(struct _node
*, char *);
61 DL_IMPORT(void) PySymtable_Free(struct symtable
*);
66 /* Flags for def-use information */
68 #define DEF_GLOBAL 1 /* global stmt */
69 #define DEF_LOCAL 2 /* assignment in code block */
70 #define DEF_PARAM 2<<1 /* formal parameter */
71 #define USE 2<<2 /* name is used */
72 #define DEF_STAR 2<<3 /* parameter is star arg */
73 #define DEF_DOUBLESTAR 2<<4 /* parameter is star-star arg */
74 #define DEF_INTUPLE 2<<5 /* name defined in tuple in parameters */
75 #define DEF_FREE 2<<6 /* name used but not defined in nested scope */
76 #define DEF_FREE_GLOBAL 2<<7 /* free variable is actually implicit global */
77 #define DEF_FREE_CLASS 2<<8 /* free variable from class's method */
78 #define DEF_IMPORT 2<<9 /* assignment occurred via import */
80 #define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
82 #define TYPE_FUNCTION 1
87 #define GLOBAL_EXPLICIT 2
88 #define GLOBAL_IMPLICIT 3
92 #define OPT_IMPORT_STAR 1
94 #define OPT_BARE_EXEC 4
99 #endif /* !Py_SYMTABLE_H */