| 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/DeclCXX.h" |
| 20 | #include "clang/AST/Type.h" |
| 21 | #include "clang/Basic/AttrKinds.h" |
| 22 | #include "clang/Basic/AttributeCommonInfo.h" |
| 23 | #include "clang/Basic/LLVM.h" |
| 24 | #include "clang/Basic/LangOptions.h" |
| 25 | #include "clang/Basic/OpenMPKinds.h" |
| 26 | #include "clang/Basic/Sanitizers.h" |
| 27 | #include "clang/Basic/SourceLocation.h" |
| 28 | #include "clang/Support/Compiler.h" |
| 29 | #include "llvm/Frontend/HLSL/HLSLResource.h" |
| 30 | #include "llvm/Support/CodeGen.h" |
| 31 | #include "llvm/Support/ErrorHandling.h" |
| 32 | #include "llvm/Support/VersionTuple.h" |
| 33 | #include "llvm/Support/raw_ostream.h" |
| 34 | #include <algorithm> |
| 35 | #include <cassert> |
| 36 | |
| 37 | namespace clang { |
| 38 | class ASTContext; |
| 39 | class AttributeCommonInfo; |
| 40 | class FunctionDecl; |
| 41 | class OMPTraitInfo; |
| 42 | class OpenACCClause; |
| 43 | struct StructuralEquivalenceContext; |
| 44 | |
| 45 | /// Attr - This represents one attribute. |
| 46 | class Attr : public AttributeCommonInfo { |
| 47 | private: |
| 48 | LLVM_PREFERRED_TYPE(attr::Kind) |
| 49 | unsigned AttrKind : 16; |
| 50 | |
| 51 | protected: |
| 52 | /// An index into the spelling list of an |
| 53 | /// attribute defined in Attr.td file. |
| 54 | LLVM_PREFERRED_TYPE(bool) |
| 55 | unsigned Inherited : 1; |
| 56 | LLVM_PREFERRED_TYPE(bool) |
| 57 | unsigned IsPackExpansion : 1; |
| 58 | LLVM_PREFERRED_TYPE(bool) |
| 59 | unsigned Implicit : 1; |
| 60 | // FIXME: These are properties of the attribute kind, not state for this |
| 61 | // instance of the attribute. |
| 62 | LLVM_PREFERRED_TYPE(bool) |
| 63 | unsigned IsLateParsed : 1; |
| 64 | LLVM_PREFERRED_TYPE(bool) |
| 65 | unsigned InheritEvenIfAlreadyPresent : 1; |
| 66 | |
| 67 | void *operator new(size_t bytes) noexcept { |
| 68 | llvm_unreachable("Attrs cannot be allocated with regular 'new'." ); |
| 69 | } |
| 70 | void operator delete(void *data) noexcept { |
| 71 | llvm_unreachable("Attrs cannot be released with regular 'delete'." ); |
| 72 | } |
| 73 | |
| 74 | public: |
| 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 = 8) noexcept { |
| 78 | return ::operator new(Bytes, C, Alignment); |
| 79 | } |
| 80 | void operator delete(void *Ptr, ASTContext &C, size_t Alignment) noexcept { |
| 81 | return ::operator delete(Ptr, C, Alignment); |
| 82 | } |
| 83 | |
| 84 | protected: |
| 85 | Attr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, |
| 86 | attr::Kind AK, bool IsLateParsed) |
| 87 | : AttributeCommonInfo(CommonInfo), AttrKind(AK), Inherited(false), |
| 88 | IsPackExpansion(false), Implicit(false), IsLateParsed(IsLateParsed), |
| 89 | InheritEvenIfAlreadyPresent(false) {} |
| 90 | |
| 91 | public: |
| 92 | attr::Kind getKind() const { return static_cast<attr::Kind>(AttrKind); } |
| 93 | |
| 94 | unsigned getSpellingListIndex() const { |
| 95 | return getAttributeSpellingListIndex(); |
| 96 | } |
| 97 | const char *getSpelling() const; |
| 98 | |
| 99 | SourceLocation getLocation() const { return getRange().getBegin(); } |
| 100 | |
| 101 | bool isInherited() const { return Inherited; } |
| 102 | |
| 103 | /// Returns true if the attribute has been implicitly created instead |
| 104 | /// of explicitly written by the user. |
| 105 | bool isImplicit() const { return Implicit; } |
| 106 | void setImplicit(bool I) { Implicit = I; } |
| 107 | |
| 108 | void setPackExpansion(bool PE) { IsPackExpansion = PE; } |
| 109 | bool isPackExpansion() const { return IsPackExpansion; } |
| 110 | |
| 111 | // Clone this attribute. |
| 112 | Attr *clone(ASTContext &C) const; |
| 113 | |
| 114 | bool isLateParsed() const { return IsLateParsed; } |
| 115 | |
| 116 | bool isEquivalent(const Attr &Other, |
| 117 | StructuralEquivalenceContext &Context) const; |
| 118 | |
| 119 | // Pretty print this attribute. |
| 120 | void printPretty(raw_ostream &OS, const PrintingPolicy &Policy) const; |
| 121 | |
| 122 | static StringRef getDocumentation(attr::Kind); |
| 123 | }; |
| 124 | |
| 125 | class TypeAttr : public Attr { |
| 126 | protected: |
| 127 | TypeAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, |
| 128 | attr::Kind AK, bool IsLateParsed) |
| 129 | : Attr(Context, CommonInfo, AK, IsLateParsed) {} |
| 130 | |
| 131 | public: |
| 132 | static bool classof(const Attr *A) { |
| 133 | return A->getKind() >= attr::FirstTypeAttr && |
| 134 | A->getKind() <= attr::LastTypeAttr; |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | class StmtAttr : public Attr { |
| 139 | protected: |
| 140 | StmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, |
| 141 | attr::Kind AK, bool IsLateParsed) |
| 142 | : Attr(Context, CommonInfo, AK, IsLateParsed) {} |
| 143 | |
| 144 | public: |
| 145 | static bool classof(const Attr *A) { |
| 146 | return A->getKind() >= attr::FirstStmtAttr && |
| 147 | A->getKind() <= attr::LastStmtAttr; |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | class InheritableAttr : public Attr { |
| 152 | protected: |
| 153 | InheritableAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, |
| 154 | attr::Kind AK, bool IsLateParsed, |
| 155 | bool InheritEvenIfAlreadyPresent) |
| 156 | : Attr(Context, CommonInfo, AK, IsLateParsed) { |
| 157 | this->InheritEvenIfAlreadyPresent = InheritEvenIfAlreadyPresent; |
| 158 | } |
| 159 | |
| 160 | public: |
| 161 | void setInherited(bool I) { Inherited = I; } |
| 162 | |
| 163 | /// Should this attribute be inherited from a prior declaration even if it's |
| 164 | /// explicitly provided in the current declaration? |
| 165 | bool shouldInheritEvenIfAlreadyPresent() const { |
| 166 | return InheritEvenIfAlreadyPresent; |
| 167 | } |
| 168 | |
| 169 | // Implement isa/cast/dyncast/etc. |
| 170 | static bool classof(const Attr *A) { |
| 171 | return A->getKind() >= attr::FirstInheritableAttr && |
| 172 | A->getKind() <= attr::LastInheritableAttr; |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | class DeclOrStmtAttr : public InheritableAttr { |
| 177 | protected: |
| 178 | DeclOrStmtAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, |
| 179 | attr::Kind AK, bool IsLateParsed, |
| 180 | bool InheritEvenIfAlreadyPresent) |
| 181 | : InheritableAttr(Context, CommonInfo, AK, IsLateParsed, |
| 182 | InheritEvenIfAlreadyPresent) {} |
| 183 | |
| 184 | public: |
| 185 | static bool classof(const Attr *A) { |
| 186 | return A->getKind() >= attr::FirstDeclOrStmtAttr && |
| 187 | A->getKind() <= attr::LastDeclOrStmtAttr; |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | class InheritableParamAttr : public InheritableAttr { |
| 192 | protected: |
| 193 | InheritableParamAttr(ASTContext &Context, |
| 194 | const AttributeCommonInfo &CommonInfo, attr::Kind AK, |
| 195 | bool IsLateParsed, bool InheritEvenIfAlreadyPresent) |
| 196 | : InheritableAttr(Context, CommonInfo, AK, IsLateParsed, |
| 197 | InheritEvenIfAlreadyPresent) {} |
| 198 | |
| 199 | public: |
| 200 | // Implement isa/cast/dyncast/etc. |
| 201 | static bool classof(const Attr *A) { |
| 202 | return A->getKind() >= attr::FirstInheritableParamAttr && |
| 203 | A->getKind() <= attr::LastInheritableParamAttr; |
| 204 | } |
| 205 | }; |
| 206 | |
| 207 | class InheritableParamOrStmtAttr : public InheritableParamAttr { |
| 208 | protected: |
| 209 | InheritableParamOrStmtAttr(ASTContext &Context, |
| 210 | const AttributeCommonInfo &CommonInfo, |
| 211 | attr::Kind AK, bool IsLateParsed, |
| 212 | bool InheritEvenIfAlreadyPresent) |
| 213 | : InheritableParamAttr(Context, CommonInfo, AK, IsLateParsed, |
| 214 | InheritEvenIfAlreadyPresent) {} |
| 215 | |
| 216 | public: |
| 217 | // Implement isa/cast/dyncast/etc. |
| 218 | static bool classof(const Attr *A) { |
| 219 | return A->getKind() >= attr::FirstInheritableParamOrStmtAttr && |
| 220 | A->getKind() <= attr::LastInheritableParamOrStmtAttr; |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | class HLSLAnnotationAttr : public InheritableAttr { |
| 225 | protected: |
| 226 | HLSLAnnotationAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, |
| 227 | attr::Kind AK, bool IsLateParsed, |
| 228 | bool InheritEvenIfAlreadyPresent) |
| 229 | : InheritableAttr(Context, CommonInfo, AK, IsLateParsed, |
| 230 | InheritEvenIfAlreadyPresent) {} |
| 231 | |
| 232 | public: |
| 233 | // Implement isa/cast/dyncast/etc. |
| 234 | static bool classof(const Attr *A) { |
| 235 | return A->getKind() >= attr::FirstHLSLAnnotationAttr && |
| 236 | A->getKind() <= attr::LastHLSLAnnotationAttr; |
| 237 | } |
| 238 | }; |
| 239 | |
| 240 | class HLSLSemanticBaseAttr : public HLSLAnnotationAttr { |
| 241 | protected: |
| 242 | HLSLSemanticBaseAttr(ASTContext &Context, |
| 243 | const AttributeCommonInfo &CommonInfo, attr::Kind AK, |
| 244 | bool IsLateParsed, bool InheritEvenIfAlreadyPresent) |
| 245 | : HLSLAnnotationAttr(Context, CommonInfo, AK, IsLateParsed, |
| 246 | InheritEvenIfAlreadyPresent) {} |
| 247 | |
| 248 | public: |
| 249 | // Implement isa/cast/dyncast/etc. |
| 250 | static bool classof(const Attr *A) { |
| 251 | return A->getKind() >= attr::FirstHLSLSemanticBaseAttr && |
| 252 | A->getKind() <= attr::LastHLSLSemanticBaseAttr; |
| 253 | } |
| 254 | }; |
| 255 | |
| 256 | /// A parameter attribute which changes the argument-passing ABI rule |
| 257 | /// for the parameter. |
| 258 | class ParameterABIAttr : public InheritableParamAttr { |
| 259 | protected: |
| 260 | ParameterABIAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo, |
| 261 | attr::Kind AK, bool IsLateParsed, |
| 262 | bool InheritEvenIfAlreadyPresent) |
| 263 | : InheritableParamAttr(Context, CommonInfo, AK, IsLateParsed, |
| 264 | InheritEvenIfAlreadyPresent) {} |
| 265 | |
| 266 | public: |
| 267 | ParameterABI getABI() const; |
| 268 | |
| 269 | static bool classof(const Attr *A) { |
| 270 | return A->getKind() >= attr::FirstParameterABIAttr && |
| 271 | A->getKind() <= attr::LastParameterABIAttr; |
| 272 | } |
| 273 | }; |
| 274 | |
| 275 | /// A single parameter index whose accessors require each use to make explicit |
| 276 | /// the parameter index encoding needed. |
| 277 | class ParamIdx { |
| 278 | // Idx is exposed only via accessors that specify specific encodings. |
| 279 | unsigned Idx : 30; |
| 280 | LLVM_PREFERRED_TYPE(bool) |
| 281 | unsigned HasThis : 1; |
| 282 | LLVM_PREFERRED_TYPE(bool) |
| 283 | unsigned IsValid : 1; |
| 284 | |
| 285 | void assertComparable(const ParamIdx &I) const { |
| 286 | assert(isValid() && I.isValid() && |
| 287 | "ParamIdx must be valid to be compared" ); |
| 288 | // It's possible to compare indices from separate functions, but so far |
| 289 | // it's not proven useful. Moreover, it might be confusing because a |
| 290 | // comparison on the results of getASTIndex might be inconsistent with a |
| 291 | // comparison on the ParamIdx objects themselves. |
| 292 | assert(HasThis == I.HasThis && |
| 293 | "ParamIdx must be for the same function to be compared" ); |
| 294 | } |
| 295 | |
| 296 | public: |
| 297 | /// Construct an invalid parameter index (\c isValid returns false and |
| 298 | /// accessors fail an assert). |
| 299 | ParamIdx() : Idx(0), HasThis(false), IsValid(false) {} |
| 300 | |
| 301 | /// \param Idx is the parameter index as it is normally specified in |
| 302 | /// attributes in the source: one-origin including any C++ implicit this |
| 303 | /// parameter. |
| 304 | /// |
| 305 | /// \param D is the declaration containing the parameters. It is used to |
| 306 | /// determine if there is a C++ implicit this parameter. |
| 307 | ParamIdx(unsigned Idx, const Decl *D) |
| 308 | : Idx(Idx), HasThis(false), IsValid(true) { |
| 309 | assert(Idx >= 1 && "Idx must be one-origin" ); |
| 310 | if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(Val: D)) |
| 311 | HasThis = MethodDecl->isImplicitObjectMemberFunction(); |
| 312 | } |
| 313 | |
| 314 | /// A type into which \c ParamIdx can be serialized. |
| 315 | /// |
| 316 | /// A static assertion that it's of the correct size follows the \c ParamIdx |
| 317 | /// class definition. |
| 318 | typedef uint32_t SerialType; |
| 319 | |
| 320 | /// Produce a representation that can later be passed to \c deserialize to |
| 321 | /// construct an equivalent \c ParamIdx. |
| 322 | SerialType serialize() const { |
| 323 | return *reinterpret_cast<const SerialType *>(this); |
| 324 | } |
| 325 | |
| 326 | /// Construct from a result from \c serialize. |
| 327 | static ParamIdx deserialize(SerialType S) { |
| 328 | // Using this two-step static_cast via void * instead of reinterpret_cast |
| 329 | // silences a -Wstrict-aliasing false positive from GCC7 and earlier. |
| 330 | void *ParamIdxPtr = static_cast<void *>(&S); |
| 331 | ParamIdx P(*static_cast<ParamIdx *>(ParamIdxPtr)); |
| 332 | assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin" ); |
| 333 | return P; |
| 334 | } |
| 335 | |
| 336 | /// Is this parameter index valid? |
| 337 | bool isValid() const { return IsValid; } |
| 338 | |
| 339 | /// Get the parameter index as it would normally be encoded for attributes at |
| 340 | /// the source level of representation: one-origin including any C++ implicit |
| 341 | /// this parameter. |
| 342 | /// |
| 343 | /// This encoding thus makes sense for diagnostics, pretty printing, and |
| 344 | /// constructing new attributes from a source-like specification. |
| 345 | unsigned getSourceIndex() const { |
| 346 | assert(isValid() && "ParamIdx must be valid" ); |
| 347 | return Idx; |
| 348 | } |
| 349 | |
| 350 | /// Get the parameter index as it would normally be encoded at the AST level |
| 351 | /// of representation: zero-origin not including any C++ implicit this |
| 352 | /// parameter. |
| 353 | /// |
| 354 | /// This is the encoding primarily used in Sema. However, in diagnostics, |
| 355 | /// Sema uses \c getSourceIndex instead. |
| 356 | unsigned getASTIndex() const { |
| 357 | assert(isValid() && "ParamIdx must be valid" ); |
| 358 | assert(Idx >= 1 + HasThis && |
| 359 | "stored index must be base-1 and not specify C++ implicit this" ); |
| 360 | return Idx - 1 - HasThis; |
| 361 | } |
| 362 | |
| 363 | /// Get the parameter index as it would normally be encoded at the LLVM level |
| 364 | /// of representation: zero-origin including any C++ implicit this parameter. |
| 365 | /// |
| 366 | /// This is the encoding primarily used in CodeGen. |
| 367 | unsigned getLLVMIndex() const { |
| 368 | assert(isValid() && "ParamIdx must be valid" ); |
| 369 | assert(Idx >= 1 && "stored index must be base-1" ); |
| 370 | return Idx - 1; |
| 371 | } |
| 372 | |
| 373 | bool operator==(const ParamIdx &I) const { |
| 374 | assertComparable(I); |
| 375 | return Idx == I.Idx; |
| 376 | } |
| 377 | bool operator!=(const ParamIdx &I) const { |
| 378 | assertComparable(I); |
| 379 | return Idx != I.Idx; |
| 380 | } |
| 381 | bool operator<(const ParamIdx &I) const { |
| 382 | assertComparable(I); |
| 383 | return Idx < I.Idx; |
| 384 | } |
| 385 | bool operator>(const ParamIdx &I) const { |
| 386 | assertComparable(I); |
| 387 | return Idx > I.Idx; |
| 388 | } |
| 389 | bool operator<=(const ParamIdx &I) const { |
| 390 | assertComparable(I); |
| 391 | return Idx <= I.Idx; |
| 392 | } |
| 393 | bool operator>=(const ParamIdx &I) const { |
| 394 | assertComparable(I); |
| 395 | return Idx >= I.Idx; |
| 396 | } |
| 397 | }; |
| 398 | |
| 399 | static_assert(sizeof(ParamIdx) == sizeof(ParamIdx::SerialType), |
| 400 | "ParamIdx does not fit its serialization type" ); |
| 401 | |
| 402 | #include "clang/AST/Attrs.inc" // IWYU pragma: export |
| 403 | |
| 404 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, |
| 405 | const Attr *At) { |
| 406 | DB.AddTaggedVal(V: reinterpret_cast<uint64_t>(At), Kind: DiagnosticsEngine::ak_attr); |
| 407 | return DB; |
| 408 | } |
| 409 | |
| 410 | inline ParameterABI ParameterABIAttr::getABI() const { |
| 411 | switch (getKind()) { |
| 412 | case attr::SwiftContext: |
| 413 | return ParameterABI::SwiftContext; |
| 414 | case attr::SwiftAsyncContext: |
| 415 | return ParameterABI::SwiftAsyncContext; |
| 416 | case attr::SwiftErrorResult: |
| 417 | return ParameterABI::SwiftErrorResult; |
| 418 | case attr::SwiftIndirectResult: |
| 419 | return ParameterABI::SwiftIndirectResult; |
| 420 | case attr::HLSLParamModifier: { |
| 421 | const auto *A = cast<HLSLParamModifierAttr>(Val: this); |
| 422 | if (A->isOut()) |
| 423 | return ParameterABI::HLSLOut; |
| 424 | if (A->isInOut()) |
| 425 | return ParameterABI::HLSLInOut; |
| 426 | return ParameterABI::Ordinary; |
| 427 | } |
| 428 | default: |
| 429 | llvm_unreachable("bad parameter ABI attribute kind" ); |
| 430 | } |
| 431 | } |
| 432 | } // end namespace clang |
| 433 | |
| 434 | #endif |
| 435 | |