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"
27 //===----------------------------------------------------------------------===//
29 /// \brief This class represents a single, uniqued attribute. That attribute
30 /// could be a single enum, a tuple, or a string.
31 class AttributeImpl : public FoldingSetNode {
34 SmallVector<Constant*, 0> Vals;
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;
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);
46 LLVMContext &getContext() { return Context; }
48 ArrayRef<Constant*> getValues() const { return Vals; }
50 bool hasAttribute(Attribute::AttrKind A) const;
52 bool hasAttributes() const;
54 uint64_t getAlignment() const;
55 uint64_t getStackAlignment() const;
57 bool operator==(Attribute::AttrKind Kind) const;
58 bool operator!=(Attribute::AttrKind Kind) const;
60 bool operator==(StringRef Kind) const;
61 bool operator!=(StringRef Kind) const;
63 bool operator<(const AttributeImpl &AI) const;
65 uint64_t Raw() const; // FIXME: Remove.
67 static uint64_t getAttrMask(Attribute::AttrKind Val);
69 void Profile(FoldingSetNodeID &ID) const {
70 Profile(ID, Data, Vals);
72 static void Profile(FoldingSetNodeID &ID, Constant *Data,
73 ArrayRef<Constant*> Vals);
76 //===----------------------------------------------------------------------===//
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;
83 AttributeSetNode(ArrayRef<Attribute> Attrs)
84 : AttrList(Attrs.begin(), Attrs.end()) {}
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;
90 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
92 typedef SmallVectorImpl<Attribute>::iterator iterator;
93 typedef SmallVectorImpl<Attribute>::const_iterator const_iterator;
95 iterator begin() { return AttrList.begin(); }
96 iterator end() { return AttrList.end(); }
98 const_iterator begin() const { return AttrList.begin(); }
99 const_iterator end() const { return AttrList.end(); }
101 void Profile(FoldingSetNodeID &ID) const {
102 Profile(ID, AttrList);
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);
110 //===----------------------------------------------------------------------===//
112 /// \brief This class represents a set of attributes that apply to the function,
113 /// return type, and parameters.
114 class AttributeSetImpl : public FoldingSetNode {
115 friend class AttributeSet;
117 LLVMContext &Context;
118 SmallVector<AttributeWithIndex, 4> AttrList;
120 typedef std::pair<uint64_t, AttributeSetNode*> IndexAttrPair;
121 SmallVector<IndexAttrPair, 4> AttrNodes;
123 // AttributesSet is uniqued, these should not be publicly available.
124 void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
125 AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
127 AttributeSetImpl(LLVMContext &C, ArrayRef<AttributeWithIndex> attrs);
129 /// \brief Get the context that created this AttributeSetImpl.
130 LLVMContext &getContext() { return Context; }
132 ArrayRef<AttributeWithIndex> getAttributes() const { return AttrList; }
134 /// \brief Return the number of attributes this AttributeSet contains.
135 unsigned getNumAttributes() const { return AttrNodes.size(); }
137 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
138 /// is the index of the return, parameter, or function object that the
139 /// attributes are applied to, not the index into the AttrNodes list where the
140 /// attributes reside.
141 uint64_t getSlotIndex(unsigned Slot) const {
142 return AttrNodes[Slot].first;
145 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
146 /// \p Slot is an index into the AttrNodes list, not the index of the return /
147 /// parameter/ function which the attributes apply to.
148 AttributeSet getSlotAttributes(unsigned Slot) const {
149 // FIXME: This needs to use AttrNodes instead.
150 return AttributeSet::get(Context, AttrList[Slot]);
153 typedef AttributeSetNode::iterator iterator;
154 typedef AttributeSetNode::const_iterator const_iterator;
156 iterator begin(unsigned Idx)
157 { return AttrNodes[Idx].second->begin(); }
158 iterator end(unsigned Idx)
159 { return AttrNodes[Idx].second->end(); }
161 const_iterator begin(unsigned Idx) const
162 { return AttrNodes[Idx].second->begin(); }
163 const_iterator end(unsigned Idx) const
164 { return AttrNodes[Idx].second->end(); }
166 void Profile(FoldingSetNodeID &ID) const {
167 Profile(ID, AttrList);
169 static void Profile(FoldingSetNodeID &ID,
170 ArrayRef<AttributeWithIndex> AttrList) {
171 for (unsigned i = 0, e = AttrList.size(); i != e; ++i) {
172 ID.AddInteger(AttrList[i].Index);
173 ID.AddInteger(AttrList[i].Attrs.Raw());
177 static void Profile(FoldingSetNodeID &ID,
178 ArrayRef<std::pair<uint64_t, AttributeSetNode*> > Nodes) {
179 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
180 ID.AddInteger(Nodes[i].first);
181 ID.AddPointer(Nodes[i].second);
185 // FIXME: This atrocity is temporary.
186 uint64_t Raw(uint64_t Index) const;
189 } // end llvm namespace