1#ifndef Py_LIMITED_API
2#ifndef Py_SYMTABLE_H
3#define Py_SYMTABLE_H
4
5#ifdef __cplusplus
6extern "C" {
7#endif
8
9/* XXX(ncoghlan): This is a weird mix of public names and interpreter internal
10 *                names.
11 */
12
13typedef enum _block_type { FunctionBlock, ClassBlock, ModuleBlock }
14    _Py_block_ty;
15
16struct _symtable_entry;
17
18struct symtable {
19    PyObject *st_filename;          /* name of file being compiled,
20                                       decoded from the filesystem encoding */
21    struct _symtable_entry *st_cur; /* current symbol table entry */
22    struct _symtable_entry *st_top; /* symbol table entry for module */
23    PyObject *st_blocks;            /* dict: map AST node addresses
24                                     *       to symbol table entries */
25    PyObject *st_stack;             /* list: stack of namespace info */
26    PyObject *st_global;            /* borrowed ref to st_top->ste_symbols */
27    int st_nblocks;                 /* number of blocks used. kept for
28                                       consistency with the corresponding
29                                       compiler structure */
30    PyObject *st_private;           /* name of current class or NULL */
31    PyFutureFeatures *st_future;    /* module's future features that affect
32                                       the symbol table */
33    int recursion_depth;            /* current recursion depth */
34    int recursion_limit;            /* recursion limit */
35};
36
37typedef struct _symtable_entry {
38    PyObject_HEAD
39    PyObject *ste_id;        /* int: key in ste_table->st_blocks */
40    PyObject *ste_symbols;   /* dict: variable names to flags */
41    PyObject *ste_name;      /* string: name of current block */
42    PyObject *ste_varnames;  /* list of function parameters */
43    PyObject *ste_children;  /* list of child blocks */
44    PyObject *ste_directives;/* locations of global and nonlocal statements */
45    _Py_block_ty ste_type;   /* module, class, or function */
46    int ste_nested;      /* true if block is nested */
47    unsigned ste_free : 1;        /* true if block has free variables */
48    unsigned ste_child_free : 1;  /* true if a child block has free vars,
49                                     including free refs to globals */
50    unsigned ste_generator : 1;   /* true if namespace is a generator */
51    unsigned ste_coroutine : 1;   /* true if namespace is a coroutine */
52    unsigned ste_varargs : 1;     /* true if block has varargs */
53    unsigned ste_varkeywords : 1; /* true if block has varkeywords */
54    unsigned ste_returns_value : 1;  /* true if namespace uses return with
55                                        an argument */
56    unsigned ste_needs_class_closure : 1; /* for class scopes, true if a
57                                             closure over __class__
58                                             should be created */
59    int ste_lineno;          /* first line of block */
60    int ste_col_offset;      /* offset of first line of block */
61    int ste_opt_lineno;      /* lineno of last exec or import * */
62    int ste_opt_col_offset;  /* offset of last exec or import * */
63    int ste_tmpname;         /* counter for listcomp temp vars */
64    struct symtable *ste_table;
65} PySTEntryObject;
66
67PyAPI_DATA(PyTypeObject) PySTEntry_Type;
68
69#define PySTEntry_Check(op) (Py_TYPE(op) == &PySTEntry_Type)
70
71PyAPI_FUNC(int) PyST_GetScope(PySTEntryObject *, PyObject *);
72
73PyAPI_FUNC(struct symtable *) PySymtable_Build(
74    mod_ty mod,
75    const char *filename,       /* decoded from the filesystem encoding */
76    PyFutureFeatures *future);
77PyAPI_FUNC(struct symtable *) PySymtable_BuildObject(
78    mod_ty mod,
79    PyObject *filename,
80    PyFutureFeatures *future);
81PyAPI_FUNC(PySTEntryObject *) PySymtable_Lookup(struct symtable *, void *);
82
83PyAPI_FUNC(void) PySymtable_Free(struct symtable *);
84
85/* Flags for def-use information */
86
87#define DEF_GLOBAL 1           /* global stmt */
88#define DEF_LOCAL 2            /* assignment in code block */
89#define DEF_PARAM 2<<1         /* formal parameter */
90#define DEF_NONLOCAL 2<<2      /* nonlocal stmt */
91#define USE 2<<3               /* name is used */
92#define DEF_FREE 2<<4          /* name used but not defined in nested block */
93#define DEF_FREE_CLASS 2<<5    /* free variable from class's method */
94#define DEF_IMPORT 2<<6        /* assignment occurred via import */
95#define DEF_ANNOT 2<<7         /* this name is annotated */
96
97#define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
98
99/* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol
100   table.  GLOBAL is returned from PyST_GetScope() for either of them.
101   It is stored in ste_symbols at bits 12-15.
102*/
103#define SCOPE_OFFSET 11
104#define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL)
105
106#define LOCAL 1
107#define GLOBAL_EXPLICIT 2
108#define GLOBAL_IMPLICIT 3
109#define FREE 4
110#define CELL 5
111
112#define GENERATOR 1
113#define GENERATOR_EXPRESSION 2
114
115#ifdef __cplusplus
116}
117#endif
118#endif /* !Py_SYMTABLE_H */
119#endif /* Py_LIMITED_API */
120