blob: beea9f50b64e5147bebd755235f74c21340e403e [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 */
Jeremy Hylton5acc0c02001-02-02 20:01:10 +000046 char *st_filename; /* name of file being compiled */
Jeremy Hylton4b38da62001-02-02 18:19:15 +000047 PyObject *st_symbols; /* dictionary of symbol tables */
48 PyObject *st_varnames; /* dictionary of parameter lists */
49 PyObject *st_stack; /* stack of namespace info */
50 PyObject *st_scopes; /* dictionary of namespace info */
51 PyObject *st_children; /* dictionary (id=[ids]) */
52 PyObject *st_cur; /* borrowed ref to dict in st_symbols */
53 PyObject *st_cur_name; /* string, name of current scope */
54 PyObject *st_cur_id; /* int id of current code block */
55 PyObject *st_cur_children; /* ref to current children list */
56 int st_cur_type; /* type of current scope */
57 int st_cur_lineno; /* line number where current scope begins */
58 PyObject *st_global; /* borrowed ref to MODULE in st_symbols */
59 int st_nscopes; /* number of scopes */
60 int st_errors; /* number of errors */
61 char *st_private; /* name of current class or NULL */
62 int st_tmpname; /* temporary name counter */
63 int st_nested; /* bool (true if nested scope) */
64};
65
66DL_IMPORT(struct symtable *) PyNode_CompileSymtable(struct _node *, char *);
67DL_IMPORT(void) PySymtable_Free(struct symtable *);
68
69
70#define TOP "global"
71#define NOOPT ".noopt"
72
73/* Flags for def-use information */
74
75#define DEF_GLOBAL 1 /* global stmt */
76#define DEF_LOCAL 2 /* assignment in code block */
77#define DEF_PARAM 2<<1 /* formal parameter */
78#define USE 2<<2 /* name is used */
79#define DEF_STAR 2<<3 /* parameter is star arg */
80#define DEF_DOUBLESTAR 2<<4 /* parameter is star-star arg */
81#define DEF_INTUPLE 2<<5 /* name defined in tuple in parameters */
82#define DEF_FREE 2<<6 /* name used by not defined in nested scope */
83#define DEF_FREE_GLOBAL 2<<7 /* free variable is actually implicit global */
84#define DEF_FREE_CLASS 2<<8 /* free variable from class's method */
85#define DEF_IMPORT 2<<9 /* assignment occurred via import */
86
87#define TYPE_FUNCTION 1
88#define TYPE_CLASS 2
89#define TYPE_MODULE 3
90
91#define LOCAL 1
92#define GLOBAL_EXPLICIT 2
93#define GLOBAL_IMPLICIT 3
94#define FREE 4
95#define CELL 5
96
97#ifdef __cplusplus
98}
99#endif
100#endif /* !Py_SYMTABLE_H */