| Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 1 | #ifndef Py_SYMTABLE_H | 
 | 2 | #define Py_SYMTABLE_H | 
 | 3 | #ifdef __cplusplus | 
 | 4 | extern "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 Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 17 | */ | 
 | 18 |  | 
| Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 19 | struct _symtable_entry; | 
 | 20 |  | 
| Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 21 | struct symtable { | 
 | 22 | 	int st_pass;             /* pass == 1 or 2 */ | 
| Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 23 | 	const char *st_filename; /* name of file being compiled */ | 
| Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 24 | 	struct _symtable_entry *st_cur; /* current symbol table entry */ | 
 | 25 | 	PyObject *st_symbols;    /* dictionary of symbol table entries */ | 
| Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 26 |         PyObject *st_stack;      /* stack of namespace info */ | 
| Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 27 | 	PyObject *st_global;     /* borrowed ref to MODULE in st_symbols */ | 
 | 28 | 	int st_nscopes;          /* number of scopes */ | 
 | 29 | 	int st_errors;           /* number of errors */ | 
 | 30 | 	char *st_private;        /* name of current class or NULL */ | 
| Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 31 | 	PyFutureFeatures *st_future; /* module's future features */ | 
| Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 32 | }; | 
 | 33 |  | 
| Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 34 | typedef struct _symtable_entry { | 
 | 35 | 	PyObject_HEAD | 
 | 36 | 	PyObject *ste_id;        /* int: key in st_symbols) */ | 
 | 37 | 	PyObject *ste_symbols;   /* dict: name to flags) */ | 
 | 38 | 	PyObject *ste_name;      /* string: name of scope */ | 
 | 39 | 	PyObject *ste_varnames;  /* list of variable names */ | 
 | 40 | 	PyObject *ste_children;  /* list of child ids */ | 
 | 41 | 	int ste_type;            /* module, class, or function */ | 
 | 42 | 	int ste_lineno;          /* first line of scope */ | 
| Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 43 | 	int ste_optimized;       /* true if namespace can't be optimized */ | 
| Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 44 | 	int ste_nested;          /* true if scope is nested */ | 
 | 45 | 	int ste_child_free;      /* true if a child scope has free variables, | 
 | 46 | 				    including free refs to globals */ | 
| Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 47 | 	int ste_generator;       /* true if namespace is a generator */ | 
| Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 48 | 	int ste_opt_lineno;      /* lineno of last exec or import * */ | 
| Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 49 | 	int ste_tmpname;          /* temporary name counter */ | 
| Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 50 | 	struct symtable *ste_table; | 
 | 51 | } PySymtableEntryObject; | 
 | 52 |  | 
| Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 53 | PyAPI_DATA(PyTypeObject) PySymtableEntry_Type; | 
| Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 54 |  | 
 | 55 | #define PySymtableEntry_Check(op) ((op)->ob_type == &PySymtableEntry_Type) | 
 | 56 |  | 
| Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 57 | PyAPI_FUNC(PyObject *) PySymtableEntry_New(struct symtable *, | 
| Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 58 | 						 char *, int, int); | 
 | 59 |  | 
| Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 60 | PyAPI_FUNC(struct symtable *) PyNode_CompileSymtable(struct _node *, const char *); | 
| Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 61 | PyAPI_FUNC(void) PySymtable_Free(struct symtable *); | 
| Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 62 |  | 
 | 63 |  | 
 | 64 | #define TOP "global" | 
| Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 65 |  | 
 | 66 | /* Flags for def-use information */ | 
 | 67 |  | 
 | 68 | #define DEF_GLOBAL 1           /* global stmt */ | 
 | 69 | #define DEF_LOCAL 2            /* assignment in code block */ | 
 | 70 | #define DEF_PARAM 2<<1         /* formal parameter */ | 
 | 71 | #define USE 2<<2               /* name is used */ | 
 | 72 | #define DEF_STAR 2<<3          /* parameter is star arg */ | 
 | 73 | #define DEF_DOUBLESTAR 2<<4    /* parameter is star-star arg */ | 
 | 74 | #define DEF_INTUPLE 2<<5       /* name defined in tuple in parameters */ | 
| Jeremy Hylton | 99858b5 | 2001-02-28 23:03:39 +0000 | [diff] [blame] | 75 | #define DEF_FREE 2<<6          /* name used but not defined in nested scope */ | 
| Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 76 | #define DEF_FREE_GLOBAL 2<<7   /* free variable is actually implicit global */ | 
 | 77 | #define DEF_FREE_CLASS 2<<8    /* free variable from class's method */ | 
 | 78 | #define DEF_IMPORT 2<<9        /* assignment occurred via import */ | 
 | 79 |  | 
| Jeremy Hylton | 99858b5 | 2001-02-28 23:03:39 +0000 | [diff] [blame] | 80 | #define DEF_BOUND (DEF_LOCAL | DEF_PARAM | DEF_IMPORT) | 
 | 81 |  | 
| Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 82 | #define TYPE_FUNCTION 1 | 
 | 83 | #define TYPE_CLASS 2 | 
 | 84 | #define TYPE_MODULE 3 | 
 | 85 |  | 
 | 86 | #define LOCAL 1 | 
 | 87 | #define GLOBAL_EXPLICIT 2 | 
 | 88 | #define GLOBAL_IMPLICIT 3 | 
 | 89 | #define FREE 4 | 
 | 90 | #define CELL 5 | 
 | 91 |  | 
| Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 92 | #define OPT_IMPORT_STAR 1 | 
 | 93 | #define OPT_EXEC 2 | 
 | 94 | #define OPT_BARE_EXEC 4 | 
 | 95 |  | 
| Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 96 | #ifdef __cplusplus | 
 | 97 | } | 
 | 98 | #endif | 
 | 99 | #endif /* !Py_SYMTABLE_H */ |