blob: da11603b98443efa3a4137cf981baaa19886463c [file] [log] [blame]
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001#ifndef Py_SYMTABLE_H
2#define Py_SYMTABLE_H
Neal Norwitz090b3dd2006-02-28 22:36:46 +00003
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004#ifdef __cplusplus
5extern "C" {
6#endif
7
Nick Coghlan650f0d02007-04-15 12:05:43 +00008/* XXX(ncoghlan): This is a weird mix of public names and interpreter internal
9 * names.
10 */
11
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012typedef enum _block_type { FunctionBlock, ClassBlock, ModuleBlock }
Neal Norwitz62c2fac2005-10-24 00:30:44 +000013 _Py_block_ty;
Jeremy Hylton4b38da62001-02-02 18:19:15 +000014
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000015struct _symtable_entry;
16
Jeremy Hylton4b38da62001-02-02 18:19:15 +000017struct symtable {
Nick Coghlan650f0d02007-04-15 12:05:43 +000018 const char *st_filename; /* name of file being compiled */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000019 struct _symtable_entry *st_cur; /* current symbol table entry */
Nick Coghlan650f0d02007-04-15 12:05:43 +000020 struct _symtable_entry *st_top; /* symbol table entry for module */
21 PyObject *st_blocks; /* dict: map AST node addresses
22 * to symbol table entries */
23 PyObject *st_stack; /* list: stack of namespace info */
24 PyObject *st_global; /* borrowed ref to st_top->st_symbols */
25 int st_nblocks; /* number of blocks used */
26 PyObject *st_private; /* name of current class or NULL */
27 PyFutureFeatures *st_future; /* module's future features */
Jeremy Hylton4b38da62001-02-02 18:19:15 +000028};
29
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000030typedef struct _symtable_entry {
31 PyObject_HEAD
Nick Coghlan650f0d02007-04-15 12:05:43 +000032 PyObject *ste_id; /* int: key in ste_table->st_blocks */
33 PyObject *ste_symbols; /* dict: variable names to flags */
34 PyObject *ste_name; /* string: name of current block */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000035 PyObject *ste_varnames; /* list of variable names */
Nick Coghlan650f0d02007-04-15 12:05:43 +000036 PyObject *ste_children; /* list of child blocks */
Neal Norwitz62c2fac2005-10-24 00:30:44 +000037 _Py_block_ty ste_type; /* module, class, or function */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000038 int ste_unoptimized; /* false if namespace is optimized */
Neal Norwitz8208b642006-01-07 21:25:23 +000039 unsigned ste_nested : 1; /* true if block is nested */
40 unsigned ste_free : 1; /* true if block has free variables */
41 unsigned ste_child_free : 1; /* true if a child block has free vars,
42 including free refs to globals */
43 unsigned ste_generator : 1; /* true if namespace is a generator */
44 unsigned ste_varargs : 1; /* true if block has varargs */
45 unsigned ste_varkeywords : 1; /* true if block has varkeywords */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000046 unsigned ste_returns_value : 1; /* true if namespace uses return with
47 an argument */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000048 int ste_lineno; /* first line of block */
Jeremy Hylton2e2cded2001-03-22 03:57:58 +000049 int ste_opt_lineno; /* lineno of last exec or import * */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000050 int ste_tmpname; /* counter for listcomp temp vars */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000051 struct symtable *ste_table;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000052} PySTEntryObject;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000053
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054PyAPI_DATA(PyTypeObject) PySTEntry_Type;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000055
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056#define PySTEntry_Check(op) ((op)->ob_type == &PySTEntry_Type)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000057
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000058PyAPI_FUNC(int) PyST_GetScope(PySTEntryObject *, PyObject *);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000059
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000060PyAPI_FUNC(struct symtable *) PySymtable_Build(mod_ty, const char *,
61 PyFutureFeatures *);
62PyAPI_FUNC(PySTEntryObject *) PySymtable_Lookup(struct symtable *, void *);
63
Mark Hammond91a681d2002-08-12 07:21:58 +000064PyAPI_FUNC(void) PySymtable_Free(struct symtable *);
Jeremy Hylton4b38da62001-02-02 18:19:15 +000065
Jeremy Hylton4b38da62001-02-02 18:19:15 +000066/* Flags for def-use information */
67
68#define DEF_GLOBAL 1 /* global stmt */
69#define DEF_LOCAL 2 /* assignment in code block */
70#define DEF_PARAM 2<<1 /* formal parameter */
Jeremy Hylton81e95022007-02-27 06:50:52 +000071#define DEF_NONLOCAL 2<<2 /* nonlocal stmt */
72#define USE 2<<3 /* name is used */
73#define DEF_STAR 2<<4 /* parameter is star arg */
74#define DEF_DOUBLESTAR 2<<5 /* parameter is star-star arg */
75#define DEF_INTUPLE 2<<6 /* name defined in tuple in parameters */
76#define DEF_FREE 2<<7 /* name used but not defined in nested block */
77#define DEF_FREE_GLOBAL 2<<8 /* free variable is actually implicit global */
78#define DEF_FREE_CLASS 2<<9 /* free variable from class's method */
79#define DEF_IMPORT 2<<10 /* assignment occurred via import */
Jeremy Hylton4b38da62001-02-02 18:19:15 +000080
Jeremy Hylton99858b52001-02-28 23:03:39 +000081#define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
82
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000083/* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol
84 table. GLOBAL is returned from PyST_GetScope() for either of them.
Jeremy Hylton81e95022007-02-27 06:50:52 +000085 It is stored in ste_symbols at bits 12-15.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000086*/
Nick Coghlan650f0d02007-04-15 12:05:43 +000087#define SCOPE_OFFSET 11
Jeremy Hylton81e95022007-02-27 06:50:52 +000088#define SCOPE_MASK (DEF_GLOBAL | DEF_LOCAL | DEF_PARAM | DEF_NONLOCAL)
Jeremy Hylton4b38da62001-02-02 18:19:15 +000089
90#define LOCAL 1
91#define GLOBAL_EXPLICIT 2
92#define GLOBAL_IMPLICIT 3
93#define FREE 4
94#define CELL 5
95
Georg Brandl7cae87c2006-09-06 06:51:57 +000096/* The following two names are used for the ste_unoptimized bit field */
Jeremy Hylton29906ee2001-02-27 04:23:34 +000097#define OPT_IMPORT_STAR 1
Georg Brandl7cae87c2006-09-06 06:51:57 +000098#define OPT_TOPLEVEL 2 /* top-level names, including eval and exec */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099
100#define GENERATOR 1
101#define GENERATOR_EXPRESSION 2
Jeremy Hylton29906ee2001-02-27 04:23:34 +0000102
Jeremy Hylton4b38da62001-02-02 18:19:15 +0000103#ifdef __cplusplus
104}
105#endif
106#endif /* !Py_SYMTABLE_H */