1 //===--- Attr.h - Classes for representing attributes ----------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the Attr interface and subclasses. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_AST_ATTR_H 14 #define LLVM_CLANG_AST_ATTR_H 15 16 #include "clang/AST/ASTFwd.h" 17 #include "clang/AST/AttrIterator.h" 18 #include "clang/AST/Decl.h" 19 #include "clang/AST/Type.h" 20 #include "clang/Basic/AttrKinds.h" 21 #include "clang/Basic/AttributeCommonInfo.h" 22 #include "clang/Basic/LangOptions.h" 23 #include "clang/Basic/LLVM.h" 24 #include "clang/Basic/OpenMPKinds.h" 25 #include "clang/Basic/Sanitizers.h" 26 #include "clang/Basic/SourceLocation.h" 27 #include "llvm/ADT/StringSwitch.h" 28 #include "llvm/Support/ErrorHandling.h" 29 #include "llvm/Support/VersionTuple.h" 30 #include "llvm/Support/raw_ostream.h" 31 #include <algorithm> 32 #include <cassert> 33 34 namespace clang { 35 class ASTContext; 36 class AttributeCommonInfo; 37 class IdentifierInfo; 38 class ObjCInterfaceDecl; 39 class Expr; 40 class QualType; 41 class FunctionDecl; 42 class TypeSourceInfo; 43 class OMPTraitInfo; 44 45 /// Attr - This represents one attribute. 46 class Attr : public AttributeCommonInfo { 47 private: 48 unsigned AttrKind : 16; 49 50 protected: 51 /// An index into the spelling list of an 52 /// attribute defined in Attr.td file. 53 unsigned Inherited : 1; 54 unsigned IsPackExpansion : 1; 55 unsigned Implicit : 1; 56 // FIXME: These are properties of the attribute kind, not state for this 57 // instance of the attribute. 58 unsigned IsLateParsed : 1; 59 unsigned InheritEvenIfAlreadyPresent : 1; 60 new(size_t bytes)61 void *operator new(size_t bytes) noexcept { 62 llvm_unreachable("Attrs cannot be allocated with regular 'new'."); 63 } delete(void * data)64 void operator delete(void *data) noexcept { 65 llvm_unreachable("Attrs cannot be released with regular 'delete'."); 66 } 67 68 public: 69 // Forward so that the regular new and delete do not hide global ones. 70 void *operator new(size_t Bytes, ASTContext &C, 71 size_t Alignment = 8) noexcept { 72 return ::operator new(Bytes, C, Alignment); 73 } delete(void * Ptr,ASTContext & C,size_t Alignment)74 void operator delete(void *Ptr, ASTContext &C, size_t Alignment) noexcept { 75 return ::operator delete(Ptr, C, Alignment); 76 } 77 78 protected: Attr(ASTContext & Context,const AttributeCommonInfo & CommonInfo,attr::Kind AK,bool IsLateParsed)79 Attr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, 80 attr::Kind AK, bool IsLateParsed) 81 : AttributeCommonInfo(CommonInfo), AttrKind(AK), Inherited(false), 82 IsPackExpansion(false), Implicit(false), IsLateParsed(IsLateParsed), 83 InheritEvenIfAlreadyPresent(false) {} 84 85 public: getKind()86 attr::Kind getKind() const { return static_cast<attr::Kind>(AttrKind); } 87 getSpellingListIndex()88 unsigned getSpellingListIndex() const { 89 return getAttributeSpellingListIndex(); 90 } 91 const char *getSpelling() const; 92 getLocation()93 SourceLocation getLocation() const { return getRange().getBegin(); } 94 isInherited()95 bool isInherited() const { return Inherited; } 96 97 /// Returns true if the attribute has been implicitly created instead 98 /// of explicitly written by the user. isImplicit()99 bool isImplicit() const { return Implicit; } setImplicit(bool I)100 void setImplicit(bool I) { Implicit = I; } 101 setPackExpansion(bool PE)102 void setPackExpansion(bool PE) { IsPackExpansion = PE; } isPackExpansion()103 bool isPackExpansion() const { return IsPackExpansion; } 104 105 // Clone this attribute. 106 Attr *clone(ASTContext &C) const; 107 isLateParsed()108 bool isLateParsed() const { return IsLateParsed; } 109 110 // Pretty print this attribute. 111 void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const; 112 }; 113 114 class TypeAttr : public Attr { 115 protected: TypeAttr(ASTContext & Context,const AttributeCommonInfo & CommonInfo,attr::Kind AK,bool IsLateParsed)116 TypeAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, 117 attr::Kind AK, bool IsLateParsed) 118 : Attr(Context, CommonInfo, AK, IsLateParsed) {} 119 120 public: classof(const Attr * A)121 static bool classof(const Attr *A) { 122 return A->getKind() >= attr::FirstTypeAttr && 123 A->getKind() <= attr::LastTypeAttr; 124 } 125 }; 126 127 class StmtAttr : public Attr { 128 protected: StmtAttr(ASTContext & Context,const AttributeCommonInfo & CommonInfo,attr::Kind AK,bool IsLateParsed)129 StmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, 130 attr::Kind AK, bool IsLateParsed) 131 : Attr(Context, CommonInfo, AK, IsLateParsed) {} 132 133 public: classof(const Attr * A)134 static bool classof(const Attr *A) { 135 return A->getKind() >= attr::FirstStmtAttr && 136 A->getKind() <= attr::LastStmtAttr; 137 } 138 }; 139 140 class InheritableAttr : public Attr { 141 protected: InheritableAttr(ASTContext & Context,const AttributeCommonInfo & CommonInfo,attr::Kind AK,bool IsLateParsed,bool InheritEvenIfAlreadyPresent)142 InheritableAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, 143 attr::Kind AK, bool IsLateParsed, 144 bool InheritEvenIfAlreadyPresent) 145 : Attr(Context, CommonInfo, AK, IsLateParsed) { 146 this->InheritEvenIfAlreadyPresent = InheritEvenIfAlreadyPresent; 147 } 148 149 public: setInherited(bool I)150 void setInherited(bool I) { Inherited = I; } 151 152 /// Should this attribute be inherited from a prior declaration even if it's 153 /// explicitly provided in the current declaration? shouldInheritEvenIfAlreadyPresent()154 bool shouldInheritEvenIfAlreadyPresent() const { 155 return InheritEvenIfAlreadyPresent; 156 } 157 158 // Implement isa/cast/dyncast/etc. classof(const Attr * A)159 static bool classof(const Attr *A) { 160 return A->getKind() >= attr::FirstInheritableAttr && 161 A->getKind() <= attr::LastInheritableAttr; 162 } 163 }; 164 165 class InheritableParamAttr : public InheritableAttr { 166 protected: InheritableParamAttr(ASTContext & Context,const AttributeCommonInfo & CommonInfo,attr::Kind AK,bool IsLateParsed,bool InheritEvenIfAlreadyPresent)167 InheritableParamAttr(ASTContext &Context, 168 const AttributeCommonInfo &CommonInfo, attr::Kind AK, 169 bool IsLateParsed, bool InheritEvenIfAlreadyPresent) 170 : InheritableAttr(Context, CommonInfo, AK, IsLateParsed, 171 InheritEvenIfAlreadyPresent) {} 172 173 public: 174 // Implement isa/cast/dyncast/etc. classof(const Attr * A)175 static bool classof(const Attr *A) { 176 return A->getKind() >= attr::FirstInheritableParamAttr && 177 A->getKind() <= attr::LastInheritableParamAttr; 178 } 179 }; 180 181 /// A parameter attribute which changes the argument-passing ABI rule 182 /// for the parameter. 183 class ParameterABIAttr : public InheritableParamAttr { 184 protected: ParameterABIAttr(ASTContext & Context,const AttributeCommonInfo & CommonInfo,attr::Kind AK,bool IsLateParsed,bool InheritEvenIfAlreadyPresent)185 ParameterABIAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, 186 attr::Kind AK, bool IsLateParsed, 187 bool InheritEvenIfAlreadyPresent) 188 : InheritableParamAttr(Context, CommonInfo, AK, IsLateParsed, 189 InheritEvenIfAlreadyPresent) {} 190 191 public: getABI()192 ParameterABI getABI() const { 193 switch (getKind()) { 194 case attr::SwiftContext: 195 return ParameterABI::SwiftContext; 196 case attr::SwiftErrorResult: 197 return ParameterABI::SwiftErrorResult; 198 case attr::SwiftIndirectResult: 199 return ParameterABI::SwiftIndirectResult; 200 default: 201 llvm_unreachable("bad parameter ABI attribute kind"); 202 } 203 } 204 classof(const Attr * A)205 static bool classof(const Attr *A) { 206 return A->getKind() >= attr::FirstParameterABIAttr && 207 A->getKind() <= attr::LastParameterABIAttr; 208 } 209 }; 210 211 /// A single parameter index whose accessors require each use to make explicit 212 /// the parameter index encoding needed. 213 class ParamIdx { 214 // Idx is exposed only via accessors that specify specific encodings. 215 unsigned Idx : 30; 216 unsigned HasThis : 1; 217 unsigned IsValid : 1; 218 assertComparable(const ParamIdx & I)219 void assertComparable(const ParamIdx &I) const { 220 assert(isValid() && I.isValid() && 221 "ParamIdx must be valid to be compared"); 222 // It's possible to compare indices from separate functions, but so far 223 // it's not proven useful. Moreover, it might be confusing because a 224 // comparison on the results of getASTIndex might be inconsistent with a 225 // comparison on the ParamIdx objects themselves. 226 assert(HasThis == I.HasThis && 227 "ParamIdx must be for the same function to be compared"); 228 } 229 230 public: 231 /// Construct an invalid parameter index (\c isValid returns false and 232 /// accessors fail an assert). ParamIdx()233 ParamIdx() : Idx(0), HasThis(false), IsValid(false) {} 234 235 /// \param Idx is the parameter index as it is normally specified in 236 /// attributes in the source: one-origin including any C++ implicit this 237 /// parameter. 238 /// 239 /// \param D is the declaration containing the parameters. It is used to 240 /// determine if there is a C++ implicit this parameter. ParamIdx(unsigned Idx,const Decl * D)241 ParamIdx(unsigned Idx, const Decl *D) 242 : Idx(Idx), HasThis(false), IsValid(true) { 243 assert(Idx >= 1 && "Idx must be one-origin"); 244 if (const auto *FD = dyn_cast<FunctionDecl>(D)) 245 HasThis = FD->isCXXInstanceMember(); 246 } 247 248 /// A type into which \c ParamIdx can be serialized. 249 /// 250 /// A static assertion that it's of the correct size follows the \c ParamIdx 251 /// class definition. 252 typedef uint32_t SerialType; 253 254 /// Produce a representation that can later be passed to \c deserialize to 255 /// construct an equivalent \c ParamIdx. serialize()256 SerialType serialize() const { 257 return *reinterpret_cast<const SerialType *>(this); 258 } 259 260 /// Construct from a result from \c serialize. deserialize(SerialType S)261 static ParamIdx deserialize(SerialType S) { 262 // Using this two-step static_cast via void * instead of reinterpret_cast 263 // silences a -Wstrict-aliasing false positive from GCC7 and earlier. 264 void *ParamIdxPtr = static_cast<void *>(&S); 265 ParamIdx P(*static_cast<ParamIdx *>(ParamIdxPtr)); 266 assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin"); 267 return P; 268 } 269 270 /// Is this parameter index valid? isValid()271 bool isValid() const { return IsValid; } 272 273 /// Get the parameter index as it would normally be encoded for attributes at 274 /// the source level of representation: one-origin including any C++ implicit 275 /// this parameter. 276 /// 277 /// This encoding thus makes sense for diagnostics, pretty printing, and 278 /// constructing new attributes from a source-like specification. getSourceIndex()279 unsigned getSourceIndex() const { 280 assert(isValid() && "ParamIdx must be valid"); 281 return Idx; 282 } 283 284 /// Get the parameter index as it would normally be encoded at the AST level 285 /// of representation: zero-origin not including any C++ implicit this 286 /// parameter. 287 /// 288 /// This is the encoding primarily used in Sema. However, in diagnostics, 289 /// Sema uses \c getSourceIndex instead. getASTIndex()290 unsigned getASTIndex() const { 291 assert(isValid() && "ParamIdx must be valid"); 292 assert(Idx >= 1 + HasThis && 293 "stored index must be base-1 and not specify C++ implicit this"); 294 return Idx - 1 - HasThis; 295 } 296 297 /// Get the parameter index as it would normally be encoded at the LLVM level 298 /// of representation: zero-origin including any C++ implicit this parameter. 299 /// 300 /// This is the encoding primarily used in CodeGen. getLLVMIndex()301 unsigned getLLVMIndex() const { 302 assert(isValid() && "ParamIdx must be valid"); 303 assert(Idx >= 1 && "stored index must be base-1"); 304 return Idx - 1; 305 } 306 307 bool operator==(const ParamIdx &I) const { 308 assertComparable(I); 309 return Idx == I.Idx; 310 } 311 bool operator!=(const ParamIdx &I) const { 312 assertComparable(I); 313 return Idx != I.Idx; 314 } 315 bool operator<(const ParamIdx &I) const { 316 assertComparable(I); 317 return Idx < I.Idx; 318 } 319 bool operator>(const ParamIdx &I) const { 320 assertComparable(I); 321 return Idx > I.Idx; 322 } 323 bool operator<=(const ParamIdx &I) const { 324 assertComparable(I); 325 return Idx <= I.Idx; 326 } 327 bool operator>=(const ParamIdx &I) const { 328 assertComparable(I); 329 return Idx >= I.Idx; 330 } 331 }; 332 333 static_assert(sizeof(ParamIdx) == sizeof(ParamIdx::SerialType), 334 "ParamIdx does not fit its serialization type"); 335 336 /// Contains information gathered from parsing the contents of TargetAttr. 337 struct ParsedTargetAttr { 338 std::vector<std::string> Features; 339 StringRef Architecture; 340 StringRef Tune; 341 StringRef BranchProtection; 342 bool DuplicateArchitecture = false; 343 bool DuplicateTune = false; 344 bool operator ==(const ParsedTargetAttr &Other) const { 345 return DuplicateArchitecture == Other.DuplicateArchitecture && 346 DuplicateTune == Other.DuplicateTune && 347 Architecture == Other.Architecture && 348 Tune == Other.Tune && 349 BranchProtection == Other.BranchProtection && 350 Features == Other.Features; 351 } 352 }; 353 354 #include "clang/AST/Attrs.inc" 355 356 inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, 357 const Attr *At) { 358 DB.AddTaggedVal(reinterpret_cast<intptr_t>(At), 359 DiagnosticsEngine::ak_attr); 360 return DB; 361 } 362 } // end namespace clang 363 364 #endif 365