-
Notifications
You must be signed in to change notification settings - Fork 178
/
ModuleOperations.java
670 lines (571 loc) · 26.8 KB
/
ModuleOperations.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
/*
* Copyright (c) 2014, 2024 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 2.0, or
* GNU General Public License version 2, or
* GNU Lesser General Public License version 2.1.
*/
package org.truffleruby.core.module;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;
import com.oracle.truffle.api.object.DynamicObjectLibrary;
import org.truffleruby.RubyContext;
import org.truffleruby.RubyLanguage;
import org.truffleruby.collections.Memo;
import org.truffleruby.core.array.ArrayUtils;
import org.truffleruby.core.klass.RubyClass;
import org.truffleruby.core.string.StringUtils;
import org.truffleruby.language.LexicalScope;
import org.truffleruby.language.RubyConstant;
import org.truffleruby.language.constants.ConstantEntry;
import org.truffleruby.language.control.RaiseException;
import org.truffleruby.language.methods.DeclarationContext;
import org.truffleruby.language.methods.InternalMethod;
import org.truffleruby.language.objects.classvariables.ClassVariableStorage;
import org.truffleruby.language.objects.shared.SharedObjects;
import org.truffleruby.parser.Identifiers;
import com.oracle.truffle.api.Assumption;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.nodes.Node;
public abstract class ModuleOperations {
public static final Assumption[] EMPTY_ASSUMPTION_ARRAY = new Assumption[0];
@TruffleBoundary
public static boolean includesModule(RubyModule module, RubyModule other) {
for (RubyModule ancestor : module.fields.ancestors()) {
if (ancestor == other) {
return true;
}
}
return false;
}
public static boolean assignableTo(RubyModule thisClass, RubyModule otherClass) {
return includesModule(thisClass, otherClass);
}
public static boolean inAncestorsOf(RubyModule module, RubyModule ancestors) {
return includesModule(ancestors, module);
}
@TruffleBoundary
public static boolean canBindMethodTo(InternalMethod method, RubyModule module) {
final RubyModule origin = method.getDeclaringModule();
if (!(origin instanceof RubyClass)) { // Module (not Class) methods can always be bound
return true;
} else if (module.fields.isRefinement()) {
RubyModule refinedModule = module.fields.getRefinedModule();
return refinedModule instanceof RubyClass && ModuleOperations.assignableTo(refinedModule, origin);
} else {
return module instanceof RubyClass && ModuleOperations.assignableTo(module, origin);
}
}
@TruffleBoundary
public static String constantName(RubyContext context, RubyModule module, String name) {
if (module == context.getCoreLibrary().objectClass) {
return "::" + name;
} else {
return module.fields.getName() + "::" + name;
}
}
public static String constantNameNoLeadingColon(RubyContext context, RubyModule module, String name) {
if (module == context.getCoreLibrary().objectClass) {
return name;
} else {
return module.fields.getName() + "::" + name;
}
}
@TruffleBoundary
public static Iterable<Entry<String, ConstantEntry>> getAllConstants(RubyModule module) {
final Map<String, ConstantEntry> constants = new HashMap<>();
// Look in the current module
for (Map.Entry<String, ConstantEntry> constant : module.fields.getConstants()) {
if (constant.getValue().getConstant() != null) {
constants.put(constant.getKey(), constant.getValue());
}
}
// Look in ancestors
for (RubyModule ancestor : module.fields.prependedAndIncludedModules()) {
for (Map.Entry<String, ConstantEntry> constant : ancestor.fields.getConstants()) {
if (constant.getValue().getConstant() != null) {
constants.putIfAbsent(constant.getKey(), constant.getValue());
}
}
}
return constants.entrySet();
}
/** NOTE: This method returns false for an undefined RubyConstant */
public static boolean isConstantDefined(RubyConstant constant) {
return constant != null && !constant.isUndefined() &&
!(constant.isAutoload() && constant.getAutoloadConstant().isAutoloadingThread());
}
/** NOTE: This method might return an undefined RubyConstant */
private static boolean constantExists(RubyConstant constant, ArrayList<Assumption> assumptions) {
if (constant != null) {
if (constant.isAutoload() && constant.getAutoloadConstant().isAutoloading()) {
if (assumptions != null) {
// Cannot cache the lookup of an autoloading constant as the result depends on the calling thread
assumptions.add(Assumption.NEVER_VALID);
}
return !constant.getAutoloadConstant().isAutoloadingThread();
} else {
return true;
}
} else {
return false;
}
}
private static boolean constantExists(ConstantEntry constantEntry, ArrayList<Assumption> assumptions) {
return constantExists(constantEntry.getConstant(), assumptions);
}
@TruffleBoundary
public static ConstantLookupResult lookupConstant(RubyContext context, RubyModule module, String name) {
return lookupConstant(context, module, name, new ArrayList<>());
}
@TruffleBoundary
private static ConstantLookupResult lookupConstant(RubyContext context, RubyModule module, String name,
ArrayList<Assumption> assumptions) {
// Look in the current module
ConstantEntry constantEntry = module.fields.getOrComputeConstantEntry(name);
assumptions.add(constantEntry.getAssumption());
if (constantExists(constantEntry, assumptions)) {
return new ConstantLookupResult(constantEntry.getConstant(), toArray(assumptions));
}
// Look in ancestors
for (RubyModule ancestor : module.fields.ancestors()) {
if (ancestor == module) {
continue;
}
constantEntry = ancestor.fields.getOrComputeConstantEntry(name);
assumptions.add(constantEntry.getAssumption());
if (constantExists(constantEntry, assumptions)) {
return new ConstantLookupResult(constantEntry.getConstant(), toArray(assumptions));
}
}
// Nothing found
return new ConstantLookupResult(null, toArray(assumptions));
}
@TruffleBoundary
public static ConstantLookupResult lookupConstantInObject(RubyContext context, String name,
ArrayList<Assumption> assumptions) {
final RubyClass objectClass = context.getCoreLibrary().objectClass;
ConstantEntry constantEntry = objectClass.fields.getOrComputeConstantEntry(name);
assumptions.add(constantEntry.getAssumption());
if (constantExists(constantEntry, assumptions)) {
return new ConstantLookupResult(constantEntry.getConstant(), toArray(assumptions));
}
for (RubyModule ancestor : objectClass.fields.prependedAndIncludedModules()) {
constantEntry = ancestor.fields.getOrComputeConstantEntry(name);
assumptions.add(constantEntry.getAssumption());
if (constantExists(constantEntry, assumptions)) {
return new ConstantLookupResult(constantEntry.getConstant(), toArray(assumptions));
}
}
return new ConstantLookupResult(null, toArray(assumptions));
}
@TruffleBoundary
public static RubyConstant lookupConstantInObjectUncached(RubyContext context, String name) {
final RubyClass objectClass = context.getCoreLibrary().objectClass;
RubyConstant constant = objectClass.fields.getConstant(name);
if (constantExists(constant, null)) {
return constant;
}
for (RubyModule ancestor : objectClass.fields.prependedAndIncludedModules()) {
constant = ancestor.fields.getConstant(name);
if (constantExists(constant, null)) {
return constant;
}
}
return null;
}
@TruffleBoundary
public static ConstantLookupResult lookupConstantAndObject(RubyContext context, RubyModule module, String name,
ArrayList<Assumption> assumptions) {
final ConstantLookupResult constant = lookupConstant(context, module, name, assumptions);
if (constant.isFound()) {
return constant;
}
// Look in Object and its included modules for modules (not for classes)
if (!(module instanceof RubyClass)) {
return lookupConstantInObject(context, name, assumptions);
} else {
return constant;
}
}
@TruffleBoundary
public static ConstantLookupResult lookupConstantWithLexicalScope(RubyContext context, LexicalScope lexicalScope,
String name) {
final RubyModule module = lexicalScope.getLiveModule();
final ArrayList<Assumption> assumptions = new ArrayList<>();
// Look in lexical scope
while (lexicalScope != context.getRootLexicalScope()) {
final ConstantEntry constantEntry = lexicalScope.getLiveModule().fields.getOrComputeConstantEntry(name);
assumptions.add(constantEntry.getAssumption());
if (constantExists(constantEntry, assumptions)) {
return new ConstantLookupResult(constantEntry.getConstant(), toArray(assumptions));
}
lexicalScope = lexicalScope.getParent();
}
return lookupConstantAndObject(context, module, name, assumptions);
}
@TruffleBoundary
public static ConstantLookupResult lookupScopedConstant(RubyContext context, RubyModule module, String fullName,
boolean inherit, Node currentNode, boolean checkName) {
int start = 0, next;
if (fullName.startsWith("::")) {
module = context.getCoreLibrary().objectClass;
start += 2;
}
while ((next = fullName.indexOf("::", start)) != -1) {
final String segment = fullName.substring(start, next);
final ConstantLookupResult constant = lookupConstantWithInherit(
context,
module,
segment,
inherit,
currentNode,
checkName);
if (!constant.isFound()) {
return constant;
} else if (constant.getConstant().getValue() instanceof RubyModule) {
module = (RubyModule) constant.getConstant().getValue();
} else {
throw new RaiseException(
context,
context.getCoreExceptions().typeError(
fullName.substring(0, next) + " does not refer to class/module",
currentNode));
}
start = next + 2;
}
final String lastSegment = fullName.substring(start);
if (checkName && !Identifiers.isValidConstantName(lastSegment)) {
throw new RaiseException(
context,
context.getCoreExceptions().nameError(
StringUtils.format("wrong constant name %s", fullName),
module,
fullName,
currentNode));
}
return lookupConstantWithInherit(context, module, lastSegment, inherit, currentNode, checkName);
}
public static ConstantLookupResult lookupConstantWithInherit(RubyContext context, RubyModule module, String name,
boolean inherit, Node currentNode, boolean checkName) {
return lookupConstantWithInherit(context, module, name, inherit, currentNode, checkName, true);
}
@TruffleBoundary
public static ConstantLookupResult lookupConstantWithInherit(RubyContext context, RubyModule module, String name,
boolean inherit, Node currentNode, boolean checkName, boolean lookInObject) {
if (checkName && !Identifiers.isValidConstantName(name)) {
throw new RaiseException(
context,
context.getCoreExceptions().nameError(
StringUtils.format("wrong constant name %s", name),
module,
name,
currentNode));
}
final ArrayList<Assumption> assumptions = new ArrayList<>();
if (inherit) {
if (lookInObject) {
return ModuleOperations.lookupConstantAndObject(context, module, name, assumptions);
} else {
return ModuleOperations.lookupConstant(context, module, name, assumptions);
}
} else {
final ConstantEntry constantEntry = module.fields.getOrComputeConstantEntry(name);
assumptions.add(constantEntry.getAssumption());
if (constantExists(constantEntry, assumptions)) {
return new ConstantLookupResult(constantEntry.getConstant(), toArray(assumptions));
} else {
return new ConstantLookupResult(null, toArray(assumptions));
}
}
}
@TruffleBoundary
public static Map<String, InternalMethod> getAllMethods(RubyModule module) {
final Map<String, InternalMethod> methods = new HashMap<>();
for (RubyModule ancestor : module.fields.ancestors()) {
for (InternalMethod method : ancestor.fields.getMethods()) {
methods.putIfAbsent(method.getName(), method);
}
}
if (module.fields.isRefinement()) {
for (RubyModule ancestor : module.fields.getRefinedModule().fields
.ancestors()) {
for (InternalMethod method : ancestor.fields.getMethods()) {
methods.putIfAbsent(method.getName(), method);
}
}
}
return methods;
}
@TruffleBoundary
public static Map<String, InternalMethod> getMethodsBeforeLogicalClass(RubyModule module) {
final Map<String, InternalMethod> methods = new HashMap<>();
ModuleChain chain = module.fields.getFirstModuleChain();
while (true) {
if (chain instanceof PrependMarker) {
// We need to stop if we are entering prepended modules of a class which is not a
// singleton class. So we look for the PrependMarker explicitly here.
final RubyModule origin = ((PrependMarker) chain).getOrigin().getActualModule();
// When we find a class which is not a singleton class, we are done
if (origin instanceof RubyClass && !((RubyClass) origin).isSingleton) {
break;
} else {
chain = chain.getParentModule();
}
}
final RubyModule ancestor = chain.getActualModule();
// When we find a class which is not a singleton class, we are done
if (ancestor instanceof RubyClass && !((RubyClass) ancestor).isSingleton) {
break;
}
for (InternalMethod method : ancestor.fields.getMethods()) {
methods.putIfAbsent(method.getName(), method);
}
chain = chain.getParentModule();
}
return methods;
}
@TruffleBoundary
public static Map<String, InternalMethod> getMethodsUntilLogicalClass(RubyModule module) {
final Map<String, InternalMethod> methods = new HashMap<>();
for (RubyModule ancestor : module.fields.ancestors()) {
for (InternalMethod method : ancestor.fields.getMethods()) {
methods.putIfAbsent(method.getName(), method);
}
// When we find a class which is not a singleton class, we are done
if (ancestor instanceof RubyClass && !((RubyClass) ancestor).isSingleton) {
break;
}
}
return methods;
}
@TruffleBoundary
public static Map<String, InternalMethod> withoutUndefinedMethods(Map<String, InternalMethod> methods) {
Map<String, InternalMethod> definedMethods = new HashMap<>();
for (Entry<String, InternalMethod> method : methods.entrySet()) {
if (!method.getValue().isUndefined()) {
definedMethods.put(method.getKey(), method.getValue());
}
}
return definedMethods;
}
@TruffleBoundary
public static MethodLookupResult lookupMethodCached(RubyModule module, String name,
DeclarationContext declarationContext) {
var assumptions = new ArrayList<Assumption>();
// Look in ancestors
for (RubyModule ancestor : module.fields.ancestors()) {
var refinements = getRefinementsFor(declarationContext, ancestor);
if (refinements != null) {
for (RubyModule refinement : refinements) {
var refinedMethod = refinement.fields.getMethodAndAssumption(name, assumptions);
if (refinedMethod != null) {
InternalMethod method = rememberUsedRefinements(refinedMethod, declarationContext);
return new MethodLookupResult(method, toArray(assumptions));
}
}
}
var method = ancestor.fields.getMethodAndAssumption(name, assumptions);
if (method != null) {
return new MethodLookupResult(method, toArray(assumptions));
}
}
// Nothing found
return new MethodLookupResult(null, toArray(assumptions));
}
@TruffleBoundary
public static InternalMethod lookupMethodUncached(RubyModule module, String name,
DeclarationContext declarationContext) {
// Look in ancestors
for (RubyModule ancestor : module.fields.ancestors()) {
var refinements = getRefinementsFor(declarationContext, ancestor);
if (refinements != null) {
for (RubyModule refinement : refinements) {
var refinedMethod = refinement.fields.getMethod(name);
if (refinedMethod != null) {
return rememberUsedRefinements(refinedMethod, declarationContext);
}
}
}
var method = ancestor.fields.getMethod(name);
if (method != null) {
return method;
}
}
// Nothing found
return null;
}
@TruffleBoundary
public static MethodLookupResult lookupSuperMethod(InternalMethod currentMethod, RubyModule objectMetaClass) {
var name = currentMethod.getSharedMethodInfo().getMethodNameForNotBlock(); // use the original name
var foundDeclaringModule = new Memo<>(false);
var declaringModule = currentMethod.getDeclaringModule();
var declarationContext = currentMethod.getDeclarationContext();
var assumptions = new ArrayList<Assumption>();
// First we need to skip all ancestors until we find declaringModule,
// and then we return the first ancestor after declaringModule which has the method defined.
for (RubyModule ancestor : objectMetaClass.fields.ancestors()) {
var refinements = getRefinementsFor(declarationContext, currentMethod.getActiveRefinements(), ancestor);
if (refinements != null) {
for (RubyModule refinement : refinements) {
var refinedMethod = lookupSuperMethodInModule(declaringModule, name, foundDeclaringModule,
refinement, assumptions);
if (refinedMethod != null) {
return new MethodLookupResult(
rememberUsedRefinements(refinedMethod, declarationContext, refinements, ancestor),
toArray(assumptions));
}
if (foundDeclaringModule.get() && declaringModule.fields.isRefinement()) {
// if method is defined in refinement module (R)
// we should lookup only in this active refinement and skip others
break;
}
}
}
var method = lookupSuperMethodInModule(declaringModule, name, foundDeclaringModule, ancestor, assumptions);
if (method != null) {
return new MethodLookupResult(method, toArray(assumptions));
}
}
// Nothing found
return new MethodLookupResult(null, toArray(assumptions));
}
private static InternalMethod lookupSuperMethodInModule(RubyModule declaringModule, String name,
Memo<Boolean> foundDeclaringModule, RubyModule module, ArrayList<Assumption> assumptions) {
if (!foundDeclaringModule.get()) {
if (module == declaringModule) {
// The declaring module's assumption needs to appended for cases where a newly included module
// should invalidate previous super lookups.
module.fields.getMethodAndAssumption(name, assumptions);
foundDeclaringModule.set(true);
}
return null;
} else {
return module.fields.getMethodAndAssumption(name, assumptions);
}
}
private static InternalMethod rememberUsedRefinements(InternalMethod method,
DeclarationContext declarationContext) {
return method.withActiveRefinements(declarationContext);
}
private static InternalMethod rememberUsedRefinements(InternalMethod method,
DeclarationContext declarationContext, RubyModule[] refinements, RubyModule ancestor) {
assert refinements != null;
final Map<RubyModule, RubyModule[]> currentRefinements = new HashMap<>(declarationContext.getRefinements());
currentRefinements.put(ancestor, refinements);
return method.withActiveRefinements(declarationContext.withRefinements(currentRefinements));
}
private static RubyModule[] getRefinementsFor(DeclarationContext declarationContext,
DeclarationContext callerDeclaringContext, RubyModule module) {
final RubyModule[] lexicalRefinements = getRefinementsFor(declarationContext, module);
final RubyModule[] callerRefinements = getRefinementsFor(callerDeclaringContext, module);
if (lexicalRefinements == null) {
return callerRefinements;
}
if (callerRefinements == null) {
return lexicalRefinements;
}
final ArrayList<RubyModule> list = new ArrayList<>(Arrays.asList(callerRefinements));
for (RubyModule refinement : lexicalRefinements) {
if (!ArrayUtils.contains(callerRefinements, refinement)) {
list.add(refinement);
}
}
return list.toArray(RubyModule.EMPTY_ARRAY);
}
private static RubyModule[] getRefinementsFor(DeclarationContext declarationContext, RubyModule module) {
if (declarationContext == null) {
return null;
}
return declarationContext.getRefinementsFor(module);
}
private static Assumption[] toArray(ArrayList<Assumption> assumptions) {
return assumptions.toArray(EMPTY_ASSUMPTION_ARRAY);
}
@TruffleBoundary
public static void setClassVariable(RubyLanguage language, RubyContext context, RubyModule module, String name,
Object value, Node currentNode) {
ModuleFields moduleFields = module.fields;
moduleFields.checkFrozen(context, currentNode);
SharedObjects.propagate(language, module, value);
// if the cvar is not already defined we need to take lock and ensure there is only one
// defined in the class tree
if (!trySetClassVariable(module, name, value)) {
synchronized (context.getClassVariableDefinitionLock()) {
if (!trySetClassVariable(module, name, value)) {
moduleFields.getClassVariables().put(name, value, DynamicObjectLibrary.getUncached());
}
}
}
}
private static boolean trySetClassVariable(RubyModule topModule, String name, Object value) {
return classVariableLookup(
topModule,
true,
module -> module.fields.getClassVariables().putIfPresent(
name,
value,
DynamicObjectLibrary.getUncached()) ? module : null) != null;
}
@TruffleBoundary
public static Object removeClassVariable(ModuleFields fields, RubyContext context, Node currentNode, String name) {
fields.checkFrozen(context, currentNode);
final ClassVariableStorage classVariables = fields.getClassVariables();
final Object found = classVariables.remove(name, DynamicObjectLibrary.getUncached());
if (found == null) {
throw new RaiseException(
context,
context.getCoreExceptions().nameErrorClassVariableNotDefined(
name,
fields.rubyModule,
currentNode));
} else {
return found;
}
}
@TruffleBoundary
public static <R> R classVariableLookup(RubyModule module, boolean inherit, Function<RubyModule, R> action) {
// Look in the current module
R result = action.apply(module);
if (result != null) {
return result;
}
if (!inherit) {
return null;
}
// If singleton class of a module, check the attached module.
if (module instanceof RubyClass) {
RubyClass klass = (RubyClass) module;
if (klass.isSingleton && klass.attached instanceof RubyModule) {
module = (RubyModule) klass.attached;
result = action.apply(module);
if (result != null) {
return result;
}
}
}
// Look in ancestors
for (RubyModule ancestor : module.fields.ancestors()) {
if (ancestor == module) {
continue;
}
result = action.apply(ancestor);
if (result != null) {
return result;
}
}
return null;
}
@TruffleBoundary
public static boolean isMethodPrivateFromName(String name) {
return (name.equals("initialize") || name.equals("initialize_copy") ||
name.equals("initialize_clone") || name.equals("initialize_dup") ||
name.equals("respond_to_missing?"));
}
}