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 LLVMContext &Context; ///< Global context for uniquing objects
34 Constant *Kind; ///< Kind of attribute: enum or string
35 Constant *Values; ///< Values associated with the attribute
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;
41 AttributeImpl(LLVMContext &C, Constant *Kind, Constant *Values = 0)
42 : Context(C), Kind(Kind), Values(Values) {}
44 LLVMContext &getContext() { return Context; }
46 bool hasAttribute(Attribute::AttrKind A) const;
48 Constant *getAttributeKind() const { return Kind; }
49 Constant *getAttributeValues() const { return Values; }
51 uint64_t getAlignment() const;
52 uint64_t getStackAlignment() const;
54 /// \brief Equality and non-equality comparison operators.
55 bool operator==(Attribute::AttrKind Kind) const;
56 bool operator!=(Attribute::AttrKind Kind) const;
58 bool operator==(StringRef Kind) const;
59 bool operator!=(StringRef Kind) const;
61 /// \brief Used when sorting the attributes.
62 bool operator<(const AttributeImpl &AI) const;
64 void Profile(FoldingSetNodeID &ID) const {
65 Profile(ID, Kind, Values);
67 static void Profile(FoldingSetNodeID &ID, Constant *Kind, Constant *Values) {
70 ID.AddPointer(Values);
73 // FIXME: Remove this!
74 static uint64_t getAttrMask(Attribute::AttrKind Val);
77 //===----------------------------------------------------------------------===//
79 /// \brief This class represents a group of attributes that apply to one
80 /// element: function, return type, or parameter.
81 class AttributeSetNode : public FoldingSetNode {
82 SmallVector<Attribute, 4> AttrList;
84 AttributeSetNode(ArrayRef<Attribute> Attrs)
85 : AttrList(Attrs.begin(), Attrs.end()) {}
87 // AttributesSetNode is uniqued, these should not be publicly available.
88 void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
89 AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
91 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
93 bool hasAttribute(Attribute::AttrKind Kind) const;
94 bool hasAttributes() const { return !AttrList.empty(); }
96 unsigned getAlignment() const;
97 unsigned getStackAlignment() const;
98 std::string getAsString() const;
100 typedef SmallVectorImpl<Attribute>::iterator iterator;
101 typedef SmallVectorImpl<Attribute>::const_iterator const_iterator;
103 iterator begin() { return AttrList.begin(); }
104 iterator end() { return AttrList.end(); }
106 const_iterator begin() const { return AttrList.begin(); }
107 const_iterator end() const { return AttrList.end(); }
109 void Profile(FoldingSetNodeID &ID) const {
110 Profile(ID, AttrList);
112 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
113 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
114 AttrList[I].Profile(ID);
118 //===----------------------------------------------------------------------===//
120 /// \brief This class represents a set of attributes that apply to the function,
121 /// return type, and parameters.
122 class AttributeSetImpl : public FoldingSetNode {
123 friend class AttributeSet;
125 LLVMContext &Context;
127 typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
128 SmallVector<IndexAttrPair, 4> AttrNodes;
130 // AttributesSet is uniqued, these should not be publicly available.
131 void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
132 AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
134 AttributeSetImpl(LLVMContext &C,
135 ArrayRef<std::pair<unsigned, AttributeSetNode*> > attrs)
136 : Context(C), AttrNodes(attrs.begin(), attrs.end()) {}
138 /// \brief Get the context that created this AttributeSetImpl.
139 LLVMContext &getContext() { return Context; }
141 /// \brief Return the number of attributes this AttributeSet contains.
142 unsigned getNumAttributes() const { return AttrNodes.size(); }
144 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
145 /// is the index of the return, parameter, or function object that the
146 /// attributes are applied to, not the index into the AttrNodes list where the
147 /// attributes reside.
148 uint64_t getSlotIndex(unsigned Slot) const {
149 return AttrNodes[Slot].first;
152 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
153 /// \p Slot is an index into the AttrNodes list, not the index of the return /
154 /// parameter/ function which the attributes apply to.
155 AttributeSet getSlotAttributes(unsigned Slot) const {
156 return AttributeSet::get(Context, AttrNodes[Slot]);
159 /// \brief Retrieve the attribute set node for the given "slot" in the
161 AttributeSetNode *getSlotNode(unsigned Slot) const {
162 return AttrNodes[Slot].second;
165 typedef AttributeSetNode::iterator iterator;
166 typedef AttributeSetNode::const_iterator const_iterator;
168 iterator begin(unsigned Idx)
169 { return AttrNodes[Idx].second->begin(); }
170 iterator end(unsigned Idx)
171 { return AttrNodes[Idx].second->end(); }
173 const_iterator begin(unsigned Idx) const
174 { return AttrNodes[Idx].second->begin(); }
175 const_iterator end(unsigned Idx) const
176 { return AttrNodes[Idx].second->end(); }
178 void Profile(FoldingSetNodeID &ID) const {
179 Profile(ID, AttrNodes);
181 static void Profile(FoldingSetNodeID &ID,
182 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
183 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
184 ID.AddInteger(Nodes[i].first);
185 ID.AddPointer(Nodes[i].second);
189 // FIXME: This atrocity is temporary.
190 uint64_t Raw(uint64_t Index) const;
193 } // end llvm namespace