Bill Wendling | 4607f4b | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 1 | //===-- AttributeImpl.h - Attribute Internals -------------------*- C++ -*-===// |
Bill Wendling | e38b804 | 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 | 66e978f | 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 | e38b804 | 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 | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Attributes.h" |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 21 | |
| 22 | namespace llvm { |
| 23 | |
Bill Wendling | 0cd0f7f | 2012-12-29 12:29:38 +0000 | [diff] [blame] | 24 | class Constant; |
Bill Wendling | 6ad6c3b | 2012-12-19 23:55:43 +0000 | [diff] [blame] | 25 | class LLVMContext; |
| 26 | |
Bill Wendling | 66e978f | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
| 28 | /// \class |
| 29 | /// \brief This class represents a single, uniqued attribute. That attribute |
Bill Wendling | 3a554ac | 2012-12-30 02:22:16 +0000 | [diff] [blame] | 30 | /// could be a single enum, a tuple, or a string. |
Bill Wendling | 4607f4b | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 31 | class AttributeImpl : public FoldingSetNode { |
Bill Wendling | 960f52a | 2013-01-05 01:36:54 +0000 | [diff] [blame] | 32 | LLVMContext &Context; |
Bill Wendling | 0cd0f7f | 2012-12-29 12:29:38 +0000 | [diff] [blame] | 33 | Constant *Data; |
Bill Wendling | 3a554ac | 2012-12-30 02:22:16 +0000 | [diff] [blame] | 34 | SmallVector<Constant*, 0> Vals; |
Bill Wendling | 97b4f70 | 2013-01-27 21:38:03 +0000 | [diff] [blame] | 35 | |
| 36 | // AttributesImpl is uniqued, these should not be publicly available. |
| 37 | void operator=(const AttributeImpl &) LLVM_DELETED_FUNCTION; |
| 38 | AttributeImpl(const AttributeImpl &) LLVM_DELETED_FUNCTION; |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 39 | public: |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame^] | 40 | AttributeImpl(LLVMContext &C, Constant *Data) |
| 41 | : Context(C), Data(Data) {} |
Bill Wendling | 3a554ac | 2012-12-30 02:22:16 +0000 | [diff] [blame] | 42 | explicit AttributeImpl(LLVMContext &C, Attribute::AttrKind data); |
| 43 | AttributeImpl(LLVMContext &C, Attribute::AttrKind data, |
| 44 | ArrayRef<Constant*> values); |
| 45 | AttributeImpl(LLVMContext &C, StringRef data); |
| 46 | |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 47 | bool hasAttribute(Attribute::AttrKind A) const; |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 48 | bool hasAttributes() const; |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 49 | |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame^] | 50 | LLVMContext &getContext() { return Context; } |
| 51 | ArrayRef<Constant*> getValues() const { return Vals; } |
| 52 | |
Bill Wendling | 73ea2de | 2012-10-08 21:47:17 +0000 | [diff] [blame] | 53 | uint64_t getAlignment() const; |
| 54 | uint64_t getStackAlignment() const; |
| 55 | |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 56 | bool operator==(Attribute::AttrKind Kind) const; |
| 57 | bool operator!=(Attribute::AttrKind Kind) const; |
Bill Wendling | b1d1261 | 2012-12-30 01:38:39 +0000 | [diff] [blame] | 58 | |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 59 | bool operator==(StringRef Kind) const; |
| 60 | bool operator!=(StringRef Kind) const; |
Bill Wendling | b1d1261 | 2012-12-30 01:38:39 +0000 | [diff] [blame] | 61 | |
Bill Wendling | d2e493b | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 62 | bool operator<(const AttributeImpl &AI) const; |
| 63 | |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 64 | void Profile(FoldingSetNodeID &ID) const { |
Bill Wendling | 3a554ac | 2012-12-30 02:22:16 +0000 | [diff] [blame] | 65 | Profile(ID, Data, Vals); |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 66 | } |
Bill Wendling | 3a554ac | 2012-12-30 02:22:16 +0000 | [diff] [blame] | 67 | static void Profile(FoldingSetNodeID &ID, Constant *Data, |
Bill Wendling | d509a66 | 2013-01-29 00:34:06 +0000 | [diff] [blame^] | 68 | ArrayRef<Constant*> Vals) { |
| 69 | ID.AddPointer(Data); |
| 70 | for (unsigned I = 0, E = Vals.size(); I != E; ++I) |
| 71 | ID.AddPointer(Vals[I]); |
| 72 | } |
| 73 | |
| 74 | // FIXME: Remove these! |
| 75 | uint64_t Raw() const; |
| 76 | static uint64_t getAttrMask(Attribute::AttrKind Val); |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Bill Wendling | 66e978f | 2012-12-20 21:28:43 +0000 | [diff] [blame] | 79 | //===----------------------------------------------------------------------===// |
| 80 | /// \class |
Bill Wendling | d2e493b | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 81 | /// \brief This class represents a group of attributes that apply to one |
| 82 | /// element: function, return type, or parameter. |
| 83 | class AttributeSetNode : public FoldingSetNode { |
| 84 | SmallVector<Attribute, 4> AttrList; |
| 85 | |
| 86 | AttributeSetNode(ArrayRef<Attribute> Attrs) |
| 87 | : AttrList(Attrs.begin(), Attrs.end()) {} |
Bill Wendling | 97b4f70 | 2013-01-27 21:38:03 +0000 | [diff] [blame] | 88 | |
| 89 | // AttributesSetNode is uniqued, these should not be publicly available. |
| 90 | void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION; |
| 91 | AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION; |
Bill Wendling | d2e493b | 2013-01-24 00:06:56 +0000 | [diff] [blame] | 92 | public: |
| 93 | static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs); |
| 94 | |
| 95 | typedef SmallVectorImpl<Attribute>::iterator iterator; |
| 96 | typedef SmallVectorImpl<Attribute>::const_iterator const_iterator; |
| 97 | |
| 98 | iterator begin() { return AttrList.begin(); } |
| 99 | iterator end() { return AttrList.end(); } |
| 100 | |
| 101 | const_iterator begin() const { return AttrList.begin(); } |
| 102 | const_iterator end() const { return AttrList.end(); } |
| 103 | |
| 104 | void Profile(FoldingSetNodeID &ID) const { |
| 105 | Profile(ID, AttrList); |
| 106 | } |
| 107 | static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) { |
| 108 | for (unsigned I = 0, E = AttrList.size(); I != E; ++I) |
| 109 | AttrList[I].Profile(ID); |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | //===----------------------------------------------------------------------===// |
| 114 | /// \class |
| 115 | /// \brief This class represents a set of attributes that apply to the function, |
| 116 | /// return type, and parameters. |
Bill Wendling | 6848e38 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 117 | class AttributeSetImpl : public FoldingSetNode { |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 118 | friend class AttributeSet; |
| 119 | |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 120 | LLVMContext &Context; |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 121 | |
Bill Wendling | eeebb13 | 2013-01-28 22:33:39 +0000 | [diff] [blame] | 122 | typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair; |
Bill Wendling | 9eb689c | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 123 | SmallVector<IndexAttrPair, 4> AttrNodes; |
Bill Wendling | 39a4c80 | 2013-01-24 01:01:34 +0000 | [diff] [blame] | 124 | |
Bill Wendling | 698e84f | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 125 | // AttributesSet is uniqued, these should not be publicly available. |
Bill Wendling | 6848e38 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 126 | void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION; |
| 127 | AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION; |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 128 | public: |
Bill Wendling | ec45454 | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 129 | AttributeSetImpl(LLVMContext &C, |
Bill Wendling | eeebb13 | 2013-01-28 22:33:39 +0000 | [diff] [blame] | 130 | ArrayRef<std::pair<unsigned, AttributeSetNode*> > attrs) |
Bill Wendling | ec45454 | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 131 | : Context(C), AttrNodes(attrs.begin(), attrs.end()) {} |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 132 | |
Bill Wendling | 5c8b2df | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 133 | /// \brief Get the context that created this AttributeSetImpl. |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 134 | LLVMContext &getContext() { return Context; } |
Bill Wendling | 5c8b2df | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 135 | |
Bill Wendling | 5c8b2df | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 136 | /// \brief Return the number of attributes this AttributeSet contains. |
| 137 | unsigned getNumAttributes() const { return AttrNodes.size(); } |
| 138 | |
| 139 | /// \brief Get the index of the given "slot" in the AttrNodes list. This index |
| 140 | /// is the index of the return, parameter, or function object that the |
| 141 | /// attributes are applied to, not the index into the AttrNodes list where the |
| 142 | /// attributes reside. |
Bill Wendling | 9eb689c | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 143 | uint64_t getSlotIndex(unsigned Slot) const { |
| 144 | return AttrNodes[Slot].first; |
| 145 | } |
Bill Wendling | 5c8b2df | 2013-01-27 21:32:11 +0000 | [diff] [blame] | 146 | |
| 147 | /// \brief Retrieve the attributes for the given "slot" in the AttrNode list. |
| 148 | /// \p Slot is an index into the AttrNodes list, not the index of the return / |
| 149 | /// parameter/ function which the attributes apply to. |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 150 | AttributeSet getSlotAttributes(unsigned Slot) const { |
| 151 | // FIXME: This needs to use AttrNodes instead. |
Bill Wendling | ec45454 | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 152 | return AttributeSet::get(Context, AttrNodes[Slot]); |
Bill Wendling | 57625a4 | 2013-01-25 23:09:36 +0000 | [diff] [blame] | 153 | } |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 154 | |
Bill Wendling | 9eb689c | 2013-01-28 00:21:34 +0000 | [diff] [blame] | 155 | typedef AttributeSetNode::iterator iterator; |
| 156 | typedef AttributeSetNode::const_iterator const_iterator; |
| 157 | |
| 158 | iterator begin(unsigned Idx) |
| 159 | { return AttrNodes[Idx].second->begin(); } |
| 160 | iterator end(unsigned Idx) |
| 161 | { return AttrNodes[Idx].second->end(); } |
| 162 | |
| 163 | const_iterator begin(unsigned Idx) const |
| 164 | { return AttrNodes[Idx].second->begin(); } |
| 165 | const_iterator end(unsigned Idx) const |
| 166 | { return AttrNodes[Idx].second->end(); } |
| 167 | |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 168 | void Profile(FoldingSetNodeID &ID) const { |
Bill Wendling | ec45454 | 2013-01-28 21:55:20 +0000 | [diff] [blame] | 169 | Profile(ID, AttrNodes); |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 170 | } |
Bill Wendling | 9ac69f9 | 2013-01-04 20:54:35 +0000 | [diff] [blame] | 171 | static void Profile(FoldingSetNodeID &ID, |
Bill Wendling | eeebb13 | 2013-01-28 22:33:39 +0000 | [diff] [blame] | 172 | ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) { |
Bill Wendling | 39a4c80 | 2013-01-24 01:01:34 +0000 | [diff] [blame] | 173 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { |
| 174 | ID.AddInteger(Nodes[i].first); |
| 175 | ID.AddPointer(Nodes[i].second); |
| 176 | } |
| 177 | } |
Bill Wendling | 1f786a7 | 2013-01-27 23:41:29 +0000 | [diff] [blame] | 178 | |
| 179 | // FIXME: This atrocity is temporary. |
| 180 | uint64_t Raw(uint64_t Index) const; |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 181 | }; |
| 182 | |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 183 | } // end llvm namespace |
| 184 | |
| 185 | #endif |