-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathModuleEnvironment.java
More file actions
1250 lines (1028 loc) · 33.7 KB
/
ModuleEnvironment.java
File metadata and controls
1250 lines (1028 loc) · 33.7 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
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2009-2013 CWI
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* * Jurgen J. Vinju - [email protected] - CWI
* * Tijs van der Storm - [email protected]
* * Emilie Balland - (CWI)
* * Anya Helene Bagge - (UiB)
* * Paul Klint - [email protected] - CWI
* * Mark Hills - [email protected] (CWI)
* * Arnold Lankamp - [email protected]
*******************************************************************************/
package org.rascalmpl.interpreter.env;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import org.rascalmpl.ast.AbstractAST;
import org.rascalmpl.ast.KeywordFormal;
import org.rascalmpl.ast.Name;
import org.rascalmpl.ast.QualifiedName;
import org.rascalmpl.interpreter.Evaluator;
import org.rascalmpl.interpreter.result.AbstractFunction;
import org.rascalmpl.interpreter.result.ConstructorFunction;
import org.rascalmpl.interpreter.result.OverloadedFunction;
import org.rascalmpl.interpreter.result.Result;
import org.rascalmpl.interpreter.staticErrors.UndeclaredModule;
import org.rascalmpl.interpreter.utils.Names;
import org.rascalmpl.library.Messages;
import org.rascalmpl.types.NonTerminalType;
import org.rascalmpl.types.RascalTypeFactory;
import org.rascalmpl.uri.URIUtil;
import org.rascalmpl.values.IRascalValueFactory;
import org.rascalmpl.values.RascalValueFactory;
import org.rascalmpl.values.ValueFactoryFactory;
import io.usethesource.capsule.SetMultimap;
import io.usethesource.capsule.core.PersistentTrieSetMultimap;
import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IMap;
import io.usethesource.vallang.IMapWriter;
import io.usethesource.vallang.ISetWriter;
import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.ITuple;
import io.usethesource.vallang.IValue;
import io.usethesource.vallang.IValueFactory;
import io.usethesource.vallang.exceptions.FactTypeUseException;
import io.usethesource.vallang.type.Type;
import io.usethesource.vallang.type.TypeFactory;
import io.usethesource.vallang.type.TypeStore;
/**
* A module environment represents a module object (i.e. a running module).
* It manages imported modules and visibility of the
* functions and variables it declares.
*/
public class ModuleEnvironment extends Environment {
private final GlobalEnvironment heap;
/** Map of imported modules to resolved ModuleEnvironments, will only be empty in case of a module was in a cycle or got reloaded and failed during the reload. use {@link #importedModulesResolved} to lazily resolve the modules. */
private Map<String, Optional<ModuleEnvironment>> importedModules;
private Set<String> extended;
private TypeStore typeStore;
private Set<IValue> productions;
private Map<Type, List<KeywordFormal>> generalKeywordParameters;
private Map<String, NonTerminalType> concreteSyntaxTypes;
private boolean initialized;
private boolean syntaxDefined;
private boolean bootstrap;
private String deprecated;
private List<IConstructor> loadMessages = new LinkedList<>();
private Map<String, AbstractFunction> resourceImporters;
private Map<Type, Set<GenericKeywordParameters>> cachedGeneralKeywordParameters;
private Map<String, List<AbstractFunction>> cachedPublicFunctions;
private static final TypeFactory TF = TypeFactory.getInstance();
public final static String SHELL_MODULE = "$";
public ModuleEnvironment(String name, GlobalEnvironment heap) {
super(ValueFactoryFactory.getValueFactory().sourceLocation(URIUtil.assumeCorrect("main", "", "/" + name.replaceAll("::", "/").replaceAll("\\$", "_dollar_"))), name);
this.heap = heap;
this.importedModules = new HashMap<>();
this.concreteSyntaxTypes = new HashMap<String, NonTerminalType>();
this.productions = new HashSet<IValue>();
this.generalKeywordParameters = new HashMap<Type,List<KeywordFormal>>();
this.typeStore = new TypeStore();
this.initialized = false;
this.syntaxDefined = false;
this.bootstrap = false;
this.resourceImporters = new HashMap<String, AbstractFunction>();
this.cachedGeneralKeywordParameters = null;
this.cachedPublicFunctions = null;
}
@Override
public void reset() {
super.reset();
this.importedModules = new HashMap<>();
this.concreteSyntaxTypes = new HashMap<>();
this.typeStore = new TypeStore();
this.productions = new HashSet<IValue>();
this.initialized = false;
this.syntaxDefined = false;
this.bootstrap = false;
this.extended = new HashSet<String>();
this.deprecated = null;
this.loadMessages.clear();
this.generalKeywordParameters = new HashMap<>();
this.cachedGeneralKeywordParameters = null;
this.cachedPublicFunctions = null;
}
public void clearLookupCaches() {
importedModules.replaceAll((k, v) -> Optional.empty());
cachedGeneralKeywordParameters = null;
cachedPublicFunctions = null;
}
/**
* This is useful for the tutor and the eval repl where we
* sometimes need to forget about previous errors without
* actually fixing them. This is not for the real REPL, where
* we _do_ want to be reminded of previous erroneous initializations.
*/
public void clearLoadMessages() {
this.loadMessages.clear();
}
public void extend(ModuleEnvironment other) {
extendNameFlags(other);
// First extend the imports before functions and variables
// so that types become available
if (other.importedModules != null) {
if (this.importedModules == null) {
this.importedModules = new HashMap<>();
}
this.importedModules.putAll(other.importedModules);
}
if (other.concreteSyntaxTypes != null) {
if (this.concreteSyntaxTypes == null) {
this.concreteSyntaxTypes = new HashMap<String,NonTerminalType>();
}
this.concreteSyntaxTypes.putAll(other.concreteSyntaxTypes);
}
if (other.typeStore != null) {
if (this.typeStore == null) {
this.typeStore = new TypeStore();
}
this.typeStore.extendStore(other.typeStore);
}
if (other.productions != null) {
if (this.productions == null) {
this.productions = new HashSet<IValue>();
}
this.productions.addAll(other.productions);
}
if (other.extended != null) {
if (this.extended == null) {
this.extended = new HashSet<String>();
}
this.extended.addAll(other.extended);
}
if (other.generalKeywordParameters != null) {
if (this.generalKeywordParameters == null) {
this.generalKeywordParameters = new HashMap<>();
}
for (Entry<Type, List<KeywordFormal>> e : other.generalKeywordParameters.entrySet()) {
this.generalKeywordParameters.compute(e.getKey(), (k, current) -> {
if (current == null) {
// only a new copy is needed
return new ArrayList<>(e.getValue());
}
else {
return mergeKeywords(e.getValue(), current);
}
});
}
}
extendTypeParams(other);
extendVariableEnv(other);
extendFunctionEnv(other);
this.initialized &= other.initialized;
this.syntaxDefined |= other.syntaxDefined;
this.bootstrap |= other.bootstrap;
addExtend(other.getName());
}
private List<KeywordFormal> mergeKeywords(List<KeywordFormal> a, List<KeywordFormal> b) {
ArrayList<KeywordFormal> result = new ArrayList<>(a.size() + b.size());
result.addAll(a);
for (var k : b) {
if (!keywordFormalExists(result, k)) {
result.add(k);
}
}
return result;
}
@Override
public GlobalEnvironment getHeap() {
return heap;
}
public boolean isSyntaxDefined() {
return syntaxDefined;
}
public void setSyntaxDefined(boolean val) {
this.syntaxDefined = val;
}
public void declareProduction(IConstructor sd) {
productions.add(sd);
}
public void clearProductions() {
if (productions != null) {
productions.clear();
}
}
public void addLoadError(String message, ISourceLocation loc, String trace) {
loadMessages.add(Messages.addCause(Messages.error(message, loc), trace, loc));
}
public void addLoadWarning(String message, ISourceLocation loc) {
loadMessages.add(Messages.warning(message, loc));
}
public void addLoadInfo(String message, ISourceLocation loc) {
loadMessages.add(Messages.info(message, loc));
}
public void writeLoadMessages(PrintWriter out) {
Messages.write(loadMessages.stream().collect(IRascalValueFactory.getInstance().listWriter()), out);
}
public boolean definesSyntax() {
if (!productions.isEmpty()) {
return true;
}
for (String mod : getExtendsTransitive()) {
ModuleEnvironment env = heap.getModule(mod);
if (env != null) {
if (!env.productions.isEmpty()) {
return true;
}
}
}
for(String mod : getImportsTransitive()){
ModuleEnvironment env = heap.getModule(mod);
if (env != null) {
if (!env.productions.isEmpty()) {
return true;
}
}
}
return false;
}
/**
* Builds a map to communicate all relevant syntax definitions to the parser generator.
* See lang::rascal::grammar::definition::Modules.modules2grammar()
*/
public IMap getSyntaxDefinition() {
List<String> todo = new LinkedList<String>();
Set<String> done = new HashSet<String>();
todo.add(getName());
IValueFactory VF = ValueFactoryFactory.getValueFactory();
IMapWriter result = VF.mapWriter();
while(!todo.isEmpty()){
String m = todo.get(0);
todo.remove(0);
if(done.contains(m))
continue;
done.add(m);
/* This allows the current module not to be loaded on the heap, for
* parsing in the IDE
*/
ModuleEnvironment env = m.equals(getName()) ? this : heap.getModule(m);
if(env != null){
ISetWriter importWriter = VF.setWriter();
for(String impname : env.getImports()){
if(!done.contains(impname)) todo.add(impname);
importWriter.insert(VF.string(impname));
}
ISetWriter extendWriter = VF.setWriter();
for(String impname : env.getExtends()){
if(!done.contains(impname)) todo.add(impname);
extendWriter.insert(VF.string(impname));
}
ISetWriter defWriter = VF.setWriter();
for(IValue def : env.productions){
defWriter.insert(def);
}
ITuple t = VF.tuple(importWriter.done(), extendWriter.done(), defWriter.done());
result.put(VF.string(m), t);
}else if(m.equals(getName())) { // This is the root scope.
ISetWriter importWriter = VF.setWriter();
for(String impname : importedModules.keySet()){
if(!done.contains(impname)) todo.add(impname);
importWriter.insert(VF.string(impname));
}
ISetWriter extendWriter = VF.setWriter();
for(String impname : getExtends()){
if(!done.contains(impname)) todo.add(impname);
extendWriter.insert(VF.string(impname));
}
ISetWriter defWriter = VF.setWriter();
for(IValue def : productions){
defWriter.insert(def);
}
ITuple t = VF.tuple(importWriter.done(), extendWriter.done(), defWriter.done());
result.put(VF.string(m), t);
}
}
return result.done();
}
public boolean isModuleEnvironment() {
return true;
}
public void addImport(String name, ModuleEnvironment env) {
assert heap.getModule(name).equals(env);
importedModules.put(name, Optional.ofNullable(env));
typeStore.importStore(env.typeStore);
this.cachedGeneralKeywordParameters = null;
this.cachedPublicFunctions = null;
}
void removeModule(String name) {
importedModules.computeIfPresent(name, (k, v) -> Optional.empty());
this.cachedGeneralKeywordParameters = null;
this.cachedPublicFunctions = null;
}
public void addExtend(String name) {
if (extended == null) {
extended = new HashSet<String>();
}
extended.add(name);
this.cachedGeneralKeywordParameters = null;
this.cachedPublicFunctions = null;
}
public List<AbstractFunction> getTests() {
List<AbstractFunction> result = new LinkedList<AbstractFunction>();
if (functionEnvironment != null) {
for (LinkedHashSet<AbstractFunction> f : functionEnvironment.values()) {
for (AbstractFunction c : f) {
if (c.isTest()) {
result.add(c);
}
}
}
}
return result;
}
@Override
public Set<String> getImports() {
return Collections.unmodifiableSet(importedModules.keySet());
}
public Set<String> getImportsTransitive() {
List<String> todo = new LinkedList<String>();
Set<String> done = new HashSet<String>();
Set<String> result = new HashSet<String>();
todo.add(this.getName());
GlobalEnvironment heap = getHeap();
while (!todo.isEmpty()) {
String mod = todo.remove(0);
done.add(mod);
ModuleEnvironment env = mod.equals(getName())? this : heap.getModule(mod);
if (env != null) {
for (String e : env.getImports()) {
result.add(e);
if (!done.contains(e)) {
todo.add(e);
}
}
}
}
return result;
}
public void unImport(String moduleName) {
if (importedModules != null) {
var old = importedModules.remove(moduleName);
if (old != null && old.isPresent()) {
typeStore.unimportStores(old.get().getStore());
}
}
cachedGeneralKeywordParameters = null;
cachedPublicFunctions = null;
}
public void unExtend(String moduleName) {
if (extended != null) {
extended.remove(moduleName);
}
clearLookupCaches();
}
@Override
public String getName() {
return name;
}
@Override
public TypeStore getStore() {
return typeStore;
}
@Override
public Result<IValue> getVariable(QualifiedName name) {
String modulename = Names.moduleName(name);
String cons = Names.name(Names.lastName(name));
Type adt = getAbstractDataType(modulename);
if (adt != null) {
List<AbstractFunction> result = new LinkedList<AbstractFunction>();
getAllFunctions(adt, cons, result);
if (result.isEmpty()) {
return null;
}
if (result.size() == 1) {
return result.get(0);
}
else {
return new OverloadedFunction(cons, result);
}
}
if (modulename != null) {
if (modulename.equals(getName())) {
return getFrameVariable(cons);
}
ModuleEnvironment imported = getImport(modulename);
if (imported == null) {
throw new UndeclaredModule(modulename, name);
}
// TODO: will this not do a transitive closure? This should not happen...
return imported.getVariable(name);
}
return getFrameVariable(cons);
}
@Override
public void storeVariable(String name, Result<IValue> value) {
Result<IValue> result = super.getFrameVariable(name);
if (result != null) {
super.storeVariable(name, value);
}
else {
for (ModuleEnvironment module : importedModulesResolved) {
result = module.getLocalPublicVariable(name);
if (result != null) {
module.storeVariable(name, value);
return;
}
}
super.storeVariable(name, value);
}
}
@Override
public org.rascalmpl.interpreter.result.Result<IValue> getSimpleVariable(String name) {
Result<IValue> var = super.getSimpleVariable(name);
if (var != null) {
return var;
}
for (ModuleEnvironment mod : importedModulesResolved) {
if (mod != null) {
var = mod.getLocalPublicVariable(name);
}
if (var != null) {
return var;
}
}
return null;
}
/**
* Search for the environment that declared a variable.
*/
@Override
protected Map<String,Result<IValue>> getVariableDefiningEnvironment(String name) {
if (variableEnvironment != null) {
Result<IValue> r = variableEnvironment.get(name);
if (r != null) {
return variableEnvironment;
}
}
for (ModuleEnvironment mod : importedModulesResolved) {
Result<IValue> r = null;
if (mod != null && mod.variableEnvironment != null)
r = mod.variableEnvironment.get(name);
if (r != null && !mod.isVariablePrivate(name)) {
return mod.variableEnvironment;
}
}
return null;
}
@Override
public void getAllFunctions(String name, List<AbstractFunction> collection) {
collection.addAll(lookupCachedFunctions(name));
}
private List<AbstractFunction> lookupFunctionsNoCache(String name) {
var result = new ArrayList<AbstractFunction>();
super.getAllFunctions(name, result);
for (ModuleEnvironment mod : importedModulesResolved) {
if (mod != null) {
mod.getLocalPublicFunctions(name, result);
}
}
return result;
}
private List<AbstractFunction> lookupCachedFunctions(String name) {
if (cachedPublicFunctions == null) {
cachedPublicFunctions = io.usethesource.capsule.Map.Transient.of();
}
if (!initialized) {
return lookupFunctionsNoCache(name);
}
else {
return cachedPublicFunctions.computeIfAbsent(name, this::lookupFunctionsNoCache);
}
}
@Override
public void getAllFunctions(Type returnType, String name, List<AbstractFunction> collection) {
for (var function: lookupCachedFunctions(name)) {
if (function.getReturnType().comparable(returnType)) {
collection.add(function);
}
}
}
private Result<IValue> getLocalPublicVariable(String name) {
Result<IValue> var = null;
if (variableEnvironment != null) {
var = variableEnvironment.get(name);
}
if (var != null && !isVariablePrivate(name)) {
return var;
}
return null;
}
private void getLocalPublicFunctions(String name, List<AbstractFunction> collection) {
if (functionEnvironment != null) {
LinkedHashSet<AbstractFunction> lst = functionEnvironment.get(name);
if (lst != null) {
if (!isFunctionPrivate(name)) {
collection.addAll(lst);
}
}
}
}
@Override
public Type abstractDataType(String name, Type... parameters) {
return TF.abstractDataType(typeStore, name, parameters);
}
@Override
public Type concreteSyntaxType(String name, IConstructor symbol) {
NonTerminalType sort = (NonTerminalType) RascalTypeFactory.getInstance().nonTerminalType(symbol);
concreteSyntaxTypes.put(name, sort);
return sort;
}
@Override
public void unsetConcreteSyntaxType(String name) {
concreteSyntaxTypes.remove(name);
}
private Type makeTupleType(Type adt, String name, Type tupleType) {
return TF.constructorFromTuple(typeStore, adt, name, tupleType);
}
@Override
public ConstructorFunction constructorFromTuple(AbstractAST ast, Evaluator eval, Type adt, String name, Type tupleType, List<KeywordFormal> initializers) {
Type cons = makeTupleType(adt, name, tupleType);
ConstructorFunction function = new ConstructorFunction(ast, eval, this, cons, initializers);
storeFunction(name, function);
markFunctionNameFinal(name);
markFunctionNameOverloadable(name);
return function;
}
// @Override
// public ConstructorFunction constructor(AbstractAST ast, Evaluator eval, Type nodeType, String name,
// Map<String, Type> kwArgs, Map<String, IValue> kwDefaults, Object... childrenAndLabels) {
// Type cons = TF.constructor(typeStore, nodeType, name, childrenAndLabels, kwArgs, kwDefaults);
// ConstructorFunction function = new ConstructorFunction(ast, eval, this, cons);
// storeFunction(name, function);
// markNameFinal(name);
// markNameOverloadable(name);
// return function;
// }
@Override
public Type aliasType(String name, Type aliased, Type... parameters) {
return TF.aliasType(typeStore, name, aliased, parameters);
}
@Override
public void declareAnnotation(Type onType, String label, Type valueType) {
// TODO: simulating annotations still here
if (RascalValueFactory.isLegacySourceLocationAnnotation(onType, label)) {
label = RascalValueFactory.Location;
}
typeStore.declareKeywordParameter(onType, label, valueType);
}
private boolean keywordFormalExists(List<KeywordFormal> haystack, KeywordFormal needle) {
String label = ((Name.Lexical) needle.getName()).getString();
for (KeywordFormal candidate : haystack) {
if (((Name.Lexical) candidate.getName()).getString().equals(label)) {
return true;
}
}
return false;
}
@Override
public void declareGenericKeywordParameters(Type adt, Type kwTypes, List<KeywordFormal> formals) {
List<KeywordFormal> list = generalKeywordParameters.get(adt);
if (list == null) {
list = new ArrayList<KeywordFormal>();
generalKeywordParameters.put(adt, list);
}
// due to the `extend` feature we might redeclare many formals, so this loop is to avoid duplicate declaration.
// it is important to retain the declaration order, due to the way default expressions can depend on previously declared formals.
// this is why the list is a list and not a possibly faster set.
for (KeywordFormal f : formals) {
if (!keywordFormalExists(list, f)) {
list.add(f);
}
}
for (String label : kwTypes.getFieldNames()) {
typeStore.declareKeywordParameter(adt, label, kwTypes.getFieldType(label));
}
}
@Override
public Map<String, Type> getKeywordParameterTypes(Type ontype) {
return typeStore.getKeywordParameters(ontype);
}
public static class GenericKeywordParameters {
// kw params with default expressions:
final List<KeywordFormal> formals;
// environment in which they are declared:
final ModuleEnvironment env;
final Map<String, Type> types;
public GenericKeywordParameters(ModuleEnvironment env, List<KeywordFormal> formals, Map<String,Type> types) {
this.env = env;
this.formals = Collections.unmodifiableList(formals);
this.types = types;
}
public Map<String, Type> getTypes() {
return types;
}
public ModuleEnvironment getEnv() {
return env;
}
public List<KeywordFormal> getFormals() {
return formals;
}
}
@Override
public Set<GenericKeywordParameters> lookupGenericKeywordParameters(Type adt) {
if (cachedGeneralKeywordParameters != null) {
var result = cachedGeneralKeywordParameters.get(adt);
if (result != null) {
return result;
}
}
else {
cachedGeneralKeywordParameters = io.usethesource.capsule.Map.Transient.of();
}
Set<GenericKeywordParameters> result = new HashSet<>();
List<KeywordFormal> list = generalKeywordParameters.get(adt);
if (list != null) {
result.add(new GenericKeywordParameters(this, list, getStore().getKeywordParameters(adt)));
}
for (ModuleEnvironment mod : importedModulesResolved) {
list = mod.generalKeywordParameters.get(adt);
if (list != null) {
result.add(new GenericKeywordParameters(mod, list, mod.getStore().getKeywordParameters(adt)));
}
}
// save the result for next time
cachedGeneralKeywordParameters.put(adt, result);
return result;
}
@Override
public void declareConstructorKeywordParameter(Type onType, String label, Type valueType) {
typeStore.declareKeywordParameter(onType, label, valueType);
}
@Override
public Type getAnnotationType(Type type, String label) {
Type anno = typeStore.getKeywordParameterType(type, label);
if (anno == null && type instanceof NonTerminalType) {
return typeStore.getKeywordParameterType(RascalValueFactory.Tree, label);
}
return anno;
}
public Collection<Type> getAbstractDatatypes() {
return typeStore.getAbstractDataTypes();
}
public Collection<Type> getAliases() {
return typeStore.getAliases();
}
public Map<Type, Map<String, Type>> getAnnotations() {
// TODO: simulating annotations here
return typeStore.getKeywordParameters();
}
@Override
public Type getAbstractDataType(String sort) {
return typeStore.lookupAbstractDataType(sort);
}
@Override
public Type getConstructor(String cons, Type args) {
return typeStore.lookupFirstConstructor(cons, args);
}
@Override
public Type getConstructor(Type sort, String cons, Type args) {
return typeStore.lookupConstructor(sort, cons, args);
}
@Override
public boolean isTreeConstructorName(QualifiedName name, Type signature) {
java.util.List<Name> names = name.getNames();
if (names.size() > 1) {
String sort = Names.sortName(name);
Type sortType = getAbstractDataType(sort);
if (sortType != null) {
String cons = Names.consName(name);
if (getConstructor(sortType, cons, signature) != null) {
return true;
}
}
}
else {
String cons = Names.consName(name);
if (getConstructor(cons, signature) != null) {
return true;
}
}
return false;
}
@Override
public String toString() {
return "Environment [ " + getName() + ", imports: " + ((importedModules != null) ? importedModules : "") + ", extends: " + ((extended != null) ? extended : "") + "]";
}
@Override
public ModuleEnvironment getImport(String moduleName) {
var result = importedModules.computeIfPresent(moduleName,
(m, c) -> c.isPresent() ? c : Optional.ofNullable(heap.getModule(m))
);
if (result == null || result.isEmpty()) {
return null;
}
return result.get();
}
private Iterable<ModuleEnvironment> importedModulesResolved =
() -> new Iterator<ModuleEnvironment>() {
Iterator<Entry<String, Optional<ModuleEnvironment>>> iterator = importedModules.entrySet().iterator();
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public ModuleEnvironment next() {
var entry = iterator.next();
var result = entry.getValue();
if (result.isEmpty()) {
result = Optional.ofNullable(heap.getModule(entry.getKey()));
entry.setValue(result);
}
return result.orElse(null);
}
};
@Override
public void storeVariable(QualifiedName name, Result<IValue> result) {
String modulename = Names.moduleName(name);
if (modulename != null) {
if (modulename.equals(getName())) {
storeVariable(Names.name(Names.lastName(name)), result);
return;
}
ModuleEnvironment imported = getImport(modulename);
if (imported == null) {
throw new UndeclaredModule(modulename, name);
}
imported.storeVariable(name, result);
return;
}
super.storeVariable(name, result);
}
@Override
public boolean declaresAnnotation(Type type, String label) {
// TODO: we simulate annotations using kw fields here
return typeStore.getKeywordParameterType(type, label) != null;
}
@Override
public Type lookupAbstractDataType(String name) {
return typeStore.lookupAbstractDataType(name);
}
@Override
public Type lookupConcreteSyntaxType(String name) {
Type type = concreteSyntaxTypes.get(name);
if (type == null) {
for (ModuleEnvironment mod : importedModulesResolved) {
if (mod == null) {
continue;
}
// don't recurse here (cyclic imports!)
type = mod.concreteSyntaxTypes.get(name);
if (type != null) {
return type;
}
}
}
return type;
}
@Override
public Type lookupAlias(String name) {
return typeStore.lookupAlias(name);
}
@Override
public Set<Type> lookupAlternatives(Type adt) {
return typeStore.lookupAlternatives(adt);
}
@Override
public Type lookupConstructor(Type adt, String cons, Type args) {
return typeStore.lookupConstructor(adt, cons, args);
}
@Override
public Set<Type> lookupConstructor(Type adt, String constructorName)
throws FactTypeUseException {
return typeStore.lookupConstructor(adt, constructorName);
}
@Override
public Set<Type> lookupConstructors(String constructorName) {
return typeStore.lookupConstructors(constructorName);
}
@Override
public Type lookupFirstConstructor(String cons, Type args) {
return typeStore.lookupFirstConstructor(cons, args);
}
public boolean isInitialized() {
return initialized;
}
public void setInitialized() {
this.initialized = true;
}
public void setInitialized(boolean init) {
this.initialized = init;
}