]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/ConcreteModule.java
Showing compilation warnings in SCL issue view and editors
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / module / ConcreteModule.java
1 package org.simantics.scl.compiler.module;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.function.Consumer;
9
10 import org.simantics.scl.compiler.common.names.Name;
11 import org.simantics.scl.compiler.constants.Constant;
12 import org.simantics.scl.compiler.elaboration.modules.Documentation;
13 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
14 import org.simantics.scl.compiler.elaboration.modules.TypeClass;
15 import org.simantics.scl.compiler.elaboration.modules.TypeClassInstance;
16 import org.simantics.scl.compiler.elaboration.modules.TypeDescriptor;
17 import org.simantics.scl.compiler.elaboration.relations.SCLEntityType;
18 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
19 import org.simantics.scl.compiler.elaboration.rules.MappingRelation;
20 import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
21 import org.simantics.scl.compiler.environment.filter.NamespaceFilter;
22 import org.simantics.scl.compiler.errors.CompilationError;
23 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
24 import org.simantics.scl.compiler.top.ModuleInitializer;
25 import org.simantics.scl.compiler.types.TCon;
26 import org.simantics.scl.runtime.profiling.BranchPoint;
27
28 import gnu.trove.map.hash.THashMap;
29 import gnu.trove.procedure.TObjectObjectProcedure;
30 import gnu.trove.procedure.TObjectProcedure;
31
32 public class ConcreteModule implements Module {
33     String moduleName;
34     THashMap<String, TypeDescriptor> typeDescriptors = new THashMap<String, TypeDescriptor>();
35     THashMap<String, EffectConstructor> effectConstructors = new THashMap<String, EffectConstructor>();
36     THashMap<String, TypeClass> typeClasses = new THashMap<String, TypeClass>();
37     THashMap<TCon, ArrayList<TypeClassInstance>> typeClassInstances = new THashMap<TCon, ArrayList<TypeClassInstance>>();
38     THashMap<String, SCLValue> values = new THashMap<String, SCLValue>();
39     THashMap<String, SCLRelation> relations = new THashMap<String, SCLRelation>();
40     THashMap<String, SCLEntityType> entityTypes = new THashMap<String, SCLEntityType>();
41     THashMap<String, TransformationRule> rules = new THashMap<String, TransformationRule>();
42     THashMap<String, MappingRelation> mappingRelations = new THashMap<String, MappingRelation>();
43     ArrayList<ImportDeclaration> dependencies = new ArrayList<ImportDeclaration>();
44     THashMap<String, BranchPoint[]> branchPoints;
45     CompilationError[] warnings = CompilationError.EMPTY_ARRAY;
46     
47     Map<String, byte[]> classes = Collections.emptyMap();
48     ModuleInitializer moduleInitializer;
49
50     protected Documentation documentation;
51
52     public ConcreteModule(String moduleName) {
53         this.moduleName = moduleName;
54     }
55
56     public boolean addTypeDescriptor(String name, TypeDescriptor typeConstructor) {
57         return typeDescriptors.put(name, typeConstructor) != null;
58     }
59
60     public boolean addEffectConstructor(String name, EffectConstructor effectConstructor) {
61         return effectConstructors.put(name, effectConstructor) != null;
62     }
63
64     public boolean addTypeClass(String name, TypeClass typeClass) {
65         return typeClasses.put(name, typeClass) != null;
66     }
67
68     public void addTypeClassInstance(TCon typeClass, TypeClassInstance typeClassInstance) {
69         ArrayList<TypeClassInstance> instances = typeClassInstances.get(typeClass);
70         if(instances == null) {
71             instances = new ArrayList<TypeClassInstance>();
72             typeClassInstances.put(typeClass, instances);
73         }
74         instances.add(typeClassInstance);
75     }
76     
77     public boolean addRule(TransformationRule rule) {
78         return rules.put(rule.name.name, rule) != null;
79     }
80     
81     public boolean addMappingRelation(MappingRelation relation) {
82         return mappingRelations.put(relation.name.name, relation) != null;
83     }
84
85     public Collection<TCon> getTypeClassesWithInstances() {
86         return typeClassInstances.keySet();
87     }
88
89     public boolean addValue(SCLValue value) {
90         return values.put(value.getName().name, value) != null;
91     }
92
93     public SCLValue addValue(String name, Constant constant) {
94         SCLValue value = new SCLValue(Name.create(moduleName, name), constant);
95         addValue(value);
96         return value;
97     }
98
99     public void addRelation(String name, SCLRelation relation) {
100         relations.put(name, relation);
101     }
102
103     public void addEntityType(String name, SCLEntityType entityType) {
104         entityTypes.put(name, entityType);
105     }
106
107     public void addDependency(ImportDeclaration module) {
108         if(!dependencies.contains(module))
109             dependencies.add(module);
110     }
111
112     public Collection<SCLValue> getValues() {
113         return values.values();
114     }
115
116     public Collection<TransformationRule> getRules() {
117         return rules.values();
118     }
119     
120     public Collection<MappingRelation> getMappingRelations() {
121         return mappingRelations.values();
122     }
123     
124     @Override
125     public String getName() {
126         return moduleName;
127     }
128
129     @Override
130     public SCLValue getValue(String name) {
131         return values.get(name);
132     }
133
134     @Override
135     public SCLRelation getRelation(String name) {
136         return relations.get(name);
137     }
138     
139     @Override
140     public MappingRelation getMappingRelation(String name) {
141         return mappingRelations.get(name);
142     }
143     
144     @Override
145     public TransformationRule getRule(String name) {
146         return rules.get(name);
147     }
148
149     @Override
150     public SCLEntityType getEntityType(String name) {
151         return entityTypes.get(name);
152     }
153
154     public TypeClass getTypeClass(String name) {
155         return typeClasses.get(name);
156     }
157
158     @Override
159     public Collection<TypeClassInstance> getInstances(TCon typeClass) {
160         Collection<TypeClassInstance> result = typeClassInstances.get(typeClass);
161         if(result == null)
162             return Collections.emptyList();
163         else
164             return result;
165     }
166
167     @Override
168     public TypeDescriptor getTypeDescriptor(String name) {
169         return typeDescriptors.get(name);
170     }
171
172     @Override
173     public EffectConstructor getEffectConstructor(String name) {
174         return effectConstructors.get(name);
175     }
176
177     public Collection<TypeClass> getTypeClasses() {
178         return typeClasses.values();
179     }
180
181     public THashMap<TCon, ArrayList<TypeClassInstance>> getTypeInstances() {
182         return typeClassInstances;
183     }
184
185     @Override
186     public List<ImportDeclaration> getDependencies() {
187         return dependencies;
188     }
189
190     public void setDocumentation(Documentation documentation) {
191         this.documentation = documentation;
192     }
193
194     public Documentation getDocumentation() {
195         return documentation;
196     }
197
198     public void setClasses(Map<String, byte[]> classes) {
199         this.classes = classes;
200     }
201
202     @Override
203     public byte[] getClass(String name) {
204         return classes.get(name);
205     }
206
207     public void setModuleInitializer(ModuleInitializer moduleInitializer) {
208         this.moduleInitializer = moduleInitializer;
209     }
210
211     @Override
212     public ModuleInitializer getModuleInitializer() {
213         return moduleInitializer;
214     }
215
216     @Override
217     public String toString() {
218         return moduleName;
219     }
220     
221     @Override
222     public void findValuesForPrefix(final String prefix, final NamespaceFilter filter,
223             final TObjectProcedure<SCLValue> proc) {
224         this.values.forEachEntry(new TObjectObjectProcedure<String,SCLValue>() {
225             @Override
226             public boolean execute(String name, SCLValue value) {
227                 if(value.isPrivate())
228                     return true;
229                 String lowerPrefix = prefix.toLowerCase();
230                 String lowerName = name.toLowerCase();
231                 if(lowerName.startsWith(lowerPrefix) && filter.isValueIncluded(name))
232                     proc.execute(value);
233                 return true;
234             }
235         });
236     }
237     
238     @Override
239     public void findValuesForPrefix(String prefix, NamespaceFilter filter, Consumer<SCLValue> consumer) {
240         values.values().forEach((Consumer<SCLValue>) (value) -> {
241             String lowerPrefix = prefix.toLowerCase();
242             String lowerName = value.getName().name.toLowerCase();
243             if(lowerName.startsWith(lowerPrefix) && filter.isValueIncluded(value.getName().name))
244                 consumer.accept(value);
245         });
246     }
247
248     public Collection<SCLRelation> getRelations() {
249         return relations.values();
250     }
251
252     @Override
253     public void findTypesForPrefix(String prefix, NamespaceFilter filter, Consumer<TCon> consumer) {
254         typeDescriptors.values().forEach(type -> {
255             TCon tcon = type.name;
256             if (tcon.name.toLowerCase().startsWith(prefix.toLowerCase()) && filter.isValueIncluded(tcon.name))
257                 consumer.accept(tcon);
258         });
259     }
260     
261     public void setBranchPoints(THashMap<String, BranchPoint[]> branchPoints) {
262         this.branchPoints = branchPoints;
263     }
264     
265     @Override
266     public THashMap<String, BranchPoint[]> getBranchPoints() {
267         return branchPoints;
268     }
269
270     @Override
271     public void dispose() {
272     }
273     
274     public void setWarnings(CompilationError[] warnings) {
275         this.warnings = warnings;
276     }
277     
278     public CompilationError[] getWarnings() {
279         return warnings;
280     }
281 }