Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 1 | //===-- AttributeImpl.h - Attribute Internals -------------------*- C++ -*-===// |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Bill Wendling | 27107f6 | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file defines various helper methods and classes used by |
| 12 | /// LLVMContextImpl for creating and managing attributes. |
| 13 | /// |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_ATTRIBUTESIMPL_H |
| 17 | #define LLVM_ATTRIBUTESIMPL_H |
| 18 | |
| 19 | #include "llvm/ADT/FoldingSet.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Attributes.h" |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 21 | #include <string> |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 22 | |
| 23 | namespace llvm { |
| 24 | |
Bill Wendling | 7c1683d | 2012-12-29 12:29:38 +0000 | [diff] [blame] | 25 | class Constant; |
Bill Wendling | 5f93e2b | 2012-12-19 23:55:43 +0000 | [diff] [blame] | 26 | class LLVMContext; |
| 27 | |
Bill Wendling | 27107f6 | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | /// \class |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 30 | /// \brief This class represents a single, uniqued attribute. That attribute |
| 31 | /// could be a single enum, a tuple, or a string. |
| 32 | class AttributeImpl : public FoldingSetNode { |
| 33 | unsigned char KindID; ///< Holds the AttrEntryKind of the attribute |
| 34 | |
| 35 | // AttributesImpl is uniqued, these should not be publicly available. |
| 36 | void operator=(const AttributeImpl &) LLVM_DELETED_FUNCTION; |
| 37 | AttributeImpl(const AttributeImpl &) LLVM_DELETED_FUNCTION; |
| 38 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 39 | protected: |
| 40 | enum AttrEntryKind { |
| 41 | EnumAttrEntry, |
| 42 | AlignAttrEntry, |
| 43 | StringAttrEntry |
| 44 | }; |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 45 | |
| 46 | AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {} |
| 47 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 48 | public: |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 49 | virtual ~AttributeImpl(); |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 50 | |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 51 | bool isEnumAttribute() const { return KindID == EnumAttrEntry; } |
| 52 | bool isAlignAttribute() const { return KindID == AlignAttrEntry; } |
| 53 | bool isStringAttribute() const { return KindID == StringAttrEntry; } |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 54 | |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 55 | bool hasAttribute(Attribute::AttrKind A) const; |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 56 | bool hasAttribute(StringRef Kind) const; |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 57 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 58 | Attribute::AttrKind getKindAsEnum() const; |
| 59 | uint64_t getValueAsInt() const; |
Bill Wendling | 9f175f8 | 2013-01-29 20:37:10 +0000 | [diff] [blame] | 60 | |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 61 | StringRef getKindAsString() const; |
| 62 | StringRef getValueAsString() const; |
Bill Wendling | 529ec71 | 2012-12-30 01:38:39 +0000 | [diff] [blame] | 63 | |
Bill Wendling | 14292a6 | 2013-01-31 20:59:05 +0000 | [diff] [blame] | 64 | /// \brief Used when sorting the attributes. |
Bill Wendling | 3467e30 | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 65 | bool operator<(const AttributeImpl &AI) const; |
| 66 | |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 67 | void Profile(FoldingSetNodeID &ID) const { |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 68 | if (isEnumAttribute()) |
| 69 | Profile(ID, getKindAsEnum(), 0); |
| 70 | else if (isAlignAttribute()) |
| 71 | Profile(ID, getKindAsEnum(), getValueAsInt()); |
| 72 | else |
| 73 | Profile(ID, getKindAsString(), getValueAsString()); |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 74 | } |
Bill Wendling | 8c74ecf | 2013-02-05 22:37:24 +0000 | [diff] [blame] | 75 | static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind, |
| 76 | uint64_t Val) { |
| 77 | ID.AddInteger(Kind); |
| 78 | if (Val) ID.AddInteger(Val); |
| 79 | } |
| 80 | static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) { |
| 81 | ID.AddString(Kind); |
Bill Wendling | 40c81fe | 2013-02-28 21:17:03 +0000 | [diff] [blame] | 82 | if (!Values.empty()) ID.AddString(Values); |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Bill Wendling | f715dbd | 2013-02-01 00:48:14 +0000 | [diff] [blame] | 85 | // FIXME: Remove this! |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 86 | static uint64_t getAttrMask(Attribute::AttrKind Val); |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
Bill Wendling | 27107f6 | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 89 | //===----------------------------------------------------------------------===// |
| 90 | /// \class |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 91 | /// \brief A set of classes that contain the value of the |
| 92 | /// attribute object. There are three main categories: enum attribute entries, |
| 93 | /// represented by Attribute::AttrKind; alignment attribute entries; and string |
| 94 | /// attribute enties, which are for target-dependent attributes. |
| 95 | |
| 96 | class EnumAttributeImpl : public AttributeImpl { |
| 97 | Attribute::AttrKind Kind; |
| 98 | |
| 99 | protected: |
| 100 | EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind) |
| 101 | : AttributeImpl(ID), Kind(Kind) {} |
| 102 | |
| 103 | public: |
| 104 | EnumAttributeImpl(Attribute::AttrKind Kind) |
| 105 | : AttributeImpl(EnumAttrEntry), Kind(Kind) {} |
| 106 | |
| 107 | Attribute::AttrKind getEnumKind() const { return Kind; } |
| 108 | }; |
| 109 | |
| 110 | class AlignAttributeImpl : public EnumAttributeImpl { |
| 111 | unsigned Align; |
| 112 | |
| 113 | public: |
| 114 | AlignAttributeImpl(Attribute::AttrKind Kind, unsigned Align) |
| 115 | : EnumAttributeImpl(AlignAttrEntry, Kind), Align(Align) { |
| 116 | assert( |
| 117 | (Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) && |
| 118 | "Wrong kind for alignment attribute!"); |
| 119 | } |
| 120 | |
| 121 | unsigned getAlignment() const { return Align; } |
| 122 | }; |
| 123 | |
| 124 | class StringAttributeImpl : public AttributeImpl { |
| 125 | std::string Kind; |
| 126 | std::string Val; |
| 127 | |
| 128 | public: |
| 129 | StringAttributeImpl(StringRef Kind, StringRef Val = StringRef()) |
| 130 | : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {} |
| 131 | |
| 132 | StringRef getStringKind() const { return Kind; } |
| 133 | StringRef getStringValue() const { return Val; } |
| 134 | }; |
| 135 | |
| 136 | //===----------------------------------------------------------------------===// |
| 137 | /// \class |
Bill Wendling | 3467e30 | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 138 | /// \brief This class represents a group of attributes that apply to one |
| 139 | /// element: function, return type, or parameter. |
| 140 | class AttributeSetNode : public FoldingSetNode { |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 141 | unsigned NumAttrs; ///< Number of attributes in this node. |
Bill Wendling | 3467e30 | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 142 | |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 143 | AttributeSetNode(ArrayRef<Attribute> Attrs) : NumAttrs(Attrs.size()) { |
| 144 | // There's memory after the node where we can store the entries in. |
| 145 | std::copy(Attrs.begin(), Attrs.end(), |
| 146 | reinterpret_cast<Attribute *>(this + 1)); |
| 147 | } |
Bill Wendling | 1cc0d5a | 2013-01-27 21:38:03 +0000 | [diff] [blame] | 148 | |
| 149 | // AttributesSetNode is uniqued, these should not be publicly available. |
| 150 | void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION; |
| 151 | AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION; |
Bill Wendling | 3467e30 | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 152 | public: |
| 153 | static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs); |
| 154 | |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 155 | bool hasAttribute(Attribute::AttrKind Kind) const; |
Bill Wendling | 0e9d5d0 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 156 | bool hasAttribute(StringRef Kind) const; |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 157 | bool hasAttributes() const { return NumAttrs != 0; } |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 158 | |
Bill Wendling | 0e9d5d0 | 2013-02-13 08:42:21 +0000 | [diff] [blame] | 159 | Attribute getAttribute(Attribute::AttrKind Kind) const; |
| 160 | Attribute getAttribute(StringRef Kind) const; |
| 161 | |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 162 | unsigned getAlignment() const; |
| 163 | unsigned getStackAlignment() const; |
Rafael Espindola | aae0298 | 2013-05-01 13:07:03 +0000 | [diff] [blame] | 164 | std::string getAsString(bool InAttrGrp) const; |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 165 | |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 166 | typedef const Attribute *iterator; |
| 167 | iterator begin() const { return reinterpret_cast<iterator>(this + 1); } |
| 168 | iterator end() const { return begin() + NumAttrs; } |
Bill Wendling | 3467e30 | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 169 | |
| 170 | void Profile(FoldingSetNodeID &ID) const { |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 171 | Profile(ID, makeArrayRef(begin(), end())); |
Bill Wendling | 3467e30 | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 172 | } |
| 173 | static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) { |
| 174 | for (unsigned I = 0, E = AttrList.size(); I != E; ++I) |
| 175 | AttrList[I].Profile(ID); |
| 176 | } |
| 177 | }; |
| 178 | |
| 179 | //===----------------------------------------------------------------------===// |
| 180 | /// \class |
| 181 | /// \brief This class represents a set of attributes that apply to the function, |
| 182 | /// return type, and parameters. |
Bill Wendling | 18e7211 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 183 | class AttributeSetImpl : public FoldingSetNode { |
Bill Wendling | 8e47daf | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 184 | friend class AttributeSet; |
| 185 | |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 186 | LLVMContext &Context; |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 187 | |
Bill Wendling | 6bdbf06 | 2013-01-28 22:33:39 +0000 | [diff] [blame] | 188 | typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair; |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 189 | unsigned NumAttrs; ///< Number of entries in this set. |
| 190 | |
| 191 | /// \brief Return a pointer to the IndexAttrPair for the specified slot. |
| 192 | const IndexAttrPair *getNode(unsigned Slot) const { |
| 193 | return reinterpret_cast<const IndexAttrPair *>(this + 1) + Slot; |
| 194 | } |
Bill Wendling | bb08593 | 2013-01-24 01:01:34 +0000 | [diff] [blame] | 195 | |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 196 | // AttributesSet is uniqued, these should not be publicly available. |
Bill Wendling | 18e7211 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 197 | void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION; |
| 198 | AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION; |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 199 | public: |
Bill Wendling | 87e10df | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 200 | AttributeSetImpl(LLVMContext &C, |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 201 | ArrayRef<std::pair<unsigned, AttributeSetNode *> > Attrs) |
| 202 | : Context(C), NumAttrs(Attrs.size()) { |
| 203 | // There's memory after the node where we can store the entries in. |
| 204 | std::copy(Attrs.begin(), Attrs.end(), |
| 205 | reinterpret_cast<IndexAttrPair *>(this + 1)); |
| 206 | } |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 207 | |
Bill Wendling | 893eac1 | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 208 | /// \brief Get the context that created this AttributeSetImpl. |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 209 | LLVMContext &getContext() { return Context; } |
Bill Wendling | 893eac1 | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 210 | |
Bill Wendling | 893eac1 | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 211 | /// \brief Return the number of attributes this AttributeSet contains. |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 212 | unsigned getNumAttributes() const { return NumAttrs; } |
Bill Wendling | 893eac1 | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 213 | |
| 214 | /// \brief Get the index of the given "slot" in the AttrNodes list. This index |
| 215 | /// is the index of the return, parameter, or function object that the |
| 216 | /// attributes are applied to, not the index into the AttrNodes list where the |
| 217 | /// attributes reside. |
Rafael Espindola | 76f103e | 2013-04-30 16:53:38 +0000 | [diff] [blame] | 218 | unsigned getSlotIndex(unsigned Slot) const { |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 219 | return getNode(Slot)->first; |
Bill Wendling | 73bc452 | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 220 | } |
Bill Wendling | 893eac1 | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 221 | |
| 222 | /// \brief Retrieve the attributes for the given "slot" in the AttrNode list. |
| 223 | /// \p Slot is an index into the AttrNodes list, not the index of the return / |
| 224 | /// parameter/ function which the attributes apply to. |
Bill Wendling | 8e47daf | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 225 | AttributeSet getSlotAttributes(unsigned Slot) const { |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 226 | return AttributeSet::get(Context, *getNode(Slot)); |
Bill Wendling | 8e47daf | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 227 | } |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 228 | |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 229 | /// \brief Retrieve the attribute set node for the given "slot" in the |
| 230 | /// AttrNode list. |
| 231 | AttributeSetNode *getSlotNode(unsigned Slot) const { |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 232 | return getNode(Slot)->second; |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 235 | typedef AttributeSetNode::iterator iterator; |
| 236 | iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); } |
| 237 | iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); } |
Bill Wendling | 73bc452 | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 238 | |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 239 | void Profile(FoldingSetNodeID &ID) const { |
Benjamin Kramer | e22cde0 | 2013-07-11 12:13:16 +0000 | [diff] [blame] | 240 | Profile(ID, makeArrayRef(getNode(0), getNumAttributes())); |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 241 | } |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 242 | static void Profile(FoldingSetNodeID &ID, |
Bill Wendling | 6bdbf06 | 2013-01-28 22:33:39 +0000 | [diff] [blame] | 243 | ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) { |
Bill Wendling | bb08593 | 2013-01-24 01:01:34 +0000 | [diff] [blame] | 244 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { |
| 245 | ID.AddInteger(Nodes[i].first); |
| 246 | ID.AddPointer(Nodes[i].second); |
| 247 | } |
| 248 | } |
Bill Wendling | d05204a | 2013-01-27 23:41:29 +0000 | [diff] [blame] | 249 | |
| 250 | // FIXME: This atrocity is temporary. |
Rafael Espindola | 76f103e | 2013-04-30 16:53:38 +0000 | [diff] [blame] | 251 | uint64_t Raw(unsigned Index) const; |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 254 | } // end llvm namespace |
| 255 | |
| 256 | #endif |