]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/ConcreteModule.java
Merge "Improved Statement API in Simantics/DB"
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / module / ConcreteModule.java
1 package org.simantics.scl.compiler.module;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.function.Consumer;
9
10 import org.simantics.scl.compiler.common.names.Name;
11 import org.simantics.scl.compiler.constants.Constant;
12 import org.simantics.scl.compiler.elaboration.modules.Documentation;
13 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
14 import org.simantics.scl.compiler.elaboration.modules.TypeClass;
15 import org.simantics.scl.compiler.elaboration.modules.TypeClassInstance;
16 import org.simantics.scl.compiler.elaboration.modules.TypeDescriptor;
17 import org.simantics.scl.compiler.elaboration.relations.SCLEntityType;
18 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
19 import org.simantics.scl.compiler.elaboration.rules.MappingRelation;
20 import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
21 import org.simantics.scl.compiler.environment.filter.NamespaceFilter;
22 import org.simantics.scl.compiler.errors.CompilationError;
23 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
24 import org.simantics.scl.compiler.top.ModuleInitializer;
25 import org.simantics.scl.compiler.types.TCon;
26 import org.simantics.scl.runtime.profiling.BranchPoint;
27
28 import gnu.trove.map.hash.THashMap;
29 import gnu.trove.procedure.TObjectObjectProcedure;
30 import gnu.trove.procedure.TObjectProcedure;
31
32 public class ConcreteModule implements Module {
33     String moduleName;
34     String defaultLocalName;
35     THashMap<String, TypeDescriptor> typeDescriptors = new THashMap<String, TypeDescriptor>();
36     THashMap<String, EffectConstructor> effectConstructors = new THashMap<String, EffectConstructor>();
37     THashMap<String, TypeClass> typeClasses = new THashMap<String, TypeClass>();
38     THashMap<TCon, ArrayList<TypeClassInstance>> typeClassInstances = new THashMap<TCon, ArrayList<TypeClassInstance>>();
39     THashMap<String, SCLValue> values = new THashMap<String, SCLValue>();
40     THashMap<String, List<Constant>> fieldAccessors = new THashMap<String, List<Constant>>();
41     THashMap<String, SCLRelation> relations = new THashMap<String, SCLRelation>();
42     THashMap<String, SCLEntityType> entityTypes = new THashMap<String, SCLEntityType>();
43     THashMap<String, TransformationRule> rules = new THashMap<String, TransformationRule>();
44     THashMap<String, MappingRelation> mappingRelations = new THashMap<String, MappingRelation>();
45     ArrayList<ImportDeclaration> dependencies = new ArrayList<ImportDeclaration>();
46     THashMap<String, BranchPoint[]> branchPoints;
47     CompilationError[] warnings = CompilationError.EMPTY_ARRAY;
48     
49     Map<String, byte[]> classes = Collections.emptyMap();
50     ClassLoader parentClassLoader;
51     ModuleInitializer moduleInitializer;
52
53     protected Documentation documentation;
54
55     public ConcreteModule(String moduleName) {
56         this.moduleName = moduleName;
57     }
58
59     @Override
60     public String getDefaultLocalName() {
61         return defaultLocalName;
62     }
63     
64     public void setDefaultLocalName(String defaultLocalName) {
65                 this.defaultLocalName = defaultLocalName;
66         }
67     
68     public boolean addTypeDescriptor(String name, TypeDescriptor typeConstructor) {
69         return typeDescriptors.put(name, typeConstructor) != null;
70     }
71
72     public boolean addEffectConstructor(String name, EffectConstructor effectConstructor) {
73         return effectConstructors.put(name, effectConstructor) != null;
74     }
75
76     public boolean addTypeClass(String name, TypeClass typeClass) {
77         return typeClasses.put(name, typeClass) != null;
78     }
79
80     public void addTypeClassInstance(TCon typeClass, TypeClassInstance typeClassInstance) {
81         ArrayList<TypeClassInstance> instances = typeClassInstances.get(typeClass);
82         if(instances == null) {
83             instances = new ArrayList<TypeClassInstance>();
84             typeClassInstances.put(typeClass, instances);
85         }
86         instances.add(typeClassInstance);
87     }
88     
89     public boolean addRule(TransformationRule rule) {
90         return rules.put(rule.name.name, rule) != null;
91     }
92     
93     public boolean addMappingRelation(MappingRelation relation) {
94         return mappingRelations.put(relation.name.name, relation) != null;
95     }
96
97     public Collection<TCon> getTypeClassesWithInstances() {
98         return typeClassInstances.keySet();
99     }
100
101     public boolean addValue(SCLValue value) {
102         return values.put(value.getName().name, value) != null;
103     }
104
105     public SCLValue addValue(String name, Constant constant) {
106         SCLValue value = new SCLValue(Name.create(moduleName, name), constant);
107         addValue(value);
108         return value;
109     }
110     
111     @Override
112     public List<Constant> getFieldAccessors(String name) {
113         return fieldAccessors.get(name);
114     }
115
116     public void addRelation(String name, SCLRelation relation) {
117         relations.put(name, relation);
118     }
119
120     public void addEntityType(String name, SCLEntityType entityType) {
121         entityTypes.put(name, entityType);
122     }
123
124     public void addDependency(ImportDeclaration module) {
125         if(!dependencies.contains(module))
126             dependencies.add(module);
127     }
128
129     public Collection<SCLValue> getValues() {
130         return values.values();
131     }
132
133     public Collection<TransformationRule> getRules() {
134         return rules.values();
135     }
136     
137     public Collection<MappingRelation> getMappingRelations() {
138         return mappingRelations.values();
139     }
140     
141     @Override
142     public String getName() {
143         return moduleName;
144     }
145
146     @Override
147     public SCLValue getValue(String name) {
148         return values.get(name);
149     }
150
151     @Override
152     public SCLRelation getRelation(String name) {
153         return relations.get(name);
154     }
155     
156     @Override
157     public MappingRelation getMappingRelation(String name) {
158         return mappingRelations.get(name);
159     }
160     
161     @Override
162     public TransformationRule getRule(String name) {
163         return rules.get(name);
164     }
165
166     @Override
167     public SCLEntityType getEntityType(String name) {
168         return entityTypes.get(name);
169     }
170
171     public TypeClass getTypeClass(String name) {
172         return typeClasses.get(name);
173     }
174
175     @Override
176     public Collection<TypeClassInstance> getInstances(TCon typeClass) {
177         Collection<TypeClassInstance> result = typeClassInstances.get(typeClass);
178         if(result == null)
179             return Collections.emptyList();
180         else
181             return result;
182     }
183
184     @Override
185     public TypeDescriptor getTypeDescriptor(String name) {
186         return typeDescriptors.get(name);
187     }
188
189     @Override
190     public EffectConstructor getEffectConstructor(String name) {
191         return effectConstructors.get(name);
192     }
193
194     public Collection<TypeClass> getTypeClasses() {
195         return typeClasses.values();
196     }
197
198     public THashMap<TCon, ArrayList<TypeClassInstance>> getTypeInstances() {
199         return typeClassInstances;
200     }
201
202     @Override
203     public List<ImportDeclaration> getDependencies() {
204         return dependencies;
205     }
206
207     public void setDocumentation(Documentation documentation) {
208         this.documentation = documentation;
209     }
210
211     public Documentation getDocumentation() {
212         return documentation;
213     }
214
215     public void setClasses(Map<String, byte[]> classes) {
216         this.classes = classes;
217     }
218
219     @Override
220     public byte[] getClass(String name) {
221         return classes.get(name);
222     }
223
224     public void setModuleInitializer(ModuleInitializer moduleInitializer) {
225         this.moduleInitializer = moduleInitializer;
226     }
227
228     @Override
229     public ModuleInitializer getModuleInitializer() {
230         return moduleInitializer;
231     }
232
233     @Override
234     public String toString() {
235         return moduleName;
236     }
237     
238     @Override
239     public void findValuesForPrefix(final String prefix, final NamespaceFilter filter,
240             final TObjectProcedure<SCLValue> proc) {
241         this.values.forEachEntry(new TObjectObjectProcedure<String,SCLValue>() {
242             @Override
243             public boolean execute(String name, SCLValue value) {
244                 if(value.isPrivate())
245                     return true;
246                 String lowerPrefix = prefix.toLowerCase();
247                 String lowerName = name.toLowerCase();
248                 if(lowerName.startsWith(lowerPrefix) && filter.isValueIncluded(name))
249                     proc.execute(value);
250                 return true;
251             }
252         });
253     }
254     
255     @Override
256     public void findValuesForPrefix(String prefix, NamespaceFilter filter, Consumer<SCLValue> consumer) {
257         values.values().forEach((Consumer<SCLValue>) (value) -> {
258             String lowerPrefix = prefix.toLowerCase();
259             String lowerName = value.getName().name.toLowerCase();
260             if(lowerName.startsWith(lowerPrefix) && filter.isValueIncluded(value.getName().name))
261                 consumer.accept(value);
262         });
263     }
264
265     public Collection<SCLRelation> getRelations() {
266         return relations.values();
267     }
268
269     @Override
270     public void findTypesForPrefix(String prefix, NamespaceFilter filter, Consumer<TCon> consumer) {
271         typeDescriptors.values().forEach(type -> {
272             TCon tcon = type.name;
273             if (tcon.name.toLowerCase().startsWith(prefix.toLowerCase()) && filter.isValueIncluded(tcon.name))
274                 consumer.accept(tcon);
275         });
276     }
277     
278     public void setBranchPoints(THashMap<String, BranchPoint[]> branchPoints) {
279         this.branchPoints = branchPoints;
280     }
281     
282     @Override
283     public THashMap<String, BranchPoint[]> getBranchPoints() {
284         return branchPoints;
285     }
286
287     @Override
288     public void dispose() {
289     }
290     
291     public void setWarnings(CompilationError[] warnings) {
292         this.warnings = warnings;
293     }
294     
295     public CompilationError[] getWarnings() {
296         return warnings;
297     }
298     
299     @Override
300     public ClassLoader getParentClassLoader() {
301         return parentClassLoader;
302     }
303     
304     public void setParentClassLoader(ClassLoader parentClassLoader) {
305         if(parentClassLoader == null)
306             throw new NullPointerException();
307         this.parentClassLoader = parentClassLoader;
308     }
309     
310     public void addFieldAccessor(String name, Constant accessor) {
311         List<Constant> list = fieldAccessors.get(name);
312         if(list == null) {
313             list = new ArrayList<Constant>(2);
314             fieldAccessors.put(name, list);
315         }
316         list.add(accessor);
317     }
318 }