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.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;
18 import gnu.trove.map.hash.TObjectIntHashMap;
19 import gnu.trove.set.hash.THashSet;
20 import gnu.trove.set.hash.TIntHashSet;
22 public class EApplyType extends Expression {
23 Expression expression;
26 public EApplyType(Expression expression, Type parameter) {
27 this.expression = expression;
28 this.parameter = parameter;
31 public static Expression create(Expression expression, Type ... parameters) {
32 for(Type parameter : parameters)
33 expression = new EApplyType(expression, parameter);
37 public EApplyType(long loc, Expression expression, Type parameter) {
40 throw new NullPointerException();
41 this.expression = expression;
42 this.parameter = parameter;
45 public Expression getExpression() {
49 public Type getParameter() {
53 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
54 expression.collectVars(allVars, vars);
58 protected void updateType() throws MatchException {
59 setType(Types.instantiate(expression.getType(), parameter));
63 public IVal toVal(CompilationContext context, CodeWriter w) {
64 IVal val = expression.toVal(context, w);
65 return val.createSpecialization(parameter);
69 public void collectFreeVariables(THashSet<Variable> vars) {
70 expression.collectFreeVariables(vars);
74 public Expression simplify(SimplificationContext context) {
75 expression = expression.simplify(context);
76 if(expression instanceof EConstant) {
77 ((EConstant)expression).addTypeParameters(parameter);
84 public Expression resolve(TranslationContext context) {
85 expression = expression.resolve(context);
90 public void removeFreeVariables(THashSet<Variable> vars) {
91 expression.removeFreeVariables(vars);
95 public Expression replace(ReplaceContext context) {
96 return new EApplyType(expression.replace(context),
97 parameter.replace(context.tvarMap));
101 public IExpression toIExpression(ExpressionInterpretationContext target) {
102 return expression.toIExpression(target);
106 public Expression inferType(TypingContext context) {
107 throw new InternalCompilerError("Should not type check " + getClass().getSimpleName() + ".");
111 public boolean isEffectful() {
112 return expression.isEffectful();
116 public void setLocationDeep(long loc) {
117 if(location == Locations.NO_LOCATION) {
119 expression.setLocationDeep(loc);
124 public void accept(ExpressionVisitor visitor) {
129 public Expression accept(ExpressionTransformer transformer) {
130 return transformer.transform(this);