Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1 | #ifndef Py_LIMITED_API |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 2 | #ifndef Py_SYMTABLE_H |
| 3 | #define Py_SYMTABLE_H |
| 4 | #ifdef __cplusplus |
| 5 | extern "C" { |
| 6 | #endif |
| 7 | |
Victor Stinner | 5f2df88 | 2018-11-12 00:56:19 +0100 | [diff] [blame] | 8 | #include "Python-ast.h" /* mod_ty */ |
| 9 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 10 | /* XXX(ncoghlan): This is a weird mix of public names and interpreter internal |
| 11 | * names. |
| 12 | */ |
| 13 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 14 | typedef enum _block_type { FunctionBlock, ClassBlock, ModuleBlock } |
Neal Norwitz | 62c2fac | 2005-10-24 00:30:44 +0000 | [diff] [blame] | 15 | _Py_block_ty; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 16 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 17 | struct _symtable_entry; |
| 18 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 19 | struct symtable { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 20 | PyObject *st_filename; /* name of file being compiled, |
Victor Stinner | 00676d1 | 2010-12-27 01:49:31 +0000 | [diff] [blame] | 21 | decoded from the filesystem encoding */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 */ |
Eli Bendersky | dd97fbb | 2011-04-10 07:37:26 +0300 | [diff] [blame] | 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 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 31 | PyObject *st_private; /* name of current class or NULL */ |
Eli Bendersky | dd97fbb | 2011-04-10 07:37:26 +0300 | [diff] [blame] | 32 | PyFutureFeatures *st_future; /* module's future features that affect |
| 33 | the symbol table */ |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 34 | int recursion_depth; /* current recursion depth */ |
| 35 | int recursion_limit; /* recursion limit */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 38 | typedef struct _symtable_entry { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 */ |
Eli Bendersky | dd97fbb | 2011-04-10 07:37:26 +0300 | [diff] [blame] | 43 | PyObject *ste_varnames; /* list of function parameters */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 44 | PyObject *ste_children; /* list of child blocks */ |
Benjamin Peterson | d9c8702 | 2012-10-31 20:26:20 -0400 | [diff] [blame] | 45 | PyObject *ste_directives;/* locations of global and nonlocal statements */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | _Py_block_ty ste_type; /* module, class, or function */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 */ |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 52 | unsigned ste_coroutine : 1; /* true if namespace is a coroutine */ |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 53 | unsigned ste_comprehension : 1; /* true if namespace is a list comprehension */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 58 | unsigned ste_needs_class_closure : 1; /* for class scopes, true if a |
| 59 | closure over __class__ |
| 60 | should be created */ |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 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 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | int ste_lineno; /* first line of block */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 64 | int ste_col_offset; /* offset of first line of block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | int ste_opt_lineno; /* lineno of last exec or import * */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 66 | int ste_opt_col_offset; /* offset of last exec or import * */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | struct symtable *ste_table; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 68 | } PySTEntryObject; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 69 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 70 | PyAPI_DATA(PyTypeObject) PySTEntry_Type; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 71 | |
Dong-hee Na | d905df7 | 2020-02-14 02:37:17 +0900 | [diff] [blame] | 72 | #define PySTEntry_Check(op) Py_IS_TYPE(op, &PySTEntry_Type) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 73 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 74 | PyAPI_FUNC(int) PyST_GetScope(PySTEntryObject *, PyObject *); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 75 | |
Victor Stinner | 00676d1 | 2010-12-27 01:49:31 +0000 | [diff] [blame] | 76 | PyAPI_FUNC(struct symtable *) PySymtable_Build( |
| 77 | mod_ty mod, |
| 78 | const char *filename, /* decoded from the filesystem encoding */ |
| 79 | PyFutureFeatures *future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 80 | PyAPI_FUNC(struct symtable *) PySymtable_BuildObject( |
| 81 | mod_ty mod, |
| 82 | PyObject *filename, |
| 83 | PyFutureFeatures *future); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 84 | PyAPI_FUNC(PySTEntryObject *) PySymtable_Lookup(struct symtable *, void *); |
| 85 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 86 | PyAPI_FUNC(void) PySymtable_Free(struct symtable *); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 87 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 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 */ |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 93 | #define DEF_NONLOCAL 2<<2 /* nonlocal stmt */ |
| 94 | #define USE 2<<3 /* name is used */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 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 */ |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 98 | #define DEF_ANNOT 2<<7 /* this name is annotated */ |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 99 | #define DEF_COMP_ITER 2<<8 /* this name is a comprehension iteration variable */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 100 | |
Jeremy Hylton | 99858b5 | 2001-02-28 23:03:39 +0000 | [diff] [blame] | 101 | #define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT) |
| 102 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 103 | /* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | table. GLOBAL is returned from PyST_GetScope() for either of them. |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 105 | It is stored in ste_symbols at bits 12-15. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 106 | */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 107 | #define SCOPE_OFFSET 11 |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 108 | #define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL) |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 109 | |
| 110 | #define LOCAL 1 |
| 111 | #define GLOBAL_EXPLICIT 2 |
| 112 | #define GLOBAL_IMPLICIT 3 |
| 113 | #define FREE 4 |
| 114 | #define CELL 5 |
| 115 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 116 | #define GENERATOR 1 |
| 117 | #define GENERATOR_EXPRESSION 2 |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 118 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 119 | #ifdef __cplusplus |
| 120 | } |
| 121 | #endif |
| 122 | #endif /* !Py_SYMTABLE_H */ |
Victor Stinner | 5f2df88 | 2018-11-12 00:56:19 +0100 | [diff] [blame] | 123 | #endif /* !Py_LIMITED_API */ |