blob: 2d427aa68999a330dd357738e00b4f0474b4ecb8 [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.
17
18 The slots st_cur_XXX pointers always refer to the current code
19 block. The st_cur slot is the symbol dictionary. The st_cur_id
20 slot is the id is the key in st_symbols. The st_cur_name slot is
21 the name of the current scope. The st_cur_type slot is one of
22 TYPE_FUNCTION, TYPE_CLASS, or TYPE_MODULE. The st_cur_children is
23 a list of the ids of the current node's children.
24
25 The st_symbols slot is a dictionary that maps code block ids to
26 symbol dictionaries. The keys are generated by a counter that is
27 incremented each time a new code block is found. The counter is
28 identifies a specific scope, because both passes walk the parse
29 tree in the same order.
30
31 The st_varnames slot is a dictionary that maps code block ids to
32 parameter lists. The st_global slot always refers to the symbol
33 dictionary for the module.
34
35 The st_children slot is a dictionary that maps ids to a list
36 containing the ids of its children.
37
38 If st_keep is true then the namespace info pushed on st_stack will
39 also be stored in st_scopes. This is useful if the symbol table is
40 being passed to something other than the compiler.
41*/
42
43struct symtable {
44 int st_pass; /* pass == 1 or 2 */
45 int st_keep; /* true if symtable will be returned */
46 PyObject *st_symbols; /* dictionary of symbol tables */
47 PyObject *st_varnames; /* dictionary of parameter lists */
48 PyObject *st_stack; /* stack of namespace info */
49 PyObject *st_scopes; /* dictionary of namespace info */
50 PyObject *st_children; /* dictionary (id=[ids]) */
51 PyObject *st_cur; /* borrowed ref to dict in st_symbols */
52 PyObject *st_cur_name; /* string, name of current scope */
53 PyObject *st_cur_id; /* int id of current code block */
54 PyObject *st_cur_children; /* ref to current children list */
55 int st_cur_type; /* type of current scope */
56 int st_cur_lineno; /* line number where current scope begins */
57 PyObject *st_global; /* borrowed ref to MODULE in st_symbols */
58 int st_nscopes; /* number of scopes */
59 int st_errors; /* number of errors */
60 char *st_private; /* name of current class or NULL */
61 int st_tmpname; /* temporary name counter */
62 int st_nested; /* bool (true if nested scope) */
63};
64
65DL_IMPORT(struct symtable *) PyNode_CompileSymtable(struct _node *, char *);
66DL_IMPORT(void) PySymtable_Free(struct symtable *);
67
68
69#define TOP "global"
70#define NOOPT ".noopt"
71
72/* Flags for def-use information */
73
74#define DEF_GLOBAL 1 /* global stmt */
75#define DEF_LOCAL 2 /* assignment in code block */
76#define DEF_PARAM 2<<1 /* formal parameter */
77#define USE 2<<2 /* name is used */
78#define DEF_STAR 2<<3 /* parameter is star arg */
79#define DEF_DOUBLESTAR 2<<4 /* parameter is star-star arg */
80#define DEF_INTUPLE 2<<5 /* name defined in tuple in parameters */
81#define DEF_FREE 2<<6 /* name used by not defined in nested scope */
82#define DEF_FREE_GLOBAL 2<<7 /* free variable is actually implicit global */
83#define DEF_FREE_CLASS 2<<8 /* free variable from class's method */
84#define DEF_IMPORT 2<<9 /* assignment occurred via import */
85
86#define TYPE_FUNCTION 1
87#define TYPE_CLASS 2
88#define TYPE_MODULE 3
89
90#define LOCAL 1
91#define GLOBAL_EXPLICIT 2
92#define GLOBAL_IMPLICIT 3
93#define FREE 4
94#define CELL 5
95
96#ifdef __cplusplus
97}
98#endif
99#endif /* !Py_SYMTABLE_H */