]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/EnvironmentOfModule.java
(refs #7250) Merging master, minor CHR bugfixes
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / compilation / EnvironmentOfModule.java
1 package org.simantics.scl.compiler.compilation;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6
7 import org.simantics.scl.compiler.common.names.Name;
8 import org.simantics.scl.compiler.constants.Constant;
9 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
10 import org.simantics.scl.compiler.elaboration.modules.TypeClass;
11 import org.simantics.scl.compiler.elaboration.modules.TypeClassInstance;
12 import org.simantics.scl.compiler.elaboration.modules.TypeDescriptor;
13 import org.simantics.scl.compiler.elaboration.relations.SCLEntityType;
14 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
15 import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
16 import org.simantics.scl.compiler.environment.Environment;
17 import org.simantics.scl.compiler.environment.Namespace;
18 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
19 import org.simantics.scl.compiler.module.Module;
20 import org.simantics.scl.compiler.types.TCon;
21
22 public class EnvironmentOfModule implements Environment {
23     Environment base;
24     Module module;
25     Namespace localNamespace;
26     
27     public EnvironmentOfModule(Environment base, Module module) {
28         this.base = base;
29         this.module = module;
30         this.localNamespace = new NamespaceOfModule(base.getLocalNamespace(), module);
31     }
32
33     @Override
34     public Namespace getLocalNamespace() {
35         return localNamespace;
36     }
37     
38     @Override
39     public SCLValue getValue(Name name) {
40         if(name.module.equals(module.getName()))
41             return module.getValue(name.name);
42         else
43             return base.getValue(name);
44     }
45     
46     @Override
47     public List<Constant> getFieldAccessors(String name) {
48         List<Constant> r1 = base.getFieldAccessors(name);
49         List<Constant> r2 = module.getFieldAccessors(name);
50         if(r1 == null)
51             return r2;
52         if(r2 == null)
53             return r1;
54         ArrayList<Constant> result = new ArrayList<Constant>(r1.size() + r2.size());
55         result.addAll(r1);
56         result.addAll(r2);
57         return result;
58     }
59     
60     @Override
61     public SCLRelation getRelation(Name name) {
62         if(name.module.equals(module.getName()))
63             return module.getRelation(name.name);
64         else
65             return base.getRelation(name);
66     }
67     @Override
68     public SCLEntityType getEntityType(Name name) {
69         if(name.module.equals(module.getName()))
70             return module.getEntityType(name.name);
71         else
72             return base.getEntityType(name);
73     }
74     @Override
75     public TypeDescriptor getTypeDescriptor(TCon type) {
76         if(type.module.equals(module.getName()))
77             return module.getTypeDescriptor(type.name);
78         else
79             return base.getTypeDescriptor(type);
80     }
81     @Override
82     public EffectConstructor getEffectConstructor(TCon type) {
83         if(type.module.equals(module.getName()))
84             return module.getEffectConstructor(type.name);
85         else
86             return base.getEffectConstructor(type);
87     }
88     @Override
89     public TypeClass getTypeClass(TCon type) {
90         if(type.module.equals(module.getName()))
91             return module.getTypeClass(type.name);
92         else
93             return base.getTypeClass(type);
94     }
95     @Override
96     public Collection<TypeClassInstance> getInstances(TCon typeClass) {
97         Collection<TypeClassInstance> inst1 = module.getInstances(typeClass);
98         Collection<TypeClassInstance> inst2 = base.getInstances(typeClass);
99         if(inst1.isEmpty())
100             return inst2;
101         if(inst2.isEmpty())
102             return inst1;
103         ArrayList<TypeClassInstance> union = new ArrayList<TypeClassInstance>(inst1.size() + inst2.size());
104         union.addAll(inst1);
105         union.addAll(inst2);
106         return union;
107     }
108     
109     @Override
110     public void collectRules(Collection<TransformationRule> rules) {
111         base.collectRules(rules);
112         rules.addAll(module.getRules());
113     }
114 }