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.Types;
12 import org.simantics.scl.compiler.types.exceptions.MatchException;
13 import org.simantics.scl.compiler.types.util.MultiFunction;
15 import gnu.trove.map.hash.TObjectIntHashMap;
16 import gnu.trove.set.hash.THashSet;
17 import gnu.trove.set.hash.TIntHashSet;
19 public class EViewPattern extends Expression {
20 public Expression expression;
21 public Expression pattern;
23 public EViewPattern(Expression expression, Expression pattern) {
24 this.expression = expression;
25 this.pattern = pattern;
29 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
30 expression.collectVars(allVars, vars);
31 pattern.collectVars(allVars, vars);
35 public Expression inferType(TypingContext context) {
36 context.setInPattern(false);
37 expression = expression.inferType(context);
38 context.setInPattern(true);
41 mfun = Types.matchFunction(expression.getType(), 1);
42 } catch (MatchException e) {
43 context.getErrorLog().log(expression.location, "Expected a function as a transformation expression.");
44 return new EError(location);
46 setType(mfun.parameterTypes[0]);
47 pattern.checkType(context, mfun.returnType);
52 protected void updateType() throws MatchException {
53 MultiFunction mfun = Types.matchFunction(expression.getType(), 1);
54 setType(mfun.parameterTypes[0]);
58 public IVal toVal(CompilationContext context, CodeWriter w) {
59 throw new InternalCompilerError(location, "EViewPattern.toVal should not be invoked.");
63 public void collectFreeVariables(THashSet<Variable> vars) {
64 throw new InternalCompilerError(location, "Cannot collect free variables for a pattern.");
68 public void removeFreeVariables(THashSet<Variable> vars) {
69 expression.collectFreeVariables(vars);
70 pattern.removeFreeVariables(vars);
74 public Expression resolve(TranslationContext context) {
75 context.getErrorLog().log("View pattern cannot occur only in patterns. Maybe you are missing '\\' in front of a lambda experssion?");
76 return new EError(location);
80 public Expression resolveAsPattern(TranslationContext context) {
81 expression = expression.resolve(context);
82 pattern = pattern.resolveAsPattern(context);
87 public void setLocationDeep(long loc) {
88 if(location == Locations.NO_LOCATION) {
90 expression.setLocationDeep(loc);
91 pattern.setLocationDeep(loc);
96 public void accept(ExpressionVisitor visitor) {
101 public Expression accept(ExpressionTransformer transformer) {
102 return transformer.transform(this);
106 public Expression simplify(SimplificationContext context) {
107 expression = expression.simplify(context);
108 pattern = pattern.simplify(context);