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