Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -55,7 +55,7 @@ in favor of LLVMGetDataLayoutStr. * The C API enum LLVMAttribute is deprecated in favor of - LLVMGetAttrKindID. + LLVMGetAttributeKindForName. * ``TargetFrameLowering::eliminateCallFramePseudoInstr`` now returns an iterator to the next instruction instead of ``void``. Targets that previously Index: include/llvm-c/Core.h =================================================================== --- include/llvm-c/Core.h +++ include/llvm-c/Core.h @@ -487,7 +487,18 @@ * NB: Attribute names and/or id are subject to change without * going through the C API deprecation cycle. */ -unsigned LLVMGetAttrKindID(const char *Name, size_t SLen); +unsigned LLVMGetAttributeKindForName(const char *Name, size_t SLen); + +/** + * Create an attribute without argument. + */ +LLVMAttributeRef LLVMCreateAttribute(LLVMContextRef C, unsigned KindID); + +/** + * Get the unique id corresponding to the target independent attribute + * passed as argument. + */ +unsigned LLVMGetAttributeKind(LLVMAttributeRef A); /** * @} @@ -1977,6 +1988,21 @@ void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA); /** + * Add a return attribute to a function. + */ +void LLVMAddReturnAttr(LLVMValueRef Fn, LLVMAttributeRef A); + +/** + * Remove a return attribute from a function. + */ +void LLVMRemoveReturnAttr(LLVMValueRef Fn, unsigned KindID); + +/** + * Check if a function's return has an attribute. + */ +LLVMAttributeRef LLVMGetReturnAttr(LLVMValueRef Fn, unsigned KindID); + +/** * @defgroup LLVMCCoreValueFunctionParameters Function Parameters * * Functions in this group relate to arguments/parameters on functions. Index: include/llvm-c/Types.h =================================================================== --- include/llvm-c/Types.h +++ include/llvm-c/Types.h @@ -109,6 +109,13 @@ typedef struct LLVMOpaqueUse *LLVMUseRef; /** + * Used to represent an attributes. + * + * @see llvm::Attribute + */ +typedef struct LLVMOpaqueAttributeRef *LLVMAttributeRef; + +/** * @see llvm::DiagnosticInfo */ typedef struct LLVMOpaqueDiagnosticInfo *LLVMDiagnosticInfoRef; Index: include/llvm/IR/Attributes.h =================================================================== --- include/llvm/IR/Attributes.h +++ include/llvm/IR/Attributes.h @@ -21,6 +21,7 @@ #include "llvm/ADT/Optional.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/PointerLikeTypeTraits.h" +#include "llvm-c/Types.h" #include #include #include @@ -169,8 +170,28 @@ void Profile(FoldingSetNodeID &ID) const { ID.AddPointer(pImpl); } + + /// \brief Return a raw pointer that uniquely identifies this attribute. + void *getRawPointer() const { + return pImpl; + } + + /// \brief Get an attribute from a raw pointer created by getRawPointer. + static Attribute fromRawPointer(void *RawPtr) { + return Attribute(reinterpret_cast(RawPtr)); + } }; +// Specialized opaque value conversions. +inline LLVMAttributeRef wrap(Attribute Attr) { + return reinterpret_cast(Attr.getRawPointer()); +} + +// Specialized opaque value conversions. +inline Attribute unwrap(LLVMAttributeRef Attr) { + return Attribute::fromRawPointer(Attr); +} + //===----------------------------------------------------------------------===// /// \class /// \brief This class holds the attributes for a function, its return value, and Index: include/llvm/IR/Function.h =================================================================== --- include/llvm/IR/Function.h +++ include/llvm/IR/Function.h @@ -237,6 +237,9 @@ /// @brief adds the attribute to the list of attributes. void addAttribute(unsigned i, Attribute::AttrKind attr); + /// @brief adds the attribute to the list of attributes. + void addAttribute(unsigned i, Attribute Attr); + /// @brief adds the attributes to the list of attributes. void addAttributes(unsigned i, AttributeSet attrs); Index: lib/IR/Core.cpp =================================================================== --- lib/IR/Core.cpp +++ lib/IR/Core.cpp @@ -124,13 +124,16 @@ #define GET_ATTR_KIND_FROM_NAME #include "AttributesCompatFunc.inc" -unsigned LLVMGetAttrKindID(const char *Name, size_t SLen) { - auto K = getAttrKindFromName(StringRef(Name, SLen)); - if (K == Attribute::None) { - return 0; - } +unsigned LLVMGetAttributeKindForName(const char *Name, size_t SLen) { + return getAttrKindFromName(StringRef(Name, SLen)); +} - return AttributeImpl::getAttrMask(K); +LLVMAttributeRef LLVMCreateAttribute(LLVMContextRef C, unsigned KindID) { + return wrap(Attribute::get(*unwrap(C), (Attribute::AttrKind)KindID)); +} + +unsigned LLVMGetAttributeKind(LLVMAttributeRef A) { + return unwrap(A).getKindAsEnum(); } char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) { @@ -1821,6 +1824,19 @@ return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex); } +void LLVMAddReturnAttr(LLVMValueRef Fn, LLVMAttributeRef A) { + unwrap(Fn)->addAttribute(AttributeSet::ReturnIndex, unwrap(A)); +} + +void LLVMRemoveReturnAttr(LLVMValueRef Fn, unsigned KindID) { + unwrap(Fn)->removeAttribute(AttributeSet::ReturnIndex, + (Attribute::AttrKind)KindID); +} + +LLVMAttributeRef LLVMGetReturnAttr(LLVMValueRef Fn, unsigned KindID) { + return wrap(unwrap(Fn)->getFnAttribute((Attribute::AttrKind)KindID)); +} + /*--.. Operations on parameters ............................................--*/ unsigned LLVMCountParams(LLVMValueRef FnRef) { Index: lib/IR/Function.cpp =================================================================== --- lib/IR/Function.cpp +++ lib/IR/Function.cpp @@ -370,6 +370,12 @@ setAttributes(PAL); } +void Function::addAttribute(unsigned i, Attribute Attr) { + AttributeSet PAL = getAttributes(); + PAL = PAL.addAttribute(getContext(), i, Attr); + setAttributes(PAL); +} + void Function::addAttributes(unsigned i, AttributeSet attrs) { AttributeSet PAL = getAttributes(); PAL = PAL.addAttributes(getContext(), i, attrs);