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 collectRefs(TObjectIntHashMap<Object> allRefs, TIntHashSet refs) {
54 expression.collectRefs(allRefs, refs);
57 public void collectVars(TObjectIntHashMap<Variable> allVars, TIntHashSet vars) {
58 expression.collectVars(allVars, vars);
62 protected void updateType() throws MatchException {
63 setType(Types.instantiate(expression.getType(), parameter));
67 public IVal toVal(CompilationContext context, CodeWriter w) {
68 IVal val = expression.toVal(context, w);
69 return val.createSpecialization(parameter);
73 public void collectFreeVariables(THashSet<Variable> vars) {
74 expression.collectFreeVariables(vars);
78 public Expression simplify(SimplificationContext context) {
79 expression = expression.simplify(context);
80 if(expression instanceof EConstant) {
81 ((EConstant)expression).addTypeParameters(parameter);
88 public Expression resolve(TranslationContext context) {
89 expression = expression.resolve(context);
94 public void removeFreeVariables(THashSet<Variable> vars) {
95 expression.removeFreeVariables(vars);
99 public Expression replace(ReplaceContext context) {
100 return new EApplyType(expression.replace(context),
101 parameter.replace(context.tvarMap));
105 public IExpression toIExpression(ExpressionInterpretationContext target) {
106 return expression.toIExpression(target);
110 public Expression inferType(TypingContext context) {
111 throw new InternalCompilerError("Should not type check " + getClass().getSimpleName() + ".");
115 public boolean isEffectful() {
116 return expression.isEffectful();
120 public void collectEffects(THashSet<Type> effects) {
121 expression.collectEffects(effects);
125 public void setLocationDeep(long loc) {
126 if(location == Locations.NO_LOCATION) {
128 expression.setLocationDeep(loc);
133 public void accept(ExpressionVisitor visitor) {
138 public void forVariables(VariableProcedure procedure) {
139 expression.forVariables(procedure);
143 public Expression accept(ExpressionTransformer transformer) {
144 return transformer.transform(this);