summaryrefslogtreecommitdiffstats
path: root/Include
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2001-02-02 18:19:15 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2001-02-02 18:19:15 (GMT)
commit4b38da664c107bc08768235a66621429beef5444 (patch)
tree8fc2135063c382b2987faa8e645f97b4ede2c745 /Include
parent033f31270cb424efdb72abb03d925235f18d034d (diff)
downloadcpython-4b38da664c107bc08768235a66621429beef5444.zip
cpython-4b38da664c107bc08768235a66621429beef5444.tar.gz
cpython-4b38da664c107bc08768235a66621429beef5444.tar.bz2
Move a bunch of definitions that were internal to compile.c to
symtable.h, so that they can be used by external module. Improve error handling in symtable_enter_scope(), which return an error code that went unchecked by most callers. XXX The error handling in symtable code is sloppy in general. Modify symtable to record the line number that begins each scope. This can help to identify which code block is being referred to when multiple blocks are bound to the same name. Add st_scopes dict that is used to preserve scope info when PyNode_CompileSymtable() is called. Otherwise, this information is tossed as soon as it is no longer needed. Add Py_SymtableString() to pythonrun; analogous to Py_CompileString().
Diffstat (limited to 'Include')
-rw-r--r--Include/pythonrun.h1
-rw-r--r--Include/symtable.h99
2 files changed, 100 insertions, 0 deletions
diff --git a/Include/pythonrun.h b/Include/pythonrun.h
index b963a06..13ed471 100644
--- a/Include/pythonrun.h
+++ b/Include/pythonrun.h
@@ -37,6 +37,7 @@ DL_IMPORT(PyObject *) PyRun_FileEx(FILE *, char *, int,
PyObject *, PyObject *, int);
DL_IMPORT(PyObject *) Py_CompileString(char *, char *, int);
+DL_IMPORT(struct symtable *) Py_SymtableString(char *, char *, int);
DL_IMPORT(void) PyErr_Print(void);
DL_IMPORT(void) PyErr_PrintEx(int);
diff --git a/Include/symtable.h b/Include/symtable.h
new file mode 100644
index 0000000..2d427aa
--- /dev/null
+++ b/Include/symtable.h
@@ -0,0 +1,99 @@
+#ifndef Py_SYMTABLE_H
+#define Py_SYMTABLE_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* A symbol table is constructed each time PyNode_Compile() is
+ called. The table walks the entire parse tree and identifies each
+ use or definition of a variable.
+
+ The symbol table contains a dictionary for each code block in a
+ module: The symbol dictionary for the block. They keys of these
+ dictionaries are the name of all variables used or defined in the
+ block; the integer values are used to store several flags,
+ e.g. DEF_PARAM indicates that a variable is a parameter to a
+ function.
+
+ The slots st_cur_XXX pointers always refer to the current code
+ block. The st_cur slot is the symbol dictionary. The st_cur_id
+ slot is the id is the key in st_symbols. The st_cur_name slot is
+ the name of the current scope. The st_cur_type slot is one of
+ TYPE_FUNCTION, TYPE_CLASS, or TYPE_MODULE. The st_cur_children is
+ a list of the ids of the current node's children.
+
+ The st_symbols slot is a dictionary that maps code block ids to
+ symbol dictionaries. The keys are generated by a counter that is
+ incremented each time a new code block is found. The counter is
+ identifies a specific scope, because both passes walk the parse
+ tree in the same order.
+
+ The st_varnames slot is a dictionary that maps code block ids to
+ parameter lists. The st_global slot always refers to the symbol
+ dictionary for the module.
+
+ The st_children slot is a dictionary that maps ids to a list
+ containing the ids of its children.
+
+ If st_keep is true then the namespace info pushed on st_stack will
+ also be stored in st_scopes. This is useful if the symbol table is
+ being passed to something other than the compiler.
+*/
+
+struct symtable {
+ int st_pass; /* pass == 1 or 2 */
+ int st_keep; /* true if symtable will be returned */
+ PyObject *st_symbols; /* dictionary of symbol tables */
+ PyObject *st_varnames; /* dictionary of parameter lists */
+ PyObject *st_stack; /* stack of namespace info */
+ PyObject *st_scopes; /* dictionary of namespace info */
+ PyObject *st_children; /* dictionary (id=[ids]) */
+ PyObject *st_cur; /* borrowed ref to dict in st_symbols */
+ PyObject *st_cur_name; /* string, name of current scope */
+ PyObject *st_cur_id; /* int id of current code block */
+ PyObject *st_cur_children; /* ref to current children list */
+ int st_cur_type; /* type of current scope */
+ int st_cur_lineno; /* line number where current scope begins */
+ PyObject *st_global; /* borrowed ref to MODULE in st_symbols */
+ int st_nscopes; /* number of scopes */
+ int st_errors; /* number of errors */
+ char *st_private; /* name of current class or NULL */
+ int st_tmpname; /* temporary name counter */
+ int st_nested; /* bool (true if nested scope) */
+};
+
+DL_IMPORT(struct symtable *) PyNode_CompileSymtable(struct _node *, char *);
+DL_IMPORT(void) PySymtable_Free(struct symtable *);
+
+
+#define TOP "global"
+#define NOOPT ".noopt"
+
+/* Flags for def-use information */
+
+#define DEF_GLOBAL 1 /* global stmt */
+#define DEF_LOCAL 2 /* assignment in code block */
+#define DEF_PARAM 2<<1 /* formal parameter */
+#define USE 2<<2 /* name is used */
+#define DEF_STAR 2<<3 /* parameter is star arg */
+#define DEF_DOUBLESTAR 2<<4 /* parameter is star-star arg */
+#define DEF_INTUPLE 2<<5 /* name defined in tuple in parameters */
+#define DEF_FREE 2<<6 /* name used by not defined in nested scope */
+#define DEF_FREE_GLOBAL 2<<7 /* free variable is actually implicit global */
+#define DEF_FREE_CLASS 2<<8 /* free variable from class's method */
+#define DEF_IMPORT 2<<9 /* assignment occurred via import */
+
+#define TYPE_FUNCTION 1
+#define TYPE_CLASS 2
+#define TYPE_MODULE 3
+
+#define LOCAL 1
+#define GLOBAL_EXPLICIT 2
+#define GLOBAL_IMPLICIT 3
+#define FREE 4
+#define CELL 5
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* !Py_SYMTABLE_H */