Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:47

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     // Used for generics and type aliases. These work mostly like functions
0019     // (see PEP 695 for details). The three different blocks function identically;
0020     // they are different enum entries only so that error messages can be more
0021     // precise.
0022     TypeVarBoundBlock, TypeAliasBlock, TypeParamBlock
0023 } _Py_block_ty;
0024 
0025 typedef enum _comprehension_type {
0026     NoComprehension = 0,
0027     ListComprehension = 1,
0028     DictComprehension = 2,
0029     SetComprehension = 3,
0030     GeneratorExpression = 4 } _Py_comprehension_ty;
0031 
0032 struct _symtable_entry;
0033 
0034 struct symtable {
0035     PyObject *st_filename;          /* name of file being compiled,
0036                                        decoded from the filesystem encoding */
0037     struct _symtable_entry *st_cur; /* current symbol table entry */
0038     struct _symtable_entry *st_top; /* symbol table entry for module */
0039     PyObject *st_blocks;            /* dict: map AST node addresses
0040                                      *       to symbol table entries */
0041     PyObject *st_stack;             /* list: stack of namespace info */
0042     PyObject *st_global;            /* borrowed ref to st_top->ste_symbols */
0043     int st_nblocks;                 /* number of blocks used. kept for
0044                                        consistency with the corresponding
0045                                        compiler structure */
0046     PyObject *st_private;           /* name of current class or NULL */
0047     PyFutureFeatures *st_future;    /* module's future features that affect
0048                                        the symbol table */
0049     int recursion_depth;            /* current recursion depth */
0050     int recursion_limit;            /* recursion limit */
0051 };
0052 
0053 typedef struct _symtable_entry {
0054     PyObject_HEAD
0055     PyObject *ste_id;        /* int: key in ste_table->st_blocks */
0056     PyObject *ste_symbols;   /* dict: variable names to flags */
0057     PyObject *ste_name;      /* string: name of current block */
0058     PyObject *ste_varnames;  /* list of function parameters */
0059     PyObject *ste_children;  /* list of child blocks */
0060     PyObject *ste_directives;/* locations of global and nonlocal statements */
0061     _Py_block_ty ste_type;
0062     int ste_nested;      /* true if block is nested */
0063     unsigned ste_free : 1;        /* true if block has free variables */
0064     unsigned ste_child_free : 1;  /* true if a child block has free vars,
0065                                      including free refs to globals */
0066     unsigned ste_generator : 1;   /* true if namespace is a generator */
0067     unsigned ste_coroutine : 1;   /* true if namespace is a coroutine */
0068     _Py_comprehension_ty ste_comprehension;  /* Kind of comprehension (if any) */
0069     unsigned ste_varargs : 1;     /* true if block has varargs */
0070     unsigned ste_varkeywords : 1; /* true if block has varkeywords */
0071     unsigned ste_returns_value : 1;  /* true if namespace uses return with
0072                                         an argument */
0073     unsigned ste_needs_class_closure : 1; /* for class scopes, true if a
0074                                              closure over __class__
0075                                              should be created */
0076     unsigned ste_needs_classdict : 1; /* for class scopes, true if a closure
0077                                          over the class dict should be created */
0078     unsigned ste_comp_inlined : 1; /* true if this comprehension is inlined */
0079     unsigned ste_comp_iter_target : 1; /* true if visiting comprehension target */
0080     unsigned ste_can_see_class_scope : 1; /* true if this block can see names bound in an
0081                                              enclosing class scope */
0082     int ste_comp_iter_expr; /* non-zero if visiting a comprehension range expression */
0083     int ste_lineno;          /* first line of block */
0084     int ste_col_offset;      /* offset of first line of block */
0085     int ste_end_lineno;      /* end line of block */
0086     int ste_end_col_offset;  /* end offset of first line of block */
0087     int ste_opt_lineno;      /* lineno of last exec or import * */
0088     int ste_opt_col_offset;  /* offset of last exec or import * */
0089     struct symtable *ste_table;
0090     PyObject *ste_mangled_names; /* set of names for which mangling should be applied */
0091 } PySTEntryObject;
0092 
0093 extern PyTypeObject PySTEntry_Type;
0094 
0095 #define PySTEntry_Check(op) Py_IS_TYPE((op), &PySTEntry_Type)
0096 
0097 extern long _PyST_GetSymbol(PySTEntryObject *, PyObject *);
0098 extern int _PyST_GetScope(PySTEntryObject *, PyObject *);
0099 extern int _PyST_IsFunctionLike(PySTEntryObject *);
0100 
0101 extern struct symtable* _PySymtable_Build(
0102     struct _mod *mod,
0103     PyObject *filename,
0104     PyFutureFeatures *future);
0105 PyAPI_FUNC(PySTEntryObject *) PySymtable_Lookup(struct symtable *, void *);
0106 
0107 extern void _PySymtable_Free(struct symtable *);
0108 
0109 extern PyObject *_Py_MaybeMangle(PyObject *privateobj, PySTEntryObject *ste, PyObject *name);
0110 extern PyObject* _Py_Mangle(PyObject *p, PyObject *name);
0111 
0112 /* Flags for def-use information */
0113 
0114 #define DEF_GLOBAL 1             /* global stmt */
0115 #define DEF_LOCAL 2              /* assignment in code block */
0116 #define DEF_PARAM (2<<1)         /* formal parameter */
0117 #define DEF_NONLOCAL (2<<2)      /* nonlocal stmt */
0118 #define USE (2<<3)               /* name is used */
0119 #define DEF_FREE (2<<4)          /* name used but not defined in nested block */
0120 #define DEF_FREE_CLASS (2<<5)    /* free variable from class's method */
0121 #define DEF_IMPORT (2<<6)        /* assignment occurred via import */
0122 #define DEF_ANNOT (2<<7)         /* this name is annotated */
0123 #define DEF_COMP_ITER (2<<8)     /* this name is a comprehension iteration variable */
0124 #define DEF_TYPE_PARAM (2<<9)    /* this name is a type parameter */
0125 #define DEF_COMP_CELL (2<<10)    /* this name is a cell in an inlined comprehension */
0126 
0127 #define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
0128 
0129 /* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol
0130    table.  GLOBAL is returned from PyST_GetScope() for either of them.
0131    It is stored in ste_symbols at bits 13-16.
0132 */
0133 #define SCOPE_OFFSET 12
0134 #define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL)
0135 
0136 #define LOCAL 1
0137 #define GLOBAL_EXPLICIT 2
0138 #define GLOBAL_IMPLICIT 3
0139 #define FREE 4
0140 #define CELL 5
0141 
0142 #define GENERATOR 1
0143 #define GENERATOR_EXPRESSION 2
0144 
0145 // Used by symtablemodule.c
0146 extern struct symtable* _Py_SymtableStringObjectFlags(
0147     const char *str,
0148     PyObject *filename,
0149     int start,
0150     PyCompilerFlags *flags);
0151 
0152 int _PyFuture_FromAST(
0153     struct _mod * mod,
0154     PyObject *filename,
0155     PyFutureFeatures* futures);
0156 
0157 #ifdef __cplusplus
0158 }
0159 #endif
0160 #endif /* !Py_INTERNAL_SYMTABLE_H */