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.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;
27 import gnu.trove.map.hash.THashMap;
28 import gnu.trove.procedure.TObjectObjectProcedure;
29 import gnu.trove.procedure.TObjectProcedure;
31 public class ConcreteModule implements Module {
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;
45 Map<String, byte[]> classes = Collections.emptyMap();
46 ModuleInitializer moduleInitializer;
48 protected Documentation documentation;
50 public ConcreteModule(String moduleName) {
51 this.moduleName = moduleName;
54 public boolean addTypeDescriptor(String name, TypeDescriptor typeConstructor) {
55 return typeDescriptors.put(name, typeConstructor) != null;
58 public boolean addEffectConstructor(String name, EffectConstructor effectConstructor) {
59 return effectConstructors.put(name, effectConstructor) != null;
62 public boolean addTypeClass(String name, TypeClass typeClass) {
63 return typeClasses.put(name, typeClass) != null;
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);
72 instances.add(typeClassInstance);
75 public boolean addRule(TransformationRule rule) {
76 return rules.put(rule.name.name, rule) != null;
79 public boolean addMappingRelation(MappingRelation relation) {
80 return mappingRelations.put(relation.name.name, relation) != null;
83 public Collection<TCon> getTypeClassesWithInstances() {
84 return typeClassInstances.keySet();
87 public boolean addValue(SCLValue value) {
88 return values.put(value.getName().name, value) != null;
91 public SCLValue addValue(String name, Constant constant) {
92 SCLValue value = new SCLValue(Name.create(moduleName, name), constant);
97 public void addRelation(String name, SCLRelation relation) {
98 relations.put(name, relation);
101 public void addEntityType(String name, SCLEntityType entityType) {
102 entityTypes.put(name, entityType);
105 public void addDependency(ImportDeclaration module) {
106 if(!dependencies.contains(module))
107 dependencies.add(module);
110 public Collection<SCLValue> getValues() {
111 return values.values();
114 public Collection<TransformationRule> getRules() {
115 return rules.values();
118 public Collection<MappingRelation> getMappingRelations() {
119 return mappingRelations.values();
123 public String getName() {
128 public SCLValue getValue(String name) {
129 return values.get(name);
133 public SCLRelation getRelation(String name) {
134 return relations.get(name);
138 public MappingRelation getMappingRelation(String name) {
139 return mappingRelations.get(name);
143 public TransformationRule getRule(String name) {
144 return rules.get(name);
148 public SCLEntityType getEntityType(String name) {
149 return entityTypes.get(name);
152 public TypeClass getTypeClass(String name) {
153 return typeClasses.get(name);
157 public Collection<TypeClassInstance> getInstances(TCon typeClass) {
158 Collection<TypeClassInstance> result = typeClassInstances.get(typeClass);
160 return Collections.emptyList();
166 public TypeDescriptor getTypeDescriptor(String name) {
167 return typeDescriptors.get(name);
171 public EffectConstructor getEffectConstructor(String name) {
172 return effectConstructors.get(name);
175 public Collection<TypeClass> getTypeClasses() {
176 return typeClasses.values();
179 public THashMap<TCon, ArrayList<TypeClassInstance>> getTypeInstances() {
180 return typeClassInstances;
184 public List<ImportDeclaration> getDependencies() {
188 public void setDocumentation(Documentation documentation) {
189 this.documentation = documentation;
192 public Documentation getDocumentation() {
193 return documentation;
196 public void setClasses(Map<String, byte[]> classes) {
197 this.classes = classes;
201 public byte[] getClass(String name) {
202 return classes.get(name);
205 public void setModuleInitializer(ModuleInitializer moduleInitializer) {
206 this.moduleInitializer = moduleInitializer;
210 public ModuleInitializer getModuleInitializer() {
211 return moduleInitializer;
215 public String toString() {
220 public void findValuesForPrefix(final String prefix, final NamespaceFilter filter,
221 final TObjectProcedure<SCLValue> proc) {
222 this.values.forEachEntry(new TObjectObjectProcedure<String,SCLValue>() {
224 public boolean execute(String name, SCLValue value) {
225 if(value.isPrivate())
227 String lowerPrefix = prefix.toLowerCase();
228 String lowerName = name.toLowerCase();
229 if(lowerName.startsWith(lowerPrefix) && filter.isValueIncluded(name))
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);
246 public Collection<SCLRelation> getRelations() {
247 return relations.values();
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);
259 public void setBranchPoints(THashMap<String, BranchPoint[]> branchPoints) {
260 this.branchPoints = branchPoints;
264 public THashMap<String, BranchPoint[]> getBranchPoints() {
269 public void dispose() {