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 |
| 30 | /// \brief This class represents a single, uniqued attribute. That attribute |
Bill Wendling | 979aff6 | 2012-12-30 02:22:16 +0000 | [diff] [blame] | 31 | /// could be a single enum, a tuple, or a string. |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 32 | class AttributeImpl : public FoldingSetNode { |
Bill Wendling | 1bbd644 | 2013-01-05 01:36:54 +0000 | [diff] [blame] | 33 | LLVMContext &Context; |
Bill Wendling | 9f175f8 | 2013-01-29 20:37:10 +0000 | [diff] [blame] | 34 | Constant *Kind; |
Bill Wendling | 979aff6 | 2012-12-30 02:22:16 +0000 | [diff] [blame] | 35 | SmallVector<Constant*, 0> Vals; |
Bill Wendling | 1cc0d5a | 2013-01-27 21:38:03 +0000 | [diff] [blame] | 36 | |
| 37 | // AttributesImpl is uniqued, these should not be publicly available. |
| 38 | void operator=(const AttributeImpl &) LLVM_DELETED_FUNCTION; |
| 39 | AttributeImpl(const AttributeImpl &) LLVM_DELETED_FUNCTION; |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 40 | public: |
Bill Wendling | 9f175f8 | 2013-01-29 20:37:10 +0000 | [diff] [blame] | 41 | AttributeImpl(LLVMContext &C, Constant *Kind) |
| 42 | : Context(C), Kind(Kind) {} |
Bill Wendling | 979aff6 | 2012-12-30 02:22:16 +0000 | [diff] [blame] | 43 | explicit AttributeImpl(LLVMContext &C, Attribute::AttrKind data); |
| 44 | AttributeImpl(LLVMContext &C, Attribute::AttrKind data, |
| 45 | ArrayRef<Constant*> values); |
| 46 | AttributeImpl(LLVMContext &C, StringRef data); |
| 47 | |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 48 | bool hasAttribute(Attribute::AttrKind A) const; |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 49 | |
Bill Wendling | 9f175f8 | 2013-01-29 20:37:10 +0000 | [diff] [blame] | 50 | Constant *getAttributeKind() const { return Kind; } |
| 51 | ArrayRef<Constant*> getAttributeValues() const { return Vals; } |
| 52 | |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 53 | LLVMContext &getContext() { return Context; } |
| 54 | ArrayRef<Constant*> getValues() const { return Vals; } |
| 55 | |
Bill Wendling | 8e635db | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 56 | uint64_t getAlignment() const; |
| 57 | uint64_t getStackAlignment() const; |
| 58 | |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 59 | bool operator==(Attribute::AttrKind Kind) const; |
| 60 | bool operator!=(Attribute::AttrKind Kind) const; |
Bill Wendling | 529ec71 | 2012-12-30 01:38:39 +0000 | [diff] [blame] | 61 | |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 62 | bool operator==(StringRef Kind) const; |
| 63 | bool operator!=(StringRef Kind) const; |
Bill Wendling | 529ec71 | 2012-12-30 01:38:39 +0000 | [diff] [blame] | 64 | |
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 | 9f175f8 | 2013-01-29 20:37:10 +0000 | [diff] [blame] | 68 | Profile(ID, Kind, Vals); |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 69 | } |
Bill Wendling | 9f175f8 | 2013-01-29 20:37:10 +0000 | [diff] [blame] | 70 | static void Profile(FoldingSetNodeID &ID, Constant *Kind, |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 71 | ArrayRef<Constant*> Vals) { |
Bill Wendling | 9f175f8 | 2013-01-29 20:37:10 +0000 | [diff] [blame] | 72 | ID.AddPointer(Kind); |
Bill Wendling | c22f4aa | 2013-01-29 00:34:06 +0000 | [diff] [blame] | 73 | for (unsigned I = 0, E = Vals.size(); I != E; ++I) |
| 74 | ID.AddPointer(Vals[I]); |
| 75 | } |
| 76 | |
| 77 | // FIXME: Remove these! |
| 78 | uint64_t Raw() const; |
| 79 | static uint64_t getAttrMask(Attribute::AttrKind Val); |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
Bill Wendling | 27107f6 | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 82 | //===----------------------------------------------------------------------===// |
| 83 | /// \class |
Bill Wendling | 3467e30 | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 84 | /// \brief This class represents a group of attributes that apply to one |
| 85 | /// element: function, return type, or parameter. |
| 86 | class AttributeSetNode : public FoldingSetNode { |
| 87 | SmallVector<Attribute, 4> AttrList; |
| 88 | |
| 89 | AttributeSetNode(ArrayRef<Attribute> Attrs) |
| 90 | : AttrList(Attrs.begin(), Attrs.end()) {} |
Bill Wendling | 1cc0d5a | 2013-01-27 21:38:03 +0000 | [diff] [blame] | 91 | |
| 92 | // AttributesSetNode is uniqued, these should not be publicly available. |
| 93 | void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION; |
| 94 | AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION; |
Bill Wendling | 3467e30 | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 95 | public: |
| 96 | static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs); |
| 97 | |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 98 | bool hasAttribute(Attribute::AttrKind Kind) const; |
| 99 | bool hasAttributes() const { return !AttrList.empty(); } |
| 100 | |
| 101 | unsigned getAlignment() const; |
| 102 | unsigned getStackAlignment() const; |
| 103 | std::string getAsString() const; |
| 104 | |
Bill Wendling | 3467e30 | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 105 | typedef SmallVectorImpl<Attribute>::iterator iterator; |
| 106 | typedef SmallVectorImpl<Attribute>::const_iterator const_iterator; |
| 107 | |
| 108 | iterator begin() { return AttrList.begin(); } |
| 109 | iterator end() { return AttrList.end(); } |
| 110 | |
| 111 | const_iterator begin() const { return AttrList.begin(); } |
| 112 | const_iterator end() const { return AttrList.end(); } |
| 113 | |
| 114 | void Profile(FoldingSetNodeID &ID) const { |
| 115 | Profile(ID, AttrList); |
| 116 | } |
| 117 | static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) { |
| 118 | for (unsigned I = 0, E = AttrList.size(); I != E; ++I) |
| 119 | AttrList[I].Profile(ID); |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | //===----------------------------------------------------------------------===// |
| 124 | /// \class |
| 125 | /// \brief This class represents a set of attributes that apply to the function, |
| 126 | /// return type, and parameters. |
Bill Wendling | 18e7211 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 127 | class AttributeSetImpl : public FoldingSetNode { |
Bill Wendling | 8e47daf | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 128 | friend class AttributeSet; |
| 129 | |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 130 | LLVMContext &Context; |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 131 | |
Bill Wendling | 6bdbf06 | 2013-01-28 22:33:39 +0000 | [diff] [blame] | 132 | typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair; |
Bill Wendling | 73bc452 | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 133 | SmallVector<IndexAttrPair, 4> AttrNodes; |
Bill Wendling | bb08593 | 2013-01-24 01:01:34 +0000 | [diff] [blame] | 134 | |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 135 | // AttributesSet is uniqued, these should not be publicly available. |
Bill Wendling | 18e7211 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 136 | void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION; |
| 137 | AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION; |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 138 | public: |
Bill Wendling | 87e10df | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 139 | AttributeSetImpl(LLVMContext &C, |
Bill Wendling | 6bdbf06 | 2013-01-28 22:33:39 +0000 | [diff] [blame] | 140 | ArrayRef<std::pair<unsigned, AttributeSetNode*> > attrs) |
Bill Wendling | 87e10df | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 141 | : Context(C), AttrNodes(attrs.begin(), attrs.end()) {} |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 142 | |
Bill Wendling | 893eac1 | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 143 | /// \brief Get the context that created this AttributeSetImpl. |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 144 | LLVMContext &getContext() { return Context; } |
Bill Wendling | 893eac1 | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 145 | |
Bill Wendling | 893eac1 | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 146 | /// \brief Return the number of attributes this AttributeSet contains. |
| 147 | unsigned getNumAttributes() const { return AttrNodes.size(); } |
| 148 | |
| 149 | /// \brief Get the index of the given "slot" in the AttrNodes list. This index |
| 150 | /// is the index of the return, parameter, or function object that the |
| 151 | /// attributes are applied to, not the index into the AttrNodes list where the |
| 152 | /// attributes reside. |
Bill Wendling | 73bc452 | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 153 | uint64_t getSlotIndex(unsigned Slot) const { |
| 154 | return AttrNodes[Slot].first; |
| 155 | } |
Bill Wendling | 893eac1 | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 156 | |
| 157 | /// \brief Retrieve the attributes for the given "slot" in the AttrNode list. |
| 158 | /// \p Slot is an index into the AttrNodes list, not the index of the return / |
| 159 | /// parameter/ function which the attributes apply to. |
Bill Wendling | 8e47daf | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 160 | AttributeSet getSlotAttributes(unsigned Slot) const { |
| 161 | // FIXME: This needs to use AttrNodes instead. |
Bill Wendling | 87e10df | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 162 | return AttributeSet::get(Context, AttrNodes[Slot]); |
Bill Wendling | 8e47daf | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 163 | } |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 164 | |
Bill Wendling | 606c8e3 | 2013-01-29 03:20:31 +0000 | [diff] [blame] | 165 | /// \brief Retrieve the attribute set node for the given "slot" in the |
| 166 | /// AttrNode list. |
| 167 | AttributeSetNode *getSlotNode(unsigned Slot) const { |
| 168 | return AttrNodes[Slot].second; |
| 169 | } |
| 170 | |
Bill Wendling | 73bc452 | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 171 | typedef AttributeSetNode::iterator iterator; |
| 172 | typedef AttributeSetNode::const_iterator const_iterator; |
| 173 | |
| 174 | iterator begin(unsigned Idx) |
| 175 | { return AttrNodes[Idx].second->begin(); } |
| 176 | iterator end(unsigned Idx) |
| 177 | { return AttrNodes[Idx].second->end(); } |
| 178 | |
| 179 | const_iterator begin(unsigned Idx) const |
| 180 | { return AttrNodes[Idx].second->begin(); } |
| 181 | const_iterator end(unsigned Idx) const |
| 182 | { return AttrNodes[Idx].second->end(); } |
| 183 | |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 184 | void Profile(FoldingSetNodeID &ID) const { |
Bill Wendling | 87e10df | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 185 | Profile(ID, AttrNodes); |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 186 | } |
Bill Wendling | 60507d5 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 187 | static void Profile(FoldingSetNodeID &ID, |
Bill Wendling | 6bdbf06 | 2013-01-28 22:33:39 +0000 | [diff] [blame] | 188 | ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) { |
Bill Wendling | bb08593 | 2013-01-24 01:01:34 +0000 | [diff] [blame] | 189 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { |
| 190 | ID.AddInteger(Nodes[i].first); |
| 191 | ID.AddPointer(Nodes[i].second); |
| 192 | } |
| 193 | } |
Bill Wendling | d05204a | 2013-01-27 23:41:29 +0000 | [diff] [blame] | 194 | |
| 195 | // FIXME: This atrocity is temporary. |
| 196 | uint64_t Raw(uint64_t Index) const; |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 199 | } // end llvm namespace |
| 200 | |
| 201 | #endif |