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