1 package org.simantics.scl.compiler.module;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
8 import java.util.function.Consumer;
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;
28 import gnu.trove.map.hash.THashMap;
29 import gnu.trove.procedure.TObjectObjectProcedure;
30 import gnu.trove.procedure.TObjectProcedure;
32 public class ConcreteModule implements Module {
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;
47 Map<String, byte[]> classes = Collections.emptyMap();
48 ModuleInitializer moduleInitializer;
50 protected Documentation documentation;
52 public ConcreteModule(String moduleName) {
53 this.moduleName = moduleName;
56 public boolean addTypeConstructor(String name, TypeConstructor typeConstructor) {
57 return typeConstructors.put(name, typeConstructor) != null;
60 public boolean addTypeAlias(String name, TypeAlias alias) {
61 return typeAliases.put(name, alias) != null;
64 public boolean addEffectConstructor(String name, EffectConstructor effectConstructor) {
65 return effectConstructors.put(name, effectConstructor) != null;
68 public boolean addTypeClass(String name, TypeClass typeClass) {
69 return typeClasses.put(name, typeClass) != null;
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);
78 instances.add(typeClassInstance);
81 public boolean addRule(TransformationRule rule) {
82 return rules.put(rule.name.name, rule) != null;
85 public boolean addMappingRelation(MappingRelation relation) {
86 return mappingRelations.put(relation.name.name, relation) != null;
89 public Collection<TCon> getTypeClassesWithInstances() {
90 return typeClassInstances.keySet();
93 public boolean addValue(SCLValue value) {
94 return values.put(value.getName().name, value) != null;
97 public SCLValue addValue(String name, Constant constant) {
98 SCLValue value = new SCLValue(Name.create(moduleName, name), constant);
103 public void addRelation(String name, SCLRelation relation) {
104 relations.put(name, relation);
107 public void addEntityType(String name, SCLEntityType entityType) {
108 entityTypes.put(name, entityType);
111 public void addDependency(ImportDeclaration module) {
112 if(!dependencies.contains(module))
113 dependencies.add(module);
116 public Collection<SCLValue> getValues() {
117 return values.values();
120 public Collection<TransformationRule> getRules() {
121 return rules.values();
124 public Collection<MappingRelation> getMappingRelations() {
125 return mappingRelations.values();
129 public String getName() {
134 public SCLValue getValue(String name) {
135 return values.get(name);
139 public SCLRelation getRelation(String name) {
140 return relations.get(name);
144 public MappingRelation getMappingRelation(String name) {
145 return mappingRelations.get(name);
149 public TransformationRule getRule(String name) {
150 return rules.get(name);
154 public SCLEntityType getEntityType(String name) {
155 return entityTypes.get(name);
158 public TypeClass getTypeClass(String name) {
159 return typeClasses.get(name);
163 public Collection<TypeClassInstance> getInstances(TCon typeClass) {
164 Collection<TypeClassInstance> result = typeClassInstances.get(typeClass);
166 return Collections.emptyList();
172 public TypeConstructor getTypeConstructor(String name) {
173 return typeConstructors.get(name);
177 public EffectConstructor getEffectConstructor(String name) {
178 return effectConstructors.get(name);
181 public Collection<TypeClass> getTypeClasses() {
182 return typeClasses.values();
185 public THashMap<TCon, ArrayList<TypeClassInstance>> getTypeInstances() {
186 return typeClassInstances;
190 public List<ImportDeclaration> getDependencies() {
194 public void setDocumentation(Documentation documentation) {
195 this.documentation = documentation;
198 public Documentation getDocumentation() {
199 return documentation;
203 public TypeAlias getTypeAlias(String name) {
204 return typeAliases.get(name);
207 public void setClasses(Map<String, byte[]> classes) {
208 this.classes = classes;
212 public byte[] getClass(String name) {
213 return classes.get(name);
216 public void setModuleInitializer(ModuleInitializer moduleInitializer) {
217 this.moduleInitializer = moduleInitializer;
221 public ModuleInitializer getModuleInitializer() {
222 return moduleInitializer;
226 public String toString() {
231 public void findValuesForPrefix(final String prefix, final NamespaceFilter filter,
232 final TObjectProcedure<SCLValue> proc) {
233 this.values.forEachEntry(new TObjectObjectProcedure<String,SCLValue>() {
235 public boolean execute(String name, SCLValue value) {
236 if(value.isPrivate())
238 String lowerPrefix = prefix.toLowerCase();
239 String lowerName = name.toLowerCase();
240 if(lowerName.startsWith(lowerPrefix) && filter.isValueIncluded(name))
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);
257 public Collection<SCLRelation> getRelations() {
258 return relations.values();
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);
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);
275 public void setBranchPoints(THashMap<String, BranchPoint[]> branchPoints) {
276 this.branchPoints = branchPoints;
280 public THashMap<String, BranchPoint[]> getBranchPoints() {
285 public void dispose() {