1 package org.simantics.scl.compiler.environment;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
7 import org.simantics.scl.compiler.common.names.Name;
8 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
9 import org.simantics.scl.compiler.elaboration.modules.TypeClass;
10 import org.simantics.scl.compiler.elaboration.modules.TypeClassInstance;
11 import org.simantics.scl.compiler.elaboration.modules.TypeDescriptor;
12 import org.simantics.scl.compiler.elaboration.relations.SCLEntityType;
13 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
14 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
15 import org.simantics.scl.compiler.module.Module;
16 import org.simantics.scl.compiler.types.TCon;
18 public abstract class AbstractEnvironment implements Environment {
19 protected abstract Module getModule(String name);
20 protected abstract Collection<Module> getModules();
23 public SCLValue getValue(Name name) {
24 Module module = getModule(name.module);
27 return module.getValue(name.name);
31 public SCLRelation getRelation(Name name) {
32 Module module = getModule(name.module);
35 return module.getRelation(name.name);
39 public SCLEntityType getEntityType(Name name) {
40 Module module = getModule(name.module);
43 return module.getEntityType(name.name);
47 public TypeDescriptor getTypeDescriptor(TCon type) {
48 Module module = getModule(type.module);
51 return module.getTypeDescriptor(type.name);
55 public EffectConstructor getEffectConstructor(TCon type) {
56 Module module = getModule(type.module);
59 return module.getEffectConstructor(type.name);
63 public TypeClass getTypeClass(TCon type) {
64 Module module = getModule(type.module);
67 return module.getTypeClass(type.name);
71 public Collection<TypeClassInstance> getInstances(TCon typeClass) {
72 Collection<TypeClassInstance> result = Collections.emptyList();
73 int nonemptySubresults = 0;
74 for(Module module : getModules()) {
75 Collection<TypeClassInstance> instances = module.getInstances(typeClass);
76 if(!instances.isEmpty()) {
77 switch(nonemptySubresults) {
82 ArrayList<TypeClassInstance> temp =
83 new ArrayList<TypeClassInstance>(
84 ((result.size() + instances.size())*3) / 2);
89 result.addAll(instances);