1 //===--- ParseDeclCXX.cpp - C++ Declaration Parsing -------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the C++ Declaration portions of the Parser interfaces. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/AST/ASTContext.h" 14 #include "clang/AST/DeclTemplate.h" 15 #include "clang/AST/PrettyDeclStackTrace.h" 16 #include "clang/Basic/AttributeCommonInfo.h" 17 #include "clang/Basic/Attributes.h" 18 #include "clang/Basic/CharInfo.h" 19 #include "clang/Basic/OperatorKinds.h" 20 #include "clang/Basic/TargetInfo.h" 21 #include "clang/Basic/TokenKinds.h" 22 #include "clang/Lex/LiteralSupport.h" 23 #include "clang/Parse/ParseDiagnostic.h" 24 #include "clang/Parse/Parser.h" 25 #include "clang/Parse/RAIIObjectsForParser.h" 26 #include "clang/Sema/DeclSpec.h" 27 #include "clang/Sema/EnterExpressionEvaluationContext.h" 28 #include "clang/Sema/ParsedTemplate.h" 29 #include "clang/Sema/Scope.h" 30 #include "llvm/ADT/SmallString.h" 31 #include "llvm/Support/TimeProfiler.h" 32 #include <optional> 33 34 using namespace clang; 35 36 /// ParseNamespace - We know that the current token is a namespace keyword. This 37 /// may either be a top level namespace or a block-level namespace alias. If 38 /// there was an inline keyword, it has already been parsed. 39 /// 40 /// namespace-definition: [C++: namespace.def] 41 /// named-namespace-definition 42 /// unnamed-namespace-definition 43 /// nested-namespace-definition 44 /// 45 /// named-namespace-definition: 46 /// 'inline'[opt] 'namespace' attributes[opt] identifier '{' 47 /// namespace-body '}' 48 /// 49 /// unnamed-namespace-definition: 50 /// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}' 51 /// 52 /// nested-namespace-definition: 53 /// 'namespace' enclosing-namespace-specifier '::' 'inline'[opt] 54 /// identifier '{' namespace-body '}' 55 /// 56 /// enclosing-namespace-specifier: 57 /// identifier 58 /// enclosing-namespace-specifier '::' 'inline'[opt] identifier 59 /// 60 /// namespace-alias-definition: [C++ 7.3.2: namespace.alias] 61 /// 'namespace' identifier '=' qualified-namespace-specifier ';' 62 /// 63 Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context, 64 SourceLocation &DeclEnd, 65 SourceLocation InlineLoc) { 66 assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); 67 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. 68 ObjCDeclContextSwitch ObjCDC(*this); 69 70 if (Tok.is(tok::code_completion)) { 71 cutOffParsing(); 72 Actions.CodeCompleteNamespaceDecl(getCurScope()); 73 return nullptr; 74 } 75 76 SourceLocation IdentLoc; 77 IdentifierInfo *Ident = nullptr; 78 InnerNamespaceInfoList ExtraNSs; 79 SourceLocation FirstNestedInlineLoc; 80 81 ParsedAttributes attrs(AttrFactory); 82 83 auto ReadAttributes = [&] { 84 bool MoreToParse; 85 do { 86 MoreToParse = false; 87 if (Tok.is(tok::kw___attribute)) { 88 ParseGNUAttributes(attrs); 89 MoreToParse = true; 90 } 91 if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) { 92 Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 93 ? diag::warn_cxx14_compat_ns_enum_attribute 94 : diag::ext_ns_enum_attribute) 95 << 0 /*namespace*/; 96 ParseCXX11Attributes(attrs); 97 MoreToParse = true; 98 } 99 } while (MoreToParse); 100 }; 101 102 ReadAttributes(); 103 104 if (Tok.is(tok::identifier)) { 105 Ident = Tok.getIdentifierInfo(); 106 IdentLoc = ConsumeToken(); // eat the identifier. 107 while (Tok.is(tok::coloncolon) && 108 (NextToken().is(tok::identifier) || 109 (NextToken().is(tok::kw_inline) && 110 GetLookAheadToken(2).is(tok::identifier)))) { 111 112 InnerNamespaceInfo Info; 113 Info.NamespaceLoc = ConsumeToken(); 114 115 if (Tok.is(tok::kw_inline)) { 116 Info.InlineLoc = ConsumeToken(); 117 if (FirstNestedInlineLoc.isInvalid()) 118 FirstNestedInlineLoc = Info.InlineLoc; 119 } 120 121 Info.Ident = Tok.getIdentifierInfo(); 122 Info.IdentLoc = ConsumeToken(); 123 124 ExtraNSs.push_back(Info); 125 } 126 } 127 128 ReadAttributes(); 129 130 SourceLocation attrLoc = attrs.Range.getBegin(); 131 132 // A nested namespace definition cannot have attributes. 133 if (!ExtraNSs.empty() && attrLoc.isValid()) 134 Diag(attrLoc, diag::err_unexpected_nested_namespace_attribute); 135 136 if (Tok.is(tok::equal)) { 137 if (!Ident) { 138 Diag(Tok, diag::err_expected) << tok::identifier; 139 // Skip to end of the definition and eat the ';'. 140 SkipUntil(tok::semi); 141 return nullptr; 142 } 143 if (attrLoc.isValid()) 144 Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias); 145 if (InlineLoc.isValid()) 146 Diag(InlineLoc, diag::err_inline_namespace_alias) 147 << FixItHint::CreateRemoval(InlineLoc); 148 Decl *NSAlias = ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd); 149 return Actions.ConvertDeclToDeclGroup(NSAlias); 150 } 151 152 BalancedDelimiterTracker T(*this, tok::l_brace); 153 if (T.consumeOpen()) { 154 if (Ident) 155 Diag(Tok, diag::err_expected) << tok::l_brace; 156 else 157 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace; 158 return nullptr; 159 } 160 161 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() || 162 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() || 163 getCurScope()->getFnParent()) { 164 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope); 165 SkipUntil(tok::r_brace); 166 return nullptr; 167 } 168 169 if (ExtraNSs.empty()) { 170 // Normal namespace definition, not a nested-namespace-definition. 171 } else if (InlineLoc.isValid()) { 172 Diag(InlineLoc, diag::err_inline_nested_namespace_definition); 173 } else if (getLangOpts().CPlusPlus20) { 174 Diag(ExtraNSs[0].NamespaceLoc, 175 diag::warn_cxx14_compat_nested_namespace_definition); 176 if (FirstNestedInlineLoc.isValid()) 177 Diag(FirstNestedInlineLoc, 178 diag::warn_cxx17_compat_inline_nested_namespace_definition); 179 } else if (getLangOpts().CPlusPlus17) { 180 Diag(ExtraNSs[0].NamespaceLoc, 181 diag::warn_cxx14_compat_nested_namespace_definition); 182 if (FirstNestedInlineLoc.isValid()) 183 Diag(FirstNestedInlineLoc, diag::ext_inline_nested_namespace_definition); 184 } else { 185 TentativeParsingAction TPA(*this); 186 SkipUntil(tok::r_brace, StopBeforeMatch); 187 Token rBraceToken = Tok; 188 TPA.Revert(); 189 190 if (!rBraceToken.is(tok::r_brace)) { 191 Diag(ExtraNSs[0].NamespaceLoc, diag::ext_nested_namespace_definition) 192 << SourceRange(ExtraNSs.front().NamespaceLoc, 193 ExtraNSs.back().IdentLoc); 194 } else { 195 std::string NamespaceFix; 196 for (const auto &ExtraNS : ExtraNSs) { 197 NamespaceFix += " { "; 198 if (ExtraNS.InlineLoc.isValid()) 199 NamespaceFix += "inline "; 200 NamespaceFix += "namespace "; 201 NamespaceFix += ExtraNS.Ident->getName(); 202 } 203 204 std::string RBraces; 205 for (unsigned i = 0, e = ExtraNSs.size(); i != e; ++i) 206 RBraces += "} "; 207 208 Diag(ExtraNSs[0].NamespaceLoc, diag::ext_nested_namespace_definition) 209 << FixItHint::CreateReplacement( 210 SourceRange(ExtraNSs.front().NamespaceLoc, 211 ExtraNSs.back().IdentLoc), 212 NamespaceFix) 213 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces); 214 } 215 216 // Warn about nested inline namespaces. 217 if (FirstNestedInlineLoc.isValid()) 218 Diag(FirstNestedInlineLoc, diag::ext_inline_nested_namespace_definition); 219 } 220 221 // If we're still good, complain about inline namespaces in non-C++0x now. 222 if (InlineLoc.isValid()) 223 Diag(InlineLoc, getLangOpts().CPlusPlus11 224 ? diag::warn_cxx98_compat_inline_namespace 225 : diag::ext_inline_namespace); 226 227 // Enter a scope for the namespace. 228 ParseScope NamespaceScope(this, Scope::DeclScope); 229 230 UsingDirectiveDecl *ImplicitUsingDirectiveDecl = nullptr; 231 Decl *NamespcDecl = Actions.ActOnStartNamespaceDef( 232 getCurScope(), InlineLoc, NamespaceLoc, IdentLoc, Ident, 233 T.getOpenLocation(), attrs, ImplicitUsingDirectiveDecl, false); 234 235 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, NamespcDecl, 236 NamespaceLoc, "parsing namespace"); 237 238 // Parse the contents of the namespace. This includes parsing recovery on 239 // any improperly nested namespaces. 240 ParseInnerNamespace(ExtraNSs, 0, InlineLoc, attrs, T); 241 242 // Leave the namespace scope. 243 NamespaceScope.Exit(); 244 245 DeclEnd = T.getCloseLocation(); 246 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd); 247 248 return Actions.ConvertDeclToDeclGroup(NamespcDecl, 249 ImplicitUsingDirectiveDecl); 250 } 251 252 /// ParseInnerNamespace - Parse the contents of a namespace. 253 void Parser::ParseInnerNamespace(const InnerNamespaceInfoList &InnerNSs, 254 unsigned int index, SourceLocation &InlineLoc, 255 ParsedAttributes &attrs, 256 BalancedDelimiterTracker &Tracker) { 257 if (index == InnerNSs.size()) { 258 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && 259 Tok.isNot(tok::eof)) { 260 ParsedAttributes DeclAttrs(AttrFactory); 261 MaybeParseCXX11Attributes(DeclAttrs); 262 ParsedAttributes EmptyDeclSpecAttrs(AttrFactory); 263 ParseExternalDeclaration(DeclAttrs, EmptyDeclSpecAttrs); 264 } 265 266 // The caller is what called check -- we are simply calling 267 // the close for it. 268 Tracker.consumeClose(); 269 270 return; 271 } 272 273 // Handle a nested namespace definition. 274 // FIXME: Preserve the source information through to the AST rather than 275 // desugaring it here. 276 ParseScope NamespaceScope(this, Scope::DeclScope); 277 UsingDirectiveDecl *ImplicitUsingDirectiveDecl = nullptr; 278 Decl *NamespcDecl = Actions.ActOnStartNamespaceDef( 279 getCurScope(), InnerNSs[index].InlineLoc, InnerNSs[index].NamespaceLoc, 280 InnerNSs[index].IdentLoc, InnerNSs[index].Ident, 281 Tracker.getOpenLocation(), attrs, ImplicitUsingDirectiveDecl, true); 282 assert(!ImplicitUsingDirectiveDecl && 283 "nested namespace definition cannot define anonymous namespace"); 284 285 ParseInnerNamespace(InnerNSs, ++index, InlineLoc, attrs, Tracker); 286 287 NamespaceScope.Exit(); 288 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation()); 289 } 290 291 /// ParseNamespaceAlias - Parse the part after the '=' in a namespace 292 /// alias definition. 293 /// 294 Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc, 295 SourceLocation AliasLoc, 296 IdentifierInfo *Alias, 297 SourceLocation &DeclEnd) { 298 assert(Tok.is(tok::equal) && "Not equal token"); 299 300 ConsumeToken(); // eat the '='. 301 302 if (Tok.is(tok::code_completion)) { 303 cutOffParsing(); 304 Actions.CodeCompleteNamespaceAliasDecl(getCurScope()); 305 return nullptr; 306 } 307 308 CXXScopeSpec SS; 309 // Parse (optional) nested-name-specifier. 310 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, 311 /*ObjectHasErrors=*/false, 312 /*EnteringContext=*/false, 313 /*MayBePseudoDestructor=*/nullptr, 314 /*IsTypename=*/false, 315 /*LastII=*/nullptr, 316 /*OnlyNamespace=*/true); 317 318 if (Tok.isNot(tok::identifier)) { 319 Diag(Tok, diag::err_expected_namespace_name); 320 // Skip to end of the definition and eat the ';'. 321 SkipUntil(tok::semi); 322 return nullptr; 323 } 324 325 if (SS.isInvalid()) { 326 // Diagnostics have been emitted in ParseOptionalCXXScopeSpecifier. 327 // Skip to end of the definition and eat the ';'. 328 SkipUntil(tok::semi); 329 return nullptr; 330 } 331 332 // Parse identifier. 333 IdentifierInfo *Ident = Tok.getIdentifierInfo(); 334 SourceLocation IdentLoc = ConsumeToken(); 335 336 // Eat the ';'. 337 DeclEnd = Tok.getLocation(); 338 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name)) 339 SkipUntil(tok::semi); 340 341 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, 342 Alias, SS, IdentLoc, Ident); 343 } 344 345 /// ParseLinkage - We know that the current token is a string_literal 346 /// and just before that, that extern was seen. 347 /// 348 /// linkage-specification: [C++ 7.5p2: dcl.link] 349 /// 'extern' string-literal '{' declaration-seq[opt] '}' 350 /// 'extern' string-literal declaration 351 /// 352 Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) { 353 assert(isTokenStringLiteral() && "Not a string literal!"); 354 ExprResult Lang = ParseUnevaluatedStringLiteralExpression(); 355 356 ParseScope LinkageScope(this, Scope::DeclScope); 357 Decl *LinkageSpec = 358 Lang.isInvalid() 359 ? nullptr 360 : Actions.ActOnStartLinkageSpecification( 361 getCurScope(), DS.getSourceRange().getBegin(), Lang.get(), 362 Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation()); 363 364 ParsedAttributes DeclAttrs(AttrFactory); 365 ParsedAttributes DeclSpecAttrs(AttrFactory); 366 367 while (MaybeParseCXX11Attributes(DeclAttrs) || 368 MaybeParseGNUAttributes(DeclSpecAttrs)) 369 ; 370 371 if (Tok.isNot(tok::l_brace)) { 372 // Reset the source range in DS, as the leading "extern" 373 // does not really belong to the inner declaration ... 374 DS.SetRangeStart(SourceLocation()); 375 DS.SetRangeEnd(SourceLocation()); 376 // ... but anyway remember that such an "extern" was seen. 377 DS.setExternInLinkageSpec(true); 378 ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs, &DS); 379 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification( 380 getCurScope(), LinkageSpec, SourceLocation()) 381 : nullptr; 382 } 383 384 DS.abort(); 385 386 ProhibitAttributes(DeclAttrs); 387 388 BalancedDelimiterTracker T(*this, tok::l_brace); 389 T.consumeOpen(); 390 391 unsigned NestedModules = 0; 392 while (true) { 393 switch (Tok.getKind()) { 394 case tok::annot_module_begin: 395 ++NestedModules; 396 ParseTopLevelDecl(); 397 continue; 398 399 case tok::annot_module_end: 400 if (!NestedModules) 401 break; 402 --NestedModules; 403 ParseTopLevelDecl(); 404 continue; 405 406 case tok::annot_module_include: 407 ParseTopLevelDecl(); 408 continue; 409 410 case tok::eof: 411 break; 412 413 case tok::r_brace: 414 if (!NestedModules) 415 break; 416 [[fallthrough]]; 417 default: 418 ParsedAttributes DeclAttrs(AttrFactory); 419 MaybeParseCXX11Attributes(DeclAttrs); 420 ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs); 421 continue; 422 } 423 424 break; 425 } 426 427 T.consumeClose(); 428 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification( 429 getCurScope(), LinkageSpec, T.getCloseLocation()) 430 : nullptr; 431 } 432 433 /// Parse a standard C++ Modules export-declaration. 434 /// 435 /// export-declaration: 436 /// 'export' declaration 437 /// 'export' '{' declaration-seq[opt] '}' 438 /// 439 Decl *Parser::ParseExportDeclaration() { 440 assert(Tok.is(tok::kw_export)); 441 SourceLocation ExportLoc = ConsumeToken(); 442 443 ParseScope ExportScope(this, Scope::DeclScope); 444 Decl *ExportDecl = Actions.ActOnStartExportDecl( 445 getCurScope(), ExportLoc, 446 Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation()); 447 448 if (Tok.isNot(tok::l_brace)) { 449 // FIXME: Factor out a ParseExternalDeclarationWithAttrs. 450 ParsedAttributes DeclAttrs(AttrFactory); 451 MaybeParseCXX11Attributes(DeclAttrs); 452 ParsedAttributes EmptyDeclSpecAttrs(AttrFactory); 453 ParseExternalDeclaration(DeclAttrs, EmptyDeclSpecAttrs); 454 return Actions.ActOnFinishExportDecl(getCurScope(), ExportDecl, 455 SourceLocation()); 456 } 457 458 BalancedDelimiterTracker T(*this, tok::l_brace); 459 T.consumeOpen(); 460 461 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && 462 Tok.isNot(tok::eof)) { 463 ParsedAttributes DeclAttrs(AttrFactory); 464 MaybeParseCXX11Attributes(DeclAttrs); 465 ParsedAttributes EmptyDeclSpecAttrs(AttrFactory); 466 ParseExternalDeclaration(DeclAttrs, EmptyDeclSpecAttrs); 467 } 468 469 T.consumeClose(); 470 return Actions.ActOnFinishExportDecl(getCurScope(), ExportDecl, 471 T.getCloseLocation()); 472 } 473 474 /// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or 475 /// using-directive. Assumes that current token is 'using'. 476 Parser::DeclGroupPtrTy Parser::ParseUsingDirectiveOrDeclaration( 477 DeclaratorContext Context, const ParsedTemplateInfo &TemplateInfo, 478 SourceLocation &DeclEnd, ParsedAttributes &Attrs) { 479 assert(Tok.is(tok::kw_using) && "Not using token"); 480 ObjCDeclContextSwitch ObjCDC(*this); 481 482 // Eat 'using'. 483 SourceLocation UsingLoc = ConsumeToken(); 484 485 if (Tok.is(tok::code_completion)) { 486 cutOffParsing(); 487 Actions.CodeCompleteUsing(getCurScope()); 488 return nullptr; 489 } 490 491 // Consume unexpected 'template' keywords. 492 while (Tok.is(tok::kw_template)) { 493 SourceLocation TemplateLoc = ConsumeToken(); 494 Diag(TemplateLoc, diag::err_unexpected_template_after_using) 495 << FixItHint::CreateRemoval(TemplateLoc); 496 } 497 498 // 'using namespace' means this is a using-directive. 499 if (Tok.is(tok::kw_namespace)) { 500 // Template parameters are always an error here. 501 if (TemplateInfo.Kind) { 502 SourceRange R = TemplateInfo.getSourceRange(); 503 Diag(UsingLoc, diag::err_templated_using_directive_declaration) 504 << 0 /* directive */ << R << FixItHint::CreateRemoval(R); 505 } 506 507 Decl *UsingDir = ParseUsingDirective(Context, UsingLoc, DeclEnd, Attrs); 508 return Actions.ConvertDeclToDeclGroup(UsingDir); 509 } 510 511 // Otherwise, it must be a using-declaration or an alias-declaration. 512 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd, Attrs, 513 AS_none); 514 } 515 516 /// ParseUsingDirective - Parse C++ using-directive, assumes 517 /// that current token is 'namespace' and 'using' was already parsed. 518 /// 519 /// using-directive: [C++ 7.3.p4: namespace.udir] 520 /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] 521 /// namespace-name ; 522 /// [GNU] using-directive: 523 /// 'using' 'namespace' ::[opt] nested-name-specifier[opt] 524 /// namespace-name attributes[opt] ; 525 /// 526 Decl *Parser::ParseUsingDirective(DeclaratorContext Context, 527 SourceLocation UsingLoc, 528 SourceLocation &DeclEnd, 529 ParsedAttributes &attrs) { 530 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token"); 531 532 // Eat 'namespace'. 533 SourceLocation NamespcLoc = ConsumeToken(); 534 535 if (Tok.is(tok::code_completion)) { 536 cutOffParsing(); 537 Actions.CodeCompleteUsingDirective(getCurScope()); 538 return nullptr; 539 } 540 541 CXXScopeSpec SS; 542 // Parse (optional) nested-name-specifier. 543 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, 544 /*ObjectHasErrors=*/false, 545 /*EnteringContext=*/false, 546 /*MayBePseudoDestructor=*/nullptr, 547 /*IsTypename=*/false, 548 /*LastII=*/nullptr, 549 /*OnlyNamespace=*/true); 550 551 IdentifierInfo *NamespcName = nullptr; 552 SourceLocation IdentLoc = SourceLocation(); 553 554 // Parse namespace-name. 555 if (Tok.isNot(tok::identifier)) { 556 Diag(Tok, diag::err_expected_namespace_name); 557 // If there was invalid namespace name, skip to end of decl, and eat ';'. 558 SkipUntil(tok::semi); 559 // FIXME: Are there cases, when we would like to call ActOnUsingDirective? 560 return nullptr; 561 } 562 563 if (SS.isInvalid()) { 564 // Diagnostics have been emitted in ParseOptionalCXXScopeSpecifier. 565 // Skip to end of the definition and eat the ';'. 566 SkipUntil(tok::semi); 567 return nullptr; 568 } 569 570 // Parse identifier. 571 NamespcName = Tok.getIdentifierInfo(); 572 IdentLoc = ConsumeToken(); 573 574 // Parse (optional) attributes (most likely GNU strong-using extension). 575 bool GNUAttr = false; 576 if (Tok.is(tok::kw___attribute)) { 577 GNUAttr = true; 578 ParseGNUAttributes(attrs); 579 } 580 581 // Eat ';'. 582 DeclEnd = Tok.getLocation(); 583 if (ExpectAndConsume(tok::semi, 584 GNUAttr ? diag::err_expected_semi_after_attribute_list 585 : diag::err_expected_semi_after_namespace_name)) 586 SkipUntil(tok::semi); 587 588 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS, 589 IdentLoc, NamespcName, attrs); 590 } 591 592 /// Parse a using-declarator (or the identifier in a C++11 alias-declaration). 593 /// 594 /// using-declarator: 595 /// 'typename'[opt] nested-name-specifier unqualified-id 596 /// 597 bool Parser::ParseUsingDeclarator(DeclaratorContext Context, 598 UsingDeclarator &D) { 599 D.clear(); 600 601 // Ignore optional 'typename'. 602 // FIXME: This is wrong; we should parse this as a typename-specifier. 603 TryConsumeToken(tok::kw_typename, D.TypenameLoc); 604 605 if (Tok.is(tok::kw___super)) { 606 Diag(Tok.getLocation(), diag::err_super_in_using_declaration); 607 return true; 608 } 609 610 // Parse nested-name-specifier. 611 IdentifierInfo *LastII = nullptr; 612 if (ParseOptionalCXXScopeSpecifier(D.SS, /*ObjectType=*/nullptr, 613 /*ObjectHasErrors=*/false, 614 /*EnteringContext=*/false, 615 /*MayBePseudoDtor=*/nullptr, 616 /*IsTypename=*/false, 617 /*LastII=*/&LastII, 618 /*OnlyNamespace=*/false, 619 /*InUsingDeclaration=*/true)) 620 621 return true; 622 if (D.SS.isInvalid()) 623 return true; 624 625 // Parse the unqualified-id. We allow parsing of both constructor and 626 // destructor names and allow the action module to diagnose any semantic 627 // errors. 628 // 629 // C++11 [class.qual]p2: 630 // [...] in a using-declaration that is a member-declaration, if the name 631 // specified after the nested-name-specifier is the same as the identifier 632 // or the simple-template-id's template-name in the last component of the 633 // nested-name-specifier, the name is [...] considered to name the 634 // constructor. 635 if (getLangOpts().CPlusPlus11 && Context == DeclaratorContext::Member && 636 Tok.is(tok::identifier) && 637 (NextToken().is(tok::semi) || NextToken().is(tok::comma) || 638 NextToken().is(tok::ellipsis) || NextToken().is(tok::l_square) || 639 NextToken().isRegularKeywordAttribute() || 640 NextToken().is(tok::kw___attribute)) && 641 D.SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() && 642 !D.SS.getScopeRep()->getAsNamespace() && 643 !D.SS.getScopeRep()->getAsNamespaceAlias()) { 644 SourceLocation IdLoc = ConsumeToken(); 645 ParsedType Type = 646 Actions.getInheritingConstructorName(D.SS, IdLoc, *LastII); 647 D.Name.setConstructorName(Type, IdLoc, IdLoc); 648 } else { 649 if (ParseUnqualifiedId( 650 D.SS, /*ObjectType=*/nullptr, 651 /*ObjectHadErrors=*/false, /*EnteringContext=*/false, 652 /*AllowDestructorName=*/true, 653 /*AllowConstructorName=*/ 654 !(Tok.is(tok::identifier) && NextToken().is(tok::equal)), 655 /*AllowDeductionGuide=*/false, nullptr, D.Name)) 656 return true; 657 } 658 659 if (TryConsumeToken(tok::ellipsis, D.EllipsisLoc)) 660 Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 661 ? diag::warn_cxx17_compat_using_declaration_pack 662 : diag::ext_using_declaration_pack); 663 664 return false; 665 } 666 667 /// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration. 668 /// Assumes that 'using' was already seen. 669 /// 670 /// using-declaration: [C++ 7.3.p3: namespace.udecl] 671 /// 'using' using-declarator-list[opt] ; 672 /// 673 /// using-declarator-list: [C++1z] 674 /// using-declarator '...'[opt] 675 /// using-declarator-list ',' using-declarator '...'[opt] 676 /// 677 /// using-declarator-list: [C++98-14] 678 /// using-declarator 679 /// 680 /// alias-declaration: C++11 [dcl.dcl]p1 681 /// 'using' identifier attribute-specifier-seq[opt] = type-id ; 682 /// 683 /// using-enum-declaration: [C++20, dcl.enum] 684 /// 'using' elaborated-enum-specifier ; 685 /// The terminal name of the elaborated-enum-specifier undergoes 686 /// ordinary lookup 687 /// 688 /// elaborated-enum-specifier: 689 /// 'enum' nested-name-specifier[opt] identifier 690 Parser::DeclGroupPtrTy Parser::ParseUsingDeclaration( 691 DeclaratorContext Context, const ParsedTemplateInfo &TemplateInfo, 692 SourceLocation UsingLoc, SourceLocation &DeclEnd, 693 ParsedAttributes &PrefixAttrs, AccessSpecifier AS) { 694 SourceLocation UELoc; 695 bool InInitStatement = Context == DeclaratorContext::SelectionInit || 696 Context == DeclaratorContext::ForInit; 697 698 if (TryConsumeToken(tok::kw_enum, UELoc) && !InInitStatement) { 699 // C++20 using-enum 700 Diag(UELoc, getLangOpts().CPlusPlus20 701 ? diag::warn_cxx17_compat_using_enum_declaration 702 : diag::ext_using_enum_declaration); 703 704 DiagnoseCXX11AttributeExtension(PrefixAttrs); 705 706 if (TemplateInfo.Kind) { 707 SourceRange R = TemplateInfo.getSourceRange(); 708 Diag(UsingLoc, diag::err_templated_using_directive_declaration) 709 << 1 /* declaration */ << R << FixItHint::CreateRemoval(R); 710 SkipUntil(tok::semi); 711 return nullptr; 712 } 713 CXXScopeSpec SS; 714 if (ParseOptionalCXXScopeSpecifier(SS, /*ParsedType=*/nullptr, 715 /*ObectHasErrors=*/false, 716 /*EnteringConttext=*/false, 717 /*MayBePseudoDestructor=*/nullptr, 718 /*IsTypename=*/false, 719 /*IdentifierInfo=*/nullptr, 720 /*OnlyNamespace=*/false, 721 /*InUsingDeclaration=*/true)) { 722 SkipUntil(tok::semi); 723 return nullptr; 724 } 725 726 if (Tok.is(tok::code_completion)) { 727 cutOffParsing(); 728 Actions.CodeCompleteUsing(getCurScope()); 729 return nullptr; 730 } 731 732 if (!Tok.is(tok::identifier)) { 733 Diag(Tok.getLocation(), diag::err_using_enum_expect_identifier) 734 << Tok.is(tok::kw_enum); 735 SkipUntil(tok::semi); 736 return nullptr; 737 } 738 IdentifierInfo *IdentInfo = Tok.getIdentifierInfo(); 739 SourceLocation IdentLoc = ConsumeToken(); 740 Decl *UED = Actions.ActOnUsingEnumDeclaration( 741 getCurScope(), AS, UsingLoc, UELoc, IdentLoc, *IdentInfo, &SS); 742 if (!UED) { 743 SkipUntil(tok::semi); 744 return nullptr; 745 } 746 747 DeclEnd = Tok.getLocation(); 748 if (ExpectAndConsume(tok::semi, diag::err_expected_after, 749 "using-enum declaration")) 750 SkipUntil(tok::semi); 751 752 return Actions.ConvertDeclToDeclGroup(UED); 753 } 754 755 // Check for misplaced attributes before the identifier in an 756 // alias-declaration. 757 ParsedAttributes MisplacedAttrs(AttrFactory); 758 MaybeParseCXX11Attributes(MisplacedAttrs); 759 760 if (InInitStatement && Tok.isNot(tok::identifier)) 761 return nullptr; 762 763 UsingDeclarator D; 764 bool InvalidDeclarator = ParseUsingDeclarator(Context, D); 765 766 ParsedAttributes Attrs(AttrFactory); 767 MaybeParseAttributes(PAKM_GNU | PAKM_CXX11, Attrs); 768 769 // If we had any misplaced attributes from earlier, this is where they 770 // should have been written. 771 if (MisplacedAttrs.Range.isValid()) { 772 auto *FirstAttr = 773 MisplacedAttrs.empty() ? nullptr : &MisplacedAttrs.front(); 774 auto &Range = MisplacedAttrs.Range; 775 (FirstAttr && FirstAttr->isRegularKeywordAttribute() 776 ? Diag(Range.getBegin(), diag::err_keyword_not_allowed) << FirstAttr 777 : Diag(Range.getBegin(), diag::err_attributes_not_allowed)) 778 << FixItHint::CreateInsertionFromRange( 779 Tok.getLocation(), CharSourceRange::getTokenRange(Range)) 780 << FixItHint::CreateRemoval(Range); 781 Attrs.takeAllFrom(MisplacedAttrs); 782 } 783 784 // Maybe this is an alias-declaration. 785 if (Tok.is(tok::equal) || InInitStatement) { 786 if (InvalidDeclarator) { 787 SkipUntil(tok::semi); 788 return nullptr; 789 } 790 791 ProhibitAttributes(PrefixAttrs); 792 793 Decl *DeclFromDeclSpec = nullptr; 794 Decl *AD = ParseAliasDeclarationAfterDeclarator( 795 TemplateInfo, UsingLoc, D, DeclEnd, AS, Attrs, &DeclFromDeclSpec); 796 return Actions.ConvertDeclToDeclGroup(AD, DeclFromDeclSpec); 797 } 798 799 DiagnoseCXX11AttributeExtension(PrefixAttrs); 800 801 // Diagnose an attempt to declare a templated using-declaration. 802 // In C++11, alias-declarations can be templates: 803 // template <...> using id = type; 804 if (TemplateInfo.Kind) { 805 SourceRange R = TemplateInfo.getSourceRange(); 806 Diag(UsingLoc, diag::err_templated_using_directive_declaration) 807 << 1 /* declaration */ << R << FixItHint::CreateRemoval(R); 808 809 // Unfortunately, we have to bail out instead of recovering by 810 // ignoring the parameters, just in case the nested name specifier 811 // depends on the parameters. 812 return nullptr; 813 } 814 815 SmallVector<Decl *, 8> DeclsInGroup; 816 while (true) { 817 // Parse (optional) attributes. 818 MaybeParseAttributes(PAKM_GNU | PAKM_CXX11, Attrs); 819 DiagnoseCXX11AttributeExtension(Attrs); 820 Attrs.addAll(PrefixAttrs.begin(), PrefixAttrs.end()); 821 822 if (InvalidDeclarator) 823 SkipUntil(tok::comma, tok::semi, StopBeforeMatch); 824 else { 825 // "typename" keyword is allowed for identifiers only, 826 // because it may be a type definition. 827 if (D.TypenameLoc.isValid() && 828 D.Name.getKind() != UnqualifiedIdKind::IK_Identifier) { 829 Diag(D.Name.getSourceRange().getBegin(), 830 diag::err_typename_identifiers_only) 831 << FixItHint::CreateRemoval(SourceRange(D.TypenameLoc)); 832 // Proceed parsing, but discard the typename keyword. 833 D.TypenameLoc = SourceLocation(); 834 } 835 836 Decl *UD = Actions.ActOnUsingDeclaration(getCurScope(), AS, UsingLoc, 837 D.TypenameLoc, D.SS, D.Name, 838 D.EllipsisLoc, Attrs); 839 if (UD) 840 DeclsInGroup.push_back(UD); 841 } 842 843 if (!TryConsumeToken(tok::comma)) 844 break; 845 846 // Parse another using-declarator. 847 Attrs.clear(); 848 InvalidDeclarator = ParseUsingDeclarator(Context, D); 849 } 850 851 if (DeclsInGroup.size() > 1) 852 Diag(Tok.getLocation(), 853 getLangOpts().CPlusPlus17 854 ? diag::warn_cxx17_compat_multi_using_declaration 855 : diag::ext_multi_using_declaration); 856 857 // Eat ';'. 858 DeclEnd = Tok.getLocation(); 859 if (ExpectAndConsume(tok::semi, diag::err_expected_after, 860 !Attrs.empty() ? "attributes list" 861 : UELoc.isValid() ? "using-enum declaration" 862 : "using declaration")) 863 SkipUntil(tok::semi); 864 865 return Actions.BuildDeclaratorGroup(DeclsInGroup); 866 } 867 868 Decl *Parser::ParseAliasDeclarationAfterDeclarator( 869 const ParsedTemplateInfo &TemplateInfo, SourceLocation UsingLoc, 870 UsingDeclarator &D, SourceLocation &DeclEnd, AccessSpecifier AS, 871 ParsedAttributes &Attrs, Decl **OwnedType) { 872 if (ExpectAndConsume(tok::equal)) { 873 SkipUntil(tok::semi); 874 return nullptr; 875 } 876 877 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 878 ? diag::warn_cxx98_compat_alias_declaration 879 : diag::ext_alias_declaration); 880 881 // Type alias templates cannot be specialized. 882 int SpecKind = -1; 883 if (TemplateInfo.Kind == ParsedTemplateInfo::Template && 884 D.Name.getKind() == UnqualifiedIdKind::IK_TemplateId) 885 SpecKind = 0; 886 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) 887 SpecKind = 1; 888 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) 889 SpecKind = 2; 890 if (SpecKind != -1) { 891 SourceRange Range; 892 if (SpecKind == 0) 893 Range = SourceRange(D.Name.TemplateId->LAngleLoc, 894 D.Name.TemplateId->RAngleLoc); 895 else 896 Range = TemplateInfo.getSourceRange(); 897 Diag(Range.getBegin(), diag::err_alias_declaration_specialization) 898 << SpecKind << Range; 899 SkipUntil(tok::semi); 900 return nullptr; 901 } 902 903 // Name must be an identifier. 904 if (D.Name.getKind() != UnqualifiedIdKind::IK_Identifier) { 905 Diag(D.Name.StartLocation, diag::err_alias_declaration_not_identifier); 906 // No removal fixit: can't recover from this. 907 SkipUntil(tok::semi); 908 return nullptr; 909 } else if (D.TypenameLoc.isValid()) 910 Diag(D.TypenameLoc, diag::err_alias_declaration_not_identifier) 911 << FixItHint::CreateRemoval( 912 SourceRange(D.TypenameLoc, D.SS.isNotEmpty() ? D.SS.getEndLoc() 913 : D.TypenameLoc)); 914 else if (D.SS.isNotEmpty()) 915 Diag(D.SS.getBeginLoc(), diag::err_alias_declaration_not_identifier) 916 << FixItHint::CreateRemoval(D.SS.getRange()); 917 if (D.EllipsisLoc.isValid()) 918 Diag(D.EllipsisLoc, diag::err_alias_declaration_pack_expansion) 919 << FixItHint::CreateRemoval(SourceRange(D.EllipsisLoc)); 920 921 Decl *DeclFromDeclSpec = nullptr; 922 TypeResult TypeAlias = 923 ParseTypeName(nullptr, 924 TemplateInfo.Kind ? DeclaratorContext::AliasTemplate 925 : DeclaratorContext::AliasDecl, 926 AS, &DeclFromDeclSpec, &Attrs); 927 if (OwnedType) 928 *OwnedType = DeclFromDeclSpec; 929 930 // Eat ';'. 931 DeclEnd = Tok.getLocation(); 932 if (ExpectAndConsume(tok::semi, diag::err_expected_after, 933 !Attrs.empty() ? "attributes list" 934 : "alias declaration")) 935 SkipUntil(tok::semi); 936 937 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; 938 MultiTemplateParamsArg TemplateParamsArg( 939 TemplateParams ? TemplateParams->data() : nullptr, 940 TemplateParams ? TemplateParams->size() : 0); 941 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg, 942 UsingLoc, D.Name, Attrs, TypeAlias, 943 DeclFromDeclSpec); 944 } 945 946 static FixItHint getStaticAssertNoMessageFixIt(const Expr *AssertExpr, 947 SourceLocation EndExprLoc) { 948 if (const auto *BO = dyn_cast_or_null<BinaryOperator>(AssertExpr)) { 949 if (BO->getOpcode() == BO_LAnd && 950 isa<StringLiteral>(BO->getRHS()->IgnoreImpCasts())) 951 return FixItHint::CreateReplacement(BO->getOperatorLoc(), ","); 952 } 953 return FixItHint::CreateInsertion(EndExprLoc, ", \"\""); 954 } 955 956 /// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration. 957 /// 958 /// [C++0x] static_assert-declaration: 959 /// static_assert ( constant-expression , string-literal ) ; 960 /// 961 /// [C11] static_assert-declaration: 962 /// _Static_assert ( constant-expression , string-literal ) ; 963 /// 964 Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd) { 965 assert(Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert) && 966 "Not a static_assert declaration"); 967 968 // Save the token name used for static assertion. 969 const char *TokName = Tok.getName(); 970 971 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11) 972 Diag(Tok, diag::ext_c11_feature) << Tok.getName(); 973 if (Tok.is(tok::kw_static_assert)) { 974 if (!getLangOpts().CPlusPlus) { 975 if (getLangOpts().C23) 976 Diag(Tok, diag::warn_c23_compat_keyword) << Tok.getName(); 977 else 978 Diag(Tok, diag::ext_ms_static_assert) << FixItHint::CreateReplacement( 979 Tok.getLocation(), "_Static_assert"); 980 } else 981 Diag(Tok, diag::warn_cxx98_compat_static_assert); 982 } 983 984 SourceLocation StaticAssertLoc = ConsumeToken(); 985 986 BalancedDelimiterTracker T(*this, tok::l_paren); 987 if (T.consumeOpen()) { 988 Diag(Tok, diag::err_expected) << tok::l_paren; 989 SkipMalformedDecl(); 990 return nullptr; 991 } 992 993 EnterExpressionEvaluationContext ConstantEvaluated( 994 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); 995 ExprResult AssertExpr(ParseConstantExpressionInExprEvalContext()); 996 if (AssertExpr.isInvalid()) { 997 SkipMalformedDecl(); 998 return nullptr; 999 } 1000 1001 ExprResult AssertMessage; 1002 if (Tok.is(tok::r_paren)) { 1003 unsigned DiagVal; 1004 if (getLangOpts().CPlusPlus17) 1005 DiagVal = diag::warn_cxx14_compat_static_assert_no_message; 1006 else if (getLangOpts().CPlusPlus) 1007 DiagVal = diag::ext_cxx_static_assert_no_message; 1008 else if (getLangOpts().C23) 1009 DiagVal = diag::warn_c17_compat_static_assert_no_message; 1010 else 1011 DiagVal = diag::ext_c_static_assert_no_message; 1012 Diag(Tok, DiagVal) << getStaticAssertNoMessageFixIt(AssertExpr.get(), 1013 Tok.getLocation()); 1014 } else { 1015 if (ExpectAndConsume(tok::comma)) { 1016 SkipUntil(tok::semi); 1017 return nullptr; 1018 } 1019 1020 bool ParseAsExpression = false; 1021 if (getLangOpts().CPlusPlus26) { 1022 for (unsigned I = 0;; ++I) { 1023 const Token &T = GetLookAheadToken(I); 1024 if (T.is(tok::r_paren)) 1025 break; 1026 if (!tokenIsLikeStringLiteral(T, getLangOpts()) || T.hasUDSuffix()) { 1027 ParseAsExpression = true; 1028 break; 1029 } 1030 } 1031 } 1032 1033 if (ParseAsExpression) 1034 AssertMessage = ParseConstantExpressionInExprEvalContext(); 1035 else if (tokenIsLikeStringLiteral(Tok, getLangOpts())) 1036 AssertMessage = ParseUnevaluatedStringLiteralExpression(); 1037 else { 1038 Diag(Tok, diag::err_expected_string_literal) 1039 << /*Source='static_assert'*/ 1; 1040 SkipMalformedDecl(); 1041 return nullptr; 1042 } 1043 1044 if (AssertMessage.isInvalid()) { 1045 SkipMalformedDecl(); 1046 return nullptr; 1047 } 1048 } 1049 1050 T.consumeClose(); 1051 1052 DeclEnd = Tok.getLocation(); 1053 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert, TokName); 1054 1055 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, AssertExpr.get(), 1056 AssertMessage.get(), 1057 T.getCloseLocation()); 1058 } 1059 1060 /// ParseDecltypeSpecifier - Parse a C++11 decltype specifier. 1061 /// 1062 /// 'decltype' ( expression ) 1063 /// 'decltype' ( 'auto' ) [C++1y] 1064 /// 1065 SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) { 1066 assert(Tok.isOneOf(tok::kw_decltype, tok::annot_decltype) && 1067 "Not a decltype specifier"); 1068 1069 ExprResult Result; 1070 SourceLocation StartLoc = Tok.getLocation(); 1071 SourceLocation EndLoc; 1072 1073 if (Tok.is(tok::annot_decltype)) { 1074 Result = getExprAnnotation(Tok); 1075 EndLoc = Tok.getAnnotationEndLoc(); 1076 // Unfortunately, we don't know the LParen source location as the annotated 1077 // token doesn't have it. 1078 DS.setTypeArgumentRange(SourceRange(SourceLocation(), EndLoc)); 1079 ConsumeAnnotationToken(); 1080 if (Result.isInvalid()) { 1081 DS.SetTypeSpecError(); 1082 return EndLoc; 1083 } 1084 } else { 1085 if (Tok.getIdentifierInfo()->isStr("decltype")) 1086 Diag(Tok, diag::warn_cxx98_compat_decltype); 1087 1088 ConsumeToken(); 1089 1090 BalancedDelimiterTracker T(*this, tok::l_paren); 1091 if (T.expectAndConsume(diag::err_expected_lparen_after, "decltype", 1092 tok::r_paren)) { 1093 DS.SetTypeSpecError(); 1094 return T.getOpenLocation() == Tok.getLocation() ? StartLoc 1095 : T.getOpenLocation(); 1096 } 1097 1098 // Check for C++1y 'decltype(auto)'. 1099 if (Tok.is(tok::kw_auto) && NextToken().is(tok::r_paren)) { 1100 // the typename-specifier in a function-style cast expression may 1101 // be 'auto' since C++23. 1102 Diag(Tok.getLocation(), 1103 getLangOpts().CPlusPlus14 1104 ? diag::warn_cxx11_compat_decltype_auto_type_specifier 1105 : diag::ext_decltype_auto_type_specifier); 1106 ConsumeToken(); 1107 } else { 1108 // Parse the expression 1109 1110 // C++11 [dcl.type.simple]p4: 1111 // The operand of the decltype specifier is an unevaluated operand. 1112 EnterExpressionEvaluationContext Unevaluated( 1113 Actions, Sema::ExpressionEvaluationContext::Unevaluated, nullptr, 1114 Sema::ExpressionEvaluationContextRecord::EK_Decltype); 1115 Result = Actions.CorrectDelayedTyposInExpr( 1116 ParseExpression(), /*InitDecl=*/nullptr, 1117 /*RecoverUncorrectedTypos=*/false, 1118 [](Expr *E) { return E->hasPlaceholderType() ? ExprError() : E; }); 1119 if (Result.isInvalid()) { 1120 DS.SetTypeSpecError(); 1121 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) { 1122 EndLoc = ConsumeParen(); 1123 } else { 1124 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) { 1125 // Backtrack to get the location of the last token before the semi. 1126 PP.RevertCachedTokens(2); 1127 ConsumeToken(); // the semi. 1128 EndLoc = ConsumeAnyToken(); 1129 assert(Tok.is(tok::semi)); 1130 } else { 1131 EndLoc = Tok.getLocation(); 1132 } 1133 } 1134 return EndLoc; 1135 } 1136 1137 Result = Actions.ActOnDecltypeExpression(Result.get()); 1138 } 1139 1140 // Match the ')' 1141 T.consumeClose(); 1142 DS.setTypeArgumentRange(T.getRange()); 1143 if (T.getCloseLocation().isInvalid()) { 1144 DS.SetTypeSpecError(); 1145 // FIXME: this should return the location of the last token 1146 // that was consumed (by "consumeClose()") 1147 return T.getCloseLocation(); 1148 } 1149 1150 if (Result.isInvalid()) { 1151 DS.SetTypeSpecError(); 1152 return T.getCloseLocation(); 1153 } 1154 1155 EndLoc = T.getCloseLocation(); 1156 } 1157 assert(!Result.isInvalid()); 1158 1159 const char *PrevSpec = nullptr; 1160 unsigned DiagID; 1161 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); 1162 // Check for duplicate type specifiers (e.g. "int decltype(a)"). 1163 if (Result.get() ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, 1164 PrevSpec, DiagID, Result.get(), Policy) 1165 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, 1166 PrevSpec, DiagID, Policy)) { 1167 Diag(StartLoc, DiagID) << PrevSpec; 1168 DS.SetTypeSpecError(); 1169 } 1170 return EndLoc; 1171 } 1172 1173 void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec &DS, 1174 SourceLocation StartLoc, 1175 SourceLocation EndLoc) { 1176 // make sure we have a token we can turn into an annotation token 1177 if (PP.isBacktrackEnabled()) { 1178 PP.RevertCachedTokens(1); 1179 if (DS.getTypeSpecType() == TST_error) { 1180 // We encountered an error in parsing 'decltype(...)' so lets annotate all 1181 // the tokens in the backtracking cache - that we likely had to skip over 1182 // to get to a token that allows us to resume parsing, such as a 1183 // semi-colon. 1184 EndLoc = PP.getLastCachedTokenLocation(); 1185 } 1186 } else 1187 PP.EnterToken(Tok, /*IsReinject*/ true); 1188 1189 Tok.setKind(tok::annot_decltype); 1190 setExprAnnotation(Tok, 1191 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() 1192 : DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() 1193 : ExprError()); 1194 Tok.setAnnotationEndLoc(EndLoc); 1195 Tok.setLocation(StartLoc); 1196 PP.AnnotateCachedTokens(Tok); 1197 } 1198 1199 DeclSpec::TST Parser::TypeTransformTokToDeclSpec() { 1200 switch (Tok.getKind()) { 1201 #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) \ 1202 case tok::kw___##Trait: \ 1203 return DeclSpec::TST_##Trait; 1204 #include "clang/Basic/TransformTypeTraits.def" 1205 default: 1206 llvm_unreachable("passed in an unhandled type transformation built-in"); 1207 } 1208 } 1209 1210 bool Parser::MaybeParseTypeTransformTypeSpecifier(DeclSpec &DS) { 1211 if (!NextToken().is(tok::l_paren)) { 1212 Tok.setKind(tok::identifier); 1213 return false; 1214 } 1215 DeclSpec::TST TypeTransformTST = TypeTransformTokToDeclSpec(); 1216 SourceLocation StartLoc = ConsumeToken(); 1217 1218 BalancedDelimiterTracker T(*this, tok::l_paren); 1219 if (T.expectAndConsume(diag::err_expected_lparen_after, Tok.getName(), 1220 tok::r_paren)) 1221 return true; 1222 1223 TypeResult Result = ParseTypeName(); 1224 if (Result.isInvalid()) { 1225 SkipUntil(tok::r_paren, StopAtSemi); 1226 return true; 1227 } 1228 1229 T.consumeClose(); 1230 if (T.getCloseLocation().isInvalid()) 1231 return true; 1232 1233 const char *PrevSpec = nullptr; 1234 unsigned DiagID; 1235 if (DS.SetTypeSpecType(TypeTransformTST, StartLoc, PrevSpec, DiagID, 1236 Result.get(), 1237 Actions.getASTContext().getPrintingPolicy())) 1238 Diag(StartLoc, DiagID) << PrevSpec; 1239 DS.setTypeArgumentRange(T.getRange()); 1240 return true; 1241 } 1242 1243 /// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a 1244 /// class name or decltype-specifier. Note that we only check that the result 1245 /// names a type; semantic analysis will need to verify that the type names a 1246 /// class. The result is either a type or null, depending on whether a type 1247 /// name was found. 1248 /// 1249 /// base-type-specifier: [C++11 class.derived] 1250 /// class-or-decltype 1251 /// class-or-decltype: [C++11 class.derived] 1252 /// nested-name-specifier[opt] class-name 1253 /// decltype-specifier 1254 /// class-name: [C++ class.name] 1255 /// identifier 1256 /// simple-template-id 1257 /// 1258 /// In C++98, instead of base-type-specifier, we have: 1259 /// 1260 /// ::[opt] nested-name-specifier[opt] class-name 1261 TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc, 1262 SourceLocation &EndLocation) { 1263 // Ignore attempts to use typename 1264 if (Tok.is(tok::kw_typename)) { 1265 Diag(Tok, diag::err_expected_class_name_not_template) 1266 << FixItHint::CreateRemoval(Tok.getLocation()); 1267 ConsumeToken(); 1268 } 1269 1270 // Parse optional nested-name-specifier 1271 CXXScopeSpec SS; 1272 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, 1273 /*ObjectHasErrors=*/false, 1274 /*EnteringContext=*/false)) 1275 return true; 1276 1277 BaseLoc = Tok.getLocation(); 1278 1279 // Parse decltype-specifier 1280 // tok == kw_decltype is just error recovery, it can only happen when SS 1281 // isn't empty 1282 if (Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)) { 1283 if (SS.isNotEmpty()) 1284 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype) 1285 << FixItHint::CreateRemoval(SS.getRange()); 1286 // Fake up a Declarator to use with ActOnTypeName. 1287 DeclSpec DS(AttrFactory); 1288 1289 EndLocation = ParseDecltypeSpecifier(DS); 1290 1291 Declarator DeclaratorInfo(DS, ParsedAttributesView::none(), 1292 DeclaratorContext::TypeName); 1293 return Actions.ActOnTypeName(DeclaratorInfo); 1294 } 1295 1296 // Check whether we have a template-id that names a type. 1297 if (Tok.is(tok::annot_template_id)) { 1298 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); 1299 if (TemplateId->mightBeType()) { 1300 AnnotateTemplateIdTokenAsType(SS, ImplicitTypenameContext::No, 1301 /*IsClassName=*/true); 1302 1303 assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); 1304 TypeResult Type = getTypeAnnotation(Tok); 1305 EndLocation = Tok.getAnnotationEndLoc(); 1306 ConsumeAnnotationToken(); 1307 return Type; 1308 } 1309 1310 // Fall through to produce an error below. 1311 } 1312 1313 if (Tok.isNot(tok::identifier)) { 1314 Diag(Tok, diag::err_expected_class_name); 1315 return true; 1316 } 1317 1318 IdentifierInfo *Id = Tok.getIdentifierInfo(); 1319 SourceLocation IdLoc = ConsumeToken(); 1320 1321 if (Tok.is(tok::less)) { 1322 // It looks the user intended to write a template-id here, but the 1323 // template-name was wrong. Try to fix that. 1324 // FIXME: Invoke ParseOptionalCXXScopeSpecifier in a "'template' is neither 1325 // required nor permitted" mode, and do this there. 1326 TemplateNameKind TNK = TNK_Non_template; 1327 TemplateTy Template; 1328 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(), &SS, 1329 Template, TNK)) { 1330 Diag(IdLoc, diag::err_unknown_template_name) << Id; 1331 } 1332 1333 // Form the template name 1334 UnqualifiedId TemplateName; 1335 TemplateName.setIdentifier(Id, IdLoc); 1336 1337 // Parse the full template-id, then turn it into a type. 1338 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(), 1339 TemplateName)) 1340 return true; 1341 if (Tok.is(tok::annot_template_id) && 1342 takeTemplateIdAnnotation(Tok)->mightBeType()) 1343 AnnotateTemplateIdTokenAsType(SS, ImplicitTypenameContext::No, 1344 /*IsClassName=*/true); 1345 1346 // If we didn't end up with a typename token, there's nothing more we 1347 // can do. 1348 if (Tok.isNot(tok::annot_typename)) 1349 return true; 1350 1351 // Retrieve the type from the annotation token, consume that token, and 1352 // return. 1353 EndLocation = Tok.getAnnotationEndLoc(); 1354 TypeResult Type = getTypeAnnotation(Tok); 1355 ConsumeAnnotationToken(); 1356 return Type; 1357 } 1358 1359 // We have an identifier; check whether it is actually a type. 1360 IdentifierInfo *CorrectedII = nullptr; 1361 ParsedType Type = Actions.getTypeName( 1362 *Id, IdLoc, getCurScope(), &SS, /*isClassName=*/true, false, nullptr, 1363 /*IsCtorOrDtorName=*/false, 1364 /*WantNontrivialTypeSourceInfo=*/true, 1365 /*IsClassTemplateDeductionContext=*/false, ImplicitTypenameContext::No, 1366 &CorrectedII); 1367 if (!Type) { 1368 Diag(IdLoc, diag::err_expected_class_name); 1369 return true; 1370 } 1371 1372 // Consume the identifier. 1373 EndLocation = IdLoc; 1374 1375 // Fake up a Declarator to use with ActOnTypeName. 1376 DeclSpec DS(AttrFactory); 1377 DS.SetRangeStart(IdLoc); 1378 DS.SetRangeEnd(EndLocation); 1379 DS.getTypeSpecScope() = SS; 1380 1381 const char *PrevSpec = nullptr; 1382 unsigned DiagID; 1383 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type, 1384 Actions.getASTContext().getPrintingPolicy()); 1385 1386 Declarator DeclaratorInfo(DS, ParsedAttributesView::none(), 1387 DeclaratorContext::TypeName); 1388 return Actions.ActOnTypeName(DeclaratorInfo); 1389 } 1390 1391 void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) { 1392 while (Tok.isOneOf(tok::kw___single_inheritance, 1393 tok::kw___multiple_inheritance, 1394 tok::kw___virtual_inheritance)) { 1395 IdentifierInfo *AttrName = Tok.getIdentifierInfo(); 1396 auto Kind = Tok.getKind(); 1397 SourceLocation AttrNameLoc = ConsumeToken(); 1398 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, Kind); 1399 } 1400 } 1401 1402 /// Determine whether the following tokens are valid after a type-specifier 1403 /// which could be a standalone declaration. This will conservatively return 1404 /// true if there's any doubt, and is appropriate for insert-';' fixits. 1405 bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) { 1406 // This switch enumerates the valid "follow" set for type-specifiers. 1407 switch (Tok.getKind()) { 1408 default: 1409 if (Tok.isRegularKeywordAttribute()) 1410 return true; 1411 break; 1412 case tok::semi: // struct foo {...} ; 1413 case tok::star: // struct foo {...} * P; 1414 case tok::amp: // struct foo {...} & R = ... 1415 case tok::ampamp: // struct foo {...} && R = ... 1416 case tok::identifier: // struct foo {...} V ; 1417 case tok::r_paren: //(struct foo {...} ) {4} 1418 case tok::coloncolon: // struct foo {...} :: a::b; 1419 case tok::annot_cxxscope: // struct foo {...} a:: b; 1420 case tok::annot_typename: // struct foo {...} a ::b; 1421 case tok::annot_template_id: // struct foo {...} a<int> ::b; 1422 case tok::kw_decltype: // struct foo {...} decltype (a)::b; 1423 case tok::l_paren: // struct foo {...} ( x); 1424 case tok::comma: // __builtin_offsetof(struct foo{...} , 1425 case tok::kw_operator: // struct foo operator ++() {...} 1426 case tok::kw___declspec: // struct foo {...} __declspec(...) 1427 case tok::l_square: // void f(struct f [ 3]) 1428 case tok::ellipsis: // void f(struct f ... [Ns]) 1429 // FIXME: we should emit semantic diagnostic when declaration 1430 // attribute is in type attribute position. 1431 case tok::kw___attribute: // struct foo __attribute__((used)) x; 1432 case tok::annot_pragma_pack: // struct foo {...} _Pragma(pack(pop)); 1433 // struct foo {...} _Pragma(section(...)); 1434 case tok::annot_pragma_ms_pragma: 1435 // struct foo {...} _Pragma(vtordisp(pop)); 1436 case tok::annot_pragma_ms_vtordisp: 1437 // struct foo {...} _Pragma(pointers_to_members(...)); 1438 case tok::annot_pragma_ms_pointers_to_members: 1439 return true; 1440 case tok::colon: 1441 return CouldBeBitfield || // enum E { ... } : 2; 1442 ColonIsSacred; // _Generic(..., enum E : 2); 1443 // Microsoft compatibility 1444 case tok::kw___cdecl: // struct foo {...} __cdecl x; 1445 case tok::kw___fastcall: // struct foo {...} __fastcall x; 1446 case tok::kw___stdcall: // struct foo {...} __stdcall x; 1447 case tok::kw___thiscall: // struct foo {...} __thiscall x; 1448 case tok::kw___vectorcall: // struct foo {...} __vectorcall x; 1449 // We will diagnose these calling-convention specifiers on non-function 1450 // declarations later, so claim they are valid after a type specifier. 1451 return getLangOpts().MicrosoftExt; 1452 // Type qualifiers 1453 case tok::kw_const: // struct foo {...} const x; 1454 case tok::kw_volatile: // struct foo {...} volatile x; 1455 case tok::kw_restrict: // struct foo {...} restrict x; 1456 case tok::kw__Atomic: // struct foo {...} _Atomic x; 1457 case tok::kw___unaligned: // struct foo {...} __unaligned *x; 1458 // Function specifiers 1459 // Note, no 'explicit'. An explicit function must be either a conversion 1460 // operator or a constructor. Either way, it can't have a return type. 1461 case tok::kw_inline: // struct foo inline f(); 1462 case tok::kw_virtual: // struct foo virtual f(); 1463 case tok::kw_friend: // struct foo friend f(); 1464 // Storage-class specifiers 1465 case tok::kw_static: // struct foo {...} static x; 1466 case tok::kw_extern: // struct foo {...} extern x; 1467 case tok::kw_typedef: // struct foo {...} typedef x; 1468 case tok::kw_register: // struct foo {...} register x; 1469 case tok::kw_auto: // struct foo {...} auto x; 1470 case tok::kw_mutable: // struct foo {...} mutable x; 1471 case tok::kw_thread_local: // struct foo {...} thread_local x; 1472 case tok::kw_constexpr: // struct foo {...} constexpr x; 1473 case tok::kw_consteval: // struct foo {...} consteval x; 1474 case tok::kw_constinit: // struct foo {...} constinit x; 1475 // As shown above, type qualifiers and storage class specifiers absolutely 1476 // can occur after class specifiers according to the grammar. However, 1477 // almost no one actually writes code like this. If we see one of these, 1478 // it is much more likely that someone missed a semi colon and the 1479 // type/storage class specifier we're seeing is part of the *next* 1480 // intended declaration, as in: 1481 // 1482 // struct foo { ... } 1483 // typedef int X; 1484 // 1485 // We'd really like to emit a missing semicolon error instead of emitting 1486 // an error on the 'int' saying that you can't have two type specifiers in 1487 // the same declaration of X. Because of this, we look ahead past this 1488 // token to see if it's a type specifier. If so, we know the code is 1489 // otherwise invalid, so we can produce the expected semi error. 1490 if (!isKnownToBeTypeSpecifier(NextToken())) 1491 return true; 1492 break; 1493 case tok::r_brace: // struct bar { struct foo {...} } 1494 // Missing ';' at end of struct is accepted as an extension in C mode. 1495 if (!getLangOpts().CPlusPlus) 1496 return true; 1497 break; 1498 case tok::greater: 1499 // template<class T = class X> 1500 return getLangOpts().CPlusPlus; 1501 } 1502 return false; 1503 } 1504 1505 /// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or 1506 /// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which 1507 /// until we reach the start of a definition or see a token that 1508 /// cannot start a definition. 1509 /// 1510 /// class-specifier: [C++ class] 1511 /// class-head '{' member-specification[opt] '}' 1512 /// class-head '{' member-specification[opt] '}' attributes[opt] 1513 /// class-head: 1514 /// class-key identifier[opt] base-clause[opt] 1515 /// class-key nested-name-specifier identifier base-clause[opt] 1516 /// class-key nested-name-specifier[opt] simple-template-id 1517 /// base-clause[opt] 1518 /// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] 1519 /// [GNU] class-key attributes[opt] nested-name-specifier 1520 /// identifier base-clause[opt] 1521 /// [GNU] class-key attributes[opt] nested-name-specifier[opt] 1522 /// simple-template-id base-clause[opt] 1523 /// class-key: 1524 /// 'class' 1525 /// 'struct' 1526 /// 'union' 1527 /// 1528 /// elaborated-type-specifier: [C++ dcl.type.elab] 1529 /// class-key ::[opt] nested-name-specifier[opt] identifier 1530 /// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] 1531 /// simple-template-id 1532 /// 1533 /// Note that the C++ class-specifier and elaborated-type-specifier, 1534 /// together, subsume the C99 struct-or-union-specifier: 1535 /// 1536 /// struct-or-union-specifier: [C99 6.7.2.1] 1537 /// struct-or-union identifier[opt] '{' struct-contents '}' 1538 /// struct-or-union identifier 1539 /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents 1540 /// '}' attributes[opt] 1541 /// [GNU] struct-or-union attributes[opt] identifier 1542 /// struct-or-union: 1543 /// 'struct' 1544 /// 'union' 1545 void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, 1546 SourceLocation StartLoc, DeclSpec &DS, 1547 const ParsedTemplateInfo &TemplateInfo, 1548 AccessSpecifier AS, bool EnteringContext, 1549 DeclSpecContext DSC, 1550 ParsedAttributes &Attributes) { 1551 DeclSpec::TST TagType; 1552 if (TagTokKind == tok::kw_struct) 1553 TagType = DeclSpec::TST_struct; 1554 else if (TagTokKind == tok::kw___interface) 1555 TagType = DeclSpec::TST_interface; 1556 else if (TagTokKind == tok::kw_class) 1557 TagType = DeclSpec::TST_class; 1558 else { 1559 assert(TagTokKind == tok::kw_union && "Not a class specifier"); 1560 TagType = DeclSpec::TST_union; 1561 } 1562 1563 if (Tok.is(tok::code_completion)) { 1564 // Code completion for a struct, class, or union name. 1565 cutOffParsing(); 1566 Actions.CodeCompleteTag(getCurScope(), TagType); 1567 return; 1568 } 1569 1570 // C++20 [temp.class.spec] 13.7.5/10 1571 // The usual access checking rules do not apply to non-dependent names 1572 // used to specify template arguments of the simple-template-id of the 1573 // partial specialization. 1574 // C++20 [temp.spec] 13.9/6: 1575 // The usual access checking rules do not apply to names in a declaration 1576 // of an explicit instantiation or explicit specialization... 1577 const bool shouldDelayDiagsInTag = 1578 (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate); 1579 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); 1580 1581 ParsedAttributes attrs(AttrFactory); 1582 // If attributes exist after tag, parse them. 1583 MaybeParseAttributes(PAKM_CXX11 | PAKM_Declspec | PAKM_GNU, attrs); 1584 1585 // Parse inheritance specifiers. 1586 if (Tok.isOneOf(tok::kw___single_inheritance, tok::kw___multiple_inheritance, 1587 tok::kw___virtual_inheritance)) 1588 ParseMicrosoftInheritanceClassAttributes(attrs); 1589 1590 // Allow attributes to precede or succeed the inheritance specifiers. 1591 MaybeParseAttributes(PAKM_CXX11 | PAKM_Declspec | PAKM_GNU, attrs); 1592 1593 // Source location used by FIXIT to insert misplaced 1594 // C++11 attributes 1595 SourceLocation AttrFixitLoc = Tok.getLocation(); 1596 1597 if (TagType == DeclSpec::TST_struct && Tok.isNot(tok::identifier) && 1598 !Tok.isAnnotation() && Tok.getIdentifierInfo() && 1599 Tok.isOneOf( 1600 #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) tok::kw___##Trait, 1601 #include "clang/Basic/TransformTypeTraits.def" 1602 tok::kw___is_abstract, 1603 tok::kw___is_aggregate, 1604 tok::kw___is_arithmetic, 1605 tok::kw___is_array, 1606 tok::kw___is_assignable, 1607 tok::kw___is_base_of, 1608 tok::kw___is_bounded_array, 1609 tok::kw___is_class, 1610 tok::kw___is_complete_type, 1611 tok::kw___is_compound, 1612 tok::kw___is_const, 1613 tok::kw___is_constructible, 1614 tok::kw___is_convertible, 1615 tok::kw___is_convertible_to, 1616 tok::kw___is_destructible, 1617 tok::kw___is_empty, 1618 tok::kw___is_enum, 1619 tok::kw___is_floating_point, 1620 tok::kw___is_final, 1621 tok::kw___is_function, 1622 tok::kw___is_fundamental, 1623 tok::kw___is_integral, 1624 tok::kw___is_interface_class, 1625 tok::kw___is_literal, 1626 tok::kw___is_lvalue_expr, 1627 tok::kw___is_lvalue_reference, 1628 tok::kw___is_member_function_pointer, 1629 tok::kw___is_member_object_pointer, 1630 tok::kw___is_member_pointer, 1631 tok::kw___is_nothrow_assignable, 1632 tok::kw___is_nothrow_constructible, 1633 tok::kw___is_nothrow_destructible, 1634 tok::kw___is_nullptr, 1635 tok::kw___is_object, 1636 tok::kw___is_pod, 1637 tok::kw___is_pointer, 1638 tok::kw___is_polymorphic, 1639 tok::kw___is_reference, 1640 tok::kw___is_referenceable, 1641 tok::kw___is_rvalue_expr, 1642 tok::kw___is_rvalue_reference, 1643 tok::kw___is_same, 1644 tok::kw___is_scalar, 1645 tok::kw___is_scoped_enum, 1646 tok::kw___is_sealed, 1647 tok::kw___is_signed, 1648 tok::kw___is_standard_layout, 1649 tok::kw___is_trivial, 1650 tok::kw___is_trivially_equality_comparable, 1651 tok::kw___is_trivially_assignable, 1652 tok::kw___is_trivially_constructible, 1653 tok::kw___is_trivially_copyable, 1654 tok::kw___is_unbounded_array, 1655 tok::kw___is_union, 1656 tok::kw___is_unsigned, 1657 tok::kw___is_void, 1658 tok::kw___is_volatile, 1659 tok::kw___reference_binds_to_temporary, 1660 tok::kw___reference_constructs_from_temporary)) 1661 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the 1662 // name of struct templates, but some are keywords in GCC >= 4.3 1663 // and Clang. Therefore, when we see the token sequence "struct 1664 // X", make X into a normal identifier rather than a keyword, to 1665 // allow libstdc++ 4.2 and libc++ to work properly. 1666 TryKeywordIdentFallback(true); 1667 1668 struct PreserveAtomicIdentifierInfoRAII { 1669 PreserveAtomicIdentifierInfoRAII(Token &Tok, bool Enabled) 1670 : AtomicII(nullptr) { 1671 if (!Enabled) 1672 return; 1673 assert(Tok.is(tok::kw__Atomic)); 1674 AtomicII = Tok.getIdentifierInfo(); 1675 AtomicII->revertTokenIDToIdentifier(); 1676 Tok.setKind(tok::identifier); 1677 } 1678 ~PreserveAtomicIdentifierInfoRAII() { 1679 if (!AtomicII) 1680 return; 1681 AtomicII->revertIdentifierToTokenID(tok::kw__Atomic); 1682 } 1683 IdentifierInfo *AtomicII; 1684 }; 1685 1686 // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL 1687 // implementation for VS2013 uses _Atomic as an identifier for one of the 1688 // classes in <atomic>. When we are parsing 'struct _Atomic', don't consider 1689 // '_Atomic' to be a keyword. We are careful to undo this so that clang can 1690 // use '_Atomic' in its own header files. 1691 bool ShouldChangeAtomicToIdentifier = getLangOpts().MSVCCompat && 1692 Tok.is(tok::kw__Atomic) && 1693 TagType == DeclSpec::TST_struct; 1694 PreserveAtomicIdentifierInfoRAII AtomicTokenGuard( 1695 Tok, ShouldChangeAtomicToIdentifier); 1696 1697 // Parse the (optional) nested-name-specifier. 1698 CXXScopeSpec &SS = DS.getTypeSpecScope(); 1699 if (getLangOpts().CPlusPlus) { 1700 // "FOO : BAR" is not a potential typo for "FOO::BAR". In this context it 1701 // is a base-specifier-list. 1702 ColonProtectionRAIIObject X(*this); 1703 1704 CXXScopeSpec Spec; 1705 if (TemplateInfo.TemplateParams) 1706 Spec.setTemplateParamLists(*TemplateInfo.TemplateParams); 1707 1708 bool HasValidSpec = true; 1709 if (ParseOptionalCXXScopeSpecifier(Spec, /*ObjectType=*/nullptr, 1710 /*ObjectHasErrors=*/false, 1711 EnteringContext)) { 1712 DS.SetTypeSpecError(); 1713 HasValidSpec = false; 1714 } 1715 if (Spec.isSet()) 1716 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) { 1717 Diag(Tok, diag::err_expected) << tok::identifier; 1718 HasValidSpec = false; 1719 } 1720 if (HasValidSpec) 1721 SS = Spec; 1722 } 1723 1724 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; 1725 1726 auto RecoverFromUndeclaredTemplateName = [&](IdentifierInfo *Name, 1727 SourceLocation NameLoc, 1728 SourceRange TemplateArgRange, 1729 bool KnownUndeclared) { 1730 Diag(NameLoc, diag::err_explicit_spec_non_template) 1731 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) 1732 << TagTokKind << Name << TemplateArgRange << KnownUndeclared; 1733 1734 // Strip off the last template parameter list if it was empty, since 1735 // we've removed its template argument list. 1736 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) { 1737 if (TemplateParams->size() > 1) { 1738 TemplateParams->pop_back(); 1739 } else { 1740 TemplateParams = nullptr; 1741 const_cast<ParsedTemplateInfo &>(TemplateInfo).Kind = 1742 ParsedTemplateInfo::NonTemplate; 1743 } 1744 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { 1745 // Pretend this is just a forward declaration. 1746 TemplateParams = nullptr; 1747 const_cast<ParsedTemplateInfo &>(TemplateInfo).Kind = 1748 ParsedTemplateInfo::NonTemplate; 1749 const_cast<ParsedTemplateInfo &>(TemplateInfo).TemplateLoc = 1750 SourceLocation(); 1751 const_cast<ParsedTemplateInfo &>(TemplateInfo).ExternLoc = 1752 SourceLocation(); 1753 } 1754 }; 1755 1756 // Parse the (optional) class name or simple-template-id. 1757 IdentifierInfo *Name = nullptr; 1758 SourceLocation NameLoc; 1759 TemplateIdAnnotation *TemplateId = nullptr; 1760 if (Tok.is(tok::identifier)) { 1761 Name = Tok.getIdentifierInfo(); 1762 NameLoc = ConsumeToken(); 1763 1764 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) { 1765 // The name was supposed to refer to a template, but didn't. 1766 // Eat the template argument list and try to continue parsing this as 1767 // a class (or template thereof). 1768 TemplateArgList TemplateArgs; 1769 SourceLocation LAngleLoc, RAngleLoc; 1770 if (ParseTemplateIdAfterTemplateName(true, LAngleLoc, TemplateArgs, 1771 RAngleLoc)) { 1772 // We couldn't parse the template argument list at all, so don't 1773 // try to give any location information for the list. 1774 LAngleLoc = RAngleLoc = SourceLocation(); 1775 } 1776 RecoverFromUndeclaredTemplateName( 1777 Name, NameLoc, SourceRange(LAngleLoc, RAngleLoc), false); 1778 } 1779 } else if (Tok.is(tok::annot_template_id)) { 1780 TemplateId = takeTemplateIdAnnotation(Tok); 1781 NameLoc = ConsumeAnnotationToken(); 1782 1783 if (TemplateId->Kind == TNK_Undeclared_template) { 1784 // Try to resolve the template name to a type template. May update Kind. 1785 Actions.ActOnUndeclaredTypeTemplateName( 1786 getCurScope(), TemplateId->Template, TemplateId->Kind, NameLoc, Name); 1787 if (TemplateId->Kind == TNK_Undeclared_template) { 1788 RecoverFromUndeclaredTemplateName( 1789 Name, NameLoc, 1790 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc), true); 1791 TemplateId = nullptr; 1792 } 1793 } 1794 1795 if (TemplateId && !TemplateId->mightBeType()) { 1796 // The template-name in the simple-template-id refers to 1797 // something other than a type template. Give an appropriate 1798 // error message and skip to the ';'. 1799 SourceRange Range(NameLoc); 1800 if (SS.isNotEmpty()) 1801 Range.setBegin(SS.getBeginLoc()); 1802 1803 // FIXME: Name may be null here. 1804 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template) 1805 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range; 1806 1807 DS.SetTypeSpecError(); 1808 SkipUntil(tok::semi, StopBeforeMatch); 1809 return; 1810 } 1811 } 1812 1813 // There are four options here. 1814 // - If we are in a trailing return type, this is always just a reference, 1815 // and we must not try to parse a definition. For instance, 1816 // [] () -> struct S { }; 1817 // does not define a type. 1818 // - If we have 'struct foo {...', 'struct foo :...', 1819 // 'struct foo final :' or 'struct foo final {', then this is a definition. 1820 // - If we have 'struct foo;', then this is either a forward declaration 1821 // or a friend declaration, which have to be treated differently. 1822 // - Otherwise we have something like 'struct foo xyz', a reference. 1823 // 1824 // We also detect these erroneous cases to provide better diagnostic for 1825 // C++11 attributes parsing. 1826 // - attributes follow class name: 1827 // struct foo [[]] {}; 1828 // - attributes appear before or after 'final': 1829 // struct foo [[]] final [[]] {}; 1830 // 1831 // However, in type-specifier-seq's, things look like declarations but are 1832 // just references, e.g. 1833 // new struct s; 1834 // or 1835 // &T::operator struct s; 1836 // For these, DSC is DeclSpecContext::DSC_type_specifier or 1837 // DeclSpecContext::DSC_alias_declaration. 1838 1839 // If there are attributes after class name, parse them. 1840 MaybeParseCXX11Attributes(Attributes); 1841 1842 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); 1843 Sema::TagUseKind TUK; 1844 if (isDefiningTypeSpecifierContext(DSC, getLangOpts().CPlusPlus) == 1845 AllowDefiningTypeSpec::No || 1846 (getLangOpts().OpenMP && OpenMPDirectiveParsing)) 1847 TUK = Sema::TUK_Reference; 1848 else if (Tok.is(tok::l_brace) || 1849 (DSC != DeclSpecContext::DSC_association && 1850 getLangOpts().CPlusPlus && Tok.is(tok::colon)) || 1851 (isClassCompatibleKeyword() && 1852 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) { 1853 if (DS.isFriendSpecified()) { 1854 // C++ [class.friend]p2: 1855 // A class shall not be defined in a friend declaration. 1856 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) 1857 << SourceRange(DS.getFriendSpecLoc()); 1858 1859 // Skip everything up to the semicolon, so that this looks like a proper 1860 // friend class (or template thereof) declaration. 1861 SkipUntil(tok::semi, StopBeforeMatch); 1862 TUK = Sema::TUK_Friend; 1863 } else { 1864 // Okay, this is a class definition. 1865 TUK = Sema::TUK_Definition; 1866 } 1867 } else if (isClassCompatibleKeyword() && 1868 (NextToken().is(tok::l_square) || 1869 NextToken().is(tok::kw_alignas) || 1870 NextToken().isRegularKeywordAttribute() || 1871 isCXX11VirtSpecifier(NextToken()) != VirtSpecifiers::VS_None)) { 1872 // We can't tell if this is a definition or reference 1873 // until we skipped the 'final' and C++11 attribute specifiers. 1874 TentativeParsingAction PA(*this); 1875 1876 // Skip the 'final', abstract'... keywords. 1877 while (isClassCompatibleKeyword()) { 1878 ConsumeToken(); 1879 } 1880 1881 // Skip C++11 attribute specifiers. 1882 while (true) { 1883 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) { 1884 ConsumeBracket(); 1885 if (!SkipUntil(tok::r_square, StopAtSemi)) 1886 break; 1887 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) { 1888 ConsumeToken(); 1889 ConsumeParen(); 1890 if (!SkipUntil(tok::r_paren, StopAtSemi)) 1891 break; 1892 } else if (Tok.isRegularKeywordAttribute()) { 1893 bool TakesArgs = doesKeywordAttributeTakeArgs(Tok.getKind()); 1894 ConsumeToken(); 1895 if (TakesArgs) { 1896 BalancedDelimiterTracker T(*this, tok::l_paren); 1897 if (!T.consumeOpen()) 1898 T.skipToEnd(); 1899 } 1900 } else { 1901 break; 1902 } 1903 } 1904 1905 if (Tok.isOneOf(tok::l_brace, tok::colon)) 1906 TUK = Sema::TUK_Definition; 1907 else 1908 TUK = Sema::TUK_Reference; 1909 1910 PA.Revert(); 1911 } else if (!isTypeSpecifier(DSC) && 1912 (Tok.is(tok::semi) || 1913 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) { 1914 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; 1915 if (Tok.isNot(tok::semi)) { 1916 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy(); 1917 // A semicolon was missing after this declaration. Diagnose and recover. 1918 ExpectAndConsume(tok::semi, diag::err_expected_after, 1919 DeclSpec::getSpecifierName(TagType, PPol)); 1920 PP.EnterToken(Tok, /*IsReinject*/ true); 1921 Tok.setKind(tok::semi); 1922 } 1923 } else 1924 TUK = Sema::TUK_Reference; 1925 1926 // Forbid misplaced attributes. In cases of a reference, we pass attributes 1927 // to caller to handle. 1928 if (TUK != Sema::TUK_Reference) { 1929 // If this is not a reference, then the only possible 1930 // valid place for C++11 attributes to appear here 1931 // is between class-key and class-name. If there are 1932 // any attributes after class-name, we try a fixit to move 1933 // them to the right place. 1934 SourceRange AttrRange = Attributes.Range; 1935 if (AttrRange.isValid()) { 1936 auto *FirstAttr = Attributes.empty() ? nullptr : &Attributes.front(); 1937 auto Loc = AttrRange.getBegin(); 1938 (FirstAttr && FirstAttr->isRegularKeywordAttribute() 1939 ? Diag(Loc, diag::err_keyword_not_allowed) << FirstAttr 1940 : Diag(Loc, diag::err_attributes_not_allowed)) 1941 << AttrRange 1942 << FixItHint::CreateInsertionFromRange( 1943 AttrFixitLoc, CharSourceRange(AttrRange, true)) 1944 << FixItHint::CreateRemoval(AttrRange); 1945 1946 // Recover by adding misplaced attributes to the attribute list 1947 // of the class so they can be applied on the class later. 1948 attrs.takeAllFrom(Attributes); 1949 } 1950 } 1951 1952 if (!Name && !TemplateId && 1953 (DS.getTypeSpecType() == DeclSpec::TST_error || 1954 TUK != Sema::TUK_Definition)) { 1955 if (DS.getTypeSpecType() != DeclSpec::TST_error) { 1956 // We have a declaration or reference to an anonymous class. 1957 Diag(StartLoc, diag::err_anon_type_definition) 1958 << DeclSpec::getSpecifierName(TagType, Policy); 1959 } 1960 1961 // If we are parsing a definition and stop at a base-clause, continue on 1962 // until the semicolon. Continuing from the comma will just trick us into 1963 // thinking we are seeing a variable declaration. 1964 if (TUK == Sema::TUK_Definition && Tok.is(tok::colon)) 1965 SkipUntil(tok::semi, StopBeforeMatch); 1966 else 1967 SkipUntil(tok::comma, StopAtSemi); 1968 return; 1969 } 1970 1971 // Create the tag portion of the class or class template. 1972 DeclResult TagOrTempResult = true; // invalid 1973 TypeResult TypeResult = true; // invalid 1974 1975 bool Owned = false; 1976 Sema::SkipBodyInfo SkipBody; 1977 if (TemplateId) { 1978 // Explicit specialization, class template partial specialization, 1979 // or explicit instantiation. 1980 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), 1981 TemplateId->NumArgs); 1982 if (TemplateId->isInvalid()) { 1983 // Can't build the declaration. 1984 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && 1985 TUK == Sema::TUK_Declaration) { 1986 // This is an explicit instantiation of a class template. 1987 ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed, 1988 diag::err_keyword_not_allowed, 1989 /*DiagnoseEmptyAttrs=*/true); 1990 1991 TagOrTempResult = Actions.ActOnExplicitInstantiation( 1992 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, 1993 TagType, StartLoc, SS, TemplateId->Template, 1994 TemplateId->TemplateNameLoc, TemplateId->LAngleLoc, TemplateArgsPtr, 1995 TemplateId->RAngleLoc, attrs); 1996 1997 // Friend template-ids are treated as references unless 1998 // they have template headers, in which case they're ill-formed 1999 // (FIXME: "template <class T> friend class A<T>::B<int>;"). 2000 // We diagnose this error in ActOnClassTemplateSpecialization. 2001 } else if (TUK == Sema::TUK_Reference || 2002 (TUK == Sema::TUK_Friend && 2003 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) { 2004 ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed, 2005 diag::err_keyword_not_allowed, 2006 /*DiagnoseEmptyAttrs=*/true); 2007 TypeResult = Actions.ActOnTagTemplateIdType( 2008 TUK, TagType, StartLoc, SS, TemplateId->TemplateKWLoc, 2009 TemplateId->Template, TemplateId->TemplateNameLoc, 2010 TemplateId->LAngleLoc, TemplateArgsPtr, TemplateId->RAngleLoc); 2011 } else { 2012 // This is an explicit specialization or a class template 2013 // partial specialization. 2014 TemplateParameterLists FakedParamLists; 2015 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { 2016 // This looks like an explicit instantiation, because we have 2017 // something like 2018 // 2019 // template class Foo<X> 2020 // 2021 // but it actually has a definition. Most likely, this was 2022 // meant to be an explicit specialization, but the user forgot 2023 // the '<>' after 'template'. 2024 // It this is friend declaration however, since it cannot have a 2025 // template header, it is most likely that the user meant to 2026 // remove the 'template' keyword. 2027 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) && 2028 "Expected a definition here"); 2029 2030 if (TUK == Sema::TUK_Friend) { 2031 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation); 2032 TemplateParams = nullptr; 2033 } else { 2034 SourceLocation LAngleLoc = 2035 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); 2036 Diag(TemplateId->TemplateNameLoc, 2037 diag::err_explicit_instantiation_with_definition) 2038 << SourceRange(TemplateInfo.TemplateLoc) 2039 << FixItHint::CreateInsertion(LAngleLoc, "<>"); 2040 2041 // Create a fake template parameter list that contains only 2042 // "template<>", so that we treat this construct as a class 2043 // template specialization. 2044 FakedParamLists.push_back(Actions.ActOnTemplateParameterList( 2045 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 2046 std::nullopt, LAngleLoc, nullptr)); 2047 TemplateParams = &FakedParamLists; 2048 } 2049 } 2050 2051 // Build the class template specialization. 2052 TagOrTempResult = Actions.ActOnClassTemplateSpecialization( 2053 getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(), 2054 SS, *TemplateId, attrs, 2055 MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0] 2056 : nullptr, 2057 TemplateParams ? TemplateParams->size() : 0), 2058 &SkipBody); 2059 } 2060 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && 2061 TUK == Sema::TUK_Declaration) { 2062 // Explicit instantiation of a member of a class template 2063 // specialization, e.g., 2064 // 2065 // template struct Outer<int>::Inner; 2066 // 2067 ProhibitAttributes(attrs); 2068 2069 TagOrTempResult = Actions.ActOnExplicitInstantiation( 2070 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, 2071 TagType, StartLoc, SS, Name, NameLoc, attrs); 2072 } else if (TUK == Sema::TUK_Friend && 2073 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) { 2074 ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed, 2075 diag::err_keyword_not_allowed, 2076 /*DiagnoseEmptyAttrs=*/true); 2077 2078 TagOrTempResult = Actions.ActOnTemplatedFriendTag( 2079 getCurScope(), DS.getFriendSpecLoc(), TagType, StartLoc, SS, Name, 2080 NameLoc, attrs, 2081 MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0] : nullptr, 2082 TemplateParams ? TemplateParams->size() : 0)); 2083 } else { 2084 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition) 2085 ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed, 2086 diag::err_keyword_not_allowed, 2087 /* DiagnoseEmptyAttrs=*/true); 2088 2089 if (TUK == Sema::TUK_Definition && 2090 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { 2091 // If the declarator-id is not a template-id, issue a diagnostic and 2092 // recover by ignoring the 'template' keyword. 2093 Diag(Tok, diag::err_template_defn_explicit_instantiation) 2094 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); 2095 TemplateParams = nullptr; 2096 } 2097 2098 bool IsDependent = false; 2099 2100 // Don't pass down template parameter lists if this is just a tag 2101 // reference. For example, we don't need the template parameters here: 2102 // template <class T> class A *makeA(T t); 2103 MultiTemplateParamsArg TParams; 2104 if (TUK != Sema::TUK_Reference && TemplateParams) 2105 TParams = 2106 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size()); 2107 2108 stripTypeAttributesOffDeclSpec(attrs, DS, TUK); 2109 2110 // Declaration or definition of a class type 2111 TagOrTempResult = Actions.ActOnTag( 2112 getCurScope(), TagType, TUK, StartLoc, SS, Name, NameLoc, attrs, AS, 2113 DS.getModulePrivateSpecLoc(), TParams, Owned, IsDependent, 2114 SourceLocation(), false, clang::TypeResult(), 2115 DSC == DeclSpecContext::DSC_type_specifier, 2116 DSC == DeclSpecContext::DSC_template_param || 2117 DSC == DeclSpecContext::DSC_template_type_arg, 2118 OffsetOfState, &SkipBody); 2119 2120 // If ActOnTag said the type was dependent, try again with the 2121 // less common call. 2122 if (IsDependent) { 2123 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend); 2124 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK, SS, 2125 Name, StartLoc, NameLoc); 2126 } 2127 } 2128 2129 // If this is an elaborated type specifier in function template, 2130 // and we delayed diagnostics before, 2131 // just merge them into the current pool. 2132 if (shouldDelayDiagsInTag) { 2133 diagsFromTag.done(); 2134 if (TUK == Sema::TUK_Reference && 2135 TemplateInfo.Kind == ParsedTemplateInfo::Template) 2136 diagsFromTag.redelay(); 2137 } 2138 2139 // If there is a body, parse it and inform the actions module. 2140 if (TUK == Sema::TUK_Definition) { 2141 assert(Tok.is(tok::l_brace) || 2142 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) || 2143 isClassCompatibleKeyword()); 2144 if (SkipBody.ShouldSkip) 2145 SkipCXXMemberSpecification(StartLoc, AttrFixitLoc, TagType, 2146 TagOrTempResult.get()); 2147 else if (getLangOpts().CPlusPlus) 2148 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType, 2149 TagOrTempResult.get()); 2150 else { 2151 Decl *D = 2152 SkipBody.CheckSameAsPrevious ? SkipBody.New : TagOrTempResult.get(); 2153 // Parse the definition body. 2154 ParseStructUnionBody(StartLoc, TagType, cast<RecordDecl>(D)); 2155 if (SkipBody.CheckSameAsPrevious && 2156 !Actions.ActOnDuplicateDefinition(TagOrTempResult.get(), SkipBody)) { 2157 DS.SetTypeSpecError(); 2158 return; 2159 } 2160 } 2161 } 2162 2163 if (!TagOrTempResult.isInvalid()) 2164 // Delayed processing of attributes. 2165 Actions.ProcessDeclAttributeDelayed(TagOrTempResult.get(), attrs); 2166 2167 const char *PrevSpec = nullptr; 2168 unsigned DiagID; 2169 bool Result; 2170 if (!TypeResult.isInvalid()) { 2171 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, 2172 NameLoc.isValid() ? NameLoc : StartLoc, 2173 PrevSpec, DiagID, TypeResult.get(), Policy); 2174 } else if (!TagOrTempResult.isInvalid()) { 2175 Result = DS.SetTypeSpecType( 2176 TagType, StartLoc, NameLoc.isValid() ? NameLoc : StartLoc, PrevSpec, 2177 DiagID, TagOrTempResult.get(), Owned, Policy); 2178 } else { 2179 DS.SetTypeSpecError(); 2180 return; 2181 } 2182 2183 if (Result) 2184 Diag(StartLoc, DiagID) << PrevSpec; 2185 2186 // At this point, we've successfully parsed a class-specifier in 'definition' 2187 // form (e.g. "struct foo { int x; }". While we could just return here, we're 2188 // going to look at what comes after it to improve error recovery. If an 2189 // impossible token occurs next, we assume that the programmer forgot a ; at 2190 // the end of the declaration and recover that way. 2191 // 2192 // Also enforce C++ [temp]p3: 2193 // In a template-declaration which defines a class, no declarator 2194 // is permitted. 2195 // 2196 // After a type-specifier, we don't expect a semicolon. This only happens in 2197 // C, since definitions are not permitted in this context in C++. 2198 if (TUK == Sema::TUK_Definition && 2199 (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) && 2200 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) { 2201 if (Tok.isNot(tok::semi)) { 2202 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy(); 2203 ExpectAndConsume(tok::semi, diag::err_expected_after, 2204 DeclSpec::getSpecifierName(TagType, PPol)); 2205 // Push this token back into the preprocessor and change our current token 2206 // to ';' so that the rest of the code recovers as though there were an 2207 // ';' after the definition. 2208 PP.EnterToken(Tok, /*IsReinject=*/true); 2209 Tok.setKind(tok::semi); 2210 } 2211 } 2212 } 2213 2214 /// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. 2215 /// 2216 /// base-clause : [C++ class.derived] 2217 /// ':' base-specifier-list 2218 /// base-specifier-list: 2219 /// base-specifier '...'[opt] 2220 /// base-specifier-list ',' base-specifier '...'[opt] 2221 void Parser::ParseBaseClause(Decl *ClassDecl) { 2222 assert(Tok.is(tok::colon) && "Not a base clause"); 2223 ConsumeToken(); 2224 2225 // Build up an array of parsed base specifiers. 2226 SmallVector<CXXBaseSpecifier *, 8> BaseInfo; 2227 2228 while (true) { 2229 // Parse a base-specifier. 2230 BaseResult Result = ParseBaseSpecifier(ClassDecl); 2231 if (Result.isInvalid()) { 2232 // Skip the rest of this base specifier, up until the comma or 2233 // opening brace. 2234 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch); 2235 } else { 2236 // Add this to our array of base specifiers. 2237 BaseInfo.push_back(Result.get()); 2238 } 2239 2240 // If the next token is a comma, consume it and keep reading 2241 // base-specifiers. 2242 if (!TryConsumeToken(tok::comma)) 2243 break; 2244 } 2245 2246 // Attach the base specifiers 2247 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo); 2248 } 2249 2250 /// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is 2251 /// one entry in the base class list of a class specifier, for example: 2252 /// class foo : public bar, virtual private baz { 2253 /// 'public bar' and 'virtual private baz' are each base-specifiers. 2254 /// 2255 /// base-specifier: [C++ class.derived] 2256 /// attribute-specifier-seq[opt] base-type-specifier 2257 /// attribute-specifier-seq[opt] 'virtual' access-specifier[opt] 2258 /// base-type-specifier 2259 /// attribute-specifier-seq[opt] access-specifier 'virtual'[opt] 2260 /// base-type-specifier 2261 BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) { 2262 bool IsVirtual = false; 2263 SourceLocation StartLoc = Tok.getLocation(); 2264 2265 ParsedAttributes Attributes(AttrFactory); 2266 MaybeParseCXX11Attributes(Attributes); 2267 2268 // Parse the 'virtual' keyword. 2269 if (TryConsumeToken(tok::kw_virtual)) 2270 IsVirtual = true; 2271 2272 CheckMisplacedCXX11Attribute(Attributes, StartLoc); 2273 2274 // Parse an (optional) access specifier. 2275 AccessSpecifier Access = getAccessSpecifierIfPresent(); 2276 if (Access != AS_none) { 2277 ConsumeToken(); 2278 if (getLangOpts().HLSL) 2279 Diag(Tok.getLocation(), diag::ext_hlsl_access_specifiers); 2280 } 2281 2282 CheckMisplacedCXX11Attribute(Attributes, StartLoc); 2283 2284 // Parse the 'virtual' keyword (again!), in case it came after the 2285 // access specifier. 2286 if (Tok.is(tok::kw_virtual)) { 2287 SourceLocation VirtualLoc = ConsumeToken(); 2288 if (IsVirtual) { 2289 // Complain about duplicate 'virtual' 2290 Diag(VirtualLoc, diag::err_dup_virtual) 2291 << FixItHint::CreateRemoval(VirtualLoc); 2292 } 2293 2294 IsVirtual = true; 2295 } 2296 2297 CheckMisplacedCXX11Attribute(Attributes, StartLoc); 2298 2299 // Parse the class-name. 2300 2301 // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL 2302 // implementation for VS2013 uses _Atomic as an identifier for one of the 2303 // classes in <atomic>. Treat '_Atomic' to be an identifier when we are 2304 // parsing the class-name for a base specifier. 2305 if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) && 2306 NextToken().is(tok::less)) 2307 Tok.setKind(tok::identifier); 2308 2309 SourceLocation EndLocation; 2310 SourceLocation BaseLoc; 2311 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation); 2312 if (BaseType.isInvalid()) 2313 return true; 2314 2315 // Parse the optional ellipsis (for a pack expansion). The ellipsis is 2316 // actually part of the base-specifier-list grammar productions, but we 2317 // parse it here for convenience. 2318 SourceLocation EllipsisLoc; 2319 TryConsumeToken(tok::ellipsis, EllipsisLoc); 2320 2321 // Find the complete source range for the base-specifier. 2322 SourceRange Range(StartLoc, EndLocation); 2323 2324 // Notify semantic analysis that we have parsed a complete 2325 // base-specifier. 2326 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual, 2327 Access, BaseType.get(), BaseLoc, 2328 EllipsisLoc); 2329 } 2330 2331 /// getAccessSpecifierIfPresent - Determine whether the next token is 2332 /// a C++ access-specifier. 2333 /// 2334 /// access-specifier: [C++ class.derived] 2335 /// 'private' 2336 /// 'protected' 2337 /// 'public' 2338 AccessSpecifier Parser::getAccessSpecifierIfPresent() const { 2339 switch (Tok.getKind()) { 2340 default: 2341 return AS_none; 2342 case tok::kw_private: 2343 return AS_private; 2344 case tok::kw_protected: 2345 return AS_protected; 2346 case tok::kw_public: 2347 return AS_public; 2348 } 2349 } 2350 2351 /// If the given declarator has any parts for which parsing has to be 2352 /// delayed, e.g., default arguments or an exception-specification, create a 2353 /// late-parsed method declaration record to handle the parsing at the end of 2354 /// the class definition. 2355 void Parser::HandleMemberFunctionDeclDelays(Declarator &DeclaratorInfo, 2356 Decl *ThisDecl) { 2357 DeclaratorChunk::FunctionTypeInfo &FTI = DeclaratorInfo.getFunctionTypeInfo(); 2358 // If there was a late-parsed exception-specification, we'll need a 2359 // late parse 2360 bool NeedLateParse = FTI.getExceptionSpecType() == EST_Unparsed; 2361 2362 if (!NeedLateParse) { 2363 // Look ahead to see if there are any default args 2364 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) { 2365 auto Param = cast<ParmVarDecl>(FTI.Params[ParamIdx].Param); 2366 if (Param->hasUnparsedDefaultArg()) { 2367 NeedLateParse = true; 2368 break; 2369 } 2370 } 2371 } 2372 2373 if (NeedLateParse) { 2374 // Push this method onto the stack of late-parsed method 2375 // declarations. 2376 auto LateMethod = new LateParsedMethodDeclaration(this, ThisDecl); 2377 getCurrentClass().LateParsedDeclarations.push_back(LateMethod); 2378 2379 // Push tokens for each parameter. Those that do not have defaults will be 2380 // NULL. We need to track all the parameters so that we can push them into 2381 // scope for later parameters and perhaps for the exception specification. 2382 LateMethod->DefaultArgs.reserve(FTI.NumParams); 2383 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) 2384 LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument( 2385 FTI.Params[ParamIdx].Param, 2386 std::move(FTI.Params[ParamIdx].DefaultArgTokens))); 2387 2388 // Stash the exception-specification tokens in the late-pased method. 2389 if (FTI.getExceptionSpecType() == EST_Unparsed) { 2390 LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens; 2391 FTI.ExceptionSpecTokens = nullptr; 2392 } 2393 } 2394 } 2395 2396 /// isCXX11VirtSpecifier - Determine whether the given token is a C++11 2397 /// virt-specifier. 2398 /// 2399 /// virt-specifier: 2400 /// override 2401 /// final 2402 /// __final 2403 VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const { 2404 if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier)) 2405 return VirtSpecifiers::VS_None; 2406 2407 IdentifierInfo *II = Tok.getIdentifierInfo(); 2408 2409 // Initialize the contextual keywords. 2410 if (!Ident_final) { 2411 Ident_final = &PP.getIdentifierTable().get("final"); 2412 if (getLangOpts().GNUKeywords) 2413 Ident_GNU_final = &PP.getIdentifierTable().get("__final"); 2414 if (getLangOpts().MicrosoftExt) { 2415 Ident_sealed = &PP.getIdentifierTable().get("sealed"); 2416 Ident_abstract = &PP.getIdentifierTable().get("abstract"); 2417 } 2418 Ident_override = &PP.getIdentifierTable().get("override"); 2419 } 2420 2421 if (II == Ident_override) 2422 return VirtSpecifiers::VS_Override; 2423 2424 if (II == Ident_sealed) 2425 return VirtSpecifiers::VS_Sealed; 2426 2427 if (II == Ident_abstract) 2428 return VirtSpecifiers::VS_Abstract; 2429 2430 if (II == Ident_final) 2431 return VirtSpecifiers::VS_Final; 2432 2433 if (II == Ident_GNU_final) 2434 return VirtSpecifiers::VS_GNU_Final; 2435 2436 return VirtSpecifiers::VS_None; 2437 } 2438 2439 /// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq. 2440 /// 2441 /// virt-specifier-seq: 2442 /// virt-specifier 2443 /// virt-specifier-seq virt-specifier 2444 void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS, 2445 bool IsInterface, 2446 SourceLocation FriendLoc) { 2447 while (true) { 2448 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(); 2449 if (Specifier == VirtSpecifiers::VS_None) 2450 return; 2451 2452 if (FriendLoc.isValid()) { 2453 Diag(Tok.getLocation(), diag::err_friend_decl_spec) 2454 << VirtSpecifiers::getSpecifierName(Specifier) 2455 << FixItHint::CreateRemoval(Tok.getLocation()) 2456 << SourceRange(FriendLoc, FriendLoc); 2457 ConsumeToken(); 2458 continue; 2459 } 2460 2461 // C++ [class.mem]p8: 2462 // A virt-specifier-seq shall contain at most one of each virt-specifier. 2463 const char *PrevSpec = nullptr; 2464 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec)) 2465 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier) 2466 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); 2467 2468 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final || 2469 Specifier == VirtSpecifiers::VS_Sealed)) { 2470 Diag(Tok.getLocation(), diag::err_override_control_interface) 2471 << VirtSpecifiers::getSpecifierName(Specifier); 2472 } else if (Specifier == VirtSpecifiers::VS_Sealed) { 2473 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword); 2474 } else if (Specifier == VirtSpecifiers::VS_Abstract) { 2475 Diag(Tok.getLocation(), diag::ext_ms_abstract_keyword); 2476 } else if (Specifier == VirtSpecifiers::VS_GNU_Final) { 2477 Diag(Tok.getLocation(), diag::ext_warn_gnu_final); 2478 } else { 2479 Diag(Tok.getLocation(), 2480 getLangOpts().CPlusPlus11 2481 ? diag::warn_cxx98_compat_override_control_keyword 2482 : diag::ext_override_control_keyword) 2483 << VirtSpecifiers::getSpecifierName(Specifier); 2484 } 2485 ConsumeToken(); 2486 } 2487 } 2488 2489 /// isCXX11FinalKeyword - Determine whether the next token is a C++11 2490 /// 'final' or Microsoft 'sealed' contextual keyword. 2491 bool Parser::isCXX11FinalKeyword() const { 2492 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(); 2493 return Specifier == VirtSpecifiers::VS_Final || 2494 Specifier == VirtSpecifiers::VS_GNU_Final || 2495 Specifier == VirtSpecifiers::VS_Sealed; 2496 } 2497 2498 /// isClassCompatibleKeyword - Determine whether the next token is a C++11 2499 /// 'final' or Microsoft 'sealed' or 'abstract' contextual keywords. 2500 bool Parser::isClassCompatibleKeyword() const { 2501 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(); 2502 return Specifier == VirtSpecifiers::VS_Final || 2503 Specifier == VirtSpecifiers::VS_GNU_Final || 2504 Specifier == VirtSpecifiers::VS_Sealed || 2505 Specifier == VirtSpecifiers::VS_Abstract; 2506 } 2507 2508 /// Parse a C++ member-declarator up to, but not including, the optional 2509 /// brace-or-equal-initializer or pure-specifier. 2510 bool Parser::ParseCXXMemberDeclaratorBeforeInitializer( 2511 Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize, 2512 LateParsedAttrList &LateParsedAttrs) { 2513 // member-declarator: 2514 // declarator virt-specifier-seq[opt] pure-specifier[opt] 2515 // declarator requires-clause 2516 // declarator brace-or-equal-initializer[opt] 2517 // identifier attribute-specifier-seq[opt] ':' constant-expression 2518 // brace-or-equal-initializer[opt] 2519 // ':' constant-expression 2520 // 2521 // NOTE: the latter two productions are a proposed bugfix rather than the 2522 // current grammar rules as of C++20. 2523 if (Tok.isNot(tok::colon)) 2524 ParseDeclarator(DeclaratorInfo); 2525 else 2526 DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation()); 2527 2528 if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) { 2529 assert(DeclaratorInfo.isPastIdentifier() && 2530 "don't know where identifier would go yet?"); 2531 BitfieldSize = ParseConstantExpression(); 2532 if (BitfieldSize.isInvalid()) 2533 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); 2534 } else if (Tok.is(tok::kw_requires)) { 2535 ParseTrailingRequiresClause(DeclaratorInfo); 2536 } else { 2537 ParseOptionalCXX11VirtSpecifierSeq( 2538 VS, getCurrentClass().IsInterface, 2539 DeclaratorInfo.getDeclSpec().getFriendSpecLoc()); 2540 if (!VS.isUnset()) 2541 MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, 2542 VS); 2543 } 2544 2545 // If a simple-asm-expr is present, parse it. 2546 if (Tok.is(tok::kw_asm)) { 2547 SourceLocation Loc; 2548 ExprResult AsmLabel(ParseSimpleAsm(/*ForAsmLabel*/ true, &Loc)); 2549 if (AsmLabel.isInvalid()) 2550 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); 2551 2552 DeclaratorInfo.setAsmLabel(AsmLabel.get()); 2553 DeclaratorInfo.SetRangeEnd(Loc); 2554 } 2555 2556 // If attributes exist after the declarator, but before an '{', parse them. 2557 // However, this does not apply for [[]] attributes (which could show up 2558 // before or after the __attribute__ attributes). 2559 DiagnoseAndSkipCXX11Attributes(); 2560 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs); 2561 DiagnoseAndSkipCXX11Attributes(); 2562 2563 // For compatibility with code written to older Clang, also accept a 2564 // virt-specifier *after* the GNU attributes. 2565 if (BitfieldSize.isUnset() && VS.isUnset()) { 2566 ParseOptionalCXX11VirtSpecifierSeq( 2567 VS, getCurrentClass().IsInterface, 2568 DeclaratorInfo.getDeclSpec().getFriendSpecLoc()); 2569 if (!VS.isUnset()) { 2570 // If we saw any GNU-style attributes that are known to GCC followed by a 2571 // virt-specifier, issue a GCC-compat warning. 2572 for (const ParsedAttr &AL : DeclaratorInfo.getAttributes()) 2573 if (AL.isKnownToGCC() && !AL.isCXX11Attribute()) 2574 Diag(AL.getLoc(), diag::warn_gcc_attribute_location); 2575 2576 MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, 2577 VS); 2578 } 2579 } 2580 2581 // If this has neither a name nor a bit width, something has gone seriously 2582 // wrong. Skip until the semi-colon or }. 2583 if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) { 2584 // If so, skip until the semi-colon or a }. 2585 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); 2586 return true; 2587 } 2588 return false; 2589 } 2590 2591 /// Look for declaration specifiers possibly occurring after C++11 2592 /// virt-specifier-seq and diagnose them. 2593 void Parser::MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq( 2594 Declarator &D, VirtSpecifiers &VS) { 2595 DeclSpec DS(AttrFactory); 2596 2597 // GNU-style and C++11 attributes are not allowed here, but they will be 2598 // handled by the caller. Diagnose everything else. 2599 ParseTypeQualifierListOpt( 2600 DS, AR_NoAttributesParsed, false, 2601 /*IdentifierRequired=*/false, llvm::function_ref<void()>([&]() { 2602 Actions.CodeCompleteFunctionQualifiers(DS, D, &VS); 2603 })); 2604 D.ExtendWithDeclSpec(DS); 2605 2606 if (D.isFunctionDeclarator()) { 2607 auto &Function = D.getFunctionTypeInfo(); 2608 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { 2609 auto DeclSpecCheck = [&](DeclSpec::TQ TypeQual, StringRef FixItName, 2610 SourceLocation SpecLoc) { 2611 FixItHint Insertion; 2612 auto &MQ = Function.getOrCreateMethodQualifiers(); 2613 if (!(MQ.getTypeQualifiers() & TypeQual)) { 2614 std::string Name(FixItName.data()); 2615 Name += " "; 2616 Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name); 2617 MQ.SetTypeQual(TypeQual, SpecLoc); 2618 } 2619 Diag(SpecLoc, diag::err_declspec_after_virtspec) 2620 << FixItName 2621 << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier()) 2622 << FixItHint::CreateRemoval(SpecLoc) << Insertion; 2623 }; 2624 DS.forEachQualifier(DeclSpecCheck); 2625 } 2626 2627 // Parse ref-qualifiers. 2628 bool RefQualifierIsLValueRef = true; 2629 SourceLocation RefQualifierLoc; 2630 if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) { 2631 const char *Name = (RefQualifierIsLValueRef ? "& " : "&& "); 2632 FixItHint Insertion = 2633 FixItHint::CreateInsertion(VS.getFirstLocation(), Name); 2634 Function.RefQualifierIsLValueRef = RefQualifierIsLValueRef; 2635 Function.RefQualifierLoc = RefQualifierLoc; 2636 2637 Diag(RefQualifierLoc, diag::err_declspec_after_virtspec) 2638 << (RefQualifierIsLValueRef ? "&" : "&&") 2639 << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier()) 2640 << FixItHint::CreateRemoval(RefQualifierLoc) << Insertion; 2641 D.SetRangeEnd(RefQualifierLoc); 2642 } 2643 } 2644 } 2645 2646 /// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. 2647 /// 2648 /// member-declaration: 2649 /// decl-specifier-seq[opt] member-declarator-list[opt] ';' 2650 /// function-definition ';'[opt] 2651 /// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] 2652 /// using-declaration [TODO] 2653 /// [C++0x] static_assert-declaration 2654 /// template-declaration 2655 /// [GNU] '__extension__' member-declaration 2656 /// 2657 /// member-declarator-list: 2658 /// member-declarator 2659 /// member-declarator-list ',' member-declarator 2660 /// 2661 /// member-declarator: 2662 /// declarator virt-specifier-seq[opt] pure-specifier[opt] 2663 /// [C++2a] declarator requires-clause 2664 /// declarator constant-initializer[opt] 2665 /// [C++11] declarator brace-or-equal-initializer[opt] 2666 /// identifier[opt] ':' constant-expression 2667 /// 2668 /// virt-specifier-seq: 2669 /// virt-specifier 2670 /// virt-specifier-seq virt-specifier 2671 /// 2672 /// virt-specifier: 2673 /// override 2674 /// final 2675 /// [MS] sealed 2676 /// 2677 /// pure-specifier: 2678 /// '= 0' 2679 /// 2680 /// constant-initializer: 2681 /// '=' constant-expression 2682 /// 2683 Parser::DeclGroupPtrTy 2684 Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, 2685 ParsedAttributes &AccessAttrs, 2686 const ParsedTemplateInfo &TemplateInfo, 2687 ParsingDeclRAIIObject *TemplateDiags) { 2688 assert(getLangOpts().CPlusPlus && 2689 "ParseCXXClassMemberDeclaration should only be called in C++ mode"); 2690 if (Tok.is(tok::at)) { 2691 if (getLangOpts().ObjC && NextToken().isObjCAtKeyword(tok::objc_defs)) 2692 Diag(Tok, diag::err_at_defs_cxx); 2693 else 2694 Diag(Tok, diag::err_at_in_class); 2695 2696 ConsumeToken(); 2697 SkipUntil(tok::r_brace, StopAtSemi); 2698 return nullptr; 2699 } 2700 2701 // Turn on colon protection early, while parsing declspec, although there is 2702 // nothing to protect there. It prevents from false errors if error recovery 2703 // incorrectly determines where the declspec ends, as in the example: 2704 // struct A { enum class B { C }; }; 2705 // const int C = 4; 2706 // struct D { A::B : C; }; 2707 ColonProtectionRAIIObject X(*this); 2708 2709 // Access declarations. 2710 bool MalformedTypeSpec = false; 2711 if (!TemplateInfo.Kind && 2712 Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw___super)) { 2713 if (TryAnnotateCXXScopeToken()) 2714 MalformedTypeSpec = true; 2715 2716 bool isAccessDecl; 2717 if (Tok.isNot(tok::annot_cxxscope)) 2718 isAccessDecl = false; 2719 else if (NextToken().is(tok::identifier)) 2720 isAccessDecl = GetLookAheadToken(2).is(tok::semi); 2721 else 2722 isAccessDecl = NextToken().is(tok::kw_operator); 2723 2724 if (isAccessDecl) { 2725 // Collect the scope specifier token we annotated earlier. 2726 CXXScopeSpec SS; 2727 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, 2728 /*ObjectHasErrors=*/false, 2729 /*EnteringContext=*/false); 2730 2731 if (SS.isInvalid()) { 2732 SkipUntil(tok::semi); 2733 return nullptr; 2734 } 2735 2736 // Try to parse an unqualified-id. 2737 SourceLocation TemplateKWLoc; 2738 UnqualifiedId Name; 2739 if (ParseUnqualifiedId(SS, /*ObjectType=*/nullptr, 2740 /*ObjectHadErrors=*/false, false, true, true, 2741 false, &TemplateKWLoc, Name)) { 2742 SkipUntil(tok::semi); 2743 return nullptr; 2744 } 2745 2746 // TODO: recover from mistakenly-qualified operator declarations. 2747 if (ExpectAndConsume(tok::semi, diag::err_expected_after, 2748 "access declaration")) { 2749 SkipUntil(tok::semi); 2750 return nullptr; 2751 } 2752 2753 // FIXME: We should do something with the 'template' keyword here. 2754 return DeclGroupPtrTy::make(DeclGroupRef(Actions.ActOnUsingDeclaration( 2755 getCurScope(), AS, /*UsingLoc*/ SourceLocation(), 2756 /*TypenameLoc*/ SourceLocation(), SS, Name, 2757 /*EllipsisLoc*/ SourceLocation(), 2758 /*AttrList*/ ParsedAttributesView()))); 2759 } 2760 } 2761 2762 // static_assert-declaration. A templated static_assert declaration is 2763 // diagnosed in Parser::ParseSingleDeclarationAfterTemplate. 2764 if (!TemplateInfo.Kind && 2765 Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert)) { 2766 SourceLocation DeclEnd; 2767 return DeclGroupPtrTy::make( 2768 DeclGroupRef(ParseStaticAssertDeclaration(DeclEnd))); 2769 } 2770 2771 if (Tok.is(tok::kw_template)) { 2772 assert(!TemplateInfo.TemplateParams && 2773 "Nested template improperly parsed?"); 2774 ObjCDeclContextSwitch ObjCDC(*this); 2775 SourceLocation DeclEnd; 2776 return DeclGroupPtrTy::make( 2777 DeclGroupRef(ParseTemplateDeclarationOrSpecialization( 2778 DeclaratorContext::Member, DeclEnd, AccessAttrs, AS))); 2779 } 2780 2781 // Handle: member-declaration ::= '__extension__' member-declaration 2782 if (Tok.is(tok::kw___extension__)) { 2783 // __extension__ silences extension warnings in the subexpression. 2784 ExtensionRAIIObject O(Diags); // Use RAII to do this. 2785 ConsumeToken(); 2786 return ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo, 2787 TemplateDiags); 2788 } 2789 2790 ParsedAttributes DeclAttrs(AttrFactory); 2791 // Optional C++11 attribute-specifier 2792 MaybeParseCXX11Attributes(DeclAttrs); 2793 2794 // The next token may be an OpenMP pragma annotation token. That would 2795 // normally be handled from ParseCXXClassMemberDeclarationWithPragmas, but in 2796 // this case, it came from an *attribute* rather than a pragma. Handle it now. 2797 if (Tok.is(tok::annot_attr_openmp)) 2798 return ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, DeclAttrs); 2799 2800 if (Tok.is(tok::kw_using)) { 2801 // Eat 'using'. 2802 SourceLocation UsingLoc = ConsumeToken(); 2803 2804 // Consume unexpected 'template' keywords. 2805 while (Tok.is(tok::kw_template)) { 2806 SourceLocation TemplateLoc = ConsumeToken(); 2807 Diag(TemplateLoc, diag::err_unexpected_template_after_using) 2808 << FixItHint::CreateRemoval(TemplateLoc); 2809 } 2810 2811 if (Tok.is(tok::kw_namespace)) { 2812 Diag(UsingLoc, diag::err_using_namespace_in_class); 2813 SkipUntil(tok::semi, StopBeforeMatch); 2814 return nullptr; 2815 } 2816 SourceLocation DeclEnd; 2817 // Otherwise, it must be a using-declaration or an alias-declaration. 2818 return ParseUsingDeclaration(DeclaratorContext::Member, TemplateInfo, 2819 UsingLoc, DeclEnd, DeclAttrs, AS); 2820 } 2821 2822 ParsedAttributes DeclSpecAttrs(AttrFactory); 2823 MaybeParseMicrosoftAttributes(DeclSpecAttrs); 2824 2825 // Hold late-parsed attributes so we can attach a Decl to them later. 2826 LateParsedAttrList CommonLateParsedAttrs; 2827 2828 // decl-specifier-seq: 2829 // Parse the common declaration-specifiers piece. 2830 ParsingDeclSpec DS(*this, TemplateDiags); 2831 DS.takeAttributesFrom(DeclSpecAttrs); 2832 2833 if (MalformedTypeSpec) 2834 DS.SetTypeSpecError(); 2835 2836 // Turn off usual access checking for templates explicit specialization 2837 // and instantiation. 2838 // C++20 [temp.spec] 13.9/6. 2839 // This disables the access checking rules for member function template 2840 // explicit instantiation and explicit specialization. 2841 bool IsTemplateSpecOrInst = 2842 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || 2843 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); 2844 SuppressAccessChecks diagsFromTag(*this, IsTemplateSpecOrInst); 2845 2846 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DeclSpecContext::DSC_class, 2847 &CommonLateParsedAttrs); 2848 2849 if (IsTemplateSpecOrInst) 2850 diagsFromTag.done(); 2851 2852 // Turn off colon protection that was set for declspec. 2853 X.restore(); 2854 2855 // If we had a free-standing type definition with a missing semicolon, we 2856 // may get this far before the problem becomes obvious. 2857 if (DS.hasTagDefinition() && 2858 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate && 2859 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DeclSpecContext::DSC_class, 2860 &CommonLateParsedAttrs)) 2861 return nullptr; 2862 2863 MultiTemplateParamsArg TemplateParams( 2864 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() 2865 : nullptr, 2866 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0); 2867 2868 if (TryConsumeToken(tok::semi)) { 2869 if (DS.isFriendSpecified()) 2870 ProhibitAttributes(DeclAttrs); 2871 2872 RecordDecl *AnonRecord = nullptr; 2873 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec( 2874 getCurScope(), AS, DS, DeclAttrs, TemplateParams, false, AnonRecord); 2875 Actions.ActOnDefinedDeclarationSpecifier(TheDecl); 2876 DS.complete(TheDecl); 2877 if (AnonRecord) { 2878 Decl *decls[] = {AnonRecord, TheDecl}; 2879 return Actions.BuildDeclaratorGroup(decls); 2880 } 2881 return Actions.ConvertDeclToDeclGroup(TheDecl); 2882 } 2883 2884 if (DS.hasTagDefinition()) 2885 Actions.ActOnDefinedDeclarationSpecifier(DS.getRepAsDecl()); 2886 2887 ParsingDeclarator DeclaratorInfo(*this, DS, DeclAttrs, 2888 DeclaratorContext::Member); 2889 if (TemplateInfo.TemplateParams) 2890 DeclaratorInfo.setTemplateParameterLists(TemplateParams); 2891 VirtSpecifiers VS; 2892 2893 // Hold late-parsed attributes so we can attach a Decl to them later. 2894 LateParsedAttrList LateParsedAttrs; 2895 2896 SourceLocation EqualLoc; 2897 SourceLocation PureSpecLoc; 2898 2899 auto TryConsumePureSpecifier = [&](bool AllowDefinition) { 2900 if (Tok.isNot(tok::equal)) 2901 return false; 2902 2903 auto &Zero = NextToken(); 2904 SmallString<8> Buffer; 2905 if (Zero.isNot(tok::numeric_constant) || 2906 PP.getSpelling(Zero, Buffer) != "0") 2907 return false; 2908 2909 auto &After = GetLookAheadToken(2); 2910 if (!After.isOneOf(tok::semi, tok::comma) && 2911 !(AllowDefinition && 2912 After.isOneOf(tok::l_brace, tok::colon, tok::kw_try))) 2913 return false; 2914 2915 EqualLoc = ConsumeToken(); 2916 PureSpecLoc = ConsumeToken(); 2917 return true; 2918 }; 2919 2920 SmallVector<Decl *, 8> DeclsInGroup; 2921 ExprResult BitfieldSize; 2922 ExprResult TrailingRequiresClause; 2923 bool ExpectSemi = true; 2924 2925 // C++20 [temp.spec] 13.9/6. 2926 // This disables the access checking rules for member function template 2927 // explicit instantiation and explicit specialization. 2928 SuppressAccessChecks SAC(*this, IsTemplateSpecOrInst); 2929 2930 // Parse the first declarator. 2931 if (ParseCXXMemberDeclaratorBeforeInitializer( 2932 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) { 2933 TryConsumeToken(tok::semi); 2934 return nullptr; 2935 } 2936 2937 if (IsTemplateSpecOrInst) 2938 SAC.done(); 2939 2940 // Check for a member function definition. 2941 if (BitfieldSize.isUnset()) { 2942 // MSVC permits pure specifier on inline functions defined at class scope. 2943 // Hence check for =0 before checking for function definition. 2944 if (getLangOpts().MicrosoftExt && DeclaratorInfo.isDeclarationOfFunction()) 2945 TryConsumePureSpecifier(/*AllowDefinition*/ true); 2946 2947 FunctionDefinitionKind DefinitionKind = FunctionDefinitionKind::Declaration; 2948 // function-definition: 2949 // 2950 // In C++11, a non-function declarator followed by an open brace is a 2951 // braced-init-list for an in-class member initialization, not an 2952 // erroneous function definition. 2953 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) { 2954 DefinitionKind = FunctionDefinitionKind::Definition; 2955 } else if (DeclaratorInfo.isFunctionDeclarator()) { 2956 if (Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)) { 2957 DefinitionKind = FunctionDefinitionKind::Definition; 2958 } else if (Tok.is(tok::equal)) { 2959 const Token &KW = NextToken(); 2960 if (KW.is(tok::kw_default)) 2961 DefinitionKind = FunctionDefinitionKind::Defaulted; 2962 else if (KW.is(tok::kw_delete)) 2963 DefinitionKind = FunctionDefinitionKind::Deleted; 2964 else if (KW.is(tok::code_completion)) { 2965 cutOffParsing(); 2966 Actions.CodeCompleteAfterFunctionEquals(DeclaratorInfo); 2967 return nullptr; 2968 } 2969 } 2970 } 2971 DeclaratorInfo.setFunctionDefinitionKind(DefinitionKind); 2972 2973 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains 2974 // to a friend declaration, that declaration shall be a definition. 2975 if (DeclaratorInfo.isFunctionDeclarator() && 2976 DefinitionKind == FunctionDefinitionKind::Declaration && 2977 DS.isFriendSpecified()) { 2978 // Diagnose attributes that appear before decl specifier: 2979 // [[]] friend int foo(); 2980 ProhibitAttributes(DeclAttrs); 2981 } 2982 2983 if (DefinitionKind != FunctionDefinitionKind::Declaration) { 2984 if (!DeclaratorInfo.isFunctionDeclarator()) { 2985 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params); 2986 ConsumeBrace(); 2987 SkipUntil(tok::r_brace); 2988 2989 // Consume the optional ';' 2990 TryConsumeToken(tok::semi); 2991 2992 return nullptr; 2993 } 2994 2995 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { 2996 Diag(DeclaratorInfo.getIdentifierLoc(), 2997 diag::err_function_declared_typedef); 2998 2999 // Recover by treating the 'typedef' as spurious. 3000 DS.ClearStorageClassSpecs(); 3001 } 3002 3003 Decl *FunDecl = ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, 3004 TemplateInfo, VS, PureSpecLoc); 3005 3006 if (FunDecl) { 3007 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) { 3008 CommonLateParsedAttrs[i]->addDecl(FunDecl); 3009 } 3010 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) { 3011 LateParsedAttrs[i]->addDecl(FunDecl); 3012 } 3013 } 3014 LateParsedAttrs.clear(); 3015 3016 // Consume the ';' - it's optional unless we have a delete or default 3017 if (Tok.is(tok::semi)) 3018 ConsumeExtraSemi(AfterMemberFunctionDefinition); 3019 3020 return DeclGroupPtrTy::make(DeclGroupRef(FunDecl)); 3021 } 3022 } 3023 3024 // member-declarator-list: 3025 // member-declarator 3026 // member-declarator-list ',' member-declarator 3027 3028 while (true) { 3029 InClassInitStyle HasInClassInit = ICIS_NoInit; 3030 bool HasStaticInitializer = false; 3031 if (Tok.isOneOf(tok::equal, tok::l_brace) && PureSpecLoc.isInvalid()) { 3032 // DRXXXX: Anonymous bit-fields cannot have a brace-or-equal-initializer. 3033 if (BitfieldSize.isUsable() && !DeclaratorInfo.hasName()) { 3034 // Diagnose the error and pretend there is no in-class initializer. 3035 Diag(Tok, diag::err_anon_bitfield_member_init); 3036 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); 3037 } else if (DeclaratorInfo.isDeclarationOfFunction()) { 3038 // It's a pure-specifier. 3039 if (!TryConsumePureSpecifier(/*AllowFunctionDefinition*/ false)) 3040 // Parse it as an expression so that Sema can diagnose it. 3041 HasStaticInitializer = true; 3042 } else if (DeclaratorInfo.getDeclSpec().getStorageClassSpec() != 3043 DeclSpec::SCS_static && 3044 DeclaratorInfo.getDeclSpec().getStorageClassSpec() != 3045 DeclSpec::SCS_typedef && 3046 !DS.isFriendSpecified()) { 3047 // It's a default member initializer. 3048 if (BitfieldSize.get()) 3049 Diag(Tok, getLangOpts().CPlusPlus20 3050 ? diag::warn_cxx17_compat_bitfield_member_init 3051 : diag::ext_bitfield_member_init); 3052 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit; 3053 } else { 3054 HasStaticInitializer = true; 3055 } 3056 } 3057 3058 // NOTE: If Sema is the Action module and declarator is an instance field, 3059 // this call will *not* return the created decl; It will return null. 3060 // See Sema::ActOnCXXMemberDeclarator for details. 3061 3062 NamedDecl *ThisDecl = nullptr; 3063 if (DS.isFriendSpecified()) { 3064 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains 3065 // to a friend declaration, that declaration shall be a definition. 3066 // 3067 // Diagnose attributes that appear in a friend member function declarator: 3068 // friend int foo [[]] (); 3069 for (const ParsedAttr &AL : DeclaratorInfo.getAttributes()) 3070 if (AL.isCXX11Attribute() || AL.isRegularKeywordAttribute()) { 3071 auto Loc = AL.getRange().getBegin(); 3072 (AL.isRegularKeywordAttribute() 3073 ? Diag(Loc, diag::err_keyword_not_allowed) << AL 3074 : Diag(Loc, diag::err_attributes_not_allowed)) 3075 << AL.getRange(); 3076 } 3077 3078 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo, 3079 TemplateParams); 3080 } else { 3081 ThisDecl = Actions.ActOnCXXMemberDeclarator( 3082 getCurScope(), AS, DeclaratorInfo, TemplateParams, BitfieldSize.get(), 3083 VS, HasInClassInit); 3084 3085 if (VarTemplateDecl *VT = 3086 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr) 3087 // Re-direct this decl to refer to the templated decl so that we can 3088 // initialize it. 3089 ThisDecl = VT->getTemplatedDecl(); 3090 3091 if (ThisDecl) 3092 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs); 3093 } 3094 3095 // Error recovery might have converted a non-static member into a static 3096 // member. 3097 if (HasInClassInit != ICIS_NoInit && 3098 DeclaratorInfo.getDeclSpec().getStorageClassSpec() == 3099 DeclSpec::SCS_static) { 3100 HasInClassInit = ICIS_NoInit; 3101 HasStaticInitializer = true; 3102 } 3103 3104 if (PureSpecLoc.isValid() && VS.getAbstractLoc().isValid()) { 3105 Diag(PureSpecLoc, diag::err_duplicate_virt_specifier) << "abstract"; 3106 } 3107 if (ThisDecl && PureSpecLoc.isValid()) 3108 Actions.ActOnPureSpecifier(ThisDecl, PureSpecLoc); 3109 else if (ThisDecl && VS.getAbstractLoc().isValid()) 3110 Actions.ActOnPureSpecifier(ThisDecl, VS.getAbstractLoc()); 3111 3112 // Handle the initializer. 3113 if (HasInClassInit != ICIS_NoInit) { 3114 // The initializer was deferred; parse it and cache the tokens. 3115 Diag(Tok, getLangOpts().CPlusPlus11 3116 ? diag::warn_cxx98_compat_nonstatic_member_init 3117 : diag::ext_nonstatic_member_init); 3118 3119 if (DeclaratorInfo.isArrayOfUnknownBound()) { 3120 // C++11 [dcl.array]p3: An array bound may also be omitted when the 3121 // declarator is followed by an initializer. 3122 // 3123 // A brace-or-equal-initializer for a member-declarator is not an 3124 // initializer in the grammar, so this is ill-formed. 3125 Diag(Tok, diag::err_incomplete_array_member_init); 3126 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); 3127 3128 // Avoid later warnings about a class member of incomplete type. 3129 if (ThisDecl) 3130 ThisDecl->setInvalidDecl(); 3131 } else 3132 ParseCXXNonStaticMemberInitializer(ThisDecl); 3133 } else if (HasStaticInitializer) { 3134 // Normal initializer. 3135 ExprResult Init = ParseCXXMemberInitializer( 3136 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc); 3137 3138 if (Init.isInvalid()) { 3139 if (ThisDecl) 3140 Actions.ActOnUninitializedDecl(ThisDecl); 3141 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); 3142 } else if (ThisDecl) 3143 Actions.AddInitializerToDecl(ThisDecl, Init.get(), 3144 EqualLoc.isInvalid()); 3145 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) 3146 // No initializer. 3147 Actions.ActOnUninitializedDecl(ThisDecl); 3148 3149 if (ThisDecl) { 3150 if (!ThisDecl->isInvalidDecl()) { 3151 // Set the Decl for any late parsed attributes 3152 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) 3153 CommonLateParsedAttrs[i]->addDecl(ThisDecl); 3154 3155 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) 3156 LateParsedAttrs[i]->addDecl(ThisDecl); 3157 } 3158 Actions.FinalizeDeclaration(ThisDecl); 3159 DeclsInGroup.push_back(ThisDecl); 3160 3161 if (DeclaratorInfo.isFunctionDeclarator() && 3162 DeclaratorInfo.getDeclSpec().getStorageClassSpec() != 3163 DeclSpec::SCS_typedef) 3164 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl); 3165 } 3166 LateParsedAttrs.clear(); 3167 3168 DeclaratorInfo.complete(ThisDecl); 3169 3170 // If we don't have a comma, it is either the end of the list (a ';') 3171 // or an error, bail out. 3172 SourceLocation CommaLoc; 3173 if (!TryConsumeToken(tok::comma, CommaLoc)) 3174 break; 3175 3176 if (Tok.isAtStartOfLine() && 3177 !MightBeDeclarator(DeclaratorContext::Member)) { 3178 // This comma was followed by a line-break and something which can't be 3179 // the start of a declarator. The comma was probably a typo for a 3180 // semicolon. 3181 Diag(CommaLoc, diag::err_expected_semi_declaration) 3182 << FixItHint::CreateReplacement(CommaLoc, ";"); 3183 ExpectSemi = false; 3184 break; 3185 } 3186 3187 // Parse the next declarator. 3188 DeclaratorInfo.clear(); 3189 VS.clear(); 3190 BitfieldSize = ExprResult(/*Invalid=*/false); 3191 EqualLoc = PureSpecLoc = SourceLocation(); 3192 DeclaratorInfo.setCommaLoc(CommaLoc); 3193 3194 // GNU attributes are allowed before the second and subsequent declarator. 3195 // However, this does not apply for [[]] attributes (which could show up 3196 // before or after the __attribute__ attributes). 3197 DiagnoseAndSkipCXX11Attributes(); 3198 MaybeParseGNUAttributes(DeclaratorInfo); 3199 DiagnoseAndSkipCXX11Attributes(); 3200 3201 if (ParseCXXMemberDeclaratorBeforeInitializer( 3202 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) 3203 break; 3204 } 3205 3206 if (ExpectSemi && 3207 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) { 3208 // Skip to end of block or statement. 3209 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); 3210 // If we stopped at a ';', eat it. 3211 TryConsumeToken(tok::semi); 3212 return nullptr; 3213 } 3214 3215 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); 3216 } 3217 3218 /// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer. 3219 /// Also detect and reject any attempted defaulted/deleted function definition. 3220 /// The location of the '=', if any, will be placed in EqualLoc. 3221 /// 3222 /// This does not check for a pure-specifier; that's handled elsewhere. 3223 /// 3224 /// brace-or-equal-initializer: 3225 /// '=' initializer-expression 3226 /// braced-init-list 3227 /// 3228 /// initializer-clause: 3229 /// assignment-expression 3230 /// braced-init-list 3231 /// 3232 /// defaulted/deleted function-definition: 3233 /// '=' 'default' 3234 /// '=' 'delete' 3235 /// 3236 /// Prior to C++0x, the assignment-expression in an initializer-clause must 3237 /// be a constant-expression. 3238 ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction, 3239 SourceLocation &EqualLoc) { 3240 assert(Tok.isOneOf(tok::equal, tok::l_brace) && 3241 "Data member initializer not starting with '=' or '{'"); 3242 3243 bool IsFieldInitialization = isa_and_present<FieldDecl>(D); 3244 3245 EnterExpressionEvaluationContext Context( 3246 Actions, 3247 IsFieldInitialization 3248 ? Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed 3249 : Sema::ExpressionEvaluationContext::PotentiallyEvaluated, 3250 D); 3251 3252 // CWG2760 3253 // Default member initializers used to initialize a base or member subobject 3254 // [...] are considered to be part of the function body 3255 Actions.ExprEvalContexts.back().InImmediateEscalatingFunctionContext = 3256 IsFieldInitialization; 3257 3258 if (TryConsumeToken(tok::equal, EqualLoc)) { 3259 if (Tok.is(tok::kw_delete)) { 3260 // In principle, an initializer of '= delete p;' is legal, but it will 3261 // never type-check. It's better to diagnose it as an ill-formed 3262 // expression than as an ill-formed deleted non-function member. An 3263 // initializer of '= delete p, foo' will never be parsed, because a 3264 // top-level comma always ends the initializer expression. 3265 const Token &Next = NextToken(); 3266 if (IsFunction || Next.isOneOf(tok::semi, tok::comma, tok::eof)) { 3267 if (IsFunction) 3268 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) 3269 << 1 /* delete */; 3270 else 3271 Diag(ConsumeToken(), diag::err_deleted_non_function); 3272 return ExprError(); 3273 } 3274 } else if (Tok.is(tok::kw_default)) { 3275 if (IsFunction) 3276 Diag(Tok, diag::err_default_delete_in_multiple_declaration) 3277 << 0 /* default */; 3278 else 3279 Diag(ConsumeToken(), diag::err_default_special_members) 3280 << getLangOpts().CPlusPlus20; 3281 return ExprError(); 3282 } 3283 } 3284 if (const auto *PD = dyn_cast_or_null<MSPropertyDecl>(D)) { 3285 Diag(Tok, diag::err_ms_property_initializer) << PD; 3286 return ExprError(); 3287 } 3288 return ParseInitializer(); 3289 } 3290 3291 void Parser::SkipCXXMemberSpecification(SourceLocation RecordLoc, 3292 SourceLocation AttrFixitLoc, 3293 unsigned TagType, Decl *TagDecl) { 3294 // Skip the optional 'final' keyword. 3295 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) { 3296 assert(isCXX11FinalKeyword() && "not a class definition"); 3297 ConsumeToken(); 3298 3299 // Diagnose any C++11 attributes after 'final' keyword. 3300 // We deliberately discard these attributes. 3301 ParsedAttributes Attrs(AttrFactory); 3302 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc); 3303 3304 // This can only happen if we had malformed misplaced attributes; 3305 // we only get called if there is a colon or left-brace after the 3306 // attributes. 3307 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_brace)) 3308 return; 3309 } 3310 3311 // Skip the base clauses. This requires actually parsing them, because 3312 // otherwise we can't be sure where they end (a left brace may appear 3313 // within a template argument). 3314 if (Tok.is(tok::colon)) { 3315 // Enter the scope of the class so that we can correctly parse its bases. 3316 ParseScope ClassScope(this, Scope::ClassScope | Scope::DeclScope); 3317 ParsingClassDefinition ParsingDef(*this, TagDecl, /*NonNestedClass*/ true, 3318 TagType == DeclSpec::TST_interface); 3319 auto OldContext = 3320 Actions.ActOnTagStartSkippedDefinition(getCurScope(), TagDecl); 3321 3322 // Parse the bases but don't attach them to the class. 3323 ParseBaseClause(nullptr); 3324 3325 Actions.ActOnTagFinishSkippedDefinition(OldContext); 3326 3327 if (!Tok.is(tok::l_brace)) { 3328 Diag(PP.getLocForEndOfToken(PrevTokLocation), 3329 diag::err_expected_lbrace_after_base_specifiers); 3330 return; 3331 } 3332 } 3333 3334 // Skip the body. 3335 assert(Tok.is(tok::l_brace)); 3336 BalancedDelimiterTracker T(*this, tok::l_brace); 3337 T.consumeOpen(); 3338 T.skipToEnd(); 3339 3340 // Parse and discard any trailing attributes. 3341 if (Tok.is(tok::kw___attribute)) { 3342 ParsedAttributes Attrs(AttrFactory); 3343 MaybeParseGNUAttributes(Attrs); 3344 } 3345 } 3346 3347 Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclarationWithPragmas( 3348 AccessSpecifier &AS, ParsedAttributes &AccessAttrs, DeclSpec::TST TagType, 3349 Decl *TagDecl) { 3350 ParenBraceBracketBalancer BalancerRAIIObj(*this); 3351 3352 switch (Tok.getKind()) { 3353 case tok::kw___if_exists: 3354 case tok::kw___if_not_exists: 3355 ParseMicrosoftIfExistsClassDeclaration(TagType, AccessAttrs, AS); 3356 return nullptr; 3357 3358 case tok::semi: 3359 // Check for extraneous top-level semicolon. 3360 ConsumeExtraSemi(InsideStruct, TagType); 3361 return nullptr; 3362 3363 // Handle pragmas that can appear as member declarations. 3364 case tok::annot_pragma_vis: 3365 HandlePragmaVisibility(); 3366 return nullptr; 3367 case tok::annot_pragma_pack: 3368 HandlePragmaPack(); 3369 return nullptr; 3370 case tok::annot_pragma_align: 3371 HandlePragmaAlign(); 3372 return nullptr; 3373 case tok::annot_pragma_ms_pointers_to_members: 3374 HandlePragmaMSPointersToMembers(); 3375 return nullptr; 3376 case tok::annot_pragma_ms_pragma: 3377 HandlePragmaMSPragma(); 3378 return nullptr; 3379 case tok::annot_pragma_ms_vtordisp: 3380 HandlePragmaMSVtorDisp(); 3381 return nullptr; 3382 case tok::annot_pragma_dump: 3383 HandlePragmaDump(); 3384 return nullptr; 3385 3386 case tok::kw_namespace: 3387 // If we see a namespace here, a close brace was missing somewhere. 3388 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl)); 3389 return nullptr; 3390 3391 case tok::kw_private: 3392 // FIXME: We don't accept GNU attributes on access specifiers in OpenCL mode 3393 // yet. 3394 if (getLangOpts().OpenCL && !NextToken().is(tok::colon)) 3395 return ParseCXXClassMemberDeclaration(AS, AccessAttrs); 3396 [[fallthrough]]; 3397 case tok::kw_public: 3398 case tok::kw_protected: { 3399 if (getLangOpts().HLSL) 3400 Diag(Tok.getLocation(), diag::ext_hlsl_access_specifiers); 3401 AccessSpecifier NewAS = getAccessSpecifierIfPresent(); 3402 assert(NewAS != AS_none); 3403 // Current token is a C++ access specifier. 3404 AS = NewAS; 3405 SourceLocation ASLoc = Tok.getLocation(); 3406 unsigned TokLength = Tok.getLength(); 3407 ConsumeToken(); 3408 AccessAttrs.clear(); 3409 MaybeParseGNUAttributes(AccessAttrs); 3410 3411 SourceLocation EndLoc; 3412 if (TryConsumeToken(tok::colon, EndLoc)) { 3413 } else if (TryConsumeToken(tok::semi, EndLoc)) { 3414 Diag(EndLoc, diag::err_expected) 3415 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":"); 3416 } else { 3417 EndLoc = ASLoc.getLocWithOffset(TokLength); 3418 Diag(EndLoc, diag::err_expected) 3419 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":"); 3420 } 3421 3422 // The Microsoft extension __interface does not permit non-public 3423 // access specifiers. 3424 if (TagType == DeclSpec::TST_interface && AS != AS_public) { 3425 Diag(ASLoc, diag::err_access_specifier_interface) << (AS == AS_protected); 3426 } 3427 3428 if (Actions.ActOnAccessSpecifier(NewAS, ASLoc, EndLoc, AccessAttrs)) { 3429 // found another attribute than only annotations 3430 AccessAttrs.clear(); 3431 } 3432 3433 return nullptr; 3434 } 3435 3436 case tok::annot_attr_openmp: 3437 case tok::annot_pragma_openmp: 3438 return ParseOpenMPDeclarativeDirectiveWithExtDecl( 3439 AS, AccessAttrs, /*Delayed=*/true, TagType, TagDecl); 3440 case tok::annot_pragma_openacc: 3441 return ParseOpenACCDirectiveDecl(); 3442 3443 default: 3444 if (tok::isPragmaAnnotation(Tok.getKind())) { 3445 Diag(Tok.getLocation(), diag::err_pragma_misplaced_in_decl) 3446 << DeclSpec::getSpecifierName( 3447 TagType, Actions.getASTContext().getPrintingPolicy()); 3448 ConsumeAnnotationToken(); 3449 return nullptr; 3450 } 3451 return ParseCXXClassMemberDeclaration(AS, AccessAttrs); 3452 } 3453 } 3454 3455 /// ParseCXXMemberSpecification - Parse the class definition. 3456 /// 3457 /// member-specification: 3458 /// member-declaration member-specification[opt] 3459 /// access-specifier ':' member-specification[opt] 3460 /// 3461 void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, 3462 SourceLocation AttrFixitLoc, 3463 ParsedAttributes &Attrs, 3464 unsigned TagType, Decl *TagDecl) { 3465 assert((TagType == DeclSpec::TST_struct || 3466 TagType == DeclSpec::TST_interface || 3467 TagType == DeclSpec::TST_union || TagType == DeclSpec::TST_class) && 3468 "Invalid TagType!"); 3469 3470 llvm::TimeTraceScope TimeScope("ParseClass", [&]() { 3471 if (auto *TD = dyn_cast_or_null<NamedDecl>(TagDecl)) 3472 return TD->getQualifiedNameAsString(); 3473 return std::string("<anonymous>"); 3474 }); 3475 3476 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc, 3477 "parsing struct/union/class body"); 3478 3479 // Determine whether this is a non-nested class. Note that local 3480 // classes are *not* considered to be nested classes. 3481 bool NonNestedClass = true; 3482 if (!ClassStack.empty()) { 3483 for (const Scope *S = getCurScope(); S; S = S->getParent()) { 3484 if (S->isClassScope()) { 3485 // We're inside a class scope, so this is a nested class. 3486 NonNestedClass = false; 3487 3488 // The Microsoft extension __interface does not permit nested classes. 3489 if (getCurrentClass().IsInterface) { 3490 Diag(RecordLoc, diag::err_invalid_member_in_interface) 3491 << /*ErrorType=*/6 3492 << (isa<NamedDecl>(TagDecl) 3493 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString() 3494 : "(anonymous)"); 3495 } 3496 break; 3497 } 3498 3499 if (S->isFunctionScope()) 3500 // If we're in a function or function template then this is a local 3501 // class rather than a nested class. 3502 break; 3503 } 3504 } 3505 3506 // Enter a scope for the class. 3507 ParseScope ClassScope(this, Scope::ClassScope | Scope::DeclScope); 3508 3509 // Note that we are parsing a new (potentially-nested) class definition. 3510 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass, 3511 TagType == DeclSpec::TST_interface); 3512 3513 if (TagDecl) 3514 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); 3515 3516 SourceLocation FinalLoc; 3517 SourceLocation AbstractLoc; 3518 bool IsFinalSpelledSealed = false; 3519 bool IsAbstract = false; 3520 3521 // Parse the optional 'final' keyword. 3522 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) { 3523 while (true) { 3524 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok); 3525 if (Specifier == VirtSpecifiers::VS_None) 3526 break; 3527 if (isCXX11FinalKeyword()) { 3528 if (FinalLoc.isValid()) { 3529 auto Skipped = ConsumeToken(); 3530 Diag(Skipped, diag::err_duplicate_class_virt_specifier) 3531 << VirtSpecifiers::getSpecifierName(Specifier); 3532 } else { 3533 FinalLoc = ConsumeToken(); 3534 if (Specifier == VirtSpecifiers::VS_Sealed) 3535 IsFinalSpelledSealed = true; 3536 } 3537 } else { 3538 if (AbstractLoc.isValid()) { 3539 auto Skipped = ConsumeToken(); 3540 Diag(Skipped, diag::err_duplicate_class_virt_specifier) 3541 << VirtSpecifiers::getSpecifierName(Specifier); 3542 } else { 3543 AbstractLoc = ConsumeToken(); 3544 IsAbstract = true; 3545 } 3546 } 3547 if (TagType == DeclSpec::TST_interface) 3548 Diag(FinalLoc, diag::err_override_control_interface) 3549 << VirtSpecifiers::getSpecifierName(Specifier); 3550 else if (Specifier == VirtSpecifiers::VS_Final) 3551 Diag(FinalLoc, getLangOpts().CPlusPlus11 3552 ? diag::warn_cxx98_compat_override_control_keyword 3553 : diag::ext_override_control_keyword) 3554 << VirtSpecifiers::getSpecifierName(Specifier); 3555 else if (Specifier == VirtSpecifiers::VS_Sealed) 3556 Diag(FinalLoc, diag::ext_ms_sealed_keyword); 3557 else if (Specifier == VirtSpecifiers::VS_Abstract) 3558 Diag(AbstractLoc, diag::ext_ms_abstract_keyword); 3559 else if (Specifier == VirtSpecifiers::VS_GNU_Final) 3560 Diag(FinalLoc, diag::ext_warn_gnu_final); 3561 } 3562 assert((FinalLoc.isValid() || AbstractLoc.isValid()) && 3563 "not a class definition"); 3564 3565 // Parse any C++11 attributes after 'final' keyword. 3566 // These attributes are not allowed to appear here, 3567 // and the only possible place for them to appertain 3568 // to the class would be between class-key and class-name. 3569 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc); 3570 3571 // ParseClassSpecifier() does only a superficial check for attributes before 3572 // deciding to call this method. For example, for 3573 // `class C final alignas ([l) {` it will decide that this looks like a 3574 // misplaced attribute since it sees `alignas '(' ')'`. But the actual 3575 // attribute parsing code will try to parse the '[' as a constexpr lambda 3576 // and consume enough tokens that the alignas parsing code will eat the 3577 // opening '{'. So bail out if the next token isn't one we expect. 3578 if (!Tok.is(tok::colon) && !Tok.is(tok::l_brace)) { 3579 if (TagDecl) 3580 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl); 3581 return; 3582 } 3583 } 3584 3585 if (Tok.is(tok::colon)) { 3586 ParseScope InheritanceScope(this, getCurScope()->getFlags() | 3587 Scope::ClassInheritanceScope); 3588 3589 ParseBaseClause(TagDecl); 3590 if (!Tok.is(tok::l_brace)) { 3591 bool SuggestFixIt = false; 3592 SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation); 3593 if (Tok.isAtStartOfLine()) { 3594 switch (Tok.getKind()) { 3595 case tok::kw_private: 3596 case tok::kw_protected: 3597 case tok::kw_public: 3598 SuggestFixIt = NextToken().getKind() == tok::colon; 3599 break; 3600 case tok::kw_static_assert: 3601 case tok::r_brace: 3602 case tok::kw_using: 3603 // base-clause can have simple-template-id; 'template' can't be there 3604 case tok::kw_template: 3605 SuggestFixIt = true; 3606 break; 3607 case tok::identifier: 3608 SuggestFixIt = isConstructorDeclarator(true); 3609 break; 3610 default: 3611 SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false); 3612 break; 3613 } 3614 } 3615 DiagnosticBuilder LBraceDiag = 3616 Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers); 3617 if (SuggestFixIt) { 3618 LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {"); 3619 // Try recovering from missing { after base-clause. 3620 PP.EnterToken(Tok, /*IsReinject*/ true); 3621 Tok.setKind(tok::l_brace); 3622 } else { 3623 if (TagDecl) 3624 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl); 3625 return; 3626 } 3627 } 3628 } 3629 3630 assert(Tok.is(tok::l_brace)); 3631 BalancedDelimiterTracker T(*this, tok::l_brace); 3632 T.consumeOpen(); 3633 3634 if (TagDecl) 3635 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc, 3636 IsFinalSpelledSealed, IsAbstract, 3637 T.getOpenLocation()); 3638 3639 // C++ 11p3: Members of a class defined with the keyword class are private 3640 // by default. Members of a class defined with the keywords struct or union 3641 // are public by default. 3642 // HLSL: In HLSL members of a class are public by default. 3643 AccessSpecifier CurAS; 3644 if (TagType == DeclSpec::TST_class && !getLangOpts().HLSL) 3645 CurAS = AS_private; 3646 else 3647 CurAS = AS_public; 3648 ParsedAttributes AccessAttrs(AttrFactory); 3649 3650 if (TagDecl) { 3651 // While we still have something to read, read the member-declarations. 3652 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && 3653 Tok.isNot(tok::eof)) { 3654 // Each iteration of this loop reads one member-declaration. 3655 ParseCXXClassMemberDeclarationWithPragmas( 3656 CurAS, AccessAttrs, static_cast<DeclSpec::TST>(TagType), TagDecl); 3657 MaybeDestroyTemplateIds(); 3658 } 3659 T.consumeClose(); 3660 } else { 3661 SkipUntil(tok::r_brace); 3662 } 3663 3664 // If attributes exist after class contents, parse them. 3665 ParsedAttributes attrs(AttrFactory); 3666 MaybeParseGNUAttributes(attrs); 3667 3668 if (TagDecl) 3669 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl, 3670 T.getOpenLocation(), 3671 T.getCloseLocation(), attrs); 3672 3673 // C++11 [class.mem]p2: 3674 // Within the class member-specification, the class is regarded as complete 3675 // within function bodies, default arguments, exception-specifications, and 3676 // brace-or-equal-initializers for non-static data members (including such 3677 // things in nested classes). 3678 if (TagDecl && NonNestedClass) { 3679 // We are not inside a nested class. This class and its nested classes 3680 // are complete and we can parse the delayed portions of method 3681 // declarations and the lexed inline method definitions, along with any 3682 // delayed attributes. 3683 3684 SourceLocation SavedPrevTokLocation = PrevTokLocation; 3685 ParseLexedPragmas(getCurrentClass()); 3686 ParseLexedAttributes(getCurrentClass()); 3687 ParseLexedMethodDeclarations(getCurrentClass()); 3688 3689 // We've finished with all pending member declarations. 3690 Actions.ActOnFinishCXXMemberDecls(); 3691 3692 ParseLexedMemberInitializers(getCurrentClass()); 3693 ParseLexedMethodDefs(getCurrentClass()); 3694 PrevTokLocation = SavedPrevTokLocation; 3695 3696 // We've finished parsing everything, including default argument 3697 // initializers. 3698 Actions.ActOnFinishCXXNonNestedClass(); 3699 } 3700 3701 if (TagDecl) 3702 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange()); 3703 3704 // Leave the class scope. 3705 ParsingDef.Pop(); 3706 ClassScope.Exit(); 3707 } 3708 3709 void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) { 3710 assert(Tok.is(tok::kw_namespace)); 3711 3712 // FIXME: Suggest where the close brace should have gone by looking 3713 // at indentation changes within the definition body. 3714 Diag(D->getLocation(), diag::err_missing_end_of_definition) << D; 3715 Diag(Tok.getLocation(), diag::note_missing_end_of_definition_before) << D; 3716 3717 // Push '};' onto the token stream to recover. 3718 PP.EnterToken(Tok, /*IsReinject*/ true); 3719 3720 Tok.startToken(); 3721 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation)); 3722 Tok.setKind(tok::semi); 3723 PP.EnterToken(Tok, /*IsReinject*/ true); 3724 3725 Tok.setKind(tok::r_brace); 3726 } 3727 3728 /// ParseConstructorInitializer - Parse a C++ constructor initializer, 3729 /// which explicitly initializes the members or base classes of a 3730 /// class (C++ [class.base.init]). For example, the three initializers 3731 /// after the ':' in the Derived constructor below: 3732 /// 3733 /// @code 3734 /// class Base { }; 3735 /// class Derived : Base { 3736 /// int x; 3737 /// float f; 3738 /// public: 3739 /// Derived(float f) : Base(), x(17), f(f) { } 3740 /// }; 3741 /// @endcode 3742 /// 3743 /// [C++] ctor-initializer: 3744 /// ':' mem-initializer-list 3745 /// 3746 /// [C++] mem-initializer-list: 3747 /// mem-initializer ...[opt] 3748 /// mem-initializer ...[opt] , mem-initializer-list 3749 void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) { 3750 assert(Tok.is(tok::colon) && 3751 "Constructor initializer always starts with ':'"); 3752 3753 // Poison the SEH identifiers so they are flagged as illegal in constructor 3754 // initializers. 3755 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true); 3756 SourceLocation ColonLoc = ConsumeToken(); 3757 3758 SmallVector<CXXCtorInitializer *, 4> MemInitializers; 3759 bool AnyErrors = false; 3760 3761 do { 3762 if (Tok.is(tok::code_completion)) { 3763 cutOffParsing(); 3764 Actions.CodeCompleteConstructorInitializer(ConstructorDecl, 3765 MemInitializers); 3766 return; 3767 } 3768 3769 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); 3770 if (!MemInit.isInvalid()) 3771 MemInitializers.push_back(MemInit.get()); 3772 else 3773 AnyErrors = true; 3774 3775 if (Tok.is(tok::comma)) 3776 ConsumeToken(); 3777 else if (Tok.is(tok::l_brace)) 3778 break; 3779 // If the previous initializer was valid and the next token looks like a 3780 // base or member initializer, assume that we're just missing a comma. 3781 else if (!MemInit.isInvalid() && 3782 Tok.isOneOf(tok::identifier, tok::coloncolon)) { 3783 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); 3784 Diag(Loc, diag::err_ctor_init_missing_comma) 3785 << FixItHint::CreateInsertion(Loc, ", "); 3786 } else { 3787 // Skip over garbage, until we get to '{'. Don't eat the '{'. 3788 if (!MemInit.isInvalid()) 3789 Diag(Tok.getLocation(), diag::err_expected_either) 3790 << tok::l_brace << tok::comma; 3791 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch); 3792 break; 3793 } 3794 } while (true); 3795 3796 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers, 3797 AnyErrors); 3798 } 3799 3800 /// ParseMemInitializer - Parse a C++ member initializer, which is 3801 /// part of a constructor initializer that explicitly initializes one 3802 /// member or base class (C++ [class.base.init]). See 3803 /// ParseConstructorInitializer for an example. 3804 /// 3805 /// [C++] mem-initializer: 3806 /// mem-initializer-id '(' expression-list[opt] ')' 3807 /// [C++0x] mem-initializer-id braced-init-list 3808 /// 3809 /// [C++] mem-initializer-id: 3810 /// '::'[opt] nested-name-specifier[opt] class-name 3811 /// identifier 3812 MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) { 3813 // parse '::'[opt] nested-name-specifier[opt] 3814 CXXScopeSpec SS; 3815 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, 3816 /*ObjectHasErrors=*/false, 3817 /*EnteringContext=*/false)) 3818 return true; 3819 3820 // : identifier 3821 IdentifierInfo *II = nullptr; 3822 SourceLocation IdLoc = Tok.getLocation(); 3823 // : declype(...) 3824 DeclSpec DS(AttrFactory); 3825 // : template_name<...> 3826 TypeResult TemplateTypeTy; 3827 3828 if (Tok.is(tok::identifier)) { 3829 // Get the identifier. This may be a member name or a class name, 3830 // but we'll let the semantic analysis determine which it is. 3831 II = Tok.getIdentifierInfo(); 3832 ConsumeToken(); 3833 } else if (Tok.is(tok::annot_decltype)) { 3834 // Get the decltype expression, if there is one. 3835 // Uses of decltype will already have been converted to annot_decltype by 3836 // ParseOptionalCXXScopeSpecifier at this point. 3837 // FIXME: Can we get here with a scope specifier? 3838 ParseDecltypeSpecifier(DS); 3839 } else { 3840 TemplateIdAnnotation *TemplateId = Tok.is(tok::annot_template_id) 3841 ? takeTemplateIdAnnotation(Tok) 3842 : nullptr; 3843 if (TemplateId && TemplateId->mightBeType()) { 3844 AnnotateTemplateIdTokenAsType(SS, ImplicitTypenameContext::No, 3845 /*IsClassName=*/true); 3846 assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); 3847 TemplateTypeTy = getTypeAnnotation(Tok); 3848 ConsumeAnnotationToken(); 3849 } else { 3850 Diag(Tok, diag::err_expected_member_or_base_name); 3851 return true; 3852 } 3853 } 3854 3855 // Parse the '('. 3856 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { 3857 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); 3858 3859 // FIXME: Add support for signature help inside initializer lists. 3860 ExprResult InitList = ParseBraceInitializer(); 3861 if (InitList.isInvalid()) 3862 return true; 3863 3864 SourceLocation EllipsisLoc; 3865 TryConsumeToken(tok::ellipsis, EllipsisLoc); 3866 3867 if (TemplateTypeTy.isInvalid()) 3868 return true; 3869 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II, 3870 TemplateTypeTy.get(), DS, IdLoc, 3871 InitList.get(), EllipsisLoc); 3872 } else if (Tok.is(tok::l_paren)) { 3873 BalancedDelimiterTracker T(*this, tok::l_paren); 3874 T.consumeOpen(); 3875 3876 // Parse the optional expression-list. 3877 ExprVector ArgExprs; 3878 auto RunSignatureHelp = [&] { 3879 if (TemplateTypeTy.isInvalid()) 3880 return QualType(); 3881 QualType PreferredType = Actions.ProduceCtorInitMemberSignatureHelp( 3882 ConstructorDecl, SS, TemplateTypeTy.get(), ArgExprs, II, 3883 T.getOpenLocation(), /*Braced=*/false); 3884 CalledSignatureHelp = true; 3885 return PreferredType; 3886 }; 3887 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, [&] { 3888 PreferredType.enterFunctionArgument(Tok.getLocation(), 3889 RunSignatureHelp); 3890 })) { 3891 if (PP.isCodeCompletionReached() && !CalledSignatureHelp) 3892 RunSignatureHelp(); 3893 SkipUntil(tok::r_paren, StopAtSemi); 3894 return true; 3895 } 3896 3897 T.consumeClose(); 3898 3899 SourceLocation EllipsisLoc; 3900 TryConsumeToken(tok::ellipsis, EllipsisLoc); 3901 3902 if (TemplateTypeTy.isInvalid()) 3903 return true; 3904 return Actions.ActOnMemInitializer( 3905 ConstructorDecl, getCurScope(), SS, II, TemplateTypeTy.get(), DS, IdLoc, 3906 T.getOpenLocation(), ArgExprs, T.getCloseLocation(), EllipsisLoc); 3907 } 3908 3909 if (TemplateTypeTy.isInvalid()) 3910 return true; 3911 3912 if (getLangOpts().CPlusPlus11) 3913 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace; 3914 else 3915 return Diag(Tok, diag::err_expected) << tok::l_paren; 3916 } 3917 3918 /// Parse a C++ exception-specification if present (C++0x [except.spec]). 3919 /// 3920 /// exception-specification: 3921 /// dynamic-exception-specification 3922 /// noexcept-specification 3923 /// 3924 /// noexcept-specification: 3925 /// 'noexcept' 3926 /// 'noexcept' '(' constant-expression ')' 3927 ExceptionSpecificationType Parser::tryParseExceptionSpecification( 3928 bool Delayed, SourceRange &SpecificationRange, 3929 SmallVectorImpl<ParsedType> &DynamicExceptions, 3930 SmallVectorImpl<SourceRange> &DynamicExceptionRanges, 3931 ExprResult &NoexceptExpr, CachedTokens *&ExceptionSpecTokens) { 3932 ExceptionSpecificationType Result = EST_None; 3933 ExceptionSpecTokens = nullptr; 3934 3935 // Handle delayed parsing of exception-specifications. 3936 if (Delayed) { 3937 if (Tok.isNot(tok::kw_throw) && Tok.isNot(tok::kw_noexcept)) 3938 return EST_None; 3939 3940 // Consume and cache the starting token. 3941 bool IsNoexcept = Tok.is(tok::kw_noexcept); 3942 Token StartTok = Tok; 3943 SpecificationRange = SourceRange(ConsumeToken()); 3944 3945 // Check for a '('. 3946 if (!Tok.is(tok::l_paren)) { 3947 // If this is a bare 'noexcept', we're done. 3948 if (IsNoexcept) { 3949 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl); 3950 NoexceptExpr = nullptr; 3951 return EST_BasicNoexcept; 3952 } 3953 3954 Diag(Tok, diag::err_expected_lparen_after) << "throw"; 3955 return EST_DynamicNone; 3956 } 3957 3958 // Cache the tokens for the exception-specification. 3959 ExceptionSpecTokens = new CachedTokens; 3960 ExceptionSpecTokens->push_back(StartTok); // 'throw' or 'noexcept' 3961 ExceptionSpecTokens->push_back(Tok); // '(' 3962 SpecificationRange.setEnd(ConsumeParen()); // '(' 3963 3964 ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens, 3965 /*StopAtSemi=*/true, 3966 /*ConsumeFinalToken=*/true); 3967 SpecificationRange.setEnd(ExceptionSpecTokens->back().getLocation()); 3968 3969 return EST_Unparsed; 3970 } 3971 3972 // See if there's a dynamic specification. 3973 if (Tok.is(tok::kw_throw)) { 3974 Result = ParseDynamicExceptionSpecification( 3975 SpecificationRange, DynamicExceptions, DynamicExceptionRanges); 3976 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() && 3977 "Produced different number of exception types and ranges."); 3978 } 3979 3980 // If there's no noexcept specification, we're done. 3981 if (Tok.isNot(tok::kw_noexcept)) 3982 return Result; 3983 3984 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl); 3985 3986 // If we already had a dynamic specification, parse the noexcept for, 3987 // recovery, but emit a diagnostic and don't store the results. 3988 SourceRange NoexceptRange; 3989 ExceptionSpecificationType NoexceptType = EST_None; 3990 3991 SourceLocation KeywordLoc = ConsumeToken(); 3992 if (Tok.is(tok::l_paren)) { 3993 // There is an argument. 3994 BalancedDelimiterTracker T(*this, tok::l_paren); 3995 T.consumeOpen(); 3996 3997 EnterExpressionEvaluationContext ConstantEvaluated( 3998 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated); 3999 NoexceptExpr = ParseConstantExpressionInExprEvalContext(); 4000 4001 T.consumeClose(); 4002 if (!NoexceptExpr.isInvalid()) { 4003 NoexceptExpr = 4004 Actions.ActOnNoexceptSpec(NoexceptExpr.get(), NoexceptType); 4005 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation()); 4006 } else { 4007 NoexceptType = EST_BasicNoexcept; 4008 } 4009 } else { 4010 // There is no argument. 4011 NoexceptType = EST_BasicNoexcept; 4012 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc); 4013 } 4014 4015 if (Result == EST_None) { 4016 SpecificationRange = NoexceptRange; 4017 Result = NoexceptType; 4018 4019 // If there's a dynamic specification after a noexcept specification, 4020 // parse that and ignore the results. 4021 if (Tok.is(tok::kw_throw)) { 4022 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); 4023 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions, 4024 DynamicExceptionRanges); 4025 } 4026 } else { 4027 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification); 4028 } 4029 4030 return Result; 4031 } 4032 4033 static void diagnoseDynamicExceptionSpecification(Parser &P, SourceRange Range, 4034 bool IsNoexcept) { 4035 if (P.getLangOpts().CPlusPlus11) { 4036 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)"; 4037 P.Diag(Range.getBegin(), P.getLangOpts().CPlusPlus17 && !IsNoexcept 4038 ? diag::ext_dynamic_exception_spec 4039 : diag::warn_exception_spec_deprecated) 4040 << Range; 4041 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated) 4042 << Replacement << FixItHint::CreateReplacement(Range, Replacement); 4043 } 4044 } 4045 4046 /// ParseDynamicExceptionSpecification - Parse a C++ 4047 /// dynamic-exception-specification (C++ [except.spec]). 4048 /// 4049 /// dynamic-exception-specification: 4050 /// 'throw' '(' type-id-list [opt] ')' 4051 /// [MS] 'throw' '(' '...' ')' 4052 /// 4053 /// type-id-list: 4054 /// type-id ... [opt] 4055 /// type-id-list ',' type-id ... [opt] 4056 /// 4057 ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification( 4058 SourceRange &SpecificationRange, SmallVectorImpl<ParsedType> &Exceptions, 4059 SmallVectorImpl<SourceRange> &Ranges) { 4060 assert(Tok.is(tok::kw_throw) && "expected throw"); 4061 4062 SpecificationRange.setBegin(ConsumeToken()); 4063 BalancedDelimiterTracker T(*this, tok::l_paren); 4064 if (T.consumeOpen()) { 4065 Diag(Tok, diag::err_expected_lparen_after) << "throw"; 4066 SpecificationRange.setEnd(SpecificationRange.getBegin()); 4067 return EST_DynamicNone; 4068 } 4069 4070 // Parse throw(...), a Microsoft extension that means "this function 4071 // can throw anything". 4072 if (Tok.is(tok::ellipsis)) { 4073 SourceLocation EllipsisLoc = ConsumeToken(); 4074 if (!getLangOpts().MicrosoftExt) 4075 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec); 4076 T.consumeClose(); 4077 SpecificationRange.setEnd(T.getCloseLocation()); 4078 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false); 4079 return EST_MSAny; 4080 } 4081 4082 // Parse the sequence of type-ids. 4083 SourceRange Range; 4084 while (Tok.isNot(tok::r_paren)) { 4085 TypeResult Res(ParseTypeName(&Range)); 4086 4087 if (Tok.is(tok::ellipsis)) { 4088 // C++0x [temp.variadic]p5: 4089 // - In a dynamic-exception-specification (15.4); the pattern is a 4090 // type-id. 4091 SourceLocation Ellipsis = ConsumeToken(); 4092 Range.setEnd(Ellipsis); 4093 if (!Res.isInvalid()) 4094 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis); 4095 } 4096 4097 if (!Res.isInvalid()) { 4098 Exceptions.push_back(Res.get()); 4099 Ranges.push_back(Range); 4100 } 4101 4102 if (!TryConsumeToken(tok::comma)) 4103 break; 4104 } 4105 4106 T.consumeClose(); 4107 SpecificationRange.setEnd(T.getCloseLocation()); 4108 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, 4109 Exceptions.empty()); 4110 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic; 4111 } 4112 4113 /// ParseTrailingReturnType - Parse a trailing return type on a new-style 4114 /// function declaration. 4115 TypeResult Parser::ParseTrailingReturnType(SourceRange &Range, 4116 bool MayBeFollowedByDirectInit) { 4117 assert(Tok.is(tok::arrow) && "expected arrow"); 4118 4119 ConsumeToken(); 4120 4121 return ParseTypeName(&Range, MayBeFollowedByDirectInit 4122 ? DeclaratorContext::TrailingReturnVar 4123 : DeclaratorContext::TrailingReturn); 4124 } 4125 4126 /// Parse a requires-clause as part of a function declaration. 4127 void Parser::ParseTrailingRequiresClause(Declarator &D) { 4128 assert(Tok.is(tok::kw_requires) && "expected requires"); 4129 4130 SourceLocation RequiresKWLoc = ConsumeToken(); 4131 4132 ExprResult TrailingRequiresClause; 4133 ParseScope ParamScope(this, Scope::DeclScope | 4134 Scope::FunctionDeclarationScope | 4135 Scope::FunctionPrototypeScope); 4136 4137 Actions.ActOnStartTrailingRequiresClause(getCurScope(), D); 4138 4139 std::optional<Sema::CXXThisScopeRAII> ThisScope; 4140 InitCXXThisScopeForDeclaratorIfRelevant(D, D.getDeclSpec(), ThisScope); 4141 4142 TrailingRequiresClause = 4143 ParseConstraintLogicalOrExpression(/*IsTrailingRequiresClause=*/true); 4144 4145 TrailingRequiresClause = 4146 Actions.ActOnFinishTrailingRequiresClause(TrailingRequiresClause); 4147 4148 if (!D.isDeclarationOfFunction()) { 4149 Diag(RequiresKWLoc, 4150 diag::err_requires_clause_on_declarator_not_declaring_a_function); 4151 return; 4152 } 4153 4154 if (TrailingRequiresClause.isInvalid()) 4155 SkipUntil({tok::l_brace, tok::arrow, tok::kw_try, tok::comma, tok::colon}, 4156 StopAtSemi | StopBeforeMatch); 4157 else 4158 D.setTrailingRequiresClause(TrailingRequiresClause.get()); 4159 4160 // Did the user swap the trailing return type and requires clause? 4161 if (D.isFunctionDeclarator() && Tok.is(tok::arrow) && 4162 D.getDeclSpec().getTypeSpecType() == TST_auto) { 4163 SourceLocation ArrowLoc = Tok.getLocation(); 4164 SourceRange Range; 4165 TypeResult TrailingReturnType = 4166 ParseTrailingReturnType(Range, /*MayBeFollowedByDirectInit=*/false); 4167 4168 if (!TrailingReturnType.isInvalid()) { 4169 Diag(ArrowLoc, 4170 diag::err_requires_clause_must_appear_after_trailing_return) 4171 << Range; 4172 auto &FunctionChunk = D.getFunctionTypeInfo(); 4173 FunctionChunk.HasTrailingReturnType = TrailingReturnType.isUsable(); 4174 FunctionChunk.TrailingReturnType = TrailingReturnType.get(); 4175 FunctionChunk.TrailingReturnTypeLoc = Range.getBegin(); 4176 } else 4177 SkipUntil({tok::equal, tok::l_brace, tok::arrow, tok::kw_try, tok::comma}, 4178 StopAtSemi | StopBeforeMatch); 4179 } 4180 } 4181 4182 /// We have just started parsing the definition of a new class, 4183 /// so push that class onto our stack of classes that is currently 4184 /// being parsed. 4185 Sema::ParsingClassState Parser::PushParsingClass(Decl *ClassDecl, 4186 bool NonNestedClass, 4187 bool IsInterface) { 4188 assert((NonNestedClass || !ClassStack.empty()) && 4189 "Nested class without outer class"); 4190 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface)); 4191 return Actions.PushParsingClass(); 4192 } 4193 4194 /// Deallocate the given parsed class and all of its nested 4195 /// classes. 4196 void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) { 4197 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I) 4198 delete Class->LateParsedDeclarations[I]; 4199 delete Class; 4200 } 4201 4202 /// Pop the top class of the stack of classes that are 4203 /// currently being parsed. 4204 /// 4205 /// This routine should be called when we have finished parsing the 4206 /// definition of a class, but have not yet popped the Scope 4207 /// associated with the class's definition. 4208 void Parser::PopParsingClass(Sema::ParsingClassState state) { 4209 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing"); 4210 4211 Actions.PopParsingClass(state); 4212 4213 ParsingClass *Victim = ClassStack.top(); 4214 ClassStack.pop(); 4215 if (Victim->TopLevelClass) { 4216 // Deallocate all of the nested classes of this class, 4217 // recursively: we don't need to keep any of this information. 4218 DeallocateParsedClasses(Victim); 4219 return; 4220 } 4221 assert(!ClassStack.empty() && "Missing top-level class?"); 4222 4223 if (Victim->LateParsedDeclarations.empty()) { 4224 // The victim is a nested class, but we will not need to perform 4225 // any processing after the definition of this class since it has 4226 // no members whose handling was delayed. Therefore, we can just 4227 // remove this nested class. 4228 DeallocateParsedClasses(Victim); 4229 return; 4230 } 4231 4232 // This nested class has some members that will need to be processed 4233 // after the top-level class is completely defined. Therefore, add 4234 // it to the list of nested classes within its parent. 4235 assert(getCurScope()->isClassScope() && 4236 "Nested class outside of class scope?"); 4237 ClassStack.top()->LateParsedDeclarations.push_back( 4238 new LateParsedClass(this, Victim)); 4239 } 4240 4241 /// Try to parse an 'identifier' which appears within an attribute-token. 4242 /// 4243 /// \return the parsed identifier on success, and 0 if the next token is not an 4244 /// attribute-token. 4245 /// 4246 /// C++11 [dcl.attr.grammar]p3: 4247 /// If a keyword or an alternative token that satisfies the syntactic 4248 /// requirements of an identifier is contained in an attribute-token, 4249 /// it is considered an identifier. 4250 IdentifierInfo * 4251 Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc, 4252 Sema::AttributeCompletion Completion, 4253 const IdentifierInfo *Scope) { 4254 switch (Tok.getKind()) { 4255 default: 4256 // Identifiers and keywords have identifier info attached. 4257 if (!Tok.isAnnotation()) { 4258 if (IdentifierInfo *II = Tok.getIdentifierInfo()) { 4259 Loc = ConsumeToken(); 4260 return II; 4261 } 4262 } 4263 return nullptr; 4264 4265 case tok::code_completion: 4266 cutOffParsing(); 4267 Actions.CodeCompleteAttribute(getLangOpts().CPlusPlus ? ParsedAttr::AS_CXX11 4268 : ParsedAttr::AS_C23, 4269 Completion, Scope); 4270 return nullptr; 4271 4272 case tok::numeric_constant: { 4273 // If we got a numeric constant, check to see if it comes from a macro that 4274 // corresponds to the predefined __clang__ macro. If it does, warn the user 4275 // and recover by pretending they said _Clang instead. 4276 if (Tok.getLocation().isMacroID()) { 4277 SmallString<8> ExpansionBuf; 4278 SourceLocation ExpansionLoc = 4279 PP.getSourceManager().getExpansionLoc(Tok.getLocation()); 4280 StringRef Spelling = PP.getSpelling(ExpansionLoc, ExpansionBuf); 4281 if (Spelling == "__clang__") { 4282 SourceRange TokRange( 4283 ExpansionLoc, 4284 PP.getSourceManager().getExpansionLoc(Tok.getEndLoc())); 4285 Diag(Tok, diag::warn_wrong_clang_attr_namespace) 4286 << FixItHint::CreateReplacement(TokRange, "_Clang"); 4287 Loc = ConsumeToken(); 4288 return &PP.getIdentifierTable().get("_Clang"); 4289 } 4290 } 4291 return nullptr; 4292 } 4293 4294 case tok::ampamp: // 'and' 4295 case tok::pipe: // 'bitor' 4296 case tok::pipepipe: // 'or' 4297 case tok::caret: // 'xor' 4298 case tok::tilde: // 'compl' 4299 case tok::amp: // 'bitand' 4300 case tok::ampequal: // 'and_eq' 4301 case tok::pipeequal: // 'or_eq' 4302 case tok::caretequal: // 'xor_eq' 4303 case tok::exclaim: // 'not' 4304 case tok::exclaimequal: // 'not_eq' 4305 // Alternative tokens do not have identifier info, but their spelling 4306 // starts with an alphabetical character. 4307 SmallString<8> SpellingBuf; 4308 SourceLocation SpellingLoc = 4309 PP.getSourceManager().getSpellingLoc(Tok.getLocation()); 4310 StringRef Spelling = PP.getSpelling(SpellingLoc, SpellingBuf); 4311 if (isLetter(Spelling[0])) { 4312 Loc = ConsumeToken(); 4313 return &PP.getIdentifierTable().get(Spelling); 4314 } 4315 return nullptr; 4316 } 4317 } 4318 4319 void Parser::ParseOpenMPAttributeArgs(const IdentifierInfo *AttrName, 4320 CachedTokens &OpenMPTokens) { 4321 // Both 'sequence' and 'directive' attributes require arguments, so parse the 4322 // open paren for the argument list. 4323 BalancedDelimiterTracker T(*this, tok::l_paren); 4324 if (T.consumeOpen()) { 4325 Diag(Tok, diag::err_expected) << tok::l_paren; 4326 return; 4327 } 4328 4329 if (AttrName->isStr("directive")) { 4330 // If the attribute is named `directive`, we can consume its argument list 4331 // and push the tokens from it into the cached token stream for a new OpenMP 4332 // pragma directive. 4333 Token OMPBeginTok; 4334 OMPBeginTok.startToken(); 4335 OMPBeginTok.setKind(tok::annot_attr_openmp); 4336 OMPBeginTok.setLocation(Tok.getLocation()); 4337 OpenMPTokens.push_back(OMPBeginTok); 4338 4339 ConsumeAndStoreUntil(tok::r_paren, OpenMPTokens, /*StopAtSemi=*/false, 4340 /*ConsumeFinalToken*/ false); 4341 Token OMPEndTok; 4342 OMPEndTok.startToken(); 4343 OMPEndTok.setKind(tok::annot_pragma_openmp_end); 4344 OMPEndTok.setLocation(Tok.getLocation()); 4345 OpenMPTokens.push_back(OMPEndTok); 4346 } else { 4347 assert(AttrName->isStr("sequence") && 4348 "Expected either 'directive' or 'sequence'"); 4349 // If the attribute is named 'sequence', its argument is a list of one or 4350 // more OpenMP attributes (either 'omp::directive' or 'omp::sequence', 4351 // where the 'omp::' is optional). 4352 do { 4353 // We expect to see one of the following: 4354 // * An identifier (omp) for the attribute namespace followed by :: 4355 // * An identifier (directive) or an identifier (sequence). 4356 SourceLocation IdentLoc; 4357 const IdentifierInfo *Ident = TryParseCXX11AttributeIdentifier(IdentLoc); 4358 4359 // If there is an identifier and it is 'omp', a double colon is required 4360 // followed by the actual identifier we're after. 4361 if (Ident && Ident->isStr("omp") && !ExpectAndConsume(tok::coloncolon)) 4362 Ident = TryParseCXX11AttributeIdentifier(IdentLoc); 4363 4364 // If we failed to find an identifier (scoped or otherwise), or we found 4365 // an unexpected identifier, diagnose. 4366 if (!Ident || (!Ident->isStr("directive") && !Ident->isStr("sequence"))) { 4367 Diag(Tok.getLocation(), diag::err_expected_sequence_or_directive); 4368 SkipUntil(tok::r_paren, StopBeforeMatch); 4369 continue; 4370 } 4371 // We read an identifier. If the identifier is one of the ones we 4372 // expected, we can recurse to parse the args. 4373 ParseOpenMPAttributeArgs(Ident, OpenMPTokens); 4374 4375 // There may be a comma to signal that we expect another directive in the 4376 // sequence. 4377 } while (TryConsumeToken(tok::comma)); 4378 } 4379 // Parse the closing paren for the argument list. 4380 T.consumeClose(); 4381 } 4382 4383 static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName, 4384 IdentifierInfo *ScopeName) { 4385 switch ( 4386 ParsedAttr::getParsedKind(AttrName, ScopeName, ParsedAttr::AS_CXX11)) { 4387 case ParsedAttr::AT_CarriesDependency: 4388 case ParsedAttr::AT_Deprecated: 4389 case ParsedAttr::AT_FallThrough: 4390 case ParsedAttr::AT_CXX11NoReturn: 4391 case ParsedAttr::AT_NoUniqueAddress: 4392 case ParsedAttr::AT_Likely: 4393 case ParsedAttr::AT_Unlikely: 4394 return true; 4395 case ParsedAttr::AT_WarnUnusedResult: 4396 return !ScopeName && AttrName->getName().equals("nodiscard"); 4397 case ParsedAttr::AT_Unused: 4398 return !ScopeName && AttrName->getName().equals("maybe_unused"); 4399 default: 4400 return false; 4401 } 4402 } 4403 4404 /// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause. 4405 /// 4406 /// [C++11] attribute-argument-clause: 4407 /// '(' balanced-token-seq ')' 4408 /// 4409 /// [C++11] balanced-token-seq: 4410 /// balanced-token 4411 /// balanced-token-seq balanced-token 4412 /// 4413 /// [C++11] balanced-token: 4414 /// '(' balanced-token-seq ')' 4415 /// '[' balanced-token-seq ']' 4416 /// '{' balanced-token-seq '}' 4417 /// any token but '(', ')', '[', ']', '{', or '}' 4418 bool Parser::ParseCXX11AttributeArgs( 4419 IdentifierInfo *AttrName, SourceLocation AttrNameLoc, 4420 ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName, 4421 SourceLocation ScopeLoc, CachedTokens &OpenMPTokens) { 4422 assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list"); 4423 SourceLocation LParenLoc = Tok.getLocation(); 4424 const LangOptions &LO = getLangOpts(); 4425 ParsedAttr::Form Form = 4426 LO.CPlusPlus ? ParsedAttr::Form::CXX11() : ParsedAttr::Form::C23(); 4427 4428 // Try parsing microsoft attributes 4429 if (getLangOpts().MicrosoftExt || getLangOpts().HLSL) { 4430 if (hasAttribute(AttributeCommonInfo::Syntax::AS_Microsoft, ScopeName, 4431 AttrName, getTargetInfo(), getLangOpts())) 4432 Form = ParsedAttr::Form::Microsoft(); 4433 } 4434 4435 // If the attribute isn't known, we will not attempt to parse any 4436 // arguments. 4437 if (Form.getSyntax() != ParsedAttr::AS_Microsoft && 4438 !hasAttribute(LO.CPlusPlus ? AttributeCommonInfo::Syntax::AS_CXX11 4439 : AttributeCommonInfo::Syntax::AS_C23, 4440 ScopeName, AttrName, getTargetInfo(), getLangOpts())) { 4441 // Eat the left paren, then skip to the ending right paren. 4442 ConsumeParen(); 4443 SkipUntil(tok::r_paren); 4444 return false; 4445 } 4446 4447 if (ScopeName && (ScopeName->isStr("gnu") || ScopeName->isStr("__gnu__"))) { 4448 // GNU-scoped attributes have some special cases to handle GNU-specific 4449 // behaviors. 4450 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, 4451 ScopeLoc, Form, nullptr); 4452 return true; 4453 } 4454 4455 if (ScopeName && ScopeName->isStr("omp")) { 4456 Diag(AttrNameLoc, getLangOpts().OpenMP >= 51 4457 ? diag::warn_omp51_compat_attributes 4458 : diag::ext_omp_attributes); 4459 4460 ParseOpenMPAttributeArgs(AttrName, OpenMPTokens); 4461 4462 // We claim that an attribute was parsed and added so that one is not 4463 // created for us by the caller. 4464 return true; 4465 } 4466 4467 unsigned NumArgs; 4468 // Some Clang-scoped attributes have some special parsing behavior. 4469 if (ScopeName && (ScopeName->isStr("clang") || ScopeName->isStr("_Clang"))) 4470 NumArgs = ParseClangAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, 4471 ScopeName, ScopeLoc, Form); 4472 else 4473 NumArgs = ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, 4474 ScopeName, ScopeLoc, Form); 4475 4476 if (!Attrs.empty() && 4477 IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) { 4478 ParsedAttr &Attr = Attrs.back(); 4479 4480 // Ignore attributes that don't exist for the target. 4481 if (!Attr.existsInTarget(getTargetInfo())) { 4482 Diag(LParenLoc, diag::warn_unknown_attribute_ignored) << AttrName; 4483 Attr.setInvalid(true); 4484 return true; 4485 } 4486 4487 // If the attribute is a standard or built-in attribute and we are 4488 // parsing an argument list, we need to determine whether this attribute 4489 // was allowed to have an argument list (such as [[deprecated]]), and how 4490 // many arguments were parsed (so we can diagnose on [[deprecated()]]). 4491 if (Attr.getMaxArgs() && !NumArgs) { 4492 // The attribute was allowed to have arguments, but none were provided 4493 // even though the attribute parsed successfully. This is an error. 4494 Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName; 4495 Attr.setInvalid(true); 4496 } else if (!Attr.getMaxArgs()) { 4497 // The attribute parsed successfully, but was not allowed to have any 4498 // arguments. It doesn't matter whether any were provided -- the 4499 // presence of the argument list (even if empty) is diagnosed. 4500 Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments) 4501 << AttrName 4502 << FixItHint::CreateRemoval(SourceRange(LParenLoc, *EndLoc)); 4503 Attr.setInvalid(true); 4504 } 4505 } 4506 return true; 4507 } 4508 4509 /// Parse a C++11 or C23 attribute-specifier. 4510 /// 4511 /// [C++11] attribute-specifier: 4512 /// '[' '[' attribute-list ']' ']' 4513 /// alignment-specifier 4514 /// 4515 /// [C++11] attribute-list: 4516 /// attribute[opt] 4517 /// attribute-list ',' attribute[opt] 4518 /// attribute '...' 4519 /// attribute-list ',' attribute '...' 4520 /// 4521 /// [C++11] attribute: 4522 /// attribute-token attribute-argument-clause[opt] 4523 /// 4524 /// [C++11] attribute-token: 4525 /// identifier 4526 /// attribute-scoped-token 4527 /// 4528 /// [C++11] attribute-scoped-token: 4529 /// attribute-namespace '::' identifier 4530 /// 4531 /// [C++11] attribute-namespace: 4532 /// identifier 4533 void Parser::ParseCXX11AttributeSpecifierInternal(ParsedAttributes &Attrs, 4534 CachedTokens &OpenMPTokens, 4535 SourceLocation *EndLoc) { 4536 if (Tok.is(tok::kw_alignas)) { 4537 if (getLangOpts().C23) 4538 Diag(Tok, diag::warn_c23_compat_keyword) << Tok.getName(); 4539 else 4540 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas); 4541 ParseAlignmentSpecifier(Attrs, EndLoc); 4542 return; 4543 } 4544 4545 if (Tok.isRegularKeywordAttribute()) { 4546 SourceLocation Loc = Tok.getLocation(); 4547 IdentifierInfo *AttrName = Tok.getIdentifierInfo(); 4548 ParsedAttr::Form Form = ParsedAttr::Form(Tok.getKind()); 4549 bool TakesArgs = doesKeywordAttributeTakeArgs(Tok.getKind()); 4550 ConsumeToken(); 4551 if (TakesArgs) { 4552 if (!Tok.is(tok::l_paren)) 4553 Diag(Tok.getLocation(), diag::err_expected_lparen_after) << AttrName; 4554 else 4555 ParseAttributeArgsCommon(AttrName, Loc, Attrs, EndLoc, 4556 /*ScopeName*/ nullptr, 4557 /*ScopeLoc*/ Loc, Form); 4558 } else 4559 Attrs.addNew(AttrName, Loc, nullptr, Loc, nullptr, 0, Form); 4560 return; 4561 } 4562 4563 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square) && 4564 "Not a double square bracket attribute list"); 4565 4566 SourceLocation OpenLoc = Tok.getLocation(); 4567 if (getLangOpts().CPlusPlus) { 4568 Diag(OpenLoc, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_attribute 4569 : diag::warn_ext_cxx11_attributes); 4570 } else { 4571 Diag(OpenLoc, getLangOpts().C23 ? diag::warn_pre_c23_compat_attributes 4572 : diag::warn_ext_c23_attributes); 4573 } 4574 4575 ConsumeBracket(); 4576 checkCompoundToken(OpenLoc, tok::l_square, CompoundToken::AttrBegin); 4577 ConsumeBracket(); 4578 4579 SourceLocation CommonScopeLoc; 4580 IdentifierInfo *CommonScopeName = nullptr; 4581 if (Tok.is(tok::kw_using)) { 4582 Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 4583 ? diag::warn_cxx14_compat_using_attribute_ns 4584 : diag::ext_using_attribute_ns); 4585 ConsumeToken(); 4586 4587 CommonScopeName = TryParseCXX11AttributeIdentifier( 4588 CommonScopeLoc, Sema::AttributeCompletion::Scope); 4589 if (!CommonScopeName) { 4590 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; 4591 SkipUntil(tok::r_square, tok::colon, StopBeforeMatch); 4592 } 4593 if (!TryConsumeToken(tok::colon) && CommonScopeName) 4594 Diag(Tok.getLocation(), diag::err_expected) << tok::colon; 4595 } 4596 4597 bool AttrParsed = false; 4598 while (!Tok.isOneOf(tok::r_square, tok::semi, tok::eof)) { 4599 if (AttrParsed) { 4600 // If we parsed an attribute, a comma is required before parsing any 4601 // additional attributes. 4602 if (ExpectAndConsume(tok::comma)) { 4603 SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch); 4604 continue; 4605 } 4606 AttrParsed = false; 4607 } 4608 4609 // Eat all remaining superfluous commas before parsing the next attribute. 4610 while (TryConsumeToken(tok::comma)) 4611 ; 4612 4613 SourceLocation ScopeLoc, AttrLoc; 4614 IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr; 4615 4616 AttrName = TryParseCXX11AttributeIdentifier( 4617 AttrLoc, Sema::AttributeCompletion::Attribute, CommonScopeName); 4618 if (!AttrName) 4619 // Break out to the "expected ']'" diagnostic. 4620 break; 4621 4622 // scoped attribute 4623 if (TryConsumeToken(tok::coloncolon)) { 4624 ScopeName = AttrName; 4625 ScopeLoc = AttrLoc; 4626 4627 AttrName = TryParseCXX11AttributeIdentifier( 4628 AttrLoc, Sema::AttributeCompletion::Attribute, ScopeName); 4629 if (!AttrName) { 4630 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; 4631 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch); 4632 continue; 4633 } 4634 } 4635 4636 if (CommonScopeName) { 4637 if (ScopeName) { 4638 Diag(ScopeLoc, diag::err_using_attribute_ns_conflict) 4639 << SourceRange(CommonScopeLoc); 4640 } else { 4641 ScopeName = CommonScopeName; 4642 ScopeLoc = CommonScopeLoc; 4643 } 4644 } 4645 4646 // Parse attribute arguments 4647 if (Tok.is(tok::l_paren)) 4648 AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, Attrs, EndLoc, 4649 ScopeName, ScopeLoc, OpenMPTokens); 4650 4651 if (!AttrParsed) { 4652 Attrs.addNew( 4653 AttrName, 4654 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc, AttrLoc), 4655 ScopeName, ScopeLoc, nullptr, 0, 4656 getLangOpts().CPlusPlus ? ParsedAttr::Form::CXX11() 4657 : ParsedAttr::Form::C23()); 4658 AttrParsed = true; 4659 } 4660 4661 if (TryConsumeToken(tok::ellipsis)) 4662 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis) << AttrName; 4663 } 4664 4665 // If we hit an error and recovered by parsing up to a semicolon, eat the 4666 // semicolon and don't issue further diagnostics about missing brackets. 4667 if (Tok.is(tok::semi)) { 4668 ConsumeToken(); 4669 return; 4670 } 4671 4672 SourceLocation CloseLoc = Tok.getLocation(); 4673 if (ExpectAndConsume(tok::r_square)) 4674 SkipUntil(tok::r_square); 4675 else if (Tok.is(tok::r_square)) 4676 checkCompoundToken(CloseLoc, tok::r_square, CompoundToken::AttrEnd); 4677 if (EndLoc) 4678 *EndLoc = Tok.getLocation(); 4679 if (ExpectAndConsume(tok::r_square)) 4680 SkipUntil(tok::r_square); 4681 } 4682 4683 /// ParseCXX11Attributes - Parse a C++11 or C23 attribute-specifier-seq. 4684 /// 4685 /// attribute-specifier-seq: 4686 /// attribute-specifier-seq[opt] attribute-specifier 4687 void Parser::ParseCXX11Attributes(ParsedAttributes &Attrs) { 4688 SourceLocation StartLoc = Tok.getLocation(); 4689 SourceLocation EndLoc = StartLoc; 4690 4691 do { 4692 ParseCXX11AttributeSpecifier(Attrs, &EndLoc); 4693 } while (isAllowedCXX11AttributeSpecifier()); 4694 4695 Attrs.Range = SourceRange(StartLoc, EndLoc); 4696 } 4697 4698 void Parser::DiagnoseAndSkipCXX11Attributes() { 4699 auto Keyword = 4700 Tok.isRegularKeywordAttribute() ? Tok.getIdentifierInfo() : nullptr; 4701 // Start and end location of an attribute or an attribute list. 4702 SourceLocation StartLoc = Tok.getLocation(); 4703 SourceLocation EndLoc = SkipCXX11Attributes(); 4704 4705 if (EndLoc.isValid()) { 4706 SourceRange Range(StartLoc, EndLoc); 4707 (Keyword ? Diag(StartLoc, diag::err_keyword_not_allowed) << Keyword 4708 : Diag(StartLoc, diag::err_attributes_not_allowed)) 4709 << Range; 4710 } 4711 } 4712 4713 SourceLocation Parser::SkipCXX11Attributes() { 4714 SourceLocation EndLoc; 4715 4716 if (!isCXX11AttributeSpecifier()) 4717 return EndLoc; 4718 4719 do { 4720 if (Tok.is(tok::l_square)) { 4721 BalancedDelimiterTracker T(*this, tok::l_square); 4722 T.consumeOpen(); 4723 T.skipToEnd(); 4724 EndLoc = T.getCloseLocation(); 4725 } else if (Tok.isRegularKeywordAttribute() && 4726 !doesKeywordAttributeTakeArgs(Tok.getKind())) { 4727 EndLoc = Tok.getLocation(); 4728 ConsumeToken(); 4729 } else { 4730 assert((Tok.is(tok::kw_alignas) || Tok.isRegularKeywordAttribute()) && 4731 "not an attribute specifier"); 4732 ConsumeToken(); 4733 BalancedDelimiterTracker T(*this, tok::l_paren); 4734 if (!T.consumeOpen()) 4735 T.skipToEnd(); 4736 EndLoc = T.getCloseLocation(); 4737 } 4738 } while (isCXX11AttributeSpecifier()); 4739 4740 return EndLoc; 4741 } 4742 4743 /// Parse uuid() attribute when it appears in a [] Microsoft attribute. 4744 void Parser::ParseMicrosoftUuidAttributeArgs(ParsedAttributes &Attrs) { 4745 assert(Tok.is(tok::identifier) && "Not a Microsoft attribute list"); 4746 IdentifierInfo *UuidIdent = Tok.getIdentifierInfo(); 4747 assert(UuidIdent->getName() == "uuid" && "Not a Microsoft attribute list"); 4748 4749 SourceLocation UuidLoc = Tok.getLocation(); 4750 ConsumeToken(); 4751 4752 // Ignore the left paren location for now. 4753 BalancedDelimiterTracker T(*this, tok::l_paren); 4754 if (T.consumeOpen()) { 4755 Diag(Tok, diag::err_expected) << tok::l_paren; 4756 return; 4757 } 4758 4759 ArgsVector ArgExprs; 4760 if (isTokenStringLiteral()) { 4761 // Easy case: uuid("...") -- quoted string. 4762 ExprResult StringResult = ParseUnevaluatedStringLiteralExpression(); 4763 if (StringResult.isInvalid()) 4764 return; 4765 ArgExprs.push_back(StringResult.get()); 4766 } else { 4767 // something like uuid({000000A0-0000-0000-C000-000000000049}) -- no 4768 // quotes in the parens. Just append the spelling of all tokens encountered 4769 // until the closing paren. 4770 4771 SmallString<42> StrBuffer; // 2 "", 36 bytes UUID, 2 optional {}, 1 nul 4772 StrBuffer += "\""; 4773 4774 // Since none of C++'s keywords match [a-f]+, accepting just tok::l_brace, 4775 // tok::r_brace, tok::minus, tok::identifier (think C000) and 4776 // tok::numeric_constant (0000) should be enough. But the spelling of the 4777 // uuid argument is checked later anyways, so there's no harm in accepting 4778 // almost anything here. 4779 // cl is very strict about whitespace in this form and errors out if any 4780 // is present, so check the space flags on the tokens. 4781 SourceLocation StartLoc = Tok.getLocation(); 4782 while (Tok.isNot(tok::r_paren)) { 4783 if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) { 4784 Diag(Tok, diag::err_attribute_uuid_malformed_guid); 4785 SkipUntil(tok::r_paren, StopAtSemi); 4786 return; 4787 } 4788 SmallString<16> SpellingBuffer; 4789 SpellingBuffer.resize(Tok.getLength() + 1); 4790 bool Invalid = false; 4791 StringRef TokSpelling = PP.getSpelling(Tok, SpellingBuffer, &Invalid); 4792 if (Invalid) { 4793 SkipUntil(tok::r_paren, StopAtSemi); 4794 return; 4795 } 4796 StrBuffer += TokSpelling; 4797 ConsumeAnyToken(); 4798 } 4799 StrBuffer += "\""; 4800 4801 if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) { 4802 Diag(Tok, diag::err_attribute_uuid_malformed_guid); 4803 ConsumeParen(); 4804 return; 4805 } 4806 4807 // Pretend the user wrote the appropriate string literal here. 4808 // ActOnStringLiteral() copies the string data into the literal, so it's 4809 // ok that the Token points to StrBuffer. 4810 Token Toks[1]; 4811 Toks[0].startToken(); 4812 Toks[0].setKind(tok::string_literal); 4813 Toks[0].setLocation(StartLoc); 4814 Toks[0].setLiteralData(StrBuffer.data()); 4815 Toks[0].setLength(StrBuffer.size()); 4816 StringLiteral *UuidString = 4817 cast<StringLiteral>(Actions.ActOnUnevaluatedStringLiteral(Toks).get()); 4818 ArgExprs.push_back(UuidString); 4819 } 4820 4821 if (!T.consumeClose()) { 4822 Attrs.addNew(UuidIdent, SourceRange(UuidLoc, T.getCloseLocation()), nullptr, 4823 SourceLocation(), ArgExprs.data(), ArgExprs.size(), 4824 ParsedAttr::Form::Microsoft()); 4825 } 4826 } 4827 4828 /// ParseMicrosoftAttributes - Parse Microsoft attributes [Attr] 4829 /// 4830 /// [MS] ms-attribute: 4831 /// '[' token-seq ']' 4832 /// 4833 /// [MS] ms-attribute-seq: 4834 /// ms-attribute[opt] 4835 /// ms-attribute ms-attribute-seq 4836 void Parser::ParseMicrosoftAttributes(ParsedAttributes &Attrs) { 4837 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list"); 4838 4839 SourceLocation StartLoc = Tok.getLocation(); 4840 SourceLocation EndLoc = StartLoc; 4841 do { 4842 // FIXME: If this is actually a C++11 attribute, parse it as one. 4843 BalancedDelimiterTracker T(*this, tok::l_square); 4844 T.consumeOpen(); 4845 4846 // Skip most ms attributes except for a specific list. 4847 while (true) { 4848 SkipUntil(tok::r_square, tok::identifier, 4849 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); 4850 if (Tok.is(tok::code_completion)) { 4851 cutOffParsing(); 4852 Actions.CodeCompleteAttribute(AttributeCommonInfo::AS_Microsoft, 4853 Sema::AttributeCompletion::Attribute, 4854 /*Scope=*/nullptr); 4855 break; 4856 } 4857 if (Tok.isNot(tok::identifier)) // ']', but also eof 4858 break; 4859 if (Tok.getIdentifierInfo()->getName() == "uuid") 4860 ParseMicrosoftUuidAttributeArgs(Attrs); 4861 else { 4862 IdentifierInfo *II = Tok.getIdentifierInfo(); 4863 SourceLocation NameLoc = Tok.getLocation(); 4864 ConsumeToken(); 4865 ParsedAttr::Kind AttrKind = 4866 ParsedAttr::getParsedKind(II, nullptr, ParsedAttr::AS_Microsoft); 4867 // For HLSL we want to handle all attributes, but for MSVC compat, we 4868 // silently ignore unknown Microsoft attributes. 4869 if (getLangOpts().HLSL || AttrKind != ParsedAttr::UnknownAttribute) { 4870 bool AttrParsed = false; 4871 if (Tok.is(tok::l_paren)) { 4872 CachedTokens OpenMPTokens; 4873 AttrParsed = 4874 ParseCXX11AttributeArgs(II, NameLoc, Attrs, &EndLoc, nullptr, 4875 SourceLocation(), OpenMPTokens); 4876 ReplayOpenMPAttributeTokens(OpenMPTokens); 4877 } 4878 if (!AttrParsed) { 4879 Attrs.addNew(II, NameLoc, nullptr, SourceLocation(), nullptr, 0, 4880 ParsedAttr::Form::Microsoft()); 4881 } 4882 } 4883 } 4884 } 4885 4886 T.consumeClose(); 4887 EndLoc = T.getCloseLocation(); 4888 } while (Tok.is(tok::l_square)); 4889 4890 Attrs.Range = SourceRange(StartLoc, EndLoc); 4891 } 4892 4893 void Parser::ParseMicrosoftIfExistsClassDeclaration( 4894 DeclSpec::TST TagType, ParsedAttributes &AccessAttrs, 4895 AccessSpecifier &CurAS) { 4896 IfExistsCondition Result; 4897 if (ParseMicrosoftIfExistsCondition(Result)) 4898 return; 4899 4900 BalancedDelimiterTracker Braces(*this, tok::l_brace); 4901 if (Braces.consumeOpen()) { 4902 Diag(Tok, diag::err_expected) << tok::l_brace; 4903 return; 4904 } 4905 4906 switch (Result.Behavior) { 4907 case IEB_Parse: 4908 // Parse the declarations below. 4909 break; 4910 4911 case IEB_Dependent: 4912 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists) 4913 << Result.IsIfExists; 4914 // Fall through to skip. 4915 [[fallthrough]]; 4916 4917 case IEB_Skip: 4918 Braces.skipToEnd(); 4919 return; 4920 } 4921 4922 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) { 4923 // __if_exists, __if_not_exists can nest. 4924 if (Tok.isOneOf(tok::kw___if_exists, tok::kw___if_not_exists)) { 4925 ParseMicrosoftIfExistsClassDeclaration(TagType, AccessAttrs, CurAS); 4926 continue; 4927 } 4928 4929 // Check for extraneous top-level semicolon. 4930 if (Tok.is(tok::semi)) { 4931 ConsumeExtraSemi(InsideStruct, TagType); 4932 continue; 4933 } 4934 4935 AccessSpecifier AS = getAccessSpecifierIfPresent(); 4936 if (AS != AS_none) { 4937 // Current token is a C++ access specifier. 4938 CurAS = AS; 4939 SourceLocation ASLoc = Tok.getLocation(); 4940 ConsumeToken(); 4941 if (Tok.is(tok::colon)) 4942 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation(), 4943 ParsedAttributesView{}); 4944 else 4945 Diag(Tok, diag::err_expected) << tok::colon; 4946 ConsumeToken(); 4947 continue; 4948 } 4949 4950 // Parse all the comma separated declarators. 4951 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs); 4952 } 4953 4954 Braces.consumeClose(); 4955 } 4956