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. Otherwise, they
17 // are lazily evaluated (see PEP 649).
18 AnnotationBlock,
19
20 // The following blocks are used for generics and type aliases. These work
21 // mostly like functions (see PEP 695 for details). The three different
22 // blocks function identically; they are different enum entries only so
23 // that error messages can be more precise.
24
25 // The block to enter when processing a "type" (PEP 695) construction,
26 // e.g., "type MyGeneric[T] = list[T]".
27 TypeAliasBlock,
28 // The block to enter when processing a "generic" (PEP 695) object,
29 // e.g., "def foo[T](): pass" or "class A[T]: pass".
30 TypeParametersBlock,
31 // The block to enter when processing the bound, the constraint tuple
32 // or the default value of a single "type variable" in the formal sense,
33 // i.e., a TypeVar, a TypeVarTuple or a ParamSpec object (the latter two
34 // do not support a bound or a constraint tuple).
35 TypeVariableBlock,
36 } _Py_block_ty;
37
38 typedef enum _comprehension_type {
39 NoComprehension = 0,
40 ListComprehension = 1,
41 DictComprehension = 2,
42 SetComprehension = 3,
43 GeneratorExpression = 4 } _Py_comprehension_ty;
44
45 /* source location information */
46 typedef struct {
47 int lineno;
48 int end_lineno;
49 int col_offset;
50 int end_col_offset;
51 } _Py_SourceLocation;
52
53 #define SRC_LOCATION_FROM_AST(n) \
54 (_Py_SourceLocation){ \
55 .lineno = (n)->lineno, \
56 .end_lineno = (n)->end_lineno, \
57 .col_offset = (n)->col_offset, \
58 .end_col_offset = (n)->end_col_offset }
59
60 static const _Py_SourceLocation NO_LOCATION = {-1, -1, -1, -1};
61 static const _Py_SourceLocation NEXT_LOCATION = {-2, -2, -2, -2};
62
63 /* __future__ information */
64 typedef struct {
65 int ff_features; /* flags set by future statements */
66 _Py_SourceLocation ff_location; /* location of last future statement */
67 } _PyFutureFeatures;
68
69 struct _symtable_entry;
70
71 struct symtable {
72 PyObject *st_filename; /* name of file being compiled,
73 decoded from the filesystem encoding */
74 struct _symtable_entry *st_cur; /* current symbol table entry */
75 struct _symtable_entry *st_top; /* symbol table entry for module */
76 PyObject *st_blocks; /* dict: map AST node addresses
77 * to symbol table entries */
78 PyObject *st_stack; /* list: stack of namespace info */
79 PyObject *st_global; /* borrowed ref to st_top->ste_symbols */
80 int st_nblocks; /* number of blocks used. kept for
81 consistency with the corresponding
82 compiler structure */
83 PyObject *st_private; /* name of current class or NULL */
84 _PyFutureFeatures *st_future; /* module's future features that affect
85 the symbol table */
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_generator : 1; /* true if namespace is a generator */
109 unsigned ste_coroutine : 1; /* true if namespace is a coroutine */
110 unsigned ste_annotations_used : 1; /* true if there are any annotations in this scope */
111 _Py_comprehension_ty ste_comprehension; /* Kind of comprehension (if any) */
112 unsigned ste_varargs : 1; /* true if block has varargs */
113 unsigned ste_varkeywords : 1; /* true if block has varkeywords */
114 unsigned ste_returns_value : 1; /* true if namespace uses return with
115 an argument */
116 unsigned ste_needs_class_closure : 1; /* for class scopes, true if a
117 closure over __class__
118 should be created */
119 unsigned ste_needs_classdict : 1; /* for class scopes, true if a closure
120 over the class dict should be created */
121 unsigned ste_comp_inlined : 1; /* true if this comprehension is inlined */
122 unsigned ste_comp_iter_target : 1; /* true if visiting comprehension target */
123 unsigned ste_can_see_class_scope : 1; /* true if this block can see names bound in an
124 enclosing class scope */
125 unsigned ste_has_docstring : 1; /* true if docstring present */
126 unsigned ste_method : 1; /* true if block is a function block defined in class scope */
127 unsigned ste_has_conditional_annotations : 1; /* true if block has conditionally executed annotations */
128 unsigned ste_in_conditional_block : 1; /* set while we are inside a conditionally executed block */
129 unsigned ste_in_unevaluated_annotation : 1; /* set while we are processing an annotation that will not be evaluated */
130 int ste_comp_iter_expr; /* non-zero if visiting a comprehension range expression */
131 _Py_SourceLocation ste_loc; /* source location of block */
132 struct _symtable_entry *ste_annotation_block; /* symbol table entry for this entry's annotations */
133 struct symtable *ste_table;
134 } PySTEntryObject;
135
136 extern PyTypeObject PySTEntry_Type;
137
138 #define PySTEntry_Check(op) Py_IS_TYPE((op), &PySTEntry_Type)
139
140 extern long _PyST_GetSymbol(PySTEntryObject *, PyObject *);
141 extern int _PyST_GetScope(PySTEntryObject *, PyObject *);
142 extern int _PyST_IsFunctionLike(PySTEntryObject *);
143
144 extern struct symtable* _PySymtable_Build(
145 struct _mod *mod,
146 PyObject *filename,
147 _PyFutureFeatures *future);
148 extern PySTEntryObject* _PySymtable_Lookup(struct symtable *, void *);
149 extern int _PySymtable_LookupOptional(struct symtable *, void *, PySTEntryObject **);
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_CLASS (2<<5) /* free variable from class's method */
164 #define DEF_IMPORT (2<<6) /* assignment occurred via import */
165 #define DEF_ANNOT (2<<7) /* this name is annotated */
166 #define DEF_COMP_ITER (2<<8) /* this name is a comprehension iteration variable */
167 #define DEF_TYPE_PARAM (2<<9) /* this name is a type parameter */
168 #define DEF_COMP_CELL (2<<10) /* this name is a cell in an inlined comprehension */
169
170 #define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
171
172 /* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol
173 table. GLOBAL is returned from _PyST_GetScope() for either of them.
174 It is stored in ste_symbols at bits 13-16.
175 */
176 #define SCOPE_OFFSET 12
177 #define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL)
178 #define SYMBOL_TO_SCOPE(S) (((S) >> SCOPE_OFFSET) & SCOPE_MASK)
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 // Used by symtablemodule.c
187 extern struct symtable* _Py_SymtableStringObjectFlags(
188 const char *str,
189 PyObject *filename,
190 int start,
191 PyCompilerFlags *flags);
192
193 int _PyFuture_FromAST(
194 struct _mod * mod,
195 PyObject *filename,
196 _PyFutureFeatures* futures);
197
198 #ifdef __cplusplus
199 }
200 #endif
201 #endif /* !Py_INTERNAL_SYMTABLE_H */