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 //===----------------------------------------------------------------------===//
14 #include "llvm-c/Core.h"
15 #include "llvm-c/ExecutionEngine.h"
16 #include "llvm/IR/BasicBlock.h"
17 #include "llvm/IR/IRBuilder.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/IR/Type.h"
20 #include "llvm/PassRegistry.h"
22 /* When included into a C++ source file, also declares 'wrap' and 'unwrap'
23 helpers to perform opaque reference<-->pointer conversions. These helpers
24 are shorter and more tightly typed than writing the casts by hand when
25 authoring bindings. In assert builds, they will do runtime type checking.
27 Need these includes to support the LLVM 'cast' template for the C++ 'wrap'
28 and 'unwrap' conversion functions. */
32 class PassManagerBase;
34 class ExecutionEngine;
36 #define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref) \
37 inline ty *unwrap(ref P) { \
38 return reinterpret_cast<ty*>(P); \
41 inline ref wrap(const ty *P) { \
42 return reinterpret_cast<ref>(const_cast<ty*>(P)); \
45 #define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref) \
46 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref) \
48 template<typename T> \
49 inline T *unwrap(ref P) { \
50 return cast<T>(unwrap(P)); \
53 #define DEFINE_STDCXX_CONVERSION_FUNCTIONS(ty, ref) \
54 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref) \
56 template<typename T> \
57 inline T *unwrap(ref P) { \
58 T *Q = (T*)unwrap(P); \
59 assert(Q && "Invalid cast!"); \
63 DEFINE_ISA_CONVERSION_FUNCTIONS (Type, LLVMTypeRef )
64 DEFINE_ISA_CONVERSION_FUNCTIONS (Value, LLVMValueRef )
65 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Module, LLVMModuleRef )
66 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef )
67 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef )
68 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef )
69 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef )
70 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use, LLVMUseRef )
71 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionEngine, LLVMExecutionEngineRef)
72 DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBase, LLVMPassManagerRef )
73 DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassRegistry, LLVMPassRegistryRef )
75 /* LLVMModuleProviderRef exists for historical reasons, but now just holds a
78 inline Module *unwrap(LLVMModuleProviderRef MP) {
79 return reinterpret_cast<Module*>(MP);
82 /* Specialized opaque context conversions.
84 inline LLVMContext **unwrap(LLVMContextRef* Tys) {
85 return reinterpret_cast<LLVMContext**>(Tys);
88 inline LLVMContextRef *wrap(const LLVMContext **Tys) {
89 return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));
92 /* Specialized opaque type conversions.
94 inline Type **unwrap(LLVMTypeRef* Tys) {
95 return reinterpret_cast<Type**>(Tys);
98 inline LLVMTypeRef *wrap(Type **Tys) {
99 return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
102 /* Specialized opaque value conversions.
104 inline Value **unwrap(LLVMValueRef *Vals) {
105 return reinterpret_cast<Value**>(Vals);
109 inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
111 for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
115 return reinterpret_cast<T**>(Vals);
118 inline LLVMValueRef *wrap(const Value **Vals) {
119 return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));