Go bindings: expose the Metadata type.

Also modifies SetCurrentDebugLocation to take individual arguments rather
than an MDNode.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224176 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Peter Collingbourne 2014-12-13 02:25:49 +00:00
parent 77f0100f1f
commit eb8bf790eb
6 changed files with 324 additions and 252 deletions

View File

@ -13,40 +13,21 @@
#include "DIBuilderBindings.h"
#include "IRBindings.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/DIBuilder.h"
using namespace llvm;
static Metadata *unwrapMetadata(LLVMValueRef VRef) {
Value *V = unwrap(VRef);
if (!V)
return nullptr;
if (auto *MD = dyn_cast<MetadataAsValue>(V))
return MD->getMetadata();
return ValueAsMetadata::get(V);
}
static SmallVector<Metadata *, 8> unwrapMetadataArray(LLVMValueRef *Data,
size_t Length) {
SmallVector<Metadata *, 8> Elements;
for (size_t I = 0; I != Length; ++I)
Elements.push_back(unwrapMetadata(Data[I]));
return Elements;
}
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef)
namespace {
template <typename T> T unwrapDI(LLVMValueRef v) {
return T(cast_or_null<MDNode>(unwrapMetadata(v)));
template <typename T> T unwrapDI(LLVMMetadataRef v) {
return v ? T(unwrap<MDNode>(v)) : T();
}
}
static LLVMValueRef wrapDI(DIDescriptor N) {
return wrap(MetadataAsValue::get(N->getContext(), N));
}
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef)
LLVMDIBuilderRef LLVMNewDIBuilder(LLVMModuleRef mref) {
Module *m = unwrap(mref);
return wrap(new DIBuilder(*m));
@ -59,176 +40,187 @@ void LLVMDIBuilderDestroy(LLVMDIBuilderRef dref) {
void LLVMDIBuilderFinalize(LLVMDIBuilderRef dref) { unwrap(dref)->finalize(); }
LLVMValueRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Dref,
unsigned Lang, const char *File,
const char *Dir,
const char *Producer, int Optimized,
const char *Flags,
unsigned RuntimeVersion) {
LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef Dref,
unsigned Lang, const char *File,
const char *Dir,
const char *Producer,
int Optimized, const char *Flags,
unsigned RuntimeVersion) {
DIBuilder *D = unwrap(Dref);
DICompileUnit CU = D->createCompileUnit(Lang, File, Dir, Producer, Optimized,
Flags, RuntimeVersion);
return wrapDI(CU);
return wrap(CU);
}
LLVMValueRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref, const char *File,
const char *Dir) {
LLVMMetadataRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef Dref, const char *File,
const char *Dir) {
DIBuilder *D = unwrap(Dref);
DIFile F = D->createFile(File, Dir);
return wrapDI(F);
return wrap(F);
}
LLVMValueRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,
LLVMValueRef Scope,
LLVMValueRef File, unsigned Line,
unsigned Column) {
LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef Dref,
LLVMMetadataRef Scope,
LLVMMetadataRef File,
unsigned Line,
unsigned Column) {
DIBuilder *D = unwrap(Dref);
DILexicalBlock LB = D->createLexicalBlock(
unwrapDI<DIDescriptor>(Scope), unwrapDI<DIFile>(File), Line, Column);
return wrapDI(LB);
return wrap(LB);
}
LLVMValueRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,
LLVMValueRef Scope,
LLVMValueRef File,
unsigned Discriminator) {
LLVMMetadataRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Dref,
LLVMMetadataRef Scope,
LLVMMetadataRef File,
unsigned Discriminator) {
DIBuilder *D = unwrap(Dref);
DILexicalBlockFile LBF = D->createLexicalBlockFile(
unwrapDI<DIDescriptor>(Scope), unwrapDI<DIFile>(File), Discriminator);
return wrapDI(LBF);
return wrap(LBF);
}
LLVMValueRef LLVMDIBuilderCreateFunction(
LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
const char *LinkageName, LLVMValueRef File, unsigned Line,
LLVMValueRef CompositeType, int IsLocalToUnit, int IsDefinition,
LLVMMetadataRef LLVMDIBuilderCreateFunction(
LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
const char *LinkageName, LLVMMetadataRef File, unsigned Line,
LLVMMetadataRef CompositeType, int IsLocalToUnit, int IsDefinition,
unsigned ScopeLine, unsigned Flags, int IsOptimized, LLVMValueRef Func) {
DIBuilder *D = unwrap(Dref);
DISubprogram SP = D->createFunction(
unwrapDI<DIDescriptor>(Scope), Name, LinkageName, unwrapDI<DIFile>(File),
Line, unwrapDI<DICompositeType>(CompositeType), IsLocalToUnit,
IsDefinition, ScopeLine, Flags, IsOptimized, unwrap<Function>(Func));
return wrapDI(SP);
return wrap(SP);
}
LLVMValueRef LLVMDIBuilderCreateLocalVariable(
LLVMDIBuilderRef Dref, unsigned Tag, LLVMValueRef Scope, const char *Name,
LLVMValueRef File, unsigned Line, LLVMValueRef Ty, int AlwaysPreserve,
unsigned Flags, unsigned ArgNo) {
LLVMMetadataRef LLVMDIBuilderCreateLocalVariable(
LLVMDIBuilderRef Dref, unsigned Tag, LLVMMetadataRef Scope,
const char *Name, LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty,
int AlwaysPreserve, unsigned Flags, unsigned ArgNo) {
DIBuilder *D = unwrap(Dref);
DIVariable V = D->createLocalVariable(
Tag, unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
unwrapDI<DIType>(Ty), AlwaysPreserve, Flags, ArgNo);
return wrapDI(V);
return wrap(V);
}
LLVMValueRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref,
const char *Name, uint64_t SizeInBits,
uint64_t AlignInBits,
unsigned Encoding) {
LLVMMetadataRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Dref,
const char *Name,
uint64_t SizeInBits,
uint64_t AlignInBits,
unsigned Encoding) {
DIBuilder *D = unwrap(Dref);
DIBasicType T = D->createBasicType(Name, SizeInBits, AlignInBits, Encoding);
return wrapDI(T);
return wrap(T);
}
LLVMValueRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
LLVMValueRef PointeeType,
uint64_t SizeInBits,
uint64_t AlignInBits,
const char *Name) {
LLVMMetadataRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef Dref,
LLVMMetadataRef PointeeType,
uint64_t SizeInBits,
uint64_t AlignInBits,
const char *Name) {
DIBuilder *D = unwrap(Dref);
DIDerivedType T = D->createPointerType(unwrapDI<DIType>(PointeeType),
SizeInBits, AlignInBits, Name);
return wrapDI(T);
return wrap(T);
}
LLVMValueRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref,
LLVMValueRef File,
LLVMValueRef ParameterTypes) {
LLVMMetadataRef
LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Dref, LLVMMetadataRef File,
LLVMMetadataRef ParameterTypes) {
DIBuilder *D = unwrap(Dref);
DICompositeType CT = D->createSubroutineType(
unwrapDI<DIFile>(File), unwrapDI<DITypeArray>(ParameterTypes));
return wrapDI(CT);
return wrap(CT);
}
LLVMValueRef LLVMDIBuilderCreateStructType(
LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
LLVMValueRef File, unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
unsigned Flags, LLVMValueRef DerivedFrom, LLVMValueRef ElementTypes) {
LLVMMetadataRef LLVMDIBuilderCreateStructType(
LLVMDIBuilderRef Dref, LLVMMetadataRef Scope, const char *Name,
LLVMMetadataRef File, unsigned Line, uint64_t SizeInBits,
uint64_t AlignInBits, unsigned Flags, LLVMMetadataRef DerivedFrom,
LLVMMetadataRef ElementTypes) {
DIBuilder *D = unwrap(Dref);
DICompositeType CT = D->createStructType(
unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
SizeInBits, AlignInBits, Flags, unwrapDI<DIType>(DerivedFrom),
unwrapDI<DIArray>(ElementTypes));
return wrapDI(CT);
return wrap(CT);
}
LLVMValueRef LLVMDIBuilderCreateMemberType(
LLVMDIBuilderRef Dref, LLVMValueRef Scope, const char *Name,
LLVMValueRef File, unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
uint64_t OffsetInBits, unsigned Flags, LLVMValueRef Ty) {
LLVMMetadataRef
LLVMDIBuilderCreateMemberType(LLVMDIBuilderRef Dref, LLVMMetadataRef Scope,
const char *Name, LLVMMetadataRef File,
unsigned Line, uint64_t SizeInBits,
uint64_t AlignInBits, uint64_t OffsetInBits,
unsigned Flags, LLVMMetadataRef Ty) {
DIBuilder *D = unwrap(Dref);
DIDerivedType DT = D->createMemberType(
unwrapDI<DIDescriptor>(Scope), Name, unwrapDI<DIFile>(File), Line,
SizeInBits, AlignInBits, OffsetInBits, Flags, unwrapDI<DIType>(Ty));
return wrapDI(DT);
return wrap(DT);
}
LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
uint64_t SizeInBits,
uint64_t AlignInBits,
LLVMValueRef ElementType,
LLVMValueRef Subscripts) {
LLVMMetadataRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Dref,
uint64_t SizeInBits,
uint64_t AlignInBits,
LLVMMetadataRef ElementType,
LLVMMetadataRef Subscripts) {
DIBuilder *D = unwrap(Dref);
DICompositeType CT =
D->createArrayType(SizeInBits, AlignInBits, unwrapDI<DIType>(ElementType),
unwrapDI<DIArray>(Subscripts));
return wrapDI(CT);
return wrap(CT);
}
LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref, LLVMValueRef Ty,
const char *Name, LLVMValueRef File,
unsigned Line, LLVMValueRef Context) {
LLVMMetadataRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Dref,
LLVMMetadataRef Ty, const char *Name,
LLVMMetadataRef File, unsigned Line,
LLVMMetadataRef Context) {
DIBuilder *D = unwrap(Dref);
DIDerivedType DT =
D->createTypedef(unwrapDI<DIType>(Ty), Name, unwrapDI<DIFile>(File), Line,
unwrapDI<DIDescriptor>(Context));
return wrapDI(DT);
return wrap(DT);
}
LLVMValueRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref, int64_t Lo,
int64_t Count) {
LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Dref,
int64_t Lo, int64_t Count) {
DIBuilder *D = unwrap(Dref);
DISubrange S = D->getOrCreateSubrange(Lo, Count);
return wrapDI(S);
return wrap(S);
}
LLVMValueRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
LLVMValueRef *Data, size_t Length) {
LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Dref,
LLVMMetadataRef *Data,
size_t Length) {
DIBuilder *D = unwrap(Dref);
DIArray A = D->getOrCreateArray(unwrapMetadataArray(Data, Length));
return wrapDI(A);
Metadata **DataValue = unwrap(Data);
ArrayRef<Metadata *> Elements(DataValue, Length);
DIArray A = D->getOrCreateArray(Elements);
return wrap(A);
}
LLVMValueRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
LLVMValueRef *Data,
size_t Length) {
LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Dref,
LLVMMetadataRef *Data,
size_t Length) {
DIBuilder *D = unwrap(Dref);
DITypeArray A = D->getOrCreateTypeArray(unwrapMetadataArray(Data, Length));
return wrapDI(A);
Metadata **DataValue = unwrap(Data);
ArrayRef<Metadata *> Elements(DataValue, Length);
DITypeArray A = D->getOrCreateTypeArray(Elements);
return wrap(A);
}
LLVMValueRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, int64_t *Addr,
size_t Length) {
LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref,
int64_t *Addr, size_t Length) {
DIBuilder *D = unwrap(Dref);
DIExpression Expr = D->createExpression(ArrayRef<int64_t>(Addr, Length));
return wrapDI(Expr);
return wrap(Expr);
}
LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
LLVMValueRef Storage,
LLVMValueRef VarInfo,
LLVMValueRef Expr,
LLVMMetadataRef VarInfo,
LLVMMetadataRef Expr,
LLVMBasicBlockRef Block) {
DIBuilder *D = unwrap(Dref);
Instruction *Instr =
@ -239,8 +231,8 @@ LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Dref,
LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef Dref,
LLVMValueRef Val, uint64_t Offset,
LLVMValueRef VarInfo,
LLVMValueRef Expr,
LLVMMetadataRef VarInfo,
LLVMMetadataRef Expr,
LLVMBasicBlockRef Block) {
DIBuilder *D = unwrap(Dref);
Instruction *Instr = D->insertDbgValueIntrinsic(

View File

@ -15,6 +15,7 @@
#define LLVM_BINDINGS_GO_LLVM_DIBUILDERBINDINGS_H
#include "llvm-c/Core.h"
#include "IRBindings.h"
#ifdef __cplusplus
extern "C" {
@ -31,100 +32,104 @@ LLVMDIBuilderRef LLVMNewDIBuilder(LLVMModuleRef m);
void LLVMDIBuilderDestroy(LLVMDIBuilderRef d);
void LLVMDIBuilderFinalize(LLVMDIBuilderRef d);
LLVMValueRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef D,
unsigned Language, const char *File,
const char *Dir,
const char *Producer, int Optimized,
const char *Flags,
unsigned RuntimeVersion);
LLVMMetadataRef
LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef D, unsigned Language,
const char *File, const char *Dir,
const char *Producer, int Optimized,
const char *Flags, unsigned RuntimeVersion);
LLVMValueRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef D, const char *File,
const char *Dir);
LLVMMetadataRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef D, const char *File,
const char *Dir);
LLVMValueRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef D,
LLVMValueRef Scope,
LLVMValueRef File, unsigned Line,
unsigned Column);
LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef D,
LLVMMetadataRef Scope,
LLVMMetadataRef File,
unsigned Line, unsigned Column);
LLVMValueRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef D,
LLVMValueRef Scope,
LLVMValueRef File,
unsigned Discriminator);
LLVMMetadataRef LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef D,
LLVMMetadataRef Scope,
LLVMMetadataRef File,
unsigned Discriminator);
LLVMValueRef LLVMDIBuilderCreateFunction(
LLVMDIBuilderRef D, LLVMValueRef Scope, const char *Name,
const char *LinkageName, LLVMValueRef File, unsigned Line,
LLVMValueRef CompositeType, int IsLocalToUnit, int IsDefinition,
LLVMMetadataRef LLVMDIBuilderCreateFunction(
LLVMDIBuilderRef D, LLVMMetadataRef Scope, const char *Name,
const char *LinkageName, LLVMMetadataRef File, unsigned Line,
LLVMMetadataRef CompositeType, int IsLocalToUnit, int IsDefinition,
unsigned ScopeLine, unsigned Flags, int IsOptimized, LLVMValueRef Function);
LLVMValueRef LLVMDIBuilderCreateLocalVariable(
LLVMDIBuilderRef D, unsigned Tag, LLVMValueRef Scope, const char *Name,
LLVMValueRef File, unsigned Line, LLVMValueRef Ty, int AlwaysPreserve,
LLVMMetadataRef LLVMDIBuilderCreateLocalVariable(
LLVMDIBuilderRef D, unsigned Tag, LLVMMetadataRef Scope, const char *Name,
LLVMMetadataRef File, unsigned Line, LLVMMetadataRef Ty, int AlwaysPreserve,
unsigned Flags, unsigned ArgNo);
LLVMValueRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef D, const char *Name,
uint64_t SizeInBits,
uint64_t AlignInBits,
unsigned Encoding);
LLVMMetadataRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef D,
const char *Name,
uint64_t SizeInBits,
uint64_t AlignInBits,
unsigned Encoding);
LLVMValueRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef D,
LLVMValueRef PointeeType,
uint64_t SizeInBits,
uint64_t AlignInBits,
const char *Name);
LLVMMetadataRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef D,
LLVMMetadataRef PointeeType,
uint64_t SizeInBits,
uint64_t AlignInBits,
const char *Name);
LLVMValueRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef D,
LLVMValueRef File,
LLVMValueRef ParameterTypes);
LLVMMetadataRef
LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef D, LLVMMetadataRef File,
LLVMMetadataRef ParameterTypes);
LLVMValueRef LLVMDIBuilderCreateStructType(
LLVMDIBuilderRef D, LLVMValueRef Scope, const char *Name, LLVMValueRef File,
unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags,
LLVMValueRef DerivedFrom, LLVMValueRef ElementTypes);
LLVMMetadataRef LLVMDIBuilderCreateStructType(
LLVMDIBuilderRef D, LLVMMetadataRef Scope, const char *Name,
LLVMMetadataRef File, unsigned Line, uint64_t SizeInBits,
uint64_t AlignInBits, unsigned Flags, LLVMMetadataRef DerivedFrom,
LLVMMetadataRef ElementTypes);
LLVMValueRef LLVMDIBuilderCreateMemberType(
LLVMDIBuilderRef D, LLVMValueRef Scope, const char *Name, LLVMValueRef File,
unsigned Line, uint64_t SizeInBits, uint64_t AlignInBits,
uint64_t OffsetInBits, unsigned Flags, LLVMValueRef Ty);
LLVMMetadataRef
LLVMDIBuilderCreateMemberType(LLVMDIBuilderRef D, LLVMMetadataRef Scope,
const char *Name, LLVMMetadataRef File,
unsigned Line, uint64_t SizeInBits,
uint64_t AlignInBits, uint64_t OffsetInBits,
unsigned Flags, LLVMMetadataRef Ty);
LLVMValueRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef D,
uint64_t SizeInBits,
uint64_t AlignInBits,
LLVMValueRef ElementType,
LLVMValueRef Subscripts);
LLVMMetadataRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef D,
uint64_t SizeInBits,
uint64_t AlignInBits,
LLVMMetadataRef ElementType,
LLVMMetadataRef Subscripts);
LLVMValueRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef D, LLVMValueRef Ty,
const char *Name, LLVMValueRef File,
unsigned Line, LLVMValueRef Context);
LLVMMetadataRef LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef D,
LLVMMetadataRef Ty, const char *Name,
LLVMMetadataRef File, unsigned Line,
LLVMMetadataRef Context);
LLVMValueRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef D, int64_t Lo,
int64_t Count);
LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef D, int64_t Lo,
int64_t Count);
LLVMValueRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef D,
LLVMValueRef *Data, size_t Length);
LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef D,
LLVMMetadataRef *Data,
size_t Length);
LLVMValueRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef D,
LLVMValueRef *Data,
size_t Length);
LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef D,
LLVMMetadataRef *Data,
size_t Length);
LLVMValueRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref, int64_t *Addr,
size_t Length);
LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Dref,
int64_t *Addr, size_t Length);
LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef D,
LLVMValueRef Storage,
LLVMValueRef VarInfo,
LLVMValueRef Expr,
LLVMMetadataRef VarInfo,
LLVMMetadataRef Expr,
LLVMBasicBlockRef Block);
LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef D,
LLVMValueRef Val,
LLVMValueRef LLVMDIBuilderInsertValueAtEnd(LLVMDIBuilderRef D, LLVMValueRef Val,
uint64_t Offset,
LLVMValueRef VarInfo,
LLVMValueRef Expr,
LLVMMetadataRef VarInfo,
LLVMMetadataRef Expr,
LLVMBasicBlockRef Block);
#ifdef __cplusplus
} // extern "C"
} // extern "C"
#endif
#endif

