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.TIntHashSet;
21 public class EApplyType extends Expression {
22 public Expression expression;
25 public EApplyType(Expression expression, Type parameter) {
26 this.expression = expression;
27 this.parameter = parameter;
30 public static Expression create(Expression expression, Type ... parameters) {
31 for(Type parameter : parameters)
32 expression = new EApplyType(expression, parameter);
36 public EApplyType(long loc, Expression expression, Type parameter) {
39 throw new NullPointerException();
40 this.expression = expression;
41 this.parameter = parameter;
44 public Expression getExpression() {
48 public Type getParameter() {
52 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
53 expression.collectVars(allVars, vars);
57 protected void updateType() throws MatchException {
58 setType(Types.instantiate(expression.getType(), parameter));
62 public IVal toVal(CompilationContext context, CodeWriter w) {
63 IVal val = expression.toVal(context, w);
64 return val.createSpecialization(parameter);
68 public Expression simplify(SimplificationContext context) {
69 expression = expression.simplify(context);
70 if(expression instanceof EConstant) {
71 ((EConstant)expression).addTypeParameters(parameter);
78 public Expression resolve(TranslationContext context) {
79 expression = expression.resolve(context);
84 public Expression replace(ReplaceContext context) {
85 return new EApplyType(expression.replace(context),
86 parameter.replace(context.tvarMap));
90 public IExpression toIExpression(ExpressionInterpretationContext target) {
91 return expression.toIExpression(target);
95 public Expression inferType(TypingContext context) {
96 throw new InternalCompilerError("Should not type check " + getClass().getSimpleName() + ".");
100 public boolean isEffectful() {
101 return expression.isEffectful();
105 public void setLocationDeep(long loc) {
106 if(location == Locations.NO_LOCATION) {
108 expression.setLocationDeep(loc);
113 public void accept(ExpressionVisitor visitor) {
118 public Expression accept(ExpressionTransformer transformer) {
119 return transformer.transform(this);