clang API Documentation
00001 //===--- Attr.h - Classes for representing expressions ----------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the Attr interface and subclasses. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CLANG_AST_ATTR_H 00015 #define LLVM_CLANG_AST_ATTR_H 00016 00017 #include "clang/Basic/LLVM.h" 00018 #include "clang/Basic/AttrKinds.h" 00019 #include "clang/AST/Type.h" 00020 #include "clang/Basic/SourceLocation.h" 00021 #include "clang/Basic/VersionTuple.h" 00022 #include "llvm/ADT/SmallVector.h" 00023 #include "llvm/ADT/StringRef.h" 00024 #include "llvm/ADT/StringSwitch.h" 00025 #include "llvm/Support/ErrorHandling.h" 00026 #include "llvm/Support/raw_ostream.h" 00027 #include <cassert> 00028 #include <cstring> 00029 #include <algorithm> 00030 00031 namespace clang { 00032 class ASTContext; 00033 class IdentifierInfo; 00034 class ObjCInterfaceDecl; 00035 class Expr; 00036 class QualType; 00037 class FunctionDecl; 00038 class TypeSourceInfo; 00039 } 00040 00041 // Defined in ASTContext.h 00042 void *operator new(size_t Bytes, const clang::ASTContext &C, 00043 size_t Alignment = 16); 00044 // FIXME: Being forced to not have a default argument here due to redeclaration 00045 // rules on default arguments sucks 00046 void *operator new[](size_t Bytes, const clang::ASTContext &C, 00047 size_t Alignment); 00048 00049 // It is good practice to pair new/delete operators. Also, MSVC gives many 00050 // warnings if a matching delete overload is not declared, even though the 00051 // throw() spec guarantees it will not be implicitly called. 00052 void operator delete(void *Ptr, const clang::ASTContext &C, size_t); 00053 void operator delete[](void *Ptr, const clang::ASTContext &C, size_t); 00054 00055 namespace clang { 00056 00057 /// Attr - This represents one attribute. 00058 class Attr { 00059 private: 00060 SourceRange Range; 00061 unsigned AttrKind : 16; 00062 00063 protected: 00064 bool Inherited : 1; 00065 00066 virtual ~Attr(); 00067 00068 void* operator new(size_t bytes) throw() { 00069 llvm_unreachable("Attrs cannot be allocated with regular 'new'."); 00070 } 00071 void operator delete(void* data) throw() { 00072 llvm_unreachable("Attrs cannot be released with regular 'delete'."); 00073 } 00074 00075 public: 00076 // Forward so that the regular new and delete do not hide global ones. 00077 void* operator new(size_t Bytes, ASTContext &C, 00078 size_t Alignment = 16) throw() { 00079 return ::operator new(Bytes, C, Alignment); 00080 } 00081 void operator delete(void *Ptr, ASTContext &C, 00082 size_t Alignment) throw() { 00083 return ::operator delete(Ptr, C, Alignment); 00084 } 00085 00086 protected: 00087 Attr(attr::Kind AK, SourceRange R) 00088 : Range(R), AttrKind(AK), Inherited(false) {} 00089 00090 public: 00091 00092 attr::Kind getKind() const { 00093 return static_cast<attr::Kind>(AttrKind); 00094 } 00095 00096 SourceLocation getLocation() const { return Range.getBegin(); } 00097 SourceRange getRange() const { return Range; } 00098 void setRange(SourceRange R) { Range = R; } 00099 00100 bool isInherited() const { return Inherited; } 00101 00102 // Clone this attribute. 00103 virtual Attr* clone(ASTContext &C) const = 0; 00104 00105 virtual bool isLateParsed() const { return false; } 00106 00107 // Pretty print this attribute. 00108 virtual void printPretty(llvm::raw_ostream &OS, ASTContext &C) const = 0; 00109 00110 // Implement isa/cast/dyncast/etc. 00111 static bool classof(const Attr *) { return true; } 00112 }; 00113 00114 class InheritableAttr : public Attr { 00115 virtual void anchor(); 00116 protected: 00117 InheritableAttr(attr::Kind AK, SourceRange R) 00118 : Attr(AK, R) {} 00119 00120 public: 00121 void setInherited(bool I) { Inherited = I; } 00122 00123 // Implement isa/cast/dyncast/etc. 00124 static bool classof(const Attr *A) { 00125 return A->getKind() <= attr::LAST_INHERITABLE; 00126 } 00127 static bool classof(const InheritableAttr *) { return true; } 00128 }; 00129 00130 class InheritableParamAttr : public InheritableAttr { 00131 virtual void anchor(); 00132 protected: 00133 InheritableParamAttr(attr::Kind AK, SourceRange R) 00134 : InheritableAttr(AK, R) {} 00135 00136 public: 00137 // Implement isa/cast/dyncast/etc. 00138 static bool classof(const Attr *A) { 00139 return A->getKind() <= attr::LAST_INHERITABLE_PARAM; 00140 } 00141 static bool classof(const InheritableParamAttr *) { return true; } 00142 }; 00143 00144 #include "clang/AST/Attrs.inc" 00145 00146 /// AttrVec - A vector of Attr, which is how they are stored on the AST. 00147 typedef SmallVector<Attr*, 2> AttrVec; 00148 typedef SmallVector<const Attr*, 2> ConstAttrVec; 00149 00150 /// specific_attr_iterator - Iterates over a subrange of an AttrVec, only 00151 /// providing attributes that are of a specifc type. 00152 template <typename SpecificAttr> 00153 class specific_attr_iterator { 00154 /// Current - The current, underlying iterator. 00155 /// In order to ensure we don't dereference an invalid iterator unless 00156 /// specifically requested, we don't necessarily advance this all the 00157 /// way. Instead, we advance it when an operation is requested; if the 00158 /// operation is acting on what should be a past-the-end iterator, 00159 /// then we offer no guarantees, but this way we do not dererence a 00160 /// past-the-end iterator when we move to a past-the-end position. 00161 mutable AttrVec::const_iterator Current; 00162 00163 void AdvanceToNext() const { 00164 while (!isa<SpecificAttr>(*Current)) 00165 ++Current; 00166 } 00167 00168 void AdvanceToNext(AttrVec::const_iterator I) const { 00169 while (Current != I && !isa<SpecificAttr>(*Current)) 00170 ++Current; 00171 } 00172 00173 public: 00174 typedef SpecificAttr* value_type; 00175 typedef SpecificAttr* reference; 00176 typedef SpecificAttr* pointer; 00177 typedef std::forward_iterator_tag iterator_category; 00178 typedef std::ptrdiff_t difference_type; 00179 00180 specific_attr_iterator() : Current() { } 00181 explicit specific_attr_iterator(AttrVec::const_iterator i) : Current(i) { } 00182 00183 reference operator*() const { 00184 AdvanceToNext(); 00185 return cast<SpecificAttr>(*Current); 00186 } 00187 pointer operator->() const { 00188 AdvanceToNext(); 00189 return cast<SpecificAttr>(*Current); 00190 } 00191 00192 specific_attr_iterator& operator++() { 00193 ++Current; 00194 return *this; 00195 } 00196 specific_attr_iterator operator++(int) { 00197 specific_attr_iterator Tmp(*this); 00198 ++(*this); 00199 return Tmp; 00200 } 00201 00202 friend bool operator==(specific_attr_iterator Left, 00203 specific_attr_iterator Right) { 00204 if (Left.Current < Right.Current) 00205 Left.AdvanceToNext(Right.Current); 00206 else 00207 Right.AdvanceToNext(Left.Current); 00208 return Left.Current == Right.Current; 00209 } 00210 friend bool operator!=(specific_attr_iterator Left, 00211 specific_attr_iterator Right) { 00212 return !(Left == Right); 00213 } 00214 }; 00215 00216 template <typename T> 00217 inline specific_attr_iterator<T> specific_attr_begin(const AttrVec& vec) { 00218 return specific_attr_iterator<T>(vec.begin()); 00219 } 00220 template <typename T> 00221 inline specific_attr_iterator<T> specific_attr_end(const AttrVec& vec) { 00222 return specific_attr_iterator<T>(vec.end()); 00223 } 00224 00225 template <typename T> 00226 inline bool hasSpecificAttr(const AttrVec& vec) { 00227 return specific_attr_begin<T>(vec) != specific_attr_end<T>(vec); 00228 } 00229 template <typename T> 00230 inline T *getSpecificAttr(const AttrVec& vec) { 00231 specific_attr_iterator<T> i = specific_attr_begin<T>(vec); 00232 if (i != specific_attr_end<T>(vec)) 00233 return *i; 00234 else 00235 return 0; 00236 } 00237 00238 /// getMaxAlignment - Returns the highest alignment value found among 00239 /// AlignedAttrs in an AttrVec, or 0 if there are none. 00240 inline unsigned getMaxAttrAlignment(const AttrVec& V, ASTContext &Ctx) { 00241 unsigned Align = 0; 00242 specific_attr_iterator<AlignedAttr> i(V.begin()), e(V.end()); 00243 for(; i != e; ++i) 00244 Align = std::max(Align, i->getAlignment(Ctx)); 00245 return Align; 00246 } 00247 00248 } // end namespace clang 00249 00250 #endif