aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm/CHelpers.h
blob: d00aba383f9a5d1db6ea3284fb1fa57db919fe0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//===-- Support/CHelpers.h - Utilities for writing C bindings -------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// 
// These opaque reference<-->pointer conversions are shorter and more tightly
// typed than writing the casts by hand in C bindings. In assert builds, they
// will do type checking.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_CHELPERS_H
#define LLVM_SUPPORT_CHELPERS_H

#include "llvm/Module.h"
#include "llvm/Type.h"
#include "llvm/Value.h"

typedef struct LLVMOpaqueModule *LLVMModuleRef;
typedef struct LLVMOpaqueType *LLVMTypeRef;
typedef struct LLVMOpaqueValue *LLVMValueRef;

namespace llvm {
  /// Opaque module conversions
  /// 
  inline Module *unwrap(LLVMModuleRef M) {
    return reinterpret_cast<Module*>(M);
  }
  
  inline LLVMModuleRef wrap(Module *M) {
    return reinterpret_cast<LLVMModuleRef>(M);
  }
  
  /// Opaque type conversions
  /// 
  inline Type *unwrap(LLVMTypeRef Ty) {
    return reinterpret_cast<Type*>(Ty);
  }
  
  template<typename T>
  inline T *unwrap(LLVMTypeRef Ty) {
    return cast<T>(unwrap(Ty));
  }
  
  inline Type **unwrap(LLVMTypeRef* Tys) {
    return reinterpret_cast<Type**>(Tys);
  }
  
  inline LLVMTypeRef wrap(const Type *Ty) {
    return reinterpret_cast<LLVMTypeRef>(const_cast<Type*>(Ty));
  }
  
  inline LLVMTypeRef *wrap(const Type **Tys) {
    return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
  }
  
  /// Opaque value conversions
  /// 
  inline Value *unwrap(LLVMValueRef Val) {
    return reinterpret_cast<Value*>(Val);
  }
  
  template<typename T>
  inline T *unwrap(LLVMValueRef Val) {
    return cast<T>(unwrap(Val));
  }

  inline Value **unwrap(LLVMValueRef *Vals) {
    return reinterpret_cast<Value**>(Vals);
  }
  
  template<typename T>
  inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
    #if DEBUG
    for (LLVMValueRef *I = Vals, E = Vals + Length; I != E; ++I)
      cast<T>(*I);
    #endif
    return reinterpret_cast<T**>(Vals);
  }
  
  inline LLVMValueRef wrap(const Value *Val) {
    return reinterpret_cast<LLVMValueRef>(const_cast<Value*>(Val));
  }
  
  inline LLVMValueRef *wrap(const Value **Vals) {
    return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
  }
}

#endif