]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/tools/clang/include/clang/AST/Attr.h
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / llvm / tools / clang / include / clang / AST / Attr.h
1 //===--- Attr.h - Classes for representing expressions ----------*- 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 //  This file defines the Attr interface and subclasses.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_AST_ATTR_H
15 #define LLVM_CLANG_AST_ATTR_H
16
17 #include "clang/Basic/LLVM.h"
18 #include "clang/Basic/AttrKinds.h"
19 #include "clang/AST/Type.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "clang/Basic/VersionTuple.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/StringSwitch.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include <cassert>
27 #include <cstring>
28 #include <algorithm>
29
30 namespace clang {
31   class ASTContext;
32   class IdentifierInfo;
33   class ObjCInterfaceDecl;
34   class Expr;
35   class QualType;
36   class FunctionDecl;
37   class TypeSourceInfo;
38 }
39
40 // Defined in ASTContext.h
41 void *operator new(size_t Bytes, const clang::ASTContext &C,
42                    size_t Alignment = 16) throw ();
43 // FIXME: Being forced to not have a default argument here due to redeclaration
44 //        rules on default arguments sucks
45 void *operator new[](size_t Bytes, const clang::ASTContext &C,
46                      size_t Alignment) throw ();
47
48 // It is good practice to pair new/delete operators.  Also, MSVC gives many
49 // warnings if a matching delete overload is not declared, even though the
50 // throw() spec guarantees it will not be implicitly called.
51 void operator delete(void *Ptr, const clang::ASTContext &C, size_t)
52               throw ();
53 void operator delete[](void *Ptr, const clang::ASTContext &C, size_t)
54               throw ();
55
56 namespace clang {
57
58 /// Attr - This represents one attribute.
59 class Attr {
60 private:
61   SourceRange Range;
62   unsigned AttrKind : 16;
63
64 protected:
65   bool Inherited : 1;
66
67   virtual ~Attr();
68   
69   void* operator new(size_t bytes) throw() {
70     llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
71   }
72   void operator delete(void* data) throw() {
73     llvm_unreachable("Attrs cannot be released with regular 'delete'.");
74   }
75
76 public:
77   // Forward so that the regular new and delete do not hide global ones.
78   void* operator new(size_t Bytes, ASTContext &C,
79                      size_t Alignment = 16) throw() {
80     return ::operator new(Bytes, C, Alignment);
81   }
82   void operator delete(void *Ptr, ASTContext &C,
83                        size_t Alignment) throw() {
84     return ::operator delete(Ptr, C, Alignment);
85   }
86
87 protected:
88   Attr(attr::Kind AK, SourceRange R)
89     : Range(R), AttrKind(AK), Inherited(false) {}
90
91 public:
92
93   attr::Kind getKind() const {
94     return static_cast<attr::Kind>(AttrKind);
95   }
96
97   SourceLocation getLocation() const { return Range.getBegin(); }
98   SourceRange getRange() const { return Range; }
99   void setRange(SourceRange R) { Range = R; }
100
101   bool isInherited() const { return Inherited; }
102
103   // Clone this attribute.
104   virtual Attr* clone(ASTContext &C) const = 0;
105
106   // Implement isa/cast/dyncast/etc.
107   static bool classof(const Attr *) { return true; }
108 };
109
110 class InheritableAttr : public Attr {
111 protected:
112   InheritableAttr(attr::Kind AK, SourceRange R)
113     : Attr(AK, R) {}
114
115 public:
116   void setInherited(bool I) { Inherited = I; }
117
118   // Implement isa/cast/dyncast/etc.
119   static bool classof(const Attr *A) {
120     return A->getKind() <= attr::LAST_INHERITABLE;
121   }
122   static bool classof(const InheritableAttr *) { return true; }
123 };
124
125 class InheritableParamAttr : public InheritableAttr {
126 protected:
127   InheritableParamAttr(attr::Kind AK, SourceRange R)
128     : InheritableAttr(AK, R) {}
129
130 public:
131   // Implement isa/cast/dyncast/etc.
132   static bool classof(const Attr *A) {
133     return A->getKind() <= attr::LAST_INHERITABLE_PARAM;
134   }
135   static bool classof(const InheritableParamAttr *) { return true; }
136 };
137
138 #include "clang/AST/Attrs.inc"
139
140 /// AttrVec - A vector of Attr, which is how they are stored on the AST.
141 typedef SmallVector<Attr*, 2> AttrVec;
142 typedef SmallVector<const Attr*, 2> ConstAttrVec;
143
144 /// DestroyAttrs - Destroy the contents of an AttrVec.
145 inline void DestroyAttrs (AttrVec& V, ASTContext &C) {
146 }
147
148 /// specific_attr_iterator - Iterates over a subrange of an AttrVec, only
149 /// providing attributes that are of a specifc type.
150 template <typename SpecificAttr>
151 class specific_attr_iterator {
152   /// Current - The current, underlying iterator.
153   /// In order to ensure we don't dereference an invalid iterator unless
154   /// specifically requested, we don't necessarily advance this all the
155   /// way. Instead, we advance it when an operation is requested; if the
156   /// operation is acting on what should be a past-the-end iterator,
157   /// then we offer no guarantees, but this way we do not dererence a
158   /// past-the-end iterator when we move to a past-the-end position.
159   mutable AttrVec::const_iterator Current;
160
161   void AdvanceToNext() const {
162     while (!isa<SpecificAttr>(*Current))
163       ++Current;
164   }
165
166   void AdvanceToNext(AttrVec::const_iterator I) const {
167     while (Current != I && !isa<SpecificAttr>(*Current))
168       ++Current;
169   }
170
171 public:
172   typedef SpecificAttr*             value_type;
173   typedef SpecificAttr*             reference;
174   typedef SpecificAttr*             pointer;
175   typedef std::forward_iterator_tag iterator_category;
176   typedef std::ptrdiff_t            difference_type;
177
178   specific_attr_iterator() : Current() { }
179   explicit specific_attr_iterator(AttrVec::const_iterator i) : Current(i) { }
180
181   reference operator*() const {
182     AdvanceToNext();
183     return cast<SpecificAttr>(*Current);
184   }
185   pointer operator->() const {
186     AdvanceToNext();
187     return cast<SpecificAttr>(*Current);
188   }
189
190   specific_attr_iterator& operator++() {
191     ++Current;
192     return *this;
193   }
194   specific_attr_iterator operator++(int) {
195     specific_attr_iterator Tmp(*this);
196     ++(*this);
197     return Tmp;
198   }
199
200   friend bool operator==(specific_attr_iterator Left,
201                          specific_attr_iterator Right) {
202     if (Left.Current < Right.Current)
203       Left.AdvanceToNext(Right.Current); 
204     else
205       Right.AdvanceToNext(Left.Current);
206     return Left.Current == Right.Current;
207   }
208   friend bool operator!=(specific_attr_iterator Left,
209                          specific_attr_iterator Right) {
210     return !(Left == Right);
211   }
212 };
213
214 template <typename T>
215 inline specific_attr_iterator<T> specific_attr_begin(const AttrVec& vec) {
216   return specific_attr_iterator<T>(vec.begin());
217 }
218 template <typename T>
219 inline specific_attr_iterator<T> specific_attr_end(const AttrVec& vec) {
220   return specific_attr_iterator<T>(vec.end());
221 }
222
223 template <typename T>
224 inline bool hasSpecificAttr(const AttrVec& vec) {
225   return specific_attr_begin<T>(vec) != specific_attr_end<T>(vec);
226 }
227 template <typename T>
228 inline T *getSpecificAttr(const AttrVec& vec) {
229   specific_attr_iterator<T> i = specific_attr_begin<T>(vec);
230   if (i != specific_attr_end<T>(vec))
231     return *i;
232   else
233     return 0;
234 }
235
236 /// getMaxAlignment - Returns the highest alignment value found among
237 /// AlignedAttrs in an AttrVec, or 0 if there are none.
238 inline unsigned getMaxAttrAlignment(const AttrVec& V, ASTContext &Ctx) {
239   unsigned Align = 0;
240   specific_attr_iterator<AlignedAttr> i(V.begin()), e(V.end());
241   for(; i != e; ++i)
242     Align = std::max(Align, i->getAlignment(Ctx));
243   return Align;
244 }
245
246 }  // end namespace clang
247
248 #endif