]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/GetVector.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / java / GetVector.java
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/GetVector.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/GetVector.java
new file mode 100644 (file)
index 0000000..29edcfc
--- /dev/null
@@ -0,0 +1,50 @@
+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.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 GetVector extends FunctionValue {
+    private static final TVar A = Types.var(Kinds.STAR);  
+    private final TCon constructor;
+    
+    public GetVector(Type effect, TCon constructor) {
+        super(new TVar[] {A}, effect, A, Types.apply(constructor, A), Types.INTEGER);
+        this.constructor = constructor;
+    }
+    
+    @Override
+    public Type applyExact(MethodBuilder mb, Val[] parameters) {
+        Val vector = parameters[0];
+        Val index = parameters[1];
+        
+        vector.push(mb);
+        index.push(mb);
+        
+        try {
+            Type elementType = Types.canonical(
+                    Types.matchApply(constructor, vector.getType()));
+            if(elementType instanceof TVar) {
+                mb.invokeStatic("java/lang/reflect/Array", "get", TypeDesc.OBJECT, 
+                        new TypeDesc[] {TypeDesc.OBJECT, TypeDesc.INT});
+                return A;
+            }
+            else {
+                TypeDesc desc = mb.getJavaTypeTranslator().toTypeDesc(elementType);
+                mb.loadFromArray(desc);
+                return elementType;
+            }
+        } catch (MatchException e) {
+            throw new InternalCompilerError(e);
+        }
+    }   
+
+}