File tree Expand file tree Collapse file tree 3 files changed +47
-4
lines changed Expand file tree Collapse file tree 3 files changed +47
-4
lines changed Original file line number Diff line number Diff line change @@ -48,3 +48,16 @@ LLVMMemoryBufferRef LLVMGoWriteThinLTOBitcodeToMemoryBuffer(LLVMModuleRef M) {
48
48
#endif
49
49
return llvm::wrap (llvm::MemoryBuffer::getMemBufferCopy (OS.str ()).release ());
50
50
}
51
+
52
+ #if LLVM_VERSION_MAJOR < 15
53
+
54
+ // This is backported because version 14 supports opaque
55
+ // pointers but I presume if you try to access the element
56
+ // type it will crash. So this backport prevents that by
57
+ // allowing to check if the pointer is opaque before trying
58
+ // to access the element type.
59
+ LLVMBool LLVMPointerTypeIsOpaque (LLVMTypeRef Ty) {
60
+ return llvm::unwrap<llvm::Type>(Ty)->isOpaquePointerTy ();
61
+ }
62
+
63
+ #endif
Original file line number Diff line number Diff line change @@ -14,6 +14,19 @@ LLVMMemoryBufferRef LLVMGoWriteThinLTOBitcodeToMemoryBuffer(LLVMModuleRef M);
14
14
LLVMMetadataRef LLVMGoDIBuilderCreateExpression (LLVMDIBuilderRef Builder ,
15
15
uint64_t * Addr , size_t Length );
16
16
17
+ #if LLVM_VERSION_MAJOR < 15
18
+ /**
19
+ * Determine whether a pointer is opaque.
20
+ *
21
+ * True if this is an instance of an opaque PointerType.
22
+ *
23
+ * @see llvm::Type::isOpaquePointerTy()
24
+ */
25
+ LLVMBool LLVMPointerTypeIsOpaque (LLVMTypeRef Ty );
26
+
27
+ #endif
28
+
29
+
17
30
#ifdef __cplusplus
18
31
}
19
32
#endif /* defined(__cplusplus) */
Original file line number Diff line number Diff line change @@ -19,8 +19,10 @@ package llvm
19
19
#include <stdlib.h>
20
20
*/
21
21
import "C"
22
- import "unsafe"
23
- import "errors"
22
+ import (
23
+ "errors"
24
+ "unsafe"
25
+ )
24
26
25
27
type (
26
28
// We use these weird structs here because *Ref types are pointers and
@@ -700,8 +702,23 @@ func VectorType(elementType Type, elementCount int) (t Type) {
700
702
return
701
703
}
702
704
703
- func (t Type ) IsPointerOpaque () bool { return C .LLVMPointerTypeIsOpaque (t .C ) != 0 }
704
- func (t Type ) ElementType () (rt Type ) { rt .C = C .LLVMGetElementType (t .C ); return }
705
+ // IsPointerOpaque checks if the pointer is an opaque pointer.
706
+ //
707
+ // see llvm::Type::isOpaquePointerTy()
708
+ func (t Type ) IsPointerOpaque () bool { return C .LLVMPointerTypeIsOpaque (t .C ) != 0 }
709
+
710
+ // ElementType returns the type of the element for arrays, pointers and vectors.
711
+ //
712
+ // see llvm::SequentialType::getElementType()
713
+ func (t Type ) ElementType () (rt Type ) {
714
+ if t .IsPointerOpaque () {
715
+ // avoid segfault
716
+ return Type {}
717
+ }
718
+ rt .C = C .LLVMGetElementType (t .C )
719
+ return
720
+ }
721
+
705
722
func (t Type ) ArrayLength () int { return int (C .LLVMGetArrayLength (t .C )) }
706
723
func (t Type ) PointerAddressSpace () int { return int (C .LLVMGetPointerAddressSpace (t .C )) }
707
724
func (t Type ) VectorSize () int { return int (C .LLVMGetVectorSize (t .C )) }
You can’t perform that action at this time.
0 commit comments