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