blob: 2eb7f075e32ae2b12d3152025e0c2a58c3d07860 [file] [log] [blame]
Bill Wendlingf6670722012-12-20 01:36:59 +00001//===-- AttributeImpl.h - Attribute Internals -------------------*- C++ -*-===//
Bill Wendling2c79ecb2012-09-26 21:07:29 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Bill Wendling27107f62012-12-20 21:28:43 +00009///
10/// \file
11/// \brief This file defines various helper methods and classes used by
12/// LLVMContextImpl for creating and managing attributes.
13///
Bill Wendling2c79ecb2012-09-26 21:07:29 +000014//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_ATTRIBUTESIMPL_H
17#define LLVM_ATTRIBUTESIMPL_H
18
19#include "llvm/ADT/FoldingSet.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Attributes.h"
Bill Wendling606c8e32013-01-29 03:20:31 +000021#include <string>
Bill Wendling2c79ecb2012-09-26 21:07:29 +000022
23namespace llvm {
24
Bill Wendling7c1683d2012-12-29 12:29:38 +000025class Constant;
Bill Wendling5f93e2b2012-12-19 23:55:43 +000026class LLVMContext;
27
Bill Wendling27107f62012-12-20 21:28:43 +000028//===----------------------------------------------------------------------===//
29/// \class
30/// \brief This class represents a single, uniqued attribute. That attribute
Bill Wendling979aff62012-12-30 02:22:16 +000031/// could be a single enum, a tuple, or a string.
Bill Wendlingf6670722012-12-20 01:36:59 +000032class AttributeImpl : public FoldingSetNode {
Bill Wendling1bbd6442013-01-05 01:36:54 +000033 LLVMContext &Context;
Bill Wendling9f175f82013-01-29 20:37:10 +000034 Constant *Kind;
Bill Wendling979aff62012-12-30 02:22:16 +000035 SmallVector<Constant*, 0> Vals;
Bill Wendling1cc0d5a2013-01-27 21:38:03 +000036
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;
Bill Wendling2c79ecb2012-09-26 21:07:29 +000040public:
Bill Wendling9f175f82013-01-29 20:37:10 +000041 AttributeImpl(LLVMContext &C, Constant *Kind)
42 : Context(C), Kind(Kind) {}
Bill Wendling169d5272013-01-31 23:16:25 +000043 AttributeImpl(LLVMContext &C, Constant *Kind, ArrayRef<Constant*> Vals)
44 : Context(C), Kind(Kind), Vals(Vals.begin(), Vals.end()) {}
Bill Wendling979aff62012-12-30 02:22:16 +000045 explicit AttributeImpl(LLVMContext &C, Attribute::AttrKind data);
46 AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
47 ArrayRef<Constant*> values);
48 AttributeImpl(LLVMContext &C, StringRef data);
49
Bill Wendling14292a62013-01-31 20:59:05 +000050 LLVMContext &getContext() { return Context; }
51
Bill Wendling60507d52013-01-04 20:54:35 +000052 bool hasAttribute(Attribute::AttrKind A) const;
Bill Wendling8e635db2012-10-08 21:47:17 +000053
Bill Wendling9f175f82013-01-29 20:37:10 +000054 Constant *getAttributeKind() const { return Kind; }
55 ArrayRef<Constant*> getAttributeValues() const { return Vals; }
56
Bill Wendling8e635db2012-10-08 21:47:17 +000057 uint64_t getAlignment() const;
58 uint64_t getStackAlignment() const;
59
Bill Wendling14292a62013-01-31 20:59:05 +000060 /// \brief Equality and non-equality comparison operators.
Bill Wendling60507d52013-01-04 20:54:35 +000061 bool operator==(Attribute::AttrKind Kind) const;
62 bool operator!=(Attribute::AttrKind Kind) const;
Bill Wendling529ec712012-12-30 01:38:39 +000063
Bill Wendling60507d52013-01-04 20:54:35 +000064 bool operator==(StringRef Kind) const;
65 bool operator!=(StringRef Kind) const;
Bill Wendling529ec712012-12-30 01:38:39 +000066
Bill Wendling14292a62013-01-31 20:59:05 +000067 /// \brief Used when sorting the attributes.
Bill Wendling3467e302013-01-24 00:06:56 +000068 bool operator<(const AttributeImpl &AI) const;
69
Bill Wendling2c79ecb2012-09-26 21:07:29 +000070 void Profile(FoldingSetNodeID &ID) const {
Bill Wendling9f175f82013-01-29 20:37:10 +000071 Profile(ID, Kind, Vals);
Bill Wendling2c79ecb2012-09-26 21:07:29 +000072 }
Bill Wendling9f175f82013-01-29 20:37:10 +000073 static void Profile(FoldingSetNodeID &ID, Constant *Kind,
Bill Wendlingc22f4aa2013-01-29 00:34:06 +000074 ArrayRef<Constant*> Vals) {
Bill Wendling9f175f82013-01-29 20:37:10 +000075 ID.AddPointer(Kind);
Bill Wendlingc22f4aa2013-01-29 00:34:06 +000076 for (unsigned I = 0, E = Vals.size(); I != E; ++I)
77 ID.AddPointer(Vals[I]);
78 }
79
Bill Wendlingf715dbd2013-02-01 00:48:14 +000080 // FIXME: Remove this!
Bill Wendlingc22f4aa2013-01-29 00:34:06 +000081 static uint64_t getAttrMask(Attribute::AttrKind Val);
Bill Wendling2c79ecb2012-09-26 21:07:29 +000082};
83
Bill Wendling27107f62012-12-20 21:28:43 +000084//===----------------------------------------------------------------------===//
85/// \class
Bill Wendling3467e302013-01-24 00:06:56 +000086/// \brief This class represents a group of attributes that apply to one
87/// element: function, return type, or parameter.
88class AttributeSetNode : public FoldingSetNode {
89 SmallVector<Attribute, 4> AttrList;
90
91 AttributeSetNode(ArrayRef<Attribute> Attrs)
92 : AttrList(Attrs.begin(), Attrs.end()) {}
Bill Wendling1cc0d5a2013-01-27 21:38:03 +000093
94 // AttributesSetNode is uniqued, these should not be publicly available.
95 void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
96 AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
Bill Wendling3467e302013-01-24 00:06:56 +000097public:
98 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
99
Bill Wendling606c8e32013-01-29 03:20:31 +0000100 bool hasAttribute(Attribute::AttrKind Kind) const;
101 bool hasAttributes() const { return !AttrList.empty(); }
102
103 unsigned getAlignment() const;
104 unsigned getStackAlignment() const;
105 std::string getAsString() const;
106
Bill Wendling3467e302013-01-24 00:06:56 +0000107 typedef SmallVectorImpl<Attribute>::iterator iterator;
108 typedef SmallVectorImpl<Attribute>::const_iterator const_iterator;
109
110 iterator begin() { return AttrList.begin(); }
111 iterator end() { return AttrList.end(); }
112
113 const_iterator begin() const { return AttrList.begin(); }
114 const_iterator end() const { return AttrList.end(); }
115
116 void Profile(FoldingSetNodeID &ID) const {
117 Profile(ID, AttrList);
118 }
119 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
120 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
121 AttrList[I].Profile(ID);
122 }
123};
124
125//===----------------------------------------------------------------------===//
126/// \class
127/// \brief This class represents a set of attributes that apply to the function,
128/// return type, and parameters.
Bill Wendling18e72112012-12-19 22:42:22 +0000129class AttributeSetImpl : public FoldingSetNode {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000130 friend class AttributeSet;
131
Bill Wendling60507d52013-01-04 20:54:35 +0000132 LLVMContext &Context;
Bill Wendling60507d52013-01-04 20:54:35 +0000133
Bill Wendling6bdbf062013-01-28 22:33:39 +0000134 typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
Bill Wendling73bc4522013-01-28 00:21:34 +0000135 SmallVector<IndexAttrPair, 4> AttrNodes;
Bill Wendlingbb085932013-01-24 01:01:34 +0000136
Bill Wendling831737d2012-12-30 10:32:01 +0000137 // AttributesSet is uniqued, these should not be publicly available.
Bill Wendling18e72112012-12-19 22:42:22 +0000138 void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
139 AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
Bill Wendling0976e002012-11-20 05:09:20 +0000140public:
Bill Wendling87e10df2013-01-28 21:55:20 +0000141 AttributeSetImpl(LLVMContext &C,
Bill Wendling6bdbf062013-01-28 22:33:39 +0000142 ArrayRef<std::pair<unsigned, AttributeSetNode*> > attrs)
Bill Wendling87e10df2013-01-28 21:55:20 +0000143 : Context(C), AttrNodes(attrs.begin(), attrs.end()) {}
Bill Wendling60507d52013-01-04 20:54:35 +0000144
Bill Wendling893eac12013-01-27 21:32:11 +0000145 /// \brief Get the context that created this AttributeSetImpl.
Bill Wendling60507d52013-01-04 20:54:35 +0000146 LLVMContext &getContext() { return Context; }
Bill Wendling893eac12013-01-27 21:32:11 +0000147
Bill Wendling893eac12013-01-27 21:32:11 +0000148 /// \brief Return the number of attributes this AttributeSet contains.
149 unsigned getNumAttributes() const { return AttrNodes.size(); }
150
151 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
152 /// is the index of the return, parameter, or function object that the
153 /// attributes are applied to, not the index into the AttrNodes list where the
154 /// attributes reside.
Bill Wendling73bc4522013-01-28 00:21:34 +0000155 uint64_t getSlotIndex(unsigned Slot) const {
156 return AttrNodes[Slot].first;
157 }
Bill Wendling893eac12013-01-27 21:32:11 +0000158
159 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
160 /// \p Slot is an index into the AttrNodes list, not the index of the return /
161 /// parameter/ function which the attributes apply to.
Bill Wendling8e47daf2013-01-25 23:09:36 +0000162 AttributeSet getSlotAttributes(unsigned Slot) const {
163 // FIXME: This needs to use AttrNodes instead.
Bill Wendling87e10df2013-01-28 21:55:20 +0000164 return AttributeSet::get(Context, AttrNodes[Slot]);
Bill Wendling8e47daf2013-01-25 23:09:36 +0000165 }
Bill Wendling0976e002012-11-20 05:09:20 +0000166
Bill Wendling606c8e32013-01-29 03:20:31 +0000167 /// \brief Retrieve the attribute set node for the given "slot" in the
168 /// AttrNode list.
169 AttributeSetNode *getSlotNode(unsigned Slot) const {
170 return AttrNodes[Slot].second;
171 }
172
Bill Wendling73bc4522013-01-28 00:21:34 +0000173 typedef AttributeSetNode::iterator iterator;
174 typedef AttributeSetNode::const_iterator const_iterator;
175
176 iterator begin(unsigned Idx)
177 { return AttrNodes[Idx].second->begin(); }
178 iterator end(unsigned Idx)
179 { return AttrNodes[Idx].second->end(); }
180
181 const_iterator begin(unsigned Idx) const
182 { return AttrNodes[Idx].second->begin(); }
183 const_iterator end(unsigned Idx) const
184 { return AttrNodes[Idx].second->end(); }
185
Bill Wendling0976e002012-11-20 05:09:20 +0000186 void Profile(FoldingSetNodeID &ID) const {
Bill Wendling87e10df2013-01-28 21:55:20 +0000187 Profile(ID, AttrNodes);
Bill Wendling0976e002012-11-20 05:09:20 +0000188 }
Bill Wendling60507d52013-01-04 20:54:35 +0000189 static void Profile(FoldingSetNodeID &ID,
Bill Wendling6bdbf062013-01-28 22:33:39 +0000190 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
Bill Wendlingbb085932013-01-24 01:01:34 +0000191 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
192 ID.AddInteger(Nodes[i].first);
193 ID.AddPointer(Nodes[i].second);
194 }
195 }
Bill Wendlingd05204a2013-01-27 23:41:29 +0000196
197 // FIXME: This atrocity is temporary.
198 uint64_t Raw(uint64_t Index) const;
Bill Wendling0976e002012-11-20 05:09:20 +0000199};
200
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000201} // end llvm namespace
202
203#endif