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.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;
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, 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;
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 addTypeDescriptor(String name, TypeDescriptor typeConstructor) {
57 return typeDescriptors.put(name, typeConstructor) != null;
60 public boolean addEffectConstructor(String name, EffectConstructor effectConstructor) {
61 return effectConstructors.put(name, effectConstructor) != null;
64 public boolean addTypeClass(String name, TypeClass typeClass) {
65 return typeClasses.put(name, typeClass) != null;
68 public void addTypeClassInstance(TCon typeClass, TypeClassInstance typeClassInstance) {
69 ArrayList<TypeClassInstance> instances = typeClassInstances.get(typeClass);
70 if(instances == null) {
71 instances = new ArrayList<TypeClassInstance>();
72 typeClassInstances.put(typeClass, instances);
74 instances.add(typeClassInstance);
77 public boolean addRule(TransformationRule rule) {
78 return rules.put(rule.name.name, rule) != null;
81 public boolean addMappingRelation(MappingRelation relation) {
82 return mappingRelations.put(relation.name.name, relation) != null;
85 public Collection<TCon> getTypeClassesWithInstances() {
86 return typeClassInstances.keySet();
89 public boolean addValue(SCLValue value) {
90 return values.put(value.getName().name, value) != null;
93 public SCLValue addValue(String name, Constant constant) {
94 SCLValue value = new SCLValue(Name.create(moduleName, name), constant);
99 public void addRelation(String name, SCLRelation relation) {
100 relations.put(name, relation);
103 public void addEntityType(String name, SCLEntityType entityType) {
104 entityTypes.put(name, entityType);
107 public void addDependency(ImportDeclaration module) {
108 if(!dependencies.contains(module))
109 dependencies.add(module);
112 public Collection<SCLValue> getValues() {
113 return values.values();
116 public Collection<TransformationRule> getRules() {
117 return rules.values();
120 public Collection<MappingRelation> getMappingRelations() {
121 return mappingRelations.values();
125 public String getName() {
130 public SCLValue getValue(String name) {
131 return values.get(name);
135 public SCLRelation getRelation(String name) {
136 return relations.get(name);
140 public MappingRelation getMappingRelation(String name) {
141 return mappingRelations.get(name);
145 public TransformationRule getRule(String name) {
146 return rules.get(name);
150 public SCLEntityType getEntityType(String name) {
151 return entityTypes.get(name);
154 public TypeClass getTypeClass(String name) {
155 return typeClasses.get(name);
159 public Collection<TypeClassInstance> getInstances(TCon typeClass) {
160 Collection<TypeClassInstance> result = typeClassInstances.get(typeClass);
162 return Collections.emptyList();
168 public TypeDescriptor getTypeDescriptor(String name) {
169 return typeDescriptors.get(name);
173 public EffectConstructor getEffectConstructor(String name) {
174 return effectConstructors.get(name);
177 public Collection<TypeClass> getTypeClasses() {
178 return typeClasses.values();
181 public THashMap<TCon, ArrayList<TypeClassInstance>> getTypeInstances() {
182 return typeClassInstances;
186 public List<ImportDeclaration> getDependencies() {
190 public void setDocumentation(Documentation documentation) {
191 this.documentation = documentation;
194 public Documentation getDocumentation() {
195 return documentation;
198 public void setClasses(Map<String, byte[]> classes) {
199 this.classes = classes;
203 public byte[] getClass(String name) {
204 return classes.get(name);
207 public void setModuleInitializer(ModuleInitializer moduleInitializer) {
208 this.moduleInitializer = moduleInitializer;
212 public ModuleInitializer getModuleInitializer() {
213 return moduleInitializer;
217 public String toString() {
222 public void findValuesForPrefix(final String prefix, final NamespaceFilter filter,
223 final TObjectProcedure<SCLValue> proc) {
224 this.values.forEachEntry(new TObjectObjectProcedure<String,SCLValue>() {
226 public boolean execute(String name, SCLValue value) {
227 if(value.isPrivate())
229 String lowerPrefix = prefix.toLowerCase();
230 String lowerName = name.toLowerCase();
231 if(lowerName.startsWith(lowerPrefix) && filter.isValueIncluded(name))
239 public void findValuesForPrefix(String prefix, NamespaceFilter filter, Consumer<SCLValue> consumer) {
240 values.values().forEach((Consumer<SCLValue>) (value) -> {
241 String lowerPrefix = prefix.toLowerCase();
242 String lowerName = value.getName().name.toLowerCase();
243 if(lowerName.startsWith(lowerPrefix) && filter.isValueIncluded(value.getName().name))
244 consumer.accept(value);
248 public Collection<SCLRelation> getRelations() {
249 return relations.values();
253 public void findTypesForPrefix(String prefix, NamespaceFilter filter, Consumer<TCon> consumer) {
254 typeDescriptors.values().forEach(type -> {
255 TCon tcon = type.name;
256 if (tcon.name.toLowerCase().startsWith(prefix.toLowerCase()) && filter.isValueIncluded(tcon.name))
257 consumer.accept(tcon);
261 public void setBranchPoints(THashMap<String, BranchPoint[]> branchPoints) {
262 this.branchPoints = branchPoints;
266 public THashMap<String, BranchPoint[]> getBranchPoints() {
271 public void dispose() {
274 public void setWarnings(CompilationError[] warnings) {
275 this.warnings = warnings;
278 public CompilationError[] getWarnings() {