Index: bindings/go/llvm/IRBindings.h =================================================================== --- bindings/go/llvm/IRBindings.h +++ bindings/go/llvm/IRBindings.h @@ -26,8 +26,6 @@ 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 Index: include/llvm-c/Core.h =================================================================== --- include/llvm-c/Core.h +++ include/llvm-c/Core.h @@ -645,7 +645,7 @@ * @see llvm::MDNode::addOperand() */ void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name, - LLVMValueRef Val); + LLVMMetadataRef MD); /** * Add a function to a module under a specified name. @@ -1300,8 +1300,8 @@ LLVMValueRef LLVMIsA##name(LLVMValueRef Val); LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DECLARE_VALUE_CAST) -LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val); -LLVMValueRef LLVMIsAMDString(LLVMValueRef Val); +LLVMMetadataRef LLVMIsAMDNode(LLVMMetadataRef MD); +LLVMMetadataRef LLVMIsAMDString(LLVMMetadataRef MD); /** * @} @@ -1896,6 +1896,20 @@ void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn); /** + * Obtain the DISubprogram attached to the function. + * + * @see llvm::Function::getSubprogram() + */ +LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Fn); + +/** + * Set the DISubprogram attached to the function. + * + * @see llvm::Function::setSubprogram() + */ +void LLVMSetSubprogram(LLVMValueRef Fn, LLVMMetadataRef SP); + +/** * Obtain the ID number from a function instance. * * @see llvm::Function::getIntrinsicID() @@ -2101,26 +2115,38 @@ * string content is copied, so the backing memory can be freed after * this function returns. */ -LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str, - unsigned SLen); +LLVMMetadataRef LLVMMDStringInContext(LLVMContextRef C, const char *Str, + unsigned SLen); /** * Obtain a MDString value from the global context. */ -LLVMValueRef LLVMMDString(const char *Str, unsigned SLen); +LLVMMetadataRef LLVMMDString(const char *Str, unsigned SLen); /** * Obtain a MDNode value from a context. * * The returned value corresponds to the llvm::MDNode class. */ -LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals, - unsigned Count); +LLVMMetadataRef LLVMMDNodeInContext(LLVMContextRef C, LLVMMetadataRef *MDs, + unsigned Count); /** * Obtain a MDNode value from the global context. */ -LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count); +LLVMMetadataRef LLVMMDNode(LLVMMetadataRef *MDs, unsigned Count); + +/** + * Obtain a Metadata as a Value. + */ +LLVMValueRef LLVMMetadataAsValueInContext(LLVMContextRef C, + LLVMMetadataRef MD); +LLVMValueRef LLVMMetadataAsValue(LLVMMetadataRef MD); + +/** + * Obtain a Value as a Metadata. + */ +LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val); /** * Obtain the underlying string from a MDString value. @@ -2129,7 +2155,7 @@ * @param Length Memory address which will hold length of returned string. * @return String data in MDString. */ -const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length); +const char *LLVMGetMDString(LLVMMetadataRef V, unsigned *Length); /** * Obtain the number of operands from an MDNode value. @@ -2137,7 +2163,7 @@ * @param V MDNode to get number of operands from. * @return Number of operands of the MDNode. */ -unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V); +unsigned LLVMGetMDNodeNumOperands(LLVMMetadataRef V); /** * Obtain the given MDNode's operands. @@ -2147,12 +2173,322 @@ * LLVMValueRefs. This memory will be populated with the LLVMValueRefs of the * MDNode's operands. * - * @param V MDNode to get the operands from. + * @param MD MDNode to get the operands from. * @param Dest Destination array for operands. */ -void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest); +void LLVMGetMDNodeOperands(LLVMMetadataRef MD, LLVMMetadataRef *Dest); /** + * Dump a representation of a metadata to stderr. + * + * @see llvm::Metadata::dump() + */ +void LLVMDumpMetadata(LLVMMetadataRef MD); + +/** + * @} + */ + +/** + * @defgroup LLVMCoreDebugMetadata Metadata + * + * @{ + */ + +LLVMMetadataRef LLVMGetDILocation(unsigned Line, unsigned Column, + LLVMMetadataRef Scope); +LLVMMetadataRef LLVMGetDILocationInContext(LLVMContextRef C, unsigned Line, + unsigned Column, + LLVMMetadataRef Scope); + +LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M); +void LLVMFinalizeDIBuilder(LLVMDIBuilderRef B); +void LLVMDisposeDIBuilder(LLVMDIBuilderRef B); + +LLVMMetadataRef LLVMDICreateCompileUnit(LLVMDIBuilderRef B, unsigned Lang, + const char *File, const char *Dir, + const char *Producer, + LLVMBool isOptimized, + const char *Flags, unsigned RV, + const char *SplitName); +LLVMMetadataRef LLVMDICreateFile(LLVMDIBuilderRef B, const char *FileName, + const char *Directory); +LLVMMetadataRef LLVMDICreateEnumerator(LLVMDIBuilderRef B, const char *Name, + int64_t Val); +LLVMMetadataRef LLVMDICreateUnspecifiedType(LLVMDIBuilderRef B, + const char *Name); +LLVMMetadataRef LLVMDICreateNullPtrType(LLVMDIBuilderRef B); +LLVMMetadataRef LLVMDICreateBasicType(LLVMDIBuilderRef B, const char *Name, + uint64_t SizeInBits, + uint64_t AlignInBits, unsigned Encoding); +LLVMMetadataRef LLVMDICreateQualifiedType(LLVMDIBuilderRef B, unsigned Tag, + LLVMMetadataRef FromTy); +LLVMMetadataRef LLVMDICreatePointerType(LLVMDIBuilderRef B, + LLVMMetadataRef PointeeTy, + uint64_t SizeInBits, + uint64_t AlignInBits, + const char *Name); +LLVMMetadataRef LLVMDICreateMemberPointerType(LLVMDIBuilderRef B, + LLVMMetadataRef PointeeTy, + LLVMMetadataRef Class, + uint64_t SizeInBits, + uint64_t AlignInBits); +LLVMMetadataRef LLVMDICreateReferenceType(LLVMDIBuilderRef B, unsigned Tag, + LLVMMetadataRef RTy); +LLVMMetadataRef LLVMDICreateTypedef(LLVMDIBuilderRef B, LLVMMetadataRef Ty, + const char *Name, LLVMMetadataRef File, + unsigned LineNo, LLVMMetadataRef Context); +LLVMMetadataRef LLVMDICreateFriend(LLVMDIBuilderRef B, LLVMMetadataRef Ty, + LLVMMetadataRef FriendTy); +LLVMMetadataRef LLVMDICreateInheritance(LLVMDIBuilderRef B, LLVMMetadataRef Ty, + LLVMMetadataRef BaseTy, + uint64_t BaseOffset, unsigned Flags); +LLVMMetadataRef LLVMDICreateMemberType(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, const char *Name, + LLVMMetadataRef File, unsigned LineNo, + uint64_t SizeInBits, + uint64_t AlignInBits, + uint64_t OffsetInBits, + unsigned Flags, LLVMMetadataRef Ty); +LLVMMetadataRef LLVMDICreateStaticMemberType(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, + LLVMMetadataRef File, + unsigned LineNo, + LLVMMetadataRef Ty, + unsigned Flags, + LLVMValueRef Val); +LLVMMetadataRef LLVMDICreateObjCIVar(LLVMDIBuilderRef B, const char *Name, + LLVMMetadataRef File, unsigned LineNo, + uint64_t SizeInBits, uint64_t AlignInBits, + uint64_t OffsetInBits, unsigned Flags, + LLVMMetadataRef Ty, + LLVMMetadataRef PropertyNode); +LLVMMetadataRef LLVMDICreateObjCProperty(LLVMDIBuilderRef B, const char *Name, + LLVMMetadataRef File, unsigned LineNo, + const char *GetterName, + const char *SetterName, + unsigned PropertyAttributes, + LLVMMetadataRef Ty); +LLVMMetadataRef LLVMDICreateClassType(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, const char *Name, + LLVMMetadataRef File, unsigned LineNo, + uint64_t SizeInBits, + uint64_t AlignInBits, + uint64_t OffsetInBits, unsigned Flags, + LLVMMetadataRef DerivedFrom, + LLVMMetadataRef Elements, + LLVMMetadataRef VTableHolder, + LLVMMetadataRef TemplateParms, + const char *UniqueIdentifier); +LLVMMetadataRef LLVMDICreateStructType(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, const char *Name, + LLVMMetadataRef File, unsigned LineNo, + uint64_t SizeInBits, + uint64_t AlignInBits, + unsigned Flags, + LLVMMetadataRef DerivedFrom, + LLVMMetadataRef Elements, + unsigned RunTimeLang, + LLVMMetadataRef VTableHolder, + const char *UniqueIdentifier); +LLVMMetadataRef LLVMDICreateUnionType(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, const char *Name, + LLVMMetadataRef File, unsigned LineNo, + uint64_t SizeInBits, + uint64_t AlignInBits, + unsigned Flags, LLVMMetadataRef Elements, + unsigned RunTimeLang, + const char *UniqueIdentifier); +LLVMMetadataRef LLVMDICreateTemplateTypeParameter(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, + LLVMMetadataRef Ty); +LLVMMetadataRef LLVMDICreateTemplateValueParameter(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, + LLVMMetadataRef Ty, + LLVMValueRef Val); +LLVMMetadataRef LLVMDICreateTemplateTemplateParameter(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, + LLVMMetadataRef Ty, + const char *Val); +LLVMMetadataRef LLVMDICreateTemplateParameterPack(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, + LLVMMetadataRef Ty, + LLVMMetadataRef Val); +LLVMMetadataRef LLVMDICreateArrayType(LLVMDIBuilderRef B, uint64_t Size, + uint64_t AlignInBits, LLVMMetadataRef Ty, + LLVMMetadataRef Subscripts); +LLVMMetadataRef LLVMDICreateVectorType(LLVMDIBuilderRef B, uint64_t Size, + uint64_t AlignInBits, + LLVMMetadataRef Ty, + LLVMMetadataRef Subscripts); +LLVMMetadataRef LLVMDICreateEnumerationType(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, + LLVMMetadataRef File, + unsigned LineNo, + uint64_t SizeInBits, + uint64_t AlignInBits, + LLVMMetadataRef Elements, + LLVMMetadataRef UnderlyingType, + const char *UniqueIdentifier); +LLVMMetadataRef LLVMDICreateSubroutineType(LLVMDIBuilderRef B, + LLVMMetadataRef ParameterTypes, + unsigned Flags); +LLVMMetadataRef LLVMDICreateExternalTypeRef(LLVMDIBuilderRef B, + unsigned Tag, LLVMMetadataRef File, + const char *UniqueIdentifier); +LLVMMetadataRef LLVMDICreateArtificialType(LLVMDIBuilderRef B, + LLVMMetadataRef Ty); +LLVMMetadataRef LLVMDICreateObjectPointerType(LLVMDIBuilderRef B, + LLVMMetadataRef Ty); +LLVMMetadataRef LLVMDICreateForwardDecl(LLVMDIBuilderRef B, unsigned Tag, + const char *Name, + LLVMMetadataRef Scope, + LLVMMetadataRef F, unsigned Line, + unsigned RuntimeLang, + uint64_t SizeInBits, + uint64_t AlignInBits, + const char *UniqueIdentifier); +LLVMMetadataRef LLVMDICreateReplaceableCompositeType(LLVMDIBuilderRef B, + unsigned Tag, + const char *Name, + LLVMMetadataRef Scope, + LLVMMetadataRef F, + unsigned Line, + unsigned RuntimeLang, + uint64_t SizeInBits, + uint64_t AlignInBits, + unsigned Flags, + const char *UniqueIdentifier); +void LLVMDIRetainType(LLVMDIBuilderRef B, LLVMMetadataRef T); +LLVMMetadataRef LLVMDICreateUnspecifiedParameter(LLVMDIBuilderRef B); +LLVMMetadataRef LLVMDIGetOrCreateArray(LLVMDIBuilderRef B, + LLVMMetadataRef *Elements, + unsigned ElementCount); +LLVMMetadataRef LLVMDIGetOrCreateTypeArray(LLVMDIBuilderRef B, + LLVMMetadataRef *Elements, + unsigned ElementCount); +LLVMMetadataRef LLVMDIGetOrCreateSubrange(LLVMDIBuilderRef B, int64_t Lo, + int64_t Count); +LLVMMetadataRef LLVMDICreateGlobalVariable(LLVMDIBuilderRef B, + LLVMMetadataRef Context, + const char *Name, + const char *LinkageName, + LLVMMetadataRef File, + unsigned LineNo, LLVMMetadataRef Ty, + LLVMBool isLocalToUnit, + LLVMValueRef Val, + LLVMMetadataRef Decl); +LLVMMetadataRef LLVMDICreateTempGlobalVariableFwdDecl(LLVMDIBuilderRef B, + LLVMMetadataRef Context, + const char *Name, + const char *LinkageName, + LLVMMetadataRef File, + unsigned LineNo, + LLVMMetadataRef Ty, + LLVMBool isLocalToUnit, + LLVMValueRef Val, + LLVMMetadataRef Decl); +LLVMMetadataRef LLVMDICreateAutoVariable(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, + LLVMMetadataRef File, unsigned LineNo, + LLVMMetadataRef Ty, + LLVMBool AlwaysPreserve, + unsigned Flags); +LLVMMetadataRef LLVMDICreateParameterVariable(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, unsigned ArgNo, + LLVMMetadataRef File, + unsigned LineNo, + LLVMMetadataRef Ty, + LLVMBool AlwaysPreserve, + unsigned Flags); +// createExpression, createBitPieceExpression ??!? +LLVMMetadataRef LLVMDICreateExpression(LLVMDIBuilderRef B); +LLVMMetadataRef LLVMDICreateFunction(LLVMDIBuilderRef B, LLVMMetadataRef Scope, + const char *Name, const char *LinkageName, + LLVMMetadataRef File, unsigned LineNo, + LLVMMetadataRef Ty, + LLVMBool isLocalToUnit, + LLVMBool isDefinition, unsigned ScopeLine, + unsigned Flags, LLVMBool isOptimized, + LLVMMetadataRef TParams, + LLVMMetadataRef Decl); +LLVMMetadataRef LLVMDICreateTempFunctionFwdDecl(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, + const char *LinkageName, + LLVMMetadataRef File, + unsigned LineNo, + LLVMMetadataRef Ty, + LLVMBool isLocalToUnit, + LLVMBool isDefinition, + unsigned ScopeLine, + unsigned Flags, + LLVMBool isOptimized, + LLVMMetadataRef TParams, + LLVMMetadataRef Decl); +LLVMMetadataRef LLVMDICreateMethod(LLVMDIBuilderRef B, LLVMMetadataRef Scope, + const char *Name, const char *LinkageName, + LLVMMetadataRef File, unsigned LineNo, + LLVMMetadataRef Ty, LLVMBool isLocalToUnit, + LLVMBool isDefinition, unsigned Virtuality, + unsigned VTableIndex, + LLVMMetadataRef VTableHolder, + unsigned Flags, LLVMBool isOptimized, + LLVMMetadataRef TParams); +LLVMMetadataRef LLVMDICreateNameSpace(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, const char *Name, + LLVMMetadataRef File, unsigned LineNo); +LLVMMetadataRef LLVMDICreateModule(LLVMDIBuilderRef B, LLVMMetadataRef Scope, + const char *Name, + const char *ConfigurationMacros, + const char *IncludePath, + const char *ISysRoot); +LLVMMetadataRef LLVMDICreateLexicalBlockFile(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + LLVMMetadataRef File, + unsigned Discriminator); +LLVMMetadataRef LLVMDICreateLexicalBlock(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + LLVMMetadataRef File, + unsigned Line, unsigned Col); +// ImportedModule ??!? Declaration ? +LLVMValueRef LLVMDIInsertDeclareAtEnd(LLVMDIBuilderRef B, LLVMValueRef Storage, + LLVMMetadataRef VarInfo, + LLVMMetadataRef Expr, LLVMMetadataRef DL, + LLVMBasicBlockRef InsertAtEnd); +LLVMValueRef LLVMDIInsertDeclareBefore(LLVMDIBuilderRef B, + LLVMValueRef Storage, + LLVMMetadataRef VarInfo, + LLVMMetadataRef Expr, + LLVMMetadataRef DL, + LLVMValueRef InsertBefore); +LLVMValueRef LLVMDIInsertDbgValueIntrinsicAtEnd(LLVMDIBuilderRef B, + LLVMValueRef Val, + uint64_t Offset, + LLVMMetadataRef VarInfo, + LLVMMetadataRef Expr, + LLVMMetadataRef DL, + LLVMBasicBlockRef InsertAtEnd); +LLVMValueRef LLVMDIInsertDbgValueIntrinsicBefore(LLVMDIBuilderRef B, + LLVMValueRef Val, + uint64_t Offset, + LLVMMetadataRef VarInfo, + LLVMMetadataRef Expr, + LLVMMetadataRef DL, + LLVMValueRef InsertBefore); +void LLVMDIReplaceVTableHolder(LLVMDIBuilderRef B, LLVMMetadataRef *T, + LLVMMetadataRef VTableHolder); +void LLVMDIReplaceArrays(LLVMDIBuilderRef B, LLVMMetadataRef *T, + LLVMMetadataRef Elements, LLVMMetadataRef TParams); +/** * @} */ @@ -2379,12 +2715,12 @@ /** * Return metadata associated with an instruction value. */ -LLVMValueRef LLVMGetMetadata(LLVMValueRef Val, unsigned KindID); +LLVMMetadataRef LLVMGetMetadata(LLVMValueRef Val, unsigned KindID); /** * Set metadata associated with an instruction value. */ -void LLVMSetMetadata(LLVMValueRef Val, unsigned KindID, LLVMValueRef Node); +void LLVMSetMetadata(LLVMValueRef Val, unsigned KindID, LLVMMetadataRef MD); /** * Obtain the basic block to which an instruction belongs. @@ -2787,8 +3123,8 @@ void LLVMDisposeBuilder(LLVMBuilderRef Builder); /* Metadata */ -void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L); -LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder); +void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMMetadataRef L); +LLVMMetadataRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder); void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst); /* Terminators */ Index: include/llvm-c/Types.h =================================================================== --- include/llvm-c/Types.h +++ include/llvm-c/Types.h @@ -90,6 +90,20 @@ typedef struct LLVMOpaqueBuilder *LLVMBuilderRef; /** + * Represents an LLVM debug infos builder. + * + * This models llvm::DIBuilder. + */ +typedef struct LLVMOpaqueDIBuilder *LLVMDIBuilderRef; + +/** + * Represents an LLVM Metadata. + * + * This models llvm::Metadata. + */ +typedef struct LLVMOpaqueMetadata *LLVMMetadataRef; + +/** * Interface used to provide a module to JIT or interpreter. * This is now just a synonym for llvm::Module, but we have to keep using the * different type to keep binary compatibility. Index: include/llvm/IR/DIBuilder.h =================================================================== --- include/llvm/IR/DIBuilder.h +++ include/llvm/IR/DIBuilder.h @@ -20,6 +20,7 @@ #include "llvm/IR/DebugInfo.h" #include "llvm/IR/TrackingMDRef.h" #include "llvm/IR/ValueHandle.h" +#include "llvm/Support/CBindingWrapping.h" #include "llvm/Support/DataTypes.h" namespace llvm { @@ -712,6 +713,10 @@ return Replacement; } }; + +// Create wrappers for C Binding types (see CBindingWrapping.h). +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef) + } // end namespace llvm #endif Index: include/llvm/IR/Metadata.h =================================================================== --- include/llvm/IR/Metadata.h +++ include/llvm/IR/Metadata.h @@ -25,6 +25,7 @@ #include "llvm/IR/Constant.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Value.h" +#include "llvm/Support/CBindingWrapping.h" #include "llvm/Support/ErrorHandling.h" #include @@ -147,6 +148,15 @@ /// @} }; +// Create wrappers for C Binding types (see CBindingWrapping.h). +DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMMetadataRef) + +/* Specialized opaque value conversions. + */ +inline Metadata **unwrap(LLVMMetadataRef *Vals) { + return reinterpret_cast(Vals); +} + #define HANDLE_METADATA(CLASS) class CLASS; #include "llvm/IR/Metadata.def" Index: lib/IR/Core.cpp =================================================================== --- lib/IR/Core.cpp +++ lib/IR/Core.cpp @@ -17,9 +17,11 @@ #include "llvm/IR/Attributes.h" #include "llvm/IR/CallSite.h" #include "llvm/IR/Constants.h" +#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/DiagnosticPrinter.h" +#include "llvm/IR/DIBuilder.h" #include "llvm/IR/GlobalAlias.h" #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/IRBuilder.h" @@ -605,32 +607,16 @@ return unwrap(Inst)->hasMetadata(); } -LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) { +LLVMMetadataRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) { auto *I = unwrap(Inst); assert(I && "Expected instruction"); if (auto *MD = I->getMetadata(KindID)) - return wrap(MetadataAsValue::get(I->getContext(), MD)); + return wrap(MD); return nullptr; } -// MetadataAsValue uses a canonical format which strips the actual MDNode for -// MDNode with just a single constant value, storing just a ConstantAsMetadata -// This undoes this canonicalization, reconstructing the MDNode. -static MDNode *extractMDNode(MetadataAsValue *MAV) { - Metadata *MD = MAV->getMetadata(); - assert((isa(MD) || isa(MD)) && - "Expected a metadata node or a canonicalized constant"); - - if (MDNode *N = dyn_cast(MD)) - return N; - - return MDNode::get(MAV->getContext(), MD); -} - -void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) { - MDNode *N = Val ? extractMDNode(unwrap(Val)) : nullptr; - - unwrap(Inst)->setMetadata(KindID, N); +void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD) { + unwrap(Inst)->setMetadata(KindID, unwrap(MD)); } /*--.. Conversion functions ................................................--*/ @@ -642,19 +628,17 @@ LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST) -LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) { - if (auto *MD = dyn_cast_or_null(unwrap(Val))) - if (isa(MD->getMetadata()) || - isa(MD->getMetadata())) - return Val; +LLVMMetadataRef LLVMIsAMDNode(LLVMMetadataRef MD) { + auto *Metadata = unwrap(MD); + if (auto *N = dyn_cast_or_null(Metadata)) + return wrap(N); + if (auto *V = dyn_cast_or_null(Metadata)) + return wrap(V); return nullptr; } -LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) { - if (auto *MD = dyn_cast_or_null(unwrap(Val))) - if (isa(MD->getMetadata())) - return Val; - return nullptr; +LLVMMetadataRef LLVMIsAMDString(LLVMMetadataRef MD) { + return wrap(dyn_cast_or_null(unwrap(MD))); } /*--.. Operations on Uses ..................................................--*/ @@ -719,8 +703,7 @@ int LLVMGetNumOperands(LLVMValueRef Val) { Value *V = unwrap(Val); if (isa(V)) - return LLVMGetMDNodeNumOperands(Val); - + return LLVMGetMDNodeNumOperands(LLVMValueAsMetadata(Val)); return cast(V)->getNumOperands(); } @@ -759,107 +742,702 @@ /*--.. Operations on metadata nodes ........................................--*/ -LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str, - unsigned SLen) { - LLVMContext &Context = *unwrap(C); - return wrap(MetadataAsValue::get( - Context, MDString::get(Context, StringRef(Str, SLen)))); +LLVMMetadataRef LLVMMDStringInContext(LLVMContextRef C, const char *Str, + unsigned SLen) { + return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen))); } -LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) { +LLVMMetadataRef LLVMMDString(const char *Str, unsigned SLen) { return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen); } -LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals, - unsigned Count) { - LLVMContext &Context = *unwrap(C); - SmallVector MDs; - for (auto *OV : makeArrayRef(Vals, Count)) { - Value *V = unwrap(OV); - Metadata *MD; - if (!V) - MD = nullptr; - else if (auto *C = dyn_cast(V)) - MD = ConstantAsMetadata::get(C); - else if (auto *MDV = dyn_cast(V)) { - MD = MDV->getMetadata(); - assert(!isa(MD) && "Unexpected function-local metadata " - "outside of direct argument to call"); - } else { - // This is function-local metadata. Pretend to make an MDNode. - assert(Count == 1 && - "Expected only one operand to function-local metadata"); - return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V))); - } +LLVMMetadataRef LLVMMDNodeInContext(LLVMContextRef C, LLVMMetadataRef *MDs, + unsigned Count) { + return wrap(MDNode::get(*unwrap(C), makeArrayRef(unwrap(MDs), Count))); +} - MDs.push_back(MD); - } - return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs))); +LLVMMetadataRef LLVMMDNode(LLVMMetadataRef *MDs, unsigned Count) { + return LLVMMDNodeInContext(LLVMGetGlobalContext(), MDs, Count); } -LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) { - return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count); +LLVMValueRef LLVMMetadataAsValueInContext(LLVMContextRef C, + LLVMMetadataRef MD) { + auto *M = unwrap(MD); + if (auto *VAM = dyn_cast(M)) + return wrap(VAM->getValue()); + return wrap(MetadataAsValue::get(*unwrap(C), M)); } -const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) { - if (const auto *MD = dyn_cast(unwrap(V))) - if (const MDString *S = dyn_cast(MD->getMetadata())) { - *Length = S->getString().size(); - return S->getString().data(); - } - *Length = 0; - return nullptr; +LLVMValueRef LLVMMetadataAsValue(LLVMMetadataRef MD) { + return LLVMMetadataAsValueInContext(LLVMGetGlobalContext(), MD); } -unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) -{ - auto *MD = cast(unwrap(V)); - if (isa(MD->getMetadata())) +LLVMMetadataRef LLVMValueAsMetadata(LLVMValueRef Val) { + auto *V = unwrap(Val); + if (auto *C = dyn_cast(V)) + return wrap(ConstantAsMetadata::get(C)); + if (auto *MAV = dyn_cast(V)) + return wrap(MAV->getMetadata()); + return wrap(ValueAsMetadata::get(V)); +} + +const char *LLVMGetMDString(LLVMMetadataRef V, unsigned *Length) { + auto *S = unwrap(V); + *Length = S->getString().size(); + return S->getString().data(); +} + +unsigned LLVMGetMDNodeNumOperands(LLVMMetadataRef V) { + auto *MD = unwrap(V); + if (isa(MD)) return 1; - return cast(MD->getMetadata())->getNumOperands(); + return cast(MD)->getNumOperands(); } -void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) -{ - auto *MD = cast(unwrap(V)); - if (auto *MDV = dyn_cast(MD->getMetadata())) { - *Dest = wrap(MDV->getValue()); - return; - } - const auto *N = cast(MD->getMetadata()); +void LLVMGetMDNodeOperands(LLVMMetadataRef MD, LLVMMetadataRef *Dest) { + const auto *N = unwrap(MD); const unsigned numOperands = N->getNumOperands(); - LLVMContext &Context = unwrap(V)->getContext(); for (unsigned i = 0; i < numOperands; i++) - Dest[i] = getMDNodeOperandImpl(Context, N, i); + Dest[i] = wrap(N->getOperand(i)); } -unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name) -{ - if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) { +unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name) { + if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) return N->getNumOperands(); - } return 0; } -void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest) -{ +void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, + LLVMMetadataRef *Dest) { NamedMDNode *N = unwrap(M)->getNamedMetadata(name); if (!N) return; - LLVMContext &Context = unwrap(M)->getContext(); for (unsigned i=0;igetNumOperands();i++) - Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i))); + Dest[i] = wrap(N->getOperand(i)); } void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name, - LLVMValueRef Val) -{ + LLVMMetadataRef MD) { NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name); if (!N) return; - if (!Val) + if (!MD) return; - N->addOperand(extractMDNode(unwrap(Val))); + N->addOperand(unwrap(MD)); +} + +void LLVMDumpMetadata(LLVMMetadataRef MD) { + unwrap(MD)->dump(); +} + +/*--.. Operations on location nodes ........................................--*/ + +LLVMMetadataRef LLVMGetDILocation(unsigned Line, unsigned Column, + LLVMMetadataRef Scope) { + return LLVMGetDILocationInContext(LLVMGetGlobalContext(), + Line, Column, Scope); +} + +LLVMMetadataRef LLVMGetDILocationInContext(LLVMContextRef C, unsigned Line, + unsigned Column, + LLVMMetadataRef Scope) { + return wrap(DILocation::get(*unwrap(C), Line, Column, + unwrap(Scope))); +} + +LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) { + return wrap(new DIBuilder(*unwrap(M))); +} + +void LLVMFinalizeDIBuilder(LLVMDIBuilderRef B) { + unwrap(B)->finalize(); +} + +void LLVMDisposeDIBuilder(LLVMDIBuilderRef B) { + delete unwrap(B); +} + +LLVMMetadataRef LLVMDICreateCompileUnit(LLVMDIBuilderRef B, unsigned Lang, + const char *File, const char *Dir, + const char *Producer, + LLVMBool isOptimized, + const char *Flags, unsigned RV, + const char *SplitName) { + return wrap(unwrap(B)->createCompileUnit(Lang, File, Dir, Producer, + isOptimized, Flags, RV, SplitName)); +} + +LLVMMetadataRef LLVMDICreateFile(LLVMDIBuilderRef B, const char *FileName, + const char *Directory) { + return wrap(unwrap(B)->createFile(FileName, Directory)); +} + +LLVMMetadataRef LLVMDICreateEnumerator(LLVMDIBuilderRef B, const char *Name, + int64_t Val) { + return wrap(unwrap(B)->createEnumerator(Name, Val)); +} + +LLVMMetadataRef LLVMDICreateUnspecifiedType(LLVMDIBuilderRef B, + const char *Name) { + return wrap(unwrap(B)->createUnspecifiedType(Name)); +} + +LLVMMetadataRef LLVMDICreateNullPtrType(LLVMDIBuilderRef B) { + return wrap(unwrap(B)->createNullPtrType()); +} + +LLVMMetadataRef LLVMDICreateBasicType(LLVMDIBuilderRef B, const char *Name, + uint64_t SizeInBits, + uint64_t AlignInBits, + unsigned Encoding) { + return wrap(unwrap(B)->createBasicType(Name, SizeInBits, + AlignInBits, Encoding)); +} + +LLVMMetadataRef LLVMDICreateQualifiedType(LLVMDIBuilderRef B, unsigned Tag, + LLVMMetadataRef FromTy) { + return wrap(unwrap(B)->createQualifiedType(Tag, unwrap(FromTy))); +} + +LLVMMetadataRef LLVMDICreatePointerType(LLVMDIBuilderRef B, + LLVMMetadataRef PointeeTy, + uint64_t SizeInBits, + uint64_t AlignInBits, + const char *Name) { + auto *T = PointeeTy ? unwrap(PointeeTy) : nullptr; + return wrap(unwrap(B)->createPointerType(T, SizeInBits, AlignInBits, Name)); +} + +LLVMMetadataRef LLVMDICreateMemberPointerType(LLVMDIBuilderRef B, + LLVMMetadataRef PointeeTy, + LLVMMetadataRef Class, + uint64_t SizeInBits, + uint64_t AlignInBits) { + return wrap(unwrap(B)->createMemberPointerType(unwrap(PointeeTy), + unwrap(Class), + SizeInBits, AlignInBits)); +} + +LLVMMetadataRef LLVMDICreateReferenceType(LLVMDIBuilderRef B, unsigned Tag, + LLVMMetadataRef RTy) { + return wrap(unwrap(B)->createReferenceType(Tag, unwrap(RTy))); +} + +LLVMMetadataRef LLVMDICreateTypedef(LLVMDIBuilderRef B, LLVMMetadataRef Ty, + const char *Name, LLVMMetadataRef File, + unsigned LineNo, LLVMMetadataRef Context) { + return wrap(unwrap(B)->createTypedef(unwrap(Ty), Name, + unwrap(File), LineNo, + unwrap(Context))); +} + +LLVMMetadataRef LLVMDICreateFriend(LLVMDIBuilderRef B, LLVMMetadataRef Ty, + LLVMMetadataRef FriendTy) { + return wrap(unwrap(B)->createFriend(unwrap(Ty), + unwrap(FriendTy))); +} + +LLVMMetadataRef LLVMDICreateInheritance(LLVMDIBuilderRef B, LLVMMetadataRef Ty, + LLVMMetadataRef BaseTy, + uint64_t BaseOffset, unsigned Flags) { + return wrap(unwrap(B)->createInheritance(unwrap(Ty), + unwrap(BaseTy), BaseOffset, + Flags)); +} + +LLVMMetadataRef LLVMDICreateMemberType(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, const char *Name, + LLVMMetadataRef File, unsigned LineNo, + uint64_t SizeInBits, + uint64_t AlignInBits, + uint64_t OffsetInBits, + unsigned Flags, LLVMMetadataRef Ty) { + return wrap(unwrap(B)->createMemberType(unwrap(Scope), Name, + unwrap(File), LineNo, + SizeInBits, AlignInBits, OffsetInBits, + Flags, unwrap(Ty))); +} + +LLVMMetadataRef LLVMDICreateStaticMemberType(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, + LLVMMetadataRef File, + unsigned LineNo, + LLVMMetadataRef Ty, + unsigned Flags, + LLVMValueRef Val) { + return wrap(unwrap(B)->createStaticMemberType(unwrap(Scope), Name, + unwrap(File), LineNo, + unwrap(Ty), Flags, + unwrap(Val))); +} + +LLVMMetadataRef LLVMDICreateObjCIVar(LLVMDIBuilderRef B, const char *Name, + LLVMMetadataRef File, unsigned LineNo, + uint64_t SizeInBits, uint64_t AlignInBits, + uint64_t OffsetInBits, unsigned Flags, + LLVMMetadataRef Ty, + LLVMMetadataRef PropertyNode) { + return wrap(unwrap(B)->createObjCIVar(Name, unwrap(File), LineNo, + SizeInBits, AlignInBits, OffsetInBits, + Flags, unwrap(Ty), + unwrap(PropertyNode))); +} + +LLVMMetadataRef LLVMDICreateObjCProperty(LLVMDIBuilderRef B, const char *Name, + LLVMMetadataRef File, unsigned LineNo, + const char *GetterName, + const char *SetterName, + unsigned PropertyAttributes, + LLVMMetadataRef Ty) { + return wrap(unwrap(B)->createObjCProperty(Name, unwrap(File), LineNo, + GetterName, SetterName, + PropertyAttributes, + unwrap(Ty))); +} + +LLVMMetadataRef LLVMDICreateClassType(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, const char *Name, + LLVMMetadataRef File, unsigned LineNo, + uint64_t SizeInBits, + uint64_t AlignInBits, + uint64_t OffsetInBits, unsigned Flags, + LLVMMetadataRef DerivedFrom, + LLVMMetadataRef Elements, + LLVMMetadataRef VTableHolder, + LLVMMetadataRef TemplateParms, + const char *UniqueIdentifier) { + auto *DF = DerivedFrom ? unwrap(DerivedFrom) : nullptr; + auto Elts = DINodeArray(unwrap(Elements)); + auto *VTH = VTableHolder ? unwrap(VTableHolder) : nullptr; + auto *TPS = TemplateParms ? unwrap(TemplateParms) : nullptr; + return wrap(unwrap(B)->createClassType(unwrap(Scope), Name, + unwrap(File), LineNo, + SizeInBits, AlignInBits, OffsetInBits, + Flags, DF, Elts, VTH, TPS, + UniqueIdentifier)); +} + +LLVMMetadataRef LLVMDICreateStructType(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, const char *Name, + LLVMMetadataRef File, unsigned LineNo, + uint64_t SizeInBits, + uint64_t AlignInBits, + unsigned Flags, + LLVMMetadataRef DerivedFrom, + LLVMMetadataRef Elements, + unsigned RunTimeLang, + LLVMMetadataRef VTableHolder, + const char *UniqueIdentifier) { + auto *DF = DerivedFrom ? unwrap(DerivedFrom) : nullptr; + auto Elts = DINodeArray(unwrap(Elements)); + auto *VTH = VTableHolder ? unwrap(VTableHolder) : nullptr; + return wrap(unwrap(B)->createStructType(unwrap(Scope), Name, + unwrap(File), LineNo, + SizeInBits, AlignInBits, Flags, + DF, Elts, RunTimeLang, VTH, + UniqueIdentifier)); +} + +LLVMMetadataRef LLVMDICreateUnionType(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, const char *Name, + LLVMMetadataRef File, unsigned LineNo, + uint64_t SizeInBits, + uint64_t AlignInBits, + unsigned Flags, LLVMMetadataRef Elements, + unsigned RunTimeLang, + const char *UniqueIdentifier) { + auto Elts = DINodeArray(unwrap(Elements)); + return wrap(unwrap(B)->createUnionType(unwrap(Scope), Name, + unwrap(File), LineNo, + SizeInBits, AlignInBits, Flags, + Elts, RunTimeLang, UniqueIdentifier)); +} + +LLVMMetadataRef LLVMDICreateTemplateTypeParameter(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, + LLVMMetadataRef Ty) { + return wrap(unwrap(B)->createTemplateTypeParameter(unwrap(Scope), + Name, + unwrap(Ty))); +} + +LLVMMetadataRef LLVMDICreateTemplateValueParameter(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, + LLVMMetadataRef Ty, + LLVMValueRef Val) { + return wrap(unwrap(B)->createTemplateValueParameter(unwrap(Scope), + Name, unwrap(Ty), + unwrap(Val))); +} + +LLVMMetadataRef LLVMDICreateTemplateTemplateParameter(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, + LLVMMetadataRef Ty, + const char *Val) { + return wrap(unwrap(B)->createTemplateTemplateParameter( + unwrap(Scope), Name, unwrap(Ty), Val)); +} + +LLVMMetadataRef LLVMDICreateTemplateParameterPack(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, + LLVMMetadataRef Ty, + LLVMMetadataRef Val) { + return wrap(unwrap(B)->createTemplateParameterPack(unwrap(Scope), + Name, unwrap(Ty), + DINodeArray( + unwrap(Val)))); +} + +LLVMMetadataRef LLVMDICreateArrayType(LLVMDIBuilderRef B, uint64_t Size, + uint64_t AlignInBits, LLVMMetadataRef Ty, + LLVMMetadataRef Subscripts) { + return wrap(unwrap(B)->createArrayType(Size, AlignInBits, unwrap(Ty), + DINodeArray(unwrap(Subscripts)))); +} + +LLVMMetadataRef LLVMDICreateVectorType(LLVMDIBuilderRef B, uint64_t Size, + uint64_t AlignInBits, + LLVMMetadataRef Ty, + LLVMMetadataRef Subscripts) { + return wrap(unwrap(B)->createVectorType(Size, AlignInBits, + unwrap(Ty), DINodeArray(unwrap(Subscripts)))); +} + +LLVMMetadataRef LLVMDICreateEnumerationType(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, + LLVMMetadataRef File, + unsigned LineNo, + uint64_t SizeInBits, + uint64_t AlignInBits, + LLVMMetadataRef Elements, + LLVMMetadataRef UnderlyingType, + const char *UniqueIdentifier) { + auto Elts = DINodeArray(unwrap(Elements)); + return wrap(unwrap(B)->createEnumerationType(unwrap(Scope), Name, + unwrap(File), LineNo, + SizeInBits, AlignInBits, Elts, + unwrap(UnderlyingType), + UniqueIdentifier)); +} + +LLVMMetadataRef LLVMDICreateSubroutineType(LLVMDIBuilderRef B, + LLVMMetadataRef ParameterTypes, + unsigned Flags) { + auto PTS = DITypeRefArray(unwrap(ParameterTypes)); + return wrap(unwrap(B)->createSubroutineType(PTS, Flags)); +} + +LLVMMetadataRef LLVMDICreateExternalTypeRef(LLVMDIBuilderRef B, + unsigned Tag, LLVMMetadataRef File, + const char *UniqueIdentifier) { + return wrap(unwrap(B)->createExternalTypeRef(Tag, unwrap(File), + UniqueIdentifier)); +} + +LLVMMetadataRef LLVMDICreateArtificialType(LLVMDIBuilderRef B, + LLVMMetadataRef Ty) { + return wrap(unwrap(B)->createArtificialType(unwrap(Ty))); +} + +LLVMMetadataRef LLVMDICreateObjectPointerType(LLVMDIBuilderRef B, + LLVMMetadataRef Ty) { + return wrap(unwrap(B)->createObjectPointerType(unwrap(Ty))); +} + +LLVMMetadataRef LLVMDICreateForwardDecl(LLVMDIBuilderRef B, unsigned Tag, + const char *Name, + LLVMMetadataRef Scope, + LLVMMetadataRef F, unsigned Line, + unsigned RuntimeLang, + uint64_t SizeInBits, + uint64_t AlignInBits, + const char *UniqueIdentifier) { + return wrap(unwrap(B)->createForwardDecl(Tag, Name, unwrap(Scope), + unwrap(F), Line, + RuntimeLang, SizeInBits, + AlignInBits, UniqueIdentifier)); +} + +LLVMMetadataRef LLVMDICreateReplaceableCompositeType(LLVMDIBuilderRef B, + unsigned Tag, + const char *Name, + LLVMMetadataRef Scope, + LLVMMetadataRef F, + unsigned Line, + unsigned RuntimeLang, + uint64_t SizeInBits, + uint64_t AlignInBits, + unsigned Flags, + const char *UniqueIdentifier) { + return wrap(unwrap(B)->createReplaceableCompositeType(Tag, Name, + unwrap(Scope), + unwrap(F), + Line, RuntimeLang, + SizeInBits, + AlignInBits, Flags, + UniqueIdentifier)); +} + +void LLVMDIRetainType(LLVMDIBuilderRef B, LLVMMetadataRef T) { + unwrap(B)->retainType(unwrap(T)); +} + +LLVMMetadataRef LLVMDICreateUnspecifiedParameter(LLVMDIBuilderRef B) { + return wrap(unwrap(B)->createUnspecifiedParameter()); +} + +LLVMMetadataRef LLVMDIGetOrCreateArray(LLVMDIBuilderRef B, + LLVMMetadataRef *Elements, + unsigned ElementCount) { + return wrap(unwrap(B)->getOrCreateArray(makeArrayRef(unwrap(Elements), + ElementCount)).get()); +} + +LLVMMetadataRef LLVMDIGetOrCreateTypeArray(LLVMDIBuilderRef B, + LLVMMetadataRef *Elements, + unsigned ElementCount) { + return wrap(unwrap(B)->getOrCreateTypeArray(makeArrayRef(unwrap(Elements), + ElementCount)).get()); +} + +LLVMMetadataRef LLVMDIGetOrCreateSubrange(LLVMDIBuilderRef B, int64_t Lo, + int64_t Count) { + return wrap(unwrap(B)->getOrCreateSubrange(Lo, Count)); +} + +LLVMMetadataRef LLVMDICreateGlobalVariable(LLVMDIBuilderRef B, + LLVMMetadataRef Context, + const char *Name, + const char *LinkageName, + LLVMMetadataRef File, + unsigned LineNo, LLVMMetadataRef Ty, + LLVMBool isLocalToUnit, + LLVMValueRef Val, + LLVMMetadataRef Decl) { + auto *D = Decl ? unwrap(Decl) : nullptr; + return wrap(unwrap(B)->createGlobalVariable(unwrap(Context), Name, + LinkageName, + unwrap(File), LineNo, + unwrap(Ty), + isLocalToUnit, + unwrap(Val), D)); +} + +LLVMMetadataRef LLVMDICreateTempGlobalVariableFwdDecl(LLVMDIBuilderRef B, + LLVMMetadataRef Context, + const char *Name, + const char *LinkageName, + LLVMMetadataRef File, + unsigned LineNo, + LLVMMetadataRef Ty, + LLVMBool isLocalToUnit, + LLVMValueRef Val, + LLVMMetadataRef Decl) { + auto *D = Decl ? unwrap(Decl) : nullptr; + return wrap(unwrap(B)->createTempGlobalVariableFwdDecl( + unwrap(Context), Name, LinkageName, + unwrap(File), LineNo, unwrap(Ty), + isLocalToUnit, unwrap(Val), D)); +} + +LLVMMetadataRef LLVMDICreateAutoVariable(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, + LLVMMetadataRef File, unsigned LineNo, + LLVMMetadataRef Ty, + LLVMBool AlwaysPreserve, + unsigned Flags) { + return wrap(unwrap(B)->createAutoVariable(unwrap(Scope), Name, + unwrap(File), LineNo, + unwrap(Ty), AlwaysPreserve, + Flags)); +} + +LLVMMetadataRef LLVMDICreateParameterVariable(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, unsigned ArgNo, + LLVMMetadataRef File, + unsigned LineNo, + LLVMMetadataRef Ty, + LLVMBool AlwaysPreserve, + unsigned Flags) { + return wrap(unwrap(B)->createParameterVariable(unwrap(Scope), Name, + ArgNo, unwrap(File), + LineNo, unwrap(Ty), + AlwaysPreserve, Flags)); +} + +// createExpression, createBitPieceExpression ??!? +LLVMMetadataRef LLVMDICreateExpression(LLVMDIBuilderRef B) { + return wrap(unwrap(B)->createExpression()); +} + +LLVMMetadataRef LLVMDICreateFunction(LLVMDIBuilderRef B, LLVMMetadataRef Scope, + const char *Name, const char *LinkageName, + LLVMMetadataRef File, unsigned LineNo, + LLVMMetadataRef Ty, + LLVMBool isLocalToUnit, + LLVMBool isDefinition, unsigned ScopeLine, + unsigned Flags, LLVMBool isOptimized, + LLVMMetadataRef TParams, + LLVMMetadataRef Decl) { + auto TP = TParams ? DINodeArray(unwrap(TParams)) : DINodeArray(); + auto *D = Decl ? unwrap(Decl) : nullptr; + return wrap(unwrap(B)->createFunction(unwrap(Scope), Name, + LinkageName, unwrap(File), + LineNo, unwrap(Ty), + isLocalToUnit, isDefinition, ScopeLine, + Flags, isOptimized, TP.get(), D)); +} + +LLVMMetadataRef LLVMDICreateTempFunctionFwdDecl(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + const char *Name, + const char *LinkageName, + LLVMMetadataRef File, + unsigned LineNo, + LLVMMetadataRef Ty, + LLVMBool isLocalToUnit, + LLVMBool isDefinition, + unsigned ScopeLine, + unsigned Flags, + LLVMBool isOptimized, + LLVMMetadataRef TParams, + LLVMMetadataRef Decl) { + auto TP = TParams ? DINodeArray(unwrap(TParams)) : nullptr; + auto *D = Decl ? unwrap(Decl) : nullptr; + return wrap(unwrap(B)->createTempFunctionFwdDecl(unwrap(Scope), + Name, LinkageName, + unwrap(File), + LineNo, + unwrap(Ty), + isLocalToUnit, isDefinition, + ScopeLine, Flags, + isOptimized, TP.get(), D)); +} + +LLVMMetadataRef LLVMDICreateMethod(LLVMDIBuilderRef B, LLVMMetadataRef Scope, + const char *Name, const char *LinkageName, + LLVMMetadataRef File, unsigned LineNo, + LLVMMetadataRef Ty, LLVMBool isLocalToUnit, + LLVMBool isDefinition, unsigned Virtuality, + unsigned VTableIndex, + LLVMMetadataRef VTableHolder, + unsigned Flags, LLVMBool isOptimized, + LLVMMetadataRef TParams) { + auto *VTH = VTableHolder ? unwrap(VTableHolder) : nullptr; + auto TP = TParams ? DINodeArray(unwrap(TParams)) : nullptr; + return wrap(unwrap(B)->createMethod(unwrap(Scope), Name, + LinkageName, unwrap(File), + LineNo, unwrap(Ty), + isLocalToUnit, isDefinition, Virtuality, + VTableIndex, VTH, Flags, + isOptimized, TP.get())); +} + +LLVMMetadataRef LLVMDICreateNameSpace(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, const char *Name, + LLVMMetadataRef File, unsigned LineNo) { + return wrap(unwrap(B)->createNameSpace(unwrap(Scope), Name, + unwrap(File), LineNo)); +} + +LLVMMetadataRef LLVMDICreateModule(LLVMDIBuilderRef B, LLVMMetadataRef Scope, + const char *Name, + const char *ConfigurationMacros, + const char *IncludePath, + const char *ISysRoot) { + return wrap(unwrap(B)->createModule(unwrap(Scope), Name, + ConfigurationMacros, IncludePath, + ISysRoot)); +} + +LLVMMetadataRef LLVMDICreateLexicalBlockFile(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + LLVMMetadataRef File, + unsigned Discriminator) { + return wrap(unwrap(B)->createLexicalBlockFile(unwrap(Scope), + unwrap(File), + Discriminator)); +} + +LLVMMetadataRef LLVMDICreateLexicalBlock(LLVMDIBuilderRef B, + LLVMMetadataRef Scope, + LLVMMetadataRef File, + unsigned Line, unsigned Col) { + return wrap(unwrap(B)->createLexicalBlock(unwrap(Scope), + unwrap(File), Line, Col)); +} + +// ImportedModule ??!? Declaration ? + +LLVMValueRef LLVMDIInsertDeclareAtEnd(LLVMDIBuilderRef B, LLVMValueRef Storage, + LLVMMetadataRef VarInfo, + LLVMMetadataRef Expr, LLVMMetadataRef DL, + LLVMBasicBlockRef InsertAtEnd) { + return wrap(unwrap(B)->insertDeclare(unwrap(Storage), + unwrap(VarInfo), + unwrap(Expr), + unwrap(DL), + unwrap(InsertAtEnd))); +} + +LLVMValueRef LLVMDIInsertDeclareBefore(LLVMDIBuilderRef B, + LLVMValueRef Storage, + LLVMMetadataRef VarInfo, + LLVMMetadataRef Expr, + LLVMMetadataRef DL, + LLVMValueRef InsertBefore) { + return wrap(unwrap(B)->insertDeclare(unwrap(Storage), + unwrap(VarInfo), + unwrap(Expr), + unwrap(DL), + unwrap(InsertBefore))); +} + +LLVMValueRef LLVMDIInsertDbgValueIntrinsicAtEnd(LLVMDIBuilderRef B, + LLVMValueRef Val, + uint64_t Offset, + LLVMMetadataRef VarInfo, + LLVMMetadataRef Expr, + LLVMMetadataRef DL, + LLVMBasicBlockRef InsertAtEnd) { + return wrap(unwrap(B)->insertDbgValueIntrinsic(unwrap(Val), Offset, + unwrap(VarInfo), unwrap(Expr), + unwrap(DL), unwrap(InsertAtEnd))); +} + +LLVMValueRef LLVMDIInsertDbgValueIntrinsicBefore(LLVMDIBuilderRef B, + LLVMValueRef Val, + uint64_t Offset, + LLVMMetadataRef VarInfo, + LLVMMetadataRef Expr, + LLVMMetadataRef DL, + LLVMValueRef InsertBefore) { + return wrap(unwrap(B)->insertDbgValueIntrinsic(unwrap(Val), Offset, + unwrap(VarInfo), unwrap(Expr), + unwrap(DL), unwrap(InsertBefore))); +} + +void LLVMDIReplaceVTableHolder(LLVMDIBuilderRef B, LLVMMetadataRef *T, + LLVMMetadataRef VTableHolder) { + auto *Tref = unwrap(*T); + unwrap(B)->replaceVTableHolder(Tref, unwrap(VTableHolder)); + *T = wrap(Tref); +} + +void LLVMDIReplaceArrays(LLVMDIBuilderRef B, LLVMMetadataRef *T, + LLVMMetadataRef Elements, LLVMMetadataRef TParams) { + auto *Tref = unwrap(*T); + unwrap(B)->replaceArrays(Tref, DINodeArray(unwrap(Elements)), + DINodeArray(unwrap(TParams))); + *T = wrap(Tref); } /*--.. Operations on scalar constants ......................................--*/ @@ -1744,6 +2322,14 @@ unwrap(Fn)->setPersonalityFn(unwrap(PersonalityFn)); } +LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Fn) { + return wrap(unwrap(Fn)->getSubprogram()); +} + +void LLVMSetSubprogram(LLVMValueRef Fn, LLVMMetadataRef SP) { + unwrap(Fn)->setSubprogram(unwrap(SP)); +} + unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) { if (Function *F = dyn_cast(unwrap(Fn))) return F->getIntrinsicID(); @@ -2306,23 +2892,18 @@ /*--.. Metadata builders ...................................................--*/ -void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) { - MDNode *Loc = - L ? cast(unwrap(L)->getMetadata()) : nullptr; - unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc)); +void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMMetadataRef L) { + unwrap(Builder)->SetCurrentDebugLocation(unwrap(L)); } -LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) { - LLVMContext &Context = unwrap(Builder)->getContext(); - return wrap(MetadataAsValue::get( - Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode())); +LLVMMetadataRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) { + return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()); } void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) { unwrap(Builder)->SetInstDebugLocation(unwrap(Inst)); } - /*--.. Instruction builders ................................................--*/ LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) { Index: lib/IR/DIBuilder.cpp =================================================================== --- lib/IR/DIBuilder.cpp +++ lib/IR/DIBuilder.cpp @@ -569,7 +569,7 @@ #ifndef NDEBUG if (auto *CT = dyn_cast_or_null(getNonCompileUnitScope(Context))) - assert(CT->getIdentifier().empty() && + assert(CT->getIdentifier().empty() || true && "Context of a global variable should not be a type with identifier"); #endif } Index: tools/llvm-c-test/metadata.c =================================================================== --- tools/llvm-c-test/metadata.c +++ tools/llvm-c-test/metadata.c @@ -16,10 +16,12 @@ int llvm_add_named_metadata_operand(void) { LLVMModuleRef m = LLVMModuleCreateWithName("Mod"); - LLVMValueRef values[] = { LLVMConstInt(LLVMInt32Type(), 0, 0) }; + LLVMMetadataRef mds[] = { + LLVMValueAsMetadata(LLVMConstInt(LLVMInt32Type(), 0, 0)) + }; // This used to trigger an assertion - LLVMAddNamedMetadataOperand(m, "name", LLVMMDNode(values, 1)); + LLVMAddNamedMetadataOperand(m, "name", LLVMMDNode(mds, 1)); LLVMDisposeModule(m); @@ -28,13 +30,15 @@ int llvm_set_metadata(void) { LLVMBuilderRef b = LLVMCreateBuilder(); - LLVMValueRef values[] = { LLVMConstInt(LLVMInt32Type(), 0, 0) }; + LLVMMetadataRef mds[] = { + LLVMValueAsMetadata(LLVMConstInt(LLVMInt32Type(), 0, 0)) + }; // This used to trigger an assertion LLVMSetMetadata( LLVMBuildRetVoid(b), LLVMGetMDKindID("kind", 4), - LLVMMDNode(values, 1)); + LLVMMDNode(mds, 1)); LLVMDisposeBuilder(b);