whitespace
[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 #ifndef LLVM_WRAP_H
15 #define LLVM_WRAP_H
16
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"
23
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.
28
29   Need these includes to support the LLVM 'cast' template for the C++ 'wrap' 
30   and 'unwrap' conversion functions. */
31
32 namespace llvm {
33   class MemoryBuffer;
34   class PassManagerBase;
35   struct GenericValue;
36   class ExecutionEngine;
37
38   #define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)   \
39     inline ty *unwrap(ref P) {                          \
40       return reinterpret_cast<ty*>(P);                  \
41     }                                                   \
42                                                         \
43     inline ref wrap(const ty *P) {                      \
44       return reinterpret_cast<ref>(const_cast<ty*>(P)); \
45     }
46   
47   #define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref)      \
48     DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)         \
49                                                         \
50     template<typename T>                                \
51     inline T *unwrap(ref P) {                           \
52       return cast<T>(unwrap(P));                        \
53     }
54   
55   #define DEFINE_STDCXX_CONVERSION_FUNCTIONS(ty, ref)   \
56     DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)         \
57                                                         \
58     template<typename T>                                \
59     inline T *unwrap(ref P) {                           \
60       T *Q = (T*)unwrap(P);                             \
61       assert(Q && "Invalid cast!");                     \
62       return Q;                                         \
63     }
64   
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  )
75
76   /* LLVMModuleProviderRef exists for historical reasons, but now just holds a
77    * Module.
78    */
79   inline Module *unwrap(LLVMModuleProviderRef MP) {
80     return reinterpret_cast<Module*>(MP);
81   }
82   
83   /* Specialized opaque context conversions.
84    */
85   inline LLVMContext **unwrap(LLVMContextRef* Tys) {
86     return reinterpret_cast<LLVMContext**>(Tys);
87   }
88   
89   inline LLVMContextRef *wrap(const LLVMContext **Tys) {
90     return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));
91   }
92   
93   /* Specialized opaque type conversions.
94    */
95   inline Type **unwrap(LLVMTypeRef* Tys) {
96     return reinterpret_cast<Type**>(Tys);
97   }
98   
99   inline LLVMTypeRef *wrap(Type **Tys) {
100     return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
101   }
102   
103   /* Specialized opaque value conversions.
104    */ 
105   inline Value **unwrap(LLVMValueRef *Vals) {
106     return reinterpret_cast<Value**>(Vals);
107   }
108   
109   template<typename T>
110   inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
111     #ifdef DEBUG
112     for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
113       cast<T>(*I);
114     #endif
115     (void)Length;
116     return reinterpret_cast<T**>(Vals);
117   }
118   
119   inline LLVMValueRef *wrap(const Value **Vals) {
120     return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
121   }
122 }
123
124 #endif