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 explicit AttributeImpl(LLVMContext &C, Attribute::AttrKind data);
44 AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
45 ArrayRef<Constant*> values);
46 AttributeImpl(LLVMContext &C, StringRef data);
48 bool hasAttribute(Attribute::AttrKind A) const;
50 Constant *getAttributeKind() const { return Kind; }
51 ArrayRef<Constant*> getAttributeValues() const { return Vals; }
53 LLVMContext &getContext() { return Context; }
54 ArrayRef<Constant*> getValues() const { return Vals; }
56 uint64_t getAlignment() const;
57 uint64_t getStackAlignment() const;
59 bool operator==(Attribute::AttrKind Kind) const;
60 bool operator!=(Attribute::AttrKind Kind) const;
62 bool operator==(StringRef Kind) const;
63 bool operator!=(StringRef Kind) const;
65 bool operator<(const AttributeImpl &AI) const;
67 void Profile(FoldingSetNodeID &ID) const {
68 Profile(ID, Kind, Vals);
70 static void Profile(FoldingSetNodeID &ID, Constant *Kind,
71 ArrayRef<Constant*> Vals) {
73 for (unsigned I = 0, E = Vals.size(); I != E; ++I)
74 ID.AddPointer(Vals[I]);
77 // FIXME: Remove these!
79 static uint64_t getAttrMask(Attribute::AttrKind Val);
82 //===----------------------------------------------------------------------===//
84 /// \brief This class represents a group of attributes that apply to one
85 /// element: function, return type, or parameter.
86 class AttributeSetNode : public FoldingSetNode {
87 SmallVector<Attribute, 4> AttrList;
89 AttributeSetNode(ArrayRef<Attribute> Attrs)
90 : AttrList(Attrs.begin(), Attrs.end()) {}
92 // AttributesSetNode is uniqued, these should not be publicly available.
93 void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
94 AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
96 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
98 bool hasAttribute(Attribute::AttrKind Kind) const;
99 bool hasAttributes() const { return !AttrList.empty(); }
101 unsigned getAlignment() const;
102 unsigned getStackAlignment() const;
103 std::string getAsString() const;
105 typedef SmallVectorImpl<Attribute>::iterator iterator;
106 typedef SmallVectorImpl<Attribute>::const_iterator const_iterator;
108 iterator begin() { return AttrList.begin(); }
109 iterator end() { return AttrList.end(); }
111 const_iterator begin() const { return AttrList.begin(); }
112 const_iterator end() const { return AttrList.end(); }
114 void Profile(FoldingSetNodeID &ID) const {
115 Profile(ID, AttrList);
117 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
118 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
119 AttrList[I].Profile(ID);
123 //===----------------------------------------------------------------------===//
125 /// \brief This class represents a set of attributes that apply to the function,
126 /// return type, and parameters.
127 class AttributeSetImpl : public FoldingSetNode {
128 friend class AttributeSet;
130 LLVMContext &Context;
132 typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
133 SmallVector<IndexAttrPair, 4> AttrNodes;
135 // AttributesSet is uniqued, these should not be publicly available.
136 void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
137 AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
139 AttributeSetImpl(LLVMContext &C,
140 ArrayRef<std::pair<unsigned, AttributeSetNode*> > attrs)
141 : Context(C), AttrNodes(attrs.begin(), attrs.end()) {}
143 /// \brief Get the context that created this AttributeSetImpl.
144 LLVMContext &getContext() { return Context; }
146 /// \brief Return the number of attributes this AttributeSet contains.
147 unsigned getNumAttributes() const { return AttrNodes.size(); }
149 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
150 /// is the index of the return, parameter, or function object that the
151 /// attributes are applied to, not the index into the AttrNodes list where the
152 /// attributes reside.
153 uint64_t getSlotIndex(unsigned Slot) const {
154 return AttrNodes[Slot].first;
157 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
158 /// \p Slot is an index into the AttrNodes list, not the index of the return /
159 /// parameter/ function which the attributes apply to.
160 AttributeSet getSlotAttributes(unsigned Slot) const {
161 // FIXME: This needs to use AttrNodes instead.
162 return AttributeSet::get(Context, AttrNodes[Slot]);
165 /// \brief Retrieve the attribute set node for the given "slot" in the
167 AttributeSetNode *getSlotNode(unsigned Slot) const {
168 return AttrNodes[Slot].second;
171 typedef AttributeSetNode::iterator iterator;
172 typedef AttributeSetNode::const_iterator const_iterator;
174 iterator begin(unsigned Idx)
175 { return AttrNodes[Idx].second->begin(); }
176 iterator end(unsigned Idx)
177 { return AttrNodes[Idx].second->end(); }
179 const_iterator begin(unsigned Idx) const
180 { return AttrNodes[Idx].second->begin(); }
181 const_iterator end(unsigned Idx) const
182 { return AttrNodes[Idx].second->end(); }
184 void Profile(FoldingSetNodeID &ID) const {
185 Profile(ID, AttrNodes);
187 static void Profile(FoldingSetNodeID &ID,
188 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
189 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
190 ID.AddInteger(Nodes[i].first);
191 ID.AddPointer(Nodes[i].second);
195 // FIXME: This atrocity is temporary.
196 uint64_t Raw(uint64_t Index) const;
199 } // end llvm namespace