]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/GetVector.java
Builtins and JavaModule SCL modules may leak memory
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / java / GetVector.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.MethodBuilder;
8 import org.simantics.scl.compiler.types.TCon;
9 import org.simantics.scl.compiler.types.TVar;
10 import org.simantics.scl.compiler.types.Type;
11 import org.simantics.scl.compiler.types.Types;
12 import org.simantics.scl.compiler.types.exceptions.MatchException;
13 import org.simantics.scl.compiler.types.kinds.Kinds;
14
15 public class GetVector extends FunctionValue {
16     private static final TVar A = Types.var(Kinds.STAR);  
17     private final TCon constructor;
18     
19     public GetVector(Type effect, TCon constructor) {
20         super(new TVar[] {A}, effect, A, Types.apply(constructor, A), Types.INTEGER);
21         this.constructor = constructor;
22     }
23     
24     @Override
25     public Type applyExact(MethodBuilder mb, Val[] parameters) {
26         Val vector = parameters[0];
27         Val index = parameters[1];
28         
29         vector.push(mb);
30         index.push(mb);
31         
32         try {
33             Type elementType = Types.canonical(
34                     Types.matchApply(constructor, vector.getType()));
35             if(elementType instanceof TVar) {
36                 mb.invokeStatic("java/lang/reflect/Array", "get", TypeDesc.OBJECT, 
37                         new TypeDesc[] {TypeDesc.OBJECT, TypeDesc.INT});
38                 return A;
39             }
40             else {
41                 TypeDesc desc = mb.getJavaTypeTranslator().toTypeDesc(elementType);
42                 mb.loadFromArray(desc);
43                 return elementType;
44             }
45         } catch (MatchException e) {
46             throw new InternalCompilerError(e);
47         }
48     }   
49
50 }