]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/CreateMVector.java
Adding Marker-support to Logging-module
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / java / CreateMVector.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.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;
14
15 public class CreateMVector extends FunctionValue {
16     private static final TVar A = Types.var(Kinds.STAR);
17     public static final CreateMVector INSTANCE = new CreateMVector();    
18     
19     private CreateMVector() {
20         super(new TVar[] {A}, Types.PROC, Types.mvector(A), Types.pred(Types.VEC_COMP, A), Types.INTEGER);
21     }
22     
23     @Override
24     public Type applyExact(MethodBuilder mb, Val[] parameters) {
25         Val constraintVar = parameters[0];
26         Val lengthVar = parameters[1];
27
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);
35             lengthVar.push(mb);
36             mb.invokeStatic("java/lang/reflect/Array", "newInstance", TypeDesc.OBJECT, 
37                     new TypeDesc[] {Constants.CLASS, TypeDesc.INT});
38
39             return Types.mvector(A);
40         }
41         else {
42             TypeDesc desc = mb.getJavaTypeTranslator().toTypeDesc(elementType);
43             TypeDesc arrayDesc = desc.toArrayType();      
44
45             lengthVar.push(mb);
46             mb.newObject(arrayDesc, 1);                                
47
48             return Types.mvector(elementType);
49         }
50     }   
51
52 }