AttributeImpl.h revision 47990e974355dd58d1f320f252e1a7ed3a79e034
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
22namespace llvm {
23
24class Constant;
25class LLVMContext;
26
27//===----------------------------------------------------------------------===//
28/// \class
29/// \brief This class represents a single, uniqued attribute. That attribute
30/// could be a single enum, a tuple, or a string.
31class AttributeImpl : public FoldingSetNode {
32  Constant *Data;
33  SmallVector<Constant*, 0> Vals;
34public:
35  explicit AttributeImpl(LLVMContext &C, uint64_t data);
36  explicit AttributeImpl(LLVMContext &C, Attribute::AttrKind data);
37  AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
38                ArrayRef<Constant*> values);
39  AttributeImpl(LLVMContext &C, StringRef data);
40
41  ArrayRef<Constant*> getValues() const {
42    return Vals;
43  }
44
45  bool contains(Attribute::AttrKind Kind) const;
46  bool contains(StringRef Kind) const;
47
48  bool hasAttribute(Attribute::AttrKind A) const;
49
50  bool hasAttributes() const;
51  bool hasAttributes(const Attribute &A) const;
52
53  uint64_t getAlignment() const;
54  uint64_t getStackAlignment() const;
55
56  bool operator==(Attribute::AttrKind Kind) const {
57    return contains(Kind);
58  }
59  bool operator!=(Attribute::AttrKind Kind) const {
60    return !contains(Kind);
61  }
62
63  bool operator==(StringRef Kind) const {
64    return contains(Kind);
65  }
66  bool operator!=(StringRef Kind) const {
67    return !contains(Kind);
68  }
69
70  uint64_t getBitMask() const;         // FIXME: Remove.
71
72  static uint64_t getAttrMask(Attribute::AttrKind Val);
73
74  void Profile(FoldingSetNodeID &ID) const {
75    Profile(ID, Data, Vals);
76  }
77  static void Profile(FoldingSetNodeID &ID, Constant *Data,
78                      ArrayRef<Constant*> Vals) {
79    ID.AddPointer(Data);
80    for (ArrayRef<Constant*>::iterator I = Vals.begin(), E = Vals.end();
81         I != E; ++I)
82      ID.AddPointer(*I);
83  }
84};
85
86//===----------------------------------------------------------------------===//
87/// \class
88/// \brief This class represents a set of attributes.
89class AttributeSetImpl : public FoldingSetNode {
90  // AttributesSet is uniqued, these should not be publicly available.
91  void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
92  AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
93public:
94  LLVMContext &Context;
95  SmallVector<AttributeWithIndex, 4> Attrs;
96
97  AttributeSetImpl(LLVMContext &C, ArrayRef<AttributeWithIndex> attrs)
98    : Context(C), Attrs(attrs.begin(), attrs.end()) {}
99
100  void Profile(FoldingSetNodeID &ID) const {
101    Profile(ID, Attrs);
102  }
103  static void Profile(FoldingSetNodeID &ID, ArrayRef<AttributeWithIndex> Attrs){
104    for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
105      ID.AddInteger(Attrs[i].Attrs.getBitMask());
106      ID.AddInteger(Attrs[i].Index);
107    }
108  }
109};
110
111} // end llvm namespace
112
113#endif
114