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