1 //===-- AttributeImpl.h - Attribute Internals -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file defines various helper methods and classes used by
12 /// LLVMContextImpl for creating and managing attributes.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ATTRIBUTESIMPL_H
17 #define LLVM_ATTRIBUTESIMPL_H
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/IR/Attributes.h"
28 //===----------------------------------------------------------------------===//
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
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;
46 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
49 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
50 bool isAlignAttribute() const { return KindID == AlignAttrEntry; }
51 bool isStringAttribute() const { return KindID == StringAttrEntry; }
53 bool hasAttribute(Attribute::AttrKind A) const;
54 bool hasAttribute(StringRef Kind) const;
56 Attribute::AttrKind getKindAsEnum() const;
57 uint64_t getValueAsInt() const;
59 StringRef getKindAsString() const;
60 StringRef getValueAsString() const;
62 /// \brief Used when sorting the attributes.
63 bool operator<(const AttributeImpl &AI) const;
65 void Profile(FoldingSetNodeID &ID) const {
66 if (isEnumAttribute())
67 Profile(ID, getKindAsEnum(), 0);
68 else if (isAlignAttribute())
69 Profile(ID, getKindAsEnum(), getValueAsInt());
71 Profile(ID, getKindAsString(), getValueAsString());
73 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
76 if (Val) ID.AddInteger(Val);
78 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
80 if (!Values.empty()) ID.AddString(Values);
83 // FIXME: Remove this!
84 static uint64_t getAttrMask(Attribute::AttrKind Val);
87 //===----------------------------------------------------------------------===//
89 /// \brief A set of classes that contain the value of the
90 /// attribute object. There are three main categories: enum attribute entries,
91 /// represented by Attribute::AttrKind; alignment attribute entries; and string
92 /// attribute enties, which are for target-dependent attributes.
94 class EnumAttributeImpl : public AttributeImpl {
95 Attribute::AttrKind Kind;
98 EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
99 : AttributeImpl(ID), Kind(Kind) {}
102 EnumAttributeImpl(Attribute::AttrKind Kind)
103 : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
105 Attribute::AttrKind getEnumKind() const { return Kind; }
108 class AlignAttributeImpl : public EnumAttributeImpl {
112 AlignAttributeImpl(Attribute::AttrKind Kind, unsigned Align)
113 : EnumAttributeImpl(AlignAttrEntry, Kind), Align(Align) {
115 (Kind == Attribute::Alignment || Kind == Attribute::StackAlignment) &&
116 "Wrong kind for alignment attribute!");
119 unsigned getAlignment() const { return Align; }
122 class StringAttributeImpl : public AttributeImpl {
127 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
128 : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
130 StringRef getStringKind() const { return Kind; }
131 StringRef getStringValue() const { return Val; }
134 //===----------------------------------------------------------------------===//
136 /// \brief This class represents a group of attributes that apply to one
137 /// element: function, return type, or parameter.
138 class AttributeSetNode : public FoldingSetNode {
139 unsigned NumAttrs; ///< Number of attributes in this node.
141 AttributeSetNode(ArrayRef<Attribute> Attrs) : NumAttrs(Attrs.size()) {
142 // There's memory after the node where we can store the entries in.
143 std::copy(Attrs.begin(), Attrs.end(),
144 reinterpret_cast<Attribute *>(this + 1));
147 // AttributesSetNode is uniqued, these should not be publicly available.
148 void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
149 AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
151 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
153 bool hasAttribute(Attribute::AttrKind Kind) const;
154 bool hasAttribute(StringRef Kind) const;
155 bool hasAttributes() const { return NumAttrs != 0; }
157 Attribute getAttribute(Attribute::AttrKind Kind) const;
158 Attribute getAttribute(StringRef Kind) const;
160 unsigned getAlignment() const;
161 unsigned getStackAlignment() const;
162 std::string getAsString(bool InAttrGrp) const;
164 typedef const Attribute *iterator;
165 iterator begin() const { return reinterpret_cast<iterator>(this + 1); }
166 iterator end() const { return begin() + NumAttrs; }
168 void Profile(FoldingSetNodeID &ID) const {
169 Profile(ID, makeArrayRef(begin(), end()));
171 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
172 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
173 AttrList[I].Profile(ID);
177 //===----------------------------------------------------------------------===//
179 /// \brief This class represents a set of attributes that apply to the function,
180 /// return type, and parameters.
181 class AttributeSetImpl : public FoldingSetNode {
182 friend class AttributeSet;
184 LLVMContext &Context;
186 typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
187 unsigned NumAttrs; ///< Number of entries in this set.
189 /// \brief Return a pointer to the IndexAttrPair for the specified slot.
190 const IndexAttrPair *getNode(unsigned Slot) const {
191 return reinterpret_cast<const IndexAttrPair *>(this + 1) + Slot;
194 // AttributesSet is uniqued, these should not be publicly available.
195 void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
196 AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
198 AttributeSetImpl(LLVMContext &C,
199 ArrayRef<std::pair<unsigned, AttributeSetNode *> > Attrs)
200 : Context(C), NumAttrs(Attrs.size()) {
202 if (Attrs.size() >= 2) {
203 for (const std::pair<unsigned, AttributeSetNode *> *i = Attrs.begin() + 1,
206 assert((i-1)->first <= i->first && "Attribute set not ordered!");
210 // There's memory after the node where we can store the entries in.
211 std::copy(Attrs.begin(), Attrs.end(),
212 reinterpret_cast<IndexAttrPair *>(this + 1));
215 /// \brief Get the context that created this AttributeSetImpl.
216 LLVMContext &getContext() { return Context; }
218 /// \brief Return the number of attributes this AttributeSet contains.
219 unsigned getNumAttributes() const { return NumAttrs; }
221 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
222 /// is the index of the return, parameter, or function object that the
223 /// attributes are applied to, not the index into the AttrNodes list where the
224 /// attributes reside.
225 unsigned getSlotIndex(unsigned Slot) const {
226 return getNode(Slot)->first;
229 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
230 /// \p Slot is an index into the AttrNodes list, not the index of the return /
231 /// parameter/ function which the attributes apply to.
232 AttributeSet getSlotAttributes(unsigned Slot) const {
233 return AttributeSet::get(Context, *getNode(Slot));
236 /// \brief Retrieve the attribute set node for the given "slot" in the
238 AttributeSetNode *getSlotNode(unsigned Slot) const {
239 return getNode(Slot)->second;
242 typedef AttributeSetNode::iterator iterator;
243 iterator begin(unsigned Slot) const { return getSlotNode(Slot)->begin(); }
244 iterator end(unsigned Slot) const { return getSlotNode(Slot)->end(); }
246 void Profile(FoldingSetNodeID &ID) const {
247 Profile(ID, makeArrayRef(getNode(0), getNumAttributes()));
249 static void Profile(FoldingSetNodeID &ID,
250 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
251 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
252 ID.AddInteger(Nodes[i].first);
253 ID.AddPointer(Nodes[i].second);
257 // FIXME: This atrocity is temporary.
258 uint64_t Raw(unsigned Index) const;
263 } // end llvm namespace