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