1 package org.simantics.scl.compiler.elaboration.expressions;
3 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
4 import org.simantics.scl.compiler.compilation.CompilationContext;
5 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
7 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
8 import org.simantics.scl.compiler.errors.Locations;
9 import org.simantics.scl.compiler.internal.codegen.references.IVal;
10 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
11 import org.simantics.scl.compiler.types.Type;
12 import org.simantics.scl.compiler.types.Types;
13 import org.simantics.scl.compiler.types.exceptions.MatchException;
14 import org.simantics.scl.compiler.types.util.MultiFunction;
16 import gnu.trove.map.hash.TObjectIntHashMap;
17 import gnu.trove.set.hash.THashSet;
18 import gnu.trove.set.hash.TIntHashSet;
20 public class EViewPattern extends Expression {
21 public Expression expression;
22 public Expression pattern;
24 public EViewPattern(Expression expression, Expression pattern) {
25 this.expression = expression;
26 this.pattern = pattern;
30 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
31 expression.collectRefs(allRefs, refs);
32 pattern.collectRefs(allRefs, refs);
36 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
37 expression.collectVars(allVars, vars);
38 pattern.collectVars(allVars, vars);
42 public void forVariables(VariableProcedure procedure) {
43 expression.forVariables(procedure);
44 pattern.forVariables(procedure);
48 public Expression inferType(TypingContext context) {
49 context.setInPattern(false);
50 expression = expression.inferType(context);
51 context.setInPattern(true);
54 mfun = Types.matchFunction(expression.getType(), 1);
55 } catch (MatchException e) {
56 context.getErrorLog().log(expression.location, "Expected a function as a transformation expression.");
57 return new EError(location);
59 setType(mfun.parameterTypes[0]);
60 pattern.checkType(context, mfun.returnType);
65 protected void updateType() throws MatchException {
66 MultiFunction mfun = Types.matchFunction(expression.getType(), 1);
67 setType(mfun.parameterTypes[0]);
71 public IVal toVal(CompilationContext context, CodeWriter w) {
72 throw new InternalCompilerError(location, "EViewPattern.toVal should not be invoked.");
76 public void collectFreeVariables(THashSet<Variable> vars) {
77 throw new InternalCompilerError(location, "Cannot collect free variables for a pattern.");
81 public void removeFreeVariables(THashSet<Variable> vars) {
82 expression.collectFreeVariables(vars);
83 pattern.removeFreeVariables(vars);
87 public Expression resolve(TranslationContext context) {
88 context.getErrorLog().log("View pattern cannot occur only in patterns. Maybe you are missing '\\' in front of a lambda experssion?");
89 return new EError(location);
93 public Expression resolveAsPattern(TranslationContext context) {
94 expression = expression.resolve(context);
95 pattern = pattern.resolveAsPattern(context);
100 public void setLocationDeep(long loc) {
101 if(location == Locations.NO_LOCATION) {
103 expression.setLocationDeep(loc);
104 pattern.setLocationDeep(loc);
109 public void collectEffects(THashSet<Type> effects) {
110 expression.collectEffects(effects);
114 public void accept(ExpressionVisitor visitor) {
119 public Expression accept(ExpressionTransformer transformer) {
120 return transformer.transform(this);
124 public Expression simplify(SimplificationContext context) {
125 expression = expression.simplify(context);
126 pattern = pattern.simplify(context);