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 {
23 public Expression pattern;
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 collectVars(TObjectIntHashMap<Variable> allVars,
46 int id = allVars.get(var);
49 pattern.collectVars(allVars, vars);
53 public void collectFreeVariables(THashSet<Variable> vars) {
54 throw new InternalCompilerError(location, "Cannot collect free variables for a pattern.");
58 public void removeFreeVariables(THashSet<Variable> vars) {
60 pattern.removeFreeVariables(vars);
64 public Expression simplify(SimplificationContext context) {
65 pattern = pattern.simplify(context);
70 public Expression resolve(TranslationContext context) {
71 context.getErrorLog().log(location, "As binding (@) can be used only in patterns.");
76 public Expression resolveAsPattern(TranslationContext context) {
77 var = context.newVariable(eVar.name);
78 pattern = pattern.resolveAsPattern(context);
83 protected void updateType() throws MatchException {
84 setType(pattern.getType());
88 public IVal toVal(CompilationContext context, CodeWriter w) {
89 throw new InternalCompilerError("EAsPattern allowed only in patterns.");
93 public Expression inferType(TypingContext context) {
94 pattern = pattern.inferType(context);
95 var.setType(pattern.getType());
100 public Expression checkBasicType(TypingContext context, Type requiredType) {
101 pattern = pattern.checkBasicType(context, requiredType);
102 var.setType(pattern.getType());
107 public Expression replace(ReplaceContext context) {
108 if(!context.inPattern)
109 throw new IllegalStateException();
111 Variable newVariable = new Variable(var.name);
112 newVariable.setType(var.getType().replace(context.tvarMap));
113 context.varMap.put(var, new EVariable(newVariable));
115 EAsPattern result = new EAsPattern(eVar, pattern.replace(context));
116 result.var = newVariable;
122 public void setLocationDeep(long loc) {
123 if(location == Locations.NO_LOCATION) {
125 pattern.setLocationDeep(loc);
127 eVar.setLocationDeep(loc);
132 public void accept(ExpressionVisitor visitor) {
137 public Expression accept(ExpressionTransformer transformer) {
138 return transformer.transform(this);