1 //===--- DeclSpec.h - Parsed declaration specifiers -------------*- 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 /// \file 10 /// This file defines the classes used to store parsed information about 11 /// declaration-specifiers and declarators. 12 /// 13 /// \verbatim 14 /// static const int volatile x, *y, *(*(*z)[10])(const void *x); 15 /// ------------------------- - -- --------------------------- 16 /// declaration-specifiers \ | / 17 /// declarators 18 /// \endverbatim 19 /// 20 //===----------------------------------------------------------------------===// 21 22 #ifndef LLVM_CLANG_SEMA_DECLSPEC_H 23 #define LLVM_CLANG_SEMA_DECLSPEC_H 24 25 #include "clang/AST/DeclCXX.h" 26 #include "clang/AST/DeclObjCCommon.h" 27 #include "clang/AST/NestedNameSpecifier.h" 28 #include "clang/Basic/ExceptionSpecificationType.h" 29 #include "clang/Basic/Lambda.h" 30 #include "clang/Basic/OperatorKinds.h" 31 #include "clang/Basic/Specifiers.h" 32 #include "clang/Lex/Token.h" 33 #include "clang/Sema/Ownership.h" 34 #include "clang/Sema/ParsedAttr.h" 35 #include "llvm/ADT/STLExtras.h" 36 #include "llvm/ADT/SmallVector.h" 37 #include "llvm/Support/Compiler.h" 38 #include "llvm/Support/ErrorHandling.h" 39 #include <optional> 40 41 namespace clang { 42 class ASTContext; 43 class CXXRecordDecl; 44 class TypeLoc; 45 class LangOptions; 46 class IdentifierInfo; 47 class NamespaceAliasDecl; 48 class NamespaceDecl; 49 class ObjCDeclSpec; 50 class Sema; 51 class Declarator; 52 struct TemplateIdAnnotation; 53 54 /// Represents a C++ nested-name-specifier or a global scope specifier. 55 /// 56 /// These can be in 3 states: 57 /// 1) Not present, identified by isEmpty() 58 /// 2) Present, identified by isNotEmpty() 59 /// 2.a) Valid, identified by isValid() 60 /// 2.b) Invalid, identified by isInvalid(). 61 /// 62 /// isSet() is deprecated because it mostly corresponded to "valid" but was 63 /// often used as if it meant "present". 64 /// 65 /// The actual scope is described by getScopeRep(). 66 /// 67 /// If the kind of getScopeRep() is TypeSpec then TemplateParamLists may be empty 68 /// or contain the template parameter lists attached to the current declaration. 69 /// Consider the following example: 70 /// template <class T> void SomeType<T>::some_method() {} 71 /// If CXXScopeSpec refers to SomeType<T> then TemplateParamLists will contain 72 /// a single element referring to template <class T>. 73 74 class CXXScopeSpec { 75 SourceRange Range; 76 NestedNameSpecifierLocBuilder Builder; 77 ArrayRef<TemplateParameterList *> TemplateParamLists; 78 79 public: getRange()80 SourceRange getRange() const { return Range; } setRange(SourceRange R)81 void setRange(SourceRange R) { Range = R; } setBeginLoc(SourceLocation Loc)82 void setBeginLoc(SourceLocation Loc) { Range.setBegin(Loc); } setEndLoc(SourceLocation Loc)83 void setEndLoc(SourceLocation Loc) { Range.setEnd(Loc); } getBeginLoc()84 SourceLocation getBeginLoc() const { return Range.getBegin(); } getEndLoc()85 SourceLocation getEndLoc() const { return Range.getEnd(); } 86 setTemplateParamLists(ArrayRef<TemplateParameterList * > L)87 void setTemplateParamLists(ArrayRef<TemplateParameterList *> L) { 88 TemplateParamLists = L; 89 } getTemplateParamLists()90 ArrayRef<TemplateParameterList *> getTemplateParamLists() const { 91 return TemplateParamLists; 92 } 93 94 /// Retrieve the representation of the nested-name-specifier. getScopeRep()95 NestedNameSpecifier *getScopeRep() const { 96 return Builder.getRepresentation(); 97 } 98 99 /// Extend the current nested-name-specifier by another 100 /// nested-name-specifier component of the form 'type::'. 101 /// 102 /// \param Context The AST context in which this nested-name-specifier 103 /// resides. 104 /// 105 /// \param TemplateKWLoc The location of the 'template' keyword, if present. 106 /// 107 /// \param TL The TypeLoc that describes the type preceding the '::'. 108 /// 109 /// \param ColonColonLoc The location of the trailing '::'. 110 void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL, 111 SourceLocation ColonColonLoc); 112 113 /// Extend the current nested-name-specifier by another 114 /// nested-name-specifier component of the form 'identifier::'. 115 /// 116 /// \param Context The AST context in which this nested-name-specifier 117 /// resides. 118 /// 119 /// \param Identifier The identifier. 120 /// 121 /// \param IdentifierLoc The location of the identifier. 122 /// 123 /// \param ColonColonLoc The location of the trailing '::'. 124 void Extend(ASTContext &Context, IdentifierInfo *Identifier, 125 SourceLocation IdentifierLoc, SourceLocation ColonColonLoc); 126 127 /// Extend the current nested-name-specifier by another 128 /// nested-name-specifier component of the form 'namespace::'. 129 /// 130 /// \param Context The AST context in which this nested-name-specifier 131 /// resides. 132 /// 133 /// \param Namespace The namespace. 134 /// 135 /// \param NamespaceLoc The location of the namespace name. 136 /// 137 /// \param ColonColonLoc The location of the trailing '::'. 138 void Extend(ASTContext &Context, NamespaceDecl *Namespace, 139 SourceLocation NamespaceLoc, SourceLocation ColonColonLoc); 140 141 /// Extend the current nested-name-specifier by another 142 /// nested-name-specifier component of the form 'namespace-alias::'. 143 /// 144 /// \param Context The AST context in which this nested-name-specifier 145 /// resides. 146 /// 147 /// \param Alias The namespace alias. 148 /// 149 /// \param AliasLoc The location of the namespace alias 150 /// name. 151 /// 152 /// \param ColonColonLoc The location of the trailing '::'. 153 void Extend(ASTContext &Context, NamespaceAliasDecl *Alias, 154 SourceLocation AliasLoc, SourceLocation ColonColonLoc); 155 156 /// Turn this (empty) nested-name-specifier into the global 157 /// nested-name-specifier '::'. 158 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc); 159 160 /// Turns this (empty) nested-name-specifier into '__super' 161 /// nested-name-specifier. 162 /// 163 /// \param Context The AST context in which this nested-name-specifier 164 /// resides. 165 /// 166 /// \param RD The declaration of the class in which nested-name-specifier 167 /// appeared. 168 /// 169 /// \param SuperLoc The location of the '__super' keyword. 170 /// name. 171 /// 172 /// \param ColonColonLoc The location of the trailing '::'. 173 void MakeSuper(ASTContext &Context, CXXRecordDecl *RD, 174 SourceLocation SuperLoc, SourceLocation ColonColonLoc); 175 176 /// Make a new nested-name-specifier from incomplete source-location 177 /// information. 178 /// 179 /// FIXME: This routine should be used very, very rarely, in cases where we 180 /// need to synthesize a nested-name-specifier. Most code should instead use 181 /// \c Adopt() with a proper \c NestedNameSpecifierLoc. 182 void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier, 183 SourceRange R); 184 185 /// Adopt an existing nested-name-specifier (with source-range 186 /// information). 187 void Adopt(NestedNameSpecifierLoc Other); 188 189 /// Retrieve a nested-name-specifier with location information, copied 190 /// into the given AST context. 191 /// 192 /// \param Context The context into which this nested-name-specifier will be 193 /// copied. 194 NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const; 195 196 /// Retrieve the location of the name in the last qualifier 197 /// in this nested name specifier. 198 /// 199 /// For example, the location of \c bar 200 /// in 201 /// \verbatim 202 /// \::foo::bar<0>:: 203 /// ^~~ 204 /// \endverbatim 205 SourceLocation getLastQualifierNameLoc() const; 206 207 /// No scope specifier. isEmpty()208 bool isEmpty() const { return Range.isInvalid() && getScopeRep() == nullptr; } 209 /// A scope specifier is present, but may be valid or invalid. isNotEmpty()210 bool isNotEmpty() const { return !isEmpty(); } 211 212 /// An error occurred during parsing of the scope specifier. isInvalid()213 bool isInvalid() const { return Range.isValid() && getScopeRep() == nullptr; } 214 /// A scope specifier is present, and it refers to a real scope. isValid()215 bool isValid() const { return getScopeRep() != nullptr; } 216 217 /// Indicate that this nested-name-specifier is invalid. SetInvalid(SourceRange R)218 void SetInvalid(SourceRange R) { 219 assert(R.isValid() && "Must have a valid source range"); 220 if (Range.getBegin().isInvalid()) 221 Range.setBegin(R.getBegin()); 222 Range.setEnd(R.getEnd()); 223 Builder.Clear(); 224 } 225 226 /// Deprecated. Some call sites intend isNotEmpty() while others intend 227 /// isValid(). isSet()228 bool isSet() const { return getScopeRep() != nullptr; } 229 clear()230 void clear() { 231 Range = SourceRange(); 232 Builder.Clear(); 233 } 234 235 /// Retrieve the data associated with the source-location information. location_data()236 char *location_data() const { return Builder.getBuffer().first; } 237 238 /// Retrieve the size of the data associated with source-location 239 /// information. location_size()240 unsigned location_size() const { return Builder.getBuffer().second; } 241 }; 242 243 /// Captures information about "declaration specifiers". 244 /// 245 /// "Declaration specifiers" encompasses storage-class-specifiers, 246 /// type-specifiers, type-qualifiers, and function-specifiers. 247 class DeclSpec { 248 public: 249 /// storage-class-specifier 250 /// \note The order of these enumerators is important for diagnostics. 251 enum SCS { 252 SCS_unspecified = 0, 253 SCS_typedef, 254 SCS_extern, 255 SCS_static, 256 SCS_auto, 257 SCS_register, 258 SCS_private_extern, 259 SCS_mutable 260 }; 261 262 // Import thread storage class specifier enumeration and constants. 263 // These can be combined with SCS_extern and SCS_static. 264 typedef ThreadStorageClassSpecifier TSCS; 265 static const TSCS TSCS_unspecified = clang::TSCS_unspecified; 266 static const TSCS TSCS___thread = clang::TSCS___thread; 267 static const TSCS TSCS_thread_local = clang::TSCS_thread_local; 268 static const TSCS TSCS__Thread_local = clang::TSCS__Thread_local; 269 270 enum TSC { 271 TSC_unspecified, 272 TSC_imaginary, // Unsupported 273 TSC_complex 274 }; 275 276 // Import type specifier type enumeration and constants. 277 typedef TypeSpecifierType TST; 278 static const TST TST_unspecified = clang::TST_unspecified; 279 static const TST TST_void = clang::TST_void; 280 static const TST TST_char = clang::TST_char; 281 static const TST TST_wchar = clang::TST_wchar; 282 static const TST TST_char8 = clang::TST_char8; 283 static const TST TST_char16 = clang::TST_char16; 284 static const TST TST_char32 = clang::TST_char32; 285 static const TST TST_int = clang::TST_int; 286 static const TST TST_int128 = clang::TST_int128; 287 static const TST TST_bitint = clang::TST_bitint; 288 static const TST TST_half = clang::TST_half; 289 static const TST TST_BFloat16 = clang::TST_BFloat16; 290 static const TST TST_float = clang::TST_float; 291 static const TST TST_double = clang::TST_double; 292 static const TST TST_float16 = clang::TST_Float16; 293 static const TST TST_accum = clang::TST_Accum; 294 static const TST TST_fract = clang::TST_Fract; 295 static const TST TST_float128 = clang::TST_float128; 296 static const TST TST_ibm128 = clang::TST_ibm128; 297 static const TST TST_bool = clang::TST_bool; 298 static const TST TST_decimal32 = clang::TST_decimal32; 299 static const TST TST_decimal64 = clang::TST_decimal64; 300 static const TST TST_decimal128 = clang::TST_decimal128; 301 static const TST TST_enum = clang::TST_enum; 302 static const TST TST_union = clang::TST_union; 303 static const TST TST_struct = clang::TST_struct; 304 static const TST TST_interface = clang::TST_interface; 305 static const TST TST_class = clang::TST_class; 306 static const TST TST_typename = clang::TST_typename; 307 static const TST TST_typeofType = clang::TST_typeofType; 308 static const TST TST_typeofExpr = clang::TST_typeofExpr; 309 static const TST TST_typeof_unqualType = clang::TST_typeof_unqualType; 310 static const TST TST_typeof_unqualExpr = clang::TST_typeof_unqualExpr; 311 static const TST TST_decltype = clang::TST_decltype; 312 static const TST TST_decltype_auto = clang::TST_decltype_auto; 313 static const TST TST_typename_pack_indexing = 314 clang::TST_typename_pack_indexing; 315 #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) \ 316 static const TST TST_##Trait = clang::TST_##Trait; 317 #include "clang/Basic/TransformTypeTraits.def" 318 static const TST TST_auto = clang::TST_auto; 319 static const TST TST_auto_type = clang::TST_auto_type; 320 static const TST TST_unknown_anytype = clang::TST_unknown_anytype; 321 static const TST TST_atomic = clang::TST_atomic; 322 #define GENERIC_IMAGE_TYPE(ImgType, Id) \ 323 static const TST TST_##ImgType##_t = clang::TST_##ImgType##_t; 324 #include "clang/Basic/OpenCLImageTypes.def" 325 static const TST TST_error = clang::TST_error; 326 327 // type-qualifiers 328 enum TQ { // NOTE: These flags must be kept in sync with Qualifiers::TQ. 329 TQ_unspecified = 0, 330 TQ_const = 1, 331 TQ_restrict = 2, 332 TQ_volatile = 4, 333 TQ_unaligned = 8, 334 // This has no corresponding Qualifiers::TQ value, because it's not treated 335 // as a qualifier in our type system. 336 TQ_atomic = 16 337 }; 338 339 /// ParsedSpecifiers - Flags to query which specifiers were applied. This is 340 /// returned by getParsedSpecifiers. 341 enum ParsedSpecifiers { 342 PQ_None = 0, 343 PQ_StorageClassSpecifier = 1, 344 PQ_TypeSpecifier = 2, 345 PQ_TypeQualifier = 4, 346 PQ_FunctionSpecifier = 8 347 // FIXME: Attributes should be included here. 348 }; 349 350 enum FriendSpecified : bool { No, Yes }; 351 352 private: 353 // storage-class-specifier 354 LLVM_PREFERRED_TYPE(SCS) 355 unsigned StorageClassSpec : 3; 356 LLVM_PREFERRED_TYPE(TSCS) 357 unsigned ThreadStorageClassSpec : 2; 358 LLVM_PREFERRED_TYPE(bool) 359 unsigned SCS_extern_in_linkage_spec : 1; 360 361 // type-specifier 362 LLVM_PREFERRED_TYPE(TypeSpecifierWidth) 363 unsigned TypeSpecWidth : 2; 364 LLVM_PREFERRED_TYPE(TSC) 365 unsigned TypeSpecComplex : 2; 366 LLVM_PREFERRED_TYPE(TypeSpecifierSign) 367 unsigned TypeSpecSign : 2; 368 LLVM_PREFERRED_TYPE(TST) 369 unsigned TypeSpecType : 7; 370 LLVM_PREFERRED_TYPE(bool) 371 unsigned TypeAltiVecVector : 1; 372 LLVM_PREFERRED_TYPE(bool) 373 unsigned TypeAltiVecPixel : 1; 374 LLVM_PREFERRED_TYPE(bool) 375 unsigned TypeAltiVecBool : 1; 376 LLVM_PREFERRED_TYPE(bool) 377 unsigned TypeSpecOwned : 1; 378 LLVM_PREFERRED_TYPE(bool) 379 unsigned TypeSpecPipe : 1; 380 LLVM_PREFERRED_TYPE(bool) 381 unsigned TypeSpecSat : 1; 382 LLVM_PREFERRED_TYPE(bool) 383 unsigned ConstrainedAuto : 1; 384 385 // type-qualifiers 386 LLVM_PREFERRED_TYPE(TQ) 387 unsigned TypeQualifiers : 5; // Bitwise OR of TQ. 388 389 // function-specifier 390 LLVM_PREFERRED_TYPE(bool) 391 unsigned FS_inline_specified : 1; 392 LLVM_PREFERRED_TYPE(bool) 393 unsigned FS_forceinline_specified: 1; 394 LLVM_PREFERRED_TYPE(bool) 395 unsigned FS_virtual_specified : 1; 396 LLVM_PREFERRED_TYPE(bool) 397 unsigned FS_noreturn_specified : 1; 398 399 // friend-specifier 400 LLVM_PREFERRED_TYPE(bool) 401 unsigned FriendSpecifiedFirst : 1; 402 403 // constexpr-specifier LLVM_PREFERRED_TYPE(ConstexprSpecKind)404 LLVM_PREFERRED_TYPE(ConstexprSpecKind) 405 unsigned ConstexprSpecifier : 2; 406 407 union { 408 UnionParsedType TypeRep; 409 Decl *DeclRep; 410 Expr *ExprRep; 411 TemplateIdAnnotation *TemplateIdRep; 412 }; 413 Expr *PackIndexingExpr = nullptr; 414 415 /// ExplicitSpecifier - Store information about explicit spicifer. 416 ExplicitSpecifier FS_explicit_specifier; 417 418 // attributes. 419 ParsedAttributes Attrs; 420 421 // Scope specifier for the type spec, if applicable. 422 CXXScopeSpec TypeScope; 423 424 // SourceLocation info. These are null if the item wasn't specified or if 425 // the setting was synthesized. 426 SourceRange Range; 427 428 SourceLocation StorageClassSpecLoc, ThreadStorageClassSpecLoc; 429 SourceRange TSWRange; 430 SourceLocation TSCLoc, TSSLoc, TSTLoc, AltiVecLoc, TSSatLoc, EllipsisLoc; 431 /// TSTNameLoc - If TypeSpecType is any of class, enum, struct, union, 432 /// typename, then this is the location of the named type (if present); 433 /// otherwise, it is the same as TSTLoc. Hence, the pair TSTLoc and 434 /// TSTNameLoc provides source range info for tag types. 435 SourceLocation TSTNameLoc; 436 SourceRange TypeofParensRange; 437 SourceLocation TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc, TQ_atomicLoc, 438 TQ_unalignedLoc; 439 SourceLocation FS_inlineLoc, FS_virtualLoc, FS_explicitLoc, FS_noreturnLoc; 440 SourceLocation FS_explicitCloseParenLoc; 441 SourceLocation FS_forceinlineLoc; 442 SourceLocation FriendLoc, ModulePrivateLoc, ConstexprLoc; 443 SourceLocation TQ_pipeLoc; 444 445 WrittenBuiltinSpecs writtenBS; 446 void SaveWrittenBuiltinSpecs(); 447 448 ObjCDeclSpec *ObjCQualifiers; 449 isTypeRep(TST T)450 static bool isTypeRep(TST T) { 451 return T == TST_atomic || T == TST_typename || T == TST_typeofType || 452 T == TST_typeof_unqualType || isTransformTypeTrait(T) || 453 T == TST_typename_pack_indexing; 454 } isExprRep(TST T)455 static bool isExprRep(TST T) { 456 return T == TST_typeofExpr || T == TST_typeof_unqualExpr || 457 T == TST_decltype || T == TST_bitint; 458 } isTemplateIdRep(TST T)459 static bool isTemplateIdRep(TST T) { 460 return (T == TST_auto || T == TST_decltype_auto); 461 } 462 463 DeclSpec(const DeclSpec &) = delete; 464 void operator=(const DeclSpec &) = delete; 465 public: isDeclRep(TST T)466 static bool isDeclRep(TST T) { 467 return (T == TST_enum || T == TST_struct || 468 T == TST_interface || T == TST_union || 469 T == TST_class); 470 } isTransformTypeTrait(TST T)471 static bool isTransformTypeTrait(TST T) { 472 constexpr std::array<TST, 16> Traits = { 473 #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) TST_##Trait, 474 #include "clang/Basic/TransformTypeTraits.def" 475 }; 476 477 return T >= Traits.front() && T <= Traits.back(); 478 } 479 DeclSpec(AttributeFactory & attrFactory)480 DeclSpec(AttributeFactory &attrFactory) 481 : StorageClassSpec(SCS_unspecified), 482 ThreadStorageClassSpec(TSCS_unspecified), 483 SCS_extern_in_linkage_spec(false), 484 TypeSpecWidth(static_cast<unsigned>(TypeSpecifierWidth::Unspecified)), 485 TypeSpecComplex(TSC_unspecified), 486 TypeSpecSign(static_cast<unsigned>(TypeSpecifierSign::Unspecified)), 487 TypeSpecType(TST_unspecified), TypeAltiVecVector(false), 488 TypeAltiVecPixel(false), TypeAltiVecBool(false), TypeSpecOwned(false), 489 TypeSpecPipe(false), TypeSpecSat(false), ConstrainedAuto(false), 490 TypeQualifiers(TQ_unspecified), FS_inline_specified(false), 491 FS_forceinline_specified(false), FS_virtual_specified(false), 492 FS_noreturn_specified(false), FriendSpecifiedFirst(false), 493 ConstexprSpecifier( 494 static_cast<unsigned>(ConstexprSpecKind::Unspecified)), 495 Attrs(attrFactory), writtenBS(), ObjCQualifiers(nullptr) {} 496 497 // storage-class-specifier getStorageClassSpec()498 SCS getStorageClassSpec() const { return (SCS)StorageClassSpec; } getThreadStorageClassSpec()499 TSCS getThreadStorageClassSpec() const { 500 return (TSCS)ThreadStorageClassSpec; 501 } isExternInLinkageSpec()502 bool isExternInLinkageSpec() const { return SCS_extern_in_linkage_spec; } setExternInLinkageSpec(bool Value)503 void setExternInLinkageSpec(bool Value) { 504 SCS_extern_in_linkage_spec = Value; 505 } 506 getStorageClassSpecLoc()507 SourceLocation getStorageClassSpecLoc() const { return StorageClassSpecLoc; } getThreadStorageClassSpecLoc()508 SourceLocation getThreadStorageClassSpecLoc() const { 509 return ThreadStorageClassSpecLoc; 510 } 511 ClearStorageClassSpecs()512 void ClearStorageClassSpecs() { 513 StorageClassSpec = DeclSpec::SCS_unspecified; 514 ThreadStorageClassSpec = DeclSpec::TSCS_unspecified; 515 SCS_extern_in_linkage_spec = false; 516 StorageClassSpecLoc = SourceLocation(); 517 ThreadStorageClassSpecLoc = SourceLocation(); 518 } 519 ClearTypeSpecType()520 void ClearTypeSpecType() { 521 TypeSpecType = DeclSpec::TST_unspecified; 522 TypeSpecOwned = false; 523 TSTLoc = SourceLocation(); 524 } 525 526 // type-specifier getTypeSpecWidth()527 TypeSpecifierWidth getTypeSpecWidth() const { 528 return static_cast<TypeSpecifierWidth>(TypeSpecWidth); 529 } getTypeSpecComplex()530 TSC getTypeSpecComplex() const { return (TSC)TypeSpecComplex; } getTypeSpecSign()531 TypeSpecifierSign getTypeSpecSign() const { 532 return static_cast<TypeSpecifierSign>(TypeSpecSign); 533 } getTypeSpecType()534 TST getTypeSpecType() const { return (TST)TypeSpecType; } isTypeAltiVecVector()535 bool isTypeAltiVecVector() const { return TypeAltiVecVector; } isTypeAltiVecPixel()536 bool isTypeAltiVecPixel() const { return TypeAltiVecPixel; } isTypeAltiVecBool()537 bool isTypeAltiVecBool() const { return TypeAltiVecBool; } isTypeSpecOwned()538 bool isTypeSpecOwned() const { return TypeSpecOwned; } isTypeRep()539 bool isTypeRep() const { return isTypeRep((TST) TypeSpecType); } isTypeSpecPipe()540 bool isTypeSpecPipe() const { return TypeSpecPipe; } isTypeSpecSat()541 bool isTypeSpecSat() const { return TypeSpecSat; } isConstrainedAuto()542 bool isConstrainedAuto() const { return ConstrainedAuto; } 543 getRepAsType()544 ParsedType getRepAsType() const { 545 assert(isTypeRep((TST) TypeSpecType) && "DeclSpec does not store a type"); 546 return TypeRep; 547 } getRepAsDecl()548 Decl *getRepAsDecl() const { 549 assert(isDeclRep((TST) TypeSpecType) && "DeclSpec does not store a decl"); 550 return DeclRep; 551 } getRepAsExpr()552 Expr *getRepAsExpr() const { 553 assert(isExprRep((TST) TypeSpecType) && "DeclSpec does not store an expr"); 554 return ExprRep; 555 } 556 getPackIndexingExpr()557 Expr *getPackIndexingExpr() const { 558 assert(TypeSpecType == TST_typename_pack_indexing && 559 "DeclSpec is not a pack indexing expr"); 560 return PackIndexingExpr; 561 } 562 getRepAsTemplateId()563 TemplateIdAnnotation *getRepAsTemplateId() const { 564 assert(isTemplateIdRep((TST) TypeSpecType) && 565 "DeclSpec does not store a template id"); 566 return TemplateIdRep; 567 } getTypeSpecScope()568 CXXScopeSpec &getTypeSpecScope() { return TypeScope; } getTypeSpecScope()569 const CXXScopeSpec &getTypeSpecScope() const { return TypeScope; } 570 getSourceRange()571 SourceRange getSourceRange() const LLVM_READONLY { return Range; } getBeginLoc()572 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); } getEndLoc()573 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); } 574 getTypeSpecWidthLoc()575 SourceLocation getTypeSpecWidthLoc() const { return TSWRange.getBegin(); } getTypeSpecWidthRange()576 SourceRange getTypeSpecWidthRange() const { return TSWRange; } getTypeSpecComplexLoc()577 SourceLocation getTypeSpecComplexLoc() const { return TSCLoc; } getTypeSpecSignLoc()578 SourceLocation getTypeSpecSignLoc() const { return TSSLoc; } getTypeSpecTypeLoc()579 SourceLocation getTypeSpecTypeLoc() const { return TSTLoc; } getAltiVecLoc()580 SourceLocation getAltiVecLoc() const { return AltiVecLoc; } getTypeSpecSatLoc()581 SourceLocation getTypeSpecSatLoc() const { return TSSatLoc; } 582 getTypeSpecTypeNameLoc()583 SourceLocation getTypeSpecTypeNameLoc() const { 584 assert(isDeclRep((TST)TypeSpecType) || isTypeRep((TST)TypeSpecType) || 585 isExprRep((TST)TypeSpecType)); 586 return TSTNameLoc; 587 } 588 getTypeofParensRange()589 SourceRange getTypeofParensRange() const { return TypeofParensRange; } setTypeArgumentRange(SourceRange range)590 void setTypeArgumentRange(SourceRange range) { TypeofParensRange = range; } 591 hasAutoTypeSpec()592 bool hasAutoTypeSpec() const { 593 return (TypeSpecType == TST_auto || TypeSpecType == TST_auto_type || 594 TypeSpecType == TST_decltype_auto); 595 } 596 597 bool hasTagDefinition() const; 598 599 /// Turn a type-specifier-type into a string like "_Bool" or "union". 600 static const char *getSpecifierName(DeclSpec::TST T, 601 const PrintingPolicy &Policy); 602 static const char *getSpecifierName(DeclSpec::TQ Q); 603 static const char *getSpecifierName(TypeSpecifierSign S); 604 static const char *getSpecifierName(DeclSpec::TSC C); 605 static const char *getSpecifierName(TypeSpecifierWidth W); 606 static const char *getSpecifierName(DeclSpec::SCS S); 607 static const char *getSpecifierName(DeclSpec::TSCS S); 608 static const char *getSpecifierName(ConstexprSpecKind C); 609 610 // type-qualifiers 611 612 /// getTypeQualifiers - Return a set of TQs. getTypeQualifiers()613 unsigned getTypeQualifiers() const { return TypeQualifiers; } getConstSpecLoc()614 SourceLocation getConstSpecLoc() const { return TQ_constLoc; } getRestrictSpecLoc()615 SourceLocation getRestrictSpecLoc() const { return TQ_restrictLoc; } getVolatileSpecLoc()616 SourceLocation getVolatileSpecLoc() const { return TQ_volatileLoc; } getAtomicSpecLoc()617 SourceLocation getAtomicSpecLoc() const { return TQ_atomicLoc; } getUnalignedSpecLoc()618 SourceLocation getUnalignedSpecLoc() const { return TQ_unalignedLoc; } getPipeLoc()619 SourceLocation getPipeLoc() const { return TQ_pipeLoc; } getEllipsisLoc()620 SourceLocation getEllipsisLoc() const { return EllipsisLoc; } 621 622 /// Clear out all of the type qualifiers. ClearTypeQualifiers()623 void ClearTypeQualifiers() { 624 TypeQualifiers = 0; 625 TQ_constLoc = SourceLocation(); 626 TQ_restrictLoc = SourceLocation(); 627 TQ_volatileLoc = SourceLocation(); 628 TQ_atomicLoc = SourceLocation(); 629 TQ_unalignedLoc = SourceLocation(); 630 TQ_pipeLoc = SourceLocation(); 631 } 632 633 // function-specifier isInlineSpecified()634 bool isInlineSpecified() const { 635 return FS_inline_specified | FS_forceinline_specified; 636 } getInlineSpecLoc()637 SourceLocation getInlineSpecLoc() const { 638 return FS_inline_specified ? FS_inlineLoc : FS_forceinlineLoc; 639 } 640 getExplicitSpecifier()641 ExplicitSpecifier getExplicitSpecifier() const { 642 return FS_explicit_specifier; 643 } 644 isVirtualSpecified()645 bool isVirtualSpecified() const { return FS_virtual_specified; } getVirtualSpecLoc()646 SourceLocation getVirtualSpecLoc() const { return FS_virtualLoc; } 647 hasExplicitSpecifier()648 bool hasExplicitSpecifier() const { 649 return FS_explicit_specifier.isSpecified(); 650 } getExplicitSpecLoc()651 SourceLocation getExplicitSpecLoc() const { return FS_explicitLoc; } getExplicitSpecRange()652 SourceRange getExplicitSpecRange() const { 653 return FS_explicit_specifier.getExpr() 654 ? SourceRange(FS_explicitLoc, FS_explicitCloseParenLoc) 655 : SourceRange(FS_explicitLoc); 656 } 657 isNoreturnSpecified()658 bool isNoreturnSpecified() const { return FS_noreturn_specified; } getNoreturnSpecLoc()659 SourceLocation getNoreturnSpecLoc() const { return FS_noreturnLoc; } 660 ClearFunctionSpecs()661 void ClearFunctionSpecs() { 662 FS_inline_specified = false; 663 FS_inlineLoc = SourceLocation(); 664 FS_forceinline_specified = false; 665 FS_forceinlineLoc = SourceLocation(); 666 FS_virtual_specified = false; 667 FS_virtualLoc = SourceLocation(); 668 FS_explicit_specifier = ExplicitSpecifier(); 669 FS_explicitLoc = SourceLocation(); 670 FS_explicitCloseParenLoc = SourceLocation(); 671 FS_noreturn_specified = false; 672 FS_noreturnLoc = SourceLocation(); 673 } 674 675 /// This method calls the passed in handler on each CVRU qual being 676 /// set. 677 /// Handle - a handler to be invoked. 678 void forEachCVRUQualifier( 679 llvm::function_ref<void(TQ, StringRef, SourceLocation)> Handle); 680 681 /// This method calls the passed in handler on each qual being 682 /// set. 683 /// Handle - a handler to be invoked. 684 void forEachQualifier( 685 llvm::function_ref<void(TQ, StringRef, SourceLocation)> Handle); 686 687 /// Return true if any type-specifier has been found. hasTypeSpecifier()688 bool hasTypeSpecifier() const { 689 return getTypeSpecType() != DeclSpec::TST_unspecified || 690 getTypeSpecWidth() != TypeSpecifierWidth::Unspecified || 691 getTypeSpecComplex() != DeclSpec::TSC_unspecified || 692 getTypeSpecSign() != TypeSpecifierSign::Unspecified; 693 } 694 695 /// Return a bitmask of which flavors of specifiers this 696 /// DeclSpec includes. 697 unsigned getParsedSpecifiers() const; 698 699 /// isEmpty - Return true if this declaration specifier is completely empty: 700 /// no tokens were parsed in the production of it. isEmpty()701 bool isEmpty() const { 702 return getParsedSpecifiers() == DeclSpec::PQ_None; 703 } 704 SetRangeStart(SourceLocation Loc)705 void SetRangeStart(SourceLocation Loc) { Range.setBegin(Loc); } SetRangeEnd(SourceLocation Loc)706 void SetRangeEnd(SourceLocation Loc) { Range.setEnd(Loc); } 707 708 /// These methods set the specified attribute of the DeclSpec and 709 /// return false if there was no error. If an error occurs (for 710 /// example, if we tried to set "auto" on a spec with "extern" 711 /// already set), they return true and set PrevSpec and DiagID 712 /// such that 713 /// Diag(Loc, DiagID) << PrevSpec; 714 /// will yield a useful result. 715 /// 716 /// TODO: use a more general approach that still allows these 717 /// diagnostics to be ignored when desired. 718 bool SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc, 719 const char *&PrevSpec, unsigned &DiagID, 720 const PrintingPolicy &Policy); 721 bool SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc, 722 const char *&PrevSpec, unsigned &DiagID); 723 bool SetTypeSpecWidth(TypeSpecifierWidth W, SourceLocation Loc, 724 const char *&PrevSpec, unsigned &DiagID, 725 const PrintingPolicy &Policy); 726 bool SetTypeSpecComplex(TSC C, SourceLocation Loc, const char *&PrevSpec, 727 unsigned &DiagID); 728 bool SetTypeSpecSign(TypeSpecifierSign S, SourceLocation Loc, 729 const char *&PrevSpec, unsigned &DiagID); 730 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, 731 unsigned &DiagID, const PrintingPolicy &Policy); 732 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, 733 unsigned &DiagID, ParsedType Rep, 734 const PrintingPolicy &Policy); SetTypeSpecType(TST T,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,TypeResult Rep,const PrintingPolicy & Policy)735 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, 736 unsigned &DiagID, TypeResult Rep, 737 const PrintingPolicy &Policy) { 738 if (Rep.isInvalid()) 739 return SetTypeSpecError(); 740 return SetTypeSpecType(T, Loc, PrevSpec, DiagID, Rep.get(), Policy); 741 } 742 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, 743 unsigned &DiagID, Decl *Rep, bool Owned, 744 const PrintingPolicy &Policy); 745 bool SetTypeSpecType(TST T, SourceLocation TagKwLoc, 746 SourceLocation TagNameLoc, const char *&PrevSpec, 747 unsigned &DiagID, ParsedType Rep, 748 const PrintingPolicy &Policy); 749 bool SetTypeSpecType(TST T, SourceLocation TagKwLoc, 750 SourceLocation TagNameLoc, const char *&PrevSpec, 751 unsigned &DiagID, Decl *Rep, bool Owned, 752 const PrintingPolicy &Policy); 753 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, 754 unsigned &DiagID, TemplateIdAnnotation *Rep, 755 const PrintingPolicy &Policy); 756 757 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, 758 unsigned &DiagID, Expr *Rep, 759 const PrintingPolicy &policy); 760 bool SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc, 761 const char *&PrevSpec, unsigned &DiagID, 762 const PrintingPolicy &Policy); 763 bool SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc, 764 const char *&PrevSpec, unsigned &DiagID, 765 const PrintingPolicy &Policy); 766 bool SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc, 767 const char *&PrevSpec, unsigned &DiagID, 768 const PrintingPolicy &Policy); 769 bool SetTypePipe(bool isPipe, SourceLocation Loc, 770 const char *&PrevSpec, unsigned &DiagID, 771 const PrintingPolicy &Policy); 772 bool SetBitIntType(SourceLocation KWLoc, Expr *BitWidth, 773 const char *&PrevSpec, unsigned &DiagID, 774 const PrintingPolicy &Policy); 775 bool SetTypeSpecSat(SourceLocation Loc, const char *&PrevSpec, 776 unsigned &DiagID); 777 778 void SetPackIndexingExpr(SourceLocation EllipsisLoc, Expr *Pack); 779 780 bool SetTypeSpecError(); UpdateDeclRep(Decl * Rep)781 void UpdateDeclRep(Decl *Rep) { 782 assert(isDeclRep((TST) TypeSpecType)); 783 DeclRep = Rep; 784 } UpdateTypeRep(ParsedType Rep)785 void UpdateTypeRep(ParsedType Rep) { 786 assert(isTypeRep((TST) TypeSpecType)); 787 TypeRep = Rep; 788 } UpdateExprRep(Expr * Rep)789 void UpdateExprRep(Expr *Rep) { 790 assert(isExprRep((TST) TypeSpecType)); 791 ExprRep = Rep; 792 } 793 794 bool SetTypeQual(TQ T, SourceLocation Loc); 795 796 bool SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec, 797 unsigned &DiagID, const LangOptions &Lang); 798 799 bool setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec, 800 unsigned &DiagID); 801 bool setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec, 802 unsigned &DiagID); 803 bool setFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec, 804 unsigned &DiagID); 805 bool setFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec, 806 unsigned &DiagID, ExplicitSpecifier ExplicitSpec, 807 SourceLocation CloseParenLoc); 808 bool setFunctionSpecNoreturn(SourceLocation Loc, const char *&PrevSpec, 809 unsigned &DiagID); 810 811 bool SetFriendSpec(SourceLocation Loc, const char *&PrevSpec, 812 unsigned &DiagID); 813 bool setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec, 814 unsigned &DiagID); 815 bool SetConstexprSpec(ConstexprSpecKind ConstexprKind, SourceLocation Loc, 816 const char *&PrevSpec, unsigned &DiagID); 817 isFriendSpecified()818 FriendSpecified isFriendSpecified() const { 819 return static_cast<FriendSpecified>(FriendLoc.isValid()); 820 } 821 isFriendSpecifiedFirst()822 bool isFriendSpecifiedFirst() const { return FriendSpecifiedFirst; } 823 getFriendSpecLoc()824 SourceLocation getFriendSpecLoc() const { return FriendLoc; } 825 isModulePrivateSpecified()826 bool isModulePrivateSpecified() const { return ModulePrivateLoc.isValid(); } getModulePrivateSpecLoc()827 SourceLocation getModulePrivateSpecLoc() const { return ModulePrivateLoc; } 828 getConstexprSpecifier()829 ConstexprSpecKind getConstexprSpecifier() const { 830 return ConstexprSpecKind(ConstexprSpecifier); 831 } 832 getConstexprSpecLoc()833 SourceLocation getConstexprSpecLoc() const { return ConstexprLoc; } hasConstexprSpecifier()834 bool hasConstexprSpecifier() const { 835 return getConstexprSpecifier() != ConstexprSpecKind::Unspecified; 836 } 837 ClearConstexprSpec()838 void ClearConstexprSpec() { 839 ConstexprSpecifier = static_cast<unsigned>(ConstexprSpecKind::Unspecified); 840 ConstexprLoc = SourceLocation(); 841 } 842 getAttributePool()843 AttributePool &getAttributePool() const { 844 return Attrs.getPool(); 845 } 846 847 /// Concatenates two attribute lists. 848 /// 849 /// The GCC attribute syntax allows for the following: 850 /// 851 /// \code 852 /// short __attribute__(( unused, deprecated )) 853 /// int __attribute__(( may_alias, aligned(16) )) var; 854 /// \endcode 855 /// 856 /// This declares 4 attributes using 2 lists. The following syntax is 857 /// also allowed and equivalent to the previous declaration. 858 /// 859 /// \code 860 /// short __attribute__((unused)) __attribute__((deprecated)) 861 /// int __attribute__((may_alias)) __attribute__((aligned(16))) var; 862 /// \endcode 863 /// addAttributes(const ParsedAttributesView & AL)864 void addAttributes(const ParsedAttributesView &AL) { 865 Attrs.addAll(AL.begin(), AL.end()); 866 } 867 hasAttributes()868 bool hasAttributes() const { return !Attrs.empty(); } 869 getAttributes()870 ParsedAttributes &getAttributes() { return Attrs; } getAttributes()871 const ParsedAttributes &getAttributes() const { return Attrs; } 872 takeAttributesFrom(ParsedAttributes & attrs)873 void takeAttributesFrom(ParsedAttributes &attrs) { 874 Attrs.takeAllFrom(attrs); 875 } 876 877 /// Finish - This does final analysis of the declspec, issuing diagnostics for 878 /// things like "_Complex" (lacking an FP type). After calling this method, 879 /// DeclSpec is guaranteed self-consistent, even if an error occurred. 880 void Finish(Sema &S, const PrintingPolicy &Policy); 881 getWrittenBuiltinSpecs()882 const WrittenBuiltinSpecs& getWrittenBuiltinSpecs() const { 883 return writtenBS; 884 } 885 getObjCQualifiers()886 ObjCDeclSpec *getObjCQualifiers() const { return ObjCQualifiers; } setObjCQualifiers(ObjCDeclSpec * quals)887 void setObjCQualifiers(ObjCDeclSpec *quals) { ObjCQualifiers = quals; } 888 889 /// Checks if this DeclSpec can stand alone, without a Declarator. 890 /// 891 /// Only tag declspecs can stand alone. 892 bool isMissingDeclaratorOk(); 893 }; 894 895 /// Captures information about "declaration specifiers" specific to 896 /// Objective-C. 897 class ObjCDeclSpec { 898 public: 899 /// ObjCDeclQualifier - Qualifier used on types in method 900 /// declarations. Not all combinations are sensible. Parameters 901 /// can be one of { in, out, inout } with one of { bycopy, byref }. 902 /// Returns can either be { oneway } or not. 903 /// 904 /// This should be kept in sync with Decl::ObjCDeclQualifier. 905 enum ObjCDeclQualifier { 906 DQ_None = 0x0, 907 DQ_In = 0x1, 908 DQ_Inout = 0x2, 909 DQ_Out = 0x4, 910 DQ_Bycopy = 0x8, 911 DQ_Byref = 0x10, 912 DQ_Oneway = 0x20, 913 DQ_CSNullability = 0x40 914 }; 915 ObjCDeclSpec()916 ObjCDeclSpec() 917 : objcDeclQualifier(DQ_None), 918 PropertyAttributes(ObjCPropertyAttribute::kind_noattr), Nullability(0), 919 GetterName(nullptr), SetterName(nullptr) {} 920 getObjCDeclQualifier()921 ObjCDeclQualifier getObjCDeclQualifier() const { 922 return (ObjCDeclQualifier)objcDeclQualifier; 923 } setObjCDeclQualifier(ObjCDeclQualifier DQVal)924 void setObjCDeclQualifier(ObjCDeclQualifier DQVal) { 925 objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier | DQVal); 926 } clearObjCDeclQualifier(ObjCDeclQualifier DQVal)927 void clearObjCDeclQualifier(ObjCDeclQualifier DQVal) { 928 objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier & ~DQVal); 929 } 930 getPropertyAttributes()931 ObjCPropertyAttribute::Kind getPropertyAttributes() const { 932 return ObjCPropertyAttribute::Kind(PropertyAttributes); 933 } setPropertyAttributes(ObjCPropertyAttribute::Kind PRVal)934 void setPropertyAttributes(ObjCPropertyAttribute::Kind PRVal) { 935 PropertyAttributes = 936 (ObjCPropertyAttribute::Kind)(PropertyAttributes | PRVal); 937 } 938 getNullability()939 NullabilityKind getNullability() const { 940 assert( 941 ((getObjCDeclQualifier() & DQ_CSNullability) || 942 (getPropertyAttributes() & ObjCPropertyAttribute::kind_nullability)) && 943 "Objective-C declspec doesn't have nullability"); 944 return static_cast<NullabilityKind>(Nullability); 945 } 946 getNullabilityLoc()947 SourceLocation getNullabilityLoc() const { 948 assert( 949 ((getObjCDeclQualifier() & DQ_CSNullability) || 950 (getPropertyAttributes() & ObjCPropertyAttribute::kind_nullability)) && 951 "Objective-C declspec doesn't have nullability"); 952 return NullabilityLoc; 953 } 954 setNullability(SourceLocation loc,NullabilityKind kind)955 void setNullability(SourceLocation loc, NullabilityKind kind) { 956 assert( 957 ((getObjCDeclQualifier() & DQ_CSNullability) || 958 (getPropertyAttributes() & ObjCPropertyAttribute::kind_nullability)) && 959 "Set the nullability declspec or property attribute first"); 960 Nullability = static_cast<unsigned>(kind); 961 NullabilityLoc = loc; 962 } 963 getGetterName()964 const IdentifierInfo *getGetterName() const { return GetterName; } getGetterName()965 IdentifierInfo *getGetterName() { return GetterName; } getGetterNameLoc()966 SourceLocation getGetterNameLoc() const { return GetterNameLoc; } setGetterName(IdentifierInfo * name,SourceLocation loc)967 void setGetterName(IdentifierInfo *name, SourceLocation loc) { 968 GetterName = name; 969 GetterNameLoc = loc; 970 } 971 getSetterName()972 const IdentifierInfo *getSetterName() const { return SetterName; } getSetterName()973 IdentifierInfo *getSetterName() { return SetterName; } getSetterNameLoc()974 SourceLocation getSetterNameLoc() const { return SetterNameLoc; } setSetterName(IdentifierInfo * name,SourceLocation loc)975 void setSetterName(IdentifierInfo *name, SourceLocation loc) { 976 SetterName = name; 977 SetterNameLoc = loc; 978 } 979 980 private: 981 // FIXME: These two are unrelated and mutually exclusive. So perhaps 982 // we can put them in a union to reflect their mutual exclusivity 983 // (space saving is negligible). 984 unsigned objcDeclQualifier : 7; 985 986 // NOTE: VC++ treats enums as signed, avoid using ObjCPropertyAttribute::Kind 987 unsigned PropertyAttributes : NumObjCPropertyAttrsBits; 988 989 unsigned Nullability : 2; 990 991 SourceLocation NullabilityLoc; 992 993 IdentifierInfo *GetterName; // getter name or NULL if no getter 994 IdentifierInfo *SetterName; // setter name or NULL if no setter 995 SourceLocation GetterNameLoc; // location of the getter attribute's value 996 SourceLocation SetterNameLoc; // location of the setter attribute's value 997 998 }; 999 1000 /// Describes the kind of unqualified-id parsed. 1001 enum class UnqualifiedIdKind { 1002 /// An identifier. 1003 IK_Identifier, 1004 /// An overloaded operator name, e.g., operator+. 1005 IK_OperatorFunctionId, 1006 /// A conversion function name, e.g., operator int. 1007 IK_ConversionFunctionId, 1008 /// A user-defined literal name, e.g., operator "" _i. 1009 IK_LiteralOperatorId, 1010 /// A constructor name. 1011 IK_ConstructorName, 1012 /// A constructor named via a template-id. 1013 IK_ConstructorTemplateId, 1014 /// A destructor name. 1015 IK_DestructorName, 1016 /// A template-id, e.g., f<int>. 1017 IK_TemplateId, 1018 /// An implicit 'self' parameter 1019 IK_ImplicitSelfParam, 1020 /// A deduction-guide name (a template-name) 1021 IK_DeductionGuideName 1022 }; 1023 1024 /// Represents a C++ unqualified-id that has been parsed. 1025 class UnqualifiedId { 1026 private: 1027 UnqualifiedId(const UnqualifiedId &Other) = delete; 1028 const UnqualifiedId &operator=(const UnqualifiedId &) = delete; 1029 1030 /// Describes the kind of unqualified-id parsed. 1031 UnqualifiedIdKind Kind; 1032 1033 public: 1034 struct OFI { 1035 /// The kind of overloaded operator. 1036 OverloadedOperatorKind Operator; 1037 1038 /// The source locations of the individual tokens that name 1039 /// the operator, e.g., the "new", "[", and "]" tokens in 1040 /// operator new []. 1041 /// 1042 /// Different operators have different numbers of tokens in their name, 1043 /// up to three. Any remaining source locations in this array will be 1044 /// set to an invalid value for operators with fewer than three tokens. 1045 SourceLocation SymbolLocations[3]; 1046 }; 1047 1048 /// Anonymous union that holds extra data associated with the 1049 /// parsed unqualified-id. 1050 union { 1051 /// When Kind == IK_Identifier, the parsed identifier, or when 1052 /// Kind == IK_UserLiteralId, the identifier suffix. 1053 const IdentifierInfo *Identifier; 1054 1055 /// When Kind == IK_OperatorFunctionId, the overloaded operator 1056 /// that we parsed. 1057 struct OFI OperatorFunctionId; 1058 1059 /// When Kind == IK_ConversionFunctionId, the type that the 1060 /// conversion function names. 1061 UnionParsedType ConversionFunctionId; 1062 1063 /// When Kind == IK_ConstructorName, the class-name of the type 1064 /// whose constructor is being referenced. 1065 UnionParsedType ConstructorName; 1066 1067 /// When Kind == IK_DestructorName, the type referred to by the 1068 /// class-name. 1069 UnionParsedType DestructorName; 1070 1071 /// When Kind == IK_DeductionGuideName, the parsed template-name. 1072 UnionParsedTemplateTy TemplateName; 1073 1074 /// When Kind == IK_TemplateId or IK_ConstructorTemplateId, 1075 /// the template-id annotation that contains the template name and 1076 /// template arguments. 1077 TemplateIdAnnotation *TemplateId; 1078 }; 1079 1080 /// The location of the first token that describes this unqualified-id, 1081 /// which will be the location of the identifier, "operator" keyword, 1082 /// tilde (for a destructor), or the template name of a template-id. 1083 SourceLocation StartLocation; 1084 1085 /// The location of the last token that describes this unqualified-id. 1086 SourceLocation EndLocation; 1087 UnqualifiedId()1088 UnqualifiedId() 1089 : Kind(UnqualifiedIdKind::IK_Identifier), Identifier(nullptr) {} 1090 1091 /// Clear out this unqualified-id, setting it to default (invalid) 1092 /// state. clear()1093 void clear() { 1094 Kind = UnqualifiedIdKind::IK_Identifier; 1095 Identifier = nullptr; 1096 StartLocation = SourceLocation(); 1097 EndLocation = SourceLocation(); 1098 } 1099 1100 /// Determine whether this unqualified-id refers to a valid name. isValid()1101 bool isValid() const { return StartLocation.isValid(); } 1102 1103 /// Determine whether this unqualified-id refers to an invalid name. isInvalid()1104 bool isInvalid() const { return !isValid(); } 1105 1106 /// Determine what kind of name we have. getKind()1107 UnqualifiedIdKind getKind() const { return Kind; } 1108 1109 /// Specify that this unqualified-id was parsed as an identifier. 1110 /// 1111 /// \param Id the parsed identifier. 1112 /// \param IdLoc the location of the parsed identifier. setIdentifier(const IdentifierInfo * Id,SourceLocation IdLoc)1113 void setIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc) { 1114 Kind = UnqualifiedIdKind::IK_Identifier; 1115 Identifier = Id; 1116 StartLocation = EndLocation = IdLoc; 1117 } 1118 1119 /// Specify that this unqualified-id was parsed as an 1120 /// operator-function-id. 1121 /// 1122 /// \param OperatorLoc the location of the 'operator' keyword. 1123 /// 1124 /// \param Op the overloaded operator. 1125 /// 1126 /// \param SymbolLocations the locations of the individual operator symbols 1127 /// in the operator. 1128 void setOperatorFunctionId(SourceLocation OperatorLoc, 1129 OverloadedOperatorKind Op, 1130 SourceLocation SymbolLocations[3]); 1131 1132 /// Specify that this unqualified-id was parsed as a 1133 /// conversion-function-id. 1134 /// 1135 /// \param OperatorLoc the location of the 'operator' keyword. 1136 /// 1137 /// \param Ty the type to which this conversion function is converting. 1138 /// 1139 /// \param EndLoc the location of the last token that makes up the type name. setConversionFunctionId(SourceLocation OperatorLoc,ParsedType Ty,SourceLocation EndLoc)1140 void setConversionFunctionId(SourceLocation OperatorLoc, 1141 ParsedType Ty, 1142 SourceLocation EndLoc) { 1143 Kind = UnqualifiedIdKind::IK_ConversionFunctionId; 1144 StartLocation = OperatorLoc; 1145 EndLocation = EndLoc; 1146 ConversionFunctionId = Ty; 1147 } 1148 1149 /// Specific that this unqualified-id was parsed as a 1150 /// literal-operator-id. 1151 /// 1152 /// \param Id the parsed identifier. 1153 /// 1154 /// \param OpLoc the location of the 'operator' keyword. 1155 /// 1156 /// \param IdLoc the location of the identifier. setLiteralOperatorId(const IdentifierInfo * Id,SourceLocation OpLoc,SourceLocation IdLoc)1157 void setLiteralOperatorId(const IdentifierInfo *Id, SourceLocation OpLoc, 1158 SourceLocation IdLoc) { 1159 Kind = UnqualifiedIdKind::IK_LiteralOperatorId; 1160 Identifier = Id; 1161 StartLocation = OpLoc; 1162 EndLocation = IdLoc; 1163 } 1164 1165 /// Specify that this unqualified-id was parsed as a constructor name. 1166 /// 1167 /// \param ClassType the class type referred to by the constructor name. 1168 /// 1169 /// \param ClassNameLoc the location of the class name. 1170 /// 1171 /// \param EndLoc the location of the last token that makes up the type name. setConstructorName(ParsedType ClassType,SourceLocation ClassNameLoc,SourceLocation EndLoc)1172 void setConstructorName(ParsedType ClassType, 1173 SourceLocation ClassNameLoc, 1174 SourceLocation EndLoc) { 1175 Kind = UnqualifiedIdKind::IK_ConstructorName; 1176 StartLocation = ClassNameLoc; 1177 EndLocation = EndLoc; 1178 ConstructorName = ClassType; 1179 } 1180 1181 /// Specify that this unqualified-id was parsed as a 1182 /// template-id that names a constructor. 1183 /// 1184 /// \param TemplateId the template-id annotation that describes the parsed 1185 /// template-id. This UnqualifiedId instance will take ownership of the 1186 /// \p TemplateId and will free it on destruction. 1187 void setConstructorTemplateId(TemplateIdAnnotation *TemplateId); 1188 1189 /// Specify that this unqualified-id was parsed as a destructor name. 1190 /// 1191 /// \param TildeLoc the location of the '~' that introduces the destructor 1192 /// name. 1193 /// 1194 /// \param ClassType the name of the class referred to by the destructor name. setDestructorName(SourceLocation TildeLoc,ParsedType ClassType,SourceLocation EndLoc)1195 void setDestructorName(SourceLocation TildeLoc, 1196 ParsedType ClassType, 1197 SourceLocation EndLoc) { 1198 Kind = UnqualifiedIdKind::IK_DestructorName; 1199 StartLocation = TildeLoc; 1200 EndLocation = EndLoc; 1201 DestructorName = ClassType; 1202 } 1203 1204 /// Specify that this unqualified-id was parsed as a template-id. 1205 /// 1206 /// \param TemplateId the template-id annotation that describes the parsed 1207 /// template-id. This UnqualifiedId instance will take ownership of the 1208 /// \p TemplateId and will free it on destruction. 1209 void setTemplateId(TemplateIdAnnotation *TemplateId); 1210 1211 /// Specify that this unqualified-id was parsed as a template-name for 1212 /// a deduction-guide. 1213 /// 1214 /// \param Template The parsed template-name. 1215 /// \param TemplateLoc The location of the parsed template-name. setDeductionGuideName(ParsedTemplateTy Template,SourceLocation TemplateLoc)1216 void setDeductionGuideName(ParsedTemplateTy Template, 1217 SourceLocation TemplateLoc) { 1218 Kind = UnqualifiedIdKind::IK_DeductionGuideName; 1219 TemplateName = Template; 1220 StartLocation = EndLocation = TemplateLoc; 1221 } 1222 1223 /// Specify that this unqualified-id is an implicit 'self' 1224 /// parameter. 1225 /// 1226 /// \param Id the identifier. setImplicitSelfParam(const IdentifierInfo * Id)1227 void setImplicitSelfParam(const IdentifierInfo *Id) { 1228 Kind = UnqualifiedIdKind::IK_ImplicitSelfParam; 1229 Identifier = Id; 1230 StartLocation = EndLocation = SourceLocation(); 1231 } 1232 1233 /// Return the source range that covers this unqualified-id. getSourceRange()1234 SourceRange getSourceRange() const LLVM_READONLY { 1235 return SourceRange(StartLocation, EndLocation); 1236 } getBeginLoc()1237 SourceLocation getBeginLoc() const LLVM_READONLY { return StartLocation; } getEndLoc()1238 SourceLocation getEndLoc() const LLVM_READONLY { return EndLocation; } 1239 }; 1240 1241 /// A set of tokens that has been cached for later parsing. 1242 typedef SmallVector<Token, 4> CachedTokens; 1243 1244 /// One instance of this struct is used for each type in a 1245 /// declarator that is parsed. 1246 /// 1247 /// This is intended to be a small value object. 1248 struct DeclaratorChunk { DeclaratorChunkDeclaratorChunk1249 DeclaratorChunk() {}; 1250 1251 enum { 1252 Pointer, Reference, Array, Function, BlockPointer, MemberPointer, Paren, Pipe 1253 } Kind; 1254 1255 /// Loc - The place where this type was defined. 1256 SourceLocation Loc; 1257 /// EndLoc - If valid, the place where this chunck ends. 1258 SourceLocation EndLoc; 1259 getSourceRangeDeclaratorChunk1260 SourceRange getSourceRange() const { 1261 if (EndLoc.isInvalid()) 1262 return SourceRange(Loc, Loc); 1263 return SourceRange(Loc, EndLoc); 1264 } 1265 1266 ParsedAttributesView AttrList; 1267 1268 struct PointerTypeInfo { 1269 /// The type qualifiers: const/volatile/restrict/unaligned/atomic. 1270 LLVM_PREFERRED_TYPE(DeclSpec::TQ) 1271 unsigned TypeQuals : 5; 1272 1273 /// The location of the const-qualifier, if any. 1274 SourceLocation ConstQualLoc; 1275 1276 /// The location of the volatile-qualifier, if any. 1277 SourceLocation VolatileQualLoc; 1278 1279 /// The location of the restrict-qualifier, if any. 1280 SourceLocation RestrictQualLoc; 1281 1282 /// The location of the _Atomic-qualifier, if any. 1283 SourceLocation AtomicQualLoc; 1284 1285 /// The location of the __unaligned-qualifier, if any. 1286 SourceLocation UnalignedQualLoc; 1287 destroyDeclaratorChunk::PointerTypeInfo1288 void destroy() { 1289 } 1290 }; 1291 1292 struct ReferenceTypeInfo { 1293 /// The type qualifier: restrict. [GNU] C++ extension 1294 bool HasRestrict : 1; 1295 /// True if this is an lvalue reference, false if it's an rvalue reference. 1296 bool LValueRef : 1; destroyDeclaratorChunk::ReferenceTypeInfo1297 void destroy() { 1298 } 1299 }; 1300 1301 struct ArrayTypeInfo { 1302 /// The type qualifiers for the array: 1303 /// const/volatile/restrict/__unaligned/_Atomic. 1304 LLVM_PREFERRED_TYPE(DeclSpec::TQ) 1305 unsigned TypeQuals : 5; 1306 1307 /// True if this dimension included the 'static' keyword. 1308 LLVM_PREFERRED_TYPE(bool) 1309 unsigned hasStatic : 1; 1310 1311 /// True if this dimension was [*]. In this case, NumElts is null. 1312 LLVM_PREFERRED_TYPE(bool) 1313 unsigned isStar : 1; 1314 1315 /// This is the size of the array, or null if [] or [*] was specified. 1316 /// Since the parser is multi-purpose, and we don't want to impose a root 1317 /// expression class on all clients, NumElts is untyped. 1318 Expr *NumElts; 1319 destroyDeclaratorChunk::ArrayTypeInfo1320 void destroy() {} 1321 }; 1322 1323 /// ParamInfo - An array of paraminfo objects is allocated whenever a function 1324 /// declarator is parsed. There are two interesting styles of parameters 1325 /// here: 1326 /// K&R-style identifier lists and parameter type lists. K&R-style identifier 1327 /// lists will have information about the identifier, but no type information. 1328 /// Parameter type lists will have type info (if the actions module provides 1329 /// it), but may have null identifier info: e.g. for 'void foo(int X, int)'. 1330 struct ParamInfo { 1331 const IdentifierInfo *Ident; 1332 SourceLocation IdentLoc; 1333 Decl *Param; 1334 1335 /// DefaultArgTokens - When the parameter's default argument 1336 /// cannot be parsed immediately (because it occurs within the 1337 /// declaration of a member function), it will be stored here as a 1338 /// sequence of tokens to be parsed once the class definition is 1339 /// complete. Non-NULL indicates that there is a default argument. 1340 std::unique_ptr<CachedTokens> DefaultArgTokens; 1341 1342 ParamInfo() = default; 1343 ParamInfo(const IdentifierInfo *ident, SourceLocation iloc, Decl *param, 1344 std::unique_ptr<CachedTokens> DefArgTokens = nullptr) IdentDeclaratorChunk::ParamInfo1345 : Ident(ident), IdentLoc(iloc), Param(param), 1346 DefaultArgTokens(std::move(DefArgTokens)) {} 1347 }; 1348 1349 struct TypeAndRange { 1350 ParsedType Ty; 1351 SourceRange Range; 1352 }; 1353 1354 struct FunctionTypeInfo { 1355 /// hasPrototype - This is true if the function had at least one typed 1356 /// parameter. If the function is () or (a,b,c), then it has no prototype, 1357 /// and is treated as a K&R-style function. 1358 LLVM_PREFERRED_TYPE(bool) 1359 unsigned hasPrototype : 1; 1360 1361 /// isVariadic - If this function has a prototype, and if that 1362 /// proto ends with ',...)', this is true. When true, EllipsisLoc 1363 /// contains the location of the ellipsis. 1364 LLVM_PREFERRED_TYPE(bool) 1365 unsigned isVariadic : 1; 1366 1367 /// Can this declaration be a constructor-style initializer? 1368 LLVM_PREFERRED_TYPE(bool) 1369 unsigned isAmbiguous : 1; 1370 1371 /// Whether the ref-qualifier (if any) is an lvalue reference. 1372 /// Otherwise, it's an rvalue reference. 1373 LLVM_PREFERRED_TYPE(bool) 1374 unsigned RefQualifierIsLValueRef : 1; 1375 1376 /// ExceptionSpecType - An ExceptionSpecificationType value. 1377 LLVM_PREFERRED_TYPE(ExceptionSpecificationType) 1378 unsigned ExceptionSpecType : 4; 1379 1380 /// DeleteParams - If this is true, we need to delete[] Params. 1381 LLVM_PREFERRED_TYPE(bool) 1382 unsigned DeleteParams : 1; 1383 1384 /// HasTrailingReturnType - If this is true, a trailing return type was 1385 /// specified. 1386 LLVM_PREFERRED_TYPE(bool) 1387 unsigned HasTrailingReturnType : 1; 1388 1389 /// The location of the left parenthesis in the source. 1390 SourceLocation LParenLoc; 1391 1392 /// When isVariadic is true, the location of the ellipsis in the source. 1393 SourceLocation EllipsisLoc; 1394 1395 /// The location of the right parenthesis in the source. 1396 SourceLocation RParenLoc; 1397 1398 /// NumParams - This is the number of formal parameters specified by the 1399 /// declarator. 1400 unsigned NumParams; 1401 1402 /// NumExceptionsOrDecls - This is the number of types in the 1403 /// dynamic-exception-decl, if the function has one. In C, this is the 1404 /// number of declarations in the function prototype. 1405 unsigned NumExceptionsOrDecls; 1406 1407 /// The location of the ref-qualifier, if any. 1408 /// 1409 /// If this is an invalid location, there is no ref-qualifier. 1410 SourceLocation RefQualifierLoc; 1411 1412 /// The location of the 'mutable' qualifer in a lambda-declarator, if 1413 /// any. 1414 SourceLocation MutableLoc; 1415 1416 /// The beginning location of the exception specification, if any. 1417 SourceLocation ExceptionSpecLocBeg; 1418 1419 /// The end location of the exception specification, if any. 1420 SourceLocation ExceptionSpecLocEnd; 1421 1422 /// Params - This is a pointer to a new[]'d array of ParamInfo objects that 1423 /// describe the parameters specified by this function declarator. null if 1424 /// there are no parameters specified. 1425 ParamInfo *Params; 1426 1427 /// DeclSpec for the function with the qualifier related info. 1428 DeclSpec *MethodQualifiers; 1429 1430 /// AttributeFactory for the MethodQualifiers. 1431 AttributeFactory *QualAttrFactory; 1432 1433 union { 1434 /// Pointer to a new[]'d array of TypeAndRange objects that 1435 /// contain the types in the function's dynamic exception specification 1436 /// and their locations, if there is one. 1437 TypeAndRange *Exceptions; 1438 1439 /// Pointer to the expression in the noexcept-specifier of this 1440 /// function, if it has one. 1441 Expr *NoexceptExpr; 1442 1443 /// Pointer to the cached tokens for an exception-specification 1444 /// that has not yet been parsed. 1445 CachedTokens *ExceptionSpecTokens; 1446 1447 /// Pointer to a new[]'d array of declarations that need to be available 1448 /// for lookup inside the function body, if one exists. Does not exist in 1449 /// C++. 1450 NamedDecl **DeclsInPrototype; 1451 }; 1452 1453 /// If HasTrailingReturnType is true, this is the trailing return 1454 /// type specified. 1455 UnionParsedType TrailingReturnType; 1456 1457 /// If HasTrailingReturnType is true, this is the location of the trailing 1458 /// return type. 1459 SourceLocation TrailingReturnTypeLoc; 1460 1461 /// Reset the parameter list to having zero parameters. 1462 /// 1463 /// This is used in various places for error recovery. freeParamsDeclaratorChunk::FunctionTypeInfo1464 void freeParams() { 1465 for (unsigned I = 0; I < NumParams; ++I) 1466 Params[I].DefaultArgTokens.reset(); 1467 if (DeleteParams) { 1468 delete[] Params; 1469 DeleteParams = false; 1470 } 1471 NumParams = 0; 1472 } 1473 destroyDeclaratorChunk::FunctionTypeInfo1474 void destroy() { 1475 freeParams(); 1476 delete QualAttrFactory; 1477 delete MethodQualifiers; 1478 switch (getExceptionSpecType()) { 1479 default: 1480 break; 1481 case EST_Dynamic: 1482 delete[] Exceptions; 1483 break; 1484 case EST_Unparsed: 1485 delete ExceptionSpecTokens; 1486 break; 1487 case EST_None: 1488 if (NumExceptionsOrDecls != 0) 1489 delete[] DeclsInPrototype; 1490 break; 1491 } 1492 } 1493 getOrCreateMethodQualifiersDeclaratorChunk::FunctionTypeInfo1494 DeclSpec &getOrCreateMethodQualifiers() { 1495 if (!MethodQualifiers) { 1496 QualAttrFactory = new AttributeFactory(); 1497 MethodQualifiers = new DeclSpec(*QualAttrFactory); 1498 } 1499 return *MethodQualifiers; 1500 } 1501 1502 /// isKNRPrototype - Return true if this is a K&R style identifier list, 1503 /// like "void foo(a,b,c)". In a function definition, this will be followed 1504 /// by the parameter type definitions. isKNRPrototypeDeclaratorChunk::FunctionTypeInfo1505 bool isKNRPrototype() const { return !hasPrototype && NumParams != 0; } 1506 getLParenLocDeclaratorChunk::FunctionTypeInfo1507 SourceLocation getLParenLoc() const { return LParenLoc; } 1508 getEllipsisLocDeclaratorChunk::FunctionTypeInfo1509 SourceLocation getEllipsisLoc() const { return EllipsisLoc; } 1510 getRParenLocDeclaratorChunk::FunctionTypeInfo1511 SourceLocation getRParenLoc() const { return RParenLoc; } 1512 getExceptionSpecLocBegDeclaratorChunk::FunctionTypeInfo1513 SourceLocation getExceptionSpecLocBeg() const { 1514 return ExceptionSpecLocBeg; 1515 } 1516 getExceptionSpecLocEndDeclaratorChunk::FunctionTypeInfo1517 SourceLocation getExceptionSpecLocEnd() const { 1518 return ExceptionSpecLocEnd; 1519 } 1520 getExceptionSpecRangeDeclaratorChunk::FunctionTypeInfo1521 SourceRange getExceptionSpecRange() const { 1522 return SourceRange(getExceptionSpecLocBeg(), getExceptionSpecLocEnd()); 1523 } 1524 1525 /// Retrieve the location of the ref-qualifier, if any. getRefQualifierLocDeclaratorChunk::FunctionTypeInfo1526 SourceLocation getRefQualifierLoc() const { return RefQualifierLoc; } 1527 1528 /// Retrieve the location of the 'const' qualifier. getConstQualifierLocDeclaratorChunk::FunctionTypeInfo1529 SourceLocation getConstQualifierLoc() const { 1530 assert(MethodQualifiers); 1531 return MethodQualifiers->getConstSpecLoc(); 1532 } 1533 1534 /// Retrieve the location of the 'volatile' qualifier. getVolatileQualifierLocDeclaratorChunk::FunctionTypeInfo1535 SourceLocation getVolatileQualifierLoc() const { 1536 assert(MethodQualifiers); 1537 return MethodQualifiers->getVolatileSpecLoc(); 1538 } 1539 1540 /// Retrieve the location of the 'restrict' qualifier. getRestrictQualifierLocDeclaratorChunk::FunctionTypeInfo1541 SourceLocation getRestrictQualifierLoc() const { 1542 assert(MethodQualifiers); 1543 return MethodQualifiers->getRestrictSpecLoc(); 1544 } 1545 1546 /// Retrieve the location of the 'mutable' qualifier, if any. getMutableLocDeclaratorChunk::FunctionTypeInfo1547 SourceLocation getMutableLoc() const { return MutableLoc; } 1548 1549 /// Determine whether this function declaration contains a 1550 /// ref-qualifier. hasRefQualifierDeclaratorChunk::FunctionTypeInfo1551 bool hasRefQualifier() const { return getRefQualifierLoc().isValid(); } 1552 1553 /// Determine whether this lambda-declarator contains a 'mutable' 1554 /// qualifier. hasMutableQualifierDeclaratorChunk::FunctionTypeInfo1555 bool hasMutableQualifier() const { return getMutableLoc().isValid(); } 1556 1557 /// Determine whether this method has qualifiers. hasMethodTypeQualifiersDeclaratorChunk::FunctionTypeInfo1558 bool hasMethodTypeQualifiers() const { 1559 return MethodQualifiers && (MethodQualifiers->getTypeQualifiers() || 1560 MethodQualifiers->getAttributes().size()); 1561 } 1562 1563 /// Get the type of exception specification this function has. getExceptionSpecTypeDeclaratorChunk::FunctionTypeInfo1564 ExceptionSpecificationType getExceptionSpecType() const { 1565 return static_cast<ExceptionSpecificationType>(ExceptionSpecType); 1566 } 1567 1568 /// Get the number of dynamic exception specifications. getNumExceptionsDeclaratorChunk::FunctionTypeInfo1569 unsigned getNumExceptions() const { 1570 assert(ExceptionSpecType != EST_None); 1571 return NumExceptionsOrDecls; 1572 } 1573 1574 /// Get the non-parameter decls defined within this function 1575 /// prototype. Typically these are tag declarations. getDeclsInPrototypeDeclaratorChunk::FunctionTypeInfo1576 ArrayRef<NamedDecl *> getDeclsInPrototype() const { 1577 assert(ExceptionSpecType == EST_None); 1578 return llvm::ArrayRef(DeclsInPrototype, NumExceptionsOrDecls); 1579 } 1580 1581 /// Determine whether this function declarator had a 1582 /// trailing-return-type. hasTrailingReturnTypeDeclaratorChunk::FunctionTypeInfo1583 bool hasTrailingReturnType() const { return HasTrailingReturnType; } 1584 1585 /// Get the trailing-return-type for this function declarator. getTrailingReturnTypeDeclaratorChunk::FunctionTypeInfo1586 ParsedType getTrailingReturnType() const { 1587 assert(HasTrailingReturnType); 1588 return TrailingReturnType; 1589 } 1590 1591 /// Get the trailing-return-type location for this function declarator. getTrailingReturnTypeLocDeclaratorChunk::FunctionTypeInfo1592 SourceLocation getTrailingReturnTypeLoc() const { 1593 assert(HasTrailingReturnType); 1594 return TrailingReturnTypeLoc; 1595 } 1596 }; 1597 1598 struct BlockPointerTypeInfo { 1599 /// For now, sema will catch these as invalid. 1600 /// The type qualifiers: const/volatile/restrict/__unaligned/_Atomic. 1601 LLVM_PREFERRED_TYPE(DeclSpec::TQ) 1602 unsigned TypeQuals : 5; 1603 destroyDeclaratorChunk::BlockPointerTypeInfo1604 void destroy() { 1605 } 1606 }; 1607 1608 struct MemberPointerTypeInfo { 1609 /// The type qualifiers: const/volatile/restrict/__unaligned/_Atomic. 1610 LLVM_PREFERRED_TYPE(DeclSpec::TQ) 1611 unsigned TypeQuals : 5; 1612 /// Location of the '*' token. 1613 SourceLocation StarLoc; 1614 // CXXScopeSpec has a constructor, so it can't be a direct member. 1615 // So we need some pointer-aligned storage and a bit of trickery. 1616 alignas(CXXScopeSpec) char ScopeMem[sizeof(CXXScopeSpec)]; ScopeDeclaratorChunk::MemberPointerTypeInfo1617 CXXScopeSpec &Scope() { 1618 return *reinterpret_cast<CXXScopeSpec *>(ScopeMem); 1619 } ScopeDeclaratorChunk::MemberPointerTypeInfo1620 const CXXScopeSpec &Scope() const { 1621 return *reinterpret_cast<const CXXScopeSpec *>(ScopeMem); 1622 } destroyDeclaratorChunk::MemberPointerTypeInfo1623 void destroy() { 1624 Scope().~CXXScopeSpec(); 1625 } 1626 }; 1627 1628 struct PipeTypeInfo { 1629 /// The access writes. 1630 unsigned AccessWrites : 3; 1631 destroyDeclaratorChunk::PipeTypeInfo1632 void destroy() {} 1633 }; 1634 1635 union { 1636 PointerTypeInfo Ptr; 1637 ReferenceTypeInfo Ref; 1638 ArrayTypeInfo Arr; 1639 FunctionTypeInfo Fun; 1640 BlockPointerTypeInfo Cls; 1641 MemberPointerTypeInfo Mem; 1642 PipeTypeInfo PipeInfo; 1643 }; 1644 destroyDeclaratorChunk1645 void destroy() { 1646 switch (Kind) { 1647 case DeclaratorChunk::Function: return Fun.destroy(); 1648 case DeclaratorChunk::Pointer: return Ptr.destroy(); 1649 case DeclaratorChunk::BlockPointer: return Cls.destroy(); 1650 case DeclaratorChunk::Reference: return Ref.destroy(); 1651 case DeclaratorChunk::Array: return Arr.destroy(); 1652 case DeclaratorChunk::MemberPointer: return Mem.destroy(); 1653 case DeclaratorChunk::Paren: return; 1654 case DeclaratorChunk::Pipe: return PipeInfo.destroy(); 1655 } 1656 } 1657 1658 /// If there are attributes applied to this declaratorchunk, return 1659 /// them. getAttrsDeclaratorChunk1660 const ParsedAttributesView &getAttrs() const { return AttrList; } getAttrsDeclaratorChunk1661 ParsedAttributesView &getAttrs() { return AttrList; } 1662 1663 /// Return a DeclaratorChunk for a pointer. getPointerDeclaratorChunk1664 static DeclaratorChunk getPointer(unsigned TypeQuals, SourceLocation Loc, 1665 SourceLocation ConstQualLoc, 1666 SourceLocation VolatileQualLoc, 1667 SourceLocation RestrictQualLoc, 1668 SourceLocation AtomicQualLoc, 1669 SourceLocation UnalignedQualLoc) { 1670 DeclaratorChunk I; 1671 I.Kind = Pointer; 1672 I.Loc = Loc; 1673 new (&I.Ptr) PointerTypeInfo; 1674 I.Ptr.TypeQuals = TypeQuals; 1675 I.Ptr.ConstQualLoc = ConstQualLoc; 1676 I.Ptr.VolatileQualLoc = VolatileQualLoc; 1677 I.Ptr.RestrictQualLoc = RestrictQualLoc; 1678 I.Ptr.AtomicQualLoc = AtomicQualLoc; 1679 I.Ptr.UnalignedQualLoc = UnalignedQualLoc; 1680 return I; 1681 } 1682 1683 /// Return a DeclaratorChunk for a reference. getReferenceDeclaratorChunk1684 static DeclaratorChunk getReference(unsigned TypeQuals, SourceLocation Loc, 1685 bool lvalue) { 1686 DeclaratorChunk I; 1687 I.Kind = Reference; 1688 I.Loc = Loc; 1689 I.Ref.HasRestrict = (TypeQuals & DeclSpec::TQ_restrict) != 0; 1690 I.Ref.LValueRef = lvalue; 1691 return I; 1692 } 1693 1694 /// Return a DeclaratorChunk for an array. getArrayDeclaratorChunk1695 static DeclaratorChunk getArray(unsigned TypeQuals, 1696 bool isStatic, bool isStar, Expr *NumElts, 1697 SourceLocation LBLoc, SourceLocation RBLoc) { 1698 DeclaratorChunk I; 1699 I.Kind = Array; 1700 I.Loc = LBLoc; 1701 I.EndLoc = RBLoc; 1702 I.Arr.TypeQuals = TypeQuals; 1703 I.Arr.hasStatic = isStatic; 1704 I.Arr.isStar = isStar; 1705 I.Arr.NumElts = NumElts; 1706 return I; 1707 } 1708 1709 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function. 1710 /// "TheDeclarator" is the declarator that this will be added to. 1711 static DeclaratorChunk getFunction(bool HasProto, 1712 bool IsAmbiguous, 1713 SourceLocation LParenLoc, 1714 ParamInfo *Params, unsigned NumParams, 1715 SourceLocation EllipsisLoc, 1716 SourceLocation RParenLoc, 1717 bool RefQualifierIsLvalueRef, 1718 SourceLocation RefQualifierLoc, 1719 SourceLocation MutableLoc, 1720 ExceptionSpecificationType ESpecType, 1721 SourceRange ESpecRange, 1722 ParsedType *Exceptions, 1723 SourceRange *ExceptionRanges, 1724 unsigned NumExceptions, 1725 Expr *NoexceptExpr, 1726 CachedTokens *ExceptionSpecTokens, 1727 ArrayRef<NamedDecl *> DeclsInPrototype, 1728 SourceLocation LocalRangeBegin, 1729 SourceLocation LocalRangeEnd, 1730 Declarator &TheDeclarator, 1731 TypeResult TrailingReturnType = 1732 TypeResult(), 1733 SourceLocation TrailingReturnTypeLoc = 1734 SourceLocation(), 1735 DeclSpec *MethodQualifiers = nullptr); 1736 1737 /// Return a DeclaratorChunk for a block. getBlockPointerDeclaratorChunk1738 static DeclaratorChunk getBlockPointer(unsigned TypeQuals, 1739 SourceLocation Loc) { 1740 DeclaratorChunk I; 1741 I.Kind = BlockPointer; 1742 I.Loc = Loc; 1743 I.Cls.TypeQuals = TypeQuals; 1744 return I; 1745 } 1746 1747 /// Return a DeclaratorChunk for a block. getPipeDeclaratorChunk1748 static DeclaratorChunk getPipe(unsigned TypeQuals, 1749 SourceLocation Loc) { 1750 DeclaratorChunk I; 1751 I.Kind = Pipe; 1752 I.Loc = Loc; 1753 I.Cls.TypeQuals = TypeQuals; 1754 return I; 1755 } 1756 getMemberPointerDeclaratorChunk1757 static DeclaratorChunk getMemberPointer(const CXXScopeSpec &SS, 1758 unsigned TypeQuals, 1759 SourceLocation StarLoc, 1760 SourceLocation EndLoc) { 1761 DeclaratorChunk I; 1762 I.Kind = MemberPointer; 1763 I.Loc = SS.getBeginLoc(); 1764 I.EndLoc = EndLoc; 1765 new (&I.Mem) MemberPointerTypeInfo; 1766 I.Mem.StarLoc = StarLoc; 1767 I.Mem.TypeQuals = TypeQuals; 1768 new (I.Mem.ScopeMem) CXXScopeSpec(SS); 1769 return I; 1770 } 1771 1772 /// Return a DeclaratorChunk for a paren. getParenDeclaratorChunk1773 static DeclaratorChunk getParen(SourceLocation LParenLoc, 1774 SourceLocation RParenLoc) { 1775 DeclaratorChunk I; 1776 I.Kind = Paren; 1777 I.Loc = LParenLoc; 1778 I.EndLoc = RParenLoc; 1779 return I; 1780 } 1781 isParenDeclaratorChunk1782 bool isParen() const { 1783 return Kind == Paren; 1784 } 1785 }; 1786 1787 /// A parsed C++17 decomposition declarator of the form 1788 /// '[' identifier-list ']' 1789 class DecompositionDeclarator { 1790 public: 1791 struct Binding { 1792 IdentifierInfo *Name; 1793 SourceLocation NameLoc; 1794 std::optional<ParsedAttributes> Attrs; 1795 }; 1796 1797 private: 1798 /// The locations of the '[' and ']' tokens. 1799 SourceLocation LSquareLoc, RSquareLoc; 1800 1801 /// The bindings. 1802 Binding *Bindings; 1803 unsigned NumBindings : 31; 1804 LLVM_PREFERRED_TYPE(bool) 1805 unsigned DeleteBindings : 1; 1806 1807 friend class Declarator; 1808 1809 public: DecompositionDeclarator()1810 DecompositionDeclarator() 1811 : Bindings(nullptr), NumBindings(0), DeleteBindings(false) {} 1812 DecompositionDeclarator(const DecompositionDeclarator &G) = delete; 1813 DecompositionDeclarator &operator=(const DecompositionDeclarator &G) = delete; ~DecompositionDeclarator()1814 ~DecompositionDeclarator() { clear(); } 1815 clear()1816 void clear() { 1817 LSquareLoc = RSquareLoc = SourceLocation(); 1818 if (DeleteBindings) 1819 delete[] Bindings; 1820 else 1821 llvm::for_each(llvm::MutableArrayRef(Bindings, NumBindings), 1822 [](Binding &B) { B.Attrs.reset(); }); 1823 Bindings = nullptr; 1824 NumBindings = 0; 1825 DeleteBindings = false; 1826 } 1827 bindings()1828 ArrayRef<Binding> bindings() const { 1829 return llvm::ArrayRef(Bindings, NumBindings); 1830 } 1831 isSet()1832 bool isSet() const { return LSquareLoc.isValid(); } 1833 getLSquareLoc()1834 SourceLocation getLSquareLoc() const { return LSquareLoc; } getRSquareLoc()1835 SourceLocation getRSquareLoc() const { return RSquareLoc; } getSourceRange()1836 SourceRange getSourceRange() const { 1837 return SourceRange(LSquareLoc, RSquareLoc); 1838 } 1839 }; 1840 1841 /// Described the kind of function definition (if any) provided for 1842 /// a function. 1843 enum class FunctionDefinitionKind { 1844 Declaration, 1845 Definition, 1846 Defaulted, 1847 Deleted 1848 }; 1849 1850 enum class DeclaratorContext { 1851 File, // File scope declaration. 1852 Prototype, // Within a function prototype. 1853 ObjCResult, // An ObjC method result type. 1854 ObjCParameter, // An ObjC method parameter type. 1855 KNRTypeList, // K&R type definition list for formals. 1856 TypeName, // Abstract declarator for types. 1857 FunctionalCast, // Type in a C++ functional cast expression. 1858 Member, // Struct/Union field. 1859 Block, // Declaration within a block in a function. 1860 ForInit, // Declaration within first part of a for loop. 1861 SelectionInit, // Declaration within optional init stmt of if/switch. 1862 Condition, // Condition declaration in a C++ if/switch/while/for. 1863 TemplateParam, // Within a template parameter list. 1864 CXXNew, // C++ new-expression. 1865 CXXCatch, // C++ catch exception-declaration 1866 ObjCCatch, // Objective-C catch exception-declaration 1867 BlockLiteral, // Block literal declarator. 1868 LambdaExpr, // Lambda-expression declarator. 1869 LambdaExprParameter, // Lambda-expression parameter declarator. 1870 ConversionId, // C++ conversion-type-id. 1871 TrailingReturn, // C++11 trailing-type-specifier. 1872 TrailingReturnVar, // C++11 trailing-type-specifier for variable. 1873 TemplateArg, // Any template argument (in template argument list). 1874 TemplateTypeArg, // Template type argument (in default argument). 1875 AliasDecl, // C++11 alias-declaration. 1876 AliasTemplate, // C++11 alias-declaration template. 1877 RequiresExpr, // C++2a requires-expression. 1878 Association // C11 _Generic selection expression association. 1879 }; 1880 1881 // Describes whether the current context is a context where an implicit 1882 // typename is allowed (C++2a [temp.res]p5]). 1883 enum class ImplicitTypenameContext { 1884 No, 1885 Yes, 1886 }; 1887 1888 /// Information about one declarator, including the parsed type 1889 /// information and the identifier. 1890 /// 1891 /// When the declarator is fully formed, this is turned into the appropriate 1892 /// Decl object. 1893 /// 1894 /// Declarators come in two types: normal declarators and abstract declarators. 1895 /// Abstract declarators are used when parsing types, and don't have an 1896 /// identifier. Normal declarators do have ID's. 1897 /// 1898 /// Instances of this class should be a transient object that lives on the 1899 /// stack, not objects that are allocated in large quantities on the heap. 1900 class Declarator { 1901 1902 private: 1903 const DeclSpec &DS; 1904 CXXScopeSpec SS; 1905 UnqualifiedId Name; 1906 SourceRange Range; 1907 1908 /// Where we are parsing this declarator. 1909 DeclaratorContext Context; 1910 1911 /// The C++17 structured binding, if any. This is an alternative to a Name. 1912 DecompositionDeclarator BindingGroup; 1913 1914 /// DeclTypeInfo - This holds each type that the declarator includes as it is 1915 /// parsed. This is pushed from the identifier out, which means that element 1916 /// #0 will be the most closely bound to the identifier, and 1917 /// DeclTypeInfo.back() will be the least closely bound. 1918 SmallVector<DeclaratorChunk, 8> DeclTypeInfo; 1919 1920 /// InvalidType - Set by Sema::GetTypeForDeclarator(). 1921 LLVM_PREFERRED_TYPE(bool) 1922 unsigned InvalidType : 1; 1923 1924 /// GroupingParens - Set by Parser::ParseParenDeclarator(). 1925 LLVM_PREFERRED_TYPE(bool) 1926 unsigned GroupingParens : 1; 1927 1928 /// FunctionDefinition - Is this Declarator for a function or member 1929 /// definition and, if so, what kind? 1930 /// 1931 /// Actually a FunctionDefinitionKind. 1932 LLVM_PREFERRED_TYPE(FunctionDefinitionKind) 1933 unsigned FunctionDefinition : 2; 1934 1935 /// Is this Declarator a redeclaration? 1936 LLVM_PREFERRED_TYPE(bool) 1937 unsigned Redeclaration : 1; 1938 1939 /// true if the declaration is preceded by \c __extension__. 1940 LLVM_PREFERRED_TYPE(bool) 1941 unsigned Extension : 1; 1942 1943 /// Indicates whether this is an Objective-C instance variable. 1944 LLVM_PREFERRED_TYPE(bool) 1945 unsigned ObjCIvar : 1; 1946 1947 /// Indicates whether this is an Objective-C 'weak' property. 1948 LLVM_PREFERRED_TYPE(bool) 1949 unsigned ObjCWeakProperty : 1; 1950 1951 /// Indicates whether the InlineParams / InlineBindings storage has been used. 1952 LLVM_PREFERRED_TYPE(bool) 1953 unsigned InlineStorageUsed : 1; 1954 1955 /// Indicates whether this declarator has an initializer. 1956 LLVM_PREFERRED_TYPE(bool) 1957 unsigned HasInitializer : 1; 1958 1959 /// Attributes attached to the declarator. 1960 ParsedAttributes Attrs; 1961 1962 /// Attributes attached to the declaration. See also documentation for the 1963 /// corresponding constructor parameter. 1964 const ParsedAttributesView &DeclarationAttrs; 1965 1966 /// The asm label, if specified. 1967 Expr *AsmLabel; 1968 1969 /// \brief The constraint-expression specified by the trailing 1970 /// requires-clause, or null if no such clause was specified. 1971 Expr *TrailingRequiresClause; 1972 1973 /// If this declarator declares a template, its template parameter lists. 1974 ArrayRef<TemplateParameterList *> TemplateParameterLists; 1975 1976 /// If the declarator declares an abbreviated function template, the innermost 1977 /// template parameter list containing the invented and explicit template 1978 /// parameters (if any). 1979 TemplateParameterList *InventedTemplateParameterList; 1980 1981 #ifndef _MSC_VER 1982 union { 1983 #endif 1984 /// InlineParams - This is a local array used for the first function decl 1985 /// chunk to avoid going to the heap for the common case when we have one 1986 /// function chunk in the declarator. 1987 DeclaratorChunk::ParamInfo InlineParams[16]; 1988 DecompositionDeclarator::Binding InlineBindings[16]; 1989 #ifndef _MSC_VER 1990 }; 1991 #endif 1992 1993 /// If this is the second or subsequent declarator in this declaration, 1994 /// the location of the comma before this declarator. 1995 SourceLocation CommaLoc; 1996 1997 /// If provided, the source location of the ellipsis used to describe 1998 /// this declarator as a parameter pack. 1999 SourceLocation EllipsisLoc; 2000 2001 Expr *PackIndexingExpr; 2002 2003 friend struct DeclaratorChunk; 2004 2005 public: 2006 /// `DS` and `DeclarationAttrs` must outlive the `Declarator`. In particular, 2007 /// take care not to pass temporary objects for these parameters. 2008 /// 2009 /// `DeclarationAttrs` contains [[]] attributes from the 2010 /// attribute-specifier-seq at the beginning of a declaration, which appertain 2011 /// to the declared entity itself. Attributes with other syntax (e.g. GNU) 2012 /// should not be placed in this attribute list; if they occur at the 2013 /// beginning of a declaration, they apply to the `DeclSpec` and should be 2014 /// attached to that instead. 2015 /// 2016 /// Here is an example of an attribute associated with a declaration: 2017 /// 2018 /// [[deprecated]] int x, y; 2019 /// 2020 /// This attribute appertains to all of the entities declared in the 2021 /// declaration, i.e. `x` and `y` in this case. Declarator(const DeclSpec & DS,const ParsedAttributesView & DeclarationAttrs,DeclaratorContext C)2022 Declarator(const DeclSpec &DS, const ParsedAttributesView &DeclarationAttrs, 2023 DeclaratorContext C) 2024 : DS(DS), Range(DS.getSourceRange()), Context(C), 2025 InvalidType(DS.getTypeSpecType() == DeclSpec::TST_error), 2026 GroupingParens(false), FunctionDefinition(static_cast<unsigned>( 2027 FunctionDefinitionKind::Declaration)), 2028 Redeclaration(false), Extension(false), ObjCIvar(false), 2029 ObjCWeakProperty(false), InlineStorageUsed(false), 2030 HasInitializer(false), Attrs(DS.getAttributePool().getFactory()), 2031 DeclarationAttrs(DeclarationAttrs), AsmLabel(nullptr), 2032 TrailingRequiresClause(nullptr), 2033 InventedTemplateParameterList(nullptr) { 2034 assert(llvm::all_of(DeclarationAttrs, 2035 [](const ParsedAttr &AL) { 2036 return (AL.isStandardAttributeSyntax() || 2037 AL.isRegularKeywordAttribute()); 2038 }) && 2039 "DeclarationAttrs may only contain [[]] and keyword attributes"); 2040 } 2041 ~Declarator()2042 ~Declarator() { 2043 clear(); 2044 } 2045 /// getDeclSpec - Return the declaration-specifier that this declarator was 2046 /// declared with. getDeclSpec()2047 const DeclSpec &getDeclSpec() const { return DS; } 2048 2049 /// getMutableDeclSpec - Return a non-const version of the DeclSpec. This 2050 /// should be used with extreme care: declspecs can often be shared between 2051 /// multiple declarators, so mutating the DeclSpec affects all of the 2052 /// Declarators. This should only be done when the declspec is known to not 2053 /// be shared or when in error recovery etc. getMutableDeclSpec()2054 DeclSpec &getMutableDeclSpec() { return const_cast<DeclSpec &>(DS); } 2055 getAttributePool()2056 AttributePool &getAttributePool() const { 2057 return Attrs.getPool(); 2058 } 2059 2060 /// getCXXScopeSpec - Return the C++ scope specifier (global scope or 2061 /// nested-name-specifier) that is part of the declarator-id. getCXXScopeSpec()2062 const CXXScopeSpec &getCXXScopeSpec() const { return SS; } getCXXScopeSpec()2063 CXXScopeSpec &getCXXScopeSpec() { return SS; } 2064 2065 /// Retrieve the name specified by this declarator. getName()2066 UnqualifiedId &getName() { return Name; } 2067 getDecompositionDeclarator()2068 const DecompositionDeclarator &getDecompositionDeclarator() const { 2069 return BindingGroup; 2070 } 2071 getContext()2072 DeclaratorContext getContext() const { return Context; } 2073 isPrototypeContext()2074 bool isPrototypeContext() const { 2075 return (Context == DeclaratorContext::Prototype || 2076 Context == DeclaratorContext::ObjCParameter || 2077 Context == DeclaratorContext::ObjCResult || 2078 Context == DeclaratorContext::LambdaExprParameter); 2079 } 2080 2081 /// Get the source range that spans this declarator. getSourceRange()2082 SourceRange getSourceRange() const LLVM_READONLY { return Range; } getBeginLoc()2083 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); } getEndLoc()2084 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); } 2085 SetSourceRange(SourceRange R)2086 void SetSourceRange(SourceRange R) { Range = R; } 2087 /// SetRangeBegin - Set the start of the source range to Loc, unless it's 2088 /// invalid. SetRangeBegin(SourceLocation Loc)2089 void SetRangeBegin(SourceLocation Loc) { 2090 if (!Loc.isInvalid()) 2091 Range.setBegin(Loc); 2092 } 2093 /// SetRangeEnd - Set the end of the source range to Loc, unless it's invalid. SetRangeEnd(SourceLocation Loc)2094 void SetRangeEnd(SourceLocation Loc) { 2095 if (!Loc.isInvalid()) 2096 Range.setEnd(Loc); 2097 } 2098 /// ExtendWithDeclSpec - Extend the declarator source range to include the 2099 /// given declspec, unless its location is invalid. Adopts the range start if 2100 /// the current range start is invalid. ExtendWithDeclSpec(const DeclSpec & DS)2101 void ExtendWithDeclSpec(const DeclSpec &DS) { 2102 SourceRange SR = DS.getSourceRange(); 2103 if (Range.getBegin().isInvalid()) 2104 Range.setBegin(SR.getBegin()); 2105 if (!SR.getEnd().isInvalid()) 2106 Range.setEnd(SR.getEnd()); 2107 } 2108 2109 /// Reset the contents of this Declarator. clear()2110 void clear() { 2111 SS.clear(); 2112 Name.clear(); 2113 Range = DS.getSourceRange(); 2114 BindingGroup.clear(); 2115 2116 for (unsigned i = 0, e = DeclTypeInfo.size(); i != e; ++i) 2117 DeclTypeInfo[i].destroy(); 2118 DeclTypeInfo.clear(); 2119 Attrs.clear(); 2120 AsmLabel = nullptr; 2121 InlineStorageUsed = false; 2122 HasInitializer = false; 2123 ObjCIvar = false; 2124 ObjCWeakProperty = false; 2125 CommaLoc = SourceLocation(); 2126 EllipsisLoc = SourceLocation(); 2127 PackIndexingExpr = nullptr; 2128 } 2129 2130 /// mayOmitIdentifier - Return true if the identifier is either optional or 2131 /// not allowed. This is true for typenames, prototypes, and template 2132 /// parameter lists. mayOmitIdentifier()2133 bool mayOmitIdentifier() const { 2134 switch (Context) { 2135 case DeclaratorContext::File: 2136 case DeclaratorContext::KNRTypeList: 2137 case DeclaratorContext::Member: 2138 case DeclaratorContext::Block: 2139 case DeclaratorContext::ForInit: 2140 case DeclaratorContext::SelectionInit: 2141 case DeclaratorContext::Condition: 2142 return false; 2143 2144 case DeclaratorContext::TypeName: 2145 case DeclaratorContext::FunctionalCast: 2146 case DeclaratorContext::AliasDecl: 2147 case DeclaratorContext::AliasTemplate: 2148 case DeclaratorContext::Prototype: 2149 case DeclaratorContext::LambdaExprParameter: 2150 case DeclaratorContext::ObjCParameter: 2151 case DeclaratorContext::ObjCResult: 2152 case DeclaratorContext::TemplateParam: 2153 case DeclaratorContext::CXXNew: 2154 case DeclaratorContext::CXXCatch: 2155 case DeclaratorContext::ObjCCatch: 2156 case DeclaratorContext::BlockLiteral: 2157 case DeclaratorContext::LambdaExpr: 2158 case DeclaratorContext::ConversionId: 2159 case DeclaratorContext::TemplateArg: 2160 case DeclaratorContext::TemplateTypeArg: 2161 case DeclaratorContext::TrailingReturn: 2162 case DeclaratorContext::TrailingReturnVar: 2163 case DeclaratorContext::RequiresExpr: 2164 case DeclaratorContext::Association: 2165 return true; 2166 } 2167 llvm_unreachable("unknown context kind!"); 2168 } 2169 2170 /// mayHaveIdentifier - Return true if the identifier is either optional or 2171 /// required. This is true for normal declarators and prototypes, but not 2172 /// typenames. mayHaveIdentifier()2173 bool mayHaveIdentifier() const { 2174 switch (Context) { 2175 case DeclaratorContext::File: 2176 case DeclaratorContext::KNRTypeList: 2177 case DeclaratorContext::Member: 2178 case DeclaratorContext::Block: 2179 case DeclaratorContext::ForInit: 2180 case DeclaratorContext::SelectionInit: 2181 case DeclaratorContext::Condition: 2182 case DeclaratorContext::Prototype: 2183 case DeclaratorContext::LambdaExprParameter: 2184 case DeclaratorContext::TemplateParam: 2185 case DeclaratorContext::CXXCatch: 2186 case DeclaratorContext::ObjCCatch: 2187 case DeclaratorContext::RequiresExpr: 2188 return true; 2189 2190 case DeclaratorContext::TypeName: 2191 case DeclaratorContext::FunctionalCast: 2192 case DeclaratorContext::CXXNew: 2193 case DeclaratorContext::AliasDecl: 2194 case DeclaratorContext::AliasTemplate: 2195 case DeclaratorContext::ObjCParameter: 2196 case DeclaratorContext::ObjCResult: 2197 case DeclaratorContext::BlockLiteral: 2198 case DeclaratorContext::LambdaExpr: 2199 case DeclaratorContext::ConversionId: 2200 case DeclaratorContext::TemplateArg: 2201 case DeclaratorContext::TemplateTypeArg: 2202 case DeclaratorContext::TrailingReturn: 2203 case DeclaratorContext::TrailingReturnVar: 2204 case DeclaratorContext::Association: 2205 return false; 2206 } 2207 llvm_unreachable("unknown context kind!"); 2208 } 2209 2210 /// Return true if the context permits a C++17 decomposition declarator. mayHaveDecompositionDeclarator()2211 bool mayHaveDecompositionDeclarator() const { 2212 switch (Context) { 2213 case DeclaratorContext::File: 2214 // FIXME: It's not clear that the proposal meant to allow file-scope 2215 // structured bindings, but it does. 2216 case DeclaratorContext::Block: 2217 case DeclaratorContext::ForInit: 2218 case DeclaratorContext::SelectionInit: 2219 case DeclaratorContext::Condition: 2220 return true; 2221 2222 case DeclaratorContext::Member: 2223 case DeclaratorContext::Prototype: 2224 case DeclaratorContext::TemplateParam: 2225 case DeclaratorContext::RequiresExpr: 2226 // Maybe one day... 2227 return false; 2228 2229 // These contexts don't allow any kind of non-abstract declarator. 2230 case DeclaratorContext::KNRTypeList: 2231 case DeclaratorContext::TypeName: 2232 case DeclaratorContext::FunctionalCast: 2233 case DeclaratorContext::AliasDecl: 2234 case DeclaratorContext::AliasTemplate: 2235 case DeclaratorContext::LambdaExprParameter: 2236 case DeclaratorContext::ObjCParameter: 2237 case DeclaratorContext::ObjCResult: 2238 case DeclaratorContext::CXXNew: 2239 case DeclaratorContext::CXXCatch: 2240 case DeclaratorContext::ObjCCatch: 2241 case DeclaratorContext::BlockLiteral: 2242 case DeclaratorContext::LambdaExpr: 2243 case DeclaratorContext::ConversionId: 2244 case DeclaratorContext::TemplateArg: 2245 case DeclaratorContext::TemplateTypeArg: 2246 case DeclaratorContext::TrailingReturn: 2247 case DeclaratorContext::TrailingReturnVar: 2248 case DeclaratorContext::Association: 2249 return false; 2250 } 2251 llvm_unreachable("unknown context kind!"); 2252 } 2253 2254 /// mayBeFollowedByCXXDirectInit - Return true if the declarator can be 2255 /// followed by a C++ direct initializer, e.g. "int x(1);". mayBeFollowedByCXXDirectInit()2256 bool mayBeFollowedByCXXDirectInit() const { 2257 if (hasGroupingParens()) return false; 2258 2259 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) 2260 return false; 2261 2262 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern && 2263 Context != DeclaratorContext::File) 2264 return false; 2265 2266 // Special names can't have direct initializers. 2267 if (Name.getKind() != UnqualifiedIdKind::IK_Identifier) 2268 return false; 2269 2270 switch (Context) { 2271 case DeclaratorContext::File: 2272 case DeclaratorContext::Block: 2273 case DeclaratorContext::ForInit: 2274 case DeclaratorContext::SelectionInit: 2275 case DeclaratorContext::TrailingReturnVar: 2276 return true; 2277 2278 case DeclaratorContext::Condition: 2279 // This may not be followed by a direct initializer, but it can't be a 2280 // function declaration either, and we'd prefer to perform a tentative 2281 // parse in order to produce the right diagnostic. 2282 return true; 2283 2284 case DeclaratorContext::KNRTypeList: 2285 case DeclaratorContext::Member: 2286 case DeclaratorContext::Prototype: 2287 case DeclaratorContext::LambdaExprParameter: 2288 case DeclaratorContext::ObjCParameter: 2289 case DeclaratorContext::ObjCResult: 2290 case DeclaratorContext::TemplateParam: 2291 case DeclaratorContext::CXXCatch: 2292 case DeclaratorContext::ObjCCatch: 2293 case DeclaratorContext::TypeName: 2294 case DeclaratorContext::FunctionalCast: // FIXME 2295 case DeclaratorContext::CXXNew: 2296 case DeclaratorContext::AliasDecl: 2297 case DeclaratorContext::AliasTemplate: 2298 case DeclaratorContext::BlockLiteral: 2299 case DeclaratorContext::LambdaExpr: 2300 case DeclaratorContext::ConversionId: 2301 case DeclaratorContext::TemplateArg: 2302 case DeclaratorContext::TemplateTypeArg: 2303 case DeclaratorContext::TrailingReturn: 2304 case DeclaratorContext::RequiresExpr: 2305 case DeclaratorContext::Association: 2306 return false; 2307 } 2308 llvm_unreachable("unknown context kind!"); 2309 } 2310 2311 /// isPastIdentifier - Return true if we have parsed beyond the point where 2312 /// the name would appear. (This may happen even if we haven't actually parsed 2313 /// a name, perhaps because this context doesn't require one.) isPastIdentifier()2314 bool isPastIdentifier() const { return Name.isValid(); } 2315 2316 /// hasName - Whether this declarator has a name, which might be an 2317 /// identifier (accessible via getIdentifier()) or some kind of 2318 /// special C++ name (constructor, destructor, etc.), or a structured 2319 /// binding (which is not exactly a name, but occupies the same position). hasName()2320 bool hasName() const { 2321 return Name.getKind() != UnqualifiedIdKind::IK_Identifier || 2322 Name.Identifier || isDecompositionDeclarator(); 2323 } 2324 2325 /// Return whether this declarator is a decomposition declarator. isDecompositionDeclarator()2326 bool isDecompositionDeclarator() const { 2327 return BindingGroup.isSet(); 2328 } 2329 getIdentifier()2330 const IdentifierInfo *getIdentifier() const { 2331 if (Name.getKind() == UnqualifiedIdKind::IK_Identifier) 2332 return Name.Identifier; 2333 2334 return nullptr; 2335 } getIdentifierLoc()2336 SourceLocation getIdentifierLoc() const { return Name.StartLocation; } 2337 2338 /// Set the name of this declarator to be the given identifier. SetIdentifier(const IdentifierInfo * Id,SourceLocation IdLoc)2339 void SetIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc) { 2340 Name.setIdentifier(Id, IdLoc); 2341 } 2342 2343 /// Set the decomposition bindings for this declarator. 2344 void setDecompositionBindings( 2345 SourceLocation LSquareLoc, 2346 MutableArrayRef<DecompositionDeclarator::Binding> Bindings, 2347 SourceLocation RSquareLoc); 2348 2349 /// AddTypeInfo - Add a chunk to this declarator. Also extend the range to 2350 /// EndLoc, which should be the last token of the chunk. 2351 /// This function takes attrs by R-Value reference because it takes ownership 2352 /// of those attributes from the parameter. AddTypeInfo(const DeclaratorChunk & TI,ParsedAttributes && attrs,SourceLocation EndLoc)2353 void AddTypeInfo(const DeclaratorChunk &TI, ParsedAttributes &&attrs, 2354 SourceLocation EndLoc) { 2355 DeclTypeInfo.push_back(TI); 2356 DeclTypeInfo.back().getAttrs().addAll(attrs.begin(), attrs.end()); 2357 getAttributePool().takeAllFrom(attrs.getPool()); 2358 2359 if (!EndLoc.isInvalid()) 2360 SetRangeEnd(EndLoc); 2361 } 2362 2363 /// AddTypeInfo - Add a chunk to this declarator. Also extend the range to 2364 /// EndLoc, which should be the last token of the chunk. This overload is for 2365 /// copying a 'chunk' from another declarator, so it takes the pool that the 2366 /// other Declarator owns so that it can 'take' the attributes from it. AddTypeInfo(const DeclaratorChunk & TI,AttributePool & OtherPool,SourceLocation EndLoc)2367 void AddTypeInfo(const DeclaratorChunk &TI, AttributePool &OtherPool, 2368 SourceLocation EndLoc) { 2369 DeclTypeInfo.push_back(TI); 2370 getAttributePool().takeFrom(DeclTypeInfo.back().getAttrs(), OtherPool); 2371 2372 if (!EndLoc.isInvalid()) 2373 SetRangeEnd(EndLoc); 2374 } 2375 2376 /// AddTypeInfo - Add a chunk to this declarator. Also extend the range to 2377 /// EndLoc, which should be the last token of the chunk. AddTypeInfo(const DeclaratorChunk & TI,SourceLocation EndLoc)2378 void AddTypeInfo(const DeclaratorChunk &TI, SourceLocation EndLoc) { 2379 DeclTypeInfo.push_back(TI); 2380 2381 assert(TI.AttrList.empty() && 2382 "Cannot add a declarator chunk with attributes with this overload"); 2383 2384 if (!EndLoc.isInvalid()) 2385 SetRangeEnd(EndLoc); 2386 } 2387 2388 /// Add a new innermost chunk to this declarator. AddInnermostTypeInfo(const DeclaratorChunk & TI)2389 void AddInnermostTypeInfo(const DeclaratorChunk &TI) { 2390 DeclTypeInfo.insert(DeclTypeInfo.begin(), TI); 2391 } 2392 2393 /// Return the number of types applied to this declarator. getNumTypeObjects()2394 unsigned getNumTypeObjects() const { return DeclTypeInfo.size(); } 2395 2396 /// Return the specified TypeInfo from this declarator. TypeInfo #0 is 2397 /// closest to the identifier. getTypeObject(unsigned i)2398 const DeclaratorChunk &getTypeObject(unsigned i) const { 2399 assert(i < DeclTypeInfo.size() && "Invalid type chunk"); 2400 return DeclTypeInfo[i]; 2401 } getTypeObject(unsigned i)2402 DeclaratorChunk &getTypeObject(unsigned i) { 2403 assert(i < DeclTypeInfo.size() && "Invalid type chunk"); 2404 return DeclTypeInfo[i]; 2405 } 2406 2407 typedef SmallVectorImpl<DeclaratorChunk>::const_iterator type_object_iterator; 2408 typedef llvm::iterator_range<type_object_iterator> type_object_range; 2409 2410 /// Returns the range of type objects, from the identifier outwards. type_objects()2411 type_object_range type_objects() const { 2412 return type_object_range(DeclTypeInfo.begin(), DeclTypeInfo.end()); 2413 } 2414 DropFirstTypeObject()2415 void DropFirstTypeObject() { 2416 assert(!DeclTypeInfo.empty() && "No type chunks to drop."); 2417 DeclTypeInfo.front().destroy(); 2418 DeclTypeInfo.erase(DeclTypeInfo.begin()); 2419 } 2420 2421 /// Return the innermost (closest to the declarator) chunk of this 2422 /// declarator that is not a parens chunk, or null if there are no 2423 /// non-parens chunks. getInnermostNonParenChunk()2424 const DeclaratorChunk *getInnermostNonParenChunk() const { 2425 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) { 2426 if (!DeclTypeInfo[i].isParen()) 2427 return &DeclTypeInfo[i]; 2428 } 2429 return nullptr; 2430 } 2431 2432 /// Return the outermost (furthest from the declarator) chunk of 2433 /// this declarator that is not a parens chunk, or null if there are 2434 /// no non-parens chunks. getOutermostNonParenChunk()2435 const DeclaratorChunk *getOutermostNonParenChunk() const { 2436 for (unsigned i = DeclTypeInfo.size(), i_end = 0; i != i_end; --i) { 2437 if (!DeclTypeInfo[i-1].isParen()) 2438 return &DeclTypeInfo[i-1]; 2439 } 2440 return nullptr; 2441 } 2442 2443 /// isArrayOfUnknownBound - This method returns true if the declarator 2444 /// is a declarator for an array of unknown bound (looking through 2445 /// parentheses). isArrayOfUnknownBound()2446 bool isArrayOfUnknownBound() const { 2447 const DeclaratorChunk *chunk = getInnermostNonParenChunk(); 2448 return (chunk && chunk->Kind == DeclaratorChunk::Array && 2449 !chunk->Arr.NumElts); 2450 } 2451 2452 /// isFunctionDeclarator - This method returns true if the declarator 2453 /// is a function declarator (looking through parentheses). 2454 /// If true is returned, then the reference type parameter idx is 2455 /// assigned with the index of the declaration chunk. isFunctionDeclarator(unsigned & idx)2456 bool isFunctionDeclarator(unsigned& idx) const { 2457 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) { 2458 switch (DeclTypeInfo[i].Kind) { 2459 case DeclaratorChunk::Function: 2460 idx = i; 2461 return true; 2462 case DeclaratorChunk::Paren: 2463 continue; 2464 case DeclaratorChunk::Pointer: 2465 case DeclaratorChunk::Reference: 2466 case DeclaratorChunk::Array: 2467 case DeclaratorChunk::BlockPointer: 2468 case DeclaratorChunk::MemberPointer: 2469 case DeclaratorChunk::Pipe: 2470 return false; 2471 } 2472 llvm_unreachable("Invalid type chunk"); 2473 } 2474 return false; 2475 } 2476 2477 /// isFunctionDeclarator - Once this declarator is fully parsed and formed, 2478 /// this method returns true if the identifier is a function declarator 2479 /// (looking through parentheses). isFunctionDeclarator()2480 bool isFunctionDeclarator() const { 2481 unsigned index; 2482 return isFunctionDeclarator(index); 2483 } 2484 2485 /// getFunctionTypeInfo - Retrieves the function type info object 2486 /// (looking through parentheses). getFunctionTypeInfo()2487 DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() { 2488 assert(isFunctionDeclarator() && "Not a function declarator!"); 2489 unsigned index = 0; 2490 isFunctionDeclarator(index); 2491 return DeclTypeInfo[index].Fun; 2492 } 2493 2494 /// getFunctionTypeInfo - Retrieves the function type info object 2495 /// (looking through parentheses). getFunctionTypeInfo()2496 const DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() const { 2497 return const_cast<Declarator*>(this)->getFunctionTypeInfo(); 2498 } 2499 2500 /// Determine whether the declaration that will be produced from 2501 /// this declaration will be a function. 2502 /// 2503 /// A declaration can declare a function even if the declarator itself 2504 /// isn't a function declarator, if the type specifier refers to a function 2505 /// type. This routine checks for both cases. 2506 bool isDeclarationOfFunction() const; 2507 2508 /// Return true if this declaration appears in a context where a 2509 /// function declarator would be a function declaration. isFunctionDeclarationContext()2510 bool isFunctionDeclarationContext() const { 2511 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) 2512 return false; 2513 2514 switch (Context) { 2515 case DeclaratorContext::File: 2516 case DeclaratorContext::Member: 2517 case DeclaratorContext::Block: 2518 case DeclaratorContext::ForInit: 2519 case DeclaratorContext::SelectionInit: 2520 return true; 2521 2522 case DeclaratorContext::Condition: 2523 case DeclaratorContext::KNRTypeList: 2524 case DeclaratorContext::TypeName: 2525 case DeclaratorContext::FunctionalCast: 2526 case DeclaratorContext::AliasDecl: 2527 case DeclaratorContext::AliasTemplate: 2528 case DeclaratorContext::Prototype: 2529 case DeclaratorContext::LambdaExprParameter: 2530 case DeclaratorContext::ObjCParameter: 2531 case DeclaratorContext::ObjCResult: 2532 case DeclaratorContext::TemplateParam: 2533 case DeclaratorContext::CXXNew: 2534 case DeclaratorContext::CXXCatch: 2535 case DeclaratorContext::ObjCCatch: 2536 case DeclaratorContext::BlockLiteral: 2537 case DeclaratorContext::LambdaExpr: 2538 case DeclaratorContext::ConversionId: 2539 case DeclaratorContext::TemplateArg: 2540 case DeclaratorContext::TemplateTypeArg: 2541 case DeclaratorContext::TrailingReturn: 2542 case DeclaratorContext::TrailingReturnVar: 2543 case DeclaratorContext::RequiresExpr: 2544 case DeclaratorContext::Association: 2545 return false; 2546 } 2547 llvm_unreachable("unknown context kind!"); 2548 } 2549 2550 /// Determine whether this declaration appears in a context where an 2551 /// expression could appear. isExpressionContext()2552 bool isExpressionContext() const { 2553 switch (Context) { 2554 case DeclaratorContext::File: 2555 case DeclaratorContext::KNRTypeList: 2556 case DeclaratorContext::Member: 2557 2558 // FIXME: sizeof(...) permits an expression. 2559 case DeclaratorContext::TypeName: 2560 2561 case DeclaratorContext::FunctionalCast: 2562 case DeclaratorContext::AliasDecl: 2563 case DeclaratorContext::AliasTemplate: 2564 case DeclaratorContext::Prototype: 2565 case DeclaratorContext::LambdaExprParameter: 2566 case DeclaratorContext::ObjCParameter: 2567 case DeclaratorContext::ObjCResult: 2568 case DeclaratorContext::TemplateParam: 2569 case DeclaratorContext::CXXNew: 2570 case DeclaratorContext::CXXCatch: 2571 case DeclaratorContext::ObjCCatch: 2572 case DeclaratorContext::BlockLiteral: 2573 case DeclaratorContext::LambdaExpr: 2574 case DeclaratorContext::ConversionId: 2575 case DeclaratorContext::TrailingReturn: 2576 case DeclaratorContext::TrailingReturnVar: 2577 case DeclaratorContext::TemplateTypeArg: 2578 case DeclaratorContext::RequiresExpr: 2579 case DeclaratorContext::Association: 2580 return false; 2581 2582 case DeclaratorContext::Block: 2583 case DeclaratorContext::ForInit: 2584 case DeclaratorContext::SelectionInit: 2585 case DeclaratorContext::Condition: 2586 case DeclaratorContext::TemplateArg: 2587 return true; 2588 } 2589 2590 llvm_unreachable("unknown context kind!"); 2591 } 2592 2593 /// Return true if a function declarator at this position would be a 2594 /// function declaration. isFunctionDeclaratorAFunctionDeclaration()2595 bool isFunctionDeclaratorAFunctionDeclaration() const { 2596 if (!isFunctionDeclarationContext()) 2597 return false; 2598 2599 for (unsigned I = 0, N = getNumTypeObjects(); I != N; ++I) 2600 if (getTypeObject(I).Kind != DeclaratorChunk::Paren) 2601 return false; 2602 2603 return true; 2604 } 2605 2606 /// Determine whether a trailing return type was written (at any 2607 /// level) within this declarator. hasTrailingReturnType()2608 bool hasTrailingReturnType() const { 2609 for (const auto &Chunk : type_objects()) 2610 if (Chunk.Kind == DeclaratorChunk::Function && 2611 Chunk.Fun.hasTrailingReturnType()) 2612 return true; 2613 return false; 2614 } 2615 /// Get the trailing return type appearing (at any level) within this 2616 /// declarator. getTrailingReturnType()2617 ParsedType getTrailingReturnType() const { 2618 for (const auto &Chunk : type_objects()) 2619 if (Chunk.Kind == DeclaratorChunk::Function && 2620 Chunk.Fun.hasTrailingReturnType()) 2621 return Chunk.Fun.getTrailingReturnType(); 2622 return ParsedType(); 2623 } 2624 2625 /// \brief Sets a trailing requires clause for this declarator. setTrailingRequiresClause(Expr * TRC)2626 void setTrailingRequiresClause(Expr *TRC) { 2627 TrailingRequiresClause = TRC; 2628 2629 SetRangeEnd(TRC->getEndLoc()); 2630 } 2631 2632 /// \brief Sets a trailing requires clause for this declarator. getTrailingRequiresClause()2633 Expr *getTrailingRequiresClause() { 2634 return TrailingRequiresClause; 2635 } 2636 2637 /// \brief Determine whether a trailing requires clause was written in this 2638 /// declarator. hasTrailingRequiresClause()2639 bool hasTrailingRequiresClause() const { 2640 return TrailingRequiresClause != nullptr; 2641 } 2642 2643 /// Sets the template parameter lists that preceded the declarator. setTemplateParameterLists(ArrayRef<TemplateParameterList * > TPLs)2644 void setTemplateParameterLists(ArrayRef<TemplateParameterList *> TPLs) { 2645 TemplateParameterLists = TPLs; 2646 } 2647 2648 /// The template parameter lists that preceded the declarator. getTemplateParameterLists()2649 ArrayRef<TemplateParameterList *> getTemplateParameterLists() const { 2650 return TemplateParameterLists; 2651 } 2652 2653 /// Sets the template parameter list generated from the explicit template 2654 /// parameters along with any invented template parameters from 2655 /// placeholder-typed parameters. setInventedTemplateParameterList(TemplateParameterList * Invented)2656 void setInventedTemplateParameterList(TemplateParameterList *Invented) { 2657 InventedTemplateParameterList = Invented; 2658 } 2659 2660 /// The template parameter list generated from the explicit template 2661 /// parameters along with any invented template parameters from 2662 /// placeholder-typed parameters, if there were any such parameters. getInventedTemplateParameterList()2663 TemplateParameterList * getInventedTemplateParameterList() const { 2664 return InventedTemplateParameterList; 2665 } 2666 2667 /// takeAttributes - Takes attributes from the given parsed-attributes 2668 /// set and add them to this declarator. 2669 /// 2670 /// These examples both add 3 attributes to "var": 2671 /// short int var __attribute__((aligned(16),common,deprecated)); 2672 /// short int x, __attribute__((aligned(16)) var 2673 /// __attribute__((common,deprecated)); 2674 /// 2675 /// Also extends the range of the declarator. takeAttributes(ParsedAttributes & attrs)2676 void takeAttributes(ParsedAttributes &attrs) { 2677 Attrs.takeAllFrom(attrs); 2678 2679 if (attrs.Range.getEnd().isValid()) 2680 SetRangeEnd(attrs.Range.getEnd()); 2681 } 2682 getAttributes()2683 const ParsedAttributes &getAttributes() const { return Attrs; } getAttributes()2684 ParsedAttributes &getAttributes() { return Attrs; } 2685 getDeclarationAttributes()2686 const ParsedAttributesView &getDeclarationAttributes() const { 2687 return DeclarationAttrs; 2688 } 2689 2690 /// hasAttributes - do we contain any attributes? hasAttributes()2691 bool hasAttributes() const { 2692 if (!getAttributes().empty() || !getDeclarationAttributes().empty() || 2693 getDeclSpec().hasAttributes()) 2694 return true; 2695 for (unsigned i = 0, e = getNumTypeObjects(); i != e; ++i) 2696 if (!getTypeObject(i).getAttrs().empty()) 2697 return true; 2698 return false; 2699 } 2700 setAsmLabel(Expr * E)2701 void setAsmLabel(Expr *E) { AsmLabel = E; } getAsmLabel()2702 Expr *getAsmLabel() const { return AsmLabel; } 2703 2704 void setExtension(bool Val = true) { Extension = Val; } getExtension()2705 bool getExtension() const { return Extension; } 2706 2707 void setObjCIvar(bool Val = true) { ObjCIvar = Val; } isObjCIvar()2708 bool isObjCIvar() const { return ObjCIvar; } 2709 2710 void setObjCWeakProperty(bool Val = true) { ObjCWeakProperty = Val; } isObjCWeakProperty()2711 bool isObjCWeakProperty() const { return ObjCWeakProperty; } 2712 2713 void setInvalidType(bool Val = true) { InvalidType = Val; } isInvalidType()2714 bool isInvalidType() const { 2715 return InvalidType || DS.getTypeSpecType() == DeclSpec::TST_error; 2716 } 2717 setGroupingParens(bool flag)2718 void setGroupingParens(bool flag) { GroupingParens = flag; } hasGroupingParens()2719 bool hasGroupingParens() const { return GroupingParens; } 2720 isFirstDeclarator()2721 bool isFirstDeclarator() const { return !CommaLoc.isValid(); } getCommaLoc()2722 SourceLocation getCommaLoc() const { return CommaLoc; } setCommaLoc(SourceLocation CL)2723 void setCommaLoc(SourceLocation CL) { CommaLoc = CL; } 2724 hasEllipsis()2725 bool hasEllipsis() const { return EllipsisLoc.isValid(); } getEllipsisLoc()2726 SourceLocation getEllipsisLoc() const { return EllipsisLoc; } setEllipsisLoc(SourceLocation EL)2727 void setEllipsisLoc(SourceLocation EL) { EllipsisLoc = EL; } 2728 hasPackIndexing()2729 bool hasPackIndexing() const { return PackIndexingExpr != nullptr; } getPackIndexingExpr()2730 Expr *getPackIndexingExpr() const { return PackIndexingExpr; } setPackIndexingExpr(Expr * PI)2731 void setPackIndexingExpr(Expr *PI) { PackIndexingExpr = PI; } 2732 setFunctionDefinitionKind(FunctionDefinitionKind Val)2733 void setFunctionDefinitionKind(FunctionDefinitionKind Val) { 2734 FunctionDefinition = static_cast<unsigned>(Val); 2735 } 2736 isFunctionDefinition()2737 bool isFunctionDefinition() const { 2738 return getFunctionDefinitionKind() != FunctionDefinitionKind::Declaration; 2739 } 2740 getFunctionDefinitionKind()2741 FunctionDefinitionKind getFunctionDefinitionKind() const { 2742 return (FunctionDefinitionKind)FunctionDefinition; 2743 } 2744 2745 void setHasInitializer(bool Val = true) { HasInitializer = Val; } hasInitializer()2746 bool hasInitializer() const { return HasInitializer; } 2747 2748 /// Returns true if this declares a real member and not a friend. isFirstDeclarationOfMember()2749 bool isFirstDeclarationOfMember() { 2750 return getContext() == DeclaratorContext::Member && 2751 !getDeclSpec().isFriendSpecified(); 2752 } 2753 2754 /// Returns true if this declares a static member. This cannot be called on a 2755 /// declarator outside of a MemberContext because we won't know until 2756 /// redeclaration time if the decl is static. 2757 bool isStaticMember(); 2758 2759 bool isExplicitObjectMemberFunction(); 2760 2761 /// Returns true if this declares a constructor or a destructor. 2762 bool isCtorOrDtor(); 2763 setRedeclaration(bool Val)2764 void setRedeclaration(bool Val) { Redeclaration = Val; } isRedeclaration()2765 bool isRedeclaration() const { return Redeclaration; } 2766 }; 2767 2768 /// This little struct is used to capture information about 2769 /// structure field declarators, which is basically just a bitfield size. 2770 struct FieldDeclarator { 2771 Declarator D; 2772 Expr *BitfieldSize; FieldDeclaratorFieldDeclarator2773 explicit FieldDeclarator(const DeclSpec &DS, 2774 const ParsedAttributes &DeclarationAttrs) 2775 : D(DS, DeclarationAttrs, DeclaratorContext::Member), 2776 BitfieldSize(nullptr) {} 2777 }; 2778 2779 /// Represents a C++11 virt-specifier-seq. 2780 class VirtSpecifiers { 2781 public: 2782 enum Specifier { 2783 VS_None = 0, 2784 VS_Override = 1, 2785 VS_Final = 2, 2786 VS_Sealed = 4, 2787 // Represents the __final keyword, which is legal for gcc in pre-C++11 mode. 2788 VS_GNU_Final = 8, 2789 VS_Abstract = 16 2790 }; 2791 2792 VirtSpecifiers() = default; 2793 2794 bool SetSpecifier(Specifier VS, SourceLocation Loc, 2795 const char *&PrevSpec); 2796 isUnset()2797 bool isUnset() const { return Specifiers == 0; } 2798 isOverrideSpecified()2799 bool isOverrideSpecified() const { return Specifiers & VS_Override; } getOverrideLoc()2800 SourceLocation getOverrideLoc() const { return VS_overrideLoc; } 2801 isFinalSpecified()2802 bool isFinalSpecified() const { return Specifiers & (VS_Final | VS_Sealed | VS_GNU_Final); } isFinalSpelledSealed()2803 bool isFinalSpelledSealed() const { return Specifiers & VS_Sealed; } getFinalLoc()2804 SourceLocation getFinalLoc() const { return VS_finalLoc; } getAbstractLoc()2805 SourceLocation getAbstractLoc() const { return VS_abstractLoc; } 2806 clear()2807 void clear() { Specifiers = 0; } 2808 2809 static const char *getSpecifierName(Specifier VS); 2810 getFirstLocation()2811 SourceLocation getFirstLocation() const { return FirstLocation; } getLastLocation()2812 SourceLocation getLastLocation() const { return LastLocation; } getLastSpecifier()2813 Specifier getLastSpecifier() const { return LastSpecifier; } 2814 2815 private: 2816 unsigned Specifiers = 0; 2817 Specifier LastSpecifier = VS_None; 2818 2819 SourceLocation VS_overrideLoc, VS_finalLoc, VS_abstractLoc; 2820 SourceLocation FirstLocation; 2821 SourceLocation LastLocation; 2822 }; 2823 2824 enum class LambdaCaptureInitKind { 2825 NoInit, //!< [a] 2826 CopyInit, //!< [a = b], [a = {b}] 2827 DirectInit, //!< [a(b)] 2828 ListInit //!< [a{b}] 2829 }; 2830 2831 /// Represents a complete lambda introducer. 2832 struct LambdaIntroducer { 2833 /// An individual capture in a lambda introducer. 2834 struct LambdaCapture { 2835 LambdaCaptureKind Kind; 2836 SourceLocation Loc; 2837 IdentifierInfo *Id; 2838 SourceLocation EllipsisLoc; 2839 LambdaCaptureInitKind InitKind; 2840 ExprResult Init; 2841 ParsedType InitCaptureType; 2842 SourceRange ExplicitRange; 2843 LambdaCaptureLambdaIntroducer::LambdaCapture2844 LambdaCapture(LambdaCaptureKind Kind, SourceLocation Loc, 2845 IdentifierInfo *Id, SourceLocation EllipsisLoc, 2846 LambdaCaptureInitKind InitKind, ExprResult Init, 2847 ParsedType InitCaptureType, 2848 SourceRange ExplicitRange) 2849 : Kind(Kind), Loc(Loc), Id(Id), EllipsisLoc(EllipsisLoc), 2850 InitKind(InitKind), Init(Init), InitCaptureType(InitCaptureType), 2851 ExplicitRange(ExplicitRange) {} 2852 }; 2853 2854 SourceRange Range; 2855 SourceLocation DefaultLoc; 2856 LambdaCaptureDefault Default = LCD_None; 2857 SmallVector<LambdaCapture, 4> Captures; 2858 2859 LambdaIntroducer() = default; 2860 hasLambdaCaptureLambdaIntroducer2861 bool hasLambdaCapture() const { 2862 return Captures.size() > 0 || Default != LCD_None; 2863 } 2864 2865 /// Append a capture in a lambda introducer. addCaptureLambdaIntroducer2866 void addCapture(LambdaCaptureKind Kind, 2867 SourceLocation Loc, 2868 IdentifierInfo* Id, 2869 SourceLocation EllipsisLoc, 2870 LambdaCaptureInitKind InitKind, 2871 ExprResult Init, 2872 ParsedType InitCaptureType, 2873 SourceRange ExplicitRange) { 2874 Captures.push_back(LambdaCapture(Kind, Loc, Id, EllipsisLoc, InitKind, Init, 2875 InitCaptureType, ExplicitRange)); 2876 } 2877 }; 2878 2879 struct InventedTemplateParameterInfo { 2880 /// The number of parameters in the template parameter list that were 2881 /// explicitly specified by the user, as opposed to being invented by use 2882 /// of an auto parameter. 2883 unsigned NumExplicitTemplateParams = 0; 2884 2885 /// If this is a generic lambda or abbreviated function template, use this 2886 /// as the depth of each 'auto' parameter, during initial AST construction. 2887 unsigned AutoTemplateParameterDepth = 0; 2888 2889 /// Store the list of the template parameters for a generic lambda or an 2890 /// abbreviated function template. 2891 /// If this is a generic lambda or abbreviated function template, this holds 2892 /// the explicit template parameters followed by the auto parameters 2893 /// converted into TemplateTypeParmDecls. 2894 /// It can be used to construct the generic lambda or abbreviated template's 2895 /// template parameter list during initial AST construction. 2896 SmallVector<NamedDecl*, 4> TemplateParams; 2897 }; 2898 2899 } // end namespace clang 2900 2901 #endif // LLVM_CLANG_SEMA_DECLSPEC_H 2902