Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-19 09:50:50

0001 #ifndef Py_INTERNAL_SYMTABLE_H
0002 #define Py_INTERNAL_SYMTABLE_H
0003 #ifdef __cplusplus
0004 extern "C" {
0005 #endif
0006 
0007 #ifndef Py_BUILD_CORE
0008 #  error "this header requires Py_BUILD_CORE define"
0009 #endif
0010 
0011 struct _mod;   // Type defined in pycore_ast.h
0012 
0013 typedef enum _block_type {
0014     FunctionBlock, ClassBlock, ModuleBlock,
0015     // Used for annotations if 'from __future__ import annotations' is active.
0016     // Annotation blocks cannot bind names and are not evaluated.
0017     AnnotationBlock,
0018 
0019     // The following blocks are used for generics and type aliases. These work
0020     // mostly like functions (see PEP 695 for details). The three different
0021     // blocks function identically; they are different enum entries only so
0022     // that error messages can be more precise.
0023 
0024     // The block to enter when processing a "type" (PEP 695) construction,
0025     // e.g., "type MyGeneric[T] = list[T]".
0026     TypeAliasBlock,
0027     // The block to enter when processing a "generic" (PEP 695) object,
0028     // e.g., "def foo[T](): pass" or "class A[T]: pass".
0029     TypeParametersBlock,
0030     // The block to enter when processing the bound, the constraint tuple
0031     // or the default value of a single "type variable" in the formal sense,
0032     // i.e., a TypeVar, a TypeVarTuple or a ParamSpec object (the latter two
0033     // do not support a bound or a constraint tuple).
0034     TypeVariableBlock,
0035 } _Py_block_ty;
0036 
0037 typedef enum _comprehension_type {
0038     NoComprehension = 0,
0039     ListComprehension = 1,
0040     DictComprehension = 2,
0041     SetComprehension = 3,
0042     GeneratorExpression = 4 } _Py_comprehension_ty;
0043 
0044 /* source location information */
0045 typedef struct {
0046     int lineno;
0047     int end_lineno;
0048     int col_offset;
0049     int end_col_offset;
0050 } _Py_SourceLocation;
0051 
0052 #define SRC_LOCATION_FROM_AST(n) \
0053     (_Py_SourceLocation){ \
0054                .lineno = (n)->lineno, \
0055                .end_lineno = (n)->end_lineno, \
0056                .col_offset = (n)->col_offset, \
0057                .end_col_offset = (n)->end_col_offset }
0058 
0059 static const _Py_SourceLocation NO_LOCATION = {-1, -1, -1, -1};
0060 
0061 /* __future__ information */
0062 typedef struct {
0063     int ff_features;                    /* flags set by future statements */
0064     _Py_SourceLocation ff_location;     /* location of last future statement */
0065 } _PyFutureFeatures;
0066 
0067 struct _symtable_entry;
0068 
0069 struct symtable {
0070     PyObject *st_filename;          /* name of file being compiled,
0071                                        decoded from the filesystem encoding */
0072     struct _symtable_entry *st_cur; /* current symbol table entry */
0073     struct _symtable_entry *st_top; /* symbol table entry for module */
0074     PyObject *st_blocks;            /* dict: map AST node addresses
0075                                      *       to symbol table entries */
0076     PyObject *st_stack;             /* list: stack of namespace info */
0077     PyObject *st_global;            /* borrowed ref to st_top->ste_symbols */
0078     int st_nblocks;                 /* number of blocks used. kept for
0079                                        consistency with the corresponding
0080                                        compiler structure */
0081     PyObject *st_private;           /* name of current class or NULL */
0082     _PyFutureFeatures *st_future;   /* module's future features that affect
0083                                        the symbol table */
0084     int recursion_depth;            /* current recursion depth */
0085     int recursion_limit;            /* recursion limit */
0086 };
0087 
0088 typedef struct _symtable_entry {
0089     PyObject_HEAD
0090     PyObject *ste_id;        /* int: key in ste_table->st_blocks */
0091     PyObject *ste_symbols;   /* dict: variable names to flags */
0092     PyObject *ste_name;      /* string: name of current block */
0093     PyObject *ste_varnames;  /* list of function parameters */
0094     PyObject *ste_children;  /* list of child blocks */
0095     PyObject *ste_directives;/* locations of global and nonlocal statements */
0096     PyObject *ste_mangled_names; /* set of names for which mangling should be applied */
0097 
0098     _Py_block_ty ste_type;
0099     // Optional string set by symtable.c and used when reporting errors.
0100     // The content of that string is a description of the current "context".
0101     //
0102     // For instance, if we are processing the default value of the type
0103     // variable "T" in "def foo[T = int](): pass", `ste_scope_info` is
0104     // set to "a TypeVar default".
0105     const char *ste_scope_info;
0106 
0107     int ste_nested;      /* true if block is nested */
0108     unsigned ste_free : 1;        /* true if block has free variables */
0109     unsigned ste_child_free : 1;  /* true if a child block has free vars,
0110                                      including free refs to globals */
0111     unsigned ste_generator : 1;   /* true if namespace is a generator */
0112     unsigned ste_coroutine : 1;   /* true if namespace is a coroutine */
0113     _Py_comprehension_ty ste_comprehension;  /* Kind of comprehension (if any) */
0114     unsigned ste_varargs : 1;     /* true if block has varargs */
0115     unsigned ste_varkeywords : 1; /* true if block has varkeywords */
0116     unsigned ste_returns_value : 1;  /* true if namespace uses return with
0117                                         an argument */
0118     unsigned ste_needs_class_closure : 1; /* for class scopes, true if a
0119                                              closure over __class__
0120                                              should be created */
0121     unsigned ste_needs_classdict : 1; /* for class scopes, true if a closure
0122                                          over the class dict should be created */
0123     unsigned ste_comp_inlined : 1; /* true if this comprehension is inlined */
0124     unsigned ste_comp_iter_target : 1; /* true if visiting comprehension target */
0125     unsigned ste_can_see_class_scope : 1; /* true if this block can see names bound in an
0126                                              enclosing class scope */
0127     int ste_comp_iter_expr; /* non-zero if visiting a comprehension range expression */
0128     int ste_lineno;          /* first line of block */
0129     int ste_col_offset;      /* offset of first line of block */
0130     int ste_end_lineno;      /* end line of block */
0131     int ste_end_col_offset;  /* end offset of first line of block */
0132     int ste_opt_lineno;      /* lineno of last exec or import * */
0133     int ste_opt_col_offset;  /* offset of last exec or import * */
0134     struct symtable *ste_table;
0135 } PySTEntryObject;
0136 
0137 extern PyTypeObject PySTEntry_Type;
0138 
0139 #define PySTEntry_Check(op) Py_IS_TYPE((op), &PySTEntry_Type)
0140 
0141 extern long _PyST_GetSymbol(PySTEntryObject *, PyObject *);
0142 extern int _PyST_GetScope(PySTEntryObject *, PyObject *);
0143 extern int _PyST_IsFunctionLike(PySTEntryObject *);
0144 
0145 extern struct symtable* _PySymtable_Build(
0146     struct _mod *mod,
0147     PyObject *filename,
0148     _PyFutureFeatures *future);
0149 extern PySTEntryObject* _PySymtable_Lookup(struct symtable *, void *);
0150 
0151 extern void _PySymtable_Free(struct symtable *);
0152 
0153 extern PyObject *_Py_MaybeMangle(PyObject *privateobj, PySTEntryObject *ste, PyObject *name);
0154 extern PyObject* _Py_Mangle(PyObject *p, PyObject *name);
0155 
0156 /* Flags for def-use information */
0157 
0158 #define DEF_GLOBAL 1             /* global stmt */
0159 #define DEF_LOCAL 2              /* assignment in code block */
0160 #define DEF_PARAM (2<<1)         /* formal parameter */
0161 #define DEF_NONLOCAL (2<<2)      /* nonlocal stmt */
0162 #define USE (2<<3)               /* name is used */
0163 #define DEF_FREE (2<<4)          /* name used but not defined in nested block */
0164 #define DEF_FREE_CLASS (2<<5)    /* free variable from class's method */
0165 #define DEF_IMPORT (2<<6)        /* assignment occurred via import */
0166 #define DEF_ANNOT (2<<7)         /* this name is annotated */
0167 #define DEF_COMP_ITER (2<<8)     /* this name is a comprehension iteration variable */
0168 #define DEF_TYPE_PARAM (2<<9)    /* this name is a type parameter */
0169 #define DEF_COMP_CELL (2<<10)    /* this name is a cell in an inlined comprehension */
0170 
0171 #define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
0172 
0173 /* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol
0174    table.  GLOBAL is returned from PyST_GetScope() for either of them.
0175    It is stored in ste_symbols at bits 13-16.
0176 */
0177 #define SCOPE_OFFSET 12
0178 #define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL)
0179 
0180 #define LOCAL 1
0181 #define GLOBAL_EXPLICIT 2
0182 #define GLOBAL_IMPLICIT 3
0183 #define FREE 4
0184 #define CELL 5
0185 
0186 #define GENERATOR 1
0187 #define GENERATOR_EXPRESSION 2
0188 
0189 // Used by symtablemodule.c
0190 extern struct symtable* _Py_SymtableStringObjectFlags(
0191     const char *str,
0192     PyObject *filename,
0193     int start,
0194     PyCompilerFlags *flags);
0195 
0196 int _PyFuture_FromAST(
0197     struct _mod * mod,
0198     PyObject *filename,
0199     _PyFutureFeatures* futures);
0200 
0201 #ifdef __cplusplus
0202 }
0203 #endif
0204 #endif /* !Py_INTERNAL_SYMTABLE_H */