Skip to content

Commit

Permalink
Introducing LLVMMetadataRef
Browse files Browse the repository at this point in the history
Summary:
This seems like an uncontroversial first step toward providing access to the metadata hierarchy that now exists in LLVM. This should allow for good debug info support from C.

Future plans are to deprecate API that take mixed bags of values and metadata (mainly the LLVMMDNode family of functions) and migrate the rest toward the use of LLVMMetadataRef.

Once this is in place, mapping of DIBuilder will be able to start.

Reviewers: mehdi_amini, echristo, whitequark, jketema, Wallbraker

Reviewed By: Wallbraker

Subscribers: Eugene.Zelenko, axw, mehdi_amini, llvm-commits

Differential Revision: https://reviews.llvm.org/D19448

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@300447 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
deadalnix committed Apr 17, 2017
1 parent 63c0d1d commit 603e72e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
11 changes: 0 additions & 11 deletions bindings/go/llvm/IRBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
extern "C" {
#endif

typedef struct LLVMOpaqueMetadata *LLVMMetadataRef;
struct LLVMDebugLocMetadata{
unsigned Line;
unsigned Col;
Expand Down Expand Up @@ -59,16 +58,6 @@ void LLVMSetSubprogram(LLVMValueRef Fn, LLVMMetadataRef SP);
#ifdef __cplusplus
}

namespace llvm {

DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMMetadataRef)

inline Metadata **unwrap(LLVMMetadataRef *Vals) {
return reinterpret_cast<Metadata**>(Vals);
}

}

#endif

#endif
10 changes: 10 additions & 0 deletions include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -2130,6 +2130,16 @@ LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
*/
LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count);

/**
* Obtain a Metadata as a Value.
*/
LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD);

/**
* Obtain a Value as a Metadata.
*/
LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val);

/**
* Obtain the underlying string from a MDString value.
*
Expand Down
7 changes: 7 additions & 0 deletions include/llvm-c/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ typedef struct LLVMOpaqueValue *LLVMValueRef;
*/
typedef struct LLVMOpaqueBasicBlock *LLVMBasicBlockRef;

/**
* Represents an LLVM Metadata.
*
* This models llvm::Metadata.
*/
typedef struct LLVMOpaqueMetadata *LLVMMetadataRef;

/**
* Represents an LLVM basic block builder.
*
Expand Down
9 changes: 9 additions & 0 deletions include/llvm/IR/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CBindingWrapping.h"
#include "llvm/Support/ErrorHandling.h"
#include <cassert>
#include <cstddef>
Expand Down Expand Up @@ -133,6 +134,14 @@ class Metadata {
/// @}
};

// Create wrappers for C Binding types (see CBindingWrapping.h).
DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMMetadataRef)

// Specialized opaque metadata conversions.
inline Metadata **unwrap(LLVMMetadataRef *MDs) {
return reinterpret_cast<Metadata**>(MDs);
}

#define HANDLE_METADATA(CLASS) class CLASS;
#include "llvm/IR/Metadata.def"

Expand Down
13 changes: 13 additions & 0 deletions lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,19 @@ LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
}

LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMMetadataRef MD) {
return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
}

LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) {
auto *V = unwrap(Val);
if (auto *C = dyn_cast<Constant>(V))
return wrap(ConstantAsMetadata::get(C));
if (auto *MAV = dyn_cast<MetadataAsValue>(V))
return wrap(MAV->getMetadata());
return wrap(ValueAsMetadata::get(V));
}

const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
Expand Down

0 comments on commit 603e72e

Please sign in to comment.