1 package org.simantics.scl.compiler.elaboration.expressions;
3 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
4 import org.simantics.scl.compiler.elaboration.contexts.ReplaceContext;
5 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
6 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
7 import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
8 import org.simantics.scl.compiler.environment.Environment;
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.elaboration.utils.ExpressionDecorator;
13 import org.simantics.scl.compiler.types.Type;
14 import org.simantics.scl.compiler.types.exceptions.MatchException;
16 import gnu.trove.map.hash.TObjectIntHashMap;
17 import gnu.trove.set.hash.THashSet;
18 import gnu.trove.set.hash.TIntHashSet;
20 public class EAsPattern extends Expression {
26 public EAsPattern(EVar eVar, Expression pattern) {
28 this.pattern = pattern;
31 public EAsPattern(Variable var, Expression pattern) {
33 this.pattern = pattern;
36 public Variable getVariable() {
40 public Expression getPattern() {
45 public void collectRefs(TObjectIntHashMap<Object> allRefs,
47 pattern.collectRefs(allRefs, refs);
51 public void collectVars(TObjectIntHashMap<Variable> allVars,
53 int id = allVars.get(var);
56 pattern.collectVars(allVars, vars);
60 public void collectFreeVariables(THashSet<Variable> vars) {
61 throw new InternalCompilerError(location, "Cannot collect free variables for a pattern.");
65 public void removeFreeVariables(THashSet<Variable> vars) {
67 pattern.removeFreeVariables(vars);
71 public Expression simplify(SimplificationContext context) {
72 pattern = pattern.simplify(context);
77 public Expression resolve(TranslationContext context) {
78 context.getErrorLog().log(location, "As binding (@) can be used only in patterns.");
83 public Expression resolveAsPattern(TranslationContext context) {
84 var = context.newVariable(eVar.name);
85 pattern = pattern.resolveAsPattern(context);
90 protected void updateType() throws MatchException {
91 setType(pattern.getType());
95 public IVal toVal(Environment env, CodeWriter w) {
96 throw new InternalCompilerError("EAsPattern allowed only in patterns.");
100 public Expression inferType(TypingContext context) {
101 pattern = pattern.inferType(context);
102 var.setType(pattern.getType());
107 public Expression checkBasicType(TypingContext context, Type requiredType) {
108 pattern = pattern.checkBasicType(context, requiredType);
109 var.setType(pattern.getType());
114 public Expression decorate(ExpressionDecorator decorator) {
115 pattern = pattern.decorate(decorator);
116 return decorator.decorate(this);
120 public Expression replace(ReplaceContext context) {
121 if(!context.inPattern)
122 throw new IllegalStateException();
124 Variable newVariable = new Variable(var.name);
125 newVariable.setType(var.getType().replace(context.tvarMap));
126 context.varMap.put(var, new EVariable(newVariable));
128 EAsPattern result = new EAsPattern(eVar, pattern.replace(context));
129 result.var = newVariable;
135 public void collectEffects(THashSet<Type> effects) {
136 pattern.collectEffects(effects);
140 public void setLocationDeep(long loc) {
141 if(location == Locations.NO_LOCATION) {
143 pattern.setLocationDeep(loc);
145 eVar.setLocationDeep(loc);
150 public void accept(ExpressionVisitor visitor) {
155 public void forVariables(VariableProcedure procedure) {
156 procedure.execute(eVar.location, var);
157 pattern.forVariables(procedure);
161 public Expression accept(ExpressionTransformer transformer) {
162 return transformer.transform(this);