diff options
author | Gordon Henriksen <gordonhenriksen@mac.com> | 2007-09-18 03:18:57 +0000 |
---|---|---|
committer | Gordon Henriksen <gordonhenriksen@mac.com> | 2007-09-18 03:18:57 +0000 |
commit | 8b94a14a782867b1da1f272b6f502562d0c2a1aa (patch) | |
tree | 6451f3fb779575788cdbd7c876ed185d450566b8 /include/llvm | |
parent | 04bdf20ec1da00891e9903847613574c6f0505d2 (diff) | |
download | external_llvm-8b94a14a782867b1da1f272b6f502562d0c2a1aa.zip external_llvm-8b94a14a782867b1da1f272b6f502562d0c2a1aa.tar.gz external_llvm-8b94a14a782867b1da1f272b6f502562d0c2a1aa.tar.bz2 |
C bindings for libLLVMCore.a and libLLVMBitWriter.a.
- The naming prefix is LLVM.
- All types are represented using opaque references.
- Functions are not named LLVM{Type}{Method}; the names became
unreadable goop. Instead, they are named LLVM{ImperativeSentence}.
- Where an attribute only appears once in the class hierarchy (e.g.,
linkage only applies to values; parameter types only apply to
function types), the class is omitted from identifiers for
brevity. Tastes like methods.
- Strings are C strings or string/length tuples on a case-by-case
basis.
- APIs which give the caller ownership of an object are not mapped
(removeFromParent, certain constructor overloads). This keeps
keep memory management as simple as possible.
For each library with bindings:
llvm-c/<LIB>.h - Declares the bindings.
lib/<LIB>/<LIB>.cpp - Implements the bindings.
So just link with the library of your choice and use the C header
instead of the C++ one.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@42077 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm')
-rw-r--r-- | include/llvm/CHelpers.h | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/include/llvm/CHelpers.h b/include/llvm/CHelpers.h new file mode 100644 index 0000000..d00aba3 --- /dev/null +++ b/include/llvm/CHelpers.h @@ -0,0 +1,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 |