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.concurrent.SynchronousQueue;
9 import java.util.function.Consumer;
11 import org.simantics.scl.compiler.common.names.Name;
12 import org.simantics.scl.compiler.constants.Constant;
13 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
14 import org.simantics.scl.compiler.elaboration.modules.Documentation;
15 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
16 import org.simantics.scl.compiler.elaboration.modules.TypeClass;
17 import org.simantics.scl.compiler.elaboration.modules.TypeClassInstance;
18 import org.simantics.scl.compiler.elaboration.modules.TypeDescriptor;
19 import org.simantics.scl.compiler.elaboration.relations.SCLEntityType;
20 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
21 import org.simantics.scl.compiler.elaboration.rules.MappingRelation;
22 import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
23 import org.simantics.scl.compiler.environment.filter.NamespaceFilter;
24 import org.simantics.scl.compiler.errors.CompilationError;
25 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
26 import org.simantics.scl.compiler.top.ModuleInitializer;
27 import org.simantics.scl.compiler.types.TCon;
28 import org.simantics.scl.runtime.profiling.BranchPoint;
30 import gnu.trove.map.hash.THashMap;
31 import gnu.trove.procedure.TObjectObjectProcedure;
32 import gnu.trove.procedure.TObjectProcedure;
34 public class ConcreteModule implements Module {
36 String defaultLocalName;
38 THashMap<String, TypeDescriptor> typeDescriptors = new THashMap<String, TypeDescriptor>();
39 THashMap<String, EffectConstructor> effectConstructors = new THashMap<String, EffectConstructor>();
40 THashMap<String, TypeClass> typeClasses = new THashMap<String, TypeClass>();
41 THashMap<TCon, ArrayList<TypeClassInstance>> typeClassInstances = new THashMap<TCon, ArrayList<TypeClassInstance>>();
42 THashMap<String, SCLValue> values = new THashMap<String, SCLValue>();
43 THashMap<String, List<Constant>> fieldAccessors = new THashMap<String, List<Constant>>();
44 THashMap<String, SCLRelation> relations = new THashMap<String, SCLRelation>(2);
45 THashMap<String, SCLEntityType> entityTypes = new THashMap<String, SCLEntityType>(2);
46 THashMap<String, TransformationRule> rules = new THashMap<String, TransformationRule>(2);
47 THashMap<String, MappingRelation> mappingRelations = new THashMap<String, MappingRelation>(2);
48 THashMap<String, CHRRuleset> rulesets = new THashMap<String, CHRRuleset>(2);
49 ArrayList<ImportDeclaration> dependencies = new ArrayList<ImportDeclaration>();
50 THashMap<String, BranchPoint[]> branchPoints;
51 CompilationError[] warnings = CompilationError.EMPTY_ARRAY;
53 Map<String, byte[]> classes = Collections.emptyMap();
54 ClassLoader parentClassLoader;
55 ModuleInitializer moduleInitializer;
57 protected Documentation documentation;
59 public ConcreteModule(String moduleName) {
60 this.moduleName = moduleName;
64 public String getDefaultLocalName() {
65 return defaultLocalName;
68 public void setDefaultLocalName(String defaultLocalName) {
69 this.defaultLocalName = defaultLocalName;
72 public boolean addTypeDescriptor(String name, TypeDescriptor typeConstructor) {
73 return typeDescriptors.put(name, typeConstructor) != null;
76 public boolean addEffectConstructor(String name, EffectConstructor effectConstructor) {
77 return effectConstructors.put(name, effectConstructor) != null;
80 public boolean addTypeClass(String name, TypeClass typeClass) {
81 return typeClasses.put(name, typeClass) != null;
84 public void addTypeClassInstance(TCon typeClass, TypeClassInstance typeClassInstance) {
85 ArrayList<TypeClassInstance> instances = typeClassInstances.get(typeClass);
86 if(instances == null) {
87 instances = new ArrayList<TypeClassInstance>();
88 typeClassInstances.put(typeClass, instances);
90 instances.add(typeClassInstance);
93 public boolean addRule(TransformationRule rule) {
94 return rules.put(rule.name.name, rule) != null;
97 public boolean addMappingRelation(MappingRelation relation) {
98 return mappingRelations.put(relation.name.name, relation) != null;
101 public Collection<TCon> getTypeClassesWithInstances() {
102 return typeClassInstances.keySet();
105 public boolean addValue(SCLValue value) {
106 return values.put(value.getName().name, value) != null;
109 public SCLValue addValue(String name, Constant constant) {
110 SCLValue value = new SCLValue(Name.create(moduleName, name), constant);
116 public List<Constant> getFieldAccessors(String name) {
117 return fieldAccessors.get(name);
120 public void addRelation(String name, SCLRelation relation) {
121 relations.put(name, relation);
124 public void addEntityType(String name, SCLEntityType entityType) {
125 entityTypes.put(name, entityType);
128 public void addDependency(ImportDeclaration module) {
129 if(!dependencies.contains(module))
130 dependencies.add(module);
133 public Collection<SCLValue> getValues() {
134 return values.values();
137 public Collection<TransformationRule> getRules() {
138 return rules.values();
141 public Collection<MappingRelation> getMappingRelations() {
142 return mappingRelations.values();
146 public String getName() {
151 public SCLValue getValue(String name) {
152 return values.get(name);
156 public SCLRelation getRelation(String name) {
157 return relations.get(name);
161 public MappingRelation getMappingRelation(String name) {
162 return mappingRelations.get(name);
166 public TransformationRule getRule(String name) {
167 return rules.get(name);
171 public SCLEntityType getEntityType(String name) {
172 return entityTypes.get(name);
175 public TypeClass getTypeClass(String name) {
176 return typeClasses.get(name);
180 public Collection<TypeClassInstance> getInstances(TCon typeClass) {
181 Collection<TypeClassInstance> result = typeClassInstances.get(typeClass);
183 return Collections.emptyList();
189 public TypeDescriptor getTypeDescriptor(String name) {
190 return typeDescriptors.get(name);
194 public EffectConstructor getEffectConstructor(String name) {
195 return effectConstructors.get(name);
199 public CHRRuleset getRuleset(String name) {
200 return rulesets.get(name);
203 public Collection<TypeClass> getTypeClasses() {
204 return typeClasses.values();
207 public THashMap<TCon, ArrayList<TypeClassInstance>> getTypeInstances() {
208 return typeClassInstances;
212 public List<ImportDeclaration> getDependencies() {
216 public void setDocumentation(Documentation documentation) {
217 this.documentation = documentation;
220 public Documentation getDocumentation() {
221 return documentation;
224 public void setClasses(Map<String, byte[]> classes) {
225 this.classes = classes;
229 public byte[] getClass(String name) {
230 return classes.get(name);
233 public void setModuleInitializer(ModuleInitializer moduleInitializer) {
234 this.moduleInitializer = moduleInitializer;
238 public ModuleInitializer getModuleInitializer() {
239 return moduleInitializer;
243 public String toString() {
248 public void findValuesForPrefix(final String prefix, final NamespaceFilter filter,
249 final TObjectProcedure<SCLValue> proc) {
250 this.values.forEachEntry(new TObjectObjectProcedure<String,SCLValue>() {
252 public boolean execute(String name, SCLValue value) {
253 if(value.isPrivateOrDerived())
255 String lowerPrefix = prefix.toLowerCase();
256 String lowerName = name.toLowerCase();
257 if(lowerName.startsWith(lowerPrefix) && filter.isValueIncluded(name))
265 public void findValuesForPrefix(String prefix, NamespaceFilter filter, Consumer<SCLValue> consumer) {
266 values.values().forEach((Consumer<SCLValue>) (value) -> {
267 String lowerPrefix = prefix.toLowerCase();
268 String lowerName = value.getName().name.toLowerCase();
269 if(lowerName.startsWith(lowerPrefix) && filter.isValueIncluded(value.getName().name))
270 consumer.accept(value);
274 public Collection<SCLRelation> getRelations() {
275 return relations.values();
279 public void findTypesForPrefix(String prefix, NamespaceFilter filter, Consumer<TCon> consumer) {
280 typeDescriptors.values().forEach(type -> {
281 TCon tcon = type.name;
282 if (tcon.name.toLowerCase().startsWith(prefix.toLowerCase()) && filter.isValueIncluded(tcon.name))
283 consumer.accept(tcon);
287 public void setBranchPoints(THashMap<String, BranchPoint[]> branchPoints) {
288 this.branchPoints = branchPoints;
292 public THashMap<String, BranchPoint[]> getBranchPoints() {
297 public void dispose() {
300 public void setWarnings(CompilationError[] warnings) {
301 this.warnings = warnings;
304 public CompilationError[] getWarnings() {
309 public ClassLoader getParentClassLoader() {
310 return parentClassLoader;
313 public void setParentClassLoader(ClassLoader parentClassLoader) {
314 if(parentClassLoader == null)
315 throw new NullPointerException();
316 this.parentClassLoader = parentClassLoader;
319 public void addFieldAccessor(String name, Constant accessor) {
320 List<Constant> list = fieldAccessors.get(name);
322 list = new ArrayList<Constant>(2);
323 fieldAccessors.put(name, list);
328 public void addRuleset(String name, CHRRuleset ruleset) {
329 rulesets.put(name, ruleset);
333 public String getDeprecation() {
337 public void setDeprecation(String deprecation) {
338 this.deprecation = deprecation;