• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef Py_LIMITED_API
2 #ifndef Py_SYMTABLE_H
3 #define Py_SYMTABLE_H
4 
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
8 
9 /* XXX(ncoghlan): This is a weird mix of public names and interpreter internal
10  *                names.
11  */
12 
13 typedef enum _block_type { FunctionBlock, ClassBlock, ModuleBlock }
14     _Py_block_ty;
15 
16 struct _symtable_entry;
17 
18 struct 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 
37 typedef 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     struct symtable *ste_table;
64 } PySTEntryObject;
65 
66 PyAPI_DATA(PyTypeObject) PySTEntry_Type;
67 
68 #define PySTEntry_Check(op) (Py_TYPE(op) == &PySTEntry_Type)
69 
70 PyAPI_FUNC(int) PyST_GetScope(PySTEntryObject *, PyObject *);
71 
72 PyAPI_FUNC(struct symtable *) PySymtable_Build(
73     mod_ty mod,
74     const char *filename,       /* decoded from the filesystem encoding */
75     PyFutureFeatures *future);
76 PyAPI_FUNC(struct symtable *) PySymtable_BuildObject(
77     mod_ty mod,
78     PyObject *filename,
79     PyFutureFeatures *future);
80 PyAPI_FUNC(PySTEntryObject *) PySymtable_Lookup(struct symtable *, void *);
81 
82 PyAPI_FUNC(void) PySymtable_Free(struct symtable *);
83 
84 /* Flags for def-use information */
85 
86 #define DEF_GLOBAL 1           /* global stmt */
87 #define DEF_LOCAL 2            /* assignment in code block */
88 #define DEF_PARAM 2<<1         /* formal parameter */
89 #define DEF_NONLOCAL 2<<2      /* nonlocal stmt */
90 #define USE 2<<3               /* name is used */
91 #define DEF_FREE 2<<4          /* name used but not defined in nested block */
92 #define DEF_FREE_CLASS 2<<5    /* free variable from class's method */
93 #define DEF_IMPORT 2<<6        /* assignment occurred via import */
94 #define DEF_ANNOT 2<<7         /* this name is annotated */
95 
96 #define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
97 
98 /* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol
99    table.  GLOBAL is returned from PyST_GetScope() for either of them.
100    It is stored in ste_symbols at bits 12-15.
101 */
102 #define SCOPE_OFFSET 11
103 #define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL)
104 
105 #define LOCAL 1
106 #define GLOBAL_EXPLICIT 2
107 #define GLOBAL_IMPLICIT 3
108 #define FREE 4
109 #define CELL 5
110 
111 #define GENERATOR 1
112 #define GENERATOR_EXPRESSION 2
113 
114 #ifdef __cplusplus
115 }
116 #endif
117 #endif /* !Py_SYMTABLE_H */
118 #endif /* Py_LIMITED_API */
119