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