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