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 AttributeImpl(LLVMContext &C, Constant *Data)
41 : Context(C), Data(Data) {}
42 explicit AttributeImpl(LLVMContext &C, Attribute::AttrKind data);
43 AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
44 ArrayRef<Constant*> values);
45 AttributeImpl(LLVMContext &C, StringRef data);
47 bool hasAttribute(Attribute::AttrKind A) const;
48 bool hasAttributes() const;
50 LLVMContext &getContext() { return Context; }
51 ArrayRef<Constant*> getValues() const { return Vals; }
53 uint64_t getAlignment() const;
54 uint64_t getStackAlignment() const;
56 bool operator==(Attribute::AttrKind Kind) const;
57 bool operator!=(Attribute::AttrKind Kind) const;
59 bool operator==(StringRef Kind) const;
60 bool operator!=(StringRef Kind) const;
62 bool operator<(const AttributeImpl &AI) const;
64 void Profile(FoldingSetNodeID &ID) const {
65 Profile(ID, Data, Vals);
67 static void Profile(FoldingSetNodeID &ID, Constant *Data,
68 ArrayRef<Constant*> Vals) {
70 for (unsigned I = 0, E = Vals.size(); I != E; ++I)
71 ID.AddPointer(Vals[I]);
74 // FIXME: Remove these!
76 static uint64_t getAttrMask(Attribute::AttrKind Val);
79 //===----------------------------------------------------------------------===//
81 /// \brief This class represents a group of attributes that apply to one
82 /// element: function, return type, or parameter.
83 class AttributeSetNode : public FoldingSetNode {
84 SmallVector<Attribute, 4> AttrList;
86 AttributeSetNode(ArrayRef<Attribute> Attrs)
87 : AttrList(Attrs.begin(), Attrs.end()) {}
89 // AttributesSetNode is uniqued, these should not be publicly available.
90 void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
91 AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
93 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
95 typedef SmallVectorImpl<Attribute>::iterator iterator;
96 typedef SmallVectorImpl<Attribute>::const_iterator const_iterator;
98 iterator begin() { return AttrList.begin(); }
99 iterator end() { return AttrList.end(); }
101 const_iterator begin() const { return AttrList.begin(); }
102 const_iterator end() const { return AttrList.end(); }
104 void Profile(FoldingSetNodeID &ID) const {
105 Profile(ID, AttrList);
107 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
108 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
109 AttrList[I].Profile(ID);
113 //===----------------------------------------------------------------------===//
115 /// \brief This class represents a set of attributes that apply to the function,
116 /// return type, and parameters.
117 class AttributeSetImpl : public FoldingSetNode {
118 friend class AttributeSet;
120 LLVMContext &Context;
122 typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
123 SmallVector<IndexAttrPair, 4> AttrNodes;
125 // AttributesSet is uniqued, these should not be publicly available.
126 void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
127 AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
129 AttributeSetImpl(LLVMContext &C,
130 ArrayRef<std::pair<unsigned, AttributeSetNode*> > attrs)
131 : Context(C), AttrNodes(attrs.begin(), attrs.end()) {}
133 /// \brief Get the context that created this AttributeSetImpl.
134 LLVMContext &getContext() { return Context; }
136 /// \brief Return the number of attributes this AttributeSet contains.
137 unsigned getNumAttributes() const { return AttrNodes.size(); }
139 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
140 /// is the index of the return, parameter, or function object that the
141 /// attributes are applied to, not the index into the AttrNodes list where the
142 /// attributes reside.
143 uint64_t getSlotIndex(unsigned Slot) const {
144 return AttrNodes[Slot].first;
147 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
148 /// \p Slot is an index into the AttrNodes list, not the index of the return /
149 /// parameter/ function which the attributes apply to.
150 AttributeSet getSlotAttributes(unsigned Slot) const {
151 // FIXME: This needs to use AttrNodes instead.
152 return AttributeSet::get(Context, AttrNodes[Slot]);
155 typedef AttributeSetNode::iterator iterator;
156 typedef AttributeSetNode::const_iterator const_iterator;
158 iterator begin(unsigned Idx)
159 { return AttrNodes[Idx].second->begin(); }
160 iterator end(unsigned Idx)
161 { return AttrNodes[Idx].second->end(); }
163 const_iterator begin(unsigned Idx) const
164 { return AttrNodes[Idx].second->begin(); }
165 const_iterator end(unsigned Idx) const
166 { return AttrNodes[Idx].second->end(); }
168 void Profile(FoldingSetNodeID &ID) const {
169 Profile(ID, AttrNodes);
171 static void Profile(FoldingSetNodeID &ID,
172 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
173 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
174 ID.AddInteger(Nodes[i].first);
175 ID.AddPointer(Nodes[i].second);
179 // FIXME: This atrocity is temporary.
180 uint64_t Raw(uint64_t Index) const;
183 } // end llvm namespace