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