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