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