14d0d471a8031de90a2b1ce99c4ac4780e60b3bc9Martin v. Löwis#ifndef Py_LIMITED_API
24b38da664c107bc08768235a66621429beef5444Jeremy Hylton#ifndef Py_SYMTABLE_H
34b38da664c107bc08768235a66621429beef5444Jeremy Hylton#define Py_SYMTABLE_H
4090b3dde06c7590ed7d789bd01a7d800cc09a348Neal Norwitz
54b38da664c107bc08768235a66621429beef5444Jeremy Hylton#ifdef __cplusplus
64b38da664c107bc08768235a66621429beef5444Jeremy Hyltonextern "C" {
74b38da664c107bc08768235a66621429beef5444Jeremy Hylton#endif
84b38da664c107bc08768235a66621429beef5444Jeremy Hylton
9650f0d06d3574f843f52edd1126ddd9ebd6fac7dNick Coghlan/* XXX(ncoghlan): This is a weird mix of public names and interpreter internal
10650f0d06d3574f843f52edd1126ddd9ebd6fac7dNick Coghlan *                names.
11650f0d06d3574f843f52edd1126ddd9ebd6fac7dNick Coghlan */
12650f0d06d3574f843f52edd1126ddd9ebd6fac7dNick Coghlan
133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hyltontypedef enum _block_type { FunctionBlock, ClassBlock, ModuleBlock }
1462c2fac9a0758f303c0382e459031a2679a056e2Neal Norwitz    _Py_block_ty;
154b38da664c107bc08768235a66621429beef5444Jeremy Hylton
16cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltonstruct _symtable_entry;
17cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton
184b38da664c107bc08768235a66621429beef5444Jeremy Hyltonstruct symtable {
1914e461d5b92000ec4e89182fa25ab0d5b5b31234Victor Stinner    PyObject *st_filename;          /* name of file being compiled,
2000676d143626dbbb7e1ad456ed013afba4420978Victor Stinner                                       decoded from the filesystem encoding */
21f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    struct _symtable_entry *st_cur; /* current symbol table entry */
22f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    struct _symtable_entry *st_top; /* symbol table entry for module */
23f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    PyObject *st_blocks;            /* dict: map AST node addresses
24f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou                                     *       to symbol table entries */
25f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    PyObject *st_stack;             /* list: stack of namespace info */
26dd97fbb2dcfbfbf701b5290b666a62fed6a76fb2Eli Bendersky    PyObject *st_global;            /* borrowed ref to st_top->ste_symbols */
27dd97fbb2dcfbfbf701b5290b666a62fed6a76fb2Eli Bendersky    int st_nblocks;                 /* number of blocks used. kept for
28dd97fbb2dcfbfbf701b5290b666a62fed6a76fb2Eli Bendersky                                       consistency with the corresponding
29dd97fbb2dcfbfbf701b5290b666a62fed6a76fb2Eli Bendersky                                       compiler structure */
30f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    PyObject *st_private;           /* name of current class or NULL */
31dd97fbb2dcfbfbf701b5290b666a62fed6a76fb2Eli Bendersky    PyFutureFeatures *st_future;    /* module's future features that affect
32dd97fbb2dcfbfbf701b5290b666a62fed6a76fb2Eli Bendersky                                       the symbol table */
33aab9c2b2ead7b786947c367a9cc5d9c921b9ea99Nick Coghlan    int recursion_depth;            /* current recursion depth */
34aab9c2b2ead7b786947c367a9cc5d9c921b9ea99Nick Coghlan    int recursion_limit;            /* recursion limit */
354b38da664c107bc08768235a66621429beef5444Jeremy Hylton};
364b38da664c107bc08768235a66621429beef5444Jeremy Hylton
37cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hyltontypedef struct _symtable_entry {
38f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    PyObject_HEAD
39f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    PyObject *ste_id;        /* int: key in ste_table->st_blocks */
40f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    PyObject *ste_symbols;   /* dict: variable names to flags */
41f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    PyObject *ste_name;      /* string: name of current block */
42dd97fbb2dcfbfbf701b5290b666a62fed6a76fb2Eli Bendersky    PyObject *ste_varnames;  /* list of function parameters */
43f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    PyObject *ste_children;  /* list of child blocks */
44d9c8702c0fd5e1e92e47435b0f64be440b2d5165Benjamin Peterson    PyObject *ste_directives;/* locations of global and nonlocal statements */
45f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    _Py_block_ty ste_type;   /* module, class, or function */
46f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    int ste_nested;      /* true if block is nested */
47f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    unsigned ste_free : 1;        /* true if block has free variables */
48f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    unsigned ste_child_free : 1;  /* true if a child block has free vars,
49f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou                                     including free refs to globals */
50f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    unsigned ste_generator : 1;   /* true if namespace is a generator */
51eb6364557f9bc4e6be29bb8a8f43308a0e080abaYury Selivanov    unsigned ste_coroutine : 1;   /* true if namespace is a coroutine */
52f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    unsigned ste_varargs : 1;     /* true if block has varargs */
53f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    unsigned ste_varkeywords : 1; /* true if block has varkeywords */
54f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    unsigned ste_returns_value : 1;  /* true if namespace uses return with
55f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou                                        an argument */
56312595ce3a75e91d53222e486dfdd6f3668422caBenjamin Peterson    unsigned ste_needs_class_closure : 1; /* for class scopes, true if a
57312595ce3a75e91d53222e486dfdd6f3668422caBenjamin Peterson                                             closure over __class__
58312595ce3a75e91d53222e486dfdd6f3668422caBenjamin Peterson                                             should be created */
59f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    int ste_lineno;          /* first line of block */
60d4efd9eb1534eeead7f56c89d1b5e394d8633990Benjamin Peterson    int ste_col_offset;      /* offset of first line of block */
61f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    int ste_opt_lineno;      /* lineno of last exec or import * */
62d4efd9eb1534eeead7f56c89d1b5e394d8633990Benjamin Peterson    int ste_opt_col_offset;  /* offset of last exec or import * */
63f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    int ste_tmpname;         /* counter for listcomp temp vars */
64f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    struct symtable *ste_table;
653e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton} PySTEntryObject;
66cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton
673e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPyAPI_DATA(PyTypeObject) PySTEntry_Type;
68cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton
6990aa7646affbaee9628ca6ea6a702aec17b3b550Christian Heimes#define PySTEntry_Check(op) (Py_TYPE(op) == &PySTEntry_Type)
70cb17ae8b19c35cc63e7daec871c025d903c49105Jeremy Hylton
713e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPyAPI_FUNC(int) PyST_GetScope(PySTEntryObject *, PyObject *);
724b38da664c107bc08768235a66621429beef5444Jeremy Hylton
7300676d143626dbbb7e1ad456ed013afba4420978Victor StinnerPyAPI_FUNC(struct symtable *) PySymtable_Build(
7400676d143626dbbb7e1ad456ed013afba4420978Victor Stinner    mod_ty mod,
7500676d143626dbbb7e1ad456ed013afba4420978Victor Stinner    const char *filename,       /* decoded from the filesystem encoding */
7600676d143626dbbb7e1ad456ed013afba4420978Victor Stinner    PyFutureFeatures *future);
7714e461d5b92000ec4e89182fa25ab0d5b5b31234Victor StinnerPyAPI_FUNC(struct symtable *) PySymtable_BuildObject(
7814e461d5b92000ec4e89182fa25ab0d5b5b31234Victor Stinner    mod_ty mod,
7914e461d5b92000ec4e89182fa25ab0d5b5b31234Victor Stinner    PyObject *filename,
8014e461d5b92000ec4e89182fa25ab0d5b5b31234Victor Stinner    PyFutureFeatures *future);
813e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPyAPI_FUNC(PySTEntryObject *) PySymtable_Lookup(struct symtable *, void *);
824b38da664c107bc08768235a66621429beef5444Jeremy Hylton
833e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy HyltonPyAPI_FUNC(void) PySymtable_Free(struct symtable *);
844b38da664c107bc08768235a66621429beef5444Jeremy Hylton
854b38da664c107bc08768235a66621429beef5444Jeremy Hylton/* Flags for def-use information */
864b38da664c107bc08768235a66621429beef5444Jeremy Hylton
874b38da664c107bc08768235a66621429beef5444Jeremy Hylton#define DEF_GLOBAL 1           /* global stmt */
884b38da664c107bc08768235a66621429beef5444Jeremy Hylton#define DEF_LOCAL 2            /* assignment in code block */
894b38da664c107bc08768235a66621429beef5444Jeremy Hylton#define DEF_PARAM 2<<1         /* formal parameter */
9081e9502df69394821416309c7c4b5357af51f4d5Jeremy Hylton#define DEF_NONLOCAL 2<<2      /* nonlocal stmt */
9181e9502df69394821416309c7c4b5357af51f4d5Jeremy Hylton#define USE 2<<3               /* name is used */
9278565b2216b07ee16a07ae5bbc3cda474bdfa348Benjamin Peterson#define DEF_FREE 2<<4          /* name used but not defined in nested block */
9378565b2216b07ee16a07ae5bbc3cda474bdfa348Benjamin Peterson#define DEF_FREE_CLASS 2<<5    /* free variable from class's method */
9478565b2216b07ee16a07ae5bbc3cda474bdfa348Benjamin Peterson#define DEF_IMPORT 2<<6        /* assignment occurred via import */
95f8cb8a16a344ab208fd46876c4b63604987347b8Yury Selivanov#define DEF_ANNOT 2<<7         /* this name is annotated */
964b38da664c107bc08768235a66621429beef5444Jeremy Hylton
9799858b53fc47c98d04357e6a1c0aa5634a443415Jeremy Hylton#define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
9899858b53fc47c98d04357e6a1c0aa5634a443415Jeremy Hylton
993e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton/* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol
100f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou   table.  GLOBAL is returned from PyST_GetScope() for either of them.
10181e9502df69394821416309c7c4b5357af51f4d5Jeremy Hylton   It is stored in ste_symbols at bits 12-15.
1023e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton*/
103650f0d06d3574f843f52edd1126ddd9ebd6fac7dNick Coghlan#define SCOPE_OFFSET 11
10481e9502df69394821416309c7c4b5357af51f4d5Jeremy Hylton#define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL)
1054b38da664c107bc08768235a66621429beef5444Jeremy Hylton
1064b38da664c107bc08768235a66621429beef5444Jeremy Hylton#define LOCAL 1
1074b38da664c107bc08768235a66621429beef5444Jeremy Hylton#define GLOBAL_EXPLICIT 2
1084b38da664c107bc08768235a66621429beef5444Jeremy Hylton#define GLOBAL_IMPLICIT 3
1094b38da664c107bc08768235a66621429beef5444Jeremy Hylton#define FREE 4
1104b38da664c107bc08768235a66621429beef5444Jeremy Hylton#define CELL 5
1114b38da664c107bc08768235a66621429beef5444Jeremy Hylton
1123e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define GENERATOR 1
1133e0055f8c65c407e74ce476b8e2b1fb889723514Jeremy Hylton#define GENERATOR_EXPRESSION 2
11429906eef3a56c43a4cd68a8706c75844b2384e59Jeremy Hylton
1154b38da664c107bc08768235a66621429beef5444Jeremy Hylton#ifdef __cplusplus
1164b38da664c107bc08768235a66621429beef5444Jeremy Hylton}
1174b38da664c107bc08768235a66621429beef5444Jeremy Hylton#endif
1184b38da664c107bc08768235a66621429beef5444Jeremy Hylton#endif /* !Py_SYMTABLE_H */
1194d0d471a8031de90a2b1ce99c4ac4780e60b3bc9Martin v. Löwis#endif /* Py_LIMITED_API */
120