View File

@ -14,7 +14,11 @@
#include "IRBindings.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
using namespace llvm;
@ -45,3 +49,36 @@ void LLVMRemoveFunctionAttr2(LLVMValueRef Fn, uint64_t PA) {
AttributeSet::FunctionIndex, B));
Func->setAttributes(PALnew);
}
LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen) {
return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
}
LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs,
unsigned Count) {
return wrap(
MDNode::get(*unwrap(C), ArrayRef<Metadata *>(unwrap(MDs), Count)));
}
void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
LLVMMetadataRef Val) {
NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
if (!N)
return;
if (!Val)
return;
N->addOperand(unwrap<MDNode>(Val));
}
void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD) {
MDNode *N = MD ? unwrap<MDNode>(MD) : nullptr;
unwrap<Instruction>(Inst)->setMetadata(KindID, N);
}
void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line,
unsigned Col, LLVMMetadataRef Scope,
LLVMMetadataRef InlinedAt) {
unwrap(Bref)->SetCurrentDebugLocation(
DebugLoc::get(Line, Col, Scope ? unwrap<MDNode>(Scope) : nullptr,
InlinedAt ? unwrap<MDNode>(InlinedAt) : nullptr));
}

View File

@ -15,12 +15,19 @@
#define LLVM_BINDINGS_GO_LLVM_IRBINDINGS_H
#include "llvm-c/Core.h"
#ifdef __cplusplus
#include "llvm/IR/Metadata.h"
#include "llvm/Support/CBindingWrapping.h"
#endif
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct LLVMOpaqueMetadata *LLVMMetadataRef;
// These functions duplicate the LLVM*FunctionAttr functions in the stable C
// API. We cannot use the existing functions because they take 32-bit attribute
// values, and the Go bindings expose all of the LLVM attributes, some of which
@ -30,8 +37,31 @@ void LLVMAddFunctionAttr2(LLVMValueRef Fn, uint64_t PA);
uint64_t LLVMGetFunctionAttr2(LLVMValueRef Fn);
void LLVMRemoveFunctionAttr2(LLVMValueRef Fn, uint64_t PA);
LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen);
LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs,
unsigned Count);
void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
LLVMMetadataRef Val);
void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD);
void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line,
unsigned Col, LLVMMetadataRef Scope,
LLVMMetadataRef InlinedAt);
#ifdef __cplusplus
}
namespace llvm {
DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMMetadataRef)
inline Metadata **unwrap(LLVMMetadataRef *Vals) {
return reinterpret_cast<Metadata**>(Vals);
}
}
#endif
#endif

