Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
numba
GitHub Repository: numba/llvmlite
Path: blob/main/ffi/type.cpp
1154 views
1
#include "core.h"
2
#include "llvm-c/Core.h"
3
#include <string>
4
5
#include <iostream>
6
7
#include "llvm/IR/DerivedTypes.h"
8
#include "llvm/IR/Type.h"
9
10
struct ElementIterator {
11
typedef llvm::ArrayRef<llvm::Type *>::iterator subtype_iterator;
12
subtype_iterator cur;
13
subtype_iterator end;
14
ElementIterator(subtype_iterator cur, subtype_iterator end)
15
: cur(cur), end(end) {}
16
};
17
18
struct OpaqueElementIterator;
19
typedef OpaqueElementIterator *LLVMElementIteratorRef;
20
21
namespace llvm {
22
static LLVMElementIteratorRef wrap(ElementIterator *GI) {
23
return reinterpret_cast<LLVMElementIteratorRef>(GI);
24
}
25
26
static ElementIterator *unwrap(LLVMElementIteratorRef GI) {
27
return reinterpret_cast<ElementIterator *>(GI);
28
}
29
} // namespace llvm
30
31
extern "C" {
32
33
API_EXPORT(LLVMElementIteratorRef)
34
LLVMPY_ElementIter(LLVMTypeRef Val) {
35
using namespace llvm;
36
llvm::Type *ty = llvm::unwrap(Val);
37
auto elements = ty->subtypes();
38
return wrap(new ElementIterator(elements.begin(), elements.end()));
39
}
40
41
API_EXPORT(LLVMTypeRef)
42
LLVMPY_ElementIterNext(LLVMElementIteratorRef GI) {
43
using namespace llvm;
44
ElementIterator *iter = unwrap(GI);
45
if (iter->cur != iter->end) {
46
const Type *ty = *(iter->cur);
47
iter->cur++;
48
return wrap(static_cast<const Type *>(ty));
49
} else {
50
return NULL;
51
}
52
}
53
54
API_EXPORT(void)
55
LLVMPY_DisposeElementIter(LLVMElementIteratorRef GI) {
56
delete llvm::unwrap(GI);
57
}
58
59
API_EXPORT(int)
60
LLVMPY_GetTypeKind(LLVMTypeRef Val) { return (int)LLVMGetTypeKind(Val); }
61
62
API_EXPORT(LLVMTypeRef)
63
LLVMPY_TypeOf(LLVMValueRef Val) { return LLVMTypeOf(Val); }
64
65
API_EXPORT(const char *)
66
LLVMPY_PrintType(LLVMTypeRef type) {
67
char *str = LLVMPrintTypeToString(type);
68
const char *out = LLVMPY_CreateString(str);
69
LLVMDisposeMessage(str);
70
return out;
71
}
72
73
API_EXPORT(const char *)
74
LLVMPY_GetTypeName(LLVMTypeRef type) {
75
// try to convert to a struct type, works for other derived
76
// types too
77
llvm::Type *unwrapped = llvm::unwrap(type);
78
llvm::StructType *ty = llvm::dyn_cast<llvm::StructType>(unwrapped);
79
if (ty && !ty->isLiteral()) {
80
return LLVMPY_CreateString(ty->getStructName().str().c_str());
81
}
82
return LLVMPY_CreateString("");
83
}
84
85
API_EXPORT(bool)
86
LLVMPY_TypeIsPointer(LLVMTypeRef type) {
87
return llvm::unwrap(type)->isPointerTy();
88
}
89
90
API_EXPORT(bool)
91
LLVMPY_TypeIsArray(LLVMTypeRef type) { return llvm::unwrap(type)->isArrayTy(); }
92
93
API_EXPORT(bool)
94
LLVMPY_TypeIsVector(LLVMTypeRef type) {
95
return llvm::unwrap(type)->isVectorTy();
96
}
97
98
API_EXPORT(bool)
99
LLVMPY_TypeIsStruct(LLVMTypeRef type) {
100
return llvm::unwrap(type)->isStructTy();
101
}
102
103
API_EXPORT(bool)
104
LLVMPY_TypeIsFunction(LLVMTypeRef type) {
105
return llvm::unwrap(type)->isFunctionTy();
106
}
107
108
API_EXPORT(bool)
109
LLVMPY_IsFunctionVararg(LLVMTypeRef type) {
110
llvm::Type *unwrapped = llvm::unwrap(type);
111
llvm::FunctionType *ty = llvm::dyn_cast<llvm::FunctionType>(unwrapped);
112
if (ty != nullptr) {
113
return ty->isVarArg();
114
}
115
return false;
116
}
117
118
API_EXPORT(int)
119
LLVMPY_GetTypeElementCount(LLVMTypeRef type) {
120
llvm::Type *unwrapped = llvm::unwrap(type);
121
if (unwrapped->isArrayTy()) {
122
return unwrapped->getArrayNumElements();
123
}
124
if (unwrapped->isVectorTy()) {
125
// Fixed vector: get exact number of elements
126
llvm::FixedVectorType *fixedvec =
127
llvm::dyn_cast<llvm::FixedVectorType>(unwrapped);
128
if (fixedvec != nullptr) {
129
return fixedvec->getNumElements();
130
}
131
132
// Scalable vector: get minimum elements
133
llvm::ScalableVectorType *scalablevec =
134
llvm::dyn_cast<llvm::ScalableVectorType>(unwrapped);
135
if (scalablevec != nullptr) {
136
return scalablevec->getMinNumElements();
137
}
138
}
139
// Not an array nor vector
140
return -1;
141
}
142
143
API_EXPORT(uint64_t)
144
LLVMPY_GetTypeBitWidth(LLVMTypeRef type) {
145
llvm::Type *unwrapped = llvm::unwrap(type);
146
auto size = unwrapped->getPrimitiveSizeInBits();
147
return size.getFixedValue();
148
}
149
150
API_EXPORT(LLVMTypeRef)
151
LLVMPY_GetReturnType(LLVMTypeRef type) { return LLVMGetReturnType(type); }
152
153
API_EXPORT(unsigned)
154
LLVMPY_CountParamTypes(LLVMTypeRef type) { return LLVMCountParamTypes(type); }
155
156
API_EXPORT(void)
157
LLVMPY_GetParamTypes(LLVMTypeRef type, LLVMTypeRef *out_types) {
158
LLVMGetParamTypes(type, out_types);
159
}
160
161
API_EXPORT(bool)
162
LLVMPY_IsPackedStruct(LLVMTypeRef type) { return LLVMIsPackedStruct(type); }
163
164
API_EXPORT(bool)
165
LLVMPY_IsOpaqueStruct(LLVMTypeRef type) { return LLVMIsOpaqueStruct(type); }
166
167
API_EXPORT(bool)
168
LLVMPY_IsLiteralStruct(LLVMTypeRef type) { return LLVMIsLiteralStruct(type); }
169
170
} // end extern "C"
171
172