b4c788fa9d4a910d88978ee78887e4c51371409e
[oota-llvm.git] / lib / IR / AttributeImpl.h
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
22 namespace llvm {
23
24 class Constant;
25 class 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.
31 class AttributeImpl : public FoldingSetNode {
32   LLVMContext &Context;
33   Constant *Data;
34   SmallVector<Constant*, 0> Vals;
35 public:
36   explicit AttributeImpl(LLVMContext &C, uint64_t data);
37   explicit AttributeImpl(LLVMContext &C, Attribute::AttrKind data);
38   AttributeImpl(LLVMContext &C, Attribute::AttrKind data,
39                 ArrayRef<Constant*> values);
40   AttributeImpl(LLVMContext &C, StringRef data);
41
42   LLVMContext &getContext() { return Context; }
43
44   ArrayRef<Constant*> getValues() const { return Vals; }
45
46   bool hasAttribute(Attribute::AttrKind A) const;
47
48   bool hasAttributes() const;
49
50   uint64_t getAlignment() const;
51   uint64_t getStackAlignment() const;
52
53   bool operator==(Attribute::AttrKind Kind) const;
54   bool operator!=(Attribute::AttrKind Kind) const;
55
56   bool operator==(StringRef Kind) const;
57   bool operator!=(StringRef Kind) const;
58
59   bool operator<(const AttributeImpl &AI) const;
60
61   uint64_t Raw() const;         // FIXME: Remove.
62
63   static uint64_t getAttrMask(Attribute::AttrKind Val);
64
65   void Profile(FoldingSetNodeID &ID) const {
66     Profile(ID, Data, Vals);
67   }
68   static void Profile(FoldingSetNodeID &ID, Constant *Data,
69                       ArrayRef<Constant*> Vals);
70 };
71
72 //===----------------------------------------------------------------------===//
73 /// \class
74 /// \brief This class represents a group of attributes that apply to one
75 /// element: function, return type, or parameter.
76 class AttributeSetNode : public FoldingSetNode {
77   SmallVector<Attribute, 4> AttrList;
78
79   AttributeSetNode(ArrayRef<Attribute> Attrs)
80     : AttrList(Attrs.begin(), Attrs.end()) {}
81 public:
82   static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);
83
84   typedef SmallVectorImpl<Attribute>::iterator       iterator;
85   typedef SmallVectorImpl<Attribute>::const_iterator const_iterator;
86
87   iterator begin() { return AttrList.begin(); }
88   iterator end()   { return AttrList.end(); }
89
90   const_iterator begin() const { return AttrList.begin(); }
91   const_iterator end() const   { return AttrList.end(); }
92
93   void Profile(FoldingSetNodeID &ID) const {
94     Profile(ID, AttrList);
95   }
96   static void Profile(FoldingSetNodeID &ID, ArrayRef<Attribute> AttrList) {
97     for (unsigned I = 0, E = AttrList.size(); I != E; ++I)
98       AttrList[I].Profile(ID);
99   }
100 };
101
102 //===----------------------------------------------------------------------===//
103 /// \class
104 /// \brief This class represents a set of attributes that apply to the function,
105 /// return type, and parameters.
106 class AttributeSetImpl : public FoldingSetNode {
107   friend class AttributeSet;
108
109   LLVMContext &Context;
110   SmallVector<AttributeWithIndex, 4> AttrList;
111
112   SmallVector<std::pair<uint64_t, AttributeSetNode*>, 4> AttrNodes;
113
114   // AttributesSet is uniqued, these should not be publicly available.
115   void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
116   AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION;
117 public:
118   AttributeSetImpl(LLVMContext &C, ArrayRef<AttributeWithIndex> attrs);
119   AttributeSetImpl(LLVMContext &C,
120                    ArrayRef<std::pair<uint64_t, AttributeSetNode*> > attrs);
121
122   LLVMContext &getContext() { return Context; }
123   ArrayRef<AttributeWithIndex> getAttributes() const { return AttrList; }
124   unsigned getNumAttributes() const { return AttrList.size(); }
125   unsigned getSlotIndex(unsigned Slot) const {
126     // FIXME: This needs to use AttrNodes instead.
127     return AttrList[Slot].Index;
128   }
129   AttributeSet getSlotAttributes(unsigned Slot) const {
130     // FIXME: This needs to use AttrNodes instead.
131     return AttributeSet::get(Context, AttrList[Slot]);
132   }
133
134   void Profile(FoldingSetNodeID &ID) const {
135     Profile(ID, AttrList);
136   }
137   static void Profile(FoldingSetNodeID &ID,
138                       ArrayRef<AttributeWithIndex> AttrList) {
139     for (unsigned i = 0, e = AttrList.size(); i != e; ++i) {
140       ID.AddInteger(AttrList[i].Index);
141       ID.AddInteger(AttrList[i].Attrs.Raw());
142     }
143   }
144
145   static void Profile(FoldingSetNodeID &ID,
146                       ArrayRef<std::pair<uint64_t, AttributeSetNode*> > Nodes) {
147     for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
148       ID.AddInteger(Nodes[i].first);
149       ID.AddPointer(Nodes[i].second);
150     }
151   }
152 };
153
154 } // end llvm namespace
155
156 #endif