Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 1 | #ifndef Py_SYMTABLE_H |
| 2 | #define Py_SYMTABLE_H |
Neal Norwitz | 090b3dd | 2006-02-28 22:36:46 +0000 | [diff] [blame] | 3 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4 | #ifdef __cplusplus |
| 5 | extern "C" { |
| 6 | #endif |
| 7 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 8 | typedef enum _block_type { FunctionBlock, ClassBlock, ModuleBlock } |
Neal Norwitz | 62c2fac | 2005-10-24 00:30:44 +0000 | [diff] [blame] | 9 | _Py_block_ty; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 10 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 11 | struct _symtable_entry; |
| 12 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 13 | struct symtable { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 14 | const char *st_filename; /* name of file being compiled */ |
| 15 | struct _symtable_entry *st_cur; /* current symbol table entry */ |
| 16 | struct _symtable_entry *st_top; /* module entry */ |
| 17 | PyObject *st_symbols; /* dictionary of symbol table entries */ |
| 18 | PyObject *st_stack; /* stack of namespace info */ |
| 19 | PyObject *st_global; /* borrowed ref to MODULE in st_symbols */ |
| 20 | int st_nblocks; /* number of blocks */ |
| 21 | PyObject *st_private; /* name of current class or NULL */ |
| 22 | PyFutureFeatures *st_future; /* module's future features */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 23 | }; |
| 24 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 25 | typedef struct _symtable_entry { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 26 | PyObject_HEAD |
| 27 | PyObject *ste_id; /* int: key in st_symbols */ |
| 28 | PyObject *ste_symbols; /* dict: name to flags */ |
| 29 | PyObject *ste_name; /* string: name of block */ |
| 30 | PyObject *ste_varnames; /* list of variable names */ |
| 31 | PyObject *ste_children; /* list of child ids */ |
| 32 | _Py_block_ty ste_type; /* module, class, or function */ |
| 33 | int ste_unoptimized; /* false if namespace is optimized */ |
| 34 | int ste_nested; /* true if block is nested */ |
| 35 | unsigned ste_free : 1; /* true if block has free variables */ |
| 36 | unsigned ste_child_free : 1; /* true if a child block has free vars, |
| 37 | including free refs to globals */ |
| 38 | unsigned ste_generator : 1; /* true if namespace is a generator */ |
| 39 | unsigned ste_varargs : 1; /* true if block has varargs */ |
| 40 | unsigned ste_varkeywords : 1; /* true if block has varkeywords */ |
| 41 | unsigned ste_returns_value : 1; /* true if namespace uses return with |
| 42 | an argument */ |
| 43 | int ste_lineno; /* first line of block */ |
| 44 | int ste_opt_lineno; /* lineno of last exec or import * */ |
| 45 | int ste_tmpname; /* counter for listcomp temp vars */ |
| 46 | struct symtable *ste_table; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 47 | } PySTEntryObject; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 48 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 49 | PyAPI_DATA(PyTypeObject) PySTEntry_Type; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 50 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 51 | #define PySTEntry_Check(op) (Py_TYPE(op) == &PySTEntry_Type) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 52 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 53 | PyAPI_FUNC(int) PyST_GetScope(PySTEntryObject *, PyObject *); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 54 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 55 | PyAPI_FUNC(struct symtable *) PySymtable_Build(mod_ty, const char *, |
| 56 | PyFutureFeatures *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 57 | PyAPI_FUNC(PySTEntryObject *) PySymtable_Lookup(struct symtable *, void *); |
| 58 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 59 | PyAPI_FUNC(void) PySymtable_Free(struct symtable *); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 60 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 61 | /* Flags for def-use information */ |
| 62 | |
| 63 | #define DEF_GLOBAL 1 /* global stmt */ |
| 64 | #define DEF_LOCAL 2 /* assignment in code block */ |
| 65 | #define DEF_PARAM 2<<1 /* formal parameter */ |
| 66 | #define USE 2<<2 /* name is used */ |
Benjamin Peterson | 94819cf | 2009-06-23 03:01:56 +0000 | [diff] [blame] | 67 | #define DEF_FREE 2<<3 /* name used but not defined in nested block */ |
| 68 | #define DEF_FREE_CLASS 2<<4 /* free variable from class's method */ |
| 69 | #define DEF_IMPORT 2<<5 /* assignment occurred via import */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 70 | |
Jeremy Hylton | 99858b5 | 2001-02-28 23:03:39 +0000 | [diff] [blame] | 71 | #define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT) |
| 72 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 73 | /* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 74 | table. GLOBAL is returned from PyST_GetScope() for either of them. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 75 | It is stored in ste_symbols at bits 12-14. |
| 76 | */ |
| 77 | #define SCOPE_OFF 11 |
| 78 | #define SCOPE_MASK 7 |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 79 | |
| 80 | #define LOCAL 1 |
| 81 | #define GLOBAL_EXPLICIT 2 |
| 82 | #define GLOBAL_IMPLICIT 3 |
| 83 | #define FREE 4 |
| 84 | #define CELL 5 |
| 85 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 86 | /* The following three names are used for the ste_unoptimized bit field */ |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 87 | #define OPT_IMPORT_STAR 1 |
| 88 | #define OPT_EXEC 2 |
| 89 | #define OPT_BARE_EXEC 4 |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 90 | #define OPT_TOPLEVEL 8 /* top-level names, including eval and exec */ |
| 91 | |
| 92 | #define GENERATOR 1 |
| 93 | #define GENERATOR_EXPRESSION 2 |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 94 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 95 | #ifdef __cplusplus |
| 96 | } |
| 97 | #endif |
| 98 | #endif /* !Py_SYMTABLE_H */ |