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.types.Type;
13 import org.simantics.scl.compiler.types.exceptions.MatchException;
15 import gnu.trove.map.hash.TObjectIntHashMap;
16 import gnu.trove.set.hash.THashSet;
17 import gnu.trove.set.hash.TIntHashSet;
19 public class EAsPattern extends Expression {
25 public EAsPattern(EVar eVar, Expression pattern) {
27 this.pattern = pattern;
30 public EAsPattern(Variable var, Expression pattern) {
32 this.pattern = pattern;
35 public Variable getVariable() {
39 public Expression getPattern() {
44 public void collectRefs(TObjectIntHashMap<Object> allRefs,
46 pattern.collectRefs(allRefs, refs);
50 public void collectVars(TObjectIntHashMap<Variable> allVars,
52 int id = allVars.get(var);
55 pattern.collectVars(allVars, vars);
59 public void collectFreeVariables(THashSet<Variable> vars) {
60 throw new InternalCompilerError(location, "Cannot collect free variables for a pattern.");
64 public void removeFreeVariables(THashSet<Variable> vars) {
66 pattern.removeFreeVariables(vars);
70 public Expression simplify(SimplificationContext context) {
71 pattern = pattern.simplify(context);
76 public Expression resolve(TranslationContext context) {
77 context.getErrorLog().log(location, "As binding (@) can be used only in patterns.");
82 public Expression resolveAsPattern(TranslationContext context) {
83 var = context.newVariable(eVar.name);
84 pattern = pattern.resolveAsPattern(context);
89 protected void updateType() throws MatchException {
90 setType(pattern.getType());
94 public IVal toVal(CompilationContext context, CodeWriter w) {
95 throw new InternalCompilerError("EAsPattern allowed only in patterns.");
99 public Expression inferType(TypingContext context) {
100 pattern = pattern.inferType(context);
101 var.setType(pattern.getType());
106 public Expression checkBasicType(TypingContext context, Type requiredType) {
107 pattern = pattern.checkBasicType(context, requiredType);
108 var.setType(pattern.getType());
113 public Expression replace(ReplaceContext context) {
114 if(!context.inPattern)
115 throw new IllegalStateException();
117 Variable newVariable = new Variable(var.name);
118 newVariable.setType(var.getType().replace(context.tvarMap));
119 context.varMap.put(var, new EVariable(newVariable));
121 EAsPattern result = new EAsPattern(eVar, pattern.replace(context));
122 result.var = newVariable;
128 public void collectEffects(THashSet<Type> effects) {
129 pattern.collectEffects(effects);
133 public void setLocationDeep(long loc) {
134 if(location == Locations.NO_LOCATION) {
136 pattern.setLocationDeep(loc);
138 eVar.setLocationDeep(loc);
143 public void accept(ExpressionVisitor visitor) {
148 public void forVariables(VariableProcedure procedure) {
149 procedure.execute(eVar.location, var);
150 pattern.forVariables(procedure);
154 public Expression accept(ExpressionTransformer transformer) {
155 return transformer.transform(this);