1 package org.simantics.scl.compiler.elaboration.expressions;
\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
7 import java.util.ArrayList;
\r
8 import java.util.Collections;
\r
9 import java.util.Comparator;
\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
23 public class ETransformation extends SimplifiableExpression {
\r
24 public static final Object TRANSFORMATION_RULES_TYPECHECKED = new Object();
\r
26 public final String name;
\r
29 public ETransformation(String name, Query seed) {
\r
35 public void collectRefs(TObjectIntHashMap<Object> allRefs,
\r
38 int ref = allRefs.get(TRANSFORMATION_RULES_TYPECHECKED);
\r
42 seed.collectRefs(allRefs, refs);
\r
46 public void collectVars(TObjectIntHashMap<Variable> allVars,
\r
48 seed.collectVars(allVars, vars);
\r
52 protected void updateType() throws MatchException {
\r
53 setType(Types.UNIT);
\r
57 public void collectFreeVariables(THashSet<Variable> vars) {
\r
58 seed.collectFreeVariables(vars);
\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
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
73 public int compare(TransformationRule o1, TransformationRule o2) {
\r
74 return Integer.compare(Locations.beginOf(o1.location), Locations.beginOf(o2.location));
\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
86 if(SCLCompilerConfiguration.SHOW_COMPILED_RULES)
\r
87 System.out.println(expression);
\r
92 public Expression resolve(TranslationContext context) {
\r
93 seed = seed.resolve(context);
\r
98 public void setLocationDeep(long loc) {
\r
99 if(location == Locations.NO_LOCATION) {
\r
101 seed.setLocationDeep(loc);
\r
106 public Expression decorate(ExpressionDecorator decorator) {
\r
107 return decorator.decorate(this);
\r
111 public void collectEffects(THashSet<Type> effects) {
\r
112 effects.add(Types.PROC);
\r
113 //seed.collectEffects(Query.RW, effects); // FIXME
\r
117 public void accept(ExpressionVisitor visitor) {
\r
118 visitor.visit(this);
\r
122 public void forVariables(VariableProcedure procedure) {
\r
123 seed.forVariables(procedure);
\r
127 public Expression accept(ExpressionTransformer transformer) {
\r
128 return transformer.transform(this);
\r