blob: 23ce3713c20bceeeae3a4d070db8caae9e232b02 [file] [log] [blame]
Amaury Sechet17b67cd2016-07-21 04:25:06 +00001//===-- AttributeSetNode.h - AttributeSet Internal Node ---------*- 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 the node class used internally by AttributeSet.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_IR_ATTRIBUTESETNODE_H
16#define LLVM_IR_ATTRIBUTESETNODE_H
17
Eugene Zelenko9408c612016-12-07 22:06:02 +000018#include "llvm/ADT/ArrayRef.h"
Amaury Sechet17b67cd2016-07-21 04:25:06 +000019#include "llvm/ADT/FoldingSet.h"
Eugene Zelenko9408c612016-12-07 22:06:02 +000020#include "llvm/ADT/Optional.h"
21#include "llvm/ADT/StringRef.h"
Amaury Sechet17b67cd2016-07-21 04:25:06 +000022#include "llvm/IR/Attributes.h"
23#include "llvm/Support/TrailingObjects.h"
Eugene Zelenko9408c612016-12-07 22:06:02 +000024#include <algorithm>
Amaury Secheted7b7d72016-07-21 04:31:38 +000025#include <climits>
Eugene Zelenko9408c612016-12-07 22:06:02 +000026#include <cstdint>
27#include <string>
28#include <utility>
Amaury Sechet17b67cd2016-07-21 04:25:06 +000029
30namespace llvm {
31
32//===----------------------------------------------------------------------===//
33/// \class
34/// \brief This class represents a group of attributes that apply to one
35/// element: function, return type, or parameter.
36class AttributeSetNode final
37 : public FoldingSetNode,
38 private TrailingObjects<AttributeSetNode, Attribute> {
39 friend TrailingObjects;
40
41 unsigned NumAttrs; ///< Number of attributes in this node.
42 /// Bitset with a bit for each available attribute Attribute::AttrKind.
43 uint64_t AvailableAttrs;
44
45 AttributeSetNode(ArrayRef<Attribute> Attrs)
46 : NumAttrs(Attrs.size()), AvailableAttrs(0) {
47 static_assert(Attribute::EndAttrKinds <= sizeof(AvailableAttrs) * CHAR_BIT,
48 "Too many attributes for AvailableAttrs");
49 // There's memory after the node where we can store the entries in.
50 std::copy(Attrs.begin(), Attrs.end(), getTrailingObjects<Attribute>());
51
52 for (Attribute I : *this) {
53 if (!I.isStringAttribute()) {
54 AvailableAttrs |= ((uint64_t)1) << I.getKindAsEnum();
55 }
56 }
57 }
58
Amaury Sechet17b67cd2016-07-21 04:25:06 +000059public:
Eugene Zelenko9408c612016-12-07 22:06:02 +000060 // AttributesSetNode is uniqued, these should not be available.
61 AttributeSetNode(const AttributeSetNode &) = delete;
62 AttributeSetNode &operator=(const AttributeSetNode &) = delete;
63
Amaury Sechet17b67cd2016-07-21 04:25:06 +000064 void operator delete(void *p) { ::operator delete(p); }
65
66 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
67
68 static AttributeSetNode *get(AttributeSet AS, unsigned Index) {
69 return AS.getAttributes(Index);
70 }
71
72 /// \brief Return the number of attributes this AttributeSet contains.
73 unsigned getNumAttributes() const { return NumAttrs; }
74
75 bool hasAttribute(Attribute::AttrKind Kind) const {
76 return AvailableAttrs & ((uint64_t)1) << Kind;
77 }
78 bool hasAttribute(StringRef Kind) const;
79 bool hasAttributes() const { return NumAttrs != 0; }
80
81 Attribute getAttribute(Attribute::AttrKind Kind) const;
82 Attribute getAttribute(StringRef Kind) const;
83
84 unsigned getAlignment() const;
85 unsigned getStackAlignment() const;
86 uint64_t getDereferenceableBytes() const;
87 uint64_t getDereferenceableOrNullBytes() const;
88 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
89 std::string getAsString(bool InAttrGrp) const;
90
91 typedef const Attribute *iterator;
92 iterator begin() const { return getTrailingObjects<Attribute>(); }
93 iterator end() const { return begin() + NumAttrs; }
94
95 void Profile(FoldingSetNodeID &ID) const {
96 Profile(ID, makeArrayRef(begin(), end()));
97 }
98 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
Eugene Zelenko9408c612016-12-07 22:06:02 +000099 for (const auto &Attr : AttrList)
100 Attr.Profile(ID);
Amaury Sechet17b67cd2016-07-21 04:25:06 +0000101 }
102};
103
Eugene Zelenko9408c612016-12-07 22:06:02 +0000104} // end namespace llvm
Amaury Sechet17b67cd2016-07-21 04:25:06 +0000105
Eugene Zelenko9408c612016-12-07 22:06:02 +0000106#endif // LLVM_IR_ATTRIBUTESETNODE_H