1 //===- AttributeImpl.h - Attribute Internals --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file defines various helper methods and classes used by
12 /// LLVMContextImpl for creating and managing attributes.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_LIB_IR_ATTRIBUTEIMPL_H
17 #define LLVM_LIB_IR_ATTRIBUTEIMPL_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/IR/Attributes.h"
23 #include "llvm/Support/TrailingObjects.h"
34 //===----------------------------------------------------------------------===//
36 /// \brief This class represents a single, uniqued attribute. That attribute
37 /// could be a single enum, a tuple, or a string.
38 class AttributeImpl : public FoldingSetNode {
39 unsigned char KindID; ///< Holds the AttrEntryKind of the attribute
48 AttributeImpl(AttrEntryKind KindID) : KindID(KindID) {}
51 // AttributesImpl is uniqued, these should not be available.
52 AttributeImpl(const AttributeImpl &) = delete;
53 AttributeImpl &operator=(const AttributeImpl &) = delete;
55 virtual ~AttributeImpl();
57 bool isEnumAttribute() const { return KindID == EnumAttrEntry; }
58 bool isIntAttribute() const { return KindID == IntAttrEntry; }
59 bool isStringAttribute() const { return KindID == StringAttrEntry; }
61 bool hasAttribute(Attribute::AttrKind A) const;
62 bool hasAttribute(StringRef Kind) const;
64 Attribute::AttrKind getKindAsEnum() const;
65 uint64_t getValueAsInt() const;
67 StringRef getKindAsString() const;
68 StringRef getValueAsString() const;
70 /// \brief Used when sorting the attributes.
71 bool operator<(const AttributeImpl &AI) const;
73 void Profile(FoldingSetNodeID &ID) const {
74 if (isEnumAttribute())
75 Profile(ID, getKindAsEnum(), 0);
76 else if (isIntAttribute())
77 Profile(ID, getKindAsEnum(), getValueAsInt());
79 Profile(ID, getKindAsString(), getValueAsString());
82 static void Profile(FoldingSetNodeID &ID, Attribute::AttrKind Kind,
85 if (Val) ID.AddInteger(Val);
88 static void Profile(FoldingSetNodeID &ID, StringRef Kind, StringRef Values) {
90 if (!Values.empty()) ID.AddString(Values);
94 //===----------------------------------------------------------------------===//
96 /// \brief A set of classes that contain the value of the
97 /// attribute object. There are three main categories: enum attribute entries,
98 /// represented by Attribute::AttrKind; alignment attribute entries; and string
99 /// attribute enties, which are for target-dependent attributes.
101 class EnumAttributeImpl : public AttributeImpl {
102 virtual void anchor();
103 Attribute::AttrKind Kind;
106 EnumAttributeImpl(AttrEntryKind ID, Attribute::AttrKind Kind)
107 : AttributeImpl(ID), Kind(Kind) {}
110 EnumAttributeImpl(Attribute::AttrKind Kind)
111 : AttributeImpl(EnumAttrEntry), Kind(Kind) {}
113 Attribute::AttrKind getEnumKind() const { return Kind; }
116 class IntAttributeImpl : public EnumAttributeImpl {
119 void anchor() override;
122 IntAttributeImpl(Attribute::AttrKind Kind, uint64_t Val)
123 : EnumAttributeImpl(IntAttrEntry, Kind), Val(Val) {
124 assert((Kind == Attribute::Alignment || Kind == Attribute::StackAlignment ||
125 Kind == Attribute::Dereferenceable ||
126 Kind == Attribute::DereferenceableOrNull ||
127 Kind == Attribute::AllocSize) &&
128 "Wrong kind for int attribute!");
131 uint64_t getValue() const { return Val; }
134 class StringAttributeImpl : public AttributeImpl {
135 virtual void anchor();
140 StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
141 : AttributeImpl(StringAttrEntry), Kind(Kind), Val(Val) {}
143 StringRef getStringKind() const { return Kind; }
144 StringRef getStringValue() const { return Val; }
147 //===----------------------------------------------------------------------===//
149 /// \brief This class represents a group of attributes that apply to one
150 /// element: function, return type, or parameter.
151 class AttributeSetNode final
152 : public FoldingSetNode,
153 private TrailingObjects<AttributeSetNode, Attribute> {
154 friend TrailingObjects;
156 /// Bitset with a bit for each available attribute Attribute::AttrKind.
157 uint64_t AvailableAttrs;
158 unsigned NumAttrs; ///< Number of attributes in this node.
160 AttributeSetNode(ArrayRef<Attribute> Attrs);
163 // AttributesSetNode is uniqued, these should not be available.
164 AttributeSetNode(const AttributeSetNode &) = delete;
165 AttributeSetNode &operator=(const AttributeSetNode &) = delete;
167 void operator delete(void *p) { ::operator delete(p); }
169 static AttributeSetNode *get(LLVMContext &C, const AttrBuilder &B);
171 static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
173 /// \brief Return the number of attributes this AttributeList contains.
174 unsigned getNumAttributes() const { return NumAttrs; }
176 bool hasAttribute(Attribute::AttrKind Kind) const {
177 return AvailableAttrs & ((uint64_t)1) << Kind;
179 bool hasAttribute(StringRef Kind) const;
180 bool hasAttributes() const { return NumAttrs != 0; }
182 Attribute getAttribute(Attribute::AttrKind Kind) const;
183 Attribute getAttribute(StringRef Kind) const;
185 unsigned getAlignment() const;
186 unsigned getStackAlignment() const;
187 uint64_t getDereferenceableBytes() const;
188 uint64_t getDereferenceableOrNullBytes() const;
189 std::pair<unsigned, Optional<unsigned>> getAllocSizeArgs() const;
190 std::string getAsString(bool InAttrGrp) const;
192 using iterator = const Attribute *;
194 iterator begin() const { return getTrailingObjects<Attribute>(); }
195 iterator end() const { return begin() + NumAttrs; }
197 void Profile(FoldingSetNodeID &ID) const {
198 Profile(ID, makeArrayRef(begin(), end()));
201 static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
202 for (const auto &Attr : AttrList)
207 using IndexAttrPair = std::pair<unsigned, AttributeSet>;
209 //===----------------------------------------------------------------------===//
211 /// \brief This class represents a set of attributes that apply to the function,
212 /// return type, and parameters.
213 class AttributeListImpl final
214 : public FoldingSetNode,
215 private TrailingObjects<AttributeListImpl, AttributeSet> {
216 friend class AttributeList;
217 friend TrailingObjects;
220 /// Bitset with a bit for each available attribute Attribute::AttrKind.
221 uint64_t AvailableFunctionAttrs;
222 LLVMContext &Context;
223 unsigned NumAttrSets; ///< Number of entries in this set.
225 // Helper fn for TrailingObjects class.
226 size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
229 AttributeListImpl(LLVMContext &C, ArrayRef<AttributeSet> Sets);
231 // AttributesSetImpt is uniqued, these should not be available.
232 AttributeListImpl(const AttributeListImpl &) = delete;
233 AttributeListImpl &operator=(const AttributeListImpl &) = delete;
235 void operator delete(void *p) { ::operator delete(p); }
237 /// \brief Get the context that created this AttributeListImpl.
238 LLVMContext &getContext() { return Context; }
240 /// \brief Return true if the AttributeSet or the FunctionIndex has an
241 /// enum attribute of the given kind.
242 bool hasFnAttribute(Attribute::AttrKind Kind) const {
243 return AvailableFunctionAttrs & ((uint64_t)1) << Kind;
246 typedef const AttributeSet *iterator;
247 iterator begin() const { return getTrailingObjects<AttributeSet>(); }
248 iterator end() const { return begin() + NumAttrSets; }
250 void Profile(FoldingSetNodeID &ID) const;
251 static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeSet> Nodes);
256 } // end namespace llvm
258 #endif // LLVM_LIB_IR_ATTRIBUTEIMPL_H