]> granicus.if.org Git - llvm/commitdiff
[Go] Subtypes function
authorAndrew Wilkins <axwalk@gmail.com>
Thu, 8 Jun 2017 07:32:29 +0000 (07:32 +0000)
committerAndrew Wilkins <axwalk@gmail.com>
Thu, 8 Jun 2017 07:32:29 +0000 (07:32 +0000)
This patch adds LLVMGetSubtypes to Go API (as Type.Subtypes), tests included.

Patch by Ekaterina Vaartis!

Differential Revision: https://reviews.llvm.org/D33901

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@304968 91177308-0d34-0410-b5e6-96231b3b80d8

bindings/go/llvm/ir.go
bindings/go/llvm/ir_test.go

index fe191beb38132ef501e74178d9acf682a218cd90..2220970343071d09b4cc3906db47ad83d2e375a7 100644 (file)
@@ -611,6 +611,12 @@ func (t Type) StructElementTypes() []Type {
 }
 
 // Operations on array, pointer, and vector types (sequence types)
+func (t Type) Subtypes() (ret []Type) {
+       ret = make([]Type, C.LLVMGetNumContainedTypes(t.C))
+       C.LLVMGetSubtypes(t.C, llvmTypeRefPtr(&ret[0]))
+       return
+}
+
 func ArrayType(elementType Type, elementCount int) (t Type) {
        t.C = C.LLVMArrayType(elementType.C, C.unsigned(elementCount))
        return
index c823615a4293c8a2e2997b9fdbaf0a444e0081a3..325ee4890f4c16fd0545357fe203856c4ee5d31e 100644 (file)
@@ -134,3 +134,29 @@ func TestDebugLoc(t *testing.T) {
                t.Errorf("Got metadata %v as scope, though wanted %v", loc.Scope.C, scope.C)
        }
 }
+
+func TestSubtypes(t *testing.T) {
+       cont := NewContext()
+       defer cont.Dispose()
+
+       int_pointer := PointerType(cont.Int32Type(), 0)
+       int_inner := int_pointer.Subtypes()
+       if len(int_inner) != 1 {
+               t.Errorf("Got size %d, though wanted 1")
+       }
+       if int_inner[0] != cont.Int32Type() {
+               t.Errorf("Expected int32 type")
+       }
+
+       st_pointer := cont.StructType([]Type{cont.Int32Type(), cont.Int8Type()}, false)
+       st_inner := st_pointer.Subtypes()
+       if len(st_inner) != 2 {
+               t.Errorf("Got size %d, though wanted 2")
+       }
+       if st_inner[0] != cont.Int32Type() {
+               t.Errorf("Expected first struct field to be int32")
+       }
+       if st_inner[1] != cont.Int8Type() {
+               t.Errorf("Expected second struct field to be int8")
+       }
+}