1 package org.simantics.scl.compiler.elaboration.expressions;
\r
3 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
\r
4 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
\r
5 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
\r
6 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
\r
7 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
\r
8 import org.simantics.scl.compiler.environment.Environment;
\r
9 import org.simantics.scl.compiler.errors.Locations;
\r
10 import org.simantics.scl.compiler.internal.codegen.references.IVal;
\r
11 import org.simantics.scl.compiler.internal.codegen.writer.CodeWriter;
\r
12 import org.simantics.scl.compiler.internal.elaboration.utils.ExpressionDecorator;
\r
13 import org.simantics.scl.compiler.types.Type;
\r
14 import org.simantics.scl.compiler.types.exceptions.MatchException;
\r
16 import gnu.trove.map.hash.TObjectIntHashMap;
\r
17 import gnu.trove.set.hash.THashSet;
\r
18 import gnu.trove.set.hash.TIntHashSet;
\r
20 public class EAsPattern extends Expression {
\r
26 public EAsPattern(EVar eVar, Expression pattern) {
\r
28 this.pattern = pattern;
\r
31 public EAsPattern(Variable var, Expression pattern) {
\r
33 this.pattern = pattern;
\r
36 public Variable getVariable() {
\r
40 public Expression getPattern() {
\r
45 public void collectRefs(TObjectIntHashMap<Object> allRefs,
\r
47 pattern.collectRefs(allRefs, refs);
\r
51 public void collectVars(TObjectIntHashMap<Variable> allVars,
\r
53 int id = allVars.get(var);
\r
56 pattern.collectVars(allVars, vars);
\r
60 public void collectFreeVariables(THashSet<Variable> vars) {
\r
62 pattern.collectFreeVariables(vars);
\r
66 public Expression simplify(SimplificationContext context) {
\r
67 pattern = pattern.simplify(context);
\r
72 public Expression resolve(TranslationContext context) {
\r
73 context.getErrorLog().log(location, "As binding (@) can be used only in patterns.");
\r
78 public Expression resolveAsPattern(TranslationContext context) {
\r
79 var = context.newVariable(eVar.name);
\r
80 pattern = pattern.resolveAsPattern(context);
\r
85 protected void updateType() throws MatchException {
\r
86 setType(pattern.getType());
\r
90 public IVal toVal(Environment env, CodeWriter w) {
\r
91 throw new InternalCompilerError("EAsPattern allowed only in patterns.");
\r
95 public Expression inferType(TypingContext context) {
\r
96 pattern = pattern.inferType(context);
\r
97 var.setType(pattern.getType());
\r
102 public Expression checkBasicType(TypingContext context, Type requiredType) {
\r
103 pattern = pattern.checkBasicType(context, requiredType);
\r
104 var.setType(pattern.getType());
\r
109 public Expression decorate(ExpressionDecorator decorator) {
\r
110 pattern = pattern.decorate(decorator);
\r
111 return decorator.decorate(this);
\r
115 public Expression replace(ReplaceContext context) {
\r
116 if(!context.inPattern)
\r
117 throw new IllegalStateException();
\r
119 Variable newVariable = new Variable(var.name);
\r
120 newVariable.setType(var.getType().replace(context.tvarMap));
\r
121 context.varMap.put(var, new EVariable(newVariable));
\r
123 EAsPattern result = new EAsPattern(eVar, pattern.replace(context));
\r
124 result.var = newVariable;
\r
130 public void collectEffects(THashSet<Type> effects) {
\r
131 pattern.collectEffects(effects);
\r
135 public void setLocationDeep(long loc) {
\r
136 if(location == Locations.NO_LOCATION) {
\r
138 pattern.setLocationDeep(loc);
\r
140 eVar.setLocationDeep(loc);
\r
145 public void accept(ExpressionVisitor visitor) {
\r
146 visitor.visit(this);
\r
150 public void forVariables(VariableProcedure procedure) {
\r
151 procedure.execute(eVar.location, var);
\r
152 pattern.forVariables(procedure);
\r
156 public Expression accept(ExpressionTransformer transformer) {
\r
157 return transformer.transform(this);
\r