1 package org.simantics.scl.compiler.elaboration.expressions;
3 import java.util.ArrayList;
4 import java.util.Collections;
7 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
8 import org.simantics.scl.compiler.compilation.CompilationContext;
9 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
10 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
11 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
12 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
13 import org.simantics.scl.compiler.errors.Locations;
14 import org.simantics.scl.compiler.internal.codegen.references.IVal;
15 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
16 import org.simantics.scl.compiler.internal.interpreted.IExpression;
17 import org.simantics.scl.compiler.internal.interpreted.IVariable;
18 import org.simantics.scl.compiler.top.ExpressionInterpretationContext;
19 import org.simantics.scl.compiler.types.Type;
20 import org.simantics.scl.compiler.types.Types;
21 import org.simantics.scl.compiler.types.exceptions.MatchException;
22 import org.simantics.scl.compiler.types.kinds.Kinds;
23 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
25 import gnu.trove.map.hash.TObjectIntHashMap;
26 import gnu.trove.set.hash.TIntHashSet;
28 public class EVariable extends Expression {
29 public static final EVariable[] EMPTY_ARRAY = new EVariable[0];
31 public Variable variable;
33 public EVariable(Variable variable) {
34 this.variable = variable;
37 public EVariable(long loc, Variable variable) {
39 this.variable = variable;
42 public Variable getVariable() {
46 public void setVariable(Variable variable) {
47 this.variable = variable;
51 public void collectVars(TObjectIntHashMap<Variable> allVars,
53 int id = allVars.get(variable);
59 public Set<Variable> getFreeVariables() {
61 return Collections.emptySet();
63 return Collections.singleton(variable);
66 public void toString(StringBuilder b, TypeUnparsingContext tuc) {
67 b.append(variable == null ? "???" : variable.toString());
71 protected void updateType() throws MatchException {
72 setType(variable.getType());
76 public IVal toVal(CompilationContext context, CodeWriter w) {
77 return variable.getVal();
81 public Expression simplify(SimplificationContext context) {
82 Expression expression = context.getInlinedValue(variable);
83 if(expression != null)
84 return expression.copy();
90 public Expression resolve(TranslationContext context) {
95 public void getParameters(TranslationContext translationContext,
96 ArrayList<Expression> parameters) {
100 public Expression resolveAsPattern(TranslationContext context) {
105 public Expression replace(ReplaceContext context) {
106 if(context.inPattern) {
107 Type type = variable.getType().replace(context.tvarMap);
109 Variable newVariable = new Variable(variable.name);
110 newVariable.setType(type);
111 EVariable result = new EVariable(newVariable);
112 context.varMap.put(variable, result);
116 if(variable == null) {
117 EVariable newVariable = new EVariable(location, null);
118 newVariable.setType(getType().replace(context.tvarMap));
119 if(context.typingContext == null)
120 throw new InternalCompilerError(location, "Encountered unresolved variable but not in type checking phase.");
121 context.typingContext.addConstraintDemand(newVariable);
125 Expression expression = context.varMap.get(variable);
126 if(expression != null)
127 return expression.copy(context.typingContext);
129 return new EVariable(variable);
136 public IExpression toIExpression(ExpressionInterpretationContext target) {
137 return new IVariable(target.getVariableId(variable));
141 public Expression inferType(TypingContext context) {
142 if(context.isInPattern()) {
143 variable.setType(Types.metaVar(Kinds.STAR));
147 return applyPUnit(context);
151 public Expression checkBasicType(TypingContext context, Type requiredType) {
152 if(context.isInPattern()) {
153 variable.setType(requiredType);
157 return context.subsume(this, requiredType);
161 public boolean isEffectful() {
166 public void setLocationDeep(long loc) {
167 if(location == Locations.NO_LOCATION)
172 public void accept(ExpressionVisitor visitor) {
177 public boolean isPattern(int arity) {
182 public Expression accept(ExpressionTransformer transformer) {
183 return transformer.transform(this);
187 public boolean equalsExpression(Expression expression) {
188 if(expression.getClass() != getClass())
190 EVariable other = (EVariable)expression;
191 return variable == other.variable;