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