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