1 #ifndef Py_INTERNAL_SYMTABLE_H
2 #define Py_INTERNAL_SYMTABLE_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6
7 #ifndef Py_BUILD_CORE
8 # error "this header requires Py_BUILD_CORE define"
9 #endif
10
11 struct _mod; // Type defined in pycore_ast.h
12
13 typedef enum _block_type {
14 FunctionBlock, ClassBlock, ModuleBlock,
15 // Used for annotations if 'from __future__ import annotations' is active.
16 // Annotation blocks cannot bind names and are not evaluated.
17 AnnotationBlock,
18 // Used for generics and type aliases. These work mostly like functions
19 // (see PEP 695 for details). The three different blocks function identically;
20 // they are different enum entries only so that error messages can be more
21 // precise.
22 TypeVarBoundBlock, TypeAliasBlock, TypeParamBlock
23 } _Py_block_ty;
24
25 typedef enum _comprehension_type {
26 NoComprehension = 0,
27 ListComprehension = 1,
28 DictComprehension = 2,
29 SetComprehension = 3,
30 GeneratorExpression = 4 } _Py_comprehension_ty;
31
32 struct _symtable_entry;
33
34 struct symtable {
35 PyObject *st_filename; /* name of file being compiled,
36 decoded from the filesystem encoding */
37 struct _symtable_entry *st_cur; /* current symbol table entry */
38 struct _symtable_entry *st_top; /* symbol table entry for module */
39 PyObject *st_blocks; /* dict: map AST node addresses
40 * to symbol table entries */
41 PyObject *st_stack; /* list: stack of namespace info */
42 PyObject *st_global; /* borrowed ref to st_top->ste_symbols */
43 int st_nblocks; /* number of blocks used. kept for
44 consistency with the corresponding
45 compiler structure */
46 PyObject *st_private; /* name of current class or NULL */
47 PyFutureFeatures *st_future; /* module's future features that affect
48 the symbol table */
49 int recursion_depth; /* current recursion depth */
50 int recursion_limit; /* recursion limit */
51 };
52
53 typedef struct _symtable_entry {
54 PyObject_HEAD
55 PyObject *ste_id; /* int: key in ste_table->st_blocks */
56 PyObject *ste_symbols; /* dict: variable names to flags */
57 PyObject *ste_name; /* string: name of current block */
58 PyObject *ste_varnames; /* list of function parameters */
59 PyObject *ste_children; /* list of child blocks */
60 PyObject *ste_directives;/* locations of global and nonlocal statements */
61 _Py_block_ty ste_type;
62 int ste_nested; /* true if block is nested */
63 unsigned ste_free : 1; /* true if block has free variables */
64 unsigned ste_child_free : 1; /* true if a child block has free vars,
65 including free refs to globals */
66 unsigned ste_generator : 1; /* true if namespace is a generator */
67 unsigned ste_coroutine : 1; /* true if namespace is a coroutine */
68 _Py_comprehension_ty ste_comprehension; /* Kind of comprehension (if any) */
69 unsigned ste_varargs : 1; /* true if block has varargs */
70 unsigned ste_varkeywords : 1; /* true if block has varkeywords */
71 unsigned ste_returns_value : 1; /* true if namespace uses return with
72 an argument */
73 unsigned ste_needs_class_closure : 1; /* for class scopes, true if a
74 closure over __class__
75 should be created */
76 unsigned ste_needs_classdict : 1; /* for class scopes, true if a closure
77 over the class dict should be created */
78 unsigned ste_comp_inlined : 1; /* true if this comprehension is inlined */
79 unsigned ste_comp_iter_target : 1; /* true if visiting comprehension target */
80 unsigned ste_can_see_class_scope : 1; /* true if this block can see names bound in an
81 enclosing class scope */
82 int ste_comp_iter_expr; /* non-zero if visiting a comprehension range expression */
83 int ste_lineno; /* first line of block */
84 int ste_col_offset; /* offset of first line of block */
85 int ste_end_lineno; /* end line of block */
86 int ste_end_col_offset; /* end offset of first line of block */
87 int ste_opt_lineno; /* lineno of last exec or import * */
88 int ste_opt_col_offset; /* offset of last exec or import * */
89 struct symtable *ste_table;
90 } PySTEntryObject;
91
92 extern PyTypeObject PySTEntry_Type;
93
94 #define PySTEntry_Check(op) Py_IS_TYPE((op), &PySTEntry_Type)
95
96 extern long _PyST_GetSymbol(PySTEntryObject *, PyObject *);
97 extern int _PyST_GetScope(PySTEntryObject *, PyObject *);
98 extern int _PyST_IsFunctionLike(PySTEntryObject *);
99
100 extern struct symtable* _PySymtable_Build(
101 struct _mod *mod,
102 PyObject *filename,
103 PyFutureFeatures *future);
104 PyAPI_FUNC(PySTEntryObject *) PySymtable_Lookup(struct symtable *, void *);
105
106 extern void _PySymtable_Free(struct symtable *);
107
108 extern PyObject* _Py_Mangle(PyObject *p, PyObject *name);
109
110 /* Flags for def-use information */
111
112 #define DEF_GLOBAL 1 /* global stmt */
113 #define DEF_LOCAL 2 /* assignment in code block */
114 #define DEF_PARAM 2<<1 /* formal parameter */
115 #define DEF_NONLOCAL 2<<2 /* nonlocal stmt */
116 #define USE 2<<3 /* name is used */
117 #define DEF_FREE 2<<4 /* name used but not defined in nested block */
118 #define DEF_FREE_CLASS 2<<5 /* free variable from class's method */
119 #define DEF_IMPORT 2<<6 /* assignment occurred via import */
120 #define DEF_ANNOT 2<<7 /* this name is annotated */
121 #define DEF_COMP_ITER 2<<8 /* this name is a comprehension iteration variable */
122 #define DEF_TYPE_PARAM 2<<9 /* this name is a type parameter */
123 #define DEF_COMP_CELL 2<<10 /* this name is a cell in an inlined comprehension */
124
125 #define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
126
127 /* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol
128 table. GLOBAL is returned from PyST_GetScope() for either of them.
129 It is stored in ste_symbols at bits 13-16.
130 */
131 #define SCOPE_OFFSET 12
132 #define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL)
133
134 #define LOCAL 1
135 #define GLOBAL_EXPLICIT 2
136 #define GLOBAL_IMPLICIT 3
137 #define FREE 4
138 #define CELL 5
139
140 #define GENERATOR 1
141 #define GENERATOR_EXPRESSION 2
142
143 // Used by symtablemodule.c
144 extern struct symtable* _Py_SymtableStringObjectFlags(
145 const char *str,
146 PyObject *filename,
147 int start,
148 PyCompilerFlags *flags);
149
150 int _PyFuture_FromAST(
151 struct _mod * mod,
152 PyObject *filename,
153 PyFutureFeatures* futures);
154
155 #ifdef __cplusplus
156 }
157 #endif
158 #endif /* !Py_INTERNAL_SYMTABLE_H */