Switch to densemap rather than std::set
[oota-llvm.git] / include / llvm / CHelpers.h
1 //===-- Support/CHelpers.h - Utilities for writing C bindings -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // 
10 // These opaque reference<-->pointer conversions are shorter and more tightly
11 // typed than writing the casts by hand in C bindings. In assert builds, they
12 // will do type checking.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_SUPPORT_CHELPERS_H
17 #define LLVM_SUPPORT_CHELPERS_H
18
19 #include "llvm/Module.h"
20 #include "llvm/Type.h"
21 #include "llvm/Value.h"
22
23 typedef struct LLVMOpaqueModule *LLVMModuleRef;
24 typedef struct LLVMOpaqueType *LLVMTypeRef;
25 typedef struct LLVMOpaqueValue *LLVMValueRef;
26
27 namespace llvm {
28   /// Opaque module conversions
29   /// 
30   inline Module *unwrap(LLVMModuleRef M) {
31     return reinterpret_cast<Module*>(M);
32   }
33   
34   inline LLVMModuleRef wrap(Module *M) {
35     return reinterpret_cast<LLVMModuleRef>(M);
36   }
37   
38   /// Opaque type conversions
39   /// 
40   inline Type *unwrap(LLVMTypeRef Ty) {
41     return reinterpret_cast<Type*>(Ty);
42   }
43   
44   template<typename T>
45   inline T *unwrap(LLVMTypeRef Ty) {
46     return cast<T>(unwrap(Ty));
47   }
48   
49   inline Type **unwrap(LLVMTypeRef* Tys) {
50     return reinterpret_cast<Type**>(Tys);
51   }
52   
53   inline LLVMTypeRef wrap(const Type *Ty) {
54     return reinterpret_cast<LLVMTypeRef>(const_cast<Type*>(Ty));
55   }
56   
57   inline LLVMTypeRef *wrap(const Type **Tys) {
58     return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
59   }
60   
61   /// Opaque value conversions
62   /// 
63   inline Value *unwrap(LLVMValueRef Val) {
64     return reinterpret_cast<Value*>(Val);
65   }
66   
67   template<typename T>
68   inline T *unwrap(LLVMValueRef Val) {
69     return cast<T>(unwrap(Val));
70   }
71
72   inline Value **unwrap(LLVMValueRef *Vals) {
73     return reinterpret_cast<Value**>(Vals);
74   }
75   
76   template<typename T>
77   inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
78     #if DEBUG
79     for (LLVMValueRef *I = Vals, E = Vals + Length; I != E; ++I)
80       cast<T>(*I);
81     #endif
82     return reinterpret_cast<T**>(Vals);
83   }
84   
85   inline LLVMValueRef wrap(const Value *Val) {
86     return reinterpret_cast<LLVMValueRef>(const_cast<Value*>(Val));
87   }
88   
89   inline LLVMValueRef *wrap(const Value **Vals) {
90     return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
91   }
92   
93   /// Basic block conversions
94   /// 
95   inline BasicBlock *unwrap(LLVMBasicBlockRef BBRef) {
96     return reinterpret_cast<BasicBlock*>(BBRef);
97   }
98   
99   inline LLVMBasicBlockRef wrap(const BasicBlock *BB) {
100     return reinterpret_cast<LLVMBasicBlockRef>(const_cast<BasicBlock*>(BB));
101   }
102 }
103
104 #endif