]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/ConcreteModule.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / module / ConcreteModule.java
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/ConcreteModule.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/ConcreteModule.java
new file mode 100644 (file)
index 0000000..8edd9f7
--- /dev/null
@@ -0,0 +1,288 @@
+package org.simantics.scl.compiler.module;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Consumer;
+
+import org.simantics.scl.compiler.common.names.Name;
+import org.simantics.scl.compiler.constants.Constant;
+import org.simantics.scl.compiler.elaboration.modules.Documentation;
+import org.simantics.scl.compiler.elaboration.modules.SCLValue;
+import org.simantics.scl.compiler.elaboration.modules.TypeAlias;
+import org.simantics.scl.compiler.elaboration.modules.TypeClass;
+import org.simantics.scl.compiler.elaboration.modules.TypeClassInstance;
+import org.simantics.scl.compiler.elaboration.modules.TypeConstructor;
+import org.simantics.scl.compiler.elaboration.relations.SCLEntityType;
+import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
+import org.simantics.scl.compiler.elaboration.rules.MappingRelation;
+import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
+import org.simantics.scl.compiler.environment.filter.NamespaceFilter;
+import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
+import org.simantics.scl.compiler.top.ModuleInitializer;
+import org.simantics.scl.compiler.types.TCon;
+import org.simantics.scl.runtime.profiling.BranchPoint;
+
+import gnu.trove.map.hash.THashMap;
+import gnu.trove.procedure.TObjectObjectProcedure;
+import gnu.trove.procedure.TObjectProcedure;
+
+public class ConcreteModule implements Module {
+    String moduleName;
+    THashMap<String, TypeConstructor> typeConstructors = new THashMap<String, TypeConstructor>();
+    THashMap<String, TypeAlias> typeAliases = new THashMap<String, TypeAlias>();
+    THashMap<String, EffectConstructor> effectConstructors = new THashMap<String, EffectConstructor>();
+    THashMap<String, TypeClass> typeClasses = new THashMap<String, TypeClass>();
+    THashMap<TCon, ArrayList<TypeClassInstance>> typeClassInstances = new THashMap<TCon, ArrayList<TypeClassInstance>>();
+    THashMap<String, SCLValue> values = new THashMap<String, SCLValue>();
+    THashMap<String, SCLRelation> relations = new THashMap<String, SCLRelation>();
+    THashMap<String, SCLEntityType> entityTypes = new THashMap<String, SCLEntityType>();
+    THashMap<String, TransformationRule> rules = new THashMap<String, TransformationRule>();
+    THashMap<String, MappingRelation> mappingRelations = new THashMap<String, MappingRelation>();
+    ArrayList<ImportDeclaration> dependencies = new ArrayList<ImportDeclaration>();
+    THashMap<String, BranchPoint[]> branchPoints;
+
+    Map<String, byte[]> classes = Collections.emptyMap();
+    ModuleInitializer moduleInitializer;
+
+    protected Documentation documentation;
+
+    public ConcreteModule(String moduleName) {
+        this.moduleName = moduleName;
+    }
+
+    public boolean addTypeConstructor(String name, TypeConstructor typeConstructor) {
+        return typeConstructors.put(name, typeConstructor) != null;
+    }
+
+    public boolean addTypeAlias(String name, TypeAlias alias) {
+        return typeAliases.put(name, alias) != null;
+    }
+
+    public boolean addEffectConstructor(String name, EffectConstructor effectConstructor) {
+        return effectConstructors.put(name, effectConstructor) != null;
+    }
+
+    public boolean addTypeClass(String name, TypeClass typeClass) {
+        return typeClasses.put(name, typeClass) != null;
+    }
+
+    public void addTypeClassInstance(TCon typeClass, TypeClassInstance typeClassInstance) {
+        ArrayList<TypeClassInstance> instances = typeClassInstances.get(typeClass);
+        if(instances == null) {
+            instances = new ArrayList<TypeClassInstance>();
+            typeClassInstances.put(typeClass, instances);
+        }
+        instances.add(typeClassInstance);
+    }
+    
+    public boolean addRule(TransformationRule rule) {
+        return rules.put(rule.name.name, rule) != null;
+    }
+    
+    public boolean addMappingRelation(MappingRelation relation) {
+        return mappingRelations.put(relation.name.name, relation) != null;
+    }
+
+    public Collection<TCon> getTypeClassesWithInstances() {
+        return typeClassInstances.keySet();
+    }
+
+    public boolean addValue(SCLValue value) {
+        return values.put(value.getName().name, value) != null;
+    }
+
+    public SCLValue addValue(String name, Constant constant) {
+        SCLValue value = new SCLValue(Name.create(moduleName, name), constant);
+        addValue(value);
+        return value;
+    }
+
+    public void addRelation(String name, SCLRelation relation) {
+        relations.put(name, relation);
+    }
+
+    public void addEntityType(String name, SCLEntityType entityType) {
+        entityTypes.put(name, entityType);
+    }
+
+    public void addDependency(ImportDeclaration module) {
+        if(!dependencies.contains(module))
+            dependencies.add(module);
+    }
+
+    public Collection<SCLValue> getValues() {
+        return values.values();
+    }
+
+    public Collection<TransformationRule> getRules() {
+        return rules.values();
+    }
+    
+    public Collection<MappingRelation> getMappingRelations() {
+        return mappingRelations.values();
+    }
+    
+    @Override
+    public String getName() {
+        return moduleName;
+    }
+
+    @Override
+    public SCLValue getValue(String name) {
+        return values.get(name);
+    }
+
+    @Override
+    public SCLRelation getRelation(String name) {
+        return relations.get(name);
+    }
+    
+    @Override
+    public MappingRelation getMappingRelation(String name) {
+        return mappingRelations.get(name);
+    }
+    
+    @Override
+    public TransformationRule getRule(String name) {
+        return rules.get(name);
+    }
+
+    @Override
+    public SCLEntityType getEntityType(String name) {
+        return entityTypes.get(name);
+    }
+
+    public TypeClass getTypeClass(String name) {
+        return typeClasses.get(name);
+    }
+
+    @Override
+    public Collection<TypeClassInstance> getInstances(TCon typeClass) {
+        Collection<TypeClassInstance> result = typeClassInstances.get(typeClass);
+        if(result == null)
+            return Collections.emptyList();
+        else
+            return result;
+    }
+
+    @Override
+    public TypeConstructor getTypeConstructor(String name) {
+        return typeConstructors.get(name);
+    }
+
+    @Override
+    public EffectConstructor getEffectConstructor(String name) {
+        return effectConstructors.get(name);
+    }
+
+    public Collection<TypeClass> getTypeClasses() {
+        return typeClasses.values();
+    }
+
+    public THashMap<TCon, ArrayList<TypeClassInstance>> getTypeInstances() {
+        return typeClassInstances;
+    }
+
+    @Override
+    public List<ImportDeclaration> getDependencies() {
+        return dependencies;
+    }
+
+    public void setDocumentation(Documentation documentation) {
+        this.documentation = documentation;
+    }
+
+    public Documentation getDocumentation() {
+        return documentation;
+    }
+
+    @Override
+    public TypeAlias getTypeAlias(String name) {
+        return typeAliases.get(name);
+    }
+
+    public void setClasses(Map<String, byte[]> classes) {
+        this.classes = classes;
+    }
+
+    @Override
+    public byte[] getClass(String name) {
+        return classes.get(name);
+    }
+
+    public void setModuleInitializer(ModuleInitializer moduleInitializer) {
+        this.moduleInitializer = moduleInitializer;
+    }
+
+    @Override
+    public ModuleInitializer getModuleInitializer() {
+        return moduleInitializer;
+    }
+
+    @Override
+    public String toString() {
+        return moduleName;
+    }
+    
+    @Override
+    public void findValuesForPrefix(final String prefix, final NamespaceFilter filter,
+            final TObjectProcedure<SCLValue> proc) {
+        this.values.forEachEntry(new TObjectObjectProcedure<String,SCLValue>() {
+            @Override
+            public boolean execute(String name, SCLValue value) {
+                if(value.isPrivate())
+                    return true;
+                String lowerPrefix = prefix.toLowerCase();
+                String lowerName = name.toLowerCase();
+                if(lowerName.startsWith(lowerPrefix) && filter.isValueIncluded(name))
+                    proc.execute(value);
+                return true;
+            }
+        });
+    }
+    
+    @Override
+    public void findValuesForPrefix(String prefix, NamespaceFilter filter, Consumer<SCLValue> consumer) {
+        values.values().forEach((Consumer<SCLValue>) (value) -> {
+            String lowerPrefix = prefix.toLowerCase();
+            String lowerName = value.getName().name.toLowerCase();
+            if(lowerName.startsWith(lowerPrefix) && filter.isValueIncluded(value.getName().name))
+                consumer.accept(value);
+        });
+    }
+
+    public Collection<SCLRelation> getRelations() {
+        return relations.values();
+    }
+
+    @Override
+    public void findTypesForPrefix(String prefix, NamespaceFilter filter, Consumer<TCon> consumer) {
+        typeConstructors.values().forEach(type -> {
+            TCon tcon = type.name;
+            if (tcon.name.toLowerCase().startsWith(prefix.toLowerCase()) && filter.isValueIncluded(tcon.name))
+                consumer.accept(tcon);
+        });
+        typeAliases.values().forEach(type -> {
+            TCon tcon = type.getCon();
+            if (tcon.name.toLowerCase().startsWith(prefix.toLowerCase()) && filter.isValueIncluded(tcon.name))
+                consumer.accept(tcon);
+        });
+    }
+    
+    public void setBranchPoints(THashMap<String, BranchPoint[]> branchPoints) {
+        this.branchPoints = branchPoints;
+    }
+    
+    @Override
+    public THashMap<String, BranchPoint[]> getBranchPoints() {
+        return branchPoints;
+    }
+
+    @Override
+    public void dispose() {
+        
+    }
+}