blob: abd19a7923e1ba3186231e9e83826f633198cc50 [file] [log] [blame]
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001#ifndef Py_LIMITED_API
Jeremy Hylton4b38da62001-02-02 18:19:15 +00002#ifndef Py_SYMTABLE_H
3#define Py_SYMTABLE_H
4#ifdef __cplusplus
5extern "C" {
6#endif
7
Victor Stinner5f2df882018-11-12 00:56:19 +01008#include "Python-ast.h" /* mod_ty */
9
Nick Coghlan650f0d02007-04-15 12:05:43 +000010/* XXX(ncoghlan): This is a weird mix of public names and interpreter internal
11 * names.
12 */
13
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014typedef enum _block_type { FunctionBlock, ClassBlock, ModuleBlock }
Neal Norwitz62c2fac2005-10-24 00:30:44 +000015 _Py_block_ty;
Jeremy Hylton4b38da62001-02-02 18:19:15 +000016
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000017struct _symtable_entry;
18
Jeremy Hylton4b38da62001-02-02 18:19:15 +000019struct symtable {
Victor Stinner14e461d2013-08-26 22:28:21 +020020 PyObject *st_filename; /* name of file being compiled,
Victor Stinner00676d12010-12-27 01:49:31 +000021 decoded from the filesystem encoding */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 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 */
Eli Benderskydd97fbb2011-04-10 07:37:26 +030027 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 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 PyObject *st_private; /* name of current class or NULL */
Eli Benderskydd97fbb2011-04-10 07:37:26 +030032 PyFutureFeatures *st_future; /* module's future features that affect
33 the symbol table */
Nick Coghlanaab9c2b2012-11-04 23:14:34 +100034 int recursion_depth; /* current recursion depth */
35 int recursion_limit; /* recursion limit */
Jeremy Hylton4b38da62001-02-02 18:19:15 +000036};
37
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000038typedef struct _symtable_entry {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 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 */
Eli Benderskydd97fbb2011-04-10 07:37:26 +030043 PyObject *ste_varnames; /* list of function parameters */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 PyObject *ste_children; /* list of child blocks */
Benjamin Petersond9c87022012-10-31 20:26:20 -040045 PyObject *ste_directives;/* locations of global and nonlocal statements */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 _Py_block_ty ste_type; /* module, class, or function */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 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 */
Yury Selivanoveb636452016-09-08 22:01:51 -070052 unsigned ste_coroutine : 1; /* true if namespace is a coroutine */
Emily Morehouse8f59ee02019-01-24 16:49:56 -070053 unsigned ste_comprehension : 1; /* true if namespace is a list comprehension */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 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 */
Benjamin Peterson312595c2013-05-15 15:26:42 -050058 unsigned ste_needs_class_closure : 1; /* for class scopes, true if a
59 closure over __class__
60 should be created */
Nick Coghlan5dbe0f52019-08-25 23:45:40 +100061 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 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 int ste_lineno; /* first line of block */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000064 int ste_col_offset; /* offset of first line of block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 int ste_opt_lineno; /* lineno of last exec or import * */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000066 int ste_opt_col_offset; /* offset of last exec or import * */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 struct symtable *ste_table;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000068} PySTEntryObject;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000069
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000070PyAPI_DATA(PyTypeObject) PySTEntry_Type;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000071
Dong-hee Nad905df72020-02-14 02:37:17 +090072#define PySTEntry_Check(op) Py_IS_TYPE(op, &PySTEntry_Type)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000073
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000074PyAPI_FUNC(int) PyST_GetScope(PySTEntryObject *, PyObject *);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000075
Victor Stinner00676d12010-12-27 01:49:31 +000076PyAPI_FUNC(struct symtable *) PySymtable_Build(
77 mod_ty mod,
78 const char *filename, /* decoded from the filesystem encoding */
79 PyFutureFeatures *future);
Victor Stinner14e461d2013-08-26 22:28:21 +020080PyAPI_FUNC(struct symtable *) PySymtable_BuildObject(
81 mod_ty mod,
82 PyObject *filename,
83 PyFutureFeatures *future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000084PyAPI_FUNC(PySTEntryObject *) PySymtable_Lookup(struct symtable *, void *);
85
Mark Hammond91a681d2002-08-12 07:21:58 +000086PyAPI_FUNC(void) PySymtable_Free(struct symtable *);
Jeremy Hylton4b38da62001-02-02 18:19:15 +000087
Jeremy Hylton4b38da62001-02-02 18:19:15 +000088/* 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 */
Jeremy Hylton81e95022007-02-27 06:50:52 +000093#define DEF_NONLOCAL 2<<2 /* nonlocal stmt */
94#define USE 2<<3 /* name is used */
Benjamin Peterson78565b22009-06-28 19:19:51 +000095#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 */
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070098#define DEF_ANNOT 2<<7 /* this name is annotated */
Nick Coghlan5dbe0f52019-08-25 23:45:40 +100099#define DEF_COMP_ITER 2<<8 /* this name is a comprehension iteration variable */
Jeremy Hylton4b38da62001-02-02 18:19:15 +0000100
Jeremy Hylton99858b52001-02-28 23:03:39 +0000101#define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
102
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000103/* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 table. GLOBAL is returned from PyST_GetScope() for either of them.
Jeremy Hylton81e95022007-02-27 06:50:52 +0000105 It is stored in ste_symbols at bits 12-15.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000106*/
Nick Coghlan650f0d02007-04-15 12:05:43 +0000107#define SCOPE_OFFSET 11
Jeremy Hylton81e95022007-02-27 06:50:52 +0000108#define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL)
Jeremy Hylton4b38da62001-02-02 18:19:15 +0000109
110#define LOCAL 1
111#define GLOBAL_EXPLICIT 2
112#define GLOBAL_IMPLICIT 3
113#define FREE 4
114#define CELL 5
115
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000116#define GENERATOR 1
117#define GENERATOR_EXPRESSION 2
Jeremy Hylton29906ee2001-02-27 04:23:34 +0000118
Jeremy Hylton4b38da62001-02-02 18:19:15 +0000119#ifdef __cplusplus
120}
121#endif
122#endif /* !Py_SYMTABLE_H */
Victor Stinner5f2df882018-11-12 00:56:19 +0100123#endif /* !Py_LIMITED_API */