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