AttributeImpl.h revision 7beee2876795098d2e2f31ecc2ca29fa7640a8eb
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                ArrayRef<Constant*> Vals = ArrayRef<Constant*>())
43    : Context(C), Kind(Kind), Vals(Vals.begin(), Vals.end()) {}
44
45  LLVMContext &getContext() { return Context; }
46
47  bool hasAttribute(Attribute::AttrKind A) const;
48
49  Constant *getAttributeKind() const { return Kind; }
50  ArrayRef<Constant*> getAttributeValues() const { return Vals; }
51
52  uint64_t getAlignment() const;
53  uint64_t getStackAlignment() const;
54
55  /// \brief Equality and non-equality comparison operators.
56  bool operator==(Attribute::AttrKind Kind) const;
57  bool operator!=(Attribute::AttrKind Kind) const;
58
59  bool operator==(StringRef Kind) const;
60  bool operator!=(StringRef Kind) const;
61
62  /// \brief Used when sorting the attributes.
63  bool operator<(const AttributeImpl &AI) const;
64
65  void Profile(FoldingSetNodeID &ID) const {
66    Profile(ID, Kind, Vals);
67  }
68  static void Profile(FoldingSetNodeID &ID, Constant *Kind,
69                      ArrayRef<Constant*> Vals) {
70    ID.AddPointer(Kind);
71    for (unsigned I = 0, E = Vals.size(); I != E; ++I)
72      ID.AddPointer(Vals[I]);
73  }
74
75  // FIXME: Remove this!
76  static uint64_t getAttrMask(Attribute::AttrKind Val);
77};
78
79//===----------------------------------------------------------------------===//
80/// \class
81/// \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()) {}
88
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;
92public:
93  static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
94
95  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
102  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.
124class AttributeSetImpl : public FoldingSetNode {
125  friend class AttributeSet;
126
127  LLVMContext &Context;
128
129  typedef std::pair<unsigned, AttributeSetNode*> IndexAttrPair;
130  SmallVector<IndexAttrPair, 4> AttrNodes;
131
132  // AttributesSet is uniqued, these should not be publicly available.
133  void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
134  AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
135public:
136  AttributeSetImpl(LLVMContext &C,
137                   ArrayRef<std::pair<unsigned, AttributeSetNode*> > attrs)
138    : Context(C), AttrNodes(attrs.begin(), attrs.end()) {}
139
140  /// \brief Get the context that created this AttributeSetImpl.
141  LLVMContext &getContext() { return Context; }
142
143  /// \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.
150  uint64_t getSlotIndex(unsigned Slot) const {
151    return AttrNodes[Slot].first;
152  }
153
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.
157  AttributeSet getSlotAttributes(unsigned Slot) const {
158    // FIXME: This needs to use AttrNodes instead.
159    return AttributeSet::get(Context, AttrNodes[Slot]);
160  }
161
162  /// \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
168  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
181  void Profile(FoldingSetNodeID &ID) const {
182    Profile(ID, AttrNodes);
183  }
184  static void Profile(FoldingSetNodeID &ID,
185                      ArrayRef<std::pair<unsigned, AttributeSetNode*> > Nodes) {
186    for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
187      ID.AddInteger(Nodes[i].first);
188      ID.AddPointer(Nodes[i].second);
189    }
190  }
191
192  // FIXME: This atrocity is temporary.
193  uint64_t Raw(uint64_t Index) const;
194};
195
196} // end llvm namespace
197
198#endif
199