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 {
35 SmallVector<Constant*, 0> Vals;
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)
42 : Context(C), Kind(Kind) {}
43 AttributeImpl(LLVMContext &C, Constant *Kind, ArrayRef<Constant*> Vals)
44 : Context(C), Kind(Kind), Vals(Vals.begin(), Vals.end()) {}
45 explicit AttributeImpl(LLVMContext &C, Attribute::AttrKind data);
46 AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
47 ArrayRef<Constant*> values);
48 AttributeImpl(LLVMContext &C, StringRef data);
50 LLVMContext &getContext() { return Context; }
52 bool hasAttribute(Attribute::AttrKind A) const;
54 Constant *getAttributeKind() const { return Kind; }
55 ArrayRef<Constant*> getAttributeValues() const { return Vals; }
57 uint64_t getAlignment() const;
58 uint64_t getStackAlignment() const;
60 /// \brief Equality and non-equality comparison operators.
61 bool operator==(Attribute::AttrKind Kind) const;
62 bool operator!=(Attribute::AttrKind Kind) const;
64 bool operator==(StringRef Kind) const;
65 bool operator!=(StringRef Kind) const;
67 /// \brief Used when sorting the attributes.
68 bool operator<(const AttributeImpl &AI) const;
70 void Profile(FoldingSetNodeID &ID) const {
71 Profile(ID, Kind, Vals);
73 static void Profile(FoldingSetNodeID &ID, Constant *Kind,
74 ArrayRef<Constant*> Vals) {
76 for (unsigned I = 0, E = Vals.size(); I != E; ++I)
77 ID.AddPointer(Vals[I]);
80 // FIXME: Remove these!
82 static uint64_t getAttrMask(Attribute::AttrKind Val);
85 //===----------------------------------------------------------------------===//
87 /// \brief This class represents a group of attributes that apply to one
88 /// element: function, return type, or parameter.
89 class AttributeSetNode : public FoldingSetNode {
90 SmallVector<Attribute, 4> AttrList;
92 AttributeSetNode(ArrayRef<Attribute> Attrs)
93 : AttrList(Attrs.begin(), Attrs.end()) {}
95 // AttributesSetNode is uniqued, these should not be publicly available.
96 void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
97 AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
99 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
101 bool hasAttribute(Attribute::AttrKind Kind) const;
102 bool hasAttributes() const { return !AttrList.empty(); }
104 unsigned getAlignment() const;
105 unsigned getStackAlignment() const;
106 std::string getAsString() const;
108 typedef SmallVectorImpl<Attribute>::iterator iterator;
109 typedef SmallVectorImpl<Attribute>::const_iterator const_iterator;
111 iterator begin() { return AttrList.begin(); }
112 iterator end() { return AttrList.end(); }
114 const_iterator begin() const { return AttrList.begin(); }
115 const_iterator end() const { return AttrList.end(); }
117 void Profile(FoldingSetNodeID &ID) const {
118 Profile(ID, AttrList);
120 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
121 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
122 AttrList[I].Profile(ID);
126 //===----------------------------------------------------------------------===//
128 /// \brief This class represents a set of attributes that apply to the function,
129 /// return type, and parameters.
130 class AttributeSetImpl : public FoldingSetNode {
131 friend class AttributeSet;
133 LLVMContext &Context;
135 typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
136 SmallVector<IndexAttrPair, 4> AttrNodes;
138 // AttributesSet is uniqued, these should not be publicly available.
139 void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
140 AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
142 AttributeSetImpl(LLVMContext &C,
143 ArrayRef<std::pair<unsigned, AttributeSetNode*> > attrs)
144 : Context(C), AttrNodes(attrs.begin(), attrs.end()) {}
146 /// \brief Get the context that created this AttributeSetImpl.
147 LLVMContext &getContext() { return Context; }
149 /// \brief Return the number of attributes this AttributeSet contains.
150 unsigned getNumAttributes() const { return AttrNodes.size(); }
152 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
153 /// is the index of the return, parameter, or function object that the
154 /// attributes are applied to, not the index into the AttrNodes list where the
155 /// attributes reside.
156 uint64_t getSlotIndex(unsigned Slot) const {
157 return AttrNodes[Slot].first;
160 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
161 /// \p Slot is an index into the AttrNodes list, not the index of the return /
162 /// parameter/ function which the attributes apply to.
163 AttributeSet getSlotAttributes(unsigned Slot) const {
164 // FIXME: This needs to use AttrNodes instead.
165 return AttributeSet::get(Context, AttrNodes[Slot]);
168 /// \brief Retrieve the attribute set node for the given "slot" in the
170 AttributeSetNode *getSlotNode(unsigned Slot) const {
171 return AttrNodes[Slot].second;
174 typedef AttributeSetNode::iterator iterator;
175 typedef AttributeSetNode::const_iterator const_iterator;
177 iterator begin(unsigned Idx)
178 { return AttrNodes[Idx].second->begin(); }
179 iterator end(unsigned Idx)
180 { return AttrNodes[Idx].second->end(); }
182 const_iterator begin(unsigned Idx) const
183 { return AttrNodes[Idx].second->begin(); }
184 const_iterator end(unsigned Idx) const
185 { return AttrNodes[Idx].second->end(); }
187 void Profile(FoldingSetNodeID &ID) const {
188 Profile(ID, AttrNodes);
190 static void Profile(FoldingSetNodeID &ID,
191 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
192 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
193 ID.AddInteger(Nodes[i].first);
194 ID.AddPointer(Nodes[i].second);
198 // FIXME: This atrocity is temporary.
199 uint64_t Raw(uint64_t Index) const;
202 } // end llvm namespace