X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Felaboration%2Fjava%2FLengthVector.java;fp=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Felaboration%2Fjava%2FLengthVector.java;h=7d747664bfd4885eded9ec62629b601586bb3688;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/LengthVector.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/LengthVector.java new file mode 100644 index 000000000..7d747664b --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/LengthVector.java @@ -0,0 +1,47 @@ +package org.simantics.scl.compiler.elaboration.java; + +import org.cojen.classfile.TypeDesc; +import org.simantics.scl.compiler.common.exceptions.InternalCompilerError; +import org.simantics.scl.compiler.constants.FunctionValue; +import org.simantics.scl.compiler.internal.codegen.references.Val; +import org.simantics.scl.compiler.internal.codegen.utils.Constants; +import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder; +import org.simantics.scl.compiler.types.TCon; +import org.simantics.scl.compiler.types.TVar; +import org.simantics.scl.compiler.types.Type; +import org.simantics.scl.compiler.types.Types; +import org.simantics.scl.compiler.types.exceptions.MatchException; +import org.simantics.scl.compiler.types.kinds.Kinds; + +public class LengthVector extends FunctionValue { + private static final TVar A = Types.var(Kinds.STAR); + private final TCon constructor; + + public LengthVector(TCon constructor) { + super(new TVar[] {A}, Types.NO_EFFECTS, Types.INTEGER, Types.apply(constructor, A)); + this.constructor = constructor; + } + + @Override + public Type applyExact(MethodBuilder mb, Val[] parameters) { + Val vector = parameters[0]; + vector.push(mb); + + try { + Type elementType = Types.canonical( + Types.matchApply(constructor, vector.getType())); + if(elementType instanceof TVar) { + mb.invokeStatic("java/lang/reflect/Array", "getLength", TypeDesc.INT, + Constants.OBJECTS[1]); + return Types.INTEGER; + } + else { + mb.arrayLength(); + return Types.INTEGER; + } + } catch (MatchException e) { + throw new InternalCompilerError(e); + } + } + +}