Amaury Sechet | 17b67cd | 2016-07-21 04:25:06 +0000 | [diff] [blame] | 1 | //===-- AttributeSetNode.h - AttributeSet Internal Node ---------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file defines the node class used internally by AttributeSet. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_IR_ATTRIBUTESETNODE_H |
| 16 | #define LLVM_IR_ATTRIBUTESETNODE_H |
| 17 | |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/ArrayRef.h" |
Amaury Sechet | 17b67cd | 2016-07-21 04:25:06 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/FoldingSet.h" |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Optional.h" |
| 21 | #include "llvm/ADT/StringRef.h" |
Amaury Sechet | 17b67cd | 2016-07-21 04:25:06 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Attributes.h" |
| 23 | #include "llvm/Support/TrailingObjects.h" |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Amaury Sechet | ed7b7d7 | 2016-07-21 04:31:38 +0000 | [diff] [blame] | 25 | #include <climits> |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 26 | #include <cstdint> |
| 27 | #include <string> |
| 28 | #include <utility> |
Amaury Sechet | 17b67cd | 2016-07-21 04:25:06 +0000 | [diff] [blame] | 29 | |
| 30 | namespace llvm { |
| 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | /// \class |
| 34 | /// \brief This class represents a group of attributes that apply to one |
| 35 | /// element: function, return type, or parameter. |
| 36 | class AttributeSetNode final |
| 37 | : public FoldingSetNode, |
| 38 | private TrailingObjects<AttributeSetNode, Attribute> { |
| 39 | friend TrailingObjects; |
| 40 | |
| 41 | unsigned NumAttrs; ///< Number of attributes in this node. |
| 42 | /// Bitset with a bit for each available attribute Attribute::AttrKind. |
| 43 | uint64_t AvailableAttrs; |
| 44 | |
| 45 | AttributeSetNode(ArrayRef<Attribute> Attrs) |
| 46 | : NumAttrs(Attrs.size()), AvailableAttrs(0) { |
| 47 | static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT, |
| 48 | "Too many attributes for AvailableAttrs"); |
| 49 | // There's memory after the node where we can store the entries in. |
| 50 | std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>()); |
| 51 | |
| 52 | for (Attribute I : *this) { |
| 53 | if (!I.isStringAttribute()) { |
| 54 | AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum(); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
Amaury Sechet | 17b67cd | 2016-07-21 04:25:06 +0000 | [diff] [blame] | 59 | public: |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 60 | // AttributesSetNode is uniqued, these should not be available. |
| 61 | AttributeSetNode(const AttributeSetNode &) = delete; |
| 62 | AttributeSetNode &operator=(const AttributeSetNode &) = delete; |
| 63 | |
Amaury Sechet | 17b67cd | 2016-07-21 04:25:06 +0000 | [diff] [blame] | 64 | void operator delete(void *p) { ::operator delete(p); } |
| 65 | |
| 66 | static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs); |
| 67 | |
| 68 | static AttributeSetNode *get(AttributeSet AS, unsigned Index) { |
| 69 | return AS.getAttributes(Index); |
| 70 | } |
| 71 | |
| 72 | /// \brief Return the number of attributes this AttributeSet contains. |
| 73 | unsigned getNumAttributes() const { return NumAttrs; } |
| 74 | |
| 75 | bool hasAttribute(Attribute::AttrKind Kind) const { |
| 76 | return AvailableAttrs & ((uint64_t)1) << Kind; |
| 77 | } |
| 78 | bool hasAttribute(StringRef Kind) const; |
| 79 | bool hasAttributes() const { return NumAttrs != 0; } |
| 80 | |
| 81 | Attribute getAttribute(Attribute::AttrKind Kind) const; |
| 82 | Attribute getAttribute(StringRef Kind) const; |
| 83 | |
| 84 | unsigned getAlignment() const; |
| 85 | unsigned getStackAlignment() const; |
| 86 | uint64_t getDereferenceableBytes() const; |
| 87 | uint64_t getDereferenceableOrNullBytes() const; |
| 88 | std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const; |
| 89 | std::string getAsString(bool InAttrGrp) const; |
| 90 | |
| 91 | typedef const Attribute *iterator; |
| 92 | iterator begin() const { return getTrailingObjects<Attribute>(); } |
| 93 | iterator end() const { return begin() + NumAttrs; } |
| 94 | |
| 95 | void Profile(FoldingSetNodeID &ID) const { |
| 96 | Profile(ID, makeArrayRef(begin(), end())); |
| 97 | } |
| 98 | static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) { |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 99 | for (const auto &Attr : AttrList) |
| 100 | Attr.Profile(ID); |
Amaury Sechet | 17b67cd | 2016-07-21 04:25:06 +0000 | [diff] [blame] | 101 | } |
| 102 | }; |
| 103 | |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 104 | } // end namespace llvm |
Amaury Sechet | 17b67cd | 2016-07-21 04:25:06 +0000 | [diff] [blame] | 105 | |
Eugene Zelenko | 9408c61 | 2016-12-07 22:06:02 +0000 | [diff] [blame] | 106 | #endif // LLVM_IR_ATTRIBUTESETNODE_H |