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 SmallVector<std::pair<uint64_t, AttributeSetNode*>, 4> AttrNodes;
122 // AttributesSet is uniqued, these should not be publicly available.
123 void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
124 AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
126 AttributeSetImpl(LLVMContext &C, ArrayRef<AttributeWithIndex> attrs);
128 /// \brief Get the context that created this AttributeSetImpl.
129 LLVMContext &getContext() { return Context; }
131 ArrayRef<AttributeWithIndex> getAttributes() const { return AttrList; }
133 /// \brief Return the number of attributes this AttributeSet contains.
134 unsigned getNumAttributes() const { return AttrNodes.size(); }
136 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
137 /// is the index of the return, parameter, or function object that the
138 /// attributes are applied to, not the index into the AttrNodes list where the
139 /// attributes reside.
140 unsigned getSlotIndex(unsigned Slot) const { return AttrNodes[Slot].first; }
142 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
143 /// \p Slot is an index into the AttrNodes list, not the index of the return /
144 /// parameter/ function which the attributes apply to.
145 AttributeSet getSlotAttributes(unsigned Slot) const {
146 // FIXME: This needs to use AttrNodes instead.
147 return AttributeSet::get(Context, AttrList[Slot]);
150 void Profile(FoldingSetNodeID &ID) const {
151 Profile(ID, AttrList);
153 static void Profile(FoldingSetNodeID &ID,
154 ArrayRef<AttributeWithIndex> AttrList) {
155 for (unsigned i = 0, e = AttrList.size(); i != e; ++i) {
156 ID.AddInteger(AttrList[i].Index);
157 ID.AddInteger(AttrList[i].Attrs.Raw());
161 static void Profile(FoldingSetNodeID &ID,
162 ArrayRef<std::pair<uint64_t, AttributeSetNode*> > Nodes) {
163 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
164 ID.AddInteger(Nodes[i].first);
165 ID.AddPointer(Nodes[i].second);
170 } // end llvm namespace