1#ifndef Py_INTERNAL_SYMTABLE_H
2#define Py_INTERNAL_SYMTABLE_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7#ifndef Py_BUILD_CORE
8#  error "this header requires Py_BUILD_CORE define"
9#endif
10
11struct _mod;   // Type defined in pycore_ast.h
12
13typedef enum _block_type { FunctionBlock, ClassBlock, ModuleBlock, AnnotationBlock }
14    _Py_block_ty;
15
16typedef enum _comprehension_type {
17    NoComprehension = 0,
18    ListComprehension = 1,
19    DictComprehension = 2,
20    SetComprehension = 3,
21    GeneratorExpression = 4 } _Py_comprehension_ty;
22
23struct _symtable_entry;
24
25struct 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
44typedef 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
78extern PyTypeObject PySTEntry_Type;
79
80#define PySTEntry_Check(op) Py_IS_TYPE(op, &PySTEntry_Type)
81
82extern long _PyST_GetSymbol(PySTEntryObject *, PyObject *);
83extern int _PyST_GetScope(PySTEntryObject *, PyObject *);
84
85extern struct symtable* _PySymtable_Build(
86    struct _mod *mod,
87    PyObject *filename,
88    PyFutureFeatures *future);
89PyAPI_FUNC(PySTEntryObject *) PySymtable_Lookup(struct symtable *, void *);
90
91extern void _PySymtable_Free(struct symtable *);
92
93/* Flags for def-use information */
94
95#define DEF_GLOBAL 1           /* global stmt */
96#define DEF_LOCAL 2            /* assignment in code block */
97#define DEF_PARAM 2<<1         /* formal parameter */
98#define DEF_NONLOCAL 2<<2      /* nonlocal stmt */
99#define USE 2<<3               /* name is used */
100#define DEF_FREE 2<<4          /* name used but not defined in nested block */
101#define DEF_FREE_CLASS 2<<5    /* free variable from class's method */
102#define DEF_IMPORT 2<<6        /* assignment occurred via import */
103#define DEF_ANNOT 2<<7         /* this name is annotated */
104#define DEF_COMP_ITER 2<<8     /* this name is a comprehension iteration variable */
105
106#define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
107
108/* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol
109   table.  GLOBAL is returned from PyST_GetScope() for either of them.
110   It is stored in ste_symbols at bits 12-15.
111*/
112#define SCOPE_OFFSET 11
113#define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL)
114
115#define LOCAL 1
116#define GLOBAL_EXPLICIT 2
117#define GLOBAL_IMPLICIT 3
118#define FREE 4
119#define CELL 5
120
121#define GENERATOR 1
122#define GENERATOR_EXPRESSION 2
123
124// Used by symtablemodule.c
125extern struct symtable* _Py_SymtableStringObjectFlags(
126    const char *str,
127    PyObject *filename,
128    int start,
129    PyCompilerFlags *flags);
130
131#ifdef __cplusplus
132}
133#endif
134#endif /* !Py_INTERNAL_SYMTABLE_H */
135