]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/DynamicConstructor.java
(refs #7414) Added Dynamic constructor
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / java / DynamicConstructor.java
1 package org.simantics.scl.compiler.elaboration.java;
2
3 import org.cojen.classfile.TypeDesc;
4 import org.objectweb.asm.Label;
5 import org.simantics.scl.compiler.constants.FunctionValue;
6 import org.simantics.scl.compiler.constants.LocalVariableConstant;
7 import org.simantics.scl.compiler.internal.codegen.continuations.Cont;
8 import org.simantics.scl.compiler.internal.codegen.references.IVal;
9 import org.simantics.scl.compiler.internal.codegen.references.Val;
10 import org.simantics.scl.compiler.internal.codegen.utils.LocalVariable;
11 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
12 import org.simantics.scl.compiler.types.TVar;
13 import org.simantics.scl.compiler.types.Type;
14 import org.simantics.scl.compiler.types.Types;
15 import org.simantics.scl.compiler.types.kinds.Kinds;
16
17 /**
18  * Dynamic :: a -> Dynamic
19  */
20 public class DynamicConstructor extends FunctionValue {
21     private static final TVar A = Types.var(Kinds.STAR);
22     public static final DynamicConstructor INSTANCE = new DynamicConstructor();
23     
24     private DynamicConstructor() {
25         super(new TVar[] {A}, Types.NO_EFFECTS, Types.DYNAMIC, A);
26     }
27
28     @Override
29     public Type applyExact(MethodBuilder mb, Val[] parameters) {
30         mb.pushBoxed(parameters[0]);
31         return Types.DYNAMIC;
32     }
33
34     @Override
35     public void deconstruct(MethodBuilder mb, IVal parameter, Cont success, Label failure) {
36         Type expectedType = success.getParameterType(0);
37         TypeDesc expectedTypeDesc = mb.getJavaTypeTranslator().toTypeDesc(expectedType);
38         TypeDesc expectedObjectTypeDesc = expectedTypeDesc.toObjectType();
39         LocalVariable cachedParameter = mb.cacheValue(parameter, Types.DYNAMIC);
40         mb.loadLocal(cachedParameter);
41         mb.instanceOf(expectedObjectTypeDesc);
42         mb.ifZeroComparisonBranch(failure, "==");
43         
44         mb.loadLocal(cachedParameter);
45         mb.checkCast(expectedObjectTypeDesc);
46         mb.unbox(expectedType);
47         LocalVariable casted = mb.createLocalVariable("dynamicContent", expectedTypeDesc); 
48         mb.storeLocal(casted);
49         mb.jump(success, new LocalVariableConstant(expectedType, casted));
50     }
51 }