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.chr.CHRRuleset;
13 import org.simantics.scl.compiler.elaboration.modules.Documentation;
14 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
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.TypeDescriptor;
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.errors.CompilationError;
24 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
25 import org.simantics.scl.compiler.top.ModuleInitializer;
26 import org.simantics.scl.compiler.types.TCon;
27 import org.simantics.scl.runtime.profiling.BranchPoint;
29 import gnu.trove.map.hash.THashMap;
30 import gnu.trove.procedure.TObjectObjectProcedure;
31 import gnu.trove.procedure.TObjectProcedure;
33 public class ConcreteModule implements Module {
35 String defaultLocalName;
36 THashMap<String, TypeDescriptor> typeDescriptors = new THashMap<String, TypeDescriptor>();
37 THashMap<String, EffectConstructor> effectConstructors = new THashMap<String, EffectConstructor>();
38 THashMap<String, TypeClass> typeClasses = new THashMap<String, TypeClass>();
39 THashMap<TCon, ArrayList<TypeClassInstance>> typeClassInstances = new THashMap<TCon, ArrayList<TypeClassInstance>>();
40 THashMap<String, SCLValue> values = new THashMap<String, SCLValue>();
41 THashMap<String, List<Constant>> fieldAccessors = new THashMap<String, List<Constant>>();
42 THashMap<String, SCLRelation> relations = new THashMap<String, SCLRelation>(2);
43 THashMap<String, SCLEntityType> entityTypes = new THashMap<String, SCLEntityType>(2);
44 THashMap<String, TransformationRule> rules = new THashMap<String, TransformationRule>(2);
45 THashMap<String, MappingRelation> mappingRelations = new THashMap<String, MappingRelation>(2);
46 THashMap<String, CHRRuleset> rulesets = new THashMap<String, CHRRuleset>(2);
47 ArrayList<ImportDeclaration> dependencies = new ArrayList<ImportDeclaration>();
48 THashMap<String, BranchPoint[]> branchPoints;
49 CompilationError[] warnings = CompilationError.EMPTY_ARRAY;
51 Map<String, byte[]> classes = Collections.emptyMap();
52 ClassLoader parentClassLoader;
53 ModuleInitializer moduleInitializer;
55 protected Documentation documentation;
57 public ConcreteModule(String moduleName) {
58 this.moduleName = moduleName;
62 public String getDefaultLocalName() {
63 return defaultLocalName;
66 public void setDefaultLocalName(String defaultLocalName) {
67 this.defaultLocalName = defaultLocalName;
70 public boolean addTypeDescriptor(String name, TypeDescriptor typeConstructor) {
71 return typeDescriptors.put(name, typeConstructor) != null;
74 public boolean addEffectConstructor(String name, EffectConstructor effectConstructor) {
75 return effectConstructors.put(name, effectConstructor) != null;
78 public boolean addTypeClass(String name, TypeClass typeClass) {
79 return typeClasses.put(name, typeClass) != null;
82 public void addTypeClassInstance(TCon typeClass, TypeClassInstance typeClassInstance) {
83 ArrayList<TypeClassInstance> instances = typeClassInstances.get(typeClass);
84 if(instances == null) {
85 instances = new ArrayList<TypeClassInstance>();
86 typeClassInstances.put(typeClass, instances);
88 instances.add(typeClassInstance);
91 public boolean addRule(TransformationRule rule) {
92 return rules.put(rule.name.name, rule) != null;
95 public boolean addMappingRelation(MappingRelation relation) {
96 return mappingRelations.put(relation.name.name, relation) != null;
99 public Collection<TCon> getTypeClassesWithInstances() {
100 return typeClassInstances.keySet();
103 public boolean addValue(SCLValue value) {
104 return values.put(value.getName().name, value) != null;
107 public SCLValue addValue(String name, Constant constant) {
108 SCLValue value = new SCLValue(Name.create(moduleName, name), constant);
114 public List<Constant> getFieldAccessors(String name) {
115 return fieldAccessors.get(name);
118 public void addRelation(String name, SCLRelation relation) {
119 relations.put(name, relation);
122 public void addEntityType(String name, SCLEntityType entityType) {
123 entityTypes.put(name, entityType);
126 public void addDependency(ImportDeclaration module) {
127 if(!dependencies.contains(module))
128 dependencies.add(module);
131 public Collection<SCLValue> getValues() {
132 return values.values();
135 public Collection<TransformationRule> getRules() {
136 return rules.values();
139 public Collection<MappingRelation> getMappingRelations() {
140 return mappingRelations.values();
144 public String getName() {
149 public SCLValue getValue(String name) {
150 return values.get(name);
154 public SCLRelation getRelation(String name) {
155 return relations.get(name);
159 public MappingRelation getMappingRelation(String name) {
160 return mappingRelations.get(name);
164 public TransformationRule getRule(String name) {
165 return rules.get(name);
169 public SCLEntityType getEntityType(String name) {
170 return entityTypes.get(name);
173 public TypeClass getTypeClass(String name) {
174 return typeClasses.get(name);
178 public Collection<TypeClassInstance> getInstances(TCon typeClass) {
179 Collection<TypeClassInstance> result = typeClassInstances.get(typeClass);
181 return Collections.emptyList();
187 public TypeDescriptor getTypeDescriptor(String name) {
188 return typeDescriptors.get(name);
192 public EffectConstructor getEffectConstructor(String name) {
193 return effectConstructors.get(name);
197 public CHRRuleset getRuleset(String name) {
198 return rulesets.get(name);
201 public Collection<TypeClass> getTypeClasses() {
202 return typeClasses.values();
205 public THashMap<TCon, ArrayList<TypeClassInstance>> getTypeInstances() {
206 return typeClassInstances;
210 public List<ImportDeclaration> getDependencies() {
214 public void setDocumentation(Documentation documentation) {
215 this.documentation = documentation;
218 public Documentation getDocumentation() {
219 return documentation;
222 public void setClasses(Map<String, byte[]> classes) {
223 this.classes = classes;
227 public byte[] getClass(String name) {
228 return classes.get(name);
231 public void setModuleInitializer(ModuleInitializer moduleInitializer) {
232 this.moduleInitializer = moduleInitializer;
236 public ModuleInitializer getModuleInitializer() {
237 return moduleInitializer;
241 public String toString() {
246 public void findValuesForPrefix(final String prefix, final NamespaceFilter filter,
247 final TObjectProcedure<SCLValue> proc) {
248 this.values.forEachEntry(new TObjectObjectProcedure<String,SCLValue>() {
250 public boolean execute(String name, SCLValue value) {
251 if(value.isPrivate())
253 String lowerPrefix = prefix.toLowerCase();
254 String lowerName = name.toLowerCase();
255 if(lowerName.startsWith(lowerPrefix) && filter.isValueIncluded(name))
263 public void findValuesForPrefix(String prefix, NamespaceFilter filter, Consumer<SCLValue> consumer) {
264 values.values().forEach((Consumer<SCLValue>) (value) -> {
265 String lowerPrefix = prefix.toLowerCase();
266 String lowerName = value.getName().name.toLowerCase();
267 if(lowerName.startsWith(lowerPrefix) && filter.isValueIncluded(value.getName().name))
268 consumer.accept(value);
272 public Collection<SCLRelation> getRelations() {
273 return relations.values();
277 public void findTypesForPrefix(String prefix, NamespaceFilter filter, Consumer<TCon> consumer) {
278 typeDescriptors.values().forEach(type -> {
279 TCon tcon = type.name;
280 if (tcon.name.toLowerCase().startsWith(prefix.toLowerCase()) && filter.isValueIncluded(tcon.name))
281 consumer.accept(tcon);
285 public void setBranchPoints(THashMap<String, BranchPoint[]> branchPoints) {
286 this.branchPoints = branchPoints;
290 public THashMap<String, BranchPoint[]> getBranchPoints() {
295 public void dispose() {
298 public void setWarnings(CompilationError[] warnings) {
299 this.warnings = warnings;
302 public CompilationError[] getWarnings() {
307 public ClassLoader getParentClassLoader() {
308 return parentClassLoader;
311 public void setParentClassLoader(ClassLoader parentClassLoader) {
312 if(parentClassLoader == null)
313 throw new NullPointerException();
314 this.parentClassLoader = parentClassLoader;
317 public void addFieldAccessor(String name, Constant accessor) {
318 List<Constant> list = fieldAccessors.get(name);
320 list = new ArrayList<Constant>(2);
321 fieldAccessors.put(name, list);
326 public void addRuleset(String name, CHRRuleset ruleset) {
327 rulesets.put(name, ruleset);