Debian Codegraph @

Include/internal/pycore_symtable.h

raw
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
19 // The following blocks are used for generics and type aliases. These work
20 // mostly like functions (see PEP 695 for details). The three different
21 // blocks function identically; they are different enum entries only so
22 // that error messages can be more precise.
23
24 // The block to enter when processing a "type" (PEP 695) construction,
25 // e.g., "type MyGeneric[T] = list[T]".
26 TypeAliasBlock,
27 // The block to enter when processing a "generic" (PEP 695) object,
28 // e.g., "def foo[T](): pass" or "class A[T]: pass".
29 TypeParametersBlock,
30 // The block to enter when processing the bound, the constraint tuple
31 // or the default value of a single "type variable" in the formal sense,
32 // i.e., a TypeVar, a TypeVarTuple or a ParamSpec object (the latter two
33 // do not support a bound or a constraint tuple).
34 TypeVariableBlock,
35 } _Py_block_ty;
36
37 typedef enum _comprehension_type {
38 NoComprehension = 0,
39 ListComprehension = 1,
40 DictComprehension = 2,
41 SetComprehension = 3,
42 GeneratorExpression = 4 } _Py_comprehension_ty;
43
44 /* source location information */
45 typedef struct {
46 int lineno;
47 int end_lineno;
48 int col_offset;
49 int end_col_offset;
50 } _Py_SourceLocation;
51
52 #define SRC_LOCATION_FROM_AST(n) \
53 (_Py_SourceLocation){ \
54 .lineno = (n)->lineno, \
55 .end_lineno = (n)->end_lineno, \
56 .col_offset = (n)->col_offset, \
57 .end_col_offset = (n)->end_col_offset }
58
59 static const _Py_SourceLocation NO_LOCATION = {-1, -1, -1, -1};
60
61 /* __future__ information */
62 typedef struct {
63 int ff_features; /* flags set by future statements */
64 _Py_SourceLocation ff_location; /* location of last future statement */
65 } _PyFutureFeatures;
66
67 struct _symtable_entry;
68
69 struct symtable {
70 PyObject *st_filename; /* name of file being compiled,
71 decoded from the filesystem encoding */
72 struct _symtable_entry *st_cur; /* current symbol table entry */
73 struct _symtable_entry *st_top; /* symbol table entry for module */
74 PyObject *st_blocks; /* dict: map AST node addresses
75 * to symbol table entries */
76 PyObject *st_stack; /* list: stack of namespace info */
77 PyObject *st_global; /* borrowed ref to st_top->ste_symbols */
78 int st_nblocks; /* number of blocks used. kept for
79 consistency with the corresponding
80 compiler structure */
81 PyObject *st_private; /* name of current class or NULL */
82 _PyFutureFeatures *st_future; /* module's future features that affect
83 the symbol table */
84 int recursion_depth; /* current recursion depth */
85 int recursion_limit; /* recursion limit */
86 };
87
88 typedef struct _symtable_entry {
89 PyObject_HEAD
90 PyObject *ste_id; /* int: key in ste_table->st_blocks */
91 PyObject *ste_symbols; /* dict: variable names to flags */
92 PyObject *ste_name; /* string: name of current block */
93 PyObject *ste_varnames; /* list of function parameters */
94 PyObject *ste_children; /* list of child blocks */
95 PyObject *ste_directives;/* locations of global and nonlocal statements */
96 PyObject *ste_mangled_names; /* set of names for which mangling should be applied */
97
98 _Py_block_ty ste_type;
99 // Optional string set by symtable.c and used when reporting errors.
100 // The content of that string is a description of the current "context".
101 //
102 // For instance, if we are processing the default value of the type
103 // variable "T" in "def foo[T = int](): pass", `ste_scope_info` is
104 // set to "a TypeVar default".
105 const char *ste_scope_info;
106
107 int ste_nested; /* true if block is nested */
108 unsigned ste_free : 1; /* true if block has free variables */
109 unsigned ste_child_free : 1; /* true if a child block has free vars,
110 including free refs to globals */
111 unsigned ste_generator : 1; /* true if namespace is a generator */
112 unsigned ste_coroutine : 1; /* true if namespace is a coroutine */
113 _Py_comprehension_ty ste_comprehension; /* Kind of comprehension (if any) */
114 unsigned ste_varargs : 1; /* true if block has varargs */
115 unsigned ste_varkeywords : 1; /* true if block has varkeywords */
116 unsigned ste_returns_value : 1; /* true if namespace uses return with
117 an argument */
118 unsigned ste_needs_class_closure : 1; /* for class scopes, true if a
119 closure over __class__
120 should be created */
121 unsigned ste_needs_classdict : 1; /* for class scopes, true if a closure
122 over the class dict should be created */
123 unsigned ste_comp_inlined : 1; /* true if this comprehension is inlined */
124 unsigned ste_comp_iter_target : 1; /* true if visiting comprehension target */
125 unsigned ste_can_see_class_scope : 1; /* true if this block can see names bound in an
126 enclosing class scope */
127 int ste_comp_iter_expr; /* non-zero if visiting a comprehension range expression */
128 int ste_lineno; /* first line of block */
129 int ste_col_offset; /* offset of first line of block */
130 int ste_end_lineno; /* end line of block */
131 int ste_end_col_offset; /* end offset of first line of block */
132 int ste_opt_lineno; /* lineno of last exec or import * */
133 int ste_opt_col_offset; /* offset of last exec or import * */
134 struct symtable *ste_table;
135 } PySTEntryObject;
136
137 extern PyTypeObject PySTEntry_Type;
138
139 #define PySTEntry_Check(op) Py_IS_TYPE((op), &PySTEntry_Type)
140
141 extern long _PyST_GetSymbol(PySTEntryObject *, PyObject *);
142 extern int _PyST_GetScope(PySTEntryObject *, PyObject *);
143 extern int _PyST_IsFunctionLike(PySTEntryObject *);
144
145 extern struct symtable* _PySymtable_Build(
146 struct _mod *mod,
147 PyObject *filename,
148 _PyFutureFeatures *future);
149 extern PySTEntryObject* _PySymtable_Lookup(struct symtable *, void *);
150
151 extern void _PySymtable_Free(struct symtable *);
152
153 extern PyObject *_Py_MaybeMangle(PyObject *privateobj, PySTEntryObject *ste, PyObject *name);
154 extern PyObject* _Py_Mangle(PyObject *p, PyObject *name);
155
156 /* Flags for def-use information */
157
158 #define DEF_GLOBAL 1 /* global stmt */
159 #define DEF_LOCAL 2 /* assignment in code block */
160 #define DEF_PARAM (2<<1) /* formal parameter */
161 #define DEF_NONLOCAL (2<<2) /* nonlocal stmt */
162 #define USE (2<<3) /* name is used */
163 #define DEF_FREE (2<<4) /* name used but not defined in nested block */
164 #define DEF_FREE_CLASS (2<<5) /* free variable from class's method */
165 #define DEF_IMPORT (2<<6) /* assignment occurred via import */
166 #define DEF_ANNOT (2<<7) /* this name is annotated */
167 #define DEF_COMP_ITER (2<<8) /* this name is a comprehension iteration variable */
168 #define DEF_TYPE_PARAM (2<<9) /* this name is a type parameter */
169 #define DEF_COMP_CELL (2<<10) /* this name is a cell in an inlined comprehension */
170
171 #define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
172
173 /* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol
174 table. GLOBAL is returned from PyST_GetScope() for either of them.
175 It is stored in ste_symbols at bits 13-16.
176 */
177 #define SCOPE_OFFSET 12
178 #define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL)
179
180 #define LOCAL 1
181 #define GLOBAL_EXPLICIT 2
182 #define GLOBAL_IMPLICIT 3
183 #define FREE 4
184 #define CELL 5
185
186 #define GENERATOR 1
187 #define GENERATOR_EXPRESSION 2
188
189 // Used by symtablemodule.c
190 extern struct symtable* _Py_SymtableStringObjectFlags(
191 const char *str,
192 PyObject *filename,
193 int start,
194 PyCompilerFlags *flags);
195
196 int _PyFuture_FromAST(
197 struct _mod * mod,
198 PyObject *filename,
199 _PyFutureFeatures* futures);
200
201 #ifdef __cplusplus
202 }
203 #endif
204 #endif /* !Py_INTERNAL_SYMTABLE_H */