blob: eb0be1a45f58999136408ce0ca92082139098419 [file] [log] [blame]
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001#ifndef Py_SYMTABLE_H
2#define Py_SYMTABLE_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7/* A symbol table is constructed each time PyNode_Compile() is
8 called. The table walks the entire parse tree and identifies each
9 use or definition of a variable.
10
11 The symbol table contains a dictionary for each code block in a
12 module: The symbol dictionary for the block. They keys of these
13 dictionaries are the name of all variables used or defined in the
14 block; the integer values are used to store several flags,
15 e.g. DEF_PARAM indicates that a variable is a parameter to a
16 function.
Jeremy Hylton4b38da62001-02-02 18:19:15 +000017*/
18
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000019struct _symtable_entry;
20
Jeremy Hylton4b38da62001-02-02 18:19:15 +000021struct symtable {
22 int st_pass; /* pass == 1 or 2 */
Jeremy Hylton29906ee2001-02-27 04:23:34 +000023 int st_nested_scopes; /* true if nested scopes are enabled */
Jeremy Hylton5acc0c02001-02-02 20:01:10 +000024 char *st_filename; /* name of file being compiled */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000025 struct _symtable_entry *st_cur; /* current symbol table entry */
26 PyObject *st_symbols; /* dictionary of symbol table entries */
Jeremy Hylton4b38da62001-02-02 18:19:15 +000027 PyObject *st_stack; /* stack of namespace info */
Jeremy Hylton4b38da62001-02-02 18:19:15 +000028 PyObject *st_global; /* borrowed ref to MODULE in st_symbols */
29 int st_nscopes; /* number of scopes */
30 int st_errors; /* number of errors */
31 char *st_private; /* name of current class or NULL */
32 int st_tmpname; /* temporary name counter */
Jeremy Hylton4b38da62001-02-02 18:19:15 +000033};
34
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000035typedef struct _symtable_entry {
36 PyObject_HEAD
37 PyObject *ste_id; /* int: key in st_symbols) */
38 PyObject *ste_symbols; /* dict: name to flags) */
39 PyObject *ste_name; /* string: name of scope */
40 PyObject *ste_varnames; /* list of variable names */
41 PyObject *ste_children; /* list of child ids */
42 int ste_type; /* module, class, or function */
43 int ste_lineno; /* first line of scope */
Jeremy Hylton29906ee2001-02-27 04:23:34 +000044 int ste_optimized; /* true if namespace can't be optimized */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000045 int ste_nested; /* true if scope is nested */
46 int ste_child_free; /* true if a child scope has free variables,
47 including free refs to globals */
48 struct symtable *ste_table;
49} PySymtableEntryObject;
50
51extern DL_IMPORT(PyTypeObject) PySymtableEntry_Type;
52
53#define PySymtableEntry_Check(op) ((op)->ob_type == &PySymtableEntry_Type)
54
55extern DL_IMPORT(PyObject *) PySymtableEntry_New(struct symtable *,
56 char *, int, int);
57
Jeremy Hylton4b38da62001-02-02 18:19:15 +000058DL_IMPORT(struct symtable *) PyNode_CompileSymtable(struct _node *, char *);
59DL_IMPORT(void) PySymtable_Free(struct symtable *);
60
61
62#define TOP "global"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000063
64/* Flags for def-use information */
65
66#define DEF_GLOBAL 1 /* global stmt */
67#define DEF_LOCAL 2 /* assignment in code block */
68#define DEF_PARAM 2<<1 /* formal parameter */
69#define USE 2<<2 /* name is used */
70#define DEF_STAR 2<<3 /* parameter is star arg */
71#define DEF_DOUBLESTAR 2<<4 /* parameter is star-star arg */
72#define DEF_INTUPLE 2<<5 /* name defined in tuple in parameters */
73#define DEF_FREE 2<<6 /* name used by not defined in nested scope */
74#define DEF_FREE_GLOBAL 2<<7 /* free variable is actually implicit global */
75#define DEF_FREE_CLASS 2<<8 /* free variable from class's method */
76#define DEF_IMPORT 2<<9 /* assignment occurred via import */
77
78#define TYPE_FUNCTION 1
79#define TYPE_CLASS 2
80#define TYPE_MODULE 3
81
82#define LOCAL 1
83#define GLOBAL_EXPLICIT 2
84#define GLOBAL_IMPLICIT 3
85#define FREE 4
86#define CELL 5
87
Jeremy Hylton29906ee2001-02-27 04:23:34 +000088#define OPT_IMPORT_STAR 1
89#define OPT_EXEC 2
90#define OPT_BARE_EXEC 4
91
Jeremy Hylton4b38da62001-02-02 18:19:15 +000092#ifdef __cplusplus
93}
94#endif
95#endif /* !Py_SYMTABLE_H */