-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathScopeInfo.java
More file actions
321 lines (280 loc) · 10.3 KB
/
Copy pathScopeInfo.java
File metadata and controls
321 lines (280 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// (C) Copyright 2001 Samuele Pedroni
package org.python.compiler;
import org.python.antlr.PythonTree;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class ScopeInfo extends Object implements ScopeConstants {
public PythonTree scope_node;
public String scope_name;
public int level;
public int func_level;
public boolean needs_class_closure;
public boolean async;
public String qualname;
public void dump() { // for debugging
if (org.python.core.Options.verbose < org.python.core.Py.DEBUG)
return;
for(int i=0; i<level; i++) System.err.print(' ');
System.err.print(((kind != CLASSSCOPE)?scope_name:"class "+
scope_name)+": ");
for (Map.Entry<String, SymInfo> entry : tbl.entrySet()) {
String name = entry.getKey();
SymInfo info = entry.getValue();
int flags = info.flags;
System.err.print(name);
if ((flags&BOUND) != 0) System.err.print('=');
// func scope global (affect nested scopes)
// vs. class scope global
if ((flags&NGLOBAL) != 0) System.err.print('G');
else if ((flags&CLASS_GLOBAL) != 0) System.err.print('g');
if ((flags&PARAM) != 0) System.err.print('P');
else if ((flags&FROM_PARAM) != 0) System.err.print('p');
if ((flags&CELL) != 0) System.err.print('!');
if ((flags&FREE) != 0) System.err.print(",f");
System.err.print(" ");
}
System.err.println();
}
public ScopeInfo(String name, PythonTree node, int level, int kind,
int func_level, ArgListCompiler ac) {
scope_name = name;
scope_node = node;
this.level = level;
this.kind = kind;
this.func_level = func_level;
this.ac = ac;
}
public int kind;
public boolean unqual_exec;
public boolean exec;
public boolean from_import_star;
public boolean contains_ns_free_vars;
public boolean generator;
public boolean comprehension;
private boolean hasReturnWithValue;
public int yield_count;
public int max_with_count;
public ArgListCompiler ac;
public Map<String, SymInfo> tbl = new LinkedHashMap<String, SymInfo>();
public List<String> varNames = new ArrayList<>();
public int addNonlocal(String name) {
SymInfo info = tbl.get(name);
if (info == null) {
tbl.put(name,new SymInfo(FREE));
return -1;
}
int prev = info.flags;
info.flags |= FREE;
return prev;
}
public int addGlobal(String name) {
// global kind = func vs. class
int global = kind==CLASSSCOPE?CLASS_GLOBAL:NGLOBAL;
SymInfo info = tbl.get(name);
if (info == null) {
tbl.put(name,new SymInfo(global|BOUND));
return -1;
}
int prev = info.flags;
info.flags |= global|BOUND;
return prev;
}
public int local = 0;
public void addParam(String name) {
//System.out.println("addParam " + name);
tbl.put(name, new SymInfo(PARAM|BOUND,local++));
varNames.add(name);
}
public void markFromParam() {
for (SymInfo info : tbl.values()) {
info.flags |= FROM_PARAM;
}
}
public void addBound(String name) {
SymInfo info = tbl.get(name);
if (info == null) {
tbl.put(name, new SymInfo(BOUND));
return;
}
// don't bound nonlocal variables
if ((info.flags & FREE) == 0) {
info.flags |= BOUND;
}
}
public void addUsed(String name) {
if (tbl.get(name) == null) {
tbl.put(name, new SymInfo(0));
}
}
public void addConst(PythonTree node) {
constants.add(node);
}
private final static Object PRESENT = new Object();
public Hashtable<String,Object> inner_free = new Hashtable<String,Object>();
public List<String> globalNames = new ArrayList<>(); // co_names
public List<PythonTree> constants = new ArrayList<>(); // co_consts
public List<String> freevars = new ArrayList<>(); // co_freevars
public List<String> cellvars = new ArrayList<>(); // co_cellvars
public List<String> jy_paramcells = new ArrayList<>();
public int jy_npurecell;
public int cell, distance;
public ScopeInfo up;
//Resolve the names used in the given scope, and mark any freevars used in the up scope
public void cook(ScopeInfo up, int distance, CompilationContext ctxt) throws Exception {
if(up == null)
return; // top level => nop
this.up = up;
this.distance = distance;
boolean func = kind == FUNCSCOPE;
List<String> purecells = new ArrayList<>();
cell = 0;
boolean some_inner_free = inner_free.size() > 0;
for (Enumeration e = inner_free.keys(); e.hasMoreElements(); ) {
String name = (String)e.nextElement();
SymInfo info = tbl.get(name);
if (info == null) {
tbl.put(name,new SymInfo(FREE));
continue;
}
int flags = info.flags;
if (func) {
// not func global and bound ?
if ((flags&NGLOBAL) == 0 && (flags&BOUND) != 0) {
info.flags |= CELL;
if ((info.flags&PARAM) != 0)
jy_paramcells.add(name);
cellvars.add(name);
info.env_index = cell++;
if ((flags&PARAM) == 0) purecells.add(name);
continue;
}
} else {
info.flags |= FREE;
}
}
boolean some_free = false;
boolean nested = up.kind != TOPSCOPE;
for (Map.Entry<String, SymInfo> entry : tbl.entrySet()) {
String name = entry.getKey();
SymInfo info = entry.getValue();
int flags = info.flags;
if (nested && (flags&FREE) != 0) up.inner_free.put(name,PRESENT);
if ((flags&(GLOBAL|PARAM|CELL)) == 0) {
if ((flags&BOUND) != 0) { // ?? only func
// System.err.println("local: "+name);
varNames.add(name);
info.locals_index = local++;
continue;
}
info.flags |= FREE;
some_free = true;
if (nested) up.inner_free.put(name,PRESENT);
}
}
if ((jy_npurecell = purecells.size()) > 0) {
int sz = purecells.size();
for (int i = 0; i < sz; i++) {
varNames.add(purecells.get(i));
}
}
if (some_free && nested) {
up.contains_ns_free_vars = true;
}
// XXX - this doesn't catch all cases - may depend subtly
// on how visiting NOW works with antlr compared to javacc
if ((unqual_exec || from_import_star)) {
if(some_inner_free) dynastuff_trouble(true, ctxt);
else if(func_level > 1 && some_free)
dynastuff_trouble(false, ctxt);
}
}
private void dynastuff_trouble(boolean inner_free, CompilationContext ctxt) throws Exception {
StringBuilder illegal = new StringBuilder();
if (unqual_exec && from_import_star) {
illegal.append("function '")
.append(scope_name)
.append("' uses import * and bare exec, which are illegal");
} else if (unqual_exec) {
illegal.append("unqualified exec is not allowed in function '")
.append(scope_name)
.append("'");
} else {
illegal.append("import * is not allowed in function '").append(scope_name).append("'");
}
if (inner_free) {
illegal.append(" because it contains a function with free variables");
} else {
illegal.append(" because it contains free variables");
}
ctxt.error(illegal.toString(), true, scope_node);
}
/**
* setup the closure on this scope using the scope passed into cook as up as
* the containing scope
*/
public void setup_closure() {
setup_closure(up);
}
/**
* setup the closure on this scope using the passed in scope. This is used
* by jythonc to setup its closures.
*/
public void setup_closure(ScopeInfo up){
int free = cell; // env = cell...,free...
Map<String, SymInfo> up_tbl = up.tbl;
boolean nested = up.kind != TOPSCOPE;
for (Map.Entry<String, SymInfo> entry : tbl.entrySet()) {
String name = entry.getKey();
SymInfo info = entry.getValue();
int flags = info.flags;
if ((flags&FREE) != 0) {
SymInfo up_info = up_tbl.get(name);
// ?? differs from CPython -- what is the intended behaviour?
if (up_info != null) {
int up_flags = up_info.flags;
if ((up_flags&(CELL|FREE)) != 0) {
info.env_index = free++;
freevars.add(name);
continue;
}
// ! func global affect nested scopes
if (nested && (up_flags&NGLOBAL) != 0) {
info.flags = NGLOBAL|BOUND;
continue;
}
}
globalNames.add(name);
info.flags &= ~FREE;
}
}
}
// check if specified name is defined as global
public boolean isGlobal(String name) {
SymInfo info = tbl.get(name);
return info != null && (info.flags & NGLOBAL) != 0;
}
@Override
public String toString() {
return "ScopeInfo[" + scope_name + " " + kind + "]@" +
System.identityHashCode(this);
}
public void defineAsGenerator() {
generator = true;
}
public void defineAsComprehension() {
comprehension = true;
}
public void noteReturnValue() {
hasReturnWithValue = true;
}
public boolean isFunction() {
return kind == FUNCSCOPE;
}
public boolean isNested() {
return isFunction() && up.isFunction();
}
}