]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/constants/singletons/NullCheck.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / constants / singletons / NullCheck.java
1 package org.simantics.scl.compiler.constants.singletons;
2
3 import org.objectweb.asm.Label;
4 import org.simantics.scl.compiler.constants.ComparisonFunction;
5 import org.simantics.scl.compiler.constants.FunctionValue;
6 import org.simantics.scl.compiler.internal.codegen.continuations.Cont;
7 import org.simantics.scl.compiler.internal.codegen.references.Val;
8 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
9 import org.simantics.scl.compiler.types.TVar;
10 import org.simantics.scl.compiler.types.Type;
11 import org.simantics.scl.compiler.types.Types;
12 import org.simantics.scl.compiler.types.kinds.Kinds;
13
14 public class NullCheck extends FunctionValue implements ComparisonFunction {
15     private static final TVar A = Types.var(Kinds.STAR);
16     public static final NullCheck INSTANCE = 
17             new NullCheck();
18     
19     private NullCheck() {
20         super(new TVar[] {A}, Types.NO_EFFECTS, Types.BOOLEAN, A);
21     }
22         
23     @Override
24     public Type applyExact(MethodBuilder mb, Val[] parameters) {
25         parameters[0].push(mb);
26         Label join = mb.createLabel();
27         Label isNull = mb.createLabel();
28         mb.ifNullBranch(isNull, true);
29         mb.loadConstant(false);
30         mb.branch(join);
31         mb.setLocation(isNull);
32         mb.loadConstant(true);
33         mb.setLocation(join);
34         return getReturnType();
35     }
36     
37     @Override
38     public void generateCondition(MethodBuilder mb, Val[] parameters, Cont then_, Cont else_) {
39         parameters[0].push(mb);
40         mb.ifNullBranch(mb.getLabel(then_), true);
41         mb.jump(else_);
42         mb.ensureExists(then_);
43     }
44
45     @Override
46     public String toString() {
47          return "nullCheck";
48     }
49 }