blob: 8d5de77eed1f581788d0bc37f768ff4817c2993a [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 Wendling2c79ecb2012-09-26 21:07:29 +000021
22namespace llvm {
23
Bill Wendling7c1683d2012-12-29 12:29:38 +000024class Constant;
Bill Wendling5f93e2b2012-12-19 23:55:43 +000025class LLVMContext;
26
Bill Wendling27107f62012-12-20 21:28:43 +000027//===----------------------------------------------------------------------===//
28/// \class
29/// \brief This class represents a single, uniqued attribute. That attribute
Bill Wendling979aff62012-12-30 02:22:16 +000030/// could be a single enum, a tuple, or a string.
Bill Wendlingf6670722012-12-20 01:36:59 +000031class AttributeImpl : public FoldingSetNode {
Bill Wendling1bbd6442013-01-05 01:36:54 +000032 LLVMContext &Context;
Bill Wendling7c1683d2012-12-29 12:29:38 +000033 Constant *Data;
Bill Wendling979aff62012-12-30 02:22:16 +000034 SmallVector<Constant*, 0> Vals;
Bill Wendling1cc0d5a2013-01-27 21:38:03 +000035
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;
Bill Wendling2c79ecb2012-09-26 21:07:29 +000039public:
Bill Wendling979aff62012-12-30 02:22:16 +000040 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);
45
Bill Wendlingc5f1bc82013-01-21 21:57:28 +000046 LLVMContext &getContext() { return Context; }
47
Bill Wendlinga90a99a2013-01-07 08:24:35 +000048 ArrayRef<Constant*> getValues() const { return Vals; }
Bill Wendling2c79ecb2012-09-26 21:07:29 +000049
Bill Wendling60507d52013-01-04 20:54:35 +000050 bool hasAttribute(Attribute::AttrKind A) const;
Bill Wendling67658342012-10-09 07:45:08 +000051
Bill Wendling8e635db2012-10-08 21:47:17 +000052 bool hasAttributes() const;
Bill Wendling8e635db2012-10-08 21:47:17 +000053
54 uint64_t getAlignment() const;
55 uint64_t getStackAlignment() const;
56
Bill Wendling60507d52013-01-04 20:54:35 +000057 bool operator==(Attribute::AttrKind Kind) const;
58 bool operator!=(Attribute::AttrKind Kind) const;
Bill Wendling529ec712012-12-30 01:38:39 +000059
Bill Wendling60507d52013-01-04 20:54:35 +000060 bool operator==(StringRef Kind) const;
61 bool operator!=(StringRef Kind) const;
Bill Wendling529ec712012-12-30 01:38:39 +000062
Bill Wendling3467e302013-01-24 00:06:56 +000063 bool operator<(const AttributeImpl &AI) const;
64
Bill Wendling1db9b692013-01-09 23:36:50 +000065 uint64_t Raw() const; // FIXME: Remove.
Bill Wendling925bcb12012-10-16 05:57:28 +000066
Bill Wendling60507d52013-01-04 20:54:35 +000067 static uint64_t getAttrMask(Attribute::AttrKind Val);
Bill Wendling2e879bc2012-10-09 09:11:20 +000068
Bill Wendling2c79ecb2012-09-26 21:07:29 +000069 void Profile(FoldingSetNodeID &ID) const {
Bill Wendling979aff62012-12-30 02:22:16 +000070 Profile(ID, Data, Vals);
Bill Wendling2c79ecb2012-09-26 21:07:29 +000071 }
Bill Wendling979aff62012-12-30 02:22:16 +000072 static void Profile(FoldingSetNodeID &ID, Constant *Data,
Bill Wendlingff887162013-01-09 00:32:08 +000073 ArrayRef<Constant*> Vals);
Bill Wendling2c79ecb2012-09-26 21:07:29 +000074};
75
Bill Wendling27107f62012-12-20 21:28:43 +000076//===----------------------------------------------------------------------===//
77/// \class
Bill Wendling3467e302013-01-24 00:06:56 +000078/// \brief This class represents a group of attributes that apply to one
79/// element: function, return type, or parameter.
80class AttributeSetNode : public FoldingSetNode {
81 SmallVector<Attribute, 4> AttrList;
82
83 AttributeSetNode(ArrayRef<Attribute> Attrs)
84 : AttrList(Attrs.begin(), Attrs.end()) {}
Bill Wendling1cc0d5a2013-01-27 21:38:03 +000085
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;
Bill Wendling3467e302013-01-24 00:06:56 +000089public:
90 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
91
92 typedef SmallVectorImpl<Attribute>::iterator iterator;
93 typedef SmallVectorImpl<Attribute>::const_iterator const_iterator;
94
95 iterator begin() { return AttrList.begin(); }
96 iterator end() { return AttrList.end(); }
97
98 const_iterator begin() const { return AttrList.begin(); }
99 const_iterator end() const { return AttrList.end(); }
100
101 void Profile(FoldingSetNodeID &ID) const {
102 Profile(ID, AttrList);
103 }
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);
107 }
108};
109
110//===----------------------------------------------------------------------===//
111/// \class
112/// \brief This class represents a set of attributes that apply to the function,
113/// return type, and parameters.
Bill Wendling18e72112012-12-19 22:42:22 +0000114class AttributeSetImpl : public FoldingSetNode {
Bill Wendling8e47daf2013-01-25 23:09:36 +0000115 friend class AttributeSet;
116
Bill Wendling60507d52013-01-04 20:54:35 +0000117 LLVMContext &Context;
118 SmallVector<AttributeWithIndex, 4> AttrList;
119
Bill Wendling73bc4522013-01-28 00:21:34 +0000120 typedef std::pair<uint64_t, AttributeSetNode*> IndexAttrPair;
121 SmallVector<IndexAttrPair, 4> AttrNodes;
Bill Wendlingbb085932013-01-24 01:01:34 +0000122
Bill Wendling831737d2012-12-30 10:32:01 +0000123 // AttributesSet is uniqued, these should not be publicly available.
Bill Wendling18e72112012-12-19 22:42:22 +0000124 void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
125 AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
Bill Wendling0976e002012-11-20 05:09:20 +0000126public:
Bill Wendling6a325cc2013-01-27 12:50:02 +0000127 AttributeSetImpl(LLVMContext &C, ArrayRef<AttributeWithIndex> attrs);
Bill Wendling60507d52013-01-04 20:54:35 +0000128
Bill Wendling893eac12013-01-27 21:32:11 +0000129 /// \brief Get the context that created this AttributeSetImpl.
Bill Wendling60507d52013-01-04 20:54:35 +0000130 LLVMContext &getContext() { return Context; }
Bill Wendling893eac12013-01-27 21:32:11 +0000131
Bill Wendling60507d52013-01-04 20:54:35 +0000132 ArrayRef<AttributeWithIndex> getAttributes() const { return AttrList; }
Bill Wendling893eac12013-01-27 21:32:11 +0000133
134 /// \brief Return the number of attributes this AttributeSet contains.
135 unsigned getNumAttributes() const { return AttrNodes.size(); }
136
137 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
138 /// is the index of the return, parameter, or function object that the
139 /// attributes are applied to, not the index into the AttrNodes list where the
140 /// attributes reside.
Bill Wendling73bc4522013-01-28 00:21:34 +0000141 uint64_t getSlotIndex(unsigned Slot) const {
142 return AttrNodes[Slot].first;
143 }
Bill Wendling893eac12013-01-27 21:32:11 +0000144
145 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
146 /// \p Slot is an index into the AttrNodes list, not the index of the return /
147 /// parameter/ function which the attributes apply to.
Bill Wendling8e47daf2013-01-25 23:09:36 +0000148 AttributeSet getSlotAttributes(unsigned Slot) const {
149 // FIXME: This needs to use AttrNodes instead.
150 return AttributeSet::get(Context, AttrList[Slot]);
151 }
Bill Wendling0976e002012-11-20 05:09:20 +0000152
Bill Wendling73bc4522013-01-28 00:21:34 +0000153 typedef AttributeSetNode::iterator iterator;
154 typedef AttributeSetNode::const_iterator const_iterator;
155
156 iterator begin(unsigned Idx)
157 { return AttrNodes[Idx].second->begin(); }
158 iterator end(unsigned Idx)
159 { return AttrNodes[Idx].second->end(); }
160
161 const_iterator begin(unsigned Idx) const
162 { return AttrNodes[Idx].second->begin(); }
163 const_iterator end(unsigned Idx) const
164 { return AttrNodes[Idx].second->end(); }
165
Bill Wendling0976e002012-11-20 05:09:20 +0000166 void Profile(FoldingSetNodeID &ID) const {
Bill Wendling60507d52013-01-04 20:54:35 +0000167 Profile(ID, AttrList);
Bill Wendling0976e002012-11-20 05:09:20 +0000168 }
Bill Wendling60507d52013-01-04 20:54:35 +0000169 static void Profile(FoldingSetNodeID &ID,
Bill Wendlingbb085932013-01-24 01:01:34 +0000170 ArrayRef<AttributeWithIndex> AttrList) {
Bill Wendling60507d52013-01-04 20:54:35 +0000171 for (unsigned i = 0, e = AttrList.size(); i != e; ++i) {
172 ID.AddInteger(AttrList[i].Index);
Bill Wendling1db9b692013-01-09 23:36:50 +0000173 ID.AddInteger(AttrList[i].Attrs.Raw());
Bill Wendling0976e002012-11-20 05:09:20 +0000174 }
175 }
Bill Wendlingbb085932013-01-24 01:01:34 +0000176
177 static void Profile(FoldingSetNodeID &ID,
178 ArrayRef<std::pair<uint64_t, AttributeSetNode*> > Nodes) {
179 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
180 ID.AddInteger(Nodes[i].first);
181 ID.AddPointer(Nodes[i].second);
182 }
183 }
Bill Wendlingd05204a2013-01-27 23:41:29 +0000184
185 // FIXME: This atrocity is temporary.
186 uint64_t Raw(uint64_t Index) const;
Bill Wendling0976e002012-11-20 05:09:20 +0000187};
188
Bill Wendling2c79ecb2012-09-26 21:07:29 +0000189} // end llvm namespace
190
191#endif