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