11ea6e46c458fc3222f1ee31cab4f13e0fd34ea8
[oota-llvm.git] / include / llvm / Wrap.h
1 //===- llvm/Wrap.h - C++ Type Wrapping for the C Interface  -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the wrapping functions for the C interface.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
21
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.
26
27   Need these includes to support the LLVM 'cast' template for the C++ 'wrap' 
28   and 'unwrap' conversion functions. */
29
30 namespace llvm {
31   class MemoryBuffer;
32   class PassManagerBase;
33   struct GenericValue;
34   class ExecutionEngine;
35
36   #define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)   \
37     inline ty *unwrap(ref P) {                          \
38       return reinterpret_cast<ty*>(P);                  \
39     }                                                   \
40                                                         \
41     inline ref wrap(const ty *P) {                      \
42       return reinterpret_cast<ref>(const_cast<ty*>(P)); \
43     }
44   
45   #define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref)      \
46     DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)         \
47                                                         \
48     template<typename T>                                \
49     inline T *unwrap(ref P) {                           \
50       return cast<T>(unwrap(P));                        \
51     }
52   
53   #define DEFINE_STDCXX_CONVERSION_FUNCTIONS(ty, ref)   \
54     DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)         \
55                                                         \
56     template<typename T>                                \
57     inline T *unwrap(ref P) {                           \
58       T *Q = (T*)unwrap(P);                             \
59       assert(Q && "Invalid cast!");                     \
60       return Q;                                         \
61     }
62   
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  )
74
75   /* LLVMModuleProviderRef exists for historical reasons, but now just holds a
76    * Module.
77    */
78   inline Module *unwrap(LLVMModuleProviderRef MP) {
79     return reinterpret_cast<Module*>(MP);
80   }
81   
82   /* Specialized opaque context conversions.
83    */
84   inline LLVMContext **unwrap(LLVMContextRef* Tys) {
85     return reinterpret_cast<LLVMContext**>(Tys);
86   }
87   
88   inline LLVMContextRef *wrap(const LLVMContext **Tys) {
89     return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));
90   }
91   
92   /* Specialized opaque type conversions.
93    */
94   inline Type **unwrap(LLVMTypeRef* Tys) {
95     return reinterpret_cast<Type**>(Tys);
96   }
97   
98   inline LLVMTypeRef *wrap(Type **Tys) {
99     return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
100   }
101   
102   /* Specialized opaque value conversions.
103    */ 
104   inline Value **unwrap(LLVMValueRef *Vals) {
105     return reinterpret_cast<Value**>(Vals);
106   }
107   
108   template<typename T>
109   inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
110     #ifdef DEBUG
111     for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
112       cast<T>(*I);
113     #endif
114     (void)Length;
115     return reinterpret_cast<T**>(Vals);
116   }
117   
118   inline LLVMValueRef *wrap(const Value **Vals) {
119     return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
120   }
121 }