blob: 65775b0c8ab92c9dcd79762d1a5fc370e7d01bd9 [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
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008typedef enum _block_type { FunctionBlock, ClassBlock, ModuleBlock }
Neal Norwitz62c2fac2005-10-24 00:30:44 +00009 _Py_block_ty;
Jeremy Hylton4b38da62001-02-02 18:19:15 +000010
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000011struct _symtable_entry;
12
Jeremy Hylton4b38da62001-02-02 18:19:15 +000013struct symtable {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000014 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 int st_tmpname; /* temporary name counter */
23 PyFutureFeatures *st_future; /* module's future features */
Jeremy Hylton4b38da62001-02-02 18:19:15 +000024};
25
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000026typedef struct _symtable_entry {
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000027 PyObject_HEAD
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 */
31 PyObject *ste_varnames; /* list of variable names */
32 PyObject *ste_children; /* list of child ids */
33 _Py_block_ty ste_type; /* module, class, or function */
34 int ste_unoptimized; /* false if namespace is optimized */
35 int ste_nested; /* 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 */
42 unsigned ste_returns_value : 1; /* true if namespace uses return with
43 an argument */
44 int ste_lineno; /* first line of block */
45 int ste_opt_lineno; /* lineno of last exec or import * */
46 int ste_tmpname; /* counter for listcomp temp vars */
47 struct symtable *ste_table;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000048} PySTEntryObject;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000049
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000050PyAPI_DATA(PyTypeObject) PySTEntry_Type;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000051
Christian Heimese93237d2007-12-19 02:37:44 +000052#define PySTEntry_Check(op) (Py_TYPE(op) == &PySTEntry_Type)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000053
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054PyAPI_FUNC(int) PyST_GetScope(PySTEntryObject *, PyObject *);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000055
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000056PyAPI_FUNC(struct symtable *) PySymtable_Build(mod_ty, const char *,
57 PyFutureFeatures *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000058PyAPI_FUNC(PySTEntryObject *) PySymtable_Lookup(struct symtable *, void *);
59
Mark Hammond91a681d2002-08-12 07:21:58 +000060PyAPI_FUNC(void) PySymtable_Free(struct symtable *);
Jeremy Hylton4b38da62001-02-02 18:19:15 +000061
Jeremy Hylton4b38da62001-02-02 18:19:15 +000062/* 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 Hylton3e0055f2005-10-20 19:59:25 +000071#define DEF_FREE 2<<6 /* name used but not defined in nested block */
Jeremy Hylton4b38da62001-02-02 18:19:15 +000072#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 Hylton99858b52001-02-28 23:03:39 +000076#define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT)
77
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000078/* GLOBAL_EXPLICIT and GLOBAL_IMPLICIT are used internally by the symbol
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000079 table. GLOBAL is returned from PyST_GetScope() for either of them.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000080 It is stored in ste_symbols at bits 12-14.
81*/
82#define SCOPE_OFF 11
83#define SCOPE_MASK 7
Jeremy Hylton4b38da62001-02-02 18:19:15 +000084
85#define LOCAL 1
86#define GLOBAL_EXPLICIT 2
87#define GLOBAL_IMPLICIT 3
88#define FREE 4
89#define CELL 5
90
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091/* The following three names are used for the ste_unoptimized bit field */
Jeremy Hylton29906ee2001-02-27 04:23:34 +000092#define OPT_IMPORT_STAR 1
93#define OPT_EXEC 2
94#define OPT_BARE_EXEC 4
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095#define OPT_TOPLEVEL 8 /* top-level names, including eval and exec */
96
97#define GENERATOR 1
98#define GENERATOR_EXPRESSION 2
Jeremy Hylton29906ee2001-02-27 04:23:34 +000099
Jeremy Hylton4b38da62001-02-02 18:19:15 +0000100#ifdef __cplusplus
101}
102#endif
103#endif /* !Py_SYMTABLE_H */