1 //===--- Attr.h - Classes for representing attributes ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Attr interface and subclasses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_ATTR_H
15 #define LLVM_CLANG_AST_ATTR_H
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"
34 class ObjCInterfaceDecl;
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,
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);
57 /// Attr - This represents one attribute.
61 unsigned AttrKind : 16;
68 void* operator new(size_t bytes) throw() {
69 llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
71 void operator delete(void* data) throw() {
72 llvm_unreachable("Attrs cannot be released with regular 'delete'.");
76 // Forward so that the regular new and delete do not hide global ones.
77 void* operator new(size_t Bytes, ASTContext &C,
78 size_t Alignment = 16) throw() {
79 return ::operator new(Bytes, C, Alignment);
81 void operator delete(void *Ptr, ASTContext &C,
82 size_t Alignment) throw() {
83 return ::operator delete(Ptr, C, Alignment);
87 Attr(attr::Kind AK, SourceRange R)
88 : Range(R), AttrKind(AK), Inherited(false) {}
92 attr::Kind getKind() const {
93 return static_cast<attr::Kind>(AttrKind);
96 SourceLocation getLocation() const { return Range.getBegin(); }
97 SourceRange getRange() const { return Range; }
98 void setRange(SourceRange R) { Range = R; }
100 bool isInherited() const { return Inherited; }
102 // Clone this attribute.
103 virtual Attr* clone(ASTContext &C) const = 0;
105 virtual bool isLateParsed() const { return false; }
107 // Pretty print this attribute.
108 virtual void printPretty(llvm::raw_ostream &OS,
109 const PrintingPolicy &Policy) const = 0;
112 class InheritableAttr : public Attr {
113 virtual void anchor();
115 InheritableAttr(attr::Kind AK, SourceRange R)
119 void setInherited(bool I) { Inherited = I; }
121 // Implement isa/cast/dyncast/etc.
122 static bool classof(const Attr *A) {
123 return A->getKind() <= attr::LAST_INHERITABLE;
127 class InheritableParamAttr : public InheritableAttr {
128 virtual void anchor();
130 InheritableParamAttr(attr::Kind AK, SourceRange R)
131 : InheritableAttr(AK, R) {}
134 // Implement isa/cast/dyncast/etc.
135 static bool classof(const Attr *A) {
136 return A->getKind() <= attr::LAST_INHERITABLE_PARAM;
140 #include "clang/AST/Attrs.inc"
142 /// AttrVec - A vector of Attr, which is how they are stored on the AST.
143 typedef SmallVector<Attr*, 2> AttrVec;
144 typedef SmallVector<const Attr*, 2> ConstAttrVec;
146 /// specific_attr_iterator - Iterates over a subrange of an AttrVec, only
147 /// providing attributes that are of a specifc type.
148 template <typename SpecificAttr, typename Container = AttrVec>
149 class specific_attr_iterator {
150 typedef typename Container::const_iterator 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 Iterator Current;
161 void AdvanceToNext() const {
162 while (!isa<SpecificAttr>(*Current))
166 void AdvanceToNext(Iterator I) const {
167 while (Current != I && !isa<SpecificAttr>(*Current))
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;
178 specific_attr_iterator() : Current() { }
179 explicit specific_attr_iterator(Iterator i) : Current(i) { }
181 reference operator*() const {
183 return cast<SpecificAttr>(*Current);
185 pointer operator->() const {
187 return cast<SpecificAttr>(*Current);
190 specific_attr_iterator& operator++() {
194 specific_attr_iterator operator++(int) {
195 specific_attr_iterator Tmp(*this);
200 friend bool operator==(specific_attr_iterator Left,
201 specific_attr_iterator Right) {
202 if (Left.Current < Right.Current)
203 Left.AdvanceToNext(Right.Current);
205 Right.AdvanceToNext(Left.Current);
206 return Left.Current == Right.Current;
208 friend bool operator!=(specific_attr_iterator Left,
209 specific_attr_iterator Right) {
210 return !(Left == Right);
214 template <typename SpecificAttr, typename Container>
215 inline specific_attr_iterator<SpecificAttr, Container>
216 specific_attr_begin(const Container& container) {
217 return specific_attr_iterator<SpecificAttr, Container>(container.begin());
219 template <typename SpecificAttr, typename Container>
220 inline specific_attr_iterator<SpecificAttr, Container>
221 specific_attr_end(const Container& container) {
222 return specific_attr_iterator<SpecificAttr, Container>(container.end());
225 template <typename SpecificAttr, typename Container>
226 inline bool hasSpecificAttr(const Container& container) {
227 return specific_attr_begin<SpecificAttr>(container) !=
228 specific_attr_end<SpecificAttr>(container);
230 template <typename SpecificAttr, typename Container>
231 inline SpecificAttr *getSpecificAttr(const Container& container) {
232 specific_attr_iterator<SpecificAttr, Container> i =
233 specific_attr_begin<SpecificAttr>(container);
234 if (i != specific_attr_end<SpecificAttr>(container))
240 /// getMaxAlignment - Returns the highest alignment value found among
241 /// AlignedAttrs in an AttrVec, or 0 if there are none.
242 inline unsigned getMaxAttrAlignment(const AttrVec& V, ASTContext &Ctx) {
244 specific_attr_iterator<AlignedAttr> i(V.begin()), e(V.end());
246 Align = std::max(Align, i->getAlignment(Ctx));
250 } // end namespace clang