llvm.org GIT mirror llvm / 68cc6b6
[LLVM-C] [OCaml] Expose Type::subtypes. The C functions added are LLVMGetNumContainedTypes and LLVMGetSubtypes. The OCaml function added is Llvm.subtypes. Patch by Ekaterina Vaartis. Differential Revision: https://reviews.llvm.org/D33677 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@304709 91177308-0d34-0410-b5e6-96231b3b80d8 whitequark 2 years ago
6 changed file(s) with 56 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
458458 external is_opaque : lltype -> bool = "llvm_is_opaque"
459459
460460 (*--... Operations on pointer, vector, and array types .....................--*)
461
462 external subtypes : lltype -> lltype array = "llvm_subtypes"
461463 external array_type : lltype -> int -> lltype = "llvm_array_type"
462464 external pointer_type : lltype -> lltype = "llvm_pointer_type"
463465 external qualified_pointer_type : lltype -> int -> lltype
657657
658658 (** {7 Operations on pointer, vector, and array types} *)
659659
660 (** [subtypes ty] returns [ty]'s subtypes *)
661 val subtypes : lltype -> lltype array
662
660663 (** [array_type ty n] returns the array type containing [n] elements of type
661664 [ty]. See the method [llvm::ArrayType::get]. *)
662665 val array_type : lltype -> int -> lltype
505505
506506 /*--... Operations on array, pointer, and vector types .....................--*/
507507
508 /* lltype -> lltype array */
509 CAMLprim value llvm_subtypes(LLVMTypeRef Ty) {
510 CAMLparam0();
511 CAMLlocal1(Arr);
512
513 unsigned Size = LLVMGetNumContainedTypes(Ty);
514
515 Arr = caml_alloc(Size, 0);
516
517 LLVMGetSubtypes(Ty, (LLVMTypeRef *) Arr);
518
519 CAMLreturn(Arr);
520 }
521
508522 /* lltype -> int -> lltype */
509523 CAMLprim LLVMTypeRef llvm_array_type(LLVMTypeRef ElementTy, value Count) {
510524 return LLVMArrayType(ElementTy, Int_val(Count));
10371037 * @see llvm::SequentialType::getElementType()
10381038 */
10391039 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty);
1040
1041 /**
1042 * Returns type's subtypes
1043 *
1044 * @see llvm::Type::subtypes()
1045 */
1046 void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr);
1047
1048 /**
1049 * Return the number of types in the derived type.
1050 *
1051 * @see llvm::Type::getNumContainedTypes()
1052 */
1053 unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp);
10401054
10411055 /**
10421056 * Create a fixed size array type that refers to a specific type.
567567
568568 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
569569
570 void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) {
571 int i = 0;
572 for (auto *T : unwrap(Tp)->subtypes()) {
573 Arr[i] = wrap(T);
574 i++;
575 }
576 }
577
570578 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
571579 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
572580 }
584592 if (auto *PTy = dyn_cast(Ty))
585593 return wrap(PTy->getElementType());
586594 return wrap(cast(Ty)->getElementType());
595 }
596
597 unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) {
598 return unwrap(Tp)->getNumContainedTypes();
587599 }
588600
589601 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
6464
6565 let filename = Sys.argv.(1)
6666 let m = create_module context filename
67
68 (*===-- Contained types --------------------------------------------------===*)
69
70 let test_contained_types () =
71 let pointer_i32 = pointer_type i32_type in
72 insist (i32_type = (Array.get (subtypes pointer_i32) 0));
73
74 let ar = struct_type context [| i32_type; i8_type |] in
75 insist (i32_type = (Array.get (subtypes ar)) 0);
76 insist (i8_type = (Array.get (subtypes ar)) 1)
6777
6878
6979 (*===-- Conversion --------------------------------------------------------===*)
15321542 (*===-- Driver ------------------------------------------------------------===*)
15331543
15341544 let _ =
1545 suite "contained types" test_contained_types;
15351546 suite "conversion" test_conversion;
15361547 suite "target" test_target;
15371548 suite "constants" test_constants;