1 /**
2   Mirror _symtable.h
3   */
4 module deimos.python.symtable;
5 
6 import deimos.python.pyport;
7 import deimos.python.object;
8 import deimos.python.compile;
9 import deimos.python.ast;
10 import deimos.python.node;
11 
12 extern(C):
13 
14 /// _
15 struct _symtable_entry;
16 
17 version(Python_2_5_Or_Later) {
18     /// Availability: >= 2.5
19     enum _Py_block_ty{
20         /// _
21         FunctionBlock,
22         /// _
23         ClassBlock,
24         /// _
25         ModuleBlock
26     }
27 }
28 
29 /// _
30 struct symtable {
31     version(Python_2_5_Or_Later) {
32     }else{
33         /** pass == 1 or 2 */
34         /// Availability: 2.4
35         int st_pass;
36     }
37     /** name of file being compiled */
38     const(char)*st_filename;
39     /** current symbol table entry */
40     _symtable_entry* st_cur;
41     version(Python_2_5_Or_Later) {
42         /* module entry */
43         /// Availability: >= 2.5
44         _symtable_entry *st_top;
45     }
46     /** dictionary of symbol table entries */
47     PyObject* st_symbols;
48     /** stack of namespace info */
49     PyObject* st_stack;
50     /** borrowed ref to MODULE in st_symbols */
51     Borrowed!PyObject* st_global;
52     version(Python_2_5_Or_Later) {
53         /** number of blocks */
54         /// Availability: >= 2.5
55         int st_nblocks;
56         /** name of current class or NULL */
57         /// Availability: >= 2.5
58         PyObject* st_private;
59         /** temporary name counter */
60         /// Availability: >= 2.5
61         int st_tmpname;
62     }else{
63         /** number of scopes */
64         /// Availability: 2.4
65         int st_nscopes;
66         /** number of errors */
67         /// Availability: 2.4
68         int st_errors;
69         /** name of current class or NULL */
70         /// Availability: 2.4
71         char* st_private;
72     }
73     /** module's future features */
74     PyFutureFeatures* st_future;
75 };
76 
77 /// _
78 struct PySTEntryObject{
79 	mixin PyObject_HEAD;
80         /** int: key in st_symbols) */
81 	PyObject* ste_id;
82         /** dict: name to flags) */
83 	PyObject* ste_symbols;
84         /** string: name of scope */
85 	PyObject* ste_name;
86         /** list of variable names */
87 	PyObject* ste_varnames;
88         /** list of child ids */
89 	PyObject* ste_children;
90         version(Python_2_5_Or_Later) {
91             /** module, class, or function */
92             _Py_block_ty ste_type;
93             /** false if namespace is optimized */
94             /// Availability: >= 2.5
95             int ste_unoptimized;
96             /** true if block is nested */
97             uint ste_nested ;
98             /** true if block has free variables */
99             /// Availability: >= 2.5
100             uint ste_free ;
101             /** true if a child block has free vars,
102             including free refs to globals */
103             uint ste_child_free ;
104             /** true if namespace is a generator */
105             uint ste_generator ;
106             /** true if block has varargs */
107             /// Availability: >= 2.5
108             uint ste_varargs ;
109             /** true if block has varkeywords */
110             /// Availability: >= 2.5
111             uint ste_varkeywords ;
112             /** true if namespace uses return with
113             an argument */
114             /// Availability: >= 2.5
115             uint ste_returns_value ;
116             /** first line of block */
117             int ste_lineno;
118 
119         }else{
120             /** module, class, or function */
121             int ste_type;
122             /** first line of scope */
123             int ste_lineno;
124             /** true if namespace can't be optimized */
125             /// Availability: 2.4
126             int ste_optimized;
127             /** true if scope is nested */
128             int ste_nested;
129             /** true if a child scope has free variables,
130                including free refs to globals */
131             int ste_child_free;
132             /** true if namespace is a generator */
133             int ste_generator;
134         }
135         /** lineno of last exec or import * */
136 	int ste_opt_lineno;
137         /** temporary name counter */
138 	int ste_tmpname;
139         /// _
140 	symtable* ste_table;
141 }
142 
143 version(Python_2_5_Or_Later) {
144     /// Availability: >= 2.5
145     mixin(PyAPI_DATA!"PyTypeObject PySTEntry_Type");
146 
147     /// _
148     int PySymtableEntry_Check()(PyObject* op) {
149         return (Py_TYPE(op) is &PySTEntry_Type);
150     }
151     /// Availability: >= 2.5
152     int PyST_GetScope(PySTEntryObject*, PyObject*);
153     /// Availability: >= 2.5
154     symtable* PySymtable_Build(
155             mod_ty, const(char)*,
156             PyFutureFeatures*);
157 
158     /// Availability: >= 2.5
159     PySTEntryObject* PySymtable_Lookup(symtable*, void*);
160     /// _
161     void PySymtable_Free(symtable*);
162 }else{
163     /// Availability: 2.4
164     alias PySTEntryObject PySymtableEntryObject;
165     /// Availability: 2.4
166     mixin(PyAPI_DATA!"PyTypeObject PySymtableEntry_Type");
167 
168     /// _
169     int PySymtableEntry_Check()(PyObject* op) {
170         return (Py_TYPE(op) is &PySymtableEntry_Type);
171     }
172 
173     /// Availability: 2.4
174     PyObject* PySymtableEntry_New(
175             symtable*,
176             char*,
177             int,
178             int);
179     /// Availability: 2.4
180     symtable* PyNode_CompileSymtable(node*, const(char)*);
181     /// _
182     void PySymtable_Free(symtable*);
183 }
184 
185 
186 /* Flags for def-use information */
187 
188 /** global stmt */
189 enum DEF_GLOBAL=1;
190 /** assignment in code block */
191 enum DEF_LOCAL=2;
192 /** formal parameter */
193 enum DEF_PARAM=2<<1;
194 /** name is used */
195 enum USE=2<<2;
196 version(Python_2_5_Or_Later) {
197     /** name used but not defined in nested block */
198     enum DEF_FREE=2<<3;
199     /** free variable from class's method */
200     enum DEF_FREE_CLASS=2<<4;
201     /** assignment occurred via import */
202     enum DEF_IMPORT=2<<5;
203 }else{
204     /** parameter is star arg */
205     enum DEF_STAR=2<<3;
206     /** parameter is star-star arg */
207     enum DEF_DOUBLESTAR=2<<4;
208     /** name defined in tuple in parameters */
209     enum DEF_INTUPLE=2<<5 ;
210     /** name used but not defined in nested scope */
211     enum DEF_FREE=2<<6;
212     /** free variable is actually implicit global */
213     enum DEF_FREE_GLOBAL=2<<7;
214     /** free variable from class's method */
215     enum DEF_FREE_CLASS=2<<8;
216     /** assignment occurred via import */
217     enum DEF_IMPORT=2<<9;
218 }
219 
220 /// _
221 enum DEF_BOUND = (DEF_LOCAL | DEF_PARAM | DEF_IMPORT);
222 
223 /// _
224 enum TYPE_FUNCTION =1;
225 /// _
226 enum TYPE_CLASS=2;
227 /// _
228 enum TYPE_MODULE=3;
229 
230 /// _
231 enum LOCAL=1;
232 /// _
233 enum GLOBAL_EXPLICIT=2;
234 /// _
235 enum GLOBAL_IMPLICIT=3;
236 /// _
237 enum FREE=4;
238 /// _
239 enum CELL=5;
240 
241 /// _
242 enum OPT_IMPORT_STAR=1;
243 /// _
244 enum OPT_EXEC=2;
245 /// _
246 enum OPT_BARE_EXEC=4;
247 
248 /// _
249 enum GENERATOR=1;
250 /// _
251 enum GENERATOR_EXPRESSION=2;