1 package org.simantics.scl.compiler.elaboration.java;
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.TPred;
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.kinds.Kinds;
15 public class CreateMVector extends FunctionValue {
16 private static final TVar A = Types.var(Kinds.STAR);
17 public static final CreateMVector INSTANCE = new CreateMVector();
19 private CreateMVector() {
20 super(new TVar[] {A}, Types.PROC, Types.mvector(A), Types.pred(Types.VEC_COMP, A), Types.INTEGER);
24 public Type applyExact(MethodBuilder mb, Val[] parameters) {
25 Val constraintVar = parameters[0];
26 Val lengthVar = parameters[1];
28 Type pred_ = Types.canonical(constraintVar.getType());
29 if(!(pred_ instanceof TPred))
30 throw new InternalCompilerError();
31 TPred pred = (TPred)pred_;
32 Type elementType = pred.parameters[0];
33 if(elementType instanceof TVar) {
34 constraintVar.push(mb);
36 mb.invokeStatic("java/lang/reflect/Array", "newInstance", TypeDesc.OBJECT,
37 new TypeDesc[] {Constants.CLASS, TypeDesc.INT});
39 return Types.mvector(A);
42 TypeDesc desc = mb.getJavaTypeTranslator().toTypeDesc(elementType);
43 TypeDesc arrayDesc = desc.toArrayType();
46 mb.newObject(arrayDesc, 1);
48 return Types.mvector(elementType);