1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
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.internal.elaboration.utils.ExpressionDecorator;
14 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
15 import org.simantics.scl.compiler.types.Type;
16 import org.simantics.scl.compiler.types.Types;
17 import org.simantics.scl.compiler.types.exceptions.MatchException;
19 import gnu.trove.map.hash.TObjectIntHashMap;
20 import gnu.trove.set.hash.THashSet;
21 import gnu.trove.set.hash.TIntHashSet;
23 public class ETransformation extends SimplifiableExpression {
24 public static final Object TRANSFORMATION_RULES_TYPECHECKED = new Object();
26 public final String name;
29 public ETransformation(String name, Query seed) {
35 public void collectRefs(TObjectIntHashMap<Object> allRefs,
38 int ref = allRefs.get(TRANSFORMATION_RULES_TYPECHECKED);
42 seed.collectRefs(allRefs, refs);
46 public void collectVars(TObjectIntHashMap<Variable> allVars,
48 seed.collectVars(allVars, vars);
52 protected void updateType() throws MatchException {
57 public void collectFreeVariables(THashSet<Variable> vars) {
58 seed.collectFreeVariables(vars);
62 public Expression inferType(TypingContext context) {
63 context.declareEffect(location, Types.PROC);
64 seed.checkType(context);
65 return compile(context);
68 private Expression compile(TypingContext context) {
69 ArrayList<TransformationRule> rules = new ArrayList<TransformationRule>();
70 context.getEnvironment().collectRules(rules);
71 Collections.sort(rules, new Comparator<TransformationRule>() {
73 public int compare(TransformationRule o1, TransformationRule o2) {
74 return Integer.compare(Locations.beginOf(o1.location), Locations.beginOf(o2.location));
79 TransformationBuilder tb = new TransformationBuilder(context.getErrorLog(), context);
81 for(TransformationRule rule : rules)
84 Expression expression = tb.compileRules();
86 if(SCLCompilerConfiguration.SHOW_COMPILED_RULES)
87 System.out.println(expression);
92 public Expression resolve(TranslationContext context) {
93 seed = seed.resolve(context);
98 public void setLocationDeep(long loc) {
99 if(location == Locations.NO_LOCATION) {
101 seed.setLocationDeep(loc);
106 public Expression decorate(ExpressionDecorator decorator) {
107 return decorator.decorate(this);
111 public void collectEffects(THashSet<Type> effects) {
112 effects.add(Types.PROC);
113 //seed.collectEffects(Query.RW, effects); // FIXME
117 public void accept(ExpressionVisitor visitor) {
122 public void forVariables(VariableProcedure procedure) {
123 seed.forVariables(procedure);
127 public Expression accept(ExpressionTransformer transformer) {
128 return transformer.transform(this);