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 collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
31 expression.collectVars(allVars, vars);
32 pattern.collectVars(allVars, vars);
36 public Expression inferType(TypingContext context) {
37 context.setInPattern(false);
38 expression = expression.inferType(context);
39 context.setInPattern(true);
42 mfun = Types.matchFunction(expression.getType(), 1);
43 } catch (MatchException e) {
44 context.getErrorLog().log(expression.location, "Expected a function as a transformation expression.");
45 return new EError(location);
47 setType(mfun.parameterTypes[0]);
48 pattern.checkType(context, mfun.returnType);
53 protected void updateType() throws MatchException {
54 MultiFunction mfun = Types.matchFunction(expression.getType(), 1);
55 setType(mfun.parameterTypes[0]);
59 public IVal toVal(CompilationContext context, CodeWriter w) {
60 throw new InternalCompilerError(location, "EViewPattern.toVal should not be invoked.");
64 public void collectFreeVariables(THashSet<Variable> vars) {
65 throw new InternalCompilerError(location, "Cannot collect free variables for a pattern.");
69 public void removeFreeVariables(THashSet<Variable> vars) {
70 expression.collectFreeVariables(vars);
71 pattern.removeFreeVariables(vars);
75 public Expression resolve(TranslationContext context) {
76 context.getErrorLog().log("View pattern cannot occur only in patterns. Maybe you are missing '\\' in front of a lambda experssion?");
77 return new EError(location);
81 public Expression resolveAsPattern(TranslationContext context) {
82 expression = expression.resolve(context);
83 pattern = pattern.resolveAsPattern(context);
88 public void setLocationDeep(long loc) {
89 if(location == Locations.NO_LOCATION) {
91 expression.setLocationDeep(loc);
92 pattern.setLocationDeep(loc);
97 public void collectEffects(THashSet<Type> effects) {
98 expression.collectEffects(effects);
102 public void accept(ExpressionVisitor visitor) {
107 public Expression accept(ExpressionTransformer transformer) {
108 return transformer.transform(this);
112 public Expression simplify(SimplificationContext context) {
113 expression = expression.simplify(context);
114 pattern = pattern.simplify(context);