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