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