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 |
Neal Norwitz | 090b3dd | 2006-02-28 22:36:46 +0000 | [diff] [blame] | 4 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5 | #ifdef __cplusplus |
| 6 | extern "C" { |
| 7 | #endif |
| 8 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 9 | /* XXX(ncoghlan): This is a weird mix of public names and interpreter internal |
| 10 | * names. |
| 11 | */ |
| 12 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 13 | typedef enum _block_type { FunctionBlock, ClassBlock, ModuleBlock } |
Neal Norwitz | 62c2fac | 2005-10-24 00:30:44 +0000 | [diff] [blame] | 14 | _Py_block_ty; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 15 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 16 | struct _symtable_entry; |
| 17 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 18 | struct symtable { |
Victor Stinner | 00676d1 | 2010-12-27 01:49:31 +0000 | [diff] [blame] | 19 | const char *st_filename; /* name of file being compiled, |
| 20 | decoded from the filesystem encoding */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 */ |
Eli Bendersky | dd97fbb | 2011-04-10 07:37:26 +0300 | [diff] [blame] | 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 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 30 | PyObject *st_private; /* name of current class or NULL */ |
Eli Bendersky | dd97fbb | 2011-04-10 07:37:26 +0300 | [diff] [blame] | 31 | PyFutureFeatures *st_future; /* module's future features that affect |
| 32 | the symbol table */ |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 33 | int recursion_depth; /* current recursion depth */ |
| 34 | int recursion_limit; /* recursion limit */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 37 | typedef struct _symtable_entry { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 */ |
Eli Bendersky | dd97fbb | 2011-04-10 07:37:26 +0300 | [diff] [blame] | 42 | PyObject *ste_varnames; /* list of function parameters */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 43 | PyObject *ste_children; /* list of child blocks */ |
| 44 | _Py_block_ty ste_type; /* module, class, or function */ |
| 45 | int ste_unoptimized; /* false if namespace is optimized */ |
| 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_varargs : 1; /* true if block has varargs */ |
| 52 | unsigned ste_varkeywords : 1; /* true if block has varkeywords */ |
| 53 | unsigned ste_returns_value : 1; /* true if namespace uses return with |
| 54 | an argument */ |
| 55 | int ste_lineno; /* first line of block */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 56 | int ste_col_offset; /* offset of first line of block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | int ste_opt_lineno; /* lineno of last exec or import * */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 58 | int ste_opt_col_offset; /* offset of last exec or import * */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 59 | int ste_tmpname; /* counter for listcomp temp vars */ |
| 60 | struct symtable *ste_table; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 61 | } PySTEntryObject; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 62 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 63 | PyAPI_DATA(PyTypeObject) PySTEntry_Type; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 64 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 65 | #define PySTEntry_Check(op) (Py_TYPE(op) == &PySTEntry_Type) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 66 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 67 | PyAPI_FUNC(int) PyST_GetScope(PySTEntryObject *, PyObject *); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 68 | |
Victor Stinner | 00676d1 | 2010-12-27 01:49:31 +0000 | [diff] [blame] | 69 | PyAPI_FUNC(struct symtable *) PySymtable_Build( |
| 70 | mod_ty mod, |
| 71 | const char *filename, /* decoded from the filesystem encoding */ |
| 72 | PyFutureFeatures *future); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 73 | PyAPI_FUNC(PySTEntryObject *) PySymtable_Lookup(struct symtable *, void *); |
| 74 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 75 | PyAPI_FUNC(void) PySymtable_Free(struct symtable *); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 76 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 77 | /* Flags for def-use information */ |
| 78 | |
| 79 | #define DEF_GLOBAL 1 /* global stmt */ |
| 80 | #define DEF_LOCAL 2 /* assignment in code block */ |
| 81 | #define DEF_PARAM 2<<1 /* formal parameter */ |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 82 | #define DEF_NONLOCAL 2<<2 /* nonlocal stmt */ |
| 83 | #define USE 2<<3 /* name is used */ |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 84 | #define DEF_FREE 2<<4 /* name used but not defined in nested block */ |
| 85 | #define DEF_FREE_CLASS 2<<5 /* free variable from class's method */ |
| 86 | #define DEF_IMPORT 2<<6 /* assignment occurred via import */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 87 | |
Jeremy Hylton | 99858b5 | 2001-02-28 23:03:39 +0000 | [diff] [blame] | 88 | #define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT) |
| 89 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 90 | /* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | table. GLOBAL is returned from PyST_GetScope() for either of them. |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 92 | It is stored in ste_symbols at bits 12-15. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 93 | */ |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 94 | #define SCOPE_OFFSET 11 |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 95 | #define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL) |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 96 | |
| 97 | #define LOCAL 1 |
| 98 | #define GLOBAL_EXPLICIT 2 |
| 99 | #define GLOBAL_IMPLICIT 3 |
| 100 | #define FREE 4 |
| 101 | #define CELL 5 |
| 102 | |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 103 | /* The following two names are used for the ste_unoptimized bit field */ |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 104 | #define OPT_IMPORT_STAR 1 |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 105 | #define OPT_TOPLEVEL 2 /* top-level names, including eval and exec */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 106 | |
| 107 | #define GENERATOR 1 |
| 108 | #define GENERATOR_EXPRESSION 2 |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 109 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 110 | #ifdef __cplusplus |
| 111 | } |
| 112 | #endif |
| 113 | #endif /* !Py_SYMTABLE_H */ |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 114 | #endif /* Py_LIMITED_API */ |