]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/LengthVector.java
Merge "Re-enabled Acorn transaction cancellation support for testing"
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / java / LengthVector.java
1 package org.simantics.scl.compiler.elaboration.java;
2
3 import org.cojen.classfile.TypeDesc;
4 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
5 import org.simantics.scl.compiler.constants.FunctionValue;
6 import org.simantics.scl.compiler.internal.codegen.references.Val;
7 import org.simantics.scl.compiler.internal.codegen.utils.Constants;
8 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
9 import org.simantics.scl.compiler.types.TCon;
10 import org.simantics.scl.compiler.types.TVar;
11 import org.simantics.scl.compiler.types.Type;
12 import org.simantics.scl.compiler.types.Types;
13 import org.simantics.scl.compiler.types.exceptions.MatchException;
14 import org.simantics.scl.compiler.types.kinds.Kinds;
15
16 public class LengthVector extends FunctionValue {
17     private static final TVar A = Types.var(Kinds.STAR);
18     private final TCon constructor;  
19     
20     public LengthVector(TCon constructor) {
21         super(new TVar[] {A}, Types.NO_EFFECTS, Types.INTEGER, Types.apply(constructor, A));
22         this.constructor = constructor;
23     }
24     
25     @Override
26     public Type applyExact(MethodBuilder mb, Val[] parameters) {
27         Val vector = parameters[0];        
28         vector.push(mb);
29         
30         try {
31             Type elementType = Types.canonical(
32                     Types.matchApply(constructor, vector.getType()));
33             if(elementType instanceof TVar) {
34                 mb.invokeStatic("java/lang/reflect/Array", "getLength", TypeDesc.INT, 
35                         Constants.OBJECTS[1]);
36                 return Types.INTEGER;
37             }
38             else {
39                 mb.arrayLength();
40                 return Types.INTEGER;
41             }
42         } catch (MatchException e) {
43             throw new InternalCompilerError(e);
44         }
45     }   
46
47 }