1 //===- llvm/Wrap.h - C++ Type Wrapping for the C Interface -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the wrapping functions for the C interface.
12 //===----------------------------------------------------------------------===//
17 #include "llvm-c/Core.h"
18 #include "llvm/IR/BasicBlock.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/Type.h"
22 #include "llvm/PassRegistry.h"
24 /* When included into a C++ source file, also declares 'wrap' and 'unwrap'
25 helpers to perform opaque reference<-->pointer conversions. These helpers
26 are shorter and more tightly typed than writing the casts by hand when
27 authoring bindings. In assert builds, they will do runtime type checking.
29 Need these includes to support the LLVM 'cast' template for the C++ 'wrap'
30 and 'unwrap' conversion functions. */
34 class PassManagerBase;
36 class ExecutionEngine;
38 #define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref) \
39 inline ty *unwrap(ref P) { \
40 return reinterpret_cast<ty*>(P); \
43 inline ref wrap(const ty *P) { \
44 return reinterpret_cast<ref>(const_cast<ty*>(P)); \
47 #define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref) \
48 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref) \
50 template<typename T> \
51 inline T *unwrap(ref P) { \
52 return cast<T>(unwrap(P)); \
55 #define DEFINE_STDCXX_CONVERSION_FUNCTIONS(ty, ref) \
56 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref) \
58 template<typename T> \
59 inline T *unwrap(ref P) { \
60 T *Q = (T*)unwrap(P); \
61 assert(Q && "Invalid cast!"); \
65 DEFINE_ISA_CONVERSION_FUNCTIONS (Type, LLVMTypeRef )
66 DEFINE_ISA_CONVERSION_FUNCTIONS (Value, LLVMValueRef )
67 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Module, LLVMModuleRef )
68 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef )
69 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef )
70 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef )
71 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef )
72 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef )
73 DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBase, LLVMPassManagerRef )
74 DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassRegistry, LLVMPassRegistryRef )
76 /* LLVMModuleProviderRef exists for historical reasons, but now just holds a
79 inline Module *unwrap(LLVMModuleProviderRef MP) {
80 return reinterpret_cast<Module*>(MP);
83 /* Specialized opaque context conversions.
85 inline LLVMContext **unwrap(LLVMContextRef* Tys) {
86 return reinterpret_cast<LLVMContext**>(Tys);
89 inline LLVMContextRef *wrap(const LLVMContext **Tys) {
90 return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));
93 /* Specialized opaque type conversions.
95 inline Type **unwrap(LLVMTypeRef* Tys) {
96 return reinterpret_cast<Type**>(Tys);
99 inline LLVMTypeRef *wrap(Type **Tys) {
100 return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
103 /* Specialized opaque value conversions.
105 inline Value **unwrap(LLVMValueRef *Vals) {
106 return reinterpret_cast<Value**>(Vals);
110 inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
112 for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
116 return reinterpret_cast<T**>(Vals);
119 inline LLVMValueRef *wrap(const Value **Vals) {
120 return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));