View File

@ -121,7 +121,7 @@ type DICompileUnit struct {
}
// CreateCompileUnit creates compile unit debug metadata.
func (d *DIBuilder) CreateCompileUnit(cu DICompileUnit) Value {
func (d *DIBuilder) CreateCompileUnit(cu DICompileUnit) Metadata {
file := C.CString(cu.File)
defer C.free(unsafe.Pointer(file))
dir := C.CString(cu.Dir)
@ -139,28 +139,28 @@ func (d *DIBuilder) CreateCompileUnit(cu DICompileUnit) Value {
flags,
C.unsigned(cu.RuntimeVersion),
)
return Value{C: result}
return Metadata{C: result}
}
// CreateCompileUnit creates file debug metadata.
func (d *DIBuilder) CreateFile(filename, dir string) Value {
func (d *DIBuilder) CreateFile(filename, dir string) Metadata {
cfilename := C.CString(filename)
defer C.free(unsafe.Pointer(cfilename))
cdir := C.CString(dir)
defer C.free(unsafe.Pointer(cdir))
result := C.LLVMDIBuilderCreateFile(d.ref, cfilename, cdir)
return Value{C: result}
return Metadata{C: result}
}
// DILexicalBlock holds the values for creating lexical block debug metadata.
type DILexicalBlock struct {
File Value
File Metadata
Line int
Column int
}
// CreateCompileUnit creates lexical block debug metadata.
func (d *DIBuilder) CreateLexicalBlock(diScope Value, b DILexicalBlock) Value {
func (d *DIBuilder) CreateLexicalBlock(diScope Metadata, b DILexicalBlock) Metadata {
result := C.LLVMDIBuilderCreateLexicalBlock(
d.ref,
diScope.C,
@ -168,22 +168,22 @@ func (d *DIBuilder) CreateLexicalBlock(diScope Value, b DILexicalBlock) Value {
C.unsigned(b.Line),
C.unsigned(b.Column),
)
return Value{C: result}
return Metadata{C: result}
}
func (d *DIBuilder) CreateLexicalBlockFile(diScope Value, diFile Value, discriminator int) Value {
func (d *DIBuilder) CreateLexicalBlockFile(diScope Metadata, diFile Metadata, discriminator int) Metadata {
result := C.LLVMDIBuilderCreateLexicalBlockFile(d.ref, diScope.C, diFile.C,
C.unsigned(discriminator))
return Value{C: result}
return Metadata{C: result}
}
// DIFunction holds the values for creating function debug metadata.
type DIFunction struct {
Name string
LinkageName string
File Value
File Metadata
Line int
Type Value
Type Metadata
LocalToUnit bool
IsDefinition bool
ScopeLine int
@ -193,7 +193,7 @@ type DIFunction struct {
}
// CreateCompileUnit creates function debug metadata.
func (d *DIBuilder) CreateFunction(diScope Value, f DIFunction) Value {
func (d *DIBuilder) CreateFunction(diScope Metadata, f DIFunction) Metadata {
name := C.CString(f.Name)
defer C.free(unsafe.Pointer(name))
linkageName := C.CString(f.LinkageName)
@ -213,16 +213,16 @@ func (d *DIBuilder) CreateFunction(diScope Value, f DIFunction) Value {
boolToCInt(f.Optimized),
f.Function.C,
)
return Value{C: result}
return Metadata{C: result}
}
// DILocalVariable holds the values for creating local variable debug metadata.
type DILocalVariable struct {
Tag dwarf.Tag
Name string
File Value
File Metadata
Line int
Type Value
Type Metadata
AlwaysPreserve bool
Flags int
@ -232,7 +232,7 @@ type DILocalVariable struct {
}
// CreateLocalVariable creates local variable debug metadata.
func (d *DIBuilder) CreateLocalVariable(scope Value, v DILocalVariable) Value {
func (d *DIBuilder) CreateLocalVariable(scope Metadata, v DILocalVariable) Metadata {
name := C.CString(v.Name)
defer C.free(unsafe.Pointer(name))
result := C.LLVMDIBuilderCreateLocalVariable(
@ -247,7 +247,7 @@ func (d *DIBuilder) CreateLocalVariable(scope Value, v DILocalVariable) Value {
C.unsigned(v.Flags),
C.unsigned(v.ArgNo),
)
return Value{C: result}
return Metadata{C: result}
}
// DIBasicType holds the values for creating basic type debug metadata.
@ -259,7 +259,7 @@ type DIBasicType struct {
}
// CreateBasicType creates basic type debug metadata.
func (d *DIBuilder) CreateBasicType(t DIBasicType) Value {
func (d *DIBuilder) CreateBasicType(t DIBasicType) Metadata {
name := C.CString(t.Name)
defer C.free(unsafe.Pointer(name))
result := C.LLVMDIBuilderCreateBasicType(
@ -269,19 +269,19 @@ func (d *DIBuilder) CreateBasicType(t DIBasicType) Value {
C.uint64_t(t.AlignInBits),
C.unsigned(t.Encoding),
)
return Value{C: result}
return Metadata{C: result}
}
// DIPointerType holds the values for creating pointer type debug metadata.
type DIPointerType struct {
Pointee Value
Pointee Metadata
SizeInBits uint64
AlignInBits uint64 // optional
Name string // optional
}
// CreateBasicType creates basic type debug metadata.
func (d *DIBuilder) CreatePointerType(t DIPointerType) Value {
func (d *DIBuilder) CreatePointerType(t DIPointerType) Metadata {
name := C.CString(t.Name)
defer C.free(unsafe.Pointer(name))
result := C.LLVMDIBuilderCreatePointerType(
@ -291,40 +291,40 @@ func (d *DIBuilder) CreatePointerType(t DIPointerType) Value {
C.uint64_t(t.AlignInBits),
name,
)
return Value{C: result}
return Metadata{C: result}
}
// DISubroutineType holds the values for creating subroutine type debug metadata.
type DISubroutineType struct {
// File is the file in which the subroutine type is defined.
File Value
File Metadata
// Parameters contains the subroutine parameter types,
// including the return type at the 0th index.
Parameters []Value
Parameters []Metadata
}
// CreateSubroutineType creates subroutine type debug metadata.
func (d *DIBuilder) CreateSubroutineType(t DISubroutineType) Value {
func (d *DIBuilder) CreateSubroutineType(t DISubroutineType) Metadata {
params := d.getOrCreateTypeArray(t.Parameters)
result := C.LLVMDIBuilderCreateSubroutineType(d.ref, t.File.C, params.C)
return Value{C: result}
return Metadata{C: result}
}
// DIStructType holds the values for creating struct type debug metadata.
type DIStructType struct {
Name string
File Value
File Metadata
Line int
SizeInBits uint64
AlignInBits uint64
Flags int
DerivedFrom Value
Elements []Value
DerivedFrom Metadata
Elements []Metadata
}
// CreateStructType creates struct type debug metadata.
func (d *DIBuilder) CreateStructType(scope Value, t DIStructType) Value {
func (d *DIBuilder) CreateStructType(scope Metadata, t DIStructType) Metadata {
elements := d.getOrCreateArray(t.Elements)
name := C.CString(t.Name)
defer C.free(unsafe.Pointer(name))
@ -340,23 +340,23 @@ func (d *DIBuilder) CreateStructType(scope Value, t DIStructType) Value {
t.DerivedFrom.C,
elements.C,
)
return Value{C: result}
return Metadata{C: result}
}
// DIMemberType holds the values for creating member type debug metadata.
type DIMemberType struct {
Name string
File Value
File Metadata
Line int
SizeInBits uint64
AlignInBits uint64
OffsetInBits uint64
Flags int
Type Value
Type Metadata
}
// CreateMemberType creates struct type debug metadata.
func (d *DIBuilder) CreateMemberType(scope Value, t DIMemberType) Value {
func (d *DIBuilder) CreateMemberType(scope Metadata, t DIMemberType) Metadata {
name := C.CString(t.Name)
defer C.free(unsafe.Pointer(name))
result := C.LLVMDIBuilderCreateMemberType(
@ -371,7 +371,7 @@ func (d *DIBuilder) CreateMemberType(scope Value, t DIMemberType) Value {
C.unsigned(t.Flags),
t.Type.C,
)
return Value{C: result}
return Metadata{C: result}
}
// DISubrange describes an integer value range.
@ -384,13 +384,13 @@ type DISubrange struct {
type DIArrayType struct {
SizeInBits uint64
AlignInBits uint64
ElementType Value
ElementType Metadata
Subscripts []DISubrange
}
// CreateArrayType creates struct type debug metadata.
func (d *DIBuilder) CreateArrayType(t DIArrayType) Value {
subscriptsSlice := make([]Value, len(t.Subscripts))
func (d *DIBuilder) CreateArrayType(t DIArrayType) Metadata {
subscriptsSlice := make([]Metadata, len(t.Subscripts))
for i, s := range t.Subscripts {
subscriptsSlice[i] = d.getOrCreateSubrange(s.Lo, s.Count)
}
@ -402,20 +402,20 @@ func (d *DIBuilder) CreateArrayType(t DIArrayType) Value {
t.ElementType.C,
subscripts.C,
)
return Value{C: result}
return Metadata{C: result}
}
// DITypedef holds the values for creating typedef type debug metadata.
type DITypedef struct {
Type Value
Type Metadata
Name string
File Value
File Metadata
Line int
Context Value
Context Metadata
}
// CreateTypedef creates typedef type debug metadata.
func (d *DIBuilder) CreateTypedef(t DITypedef) Value {
func (d *DIBuilder) CreateTypedef(t DITypedef) Metadata {
name := C.CString(t.Name)
defer C.free(unsafe.Pointer(name))
result := C.LLVMDIBuilderCreateTypedef(
@ -426,67 +426,59 @@ func (d *DIBuilder) CreateTypedef(t DITypedef) Value {
C.unsigned(t.Line),
t.Context.C,
)
return Value{C: result}
return Metadata{C: result}
}
// getOrCreateSubrange gets a metadata node for the specified subrange,
// creating if required.
func (d *DIBuilder) getOrCreateSubrange(lo, count int64) Value {
func (d *DIBuilder) getOrCreateSubrange(lo, count int64) Metadata {
result := C.LLVMDIBuilderGetOrCreateSubrange(d.ref, C.int64_t(lo), C.int64_t(count))
return Value{C: result}
return Metadata{C: result}
}
// getOrCreateArray gets a metadata node containing the specified values,
// creating if required.
func (d *DIBuilder) getOrCreateArray(values []Value) Value {
func (d *DIBuilder) getOrCreateArray(values []Metadata) Metadata {
if len(values) == 0 {
return Value{}
}
var data *C.LLVMValueRef
length := len(values)
if length > 0 {
data = &values[0].C
return Metadata{}
}
data, length := llvmMetadataRefs(values)
result := C.LLVMDIBuilderGetOrCreateArray(d.ref, data, C.size_t(length))
return Value{C: result}
return Metadata{C: result}
}
// getOrCreateTypeArray gets a metadata node for a type array containing the
// specified values, creating if required.
func (d *DIBuilder) getOrCreateTypeArray(values []Value) Value {
func (d *DIBuilder) getOrCreateTypeArray(values []Metadata) Metadata {
if len(values) == 0 {
return Value{}
}
var data *C.LLVMValueRef
length := len(values)
if length > 0 {
data = &values[0].C
return Metadata{}
}
data, length := llvmMetadataRefs(values)
result := C.LLVMDIBuilderGetOrCreateTypeArray(d.ref, data, C.size_t(length))
return Value{C: result}
return Metadata{C: result}
}
// CreateExpression creates a new descriptor for the specified
// variable which has a complex address expression for its address.
func (d *DIBuilder) CreateExpression(addr []int64) Value {
func (d *DIBuilder) CreateExpression(addr []int64) Metadata {
var data *C.int64_t
if len(addr) > 0 {
data = (*C.int64_t)(unsafe.Pointer(&addr[0]))
}
result := C.LLVMDIBuilderCreateExpression(d.ref, data, C.size_t(len(addr)))
return Value{C: result}
return Metadata{C: result}
}
// InsertDeclareAtEnd inserts a call to llvm.dbg.declare at the end of the
// specified basic block for the given value and associated debug metadata.
func (d *DIBuilder) InsertDeclareAtEnd(v, diVarInfo, expr Value, bb BasicBlock) Value {
func (d *DIBuilder) InsertDeclareAtEnd(v Value, diVarInfo, expr Metadata, bb BasicBlock) Value {
result := C.LLVMDIBuilderInsertDeclareAtEnd(d.ref, v.C, diVarInfo.C, expr.C, bb.C)
return Value{C: result}
}
// InsertValueAtEnd inserts a call to llvm.dbg.value at the end of the
// specified basic block for the given value and associated debug metadata.
func (d *DIBuilder) InsertValueAtEnd(v, diVarInfo, expr Value, offset uint64, bb BasicBlock) Value {
func (d *DIBuilder) InsertValueAtEnd(v Value, diVarInfo, expr Metadata, offset uint64, bb BasicBlock) Value {
result := C.LLVMDIBuilderInsertValueAtEnd(d.ref, v.C, C.uint64_t(offset), diVarInfo.C, expr.C, bb.C)
return Value{C: result}
}

View File

@ -55,6 +55,9 @@ type (
Use struct {
C C.LLVMUseRef
}
Metadata struct {
C C.LLVMMetadataRef
}
Attribute uint64
Opcode C.LLVMOpcode
TypeKind C.LLVMTypeKind
@ -80,6 +83,9 @@ func (c Use) IsNil() bool { return c.C == nil }
// helpers
func llvmTypeRefPtr(t *Type) *C.LLVMTypeRef { return (*C.LLVMTypeRef)(unsafe.Pointer(t)) }
func llvmValueRefPtr(t *Value) *C.LLVMValueRef { return (*C.LLVMValueRef)(unsafe.Pointer(t)) }
func llvmMetadataRefPtr(t *Metadata) *C.LLVMMetadataRef {
return (*C.LLVMMetadataRef)(unsafe.Pointer(t))
}
func llvmBasicBlockRefPtr(t *BasicBlock) *C.LLVMBasicBlockRef {
return (*C.LLVMBasicBlockRef)(unsafe.Pointer(t))
}
@ -99,6 +105,15 @@ func llvmValueRefs(values []Value) (*C.LLVMValueRef, C.unsigned) {
return pt, ptlen
}
func llvmMetadataRefs(mds []Metadata) (*C.LLVMMetadataRef, C.unsigned) {
var pt *C.LLVMMetadataRef
ptlen := C.unsigned(len(mds))
if ptlen > 0 {
pt = llvmMetadataRefPtr(&mds[0])
}
return pt, ptlen
}
//-------------------------------------------------------------------------
// llvm.Attribute
//-------------------------------------------------------------------------
@ -421,10 +436,10 @@ func (m Module) SetInlineAsm(asm string) {
C.LLVMSetModuleInlineAsm(m.C, casm)
}
func (m Module) AddNamedMetadataOperand(name string, operand Value) {
func (m Module) AddNamedMetadataOperand(name string, operand Metadata) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
C.LLVMAddNamedMetadataOperand(m.C, cname, operand.C)
C.LLVMAddNamedMetadataOperand2(m.C, cname, operand.C)
}
func (m Module) Context() (c Context) {
@ -628,8 +643,8 @@ func (v Value) Metadata(kind int) (rv Value) {
rv.C = C.LLVMGetMetadata(v.C, C.unsigned(kind))
return
}
func (v Value) SetMetadata(kind int, node Value) {
C.LLVMSetMetadata(v.C, C.unsigned(kind), node.C)
func (v Value) SetMetadata(kind int, node Metadata) {
C.LLVMSetMetadata2(v.C, C.unsigned(kind), node.C)
}
// Conversion functions.
@ -723,15 +738,15 @@ func (v Value) IsUndef() bool { return C.LLVMIsUndef(v.C) != 0 }
func ConstPointerNull(t Type) (v Value) { v.C = C.LLVMConstPointerNull(t.C); return }
// Operations on metadata
func (c Context) MDString(str string) (v Value) {
func (c Context) MDString(str string) (md Metadata) {
cstr := C.CString(str)
defer C.free(unsafe.Pointer(cstr))
v.C = C.LLVMMDStringInContext(c.C, cstr, C.unsigned(len(str)))
md.C = C.LLVMMDString2(c.C, cstr, C.unsigned(len(str)))
return
}
func (c Context) MDNode(vals []Value) (v Value) {
ptr, nvals := llvmValueRefs(vals)
v.C = C.LLVMMDNodeInContext(c.C, ptr, nvals)
func (c Context) MDNode(mds []Metadata) (md Metadata) {
ptr, nvals := llvmMetadataRefs(mds)
md.C = C.LLVMMDNode2(c.C, ptr, nvals)
return
}
@ -1177,8 +1192,9 @@ func (b Builder) InsertWithName(instr Value, name string) {
func (b Builder) Dispose() { C.LLVMDisposeBuilder(b.C) }
// Metadata
func (b Builder) SetCurrentDebugLocation(v Value) { C.LLVMSetCurrentDebugLocation(b.C, v.C) }
func (b Builder) CurrentDebugLocation() (v Value) { v.C = C.LLVMGetCurrentDebugLocation(b.C); return }
func (b Builder) SetCurrentDebugLocation(line, col uint, scope, inlinedAt Metadata) {
C.LLVMSetCurrentDebugLocation2(b.C, C.unsigned(line), C.unsigned(col), scope.C, inlinedAt.C)
}
func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) }
func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value {
f := module.NamedFunction("llvm.dbg.declare")