Fix dependency layering issues caused by r180112.
[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/IR/BasicBlock.h"
16 #include "llvm/IR/IRBuilder.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/Type.h"
19 #include "llvm/PassRegistry.h"
20
21 /* When included into a C++ source file, also declares 'wrap' and 'unwrap'
22   helpers to perform opaque reference<-->pointer conversions. These helpers
23   are shorter and more tightly typed than writing the casts by hand when
24   authoring bindings. In assert builds, they will do runtime type checking.
25
26   Need these includes to support the LLVM 'cast' template for the C++ 'wrap' 
27   and 'unwrap' conversion functions. */
28
29 namespace llvm {
30   class MemoryBuffer;
31   class PassManagerBase;
32   struct GenericValue;
33   class ExecutionEngine;
34
35   #define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)   \
36     inline ty *unwrap(ref P) {                          \
37       return reinterpret_cast<ty*>(P);                  \
38     }                                                   \
39                                                         \
40     inline ref wrap(const ty *P) {                      \
41       return reinterpret_cast<ref>(const_cast<ty*>(P)); \
42     }
43   
44   #define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref)      \
45     DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)         \
46                                                         \
47     template<typename T>                                \
48     inline T *unwrap(ref P) {                           \
49       return cast<T>(unwrap(P));                        \
50     }
51   
52   #define DEFINE_STDCXX_CONVERSION_FUNCTIONS(ty, ref)   \
53     DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)         \
54                                                         \
55     template<typename T>                                \
56     inline T *unwrap(ref P) {                           \
57       T *Q = (T*)unwrap(P);                             \
58       assert(Q && "Invalid cast!");                     \
59       return Q;                                         \
60     }
61   
62   DEFINE_ISA_CONVERSION_FUNCTIONS   (Type,               LLVMTypeRef          )
63   DEFINE_ISA_CONVERSION_FUNCTIONS   (Value,              LLVMValueRef         )
64   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Module,             LLVMModuleRef        )
65   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock,         LLVMBasicBlockRef    )
66   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>,        LLVMBuilderRef       )
67   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer,       LLVMMemoryBufferRef  )
68   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext,        LLVMContextRef       )
69   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Use,                LLVMUseRef           )
70   DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBase,    LLVMPassManagerRef   )
71   DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassRegistry,       LLVMPassRegistryRef  )
72
73   /* LLVMModuleProviderRef exists for historical reasons, but now just holds a
74    * Module.
75    */
76   inline Module *unwrap(LLVMModuleProviderRef MP) {
77     return reinterpret_cast<Module*>(MP);
78   }
79   
80   /* Specialized opaque context conversions.
81    */
82   inline LLVMContext **unwrap(LLVMContextRef* Tys) {
83     return reinterpret_cast<LLVMContext**>(Tys);
84   }
85   
86   inline LLVMContextRef *wrap(const LLVMContext **Tys) {
87     return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));
88   }
89   
90   /* Specialized opaque type conversions.
91    */
92   inline Type **unwrap(LLVMTypeRef* Tys) {
93     return reinterpret_cast<Type**>(Tys);
94   }
95   
96   inline LLVMTypeRef *wrap(Type **Tys) {
97     return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
98   }
99   
100   /* Specialized opaque value conversions.
101    */ 
102   inline Value **unwrap(LLVMValueRef *Vals) {
103     return reinterpret_cast<Value**>(Vals);
104   }
105   
106   template<typename T>
107   inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
108     #ifdef DEBUG
109     for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
110       cast<T>(*I);
111     #endif
112     (void)Length;
113     return reinterpret_cast<T**>(Vals);
114   }
115   
116   inline LLVMValueRef *wrap(const Value **Vals) {
117     return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
118   }
119 }