]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/NamespaceOfModule.java
migrated to svn revision 33108
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / compilation / NamespaceOfModule.java
1 package org.simantics.scl.compiler.compilation;
2
3 import java.util.Arrays;
4 import java.util.function.Consumer;
5
6 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
7 import org.simantics.scl.compiler.elaboration.modules.TypeAlias;
8 import org.simantics.scl.compiler.elaboration.modules.TypeClass;
9 import org.simantics.scl.compiler.elaboration.modules.TypeConstructor;
10 import org.simantics.scl.compiler.elaboration.relations.SCLEntityType;
11 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
12 import org.simantics.scl.compiler.elaboration.rules.MappingRelation;
13 import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
14 import org.simantics.scl.compiler.environment.AmbiguousNameException;
15 import org.simantics.scl.compiler.environment.Namespace;
16 import org.simantics.scl.compiler.environment.filter.AcceptAllNamespaceFilter;
17 import org.simantics.scl.compiler.environment.filter.NamespaceFilter;
18 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
19 import org.simantics.scl.compiler.module.Module;
20 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
21 import org.simantics.scl.compiler.types.TCon;
22
23 import gnu.trove.procedure.TObjectProcedure;
24
25 public class NamespaceOfModule implements Namespace {
26     private final Namespace base;
27     private final Module module;
28     
29     public NamespaceOfModule(Namespace base, Module module) {
30         this.base = base;
31         this.module = module;
32     }
33
34     @Override
35     public Namespace getNamespace(String name) {
36         return base.getNamespace(name);
37     }
38
39     @Override
40     public SCLValue getValue(String name) throws AmbiguousNameException {
41         SCLValue value = module.getValue(name);
42         if(SCLCompilerConfiguration.ALLOW_OVERLOADING) {
43             SCLValue value2;
44             try {
45                 value2 = base.getValue(name);
46             } catch(AmbiguousNameException e) {
47                 if(value != null) {
48                     String[] conflictingModules = Arrays.copyOf(e.conflictingModules, e.conflictingModules.length+1);
49                     conflictingModules[e.conflictingModules.length] = module.getName();
50                     throw new AmbiguousNameException(Arrays.asList(conflictingModules), e.name);
51                 }
52                 else
53                     throw e;
54             }
55             if(value == null)
56                 return value2;
57             if(value2 == null)
58                 return value;
59             throw new AmbiguousNameException(Arrays.asList(value.getName().module, value2.getName().module), value.getName().name);
60         }
61         else {
62         if(value != null)
63             return value;
64         return base.getValue(name);
65         }
66     }
67
68     @Override
69     public SCLRelation getRelation(String name) throws AmbiguousNameException {
70         SCLRelation relation = module.getRelation(name);
71         if(relation != null)
72             return relation;
73         return base.getRelation(name);
74     }
75
76     @Override
77     public SCLEntityType getEntityType(String name)
78             throws AmbiguousNameException {
79         SCLEntityType entityType = module.getEntityType(name);
80         if(entityType != null)
81             return entityType;
82         return base.getEntityType(name);
83     }
84
85     @Override
86     public TypeConstructor getTypeConstructor(String name)
87             throws AmbiguousNameException {
88         TypeConstructor typeConstructor = module.getTypeConstructor(name);
89         if(typeConstructor != null)
90             return typeConstructor;
91         return base.getTypeConstructor(name);
92     }
93
94     @Override
95     public EffectConstructor getEffectConstructor(String name)
96             throws AmbiguousNameException {
97         EffectConstructor effectConstructor = module.getEffectConstructor(name);
98         if(effectConstructor != null)
99             return effectConstructor;
100         return base.getEffectConstructor(name);
101     }
102
103     @Override
104     public TypeClass getTypeClass(String name) throws AmbiguousNameException {
105         TypeClass typeClass = module.getTypeClass(name);
106         if(typeClass != null)
107             return typeClass;
108         return base.getTypeClass(name);
109     }
110
111     @Override
112     public TypeAlias getTypeAlias(String name) throws AmbiguousNameException {
113         TypeAlias typeAlias = module.getTypeAlias(name);
114         if(typeAlias != null)
115             return typeAlias;
116         return base.getTypeAlias(name);
117     }
118     
119     @Override
120     public MappingRelation getMappingRelation(String name)
121             throws AmbiguousNameException {
122         MappingRelation mappingRelation = module.getMappingRelation(name);
123         if(mappingRelation != null)
124             return mappingRelation;
125         return base.getMappingRelation(name);
126     }
127     
128     @Override
129     public TransformationRule getRule(String name) throws AmbiguousNameException {
130         TransformationRule rule = module.getRule(name);
131         if(rule != null)
132             return rule;
133         return base.getRule(name);
134     }
135
136     @Override
137     public void findValuesForPrefix(String prefix, NamespaceFilter filter, TObjectProcedure<SCLValue> proc) {
138         base.findValuesForPrefix(prefix, filter, proc);
139         module.findValuesForPrefix(prefix, AcceptAllNamespaceFilter.INSTANCE, proc);
140     }
141
142     @Override
143     public void findTypesForPrefix(String prefix, NamespaceFilter filter, Consumer<TCon> consumer) {
144         base.findTypesForPrefix(prefix, filter, consumer);
145         module.findTypesForPrefix(prefix, AcceptAllNamespaceFilter.INSTANCE, consumer);
146     }
147     
148 }