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