]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/NamespaceOfModule.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / compilation / NamespaceOfModule.java
1 package org.simantics.scl.compiler.compilation;
2
3 import java.util.Arrays;
4 import java.util.Collection;
5 import java.util.function.Consumer;
6
7 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
8 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
9 import org.simantics.scl.compiler.elaboration.modules.TypeClass;
10 import org.simantics.scl.compiler.elaboration.modules.TypeDescriptor;
11 import org.simantics.scl.compiler.elaboration.relations.SCLEntityType;
12 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
13 import org.simantics.scl.compiler.elaboration.rules.MappingRelation;
14 import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
15 import org.simantics.scl.compiler.environment.AmbiguousNameException;
16 import org.simantics.scl.compiler.environment.Namespace;
17 import org.simantics.scl.compiler.environment.filter.AcceptAllNamespaceFilter;
18 import org.simantics.scl.compiler.environment.filter.NamespaceFilter;
19 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
20 import org.simantics.scl.compiler.module.Module;
21 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
22 import org.simantics.scl.compiler.types.TCon;
23
24 import gnu.trove.procedure.TObjectProcedure;
25
26 public class NamespaceOfModule implements Namespace {
27     private final Namespace base;
28     private final Module module;
29     
30     public NamespaceOfModule(Namespace base, Module module) {
31         this.base = base;
32         this.module = module;
33     }
34
35     @Override
36     public Namespace getNamespace(String name) {
37         return base.getNamespace(name);
38     }
39     
40     @Override
41     public Collection<String> getNamespaces() {
42         return base.getNamespaces();
43     }
44
45     @Override
46     public SCLValue getValue(String name) throws AmbiguousNameException {
47         SCLValue value = module.getValue(name);
48         if(SCLCompilerConfiguration.ALLOW_OVERLOADING) {
49             SCLValue value2;
50             try {
51                 value2 = base.getValue(name);
52             } catch(AmbiguousNameException e) {
53                 if(value != null) {
54                     String[] conflictingModules = Arrays.copyOf(e.conflictingModules, e.conflictingModules.length+1);
55                     conflictingModules[e.conflictingModules.length] = module.getName();
56                     throw new AmbiguousNameException(Arrays.asList(conflictingModules), e.name);
57                 }
58                 else
59                     throw e;
60             }
61             if(value == null)
62                 return value2;
63             if(value2 == null)
64                 return value;
65             throw new AmbiguousNameException(Arrays.asList(value.getName().module, value2.getName().module), value.getName().name);
66         }
67         else {
68             if(value != null)
69                 return value;
70             return base.getValue(name);
71         }
72     }
73     
74     @Override
75     public SCLRelation getRelation(String name) throws AmbiguousNameException {
76         SCLRelation relation = module.getRelation(name);
77         if(relation != null)
78             return relation;
79         return base.getRelation(name);
80     }
81
82     @Override
83     public CHRRuleset getRuleset(String name) throws AmbiguousNameException {
84         CHRRuleset ruleset = module.getRuleset(name);
85         if(ruleset != null)
86             return ruleset;
87         return base.getRuleset(name);
88     }
89     
90     @Override
91     public SCLEntityType getEntityType(String name)
92             throws AmbiguousNameException {
93         SCLEntityType entityType = module.getEntityType(name);
94         if(entityType != null)
95             return entityType;
96         return base.getEntityType(name);
97     }
98
99     @Override
100     public TypeDescriptor getTypeDescriptor(String name)
101             throws AmbiguousNameException {
102         TypeDescriptor typeDescriptor = module.getTypeDescriptor(name);
103         if(typeDescriptor != null)
104             return typeDescriptor;
105         return base.getTypeDescriptor(name);
106     }
107
108     @Override
109     public EffectConstructor getEffectConstructor(String name)
110             throws AmbiguousNameException {
111         EffectConstructor effectConstructor = module.getEffectConstructor(name);
112         if(effectConstructor != null)
113             return effectConstructor;
114         return base.getEffectConstructor(name);
115     }
116
117     @Override
118     public TypeClass getTypeClass(String name) throws AmbiguousNameException {
119         TypeClass typeClass = module.getTypeClass(name);
120         if(typeClass != null)
121             return typeClass;
122         return base.getTypeClass(name);
123     }
124     
125     @Override
126     public MappingRelation getMappingRelation(String name)
127             throws AmbiguousNameException {
128         MappingRelation mappingRelation = module.getMappingRelation(name);
129         if(mappingRelation != null)
130             return mappingRelation;
131         return base.getMappingRelation(name);
132     }
133     
134     @Override
135     public TransformationRule getRule(String name) throws AmbiguousNameException {
136         TransformationRule rule = module.getRule(name);
137         if(rule != null)
138             return rule;
139         return base.getRule(name);
140     }
141
142     @Override
143     public void findValuesForPrefix(String prefix, NamespaceFilter filter, TObjectProcedure<SCLValue> proc) {
144         base.findValuesForPrefix(prefix, filter, proc);
145         module.findValuesForPrefix(prefix, AcceptAllNamespaceFilter.INSTANCE, proc);
146     }
147
148     @Override
149     public void findTypesForPrefix(String prefix, NamespaceFilter filter, Consumer<TCon> consumer) {
150         base.findTypesForPrefix(prefix, filter, consumer);
151         module.findTypesForPrefix(prefix, AcceptAllNamespaceFilter.INSTANCE, consumer);
152     }
153     
154 }