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.TIntHashSet;
18 public class EAsPattern extends Expression {
22 public Expression pattern;
24 public EAsPattern(EVar eVar, Expression pattern) {
26 this.pattern = pattern;
29 public EAsPattern(Variable var, Expression pattern) {
31 this.pattern = pattern;
34 public Variable getVariable() {
38 public Expression getPattern() {
43 public void collectVars(TObjectIntHashMap<Variable> allVars,
45 int id = allVars.get(var);
48 pattern.collectVars(allVars, vars);
52 public Expression simplify(SimplificationContext context) {
53 pattern = pattern.simplify(context);
58 public Expression resolve(TranslationContext context) {
59 context.getErrorLog().log(location, "As binding (@) can be used only in patterns.");
64 public Expression resolveAsPattern(TranslationContext context) {
65 var = context.newVariable(eVar.name);
66 pattern = pattern.resolveAsPattern(context);
71 protected void updateType() throws MatchException {
72 setType(pattern.getType());
76 public IVal toVal(CompilationContext context, CodeWriter w) {
77 throw new InternalCompilerError("EAsPattern allowed only in patterns.");
81 public Expression inferType(TypingContext context) {
82 pattern = pattern.inferType(context);
83 var.setType(pattern.getType());
88 public Expression checkBasicType(TypingContext context, Type requiredType) {
89 pattern = pattern.checkBasicType(context, requiredType);
90 var.setType(pattern.getType());
95 public Expression replace(ReplaceContext context) {
96 if(!context.inPattern)
97 throw new IllegalStateException();
99 Variable newVariable = new Variable(var.name);
100 newVariable.setType(var.getType().replace(context.tvarMap));
101 context.varMap.put(var, new EVariable(newVariable));
103 EAsPattern result = new EAsPattern(eVar, pattern.replace(context));
104 result.var = newVariable;
110 public void setLocationDeep(long loc) {
111 if(location == Locations.NO_LOCATION) {
113 pattern.setLocationDeep(loc);
115 eVar.setLocationDeep(loc);
120 public void accept(ExpressionVisitor visitor) {
125 public Expression accept(ExpressionTransformer transformer) {
126 return transformer.transform(this);