blob: 3fbd723e1170468ffce1eae01d49fcaeeec257e7 [file] [log] [blame]
Bill Wendling4607f4b2012-12-20 01:36:59 +00001//===-- AttributeImpl.h - Attribute Internals -------------------*- C++ -*-===//
Bill Wendlinge38b8042012-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 Wendling66e978f2012-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 Wendlinge38b8042012-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 Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Attributes.h"
Bill Wendlingf2955aa2013-01-29 03:20:31 +000021#include <string>
Bill Wendlinge38b8042012-09-26 21:07:29 +000022
23namespace llvm {
24
Bill Wendling0cd0f7f2012-12-29 12:29:38 +000025class Constant;
Bill Wendling6ad6c3b2012-12-19 23:55:43 +000026class LLVMContext;
27
Bill Wendling66e978f2012-12-20 21:28:43 +000028//===----------------------------------------------------------------------===//
29/// \class
30/// \brief This class represents a single, uniqued attribute. That attribute
Bill Wendling3a554ac2012-12-30 02:22:16 +000031/// could be a single enum, a tuple, or a string.
Bill Wendling4607f4b2012-12-20 01:36:59 +000032class AttributeImpl : public FoldingSetNode {
Bill Wendling960f52a2013-01-05 01:36:54 +000033 LLVMContext &Context;
Bill Wendlingc3c714b2013-01-29 20:37:10 +000034 Constant *Kind;
Bill Wendling3a554ac2012-12-30 02:22:16 +000035 SmallVector<Constant*, 0> Vals;
Bill Wendling97b4f702013-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 Wendlinge38b8042012-09-26 21:07:29 +000040public:
Bill Wendlingc79cdff2013-02-01 01:04:27 +000041 AttributeImpl(LLVMContext &C, Constant *Kind,
42 ArrayRef<Constant*> Vals = ArrayRef<Constant*>())
Bill Wendling1c7cc8a2013-01-31 23:16:25 +000043 : Context(C), Kind(Kind), Vals(Vals.begin(), Vals.end()) {}
Bill Wendling3a554ac2012-12-30 02:22:16 +000044
Bill Wendling9c2eba92013-01-31 20:59:05 +000045 LLVMContext &getContext() { return Context; }
46
Bill Wendling9ac69f92013-01-04 20:54:35 +000047 bool hasAttribute(Attribute::AttrKind A) const;
Bill Wendling73ea2de2012-10-08 21:47:17 +000048
Bill Wendlingc3c714b2013-01-29 20:37:10 +000049 Constant *getAttributeKind() const { return Kind; }
50 ArrayRef<Constant*> getAttributeValues() const { return Vals; }
51
Bill Wendling73ea2de2012-10-08 21:47:17 +000052 uint64_t getAlignment() const;
53 uint64_t getStackAlignment() const;
54
Bill Wendling9c2eba92013-01-31 20:59:05 +000055 /// \brief Equality and non-equality comparison operators.
Bill Wendling9ac69f92013-01-04 20:54:35 +000056 bool operator==(Attribute::AttrKind Kind) const;
57 bool operator!=(Attribute::AttrKind Kind) const;
Bill Wendlingb1d12612012-12-30 01:38:39 +000058
Bill Wendling9ac69f92013-01-04 20:54:35 +000059 bool operator==(StringRef Kind) const;
60 bool operator!=(StringRef Kind) const;
Bill Wendlingb1d12612012-12-30 01:38:39 +000061
Bill Wendling9c2eba92013-01-31 20:59:05 +000062 /// \brief Used when sorting the attributes.
Bill Wendlingd2e493b2013-01-24 00:06:56 +000063 bool operator<(const AttributeImpl &AI) const;
64
Bill Wendlinge38b8042012-09-26 21:07:29 +000065 void Profile(FoldingSetNodeID &ID) const {
Bill Wendlingc3c714b2013-01-29 20:37:10 +000066 Profile(ID, Kind, Vals);
Bill Wendlinge38b8042012-09-26 21:07:29 +000067 }
Bill Wendlingc3c714b2013-01-29 20:37:10 +000068 static void Profile(FoldingSetNodeID &ID, Constant *Kind,
Bill Wendlingd509a662013-01-29 00:34:06 +000069 ArrayRef<Constant*> Vals) {
Bill Wendlingc3c714b2013-01-29 20:37:10 +000070 ID.AddPointer(Kind);
Bill Wendlingd509a662013-01-29 00:34:06 +000071 for (unsigned I = 0, E = Vals.size(); I != E; ++I)
72 ID.AddPointer(Vals[I]);
73 }
74
Bill Wendling3f9fcd42013-02-01 00:48:14 +000075 // FIXME: Remove this!
Bill Wendlingd509a662013-01-29 00:34:06 +000076 static uint64_t getAttrMask(Attribute::AttrKind Val);
Bill Wendlinge38b8042012-09-26 21:07:29 +000077};
78
Bill Wendling66e978f2012-12-20 21:28:43 +000079//===----------------------------------------------------------------------===//
80/// \class
Bill Wendlingd2e493b2013-01-24 00:06:56 +000081/// \brief This class represents a group of attributes that apply to one
82/// element: function, return type, or parameter.
83class AttributeSetNode : public FoldingSetNode {
84 SmallVector<Attribute, 4> AttrList;
85
86 AttributeSetNode(ArrayRef<Attribute> Attrs)
87 : AttrList(Attrs.begin(), Attrs.end()) {}
Bill Wendling97b4f702013-01-27 21:38:03 +000088
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;
Bill Wendlingd2e493b2013-01-24 00:06:56 +000092public:
93 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
94
Bill Wendlingf2955aa2013-01-29 03:20:31 +000095 bool hasAttribute(Attribute::AttrKind Kind) const;
96 bool hasAttributes() const { return !AttrList.empty(); }
97
98 unsigned getAlignment() const;
99 unsigned getStackAlignment() const;
100 std::string getAsString() const;
101
Bill Wendlingd2e493b2013-01-24 00:06:56 +0000102 typedef SmallVectorImpl<Attribute>::iterator iterator;
103 typedef SmallVectorImpl<Attribute>::const_iterator const_iterator;
104
105 iterator begin() { return AttrList.begin(); }
106 iterator end() { return AttrList.end(); }
107
108 const_iterator begin() const { return AttrList.begin(); }
109 const_iterator end() const { return AttrList.end(); }
110
111 void Profile(FoldingSetNodeID &ID) const {
112 Profile(ID, AttrList);
113 }
114 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
115 for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
116 AttrList[I].Profile(ID);
117 }
118};
119
120//===----------------------------------------------------------------------===//
121/// \class
122/// \brief This class represents a set of attributes that apply to the function,
123/// return type, and parameters.
Bill Wendling6848e382012-12-19 22:42:22 +0000124class AttributeSetImpl : public FoldingSetNode {
Bill Wendling57625a42013-01-25 23:09:36 +0000125 friend class AttributeSet;
126
Bill Wendling9ac69f92013-01-04 20:54:35 +0000127 LLVMContext &Context;
Bill Wendling9ac69f92013-01-04 20:54:35 +0000128
Bill Wendlingeeebb132013-01-28 22:33:39 +0000129 typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
Bill Wendling9eb689c2013-01-28 00:21:34 +0000130 SmallVector<IndexAttrPair, 4> AttrNodes;
Bill Wendling39a4c802013-01-24 01:01:34 +0000131
Bill Wendling698e84f2012-12-30 10:32:01 +0000132 // AttributesSet is uniqued, these should not be publicly available.
Bill Wendling6848e382012-12-19 22:42:22 +0000133 void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
134 AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000135public:
Bill Wendlingec454542013-01-28 21:55:20 +0000136 AttributeSetImpl(LLVMContext &C,
Bill Wendlingeeebb132013-01-28 22:33:39 +0000137 ArrayRef<std::pair<unsigned, AttributeSetNode*> > attrs)
Bill Wendlingec454542013-01-28 21:55:20 +0000138 : Context(C), AttrNodes(attrs.begin(), attrs.end()) {}
Bill Wendling9ac69f92013-01-04 20:54:35 +0000139
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000140 /// \brief Get the context that created this AttributeSetImpl.
Bill Wendling9ac69f92013-01-04 20:54:35 +0000141 LLVMContext &getContext() { return Context; }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000142
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000143 /// \brief Return the number of attributes this AttributeSet contains.
144 unsigned getNumAttributes() const { return AttrNodes.size(); }
145
146 /// \brief Get the index of the given "slot" in the AttrNodes list. This index
147 /// is the index of the return, parameter, or function object that the
148 /// attributes are applied to, not the index into the AttrNodes list where the
149 /// attributes reside.
Bill Wendling9eb689c2013-01-28 00:21:34 +0000150 uint64_t getSlotIndex(unsigned Slot) const {
151 return AttrNodes[Slot].first;
152 }
Bill Wendling5c8b2df2013-01-27 21:32:11 +0000153
154 /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
155 /// \p Slot is an index into the AttrNodes list, not the index of the return /
156 /// parameter/ function which the attributes apply to.
Bill Wendling57625a42013-01-25 23:09:36 +0000157 AttributeSet getSlotAttributes(unsigned Slot) const {
158 // FIXME: This needs to use AttrNodes instead.
Bill Wendlingec454542013-01-28 21:55:20 +0000159 return AttributeSet::get(Context, AttrNodes[Slot]);
Bill Wendling57625a42013-01-25 23:09:36 +0000160 }
Bill Wendlingf86efb92012-11-20 05:09:20 +0000161
Bill Wendlingf2955aa2013-01-29 03:20:31 +0000162 /// \brief Retrieve the attribute set node for the given "slot" in the
163 /// AttrNode list.
164 AttributeSetNode *getSlotNode(unsigned Slot) const {
165 return AttrNodes[Slot].second;
166 }
167
Bill Wendling9eb689c2013-01-28 00:21:34 +0000168 typedef AttributeSetNode::iterator iterator;
169 typedef AttributeSetNode::const_iterator const_iterator;
170
171 iterator begin(unsigned Idx)
172 { return AttrNodes[Idx].second->begin(); }
173 iterator end(unsigned Idx)
174 { return AttrNodes[Idx].second->end(); }
175
176 const_iterator begin(unsigned Idx) const
177 { return AttrNodes[Idx].second->begin(); }
178 const_iterator end(unsigned Idx) const
179 { return AttrNodes[Idx].second->end(); }
180
Bill Wendlingf86efb92012-11-20 05:09:20 +0000181 void Profile(FoldingSetNodeID &ID) const {
Bill Wendlingec454542013-01-28 21:55:20 +0000182 Profile(ID, AttrNodes);
Bill Wendlingf86efb92012-11-20 05:09:20 +0000183 }
Bill Wendling9ac69f92013-01-04 20:54:35 +0000184 static void Profile(FoldingSetNodeID &ID,
Bill Wendlingeeebb132013-01-28 22:33:39 +0000185 ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
Bill Wendling39a4c802013-01-24 01:01:34 +0000186 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
187 ID.AddInteger(Nodes[i].first);
188 ID.AddPointer(Nodes[i].second);
189 }
190 }
Bill Wendling1f786a72013-01-27 23:41:29 +0000191
192 // FIXME: This atrocity is temporary.
193 uint64_t Raw(uint64_t Index) const;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000194};
195
Bill Wendlinge38b8042012-09-26 21:07:29 +0000196} // end llvm namespace
197
198#endif