-
Notifications
You must be signed in to change notification settings - Fork 914
/
StaticScope.java
738 lines (602 loc) · 24.8 KB
/
StaticScope.java
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/*
***** BEGIN LICENSE BLOCK *****
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v20.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2006-2007 Thomas E Enebo <enebo@acm.org>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.parser;
import java.io.Serializable;
import java.lang.invoke.MethodHandle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.function.BiConsumer;
import java.util.function.IntFunction;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyBasicObject;
import org.jruby.RubyModule;
import org.jruby.RubySymbol;
import org.jruby.ast.AssignableNode;
import org.jruby.ast.DAsgnNode;
import org.jruby.ast.DVarNode;
import org.jruby.ast.IScopedNode;
import org.jruby.ast.LocalAsgnNode;
import org.jruby.ast.LocalVarNode;
import org.jruby.ast.Node;
import org.jruby.ast.VCallNode;
import org.jruby.ir.IRMethod;
import org.jruby.ir.IRScope;
import org.jruby.ir.IRScopeType;
import org.jruby.runtime.Block;
import org.jruby.runtime.DynamicScope;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.Signature;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.scope.DynamicScopeGenerator;
import org.jruby.runtime.scope.ManyVarsDynamicScope;
/**
* StaticScope represents lexical scoping of variables and module/class constants.
*
* At a very high level every scopes enclosing scope contains variables in the next outer
* lexical layer. The enclosing scopes variables may or may not be reachable depending
* on the scoping rules for variables (governed by BlockStaticScope and LocalStaticScope).
*
* StaticScope also keeps track of current module/class that is in scope. previousCRefScope
* will point to the previous scope of the enclosing module/class (cref).
*
*/
public class StaticScope implements Serializable, Cloneable {
public static final int MAX_SPECIALIZED_SIZE = 50;
private static final long serialVersionUID = 3423852552352498148L;
// Next immediate scope. Variable and constant scoping rules make use of this variable
// in different ways.
protected StaticScope enclosingScope;
// Live reference to module
private transient RubyModule cref = null;
// Next CRef down the lexical structure
private StaticScope previousCRefScope = null;
// Our name holder (offsets are assigned as variables are added) [these are symbol strings. Use
// as key to Symbol table for actual encoded versions].
private String[] variableNames;
private int variableNamesLength;
// Arity of this scope if there is one
private Signature signature;
// File name where this static scope came from or null if a native or artificial scope
private String file;
private DynamicScope dummyScope;
protected IRScopeType scopeType;
private static final String[] NO_NAMES = new String[0];
private Type type;
private boolean isBlockOrEval;
private boolean isArgumentScope; // Is this block and argument scope of a define_method.
private int firstKeywordIndex;
// Method/Closure that this static scope corresponds to. This is used to tell whether this
// scope refers to a method scope or to determined IRScope of the parent of a compiling eval.
private IRScope irScope;
private RubyModule overlayModule;
private volatile MethodHandle constructor;
private volatile Collection<String> ivarNames;
public enum Type {
LOCAL, BLOCK, EVAL;
public static Type fromOrdinal(int value) {
return value < 0 || value >= values().length ? null : values()[value];
}
}
/**
*
*/
protected StaticScope(Type type, StaticScope enclosingScope, String file) {
this(type, enclosingScope, file, NO_NAMES, -1);
}
/**
* Construct a new static scope.
*
* @param type the type of scope
* @param enclosingScope the lexically containing scope.
*/
protected StaticScope(Type type, StaticScope enclosingScope) {
this(type, enclosingScope, null, NO_NAMES, -1);
}
/**
* Construct a new static scope. The array of strings should all be the
* interned versions, since several other optimizations depend on being
* able to do object equality checks.
*
* @param type the type of scope
* @param enclosingScope the lexically containing scope.
* @param names The list of interned String variable names.
*/
protected StaticScope(Type type, StaticScope enclosingScope, String[] names, int firstKeywordIndex) {
this(type, enclosingScope, null ,names, firstKeywordIndex);
}
protected StaticScope(Type type, StaticScope enclosingScope, String file, String[] names, int firstKeywordIndex) {
assert names != null : "names is not null";
this.enclosingScope = enclosingScope;
this.variableNames = names;
this.variableNamesLength = names.length;
this.type = type;
if (enclosingScope != null) this.scopeType = enclosingScope.getScopeType();
this.isBlockOrEval = (type != Type.LOCAL);
this.isArgumentScope = !isBlockOrEval;
this.firstKeywordIndex = firstKeywordIndex;
this.file = file;
}
protected StaticScope(Type type, StaticScope enclosingScope, String[] names) {
this(type, enclosingScope, null, names, -1);
}
public int getFirstKeywordIndex() {
return firstKeywordIndex;
}
public DynamicScope construct(DynamicScope parent) {
MethodHandle constructor = this.constructor;
if (constructor == null) constructor = acquireConstructor();
try {
return (DynamicScope) constructor.invokeExact(this, parent);
} catch (Throwable e) {
Helpers.throwException(e);
return null; // not reached
}
}
private synchronized MethodHandle acquireConstructor() {
// check again
MethodHandle constructor = this.constructor;
if (constructor != null) return constructor;
int numberOfVariables = getNumberOfVariables();
if (numberOfVariables > MAX_SPECIALIZED_SIZE) {
constructor = ManyVarsDynamicScope.CONSTRUCTOR;
} else {
constructor = DynamicScopeGenerator.generate(numberOfVariables);
}
this.constructor = constructor;
return constructor;
}
public IRScope getIRScope() {
return irScope;
}
public IRScopeType getScopeType() {
return scopeType;
}
public void setScopeType(IRScopeType scopeType) {
this.scopeType = scopeType;
}
public void setIRScope(IRScope irScope) {
this.irScope = irScope;
this.scopeType = irScope.getScopeType();
}
/**
* Add a new variable to this (current) scope unless it is already defined in the
* current scope.
*
* @param name of new variable
* @return index of variable
*/
public int addVariableThisScope(String name) {
int slot = exists(name);
if (slot >= 0) return slot;
// Clear constructor since we are adding a name
constructor = null;
// This is perhaps innefficient timewise? Optimal spacewise
growVariableNames(name);
// Returns slot of variable
return variableNames.length - 1;
}
/**
* Add a new variable to this (current) scope unless it is already defined in any
* reachable scope.
*
* @param name of new variable
* @return index+depth merged location of scope
*/
public int addVariable(String name) {
int slot = isDefined(name);
if (slot >= 0) return slot;
// Clear constructor since we are adding a name
constructor = null;
// This is perhaps innefficient timewise? Optimal spacewise
growVariableNames(name);
// Returns slot of variable
return variableNames.length - 1;
}
public String[] getVariables() {
return variableNames;
}
public int getNumberOfVariables() {
return variableNamesLength;
}
public void setVariables(String[] names) {
assert names != null : "names is not null";
// Clear constructor since we are changing names
constructor = null;
variableNames = new String[names.length];
variableNamesLength = names.length;
System.arraycopy(names, 0, variableNames, 0, names.length);
}
/**
* Gets a constant back from lexical search from the cref in this scope.
* As it is for defined? we will not forced resolution of autoloads nor
* call const_defined
*/
public IRubyObject getConstantDefined(String internedName) {
IRubyObject result = cref.fetchConstant(internedName);
if (result != null) return result;
return previousCRefScope == null ? null : previousCRefScope.getConstantDefinedNoObject(internedName);
}
public IRubyObject getConstantDefinedNoObject(String internedName) {
if (previousCRefScope == null) return null;
return getConstantDefined(internedName);
}
public IRubyObject getConstant(String internedName) {
IRubyObject result = getConstantInner(internedName);
// If we could not find the constant from cref..then try getting from inheritance hierarchy
return result == null ? cref.getConstantNoConstMissing(internedName) : result;
}
public IRubyObject getConstantInner(String internedName) {
IRubyObject result = cref.getConstantWithAutoload(internedName, RubyBasicObject.UNDEF, true);
// If we had a failed autoload, give up hierarchy search
if (result == RubyBasicObject.UNDEF) return null;
if (result != null) return result;
return previousCRefScope == null ? null : previousCRefScope.getConstantInnerNoObject(internedName);
}
private IRubyObject getConstantInnerNoObject(String internedName) {
if (previousCRefScope == null) return null;
return getConstantInner(internedName);
}
/**
* Next outer most scope in list of scopes. An enclosing scope may have no direct scoping
* relationship to its child. If I am in a localScope and then I enter something which
* creates another localScope the enclosing scope will be the first scope, but there are
* no valid scoping relationships between the two. Methods which walk the enclosing scopes
* are responsible for enforcing appropriate scoping relationships.
*
* @return the parent scope
*/
public StaticScope getEnclosingScope() {
return enclosingScope;
}
public void setEnclosingScope(StaticScope parent) {
this.enclosingScope = parent;
}
/**
* Does the variable exist?
*
* @param name of the variable to find
* @return index of variable or -1 if it does not exist
*/
public int exists(String name) {
return findVariableName(name);
}
private int findVariableName(String name) {
for (int i = 0; i < variableNames.length; i++) {
if (name.equals(variableNames[i])) return i;
}
return -1;
}
/**
* Is this name in the visible to the current scope
*
* @param name to be looked for
* @return a location where the left-most 16 bits of number of scopes down it is and the
* right-most 16 bits represents its index in that scope
*/
public int isDefined(String name) {
return isDefined(name, 0);
}
/**
* Make a DASgn or LocalAsgn node based on scope logic
*
* Note: This is private code made public only for parser.
*/
public AssignableNode assign(int line, RubySymbol name, Node value) {
return assign(line, name, value, this, 0);
}
/**
* Register a keyword argument with this staticScope. It additionally will track
* where the first keyword argument started so we can test and tell whether we have
* a kwarg or an ordinary variable during live execution (See keywordExists).
*/
public AssignableNode assignKeyword(int line, RubySymbol symbolID, Node value) {
AssignableNode assignment = assign(line, symbolID, value, this, 0);
// register first keyword index encountered
if (firstKeywordIndex == -1) firstKeywordIndex = ((IScopedNode) assignment).getIndex();
return assignment;
}
public boolean keywordExists(String name) {
if (name.equals("_")) return true;
int slot = exists(name);
return slot >= 0 && firstKeywordIndex != -1 && slot >= firstKeywordIndex;
}
/**
* Get all visible variables that we can see from this scope that have been assigned
* (e.g. seen so far)
*
* @return a list of all names (sans $~ and $_ which are special names)
*/
public String[] getAllNamesInScope() {
return collectVariables(ArrayList::new, ArrayList::add).stream().toArray(String[]::new);
}
/**
* Populate a deduplicated collection of variable names in scope using the given functions.
*
* This may include variables that are not strictly Ruby local variable names, so the consumer should validate
* names as appropriate.
*
* @param collectionFactory used to construct the collection
* @param collectionPopulator used to pass values into the collection
* @param <T> resulting collection type
* @return populated collection
*/
public <T> T collectVariables(IntFunction<T> collectionFactory, BiConsumer<T, String> collectionPopulator) {
StaticScope current = this;
T collection = collectionFactory.apply(current.variableNamesLength);
HashMap<String, Object> dedup = new HashMap<>();
while (current.isBlockOrEval) {
for (String name : current.variableNames) {
dedup.computeIfAbsent(name, key -> {collectionPopulator.accept(collection, key); return key;});
}
current = current.enclosingScope;
}
// once more for method scope
for (String name : current.variableNames) {
dedup.computeIfAbsent(name, key -> {collectionPopulator.accept(collection, key); return key;});
}
return collection;
}
/**
* Convenience wrapper around {@link #collectVariables(IntFunction, BiConsumer)}.
*
* @param runtime current runtime
* @return populated RubyArray
*/
public RubyArray getLocalVariables(Ruby runtime) {
return collectVariables(
runtime::newArray,
(array, id) -> {
RubySymbol symbol = runtime.newSymbol(id);
if (symbol.validLocalVariableName()) array.append(symbol);
});
}
public int isDefined(String name, int depth) {
if (isBlockOrEval) {
int slot = exists(name);
if (slot >= 0) return (depth << 16) | slot;
return enclosingScope.isDefined(name, depth + 1);
} else {
return (depth << 16) | exists(name);
}
}
public AssignableNode addAssign(int line, RubySymbol symbolID, Node value) {
int slot = addVariable(symbolID.idString());
// No bit math to store level since we know level is zero for this case
return new DAsgnNode(line, symbolID, slot, value);
}
public AssignableNode assign(int line, RubySymbol symbolID, Node value, StaticScope topScope, int depth) {
String id = symbolID.idString();
int slot = exists(id);
// We can assign if we already have variable of that name here or we are the only
// scope in the chain (which Local scopes always are).
if (slot >= 0) {
return isBlockOrEval ? new DAsgnNode(line, symbolID, ((depth << 16) | slot), value)
: new LocalAsgnNode(line, symbolID, ((depth << 16) | slot), value);
} else if (!isBlockOrEval && (topScope == this)) {
slot = addVariable(id);
return new LocalAsgnNode(line, symbolID, slot, value);
}
// If we are not a block-scope and we go there, we know that 'topScope' is a block scope
// because a local scope cannot be within a local scope
// If topScope was itself it would have created a LocalAsgnNode above.
return isBlockOrEval ?
enclosingScope.assign(line, symbolID, value, topScope, depth + 1) :
topScope.addAssign(line, symbolID, value);
}
// Note: This is private code made public only for parser.
public Node declare(int line, RubySymbol symbolID, int depth) {
int slot = exists(symbolID.idString());
if (slot >= 0) {
return isBlockOrEval ?
new DVarNode(line, ((depth << 16) | slot), symbolID) :
new LocalVarNode(line, ((depth << 16) | slot), symbolID);
}
return isBlockOrEval ? enclosingScope.declare(line, symbolID, depth + 1) : new VCallNode(line, symbolID);
}
/**
* Make a DVar or LocalVar node based on scoping logic
*
* @param line the location that in the source that the new node will come from
* @param symbolID of the variable to be created is named
* @return a DVarNode or LocalVarNode
*
* Note: This is private code made public only for parser.
*/
public Node declare(int line, RubySymbol symbolID) {
return declare(line, symbolID, 0);
}
/**
* Gets the Local Scope relative to the current Scope. For LocalScopes this will be itself.
* Blocks will contain the LocalScope it contains.
*
* @return localScope
*/
public StaticScope getLocalScope() {
return (type != Type.BLOCK) ? this : enclosingScope.getLocalScope();
}
/**
* Get the live CRef module associated with this scope.
*
* @return the live module
*/
public RubyModule getModule() {
return cref;
}
public StaticScope getPreviousCRefScope() {
return previousCRefScope;
}
public void setPreviousCRefScope(StaticScope crefScope) {
this.previousCRefScope = crefScope;
}
public void setModule(RubyModule module) {
this.cref = module;
for (StaticScope scope = getEnclosingScope(); scope != null; scope = scope.getEnclosingScope()) {
if (scope.cref != null) {
previousCRefScope = scope;
return;
}
}
}
/**
* Update current scoping structure to populate with proper cref scoping values. This should
* be called at any point when you reference a scope for the first time. For the interpreter
* this is done in a small number of places (defnNode, defsNode, and getBlock). The compiler
* does this in the same places.
*
* @return the current cref, though this is largely an implementation detail
*/
public RubyModule determineModule() {
if (cref == null) {
cref = getEnclosingScope().determineModule();
assert cref != null : "CRef is always created before determine happens";
previousCRefScope = getEnclosingScope().previousCRefScope;
}
return cref;
}
public boolean isBlockScope() {
return isBlockOrEval;
}
/**
* Argument scopes represent scopes which contain arguments for zsuper. All LocalStaticScopes
* are argument scopes and BlockStaticScopes can be when they are used by define_method.
*/
public boolean isArgumentScope() {
return isArgumentScope;
}
public void makeArgumentScope() {
this.isArgumentScope = true;
}
/**
* For all block or method associated with static scopes this will return the signature for that
* signature-providing scope. module bodies and other non-arity specific code will return null.
*/
public Signature getSignature() {
return signature;
}
/**
* This happens in when first defining ArgsNodes or when reifying a method from AOT.
*/
public void setSignature(Signature signature) {
this.signature = signature;
}
public DynamicScope getDummyScope() {
return dummyScope == null ? dummyScope = DynamicScope.newDynamicScope(this) : dummyScope;
}
private void growVariableNames(String name) {
String[] newVariableNames = new String[variableNames.length + 1];
System.arraycopy(variableNames, 0, newVariableNames, 0, variableNames.length);
variableNames = newVariableNames;
variableNamesLength = newVariableNames.length;
variableNames[variableNames.length - 1] = name;
}
/**
* Determine if we happen to be within a method definition.
* @return true if so
*/
public boolean isWithinMethod() {
for (StaticScope current = this; current != null; current = current.getEnclosingScope()) {
if (current.getScopeType().isMethod()) return true;
}
return false;
}
@Override
public String toString() {
// FIXME: Do we need to persist cref as well?
return "StaticScope(" + type + "):" + Arrays.toString(variableNames);
}
public Type getType() {
return type;
}
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
public StaticScope duplicate() {
StaticScope dupe = new StaticScope(type, enclosingScope, variableNames == null ? NO_NAMES : variableNames);
// irScope is not guaranteed to be set onto StaticScope until it is executed for the first time.
// We can call duplicate before its first execution.
if (irScope != null) dupe.setIRScope(irScope);
dupe.setScopeType(scopeType);
dupe.setPreviousCRefScope(previousCRefScope);
dupe.setModule(cref);
dupe.setFile(file);
dupe.setSignature(signature);
dupe.firstKeywordIndex = firstKeywordIndex;
return dupe;
}
public RubyModule getOverlayModuleForRead() {
return overlayModule;
}
public RubyModule getOverlayModuleForWrite(ThreadContext context) {
RubyModule omod = overlayModule;
if (omod == null) {
overlayModule = omod = RubyModule.newModule(context.runtime);
}
return omod;
}
/**
* Duplicate the parent scope's refinements overlay to get a moment-in-time snapshot. Caller must
* decide whether this scope is using (or maybe) using refinements.
*
* @param context
*/
public void captureParentRefinements(ThreadContext context) {
for (StaticScope cur = this.getEnclosingScope(); cur != null; cur = cur.getEnclosingScope()) {
RubyModule overlay = cur.getOverlayModuleForRead();
if (overlay != null && !overlay.getRefinements().isEmpty()) {
// capture current refinements at definition time
RubyModule myOverlay = getOverlayModuleForWrite(context);
// FIXME: MRI does a copy-on-write thing here with the overlay
myOverlay.getRefinementsForWrite().putAll(overlay.getRefinements());
// only search until we find an overlay
break;
}
}
}
public Collection<String> getInstanceVariableNames() {
if (ivarNames != null) return ivarNames;
if (irScope instanceof IRMethod) {
return ivarNames = ((IRMethod) irScope).getMethodData().getIvarNames();
}
return ivarNames = Collections.EMPTY_LIST;
}
public void setInstanceVariableNames(Collection<String> ivarWrites) {
this.ivarNames = ivarWrites;
}
public boolean isRuby2Keywords() {
return irScope.isRuby2Keywords();
}
}