AttributeImpl.h revision 14292a6be51ab57ff425ff263d4134fe46d082c4
1//===-- AttributeImpl.h - Attribute Internals -------------------*- C++ -*-===//
2//
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//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file defines various helper methods and classes used by
12/// LLVMContextImpl for creating and managing attributes.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_ATTRIBUTESIMPL_H
17#define LLVM_ATTRIBUTESIMPL_H
18
19#include "llvm/ADT/FoldingSet.h"
20#include "llvm/IR/Attributes.h"
21#include <string>
22
23namespace llvm {
24
25class Constant;
26class LLVMContext;
27
28//===----------------------------------------------------------------------===//
29/// \class
30/// \brief This class represents a single, uniqued attribute. That attribute
31/// could be a single enum, a tuple, or a string.
32class AttributeImpl : public FoldingSetNode {
33  LLVMContext &Context;
34  Constant *Kind;
35  SmallVector<Constant*, 0> Vals;
36
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;
40public:
41  AttributeImpl(LLVMContext &C, Constant *Kind)
42    : Context(C), Kind(Kind) {}
43  explicit AttributeImpl(LLVMContext &C, Attribute::AttrKind data);
44  AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
45                ArrayRef<Constant*> values);
46  AttributeImpl(LLVMContext &C, StringRef data);
47
48  LLVMContext &getContext() { return Context; }
49
50  bool hasAttribute(Attribute::AttrKind A) const;
51
52  Constant *getAttributeKind() const { return Kind; }
53  ArrayRef<Constant*> getAttributeValues() const { return Vals; }
54
55  uint64_t getAlignment() const;
56  uint64_t getStackAlignment() const;
57
58  /// \brief Equality and non-equality comparison operators.
59  bool operator==(Attribute::AttrKind Kind) const;
60  bool operator!=(Attribute::AttrKind Kind) const;
61
62  bool operator==(StringRef Kind) const;
63  bool operator!=(StringRef Kind) const;
64
65  /// \brief Used when sorting the attributes.
66  bool operator<(const AttributeImpl &AI) const;
67
68  void Profile(FoldingSetNodeID &ID) const {
69    Profile(ID, Kind, Vals);
70  }
71  static void Profile(FoldingSetNodeID &ID, Constant *Kind,
72                      ArrayRef<Constant*> Vals) {
73    ID.AddPointer(Kind);
74    for (unsigned I = 0, E = Vals.size(); I != E; ++I)
75      ID.AddPointer(Vals[I]);
76  }
77
78  // FIXME: Remove these!
79  uint64_t Raw() const;
80  static uint64_t getAttrMask(Attribute::AttrKind Val);
81};
82
83//===----------------------------------------------------------------------===//
84/// \class
85/// \brief This class represents a group of attributes that apply to one
86/// element: function, return type, or parameter.
87class AttributeSetNode : public FoldingSetNode {
88  SmallVector<Attribute, 4> AttrList;
89
90  AttributeSetNode(ArrayRef<Attribute> Attrs)
91    : AttrList(Attrs.begin(), Attrs.end()) {}
92
93  // AttributesSetNode is uniqued, these should not be publicly available.
94  void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
95  AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION;
96public:
97  static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
98
99  bool hasAttribute(Attribute::AttrKind Kind) const;
100  bool hasAttributes() const { return !AttrList.empty(); }
101
102  unsigned getAlignment() const;
103  unsigned getStackAlignment() const;
104  std::string getAsString() const;
105
106  typedef SmallVectorImpl<Attribute>::iterator       iterator;
107  typedef SmallVectorImpl<Attribute>::const_iterator const_iterator;
108
109  iterator begin() { return AttrList.begin(); }
110  iterator end()   { return AttrList.end(); }
111
112  const_iterator begin() const { return AttrList.begin(); }
113  const_iterator end() const   { return AttrList.end(); }
114
115  void Profile(FoldingSetNodeID &ID) const {
116    Profile(ID, AttrList);
117  }
118  static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
119    for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
120      AttrList[I].Profile(ID);
121  }
122};
123
124//===----------------------------------------------------------------------===//
125/// \class
126/// \brief This class represents a set of attributes that apply to the function,
127/// return type, and parameters.
128class AttributeSetImpl : public FoldingSetNode {
129  friend class AttributeSet;
130
131  LLVMContext &Context;
132
133  typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
134  SmallVector<IndexAttrPair, 4> AttrNodes;
135
136  // AttributesSet is uniqued, these should not be publicly available.
137  void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
138  AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
139public:
140  AttributeSetImpl(LLVMContext &C,
141                   ArrayRef<std::pair<unsigned, AttributeSetNode*> > attrs)
142    : Context(C), AttrNodes(attrs.begin(), attrs.end()) {}
143
144  /// \brief Get the context that created this AttributeSetImpl.
145  LLVMContext &getContext() { return Context; }
146
147  /// \brief Return the number of attributes this AttributeSet contains.
148  unsigned getNumAttributes() const { return AttrNodes.size(); }
149
150  /// \brief Get the index of the given "slot" in the AttrNodes list. This index
151  /// is the index of the return, parameter, or function object that the
152  /// attributes are applied to, not the index into the AttrNodes list where the
153  /// attributes reside.
154  uint64_t getSlotIndex(unsigned Slot) const {
155    return AttrNodes[Slot].first;
156  }
157
158  /// \brief Retrieve the attributes for the given "slot" in the AttrNode list.
159  /// \p Slot is an index into the AttrNodes list, not the index of the return /
160  /// parameter/ function which the attributes apply to.
161  AttributeSet getSlotAttributes(unsigned Slot) const {
162    // FIXME: This needs to use AttrNodes instead.
163    return AttributeSet::get(Context, AttrNodes[Slot]);
164  }
165
166  /// \brief Retrieve the attribute set node for the given "slot" in the
167  /// AttrNode list.
168  AttributeSetNode *getSlotNode(unsigned Slot) const {
169    return AttrNodes[Slot].second;
170  }
171
172  typedef AttributeSetNode::iterator       iterator;
173  typedef AttributeSetNode::const_iterator const_iterator;
174
175  iterator begin(unsigned Idx)
176    { return AttrNodes[Idx].second->begin(); }
177  iterator end(unsigned Idx)
178    { return AttrNodes[Idx].second->end(); }
179
180  const_iterator begin(unsigned Idx) const
181    { return AttrNodes[Idx].second->begin(); }
182  const_iterator end(unsigned Idx) const
183    { return AttrNodes[Idx].second->end(); }
184
185  void Profile(FoldingSetNodeID &ID) const {
186    Profile(ID, AttrNodes);
187  }
188  static void Profile(FoldingSetNodeID &ID,
189                      ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
190    for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
191      ID.AddInteger(Nodes[i].first);
192      ID.AddPointer(Nodes[i].second);
193    }
194  }
195
196  // FIXME: This atrocity is temporary.
197  uint64_t Raw(uint64_t Index) const;
198};
199
200} // end llvm namespace
201
202#endif
203