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.TIntHashSet;
18 public class EViewPattern extends Expression {
19 public Expression expression;
20 public Expression pattern;
22 public EViewPattern(Expression expression, Expression pattern) {
23 this.expression = expression;
24 this.pattern = pattern;
28 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
29 expression.collectVars(allVars, vars);
30 pattern.collectVars(allVars, vars);
34 public Expression inferType(TypingContext context) {
35 context.setInPattern(false);
36 expression = expression.inferType(context);
37 context.setInPattern(true);
40 mfun = Types.matchFunction(expression.getType(), 1);
41 } catch (MatchException e) {
42 context.getErrorLog().log(expression.location, "Expected a function as a transformation expression.");
43 return new EError(location);
45 setType(mfun.parameterTypes[0]);
46 pattern.checkType(context, mfun.returnType);
51 protected void updateType() throws MatchException {
52 MultiFunction mfun = Types.matchFunction(expression.getType(), 1);
53 setType(mfun.parameterTypes[0]);
57 public IVal toVal(CompilationContext context, CodeWriter w) {
58 throw new InternalCompilerError(location, "EViewPattern.toVal should not be invoked.");
62 public Expression resolve(TranslationContext context) {
63 context.getErrorLog().log("View pattern cannot occur only in patterns. Maybe you are missing '\\' in front of a lambda experssion?");
64 return new EError(location);
68 public Expression resolveAsPattern(TranslationContext context) {
69 expression = expression.resolve(context);
70 pattern = pattern.resolveAsPattern(context);
75 public void setLocationDeep(long loc) {
76 if(location == Locations.NO_LOCATION) {
78 expression.setLocationDeep(loc);
79 pattern.setLocationDeep(loc);
84 public void accept(ExpressionVisitor visitor) {
89 public Expression accept(ExpressionTransformer transformer) {
90 return transformer.transform(this);
94 public Expression simplify(SimplificationContext context) {
95 expression = expression.simplify(context);
96 pattern = pattern.simplify(context);