]> gerrit.simantics Code Review - simantics/platform.git/blob
63fee96ca7e8096e4454749a074052dac67c9188
[simantics/platform.git] /
1 package org.simantics.scl.compiler.elaboration.expressions;\r
2 \r
3 import gnu.trove.map.hash.TObjectIntHashMap;\r
4 import gnu.trove.set.hash.THashSet;\r
5 import gnu.trove.set.hash.TIntHashSet;\r
6 \r
7 import java.util.ArrayList;\r
8 import java.util.Collections;\r
9 import java.util.Comparator;\r
10 \r
11 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;\r
12 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;\r
13 import org.simantics.scl.compiler.elaboration.query.Query;\r
14 import org.simantics.scl.compiler.elaboration.rules.TransformationRule;\r
15 import org.simantics.scl.compiler.errors.Locations;\r
16 import org.simantics.scl.compiler.internal.elaboration.transformations.TransformationBuilder;\r
17 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;\r
18 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;\r
19 import org.simantics.scl.compiler.types.Type;\r
20 import org.simantics.scl.compiler.types.Types;\r
21 import org.simantics.scl.compiler.types.exceptions.MatchException;\r
22 \r
23 public class ETransformation extends SimplifiableExpression {\r
24     public static final Object TRANSFORMATION_RULES_TYPECHECKED = new Object();\r
25     \r
26     public final String name;\r
27     public Query seed;\r
28     \r
29     public ETransformation(String name, Query seed) {\r
30         this.name = name;\r
31         this.seed = seed;\r
32     }\r
33 \r
34     @Override\r
35     public void collectRefs(TObjectIntHashMap<Object> allRefs,\r
36             TIntHashSet refs) {\r
37         {\r
38             int ref = allRefs.get(TRANSFORMATION_RULES_TYPECHECKED);\r
39             if(ref >= 0)\r
40                 refs.add(ref);\r
41         }\r
42         seed.collectRefs(allRefs, refs);\r
43     }\r
44 \r
45     @Override\r
46     public void collectVars(TObjectIntHashMap<Variable> allVars,\r
47             TIntHashSet vars) {\r
48         seed.collectVars(allVars, vars);\r
49     }\r
50 \r
51     @Override\r
52     protected void updateType() throws MatchException {\r
53         setType(Types.UNIT);\r
54     }\r
55 \r
56     @Override\r
57     public void collectFreeVariables(THashSet<Variable> vars) {\r
58         seed.collectFreeVariables(vars);\r
59     }\r
60     \r
61     @Override\r
62     public Expression inferType(TypingContext context) {\r
63         context.declareEffect(location, Types.PROC);\r
64         seed.checkType(context);\r
65         return compile(context);\r
66     }\r
67 \r
68     private Expression compile(TypingContext context) {\r
69         ArrayList<TransformationRule> rules = new ArrayList<TransformationRule>(); \r
70         context.getEnvironment().collectRules(rules);\r
71         Collections.sort(rules, new Comparator<TransformationRule>() {\r
72             @Override\r
73             public int compare(TransformationRule o1, TransformationRule o2) {\r
74                 return Integer.compare(Locations.beginOf(o1.location), Locations.beginOf(o2.location));\r
75             }\r
76         });\r
77        \r
78         // Translation\r
79         TransformationBuilder tb = new TransformationBuilder(context.getErrorLog(), context);\r
80         tb.handleSeed(seed);\r
81         for(TransformationRule rule : rules)\r
82             if(!rule.isAbstract)\r
83                 tb.handleRule(rule);\r
84         Expression expression = tb.compileRules();\r
85         \r
86         if(SCLCompilerConfiguration.SHOW_COMPILED_RULES)\r
87             System.out.println(expression);\r
88         return expression;\r
89     }\r
90 \r
91     @Override\r
92     public Expression resolve(TranslationContext context) {\r
93         seed = seed.resolve(context);\r
94         return this;\r
95     }\r
96 \r
97     @Override\r
98     public void setLocationDeep(long loc) {\r
99         if(location == Locations.NO_LOCATION) {\r
100             location = loc;\r
101             seed.setLocationDeep(loc);\r
102         }\r
103     }\r
104 \r
105     @Override\r
106     public Expression decorate(ExpressionDecorator decorator) {\r
107         return decorator.decorate(this);\r
108     }\r
109 \r
110     @Override\r
111     public void collectEffects(THashSet<Type> effects) {\r
112         effects.add(Types.PROC);\r
113         //seed.collectEffects(Query.RW, effects); // FIXME\r
114     }\r
115 \r
116     @Override\r
117     public void accept(ExpressionVisitor visitor) {\r
118         visitor.visit(this);\r
119     }\r
120 \r
121     @Override\r
122     public void forVariables(VariableProcedure procedure) {\r
123         seed.forVariables(procedure);\r
124     }\r
125     \r
126     @Override\r
127     public Expression accept(ExpressionTransformer transformer) {\r
128         return transformer.transform(this);\r
129     }\r
130 \r
131 }\r