]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/EApplyType.java
(refs #7375) Replaced collectEffects by CollectEffectsVisitor
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / EApplyType.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.ReplaceContext;
6 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
7 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
8 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
9 import org.simantics.scl.compiler.errors.Locations;
10 import org.simantics.scl.compiler.internal.codegen.references.IVal;
11 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
12 import org.simantics.scl.compiler.internal.interpreted.IExpression;
13 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
14 import org.simantics.scl.compiler.types.Type;
15 import org.simantics.scl.compiler.types.Types;
16 import org.simantics.scl.compiler.types.exceptions.MatchException;
17
18 import gnu.trove.map.hash.TObjectIntHashMap;
19 import gnu.trove.set.hash.THashSet;
20 import gnu.trove.set.hash.TIntHashSet;
21
22 public class EApplyType extends Expression {    
23     Expression expression;
24     Type parameter;
25     
26     public EApplyType(Expression expression, Type parameter) {
27         this.expression = expression;
28         this.parameter = parameter;
29     }
30     
31     public static Expression create(Expression expression, Type ... parameters) {
32         for(Type parameter : parameters)
33             expression = new EApplyType(expression, parameter);
34         return expression;
35     }
36
37     public EApplyType(long loc, Expression expression, Type parameter) {
38         super(loc);
39         if(parameter == null)
40             throw new NullPointerException();
41         this.expression = expression;
42         this.parameter = parameter;
43     }
44     
45     public Expression getExpression() {
46         return expression;
47     }
48     
49     public Type getParameter() {
50         return parameter;
51     }
52
53     public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
54         expression.collectVars(allVars, vars);
55     }
56
57     @Override
58     protected void updateType() throws MatchException {
59         setType(Types.instantiate(expression.getType(), parameter));
60     }
61
62     @Override
63     public IVal toVal(CompilationContext context, CodeWriter w) {
64         IVal val = expression.toVal(context, w);
65         return val.createSpecialization(parameter);
66     }
67
68     @Override
69     public void collectFreeVariables(THashSet<Variable> vars) {
70         expression.collectFreeVariables(vars);
71     }
72
73     @Override
74     public Expression simplify(SimplificationContext context) {
75         expression = expression.simplify(context);
76         if(expression instanceof EConstant) {
77             ((EConstant)expression).addTypeParameters(parameter);
78             return expression;
79         }
80         return this;
81     }
82
83     @Override
84     public Expression resolve(TranslationContext context) {
85         expression = expression.resolve(context);
86         return this;
87     }
88     
89     @Override
90     public void removeFreeVariables(THashSet<Variable> vars) {
91         expression.removeFreeVariables(vars);
92     }
93
94     @Override
95     public Expression replace(ReplaceContext context) {
96         return new EApplyType(expression.replace(context), 
97                 parameter.replace(context.tvarMap));
98     }
99     
100     @Override
101     public IExpression toIExpression(ExpressionInterpretationContext target) {
102         return expression.toIExpression(target);
103     }
104     
105     @Override
106     public Expression inferType(TypingContext context) {
107         throw new InternalCompilerError("Should not type check " + getClass().getSimpleName() + ".");
108     }
109     
110     @Override
111     public boolean isEffectful() {
112         return expression.isEffectful();
113     }
114     
115     @Override
116     public void setLocationDeep(long loc) {
117         if(location == Locations.NO_LOCATION) {
118             location = loc;
119             expression.setLocationDeep(loc);
120         }
121     }
122     
123     @Override
124     public void accept(ExpressionVisitor visitor) {
125         visitor.visit(this);
126     }
127     
128     @Override
129     public Expression accept(ExpressionTransformer transformer) {
130         return transformer.transform(this);
131     }
132
133 }