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