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