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 String defaultLocalName;
35 THashMap<String, TypeDescriptor> typeDescriptors = new THashMap<String, TypeDescriptor>();
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 CompilationError[] warnings = CompilationError.EMPTY_ARRAY;
48 Map<String, byte[]> classes = Collections.emptyMap();
49 ClassLoader parentClassLoader;
50 ModuleInitializer moduleInitializer;
52 protected Documentation documentation;
54 public ConcreteModule(String moduleName) {
55 this.moduleName = moduleName;
59 public String getDefaultLocalName() {
60 return defaultLocalName;
63 public void setDefaultLocalName(String defaultLocalName) {
64 this.defaultLocalName = defaultLocalName;
67 public boolean addTypeDescriptor(String name, TypeDescriptor typeConstructor) {
68 return typeDescriptors.put(name, typeConstructor) != null;
71 public boolean addEffectConstructor(String name, EffectConstructor effectConstructor) {
72 return effectConstructors.put(name, effectConstructor) != null;
75 public boolean addTypeClass(String name, TypeClass typeClass) {
76 return typeClasses.put(name, typeClass) != null;
79 public void addTypeClassInstance(TCon typeClass, TypeClassInstance typeClassInstance) {
80 ArrayList<TypeClassInstance> instances = typeClassInstances.get(typeClass);
81 if(instances == null) {
82 instances = new ArrayList<TypeClassInstance>();
83 typeClassInstances.put(typeClass, instances);
85 instances.add(typeClassInstance);
88 public boolean addRule(TransformationRule rule) {
89 return rules.put(rule.name.name, rule) != null;
92 public boolean addMappingRelation(MappingRelation relation) {
93 return mappingRelations.put(relation.name.name, relation) != null;
96 public Collection<TCon> getTypeClassesWithInstances() {
97 return typeClassInstances.keySet();
100 public boolean addValue(SCLValue value) {
101 return values.put(value.getName().name, value) != null;
104 public SCLValue addValue(String name, Constant constant) {
105 SCLValue value = new SCLValue(Name.create(moduleName, name), constant);
110 public void addRelation(String name, SCLRelation relation) {
111 relations.put(name, relation);
114 public void addEntityType(String name, SCLEntityType entityType) {
115 entityTypes.put(name, entityType);
118 public void addDependency(ImportDeclaration module) {
119 if(!dependencies.contains(module))
120 dependencies.add(module);
123 public Collection<SCLValue> getValues() {
124 return values.values();
127 public Collection<TransformationRule> getRules() {
128 return rules.values();
131 public Collection<MappingRelation> getMappingRelations() {
132 return mappingRelations.values();
136 public String getName() {
141 public SCLValue getValue(String name) {
142 return values.get(name);
146 public SCLRelation getRelation(String name) {
147 return relations.get(name);
151 public MappingRelation getMappingRelation(String name) {
152 return mappingRelations.get(name);
156 public TransformationRule getRule(String name) {
157 return rules.get(name);
161 public SCLEntityType getEntityType(String name) {
162 return entityTypes.get(name);
165 public TypeClass getTypeClass(String name) {
166 return typeClasses.get(name);
170 public Collection<TypeClassInstance> getInstances(TCon typeClass) {
171 Collection<TypeClassInstance> result = typeClassInstances.get(typeClass);
173 return Collections.emptyList();
179 public TypeDescriptor getTypeDescriptor(String name) {
180 return typeDescriptors.get(name);
184 public EffectConstructor getEffectConstructor(String name) {
185 return effectConstructors.get(name);
188 public Collection<TypeClass> getTypeClasses() {
189 return typeClasses.values();
192 public THashMap<TCon, ArrayList<TypeClassInstance>> getTypeInstances() {
193 return typeClassInstances;
197 public List<ImportDeclaration> getDependencies() {
201 public void setDocumentation(Documentation documentation) {
202 this.documentation = documentation;
205 public Documentation getDocumentation() {
206 return documentation;
209 public void setClasses(Map<String, byte[]> classes) {
210 this.classes = classes;
214 public byte[] getClass(String name) {
215 return classes.get(name);
218 public void setModuleInitializer(ModuleInitializer moduleInitializer) {
219 this.moduleInitializer = moduleInitializer;
223 public ModuleInitializer getModuleInitializer() {
224 return moduleInitializer;
228 public String toString() {
233 public void findValuesForPrefix(final String prefix, final NamespaceFilter filter,
234 final TObjectProcedure<SCLValue> proc) {
235 this.values.forEachEntry(new TObjectObjectProcedure<String,SCLValue>() {
237 public boolean execute(String name, SCLValue value) {
238 if(value.isPrivate())
240 String lowerPrefix = prefix.toLowerCase();
241 String lowerName = name.toLowerCase();
242 if(lowerName.startsWith(lowerPrefix) && filter.isValueIncluded(name))
250 public void findValuesForPrefix(String prefix, NamespaceFilter filter, Consumer<SCLValue> consumer) {
251 values.values().forEach((Consumer<SCLValue>) (value) -> {
252 String lowerPrefix = prefix.toLowerCase();
253 String lowerName = value.getName().name.toLowerCase();
254 if(lowerName.startsWith(lowerPrefix) && filter.isValueIncluded(value.getName().name))
255 consumer.accept(value);
259 public Collection<SCLRelation> getRelations() {
260 return relations.values();
264 public void findTypesForPrefix(String prefix, NamespaceFilter filter, Consumer<TCon> consumer) {
265 typeDescriptors.values().forEach(type -> {
266 TCon tcon = type.name;
267 if (tcon.name.toLowerCase().startsWith(prefix.toLowerCase()) && filter.isValueIncluded(tcon.name))
268 consumer.accept(tcon);
272 public void setBranchPoints(THashMap<String, BranchPoint[]> branchPoints) {
273 this.branchPoints = branchPoints;
277 public THashMap<String, BranchPoint[]> getBranchPoints() {
282 public void dispose() {
285 public void setWarnings(CompilationError[] warnings) {
286 this.warnings = warnings;
289 public CompilationError[] getWarnings() {
294 public ClassLoader getParentClassLoader() {
295 return parentClassLoader;
298 public void setParentClassLoader(ClassLoader parentClassLoader) {
299 if(parentClassLoader == null)
300 throw new NullPointerException();
301 this.parentClassLoader = parentClassLoader;