1 //===--- Attr.h - Classes for representing expressions ----------*- 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 "llvm/Support/Casting.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "clang/Basic/AttrKinds.h"
21 #include "clang/AST/Type.h"
22 #include "clang/Basic/SourceLocation.h"
31 class ObjCInterfaceDecl;
38 // Defined in ASTContext.h
39 void *operator new(size_t Bytes, clang::ASTContext &C,
40 size_t Alignment = 16) throw ();
41 // FIXME: Being forced to not have a default argument here due to redeclaration
42 // rules on default arguments sucks
43 void *operator new[](size_t Bytes, clang::ASTContext &C,
44 size_t Alignment) throw ();
46 // It is good practice to pair new/delete operators. Also, MSVC gives many
47 // warnings if a matching delete overload is not declared, even though the
48 // throw() spec guarantees it will not be implicitly called.
49 void operator delete(void *Ptr, clang::ASTContext &C, size_t)
51 void operator delete[](void *Ptr, clang::ASTContext &C, size_t)
56 /// Attr - This represents one attribute.
60 unsigned AttrKind : 16;
66 void* operator new(size_t bytes) throw() {
67 assert(0 && "Attrs cannot be allocated with regular 'new'.");
70 void operator delete(void* data) throw() {
71 assert(0 && "Attrs cannot be released with regular 'delete'.");
75 // Forward so that the regular new and delete do not hide global ones.
76 void* operator new(size_t Bytes, ASTContext &C,
77 size_t Alignment = 16) throw() {
78 return ::operator new(Bytes, C, Alignment);
80 void operator delete(void *Ptr, ASTContext &C,
81 size_t Alignment) throw() {
82 return ::operator delete(Ptr, C, Alignment);
86 Attr(attr::Kind AK, SourceLocation L)
87 : Loc(L), AttrKind(AK), Inherited(false) {}
91 /// \brief Whether this attribute should be merged to new
93 virtual bool isMerged() const { return true; }
95 attr::Kind getKind() const {
96 return static_cast<attr::Kind>(AttrKind);
99 SourceLocation getLocation() const { return Loc; }
100 void setLocation(SourceLocation L) { Loc = L; }
102 bool isInherited() const { return Inherited; }
103 void setInherited(bool I) { Inherited = I; }
105 // Clone this attribute.
106 virtual Attr* clone(ASTContext &C) const = 0;
108 // Implement isa/cast/dyncast/etc.
109 static bool classof(const Attr *) { return true; }
112 #include "clang/AST/Attrs.inc"
114 /// AttrVec - A vector of Attr, which is how they are stored on the AST.
115 typedef llvm::SmallVector<Attr*, 2> AttrVec;
116 typedef llvm::SmallVector<const Attr*, 2> ConstAttrVec;
118 /// DestroyAttrs - Destroy the contents of an AttrVec.
119 inline void DestroyAttrs (AttrVec& V, ASTContext &C) {
122 /// specific_attr_iterator - Iterates over a subrange of an AttrVec, only
123 /// providing attributes that are of a specifc type.
124 template <typename SpecificAttr>
125 class specific_attr_iterator {
126 /// Current - The current, underlying iterator.
127 /// In order to ensure we don't dereference an invalid iterator unless
128 /// specifically requested, we don't necessarily advance this all the
129 /// way. Instead, we advance it when an operation is requested; if the
130 /// operation is acting on what should be a past-the-end iterator,
131 /// then we offer no guarantees, but this way we do not dererence a
132 /// past-the-end iterator when we move to a past-the-end position.
133 mutable AttrVec::const_iterator Current;
135 void AdvanceToNext() const {
136 while (!llvm::isa<SpecificAttr>(*Current))
140 void AdvanceToNext(AttrVec::const_iterator I) const {
141 while (Current != I && !llvm::isa<SpecificAttr>(*Current))
146 typedef SpecificAttr* value_type;
147 typedef SpecificAttr* reference;
148 typedef SpecificAttr* pointer;
149 typedef std::forward_iterator_tag iterator_category;
150 typedef std::ptrdiff_t difference_type;
152 specific_attr_iterator() : Current() { }
153 explicit specific_attr_iterator(AttrVec::const_iterator i) : Current(i) { }
155 reference operator*() const {
157 return llvm::cast<SpecificAttr>(*Current);
159 pointer operator->() const {
161 return llvm::cast<SpecificAttr>(*Current);
164 specific_attr_iterator& operator++() {
168 specific_attr_iterator operator++(int) {
169 specific_attr_iterator Tmp(*this);
174 friend bool operator==(specific_attr_iterator Left,
175 specific_attr_iterator Right) {
176 if (Left.Current < Right.Current)
177 Left.AdvanceToNext(Right.Current);
179 Right.AdvanceToNext(Left.Current);
180 return Left.Current == Right.Current;
182 friend bool operator!=(specific_attr_iterator Left,
183 specific_attr_iterator Right) {
184 return !(Left == Right);
188 template <typename T>
189 inline specific_attr_iterator<T> specific_attr_begin(const AttrVec& vec) {
190 return specific_attr_iterator<T>(vec.begin());
192 template <typename T>
193 inline specific_attr_iterator<T> specific_attr_end(const AttrVec& vec) {
194 return specific_attr_iterator<T>(vec.end());
197 template <typename T>
198 inline bool hasSpecificAttr(const AttrVec& vec) {
199 return specific_attr_begin<T>(vec) != specific_attr_end<T>(vec);
201 template <typename T>
202 inline T *getSpecificAttr(const AttrVec& vec) {
203 specific_attr_iterator<T> i = specific_attr_begin<T>(vec);
204 if (i != specific_attr_end<T>(vec))
210 /// getMaxAlignment - Returns the highest alignment value found among
211 /// AlignedAttrs in an AttrVec, or 0 if there are none.
212 inline unsigned getMaxAttrAlignment(const AttrVec& V, ASTContext &Ctx) {
214 specific_attr_iterator<AlignedAttr> i(V.begin()), e(V.end());
216 Align = std::max(Align, i->getAlignment(Ctx));
220 } // end namespace clang