]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EViewPattern.java
a2a0a02c5bc04b90b715ede54a0d9be0ca7e6055
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / EViewPattern.java
1 package org.simantics.scl.compiler.elaboration.expressions;
2
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;
15
16 import gnu.trove.map.hash.TObjectIntHashMap;
17 import gnu.trove.set.hash.THashSet;
18 import gnu.trove.set.hash.TIntHashSet;
19
20 public class EViewPattern extends Expression {
21     public Expression expression;
22     public Expression pattern;
23     
24     public EViewPattern(Expression expression, Expression pattern) {
25         this.expression = expression;
26         this.pattern = pattern;
27     }
28
29     @Override
30     public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
31         expression.collectVars(allVars, vars);
32         pattern.collectVars(allVars, vars);
33     }
34     
35     @Override
36     public Expression inferType(TypingContext context) {
37         context.setInPattern(false);
38         expression = expression.inferType(context);
39         context.setInPattern(true);
40         MultiFunction mfun;
41         try {
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);
46         }
47         setType(mfun.parameterTypes[0]);
48         pattern.checkType(context, mfun.returnType);
49         return this;
50     }
51
52     @Override
53     protected void updateType() throws MatchException {
54         MultiFunction mfun = Types.matchFunction(expression.getType(), 1);
55         setType(mfun.parameterTypes[0]);
56     }
57
58     @Override
59     public IVal toVal(CompilationContext context, CodeWriter w) {
60         throw new InternalCompilerError(location, "EViewPattern.toVal should not be invoked.");
61     }
62
63     @Override
64     public void collectFreeVariables(THashSet<Variable> vars) {
65         throw new InternalCompilerError(location, "Cannot collect free variables for a pattern.");
66     }
67     
68     @Override
69     public void removeFreeVariables(THashSet<Variable> vars) {
70         expression.collectFreeVariables(vars);
71         pattern.removeFreeVariables(vars);
72     }
73
74     @Override
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);
78     }
79
80     @Override
81     public Expression resolveAsPattern(TranslationContext context) {
82         expression = expression.resolve(context);
83         pattern = pattern.resolveAsPattern(context);
84         return this;
85     }
86     
87     @Override
88     public void setLocationDeep(long loc) {
89         if(location == Locations.NO_LOCATION) {
90             location = loc; 
91             expression.setLocationDeep(loc);
92             pattern.setLocationDeep(loc);
93         }
94     }
95
96     @Override
97     public void collectEffects(THashSet<Type> effects) {
98         expression.collectEffects(effects);
99     }
100
101     @Override
102     public void accept(ExpressionVisitor visitor) {
103         visitor.visit(this);
104     }
105
106     @Override
107     public Expression accept(ExpressionTransformer transformer) {
108         return transformer.transform(this);
109     }
110     
111     @Override
112     public Expression simplify(SimplificationContext context) {
113         expression = expression.simplify(context);
114         pattern = pattern.simplify(context);
115         return this;
116     }
117
118 }