1 //===-- Support/CHelpers.h - Utilities for writing C bindings -------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_SUPPORT_CHELPERS_H
17 #define LLVM_SUPPORT_CHELPERS_H
19 #include "llvm/Module.h"
20 #include "llvm/Type.h"
21 #include "llvm/Value.h"
23 typedef struct LLVMOpaqueModule *LLVMModuleRef;
24 typedef struct LLVMOpaqueType *LLVMTypeRef;
25 typedef struct LLVMOpaqueValue *LLVMValueRef;
28 /// Opaque module conversions
30 inline Module *unwrap(LLVMModuleRef M) {
31 return reinterpret_cast<Module*>(M);
34 inline LLVMModuleRef wrap(Module *M) {
35 return reinterpret_cast<LLVMModuleRef>(M);
38 /// Opaque type conversions
40 inline Type *unwrap(LLVMTypeRef Ty) {
41 return reinterpret_cast<Type*>(Ty);
45 inline T *unwrap(LLVMTypeRef Ty) {
46 return cast<T>(unwrap(Ty));
49 inline Type **unwrap(LLVMTypeRef* Tys) {
50 return reinterpret_cast<Type**>(Tys);
53 inline LLVMTypeRef wrap(const Type *Ty) {
54 return reinterpret_cast<LLVMTypeRef>(const_cast<Type*>(Ty));
57 inline LLVMTypeRef *wrap(const Type **Tys) {
58 return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
61 /// Opaque value conversions
63 inline Value *unwrap(LLVMValueRef Val) {
64 return reinterpret_cast<Value*>(Val);
68 inline T *unwrap(LLVMValueRef Val) {
69 return cast<T>(unwrap(Val));
72 inline Value **unwrap(LLVMValueRef *Vals) {
73 return reinterpret_cast<Value**>(Vals);
77 inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
79 for (LLVMValueRef *I = Vals, E = Vals + Length; I != E; ++I)
82 return reinterpret_cast<T**>(Vals);
85 inline LLVMValueRef wrap(const Value *Val) {
86 return reinterpret_cast<LLVMValueRef>(const_cast<Value*>(Val));
89 inline LLVMValueRef *wrap(const Value **Vals) {
90 return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));