1 package org.simantics.scl.compiler.elaboration.expressions;
\r
3 import gnu.trove.map.hash.TObjectIntHashMap;
\r
4 import gnu.trove.set.hash.THashSet;
\r
5 import gnu.trove.set.hash.TIntHashSet;
\r
7 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
\r
8 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
\r
9 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
\r
10 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
\r
11 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
\r
12 import org.simantics.scl.compiler.environment.Environment;
\r
13 import org.simantics.scl.compiler.errors.Locations;
\r
14 import org.simantics.scl.compiler.internal.codegen.references.IVal;
\r
15 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
\r
16 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
\r
17 import org.simantics.scl.compiler.internal.interpreted.IExpression;
\r
18 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
\r
19 import org.simantics.scl.compiler.types.Type;
\r
20 import org.simantics.scl.compiler.types.Types;
\r
21 import org.simantics.scl.compiler.types.exceptions.MatchException;
\r
23 public class EApplyType extends Expression {
\r
24 Expression expression;
\r
27 public EApplyType(Expression expression, Type parameter) {
\r
28 this.expression = expression;
\r
29 this.parameter = parameter;
\r
32 public static Expression create(Expression expression, Type ... parameters) {
\r
33 for(Type parameter : parameters)
\r
34 expression = new EApplyType(expression, parameter);
\r
38 public EApplyType(long loc, Expression expression, Type parameter) {
\r
40 if(parameter == null)
\r
41 throw new NullPointerException();
\r
42 this.expression = expression;
\r
43 this.parameter = parameter;
\r
46 public Expression getExpression() {
\r
50 public Type getParameter() {
\r
54 public void collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
\r
55 expression.collectRefs(allRefs, refs);
\r
58 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
\r
59 expression.collectVars(allVars, vars);
\r
63 protected void updateType() throws MatchException {
\r
64 setType(Types.instantiate(expression.getType(), parameter));
\r
68 public IVal toVal(Environment env, CodeWriter w) {
\r
69 IVal val = expression.toVal(env, w);
\r
70 return val.createSpecialization(parameter);
\r
74 public void collectFreeVariables(THashSet<Variable> vars) {
\r
75 expression.collectFreeVariables(vars);
\r
79 public Expression simplify(SimplificationContext context) {
\r
80 expression = expression.simplify(context);
\r
81 if(expression instanceof EConstant) {
\r
82 ((EConstant)expression).addTypeParameters(parameter);
\r
89 public Expression resolve(TranslationContext context) {
\r
90 expression = expression.resolve(context);
\r
95 public void removeFreeVariables(THashSet<Variable> vars) {
\r
96 expression.removeFreeVariables(vars);
\r
100 public Expression replace(ReplaceContext context) {
\r
101 return new EApplyType(expression.replace(context),
\r
102 parameter.replace(context.tvarMap));
\r
106 public IExpression toIExpression(ExpressionInterpretationContext target) {
\r
107 return expression.toIExpression(target);
\r
111 public Expression inferType(TypingContext context) {
\r
112 throw new InternalCompilerError("Should not type check " + getClass().getSimpleName() + ".");
\r
116 public Expression decorate(ExpressionDecorator decorator) {
\r
117 expression = expression.decorate(decorator);
\r
118 return decorator.decorate(this);
\r
122 public boolean isEffectful() {
\r
123 return expression.isEffectful();
\r
127 public void collectEffects(THashSet<Type> effects) {
\r
128 expression.collectEffects(effects);
\r
132 public void setLocationDeep(long loc) {
\r
133 if(location == Locations.NO_LOCATION) {
\r
135 expression.setLocationDeep(loc);
\r
140 public void accept(ExpressionVisitor visitor) {
\r
141 visitor.visit(this);
\r
145 public void forVariables(VariableProcedure procedure) {
\r
146 expression.forVariables(procedure);
\r
150 public Expression accept(ExpressionTransformer transformer) {
\r
151 return transformer.transform(this);
\r