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