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.internal.elaboration.utils.ExpressionDecorator;
12 import org.simantics.scl.compiler.types.Type;
13 import org.simantics.scl.compiler.types.Types;
14 import org.simantics.scl.compiler.types.exceptions.MatchException;
15 import org.simantics.scl.compiler.types.util.MultiFunction;
17 import gnu.trove.map.hash.TObjectIntHashMap;
18 import gnu.trove.set.hash.THashSet;
19 import gnu.trove.set.hash.TIntHashSet;
21 public class EViewPattern extends Expression {
22 public Expression expression;
23 public Expression pattern;
25 public EViewPattern(Expression expression, Expression pattern) {
26 this.expression = expression;
27 this.pattern = pattern;
31 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
32 expression.collectRefs(allRefs, refs);
33 pattern.collectRefs(allRefs, refs);
37 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
38 expression.collectVars(allVars, vars);
39 pattern.collectVars(allVars, vars);
43 public void forVariables(VariableProcedure procedure) {
44 expression.forVariables(procedure);
45 pattern.forVariables(procedure);
49 public Expression inferType(TypingContext context) {
50 context.setInPattern(false);
51 expression = expression.inferType(context);
52 context.setInPattern(true);
55 mfun = Types.matchFunction(expression.getType(), 1);
56 } catch (MatchException e) {
57 context.getErrorLog().log(expression.location, "Expected a function as a transformation expression.");
58 return new EError(location);
60 setType(mfun.parameterTypes[0]);
61 pattern.checkType(context, mfun.returnType);
66 protected void updateType() throws MatchException {
67 MultiFunction mfun = Types.matchFunction(expression.getType(), 1);
68 setType(mfun.parameterTypes[0]);
72 public IVal toVal(CompilationContext context, CodeWriter w) {
73 throw new InternalCompilerError(location, "EViewPattern.toVal should not be invoked.");
77 public void collectFreeVariables(THashSet<Variable> vars) {
78 throw new InternalCompilerError(location, "Cannot collect free variables for a pattern.");
82 public void removeFreeVariables(THashSet<Variable> vars) {
83 expression.collectFreeVariables(vars);
84 pattern.removeFreeVariables(vars);
88 public Expression resolve(TranslationContext context) {
89 context.getErrorLog().log("View pattern cannot occur only in patterns. Maybe you are missing '\\' in front of a lambda experssion?");
90 return new EError(location);
94 public Expression resolveAsPattern(TranslationContext context) {
95 expression = expression.resolve(context);
96 pattern = pattern.resolveAsPattern(context);
101 public void setLocationDeep(long loc) {
102 if(location == Locations.NO_LOCATION) {
104 expression.setLocationDeep(loc);
105 pattern.setLocationDeep(loc);
110 public Expression decorate(ExpressionDecorator decorator) {
111 expression = expression.decorate(decorator);
116 public void collectEffects(THashSet<Type> effects) {
117 expression.collectEffects(effects);
121 public void accept(ExpressionVisitor visitor) {
126 public Expression accept(ExpressionTransformer transformer) {
127 return transformer.transform(this);
131 public Expression simplify(SimplificationContext context) {
132 expression = expression.simplify(context);
133 pattern = pattern.simplify(context);