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