2008-06-25 06:12:16 +08:00
|
|
|
//===--- ParseCXXInlineMethods.cpp - C++ class inline methods parsing------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2008-06-25 06:12:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements parsing for C++ class inline methods.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Parse/Parser.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
|
|
|
#include "clang/Parse/ParseDiagnostic.h"
|
2017-03-23 23:11:07 +08:00
|
|
|
#include "clang/Parse/RAIIObjectsForParser.h"
|
2010-08-21 02:27:03 +08:00
|
|
|
#include "clang/Sema/DeclSpec.h"
|
|
|
|
#include "clang/Sema/Scope.h"
|
2008-06-25 06:12:16 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2009-04-27 04:35:05 +08:00
|
|
|
/// ParseCXXInlineMethodDef - We parsed and verified that the specified
|
2008-06-25 06:12:16 +08:00
|
|
|
/// Declarator is a well formed C++ inline method definition. Now lex its body
|
|
|
|
/// and store its tokens for parsing after the C++ class is complete.
|
2018-07-13 05:09:05 +08:00
|
|
|
NamedDecl *Parser::ParseCXXInlineMethodDef(
|
|
|
|
AccessSpecifier AS, ParsedAttributes &AccessAttrs, ParsingDeclarator &D,
|
|
|
|
const ParsedTemplateInfo &TemplateInfo, const VirtSpecifiers &VS,
|
|
|
|
SourceLocation PureSpecLoc) {
|
2010-12-11 00:29:40 +08:00
|
|
|
assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
|
2015-06-18 18:59:26 +08:00
|
|
|
assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try, tok::equal) &&
|
2011-05-12 14:15:49 +08:00
|
|
|
"Current token not a '{', ':', '=', or 'try'!");
|
2008-06-25 06:12:16 +08:00
|
|
|
|
2012-08-24 07:38:35 +08:00
|
|
|
MultiTemplateParamsArg TemplateParams(
|
2014-05-21 14:02:52 +08:00
|
|
|
TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data()
|
|
|
|
: nullptr,
|
2014-05-11 01:43:15 +08:00
|
|
|
TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
|
2010-04-15 07:07:37 +08:00
|
|
|
|
2013-01-09 05:00:12 +08:00
|
|
|
NamedDecl *FnD;
|
2009-08-06 10:15:43 +08:00
|
|
|
if (D.getDeclSpec().isFriendSpecified())
|
2011-10-11 08:28:45 +08:00
|
|
|
FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
|
2012-08-24 05:35:17 +08:00
|
|
|
TemplateParams);
|
2011-10-10 22:49:18 +08:00
|
|
|
else {
|
2010-07-03 01:43:08 +08:00
|
|
|
FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
|
2014-05-21 14:02:52 +08:00
|
|
|
TemplateParams, nullptr,
|
2012-06-10 11:12:00 +08:00
|
|
|
VS, ICIS_NoInit);
|
2011-10-10 22:49:18 +08:00
|
|
|
if (FnD) {
|
2013-08-29 08:47:48 +08:00
|
|
|
Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs);
|
2015-06-30 09:28:56 +08:00
|
|
|
if (PureSpecLoc.isValid())
|
|
|
|
Actions.ActOnPureSpecifier(FnD, PureSpecLoc);
|
2011-10-10 22:49:18 +08:00
|
|
|
}
|
2011-01-28 14:07:34 +08:00
|
|
|
}
|
2008-06-25 06:12:16 +08:00
|
|
|
|
2016-02-19 09:15:08 +08:00
|
|
|
if (FnD)
|
|
|
|
HandleMemberFunctionDeclDelays(D, FnD);
|
2009-07-23 05:45:50 +08:00
|
|
|
|
2011-02-14 15:13:47 +08:00
|
|
|
D.complete(FnD);
|
|
|
|
|
2013-12-17 22:12:37 +08:00
|
|
|
if (TryConsumeToken(tok::equal)) {
|
2011-11-10 17:08:44 +08:00
|
|
|
if (!FnD) {
|
|
|
|
SkipUntil(tok::semi);
|
2014-05-21 14:02:52 +08:00
|
|
|
return nullptr;
|
2011-11-10 17:08:44 +08:00
|
|
|
}
|
|
|
|
|
2011-05-12 14:15:49 +08:00
|
|
|
bool Delete = false;
|
|
|
|
SourceLocation KWLoc;
|
2015-03-24 05:43:28 +08:00
|
|
|
SourceLocation KWEndLoc = Tok.getEndLoc().getLocWithOffset(-1);
|
2013-12-17 22:12:37 +08:00
|
|
|
if (TryConsumeToken(tok::kw_delete, KWLoc)) {
|
|
|
|
Diag(KWLoc, getLangOpts().CPlusPlus11
|
2015-11-15 02:16:08 +08:00
|
|
|
? diag::warn_cxx98_compat_defaulted_deleted_function
|
|
|
|
: diag::ext_defaulted_deleted_function)
|
|
|
|
<< 1 /* deleted */;
|
2011-05-12 14:15:49 +08:00
|
|
|
Actions.SetDeclDeleted(FnD, KWLoc);
|
|
|
|
Delete = true;
|
2015-03-24 05:43:28 +08:00
|
|
|
if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
|
|
|
|
DeclAsFunction->setRangeEnd(KWEndLoc);
|
|
|
|
}
|
2013-12-17 22:12:37 +08:00
|
|
|
} else if (TryConsumeToken(tok::kw_default, KWLoc)) {
|
|
|
|
Diag(KWLoc, getLangOpts().CPlusPlus11
|
2015-11-15 02:16:08 +08:00
|
|
|
? diag::warn_cxx98_compat_defaulted_deleted_function
|
|
|
|
: diag::ext_defaulted_deleted_function)
|
|
|
|
<< 0 /* defaulted */;
|
2011-05-12 14:15:49 +08:00
|
|
|
Actions.SetDeclDefaulted(FnD, KWLoc);
|
2015-03-24 05:43:28 +08:00
|
|
|
if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
|
|
|
|
DeclAsFunction->setRangeEnd(KWEndLoc);
|
|
|
|
}
|
2011-05-12 14:15:49 +08:00
|
|
|
} else {
|
|
|
|
llvm_unreachable("function definition after = not 'delete' or 'default'");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.is(tok::comma)) {
|
|
|
|
Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
|
|
|
|
<< Delete;
|
|
|
|
SkipUntil(tok::semi);
|
2014-01-01 11:08:43 +08:00
|
|
|
} else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
|
|
|
|
Delete ? "delete" : "default")) {
|
|
|
|
SkipUntil(tok::semi);
|
2011-05-12 14:15:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return FnD;
|
|
|
|
}
|
2015-03-24 07:49:41 +08:00
|
|
|
|
2016-06-17 05:40:06 +08:00
|
|
|
if (SkipFunctionBodies && (!FnD || Actions.canSkipFunctionBody(FnD)) &&
|
|
|
|
trySkippingFunctionBody()) {
|
|
|
|
Actions.ActOnSkippedFunctionBody(FnD);
|
|
|
|
return FnD;
|
|
|
|
}
|
|
|
|
|
2011-04-23 06:18:13 +08:00
|
|
|
// In delayed template parsing mode, if we are within a class template
|
|
|
|
// or if we are about to parse function member template then consume
|
|
|
|
// the tokens and store them for parsing at the end of the translation unit.
|
2013-09-14 13:46:42 +08:00
|
|
|
if (getLangOpts().DelayedTemplateParsing &&
|
2015-03-24 07:49:41 +08:00
|
|
|
D.getFunctionDefinitionKind() == FDK_Definition &&
|
2014-01-22 15:29:52 +08:00
|
|
|
!D.getDeclSpec().isConstexprSpecified() &&
|
|
|
|
!(FnD && FnD->getAsFunction() &&
|
2014-01-26 00:55:45 +08:00
|
|
|
FnD->getAsFunction()->getReturnType()->getContainedAutoType()) &&
|
2011-04-23 06:18:13 +08:00
|
|
|
((Actions.CurContext->isDependentContext() ||
|
2013-09-14 13:46:42 +08:00
|
|
|
(TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
|
|
|
|
TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) &&
|
|
|
|
!Actions.IsInsideALocalClassWithinATemplateFunction())) {
|
2011-04-23 06:18:13 +08:00
|
|
|
|
2013-08-08 05:41:30 +08:00
|
|
|
CachedTokens Toks;
|
|
|
|
LexTemplateFunctionForLateParsing(Toks);
|
2011-04-23 06:18:13 +08:00
|
|
|
|
2013-08-08 05:41:30 +08:00
|
|
|
if (FnD) {
|
2014-01-22 15:29:52 +08:00
|
|
|
FunctionDecl *FD = FnD->getAsFunction();
|
2011-04-25 15:09:43 +08:00
|
|
|
Actions.CheckForFunctionRedefinition(FD);
|
2013-08-08 05:41:30 +08:00
|
|
|
Actions.MarkAsLateParsedTemplate(FD, FnD, Toks);
|
2011-04-23 06:18:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return FnD;
|
|
|
|
}
|
|
|
|
|
2008-06-25 06:12:16 +08:00
|
|
|
// Consume the tokens and store them for later parsing.
|
|
|
|
|
2010-10-13 00:25:54 +08:00
|
|
|
LexedMethod* LM = new LexedMethod(this, FnD);
|
|
|
|
getCurrentClass().LateParsedDeclarations.push_back(LM);
|
|
|
|
LM->TemplateScope = getCurScope()->isTemplateParamScope();
|
|
|
|
CachedTokens &Toks = LM->Toks;
|
2008-06-25 06:12:16 +08:00
|
|
|
|
2009-04-27 04:35:05 +08:00
|
|
|
tok::TokenKind kind = Tok.getKind();
|
2011-09-30 16:32:17 +08:00
|
|
|
// Consume everything up to (and including) the left brace of the
|
|
|
|
// function body.
|
|
|
|
if (ConsumeAndStoreFunctionPrologue(Toks)) {
|
|
|
|
// We didn't find the left-brace we expected after the
|
2012-02-22 12:49:04 +08:00
|
|
|
// constructor initializer; we already printed an error, and it's likely
|
|
|
|
// impossible to recover, so don't try to parse this method later.
|
2013-07-04 08:13:48 +08:00
|
|
|
// Skip over the rest of the decl and back to somewhere that looks
|
|
|
|
// reasonable.
|
|
|
|
SkipMalformedDecl();
|
2012-02-22 12:49:04 +08:00
|
|
|
delete getCurrentClass().LateParsedDeclarations.back();
|
|
|
|
getCurrentClass().LateParsedDeclarations.pop_back();
|
|
|
|
return FnD;
|
2008-11-05 12:29:56 +08:00
|
|
|
} else {
|
2011-09-30 16:32:17 +08:00
|
|
|
// Consume everything up to (and including) the matching right brace.
|
|
|
|
ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
|
2008-11-05 12:29:56 +08:00
|
|
|
}
|
2008-06-25 06:12:16 +08:00
|
|
|
|
2009-04-27 04:35:05 +08:00
|
|
|
// If we're in a function-try-block, we need to store all the catch blocks.
|
|
|
|
if (kind == tok::kw_try) {
|
|
|
|
while (Tok.is(tok::kw_catch)) {
|
2010-04-24 05:20:12 +08:00
|
|
|
ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
|
|
|
|
ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
|
2009-04-27 04:35:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 15:37:13 +08:00
|
|
|
if (FnD) {
|
2017-08-12 09:46:03 +08:00
|
|
|
FunctionDecl *FD = FnD->getAsFunction();
|
|
|
|
// Track that this function will eventually have a body; Sema needs
|
|
|
|
// to know this.
|
|
|
|
Actions.CheckForFunctionRedefinition(FD);
|
|
|
|
FD->setWillHaveBody(true);
|
2013-06-23 15:37:13 +08:00
|
|
|
} else {
|
2011-04-15 07:19:27 +08:00
|
|
|
// If semantic analysis could not build a function declaration,
|
|
|
|
// just throw away the late-parsed declaration.
|
|
|
|
delete getCurrentClass().LateParsedDeclarations.back();
|
|
|
|
getCurrentClass().LateParsedDeclarations.pop_back();
|
|
|
|
}
|
|
|
|
|
2008-06-25 06:12:16 +08:00
|
|
|
return FnD;
|
|
|
|
}
|
|
|
|
|
2011-06-12 01:19:42 +08:00
|
|
|
/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
|
|
|
|
/// specified Declarator is a well formed C++ non-static data member
|
|
|
|
/// declaration. Now lex its initializer and store its tokens for parsing
|
|
|
|
/// after the class is complete.
|
|
|
|
void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
|
2015-06-18 18:59:26 +08:00
|
|
|
assert(Tok.isOneOf(tok::l_brace, tok::equal) &&
|
2011-06-12 01:19:42 +08:00
|
|
|
"Current token not a '{' or '='!");
|
|
|
|
|
|
|
|
LateParsedMemberInitializer *MI =
|
|
|
|
new LateParsedMemberInitializer(this, VarD);
|
|
|
|
getCurrentClass().LateParsedDeclarations.push_back(MI);
|
|
|
|
CachedTokens &Toks = MI->Toks;
|
|
|
|
|
|
|
|
tok::TokenKind kind = Tok.getKind();
|
|
|
|
if (kind == tok::equal) {
|
|
|
|
Toks.push_back(Tok);
|
2012-03-08 09:00:17 +08:00
|
|
|
ConsumeToken();
|
2011-06-12 01:19:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (kind == tok::l_brace) {
|
|
|
|
// Begin by storing the '{' token.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeBrace();
|
|
|
|
|
|
|
|
// Consume everything up to (and including) the matching right brace.
|
|
|
|
ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
|
|
|
|
} else {
|
|
|
|
// Consume everything up to (but excluding) the comma or semicolon.
|
2013-09-13 07:28:08 +08:00
|
|
|
ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer);
|
2011-06-12 01:19:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store an artificial EOF token to ensure that we don't run off the end of
|
|
|
|
// the initializer when we come to parse it.
|
|
|
|
Token Eof;
|
|
|
|
Eof.startToken();
|
|
|
|
Eof.setKind(tok::eof);
|
|
|
|
Eof.setLocation(Tok.getLocation());
|
2014-12-19 10:13:56 +08:00
|
|
|
Eof.setEofData(VarD);
|
2011-06-12 01:19:42 +08:00
|
|
|
Toks.push_back(Eof);
|
|
|
|
}
|
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
|
2010-10-13 00:25:54 +08:00
|
|
|
void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
|
2011-06-12 01:19:42 +08:00
|
|
|
void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
|
2010-10-13 00:25:54 +08:00
|
|
|
void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
|
|
|
|
|
|
|
|
Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
|
|
|
|
: Self(P), Class(C) {}
|
|
|
|
|
|
|
|
Parser::LateParsedClass::~LateParsedClass() {
|
|
|
|
Self->DeallocateParsedClasses(Class);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
|
|
|
|
Self->ParseLexedMethodDeclarations(*Class);
|
|
|
|
}
|
|
|
|
|
2011-06-12 01:19:42 +08:00
|
|
|
void Parser::LateParsedClass::ParseLexedMemberInitializers() {
|
|
|
|
Self->ParseLexedMemberInitializers(*Class);
|
|
|
|
}
|
|
|
|
|
2010-10-13 00:25:54 +08:00
|
|
|
void Parser::LateParsedClass::ParseLexedMethodDefs() {
|
|
|
|
Self->ParseLexedMethodDefs(*Class);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
|
|
|
|
Self->ParseLexedMethodDeclaration(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::LexedMethod::ParseLexedMethodDefs() {
|
|
|
|
Self->ParseLexedMethodDef(*this);
|
|
|
|
}
|
|
|
|
|
2011-06-12 01:19:42 +08:00
|
|
|
void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
|
|
|
|
Self->ParseLexedMemberInitializer(*this);
|
|
|
|
}
|
|
|
|
|
2008-12-17 05:30:33 +08:00
|
|
|
/// ParseLexedMethodDeclarations - We finished parsing the member
|
|
|
|
/// specification of a top (non-nested) C++ class. Now go over the
|
|
|
|
/// stack of method declarations with some parts for which parsing was
|
|
|
|
/// delayed (such as default arguments) and parse them.
|
2009-05-28 07:11:45 +08:00
|
|
|
void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
|
|
|
|
bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
|
2014-05-11 01:43:15 +08:00
|
|
|
ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
|
|
|
|
HasTemplateScope);
|
2013-04-29 19:55:38 +08:00
|
|
|
TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
|
|
|
|
if (HasTemplateScope) {
|
2010-07-03 01:43:08 +08:00
|
|
|
Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
|
2013-04-29 19:55:38 +08:00
|
|
|
++CurTemplateDepthTracker;
|
|
|
|
}
|
2009-05-28 07:11:45 +08:00
|
|
|
|
2009-12-19 18:49:29 +08:00
|
|
|
// The current scope is still active if we're the top-level class.
|
|
|
|
// Otherwise we'll need to push and enter a new scope.
|
2009-05-28 07:11:45 +08:00
|
|
|
bool HasClassScope = !Class.TopLevelClass;
|
2010-04-15 07:07:37 +08:00
|
|
|
ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
|
|
|
|
HasClassScope);
|
2009-12-19 18:49:29 +08:00
|
|
|
if (HasClassScope)
|
2014-05-11 01:43:15 +08:00
|
|
|
Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
|
|
|
|
Class.TagOrTemplate);
|
2009-05-28 07:11:45 +08:00
|
|
|
|
2010-10-13 00:25:54 +08:00
|
|
|
for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
|
|
|
|
Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
|
|
|
|
}
|
2008-12-17 05:30:33 +08:00
|
|
|
|
2010-10-13 00:25:54 +08:00
|
|
|
if (HasClassScope)
|
2014-05-11 01:43:15 +08:00
|
|
|
Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
|
|
|
|
Class.TagOrTemplate);
|
2010-10-13 00:25:54 +08:00
|
|
|
}
|
2010-03-31 06:14:32 +08:00
|
|
|
|
2010-10-13 00:25:54 +08:00
|
|
|
void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
|
|
|
|
// If this is a member template, introduce the template parameter scope.
|
|
|
|
ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
|
2013-04-29 19:55:38 +08:00
|
|
|
TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
|
|
|
|
if (LM.TemplateScope) {
|
2010-10-13 00:25:54 +08:00
|
|
|
Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
|
2013-04-29 19:55:38 +08:00
|
|
|
++CurTemplateDepthTracker;
|
|
|
|
}
|
2010-10-13 00:25:54 +08:00
|
|
|
// Start the delayed C++ method declaration
|
|
|
|
Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
|
|
|
|
|
|
|
|
// Introduce the parameters into scope and parse their default
|
|
|
|
// arguments.
|
2013-01-29 06:42:45 +08:00
|
|
|
ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
|
|
|
|
Scope::FunctionDeclarationScope | Scope::DeclScope);
|
2010-10-13 00:25:54 +08:00
|
|
|
for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
|
2015-02-19 22:03:22 +08:00
|
|
|
auto Param = cast<ParmVarDecl>(LM.DefaultArgs[I].Param);
|
2010-10-13 00:25:54 +08:00
|
|
|
// Introduce the parameter into scope.
|
2015-02-19 22:03:22 +08:00
|
|
|
bool HasUnparsed = Param->hasUnparsedDefaultArg();
|
2015-01-30 22:21:35 +08:00
|
|
|
Actions.ActOnDelayedCXXMethodParameter(getCurScope(), Param);
|
2016-11-18 01:52:58 +08:00
|
|
|
std::unique_ptr<CachedTokens> Toks = std::move(LM.DefaultArgs[I].Toks);
|
|
|
|
if (Toks) {
|
2018-06-27 07:20:26 +08:00
|
|
|
ParenBraceBracketBalancer BalancerRAIIObj(*this);
|
|
|
|
|
2015-01-13 12:20:57 +08:00
|
|
|
// Mark the end of the default argument so that we know when to stop when
|
|
|
|
// we parse it later on.
|
|
|
|
Token LastDefaultArgToken = Toks->back();
|
|
|
|
Token DefArgEnd;
|
|
|
|
DefArgEnd.startToken();
|
|
|
|
DefArgEnd.setKind(tok::eof);
|
2015-03-19 08:10:23 +08:00
|
|
|
DefArgEnd.setLocation(LastDefaultArgToken.getEndLoc());
|
2015-01-30 22:21:35 +08:00
|
|
|
DefArgEnd.setEofData(Param);
|
2015-01-13 12:20:57 +08:00
|
|
|
Toks->push_back(DefArgEnd);
|
2010-10-13 00:25:54 +08:00
|
|
|
|
|
|
|
// Parse the default argument from its saved token stream.
|
|
|
|
Toks->push_back(Tok); // So that the current token doesn't get lost
|
2016-02-10 02:52:09 +08:00
|
|
|
PP.EnterTokenStream(*Toks, true);
|
2010-10-13 00:25:54 +08:00
|
|
|
|
|
|
|
// Consume the previously-pushed token.
|
|
|
|
ConsumeAnyToken();
|
|
|
|
|
|
|
|
// Consume the '='.
|
|
|
|
assert(Tok.is(tok::equal) && "Default argument not starting with '='");
|
|
|
|
SourceLocation EqualLoc = ConsumeToken();
|
|
|
|
|
|
|
|
// The argument isn't actually potentially evaluated unless it is
|
|
|
|
// used.
|
2017-04-02 05:30:49 +08:00
|
|
|
EnterExpressionEvaluationContext Eval(
|
|
|
|
Actions,
|
|
|
|
Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed, Param);
|
2010-10-13 00:25:54 +08:00
|
|
|
|
2012-03-14 23:54:00 +08:00
|
|
|
ExprResult DefArgResult;
|
2013-01-02 19:42:31 +08:00
|
|
|
if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
|
2012-03-21 05:24:03 +08:00
|
|
|
Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
|
2012-03-14 23:54:00 +08:00
|
|
|
DefArgResult = ParseBraceInitializer();
|
2012-03-21 05:24:03 +08:00
|
|
|
} else
|
2012-03-14 23:54:00 +08:00
|
|
|
DefArgResult = ParseAssignmentExpression();
|
2014-11-21 06:06:40 +08:00
|
|
|
DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
|
2015-01-12 14:51:15 +08:00
|
|
|
if (DefArgResult.isInvalid()) {
|
2015-01-30 22:21:35 +08:00
|
|
|
Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
|
2015-01-12 14:51:15 +08:00
|
|
|
} else {
|
2015-01-30 22:21:35 +08:00
|
|
|
if (Tok.isNot(tok::eof) || Tok.getEofData() != Param) {
|
2013-09-13 07:28:08 +08:00
|
|
|
// The last two tokens are the terminator and the saved value of
|
|
|
|
// Tok; the last token in the default argument is the one before
|
|
|
|
// those.
|
|
|
|
assert(Toks->size() >= 3 && "expected a token in default arg");
|
|
|
|
Diag(Tok.getLocation(), diag::err_default_arg_unparsed)
|
|
|
|
<< SourceRange(Tok.getLocation(),
|
|
|
|
(*Toks)[Toks->size() - 3].getLocation());
|
|
|
|
}
|
2015-01-30 22:21:35 +08:00
|
|
|
Actions.ActOnParamDefaultArgument(Param, EqualLoc,
|
2014-05-29 18:55:11 +08:00
|
|
|
DefArgResult.get());
|
2010-10-13 00:25:54 +08:00
|
|
|
}
|
2008-12-17 05:30:33 +08:00
|
|
|
|
2010-10-13 00:25:54 +08:00
|
|
|
// There could be leftover tokens (e.g. because of an error).
|
2015-01-13 12:20:57 +08:00
|
|
|
// Skip through until we reach the 'end of default argument' token.
|
|
|
|
while (Tok.isNot(tok::eof))
|
|
|
|
ConsumeAnyToken();
|
|
|
|
|
2015-01-30 22:21:35 +08:00
|
|
|
if (Tok.is(tok::eof) && Tok.getEofData() == Param)
|
2008-12-17 05:30:33 +08:00
|
|
|
ConsumeAnyToken();
|
2015-02-19 22:03:22 +08:00
|
|
|
} else if (HasUnparsed) {
|
|
|
|
assert(Param->hasInheritedDefaultArg());
|
|
|
|
FunctionDecl *Old = cast<FunctionDecl>(LM.Method)->getPreviousDecl();
|
|
|
|
ParmVarDecl *OldParam = Old->getParamDecl(I);
|
|
|
|
assert (!OldParam->hasUnparsedDefaultArg());
|
|
|
|
if (OldParam->hasUninstantiatedDefaultArg())
|
|
|
|
Param->setUninstantiatedDefaultArg(
|
2016-06-11 04:21:15 +08:00
|
|
|
OldParam->getUninstantiatedDefaultArg());
|
2015-02-19 22:03:22 +08:00
|
|
|
else
|
|
|
|
Param->setDefaultArg(OldParam->getInit());
|
2008-12-17 05:30:33 +08:00
|
|
|
}
|
|
|
|
}
|
2012-05-03 06:22:32 +08:00
|
|
|
|
2014-11-14 04:01:57 +08:00
|
|
|
// Parse a delayed exception-specification, if there is one.
|
|
|
|
if (CachedTokens *Toks = LM.ExceptionSpecTokens) {
|
2018-06-27 07:20:26 +08:00
|
|
|
ParenBraceBracketBalancer BalancerRAIIObj(*this);
|
|
|
|
|
2015-01-13 12:20:57 +08:00
|
|
|
// Add the 'stop' token.
|
|
|
|
Token LastExceptionSpecToken = Toks->back();
|
|
|
|
Token ExceptionSpecEnd;
|
|
|
|
ExceptionSpecEnd.startToken();
|
|
|
|
ExceptionSpecEnd.setKind(tok::eof);
|
2015-03-19 08:10:23 +08:00
|
|
|
ExceptionSpecEnd.setLocation(LastExceptionSpecToken.getEndLoc());
|
2015-01-13 12:20:57 +08:00
|
|
|
ExceptionSpecEnd.setEofData(LM.Method);
|
|
|
|
Toks->push_back(ExceptionSpecEnd);
|
2014-11-14 04:01:57 +08:00
|
|
|
|
|
|
|
// Parse the default argument from its saved token stream.
|
|
|
|
Toks->push_back(Tok); // So that the current token doesn't get lost
|
2016-02-10 02:52:09 +08:00
|
|
|
PP.EnterTokenStream(*Toks, true);
|
2014-11-14 04:01:57 +08:00
|
|
|
|
|
|
|
// Consume the previously-pushed token.
|
|
|
|
ConsumeAnyToken();
|
|
|
|
|
|
|
|
// C++11 [expr.prim.general]p3:
|
|
|
|
// If a declaration declares a member function or member function
|
|
|
|
// template of a class X, the expression this is a prvalue of type
|
|
|
|
// "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
|
|
|
|
// and the end of the function-definition, member-declarator, or
|
|
|
|
// declarator.
|
|
|
|
CXXMethodDecl *Method;
|
|
|
|
if (FunctionTemplateDecl *FunTmpl
|
|
|
|
= dyn_cast<FunctionTemplateDecl>(LM.Method))
|
|
|
|
Method = cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
|
|
|
|
else
|
|
|
|
Method = cast<CXXMethodDecl>(LM.Method);
|
|
|
|
|
|
|
|
Sema::CXXThisScopeRAII ThisScope(Actions, Method->getParent(),
|
2019-01-28 19:37:49 +08:00
|
|
|
Method->getMethodQualifiers(),
|
2014-11-14 04:01:57 +08:00
|
|
|
getLangOpts().CPlusPlus11);
|
|
|
|
|
|
|
|
// Parse the exception-specification.
|
|
|
|
SourceRange SpecificationRange;
|
|
|
|
SmallVector<ParsedType, 4> DynamicExceptions;
|
|
|
|
SmallVector<SourceRange, 4> DynamicExceptionRanges;
|
|
|
|
ExprResult NoexceptExpr;
|
|
|
|
CachedTokens *ExceptionSpecTokens;
|
|
|
|
|
|
|
|
ExceptionSpecificationType EST
|
|
|
|
= tryParseExceptionSpecification(/*Delayed=*/false, SpecificationRange,
|
|
|
|
DynamicExceptions,
|
|
|
|
DynamicExceptionRanges, NoexceptExpr,
|
|
|
|
ExceptionSpecTokens);
|
|
|
|
|
2015-01-13 12:20:57 +08:00
|
|
|
if (Tok.isNot(tok::eof) || Tok.getEofData() != LM.Method)
|
2014-11-14 04:01:57 +08:00
|
|
|
Diag(Tok.getLocation(), diag::err_except_spec_unparsed);
|
|
|
|
|
|
|
|
// Attach the exception-specification to the method.
|
2014-11-21 06:32:11 +08:00
|
|
|
Actions.actOnDelayedExceptionSpecification(LM.Method, EST,
|
|
|
|
SpecificationRange,
|
|
|
|
DynamicExceptions,
|
|
|
|
DynamicExceptionRanges,
|
|
|
|
NoexceptExpr.isUsable()?
|
|
|
|
NoexceptExpr.get() : nullptr);
|
2014-11-14 04:01:57 +08:00
|
|
|
|
|
|
|
// There could be leftover tokens (e.g. because of an error).
|
|
|
|
// Skip through until we reach the original token position.
|
2015-01-13 12:20:57 +08:00
|
|
|
while (Tok.isNot(tok::eof))
|
|
|
|
ConsumeAnyToken();
|
|
|
|
|
|
|
|
// Clean up the remaining EOF token.
|
|
|
|
if (Tok.is(tok::eof) && Tok.getEofData() == LM.Method)
|
2014-11-14 04:01:57 +08:00
|
|
|
ConsumeAnyToken();
|
|
|
|
|
|
|
|
delete Toks;
|
|
|
|
LM.ExceptionSpecTokens = nullptr;
|
|
|
|
}
|
|
|
|
|
2010-10-13 00:25:54 +08:00
|
|
|
PrototypeScope.Exit();
|
2009-05-28 07:11:45 +08:00
|
|
|
|
2010-10-13 00:25:54 +08:00
|
|
|
// Finish the delayed C++ method declaration.
|
|
|
|
Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
|
2008-12-17 05:30:33 +08:00
|
|
|
}
|
|
|
|
|
2008-06-25 06:12:16 +08:00
|
|
|
/// ParseLexedMethodDefs - We finished parsing the member specification of a top
|
|
|
|
/// (non-nested) C++ class. Now go over the stack of lexed methods that were
|
|
|
|
/// collected during its parsing and parse them all.
|
2009-05-28 07:11:45 +08:00
|
|
|
void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
|
|
|
|
bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
|
2010-10-13 00:25:54 +08:00
|
|
|
ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
|
2013-04-29 19:55:38 +08:00
|
|
|
TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
|
|
|
|
if (HasTemplateScope) {
|
2010-07-03 01:43:08 +08:00
|
|
|
Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
|
2013-04-29 19:55:38 +08:00
|
|
|
++CurTemplateDepthTracker;
|
|
|
|
}
|
2009-05-28 07:11:45 +08:00
|
|
|
bool HasClassScope = !Class.TopLevelClass;
|
|
|
|
ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
|
|
|
|
HasClassScope);
|
|
|
|
|
2010-10-13 00:25:54 +08:00
|
|
|
for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
|
|
|
|
Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
|
|
|
|
}
|
|
|
|
}
|
2008-06-25 06:12:16 +08:00
|
|
|
|
2010-10-13 00:25:54 +08:00
|
|
|
void Parser::ParseLexedMethodDef(LexedMethod &LM) {
|
|
|
|
// If this is a member template, introduce the template parameter scope.
|
|
|
|
ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
|
2013-04-29 19:55:38 +08:00
|
|
|
TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
|
|
|
|
if (LM.TemplateScope) {
|
2010-10-13 00:25:54 +08:00
|
|
|
Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
|
2013-04-29 19:55:38 +08:00
|
|
|
++CurTemplateDepthTracker;
|
|
|
|
}
|
2010-10-13 00:25:54 +08:00
|
|
|
|
2018-06-27 07:20:26 +08:00
|
|
|
ParenBraceBracketBalancer BalancerRAIIObj(*this);
|
|
|
|
|
2010-10-13 00:25:54 +08:00
|
|
|
assert(!LM.Toks.empty() && "Empty body!");
|
2015-01-13 13:06:20 +08:00
|
|
|
Token LastBodyToken = LM.Toks.back();
|
|
|
|
Token BodyEnd;
|
|
|
|
BodyEnd.startToken();
|
|
|
|
BodyEnd.setKind(tok::eof);
|
2015-03-19 08:10:23 +08:00
|
|
|
BodyEnd.setLocation(LastBodyToken.getEndLoc());
|
2015-01-13 13:06:20 +08:00
|
|
|
BodyEnd.setEofData(LM.D);
|
|
|
|
LM.Toks.push_back(BodyEnd);
|
2010-10-13 00:25:54 +08:00
|
|
|
// Append the current token at the end of the new token stream so that it
|
|
|
|
// doesn't get lost.
|
|
|
|
LM.Toks.push_back(Tok);
|
2016-02-10 02:52:09 +08:00
|
|
|
PP.EnterTokenStream(LM.Toks, true);
|
2010-10-13 00:25:54 +08:00
|
|
|
|
|
|
|
// Consume the previously pushed token.
|
2013-03-28 07:58:17 +08:00
|
|
|
ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
|
2015-06-18 18:59:26 +08:00
|
|
|
assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)
|
2010-10-13 00:25:54 +08:00
|
|
|
&& "Inline method not starting with '{', ':' or 'try'");
|
|
|
|
|
|
|
|
// Parse the method body. Function body parsing code is similar enough
|
|
|
|
// to be re-used for method bodies as well.
|
2017-08-10 23:43:06 +08:00
|
|
|
ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope |
|
|
|
|
Scope::CompoundStmtScope);
|
2010-10-13 00:25:54 +08:00
|
|
|
Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
|
|
|
|
|
|
|
|
if (Tok.is(tok::kw_try)) {
|
2011-03-17 01:05:57 +08:00
|
|
|
ParseFunctionTryBlock(LM.D, FnScope);
|
2015-01-13 13:06:20 +08:00
|
|
|
|
|
|
|
while (Tok.isNot(tok::eof))
|
|
|
|
ConsumeAnyToken();
|
|
|
|
|
|
|
|
if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
|
2010-10-13 00:25:54 +08:00
|
|
|
ConsumeAnyToken();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Tok.is(tok::colon)) {
|
|
|
|
ParseConstructorInitializer(LM.D);
|
2008-06-25 06:12:16 +08:00
|
|
|
|
2010-10-13 00:25:54 +08:00
|
|
|
// Error recovery.
|
|
|
|
if (!Tok.is(tok::l_brace)) {
|
2011-03-17 01:05:57 +08:00
|
|
|
FnScope.Exit();
|
2014-05-21 14:02:52 +08:00
|
|
|
Actions.ActOnFinishFunctionBody(LM.D, nullptr);
|
2015-01-13 13:06:20 +08:00
|
|
|
|
|
|
|
while (Tok.isNot(tok::eof))
|
|
|
|
ConsumeAnyToken();
|
|
|
|
|
|
|
|
if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
|
2011-09-24 06:39:23 +08:00
|
|
|
ConsumeAnyToken();
|
2010-10-13 00:25:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
Actions.ActOnDefaultCtorInitializers(LM.D);
|
|
|
|
|
2013-04-29 19:55:38 +08:00
|
|
|
assert((Actions.getDiagnostics().hasErrorOccurred() ||
|
|
|
|
!isa<FunctionTemplateDecl>(LM.D) ||
|
|
|
|
cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
|
|
|
|
< TemplateParameterDepth) &&
|
|
|
|
"TemplateParameterDepth should be greater than the depth of "
|
|
|
|
"current template being instantiated!");
|
|
|
|
|
2011-03-17 01:05:57 +08:00
|
|
|
ParseFunctionStatementBody(LM.D, FnScope);
|
2010-10-13 00:25:54 +08:00
|
|
|
|
2015-01-13 13:06:20 +08:00
|
|
|
while (Tok.isNot(tok::eof))
|
|
|
|
ConsumeAnyToken();
|
|
|
|
|
|
|
|
if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
|
|
|
|
ConsumeAnyToken();
|
2014-05-24 04:37:38 +08:00
|
|
|
|
2016-03-30 14:27:31 +08:00
|
|
|
if (auto *FD = dyn_cast_or_null<FunctionDecl>(LM.D))
|
|
|
|
if (isa<CXXMethodDecl>(FD) ||
|
|
|
|
FD->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend))
|
|
|
|
Actions.ActOnFinishInlineFunctionDef(FD);
|
2011-06-12 01:19:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseLexedMemberInitializers - We finished parsing the member specification
|
|
|
|
/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
|
|
|
|
/// initializers that were collected during its parsing and parse them all.
|
|
|
|
void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
|
|
|
|
bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
|
|
|
|
ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
|
|
|
|
HasTemplateScope);
|
2013-04-29 19:55:38 +08:00
|
|
|
TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
|
|
|
|
if (HasTemplateScope) {
|
2011-06-12 01:19:42 +08:00
|
|
|
Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
|
2013-04-29 19:55:38 +08:00
|
|
|
++CurTemplateDepthTracker;
|
|
|
|
}
|
2012-04-16 15:05:22 +08:00
|
|
|
// Set or update the scope flags.
|
2011-06-12 01:19:42 +08:00
|
|
|
bool AlreadyHasClassScope = Class.TopLevelClass;
|
2012-04-16 15:05:22 +08:00
|
|
|
unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
|
2011-06-12 01:19:42 +08:00
|
|
|
ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
|
|
|
|
ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
|
2010-06-17 18:52:22 +08:00
|
|
|
|
2011-06-12 01:19:42 +08:00
|
|
|
if (!AlreadyHasClassScope)
|
|
|
|
Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
|
|
|
|
Class.TagOrTemplate);
|
|
|
|
|
2012-05-17 20:01:52 +08:00
|
|
|
if (!Class.LateParsedDeclarations.empty()) {
|
2012-04-16 15:05:22 +08:00
|
|
|
// C++11 [expr.prim.general]p4:
|
2018-07-31 03:24:48 +08:00
|
|
|
// Otherwise, if a member-declarator declares a non-static data member
|
2012-04-16 15:05:22 +08:00
|
|
|
// (9.2) of a class X, the expression this is a prvalue of type "pointer
|
2018-07-31 03:24:48 +08:00
|
|
|
// to X" within the optional brace-or-equal-initializer. It shall not
|
2012-04-16 15:05:22 +08:00
|
|
|
// appear elsewhere in the member-declarator.
|
|
|
|
Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
|
2018-12-13 18:15:27 +08:00
|
|
|
Qualifiers());
|
2012-04-16 15:05:22 +08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
|
|
|
|
Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
|
|
|
|
}
|
2008-06-25 06:12:16 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-06-12 01:19:42 +08:00
|
|
|
if (!AlreadyHasClassScope)
|
|
|
|
Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
|
|
|
|
Class.TagOrTemplate);
|
|
|
|
|
|
|
|
Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
|
2011-09-30 03:42:27 +08:00
|
|
|
if (!MI.Field || MI.Field->isInvalidDecl())
|
2011-06-12 01:19:42 +08:00
|
|
|
return;
|
|
|
|
|
2018-06-27 07:20:26 +08:00
|
|
|
ParenBraceBracketBalancer BalancerRAIIObj(*this);
|
|
|
|
|
2011-06-12 01:19:42 +08:00
|
|
|
// Append the current token at the end of the new token stream so that it
|
|
|
|
// doesn't get lost.
|
|
|
|
MI.Toks.push_back(Tok);
|
2016-02-10 02:52:09 +08:00
|
|
|
PP.EnterTokenStream(MI.Toks, true);
|
2011-06-12 01:19:42 +08:00
|
|
|
|
|
|
|
// Consume the previously pushed token.
|
2013-03-28 07:58:17 +08:00
|
|
|
ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
|
2011-06-12 01:19:42 +08:00
|
|
|
|
|
|
|
SourceLocation EqualLoc;
|
2012-06-10 11:12:00 +08:00
|
|
|
|
2014-01-17 11:11:34 +08:00
|
|
|
Actions.ActOnStartCXXInClassMemberInitializer();
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
|
2012-02-21 10:22:07 +08:00
|
|
|
EqualLoc);
|
2011-06-12 01:19:42 +08:00
|
|
|
|
2014-01-17 11:11:34 +08:00
|
|
|
Actions.ActOnFinishCXXInClassMemberInitializer(MI.Field, EqualLoc,
|
2014-05-29 18:55:11 +08:00
|
|
|
Init.get());
|
2011-06-12 01:19:42 +08:00
|
|
|
|
|
|
|
// The next token should be our artificial terminating EOF token.
|
|
|
|
if (Tok.isNot(tok::eof)) {
|
2014-08-27 11:23:12 +08:00
|
|
|
if (!Init.isInvalid()) {
|
|
|
|
SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
|
|
|
|
if (!EndLoc.isValid())
|
|
|
|
EndLoc = Tok.getLocation();
|
|
|
|
// No fixit; we can't recover as if there were a semicolon here.
|
|
|
|
Diag(EndLoc, diag::err_expected_semi_decl_list);
|
|
|
|
}
|
2011-06-12 01:19:42 +08:00
|
|
|
|
|
|
|
// Consume tokens until we hit the artificial EOF.
|
|
|
|
while (Tok.isNot(tok::eof))
|
|
|
|
ConsumeAnyToken();
|
|
|
|
}
|
2014-12-18 17:57:31 +08:00
|
|
|
// Make sure this is *our* artificial EOF token.
|
2014-12-19 10:13:56 +08:00
|
|
|
if (Tok.getEofData() == MI.Field)
|
2014-12-18 17:57:31 +08:00
|
|
|
ConsumeAnyToken();
|
2008-06-25 06:12:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ConsumeAndStoreUntil - Consume and store the token at the passed token
|
2008-12-17 05:30:33 +08:00
|
|
|
/// container until the token 'T' is reached (which gets
|
2009-09-09 23:08:12 +08:00
|
|
|
/// consumed/stored too, if ConsumeFinalToken).
|
2010-04-24 05:20:12 +08:00
|
|
|
/// If StopAtSemi is true, then we will stop early at a ';' character.
|
2008-12-17 05:30:33 +08:00
|
|
|
/// Returns true if token 'T1' or 'T2' was found.
|
2008-06-25 06:12:16 +08:00
|
|
|
/// NOTE: This is a specialized version of Parser::SkipUntil.
|
2008-12-17 05:30:33 +08:00
|
|
|
bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
|
|
|
|
CachedTokens &Toks,
|
2010-04-24 05:20:12 +08:00
|
|
|
bool StopAtSemi, bool ConsumeFinalToken) {
|
2008-06-25 06:12:16 +08:00
|
|
|
// We always want this function to consume at least one token if the first
|
|
|
|
// token isn't T and if not at EOF.
|
|
|
|
bool isFirstTokenConsumed = true;
|
|
|
|
while (1) {
|
|
|
|
// If we found one of the tokens, stop and return true.
|
2008-12-17 05:30:33 +08:00
|
|
|
if (Tok.is(T1) || Tok.is(T2)) {
|
|
|
|
if (ConsumeFinalToken) {
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeAnyToken();
|
|
|
|
}
|
2008-06-25 06:12:16 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::eof:
|
2013-11-23 12:06:09 +08:00
|
|
|
case tok::annot_module_begin:
|
|
|
|
case tok::annot_module_end:
|
|
|
|
case tok::annot_module_include:
|
2008-06-25 06:12:16 +08:00
|
|
|
// Ran out of tokens.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case tok::l_paren:
|
|
|
|
// Recursively consume properly-nested parens.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeParen();
|
2010-04-24 05:20:12 +08:00
|
|
|
ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
|
2008-06-25 06:12:16 +08:00
|
|
|
break;
|
|
|
|
case tok::l_square:
|
|
|
|
// Recursively consume properly-nested square brackets.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeBracket();
|
2010-04-24 05:20:12 +08:00
|
|
|
ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
|
2008-06-25 06:12:16 +08:00
|
|
|
break;
|
|
|
|
case tok::l_brace:
|
|
|
|
// Recursively consume properly-nested braces.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeBrace();
|
2010-04-24 05:20:12 +08:00
|
|
|
ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
|
2008-06-25 06:12:16 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Okay, we found a ']' or '}' or ')', which we think should be balanced.
|
|
|
|
// Since the user wasn't looking for this token (if they were, it would
|
|
|
|
// already be handled), this isn't balanced. If there is a LHS token at a
|
|
|
|
// higher level, we will assume that this matches the unbalanced token
|
|
|
|
// and return it. Otherwise, this is a spurious RHS token, which we skip.
|
|
|
|
case tok::r_paren:
|
|
|
|
if (ParenCount && !isFirstTokenConsumed)
|
|
|
|
return false; // Matches something.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeParen();
|
|
|
|
break;
|
|
|
|
case tok::r_square:
|
|
|
|
if (BracketCount && !isFirstTokenConsumed)
|
|
|
|
return false; // Matches something.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeBracket();
|
|
|
|
break;
|
|
|
|
case tok::r_brace:
|
|
|
|
if (BraceCount && !isFirstTokenConsumed)
|
|
|
|
return false; // Matches something.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeBrace();
|
|
|
|
break;
|
|
|
|
|
2010-04-24 05:20:12 +08:00
|
|
|
case tok::semi:
|
|
|
|
if (StopAtSemi)
|
|
|
|
return false;
|
Fix clang -Wimplicit-fallthrough warnings across llvm, NFC
This patch should not introduce any behavior changes. It consists of
mostly one of two changes:
1. Replacing fall through comments with the LLVM_FALLTHROUGH macro
2. Inserting 'break' before falling through into a case block consisting
of only 'break'.
We were already using this warning with GCC, but its warning behaves
slightly differently. In this patch, the following differences are
relevant:
1. GCC recognizes comments that say "fall through" as annotations, clang
doesn't
2. GCC doesn't warn on "case N: foo(); default: break;", clang does
3. GCC doesn't warn when the case contains a switch, but falls through
the outer case.
I will enable the warning separately in a follow-up patch so that it can
be cleanly reverted if necessary.
Reviewers: alexfh, rsmith, lattner, rtrieu, EricWF, bollu
Differential Revision: https://reviews.llvm.org/D53950
llvm-svn: 345882
2018-11-02 03:54:45 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2008-06-25 06:12:16 +08:00
|
|
|
default:
|
|
|
|
// consume this token.
|
|
|
|
Toks.push_back(Tok);
|
2017-05-19 03:21:48 +08:00
|
|
|
ConsumeAnyToken(/*ConsumeCodeCompletionTok*/true);
|
2008-06-25 06:12:16 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
isFirstTokenConsumed = false;
|
|
|
|
}
|
|
|
|
}
|
2011-09-25 01:48:25 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Consume tokens and store them in the passed token container until
|
2011-09-25 01:48:25 +08:00
|
|
|
/// we've passed the try keyword and constructor initializers and have consumed
|
2011-09-30 16:32:17 +08:00
|
|
|
/// the opening brace of the function body. The opening brace will be consumed
|
|
|
|
/// if and only if there was no error.
|
2011-09-25 01:48:25 +08:00
|
|
|
///
|
2013-07-04 08:13:48 +08:00
|
|
|
/// \return True on error.
|
2011-09-30 16:32:17 +08:00
|
|
|
bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
|
2011-09-25 01:48:25 +08:00
|
|
|
if (Tok.is(tok::kw_try)) {
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
|
2013-07-04 08:13:48 +08:00
|
|
|
if (Tok.isNot(tok::colon)) {
|
|
|
|
// Easy case, just a function body.
|
2011-09-25 01:48:25 +08:00
|
|
|
|
2013-07-04 08:13:48 +08:00
|
|
|
// Grab any remaining garbage to be diagnosed later. We stop when we reach a
|
|
|
|
// brace: an opening one is the function body, while a closing one probably
|
|
|
|
// means we've reached the end of the class.
|
|
|
|
ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
|
|
|
|
/*StopAtSemi=*/true,
|
|
|
|
/*ConsumeFinalToken=*/false);
|
|
|
|
if (Tok.isNot(tok::l_brace))
|
2013-12-24 17:48:30 +08:00
|
|
|
return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
|
2012-02-22 12:49:04 +08:00
|
|
|
|
2013-07-04 08:13:48 +08:00
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeBrace();
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-25 01:48:25 +08:00
|
|
|
|
2013-07-04 08:13:48 +08:00
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
// We can't reliably skip over a mem-initializer-id, because it could be
|
|
|
|
// a template-id involving not-yet-declared names. Given:
|
|
|
|
//
|
|
|
|
// S ( ) : a < b < c > ( e )
|
|
|
|
//
|
|
|
|
// 'e' might be an initializer or part of a template argument, depending
|
|
|
|
// on whether 'b' is a template.
|
|
|
|
|
|
|
|
// Track whether we might be inside a template argument. We can give
|
|
|
|
// significantly better diagnostics if we know that we're not.
|
|
|
|
bool MightBeTemplateArgument = false;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
// Skip over the mem-initializer-id, if possible.
|
|
|
|
if (Tok.is(tok::kw_decltype)) {
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
SourceLocation OpenLoc = ConsumeToken();
|
|
|
|
if (Tok.isNot(tok::l_paren))
|
|
|
|
return Diag(Tok.getLocation(), diag::err_expected_lparen_after)
|
|
|
|
<< "decltype";
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeParen();
|
|
|
|
if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) {
|
2013-12-24 17:48:30 +08:00
|
|
|
Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
|
|
|
|
Diag(OpenLoc, diag::note_matching) << tok::l_paren;
|
2011-09-25 01:48:25 +08:00
|
|
|
return true;
|
2012-02-22 12:49:04 +08:00
|
|
|
}
|
2013-07-04 08:13:48 +08:00
|
|
|
}
|
|
|
|
do {
|
|
|
|
// Walk over a component of a nested-name-specifier.
|
|
|
|
if (Tok.is(tok::coloncolon)) {
|
2012-02-22 12:49:04 +08:00
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
2013-07-04 08:13:48 +08:00
|
|
|
|
|
|
|
if (Tok.is(tok::kw_template)) {
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
2012-02-22 12:49:04 +08:00
|
|
|
}
|
2011-09-30 16:32:17 +08:00
|
|
|
|
Fix heuristics skipping invalid ctor-initializers with C++11
Use better heuristics to detect if a '{' might be the start of the constructor body
or not. Especially when there is a completion token.
Fix the test 'test/CodeCompletion/ctor-initializer.cpp ' when clang defaults to c++11
The problem was is how we recover invalid code in the ctor-init part as we skip the
function body. In particular, we want to know if a '{' is the begining of the body.
In C++03, we always consider it as the beginng of the body. The problem was that in
C++11, it may be the start of an initializer, so we skip over it, causing further
parse errors later. (It is important that we are able to parse correctly the rest
of the class definition, to know what are the class member, for example)
This commit is improving the heuristics to decide if the '{' is starting a function
body. The rules are the following: If we are not in a template argument, and that the
previous tokens are not an identifier, or a >, then it is much more likely to be the
function body. We verify that further by checking the token after the matching '}'
The commit also fix the behavior when there is a code_completion token in the
ctor-initializers.
Differential Revision: https://reviews.llvm.org/D21502
llvm-svn: 285883
2016-11-03 15:36:17 +08:00
|
|
|
if (Tok.is(tok::identifier)) {
|
2011-09-30 16:32:17 +08:00
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
2013-07-04 08:13:48 +08:00
|
|
|
} else {
|
2012-02-22 12:49:04 +08:00
|
|
|
break;
|
2011-09-30 16:32:17 +08:00
|
|
|
}
|
2013-07-04 08:13:48 +08:00
|
|
|
} while (Tok.is(tok::coloncolon));
|
|
|
|
|
Fix heuristics skipping invalid ctor-initializers with C++11
Use better heuristics to detect if a '{' might be the start of the constructor body
or not. Especially when there is a completion token.
Fix the test 'test/CodeCompletion/ctor-initializer.cpp ' when clang defaults to c++11
The problem was is how we recover invalid code in the ctor-init part as we skip the
function body. In particular, we want to know if a '{' is the begining of the body.
In C++03, we always consider it as the beginng of the body. The problem was that in
C++11, it may be the start of an initializer, so we skip over it, causing further
parse errors later. (It is important that we are able to parse correctly the rest
of the class definition, to know what are the class member, for example)
This commit is improving the heuristics to decide if the '{' is starting a function
body. The rules are the following: If we are not in a template argument, and that the
previous tokens are not an identifier, or a >, then it is much more likely to be the
function body. We verify that further by checking the token after the matching '}'
The commit also fix the behavior when there is a code_completion token in the
ctor-initializers.
Differential Revision: https://reviews.llvm.org/D21502
llvm-svn: 285883
2016-11-03 15:36:17 +08:00
|
|
|
if (Tok.is(tok::code_completion)) {
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeCodeCompletionToken();
|
|
|
|
if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype)) {
|
|
|
|
// Could be the start of another member initializer (the ',' has not
|
|
|
|
// been written yet)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.is(tok::comma)) {
|
|
|
|
// The initialization is missing, we'll diagnose it later.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-04 08:13:48 +08:00
|
|
|
if (Tok.is(tok::less))
|
|
|
|
MightBeTemplateArgument = true;
|
|
|
|
|
|
|
|
if (MightBeTemplateArgument) {
|
|
|
|
// We may be inside a template argument list. Grab up to the start of the
|
|
|
|
// next parenthesized initializer or braced-init-list. This *might* be the
|
|
|
|
// initializer, or it might be a subexpression in the template argument
|
|
|
|
// list.
|
|
|
|
// FIXME: Count angle brackets, and clear MightBeTemplateArgument
|
|
|
|
// if all angles are closed.
|
|
|
|
if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
|
|
|
|
/*StopAtSemi=*/true,
|
|
|
|
/*ConsumeFinalToken=*/false)) {
|
|
|
|
// We're not just missing the initializer, we're also missing the
|
|
|
|
// function body!
|
2013-12-24 17:48:30 +08:00
|
|
|
return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
|
2013-07-04 08:13:48 +08:00
|
|
|
}
|
|
|
|
} else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) {
|
|
|
|
// We found something weird in a mem-initializer-id.
|
2013-12-24 17:48:30 +08:00
|
|
|
if (getLangOpts().CPlusPlus11)
|
|
|
|
return Diag(Tok.getLocation(), diag::err_expected_either)
|
|
|
|
<< tok::l_paren << tok::l_brace;
|
|
|
|
else
|
|
|
|
return Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
|
2013-07-04 08:13:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tok::TokenKind kind = Tok.getKind();
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
bool IsLParen = (kind == tok::l_paren);
|
|
|
|
SourceLocation OpenLoc = Tok.getLocation();
|
|
|
|
|
|
|
|
if (IsLParen) {
|
|
|
|
ConsumeParen();
|
|
|
|
} else {
|
|
|
|
assert(kind == tok::l_brace && "Must be left paren or brace here.");
|
|
|
|
ConsumeBrace();
|
|
|
|
// In C++03, this has to be the start of the function body, which
|
|
|
|
// means the initializer is malformed; we'll diagnose it later.
|
|
|
|
if (!getLangOpts().CPlusPlus11)
|
|
|
|
return false;
|
Fix heuristics skipping invalid ctor-initializers with C++11
Use better heuristics to detect if a '{' might be the start of the constructor body
or not. Especially when there is a completion token.
Fix the test 'test/CodeCompletion/ctor-initializer.cpp ' when clang defaults to c++11
The problem was is how we recover invalid code in the ctor-init part as we skip the
function body. In particular, we want to know if a '{' is the begining of the body.
In C++03, we always consider it as the beginng of the body. The problem was that in
C++11, it may be the start of an initializer, so we skip over it, causing further
parse errors later. (It is important that we are able to parse correctly the rest
of the class definition, to know what are the class member, for example)
This commit is improving the heuristics to decide if the '{' is starting a function
body. The rules are the following: If we are not in a template argument, and that the
previous tokens are not an identifier, or a >, then it is much more likely to be the
function body. We verify that further by checking the token after the matching '}'
The commit also fix the behavior when there is a code_completion token in the
ctor-initializers.
Differential Revision: https://reviews.llvm.org/D21502
llvm-svn: 285883
2016-11-03 15:36:17 +08:00
|
|
|
|
|
|
|
const Token &PreviousToken = Toks[Toks.size() - 2];
|
|
|
|
if (!MightBeTemplateArgument &&
|
|
|
|
!PreviousToken.isOneOf(tok::identifier, tok::greater,
|
|
|
|
tok::greatergreater)) {
|
|
|
|
// If the opening brace is not preceded by one of these tokens, we are
|
|
|
|
// missing the mem-initializer-id. In order to recover better, we need
|
|
|
|
// to use heuristics to determine if this '{' is most likely the
|
2017-07-13 14:51:20 +08:00
|
|
|
// beginning of a brace-init-list or the function body.
|
Fix heuristics skipping invalid ctor-initializers with C++11
Use better heuristics to detect if a '{' might be the start of the constructor body
or not. Especially when there is a completion token.
Fix the test 'test/CodeCompletion/ctor-initializer.cpp ' when clang defaults to c++11
The problem was is how we recover invalid code in the ctor-init part as we skip the
function body. In particular, we want to know if a '{' is the begining of the body.
In C++03, we always consider it as the beginng of the body. The problem was that in
C++11, it may be the start of an initializer, so we skip over it, causing further
parse errors later. (It is important that we are able to parse correctly the rest
of the class definition, to know what are the class member, for example)
This commit is improving the heuristics to decide if the '{' is starting a function
body. The rules are the following: If we are not in a template argument, and that the
previous tokens are not an identifier, or a >, then it is much more likely to be the
function body. We verify that further by checking the token after the matching '}'
The commit also fix the behavior when there is a code_completion token in the
ctor-initializers.
Differential Revision: https://reviews.llvm.org/D21502
llvm-svn: 285883
2016-11-03 15:36:17 +08:00
|
|
|
// Check the token after the corresponding '}'.
|
|
|
|
TentativeParsingAction PA(*this);
|
|
|
|
if (SkipUntil(tok::r_brace) &&
|
|
|
|
!Tok.isOneOf(tok::comma, tok::ellipsis, tok::l_brace)) {
|
|
|
|
// Consider there was a malformed initializer and this is the start
|
|
|
|
// of the function body. We'll diagnose it later.
|
|
|
|
PA.Revert();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
PA.Revert();
|
|
|
|
}
|
2013-07-04 08:13:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the initializer (or the subexpression of the template argument).
|
|
|
|
// FIXME: If we support lambdas here, we'll need to set StopAtSemi to false
|
|
|
|
// if we might be inside the braces of a lambda-expression.
|
2013-12-24 17:48:30 +08:00
|
|
|
tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace;
|
|
|
|
if (!ConsumeAndStoreUntil(CloseKind, Toks, /*StopAtSemi=*/true)) {
|
|
|
|
Diag(Tok, diag::err_expected) << CloseKind;
|
|
|
|
Diag(OpenLoc, diag::note_matching) << kind;
|
2013-07-04 08:13:48 +08:00
|
|
|
return true;
|
2011-09-25 01:48:25 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 08:13:48 +08:00
|
|
|
// Grab pack ellipsis, if present.
|
|
|
|
if (Tok.is(tok::ellipsis)) {
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we know we just consumed a mem-initializer, we must have ',' or '{'
|
|
|
|
// next.
|
|
|
|
if (Tok.is(tok::comma)) {
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
|
|
|
} else if (Tok.is(tok::l_brace)) {
|
|
|
|
// This is the function body if the ')' or '}' is immediately followed by
|
|
|
|
// a '{'. That cannot happen within a template argument, apart from the
|
|
|
|
// case where a template argument contains a compound literal:
|
|
|
|
//
|
|
|
|
// S ( ) : a < b < c > ( d ) { }
|
|
|
|
// // End of declaration, or still inside the template argument?
|
|
|
|
//
|
|
|
|
// ... and the case where the template argument contains a lambda:
|
|
|
|
//
|
|
|
|
// S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; }
|
|
|
|
// ( ) > ( ) { }
|
|
|
|
//
|
|
|
|
// FIXME: Disambiguate these cases. Note that the latter case is probably
|
|
|
|
// going to be made ill-formed by core issue 1607.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeBrace();
|
|
|
|
return false;
|
|
|
|
} else if (!MightBeTemplateArgument) {
|
2013-12-24 17:48:30 +08:00
|
|
|
return Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
|
|
|
|
<< tok::comma;
|
2013-07-04 08:13:48 +08:00
|
|
|
}
|
2012-02-22 12:49:04 +08:00
|
|
|
}
|
2011-09-25 01:48:25 +08:00
|
|
|
}
|
2013-09-13 07:28:08 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Consume and store tokens from the '?' to the ':' in a conditional
|
2013-09-13 07:28:08 +08:00
|
|
|
/// expression.
|
|
|
|
bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) {
|
|
|
|
// Consume '?'.
|
|
|
|
assert(Tok.is(tok::question));
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
while (Tok.isNot(tok::colon)) {
|
2014-05-11 01:43:15 +08:00
|
|
|
if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks,
|
|
|
|
/*StopAtSemi=*/true,
|
|
|
|
/*ConsumeFinalToken=*/false))
|
2013-09-13 07:28:08 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// If we found a nested conditional, consume it.
|
|
|
|
if (Tok.is(tok::question) && !ConsumeAndStoreConditional(Toks))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume ':'.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A tentative parsing action that can also revert token annotations.
|
2013-09-13 07:28:08 +08:00
|
|
|
class Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction {
|
|
|
|
public:
|
|
|
|
explicit UnannotatedTentativeParsingAction(Parser &Self,
|
|
|
|
tok::TokenKind EndKind)
|
|
|
|
: TentativeParsingAction(Self), Self(Self), EndKind(EndKind) {
|
|
|
|
// Stash away the old token stream, so we can restore it once the
|
|
|
|
// tentative parse is complete.
|
|
|
|
TentativeParsingAction Inner(Self);
|
|
|
|
Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false);
|
|
|
|
Inner.Revert();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RevertAnnotations() {
|
|
|
|
Revert();
|
|
|
|
|
|
|
|
// Put back the original tokens.
|
2013-11-18 16:17:37 +08:00
|
|
|
Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch);
|
2013-09-13 07:28:08 +08:00
|
|
|
if (Toks.size()) {
|
2016-02-10 02:52:09 +08:00
|
|
|
auto Buffer = llvm::make_unique<Token[]>(Toks.size());
|
|
|
|
std::copy(Toks.begin() + 1, Toks.end(), Buffer.get());
|
2013-09-13 07:28:08 +08:00
|
|
|
Buffer[Toks.size() - 1] = Self.Tok;
|
2016-02-10 02:52:09 +08:00
|
|
|
Self.PP.EnterTokenStream(std::move(Buffer), Toks.size(), true);
|
2013-09-13 07:28:08 +08:00
|
|
|
|
|
|
|
Self.Tok = Toks.front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Parser &Self;
|
|
|
|
CachedTokens Toks;
|
|
|
|
tok::TokenKind EndKind;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// ConsumeAndStoreInitializer - Consume and store the token at the passed token
|
|
|
|
/// container until the end of the current initializer expression (either a
|
|
|
|
/// default argument or an in-class initializer for a non-static data member).
|
2014-08-27 11:23:12 +08:00
|
|
|
///
|
|
|
|
/// Returns \c true if we reached the end of something initializer-shaped,
|
|
|
|
/// \c false if we bailed out.
|
2013-09-13 07:28:08 +08:00
|
|
|
bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks,
|
|
|
|
CachedInitKind CIK) {
|
|
|
|
// We always want this function to consume at least one token if not at EOF.
|
2014-08-27 11:23:12 +08:00
|
|
|
bool IsFirstToken = true;
|
2013-09-13 07:28:08 +08:00
|
|
|
|
|
|
|
// Number of possible unclosed <s we've seen so far. These might be templates,
|
|
|
|
// and might not, but if there were none of them (or we know for sure that
|
|
|
|
// we're within a template), we can avoid a tentative parse.
|
|
|
|
unsigned AngleCount = 0;
|
|
|
|
unsigned KnownTemplateCount = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::comma:
|
|
|
|
// If we might be in a template, perform a tentative parse to check.
|
|
|
|
if (!AngleCount)
|
|
|
|
// Not a template argument: this is the end of the initializer.
|
|
|
|
return true;
|
|
|
|
if (KnownTemplateCount)
|
|
|
|
goto consume_token;
|
|
|
|
|
|
|
|
// We hit a comma inside angle brackets. This is the hard case. The
|
|
|
|
// rule we follow is:
|
|
|
|
// * For a default argument, if the tokens after the comma form a
|
|
|
|
// syntactically-valid parameter-declaration-clause, in which each
|
|
|
|
// parameter has an initializer, then this comma ends the default
|
|
|
|
// argument.
|
|
|
|
// * For a default initializer, if the tokens after the comma form a
|
|
|
|
// syntactically-valid init-declarator-list, then this comma ends
|
|
|
|
// the default initializer.
|
|
|
|
{
|
|
|
|
UnannotatedTentativeParsingAction PA(*this,
|
|
|
|
CIK == CIK_DefaultInitializer
|
|
|
|
? tok::semi : tok::r_paren);
|
|
|
|
Sema::TentativeAnalysisScope Scope(Actions);
|
|
|
|
|
2014-05-16 09:56:53 +08:00
|
|
|
TPResult Result = TPResult::Error;
|
2013-09-13 07:28:08 +08:00
|
|
|
ConsumeToken();
|
|
|
|
switch (CIK) {
|
|
|
|
case CIK_DefaultInitializer:
|
|
|
|
Result = TryParseInitDeclaratorList();
|
|
|
|
// If we parsed a complete, ambiguous init-declarator-list, this
|
|
|
|
// is only syntactically-valid if it's followed by a semicolon.
|
2014-05-16 09:56:53 +08:00
|
|
|
if (Result == TPResult::Ambiguous && Tok.isNot(tok::semi))
|
|
|
|
Result = TPResult::False;
|
2013-09-13 07:28:08 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CIK_DefaultArgument:
|
|
|
|
bool InvalidAsDeclaration = false;
|
|
|
|
Result = TryParseParameterDeclarationClause(
|
2014-12-29 07:24:02 +08:00
|
|
|
&InvalidAsDeclaration, /*VersusTemplateArgument=*/true);
|
2013-09-13 07:28:08 +08:00
|
|
|
// If this is an expression or a declaration with a missing
|
|
|
|
// 'typename', assume it's not a declaration.
|
2014-05-16 09:56:53 +08:00
|
|
|
if (Result == TPResult::Ambiguous && InvalidAsDeclaration)
|
|
|
|
Result = TPResult::False;
|
2013-09-13 07:28:08 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If what follows could be a declaration, it is a declaration.
|
2014-05-16 09:56:53 +08:00
|
|
|
if (Result != TPResult::False && Result != TPResult::Error) {
|
2013-09-13 07:28:08 +08:00
|
|
|
PA.Revert();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the uncommon case that we decide the following tokens are part
|
|
|
|
// of a template argument, revert any annotations we've performed in
|
|
|
|
// those tokens. We're not going to look them up until we've parsed
|
|
|
|
// the rest of the class, and that might add more declarations.
|
|
|
|
PA.RevertAnnotations();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep going. We know we're inside a template argument list now.
|
|
|
|
++KnownTemplateCount;
|
|
|
|
goto consume_token;
|
|
|
|
|
|
|
|
case tok::eof:
|
2013-11-23 12:06:09 +08:00
|
|
|
case tok::annot_module_begin:
|
|
|
|
case tok::annot_module_end:
|
|
|
|
case tok::annot_module_include:
|
2013-09-13 07:28:08 +08:00
|
|
|
// Ran out of tokens.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case tok::less:
|
|
|
|
// FIXME: A '<' can only start a template-id if it's preceded by an
|
|
|
|
// identifier, an operator-function-id, or a literal-operator-id.
|
|
|
|
++AngleCount;
|
|
|
|
goto consume_token;
|
|
|
|
|
|
|
|
case tok::question:
|
|
|
|
// In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does,
|
|
|
|
// that is *never* the end of the initializer. Skip to the ':'.
|
|
|
|
if (!ConsumeAndStoreConditional(Toks))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case tok::greatergreatergreater:
|
|
|
|
if (!getLangOpts().CPlusPlus11)
|
|
|
|
goto consume_token;
|
|
|
|
if (AngleCount) --AngleCount;
|
|
|
|
if (KnownTemplateCount) --KnownTemplateCount;
|
Fix clang -Wimplicit-fallthrough warnings across llvm, NFC
This patch should not introduce any behavior changes. It consists of
mostly one of two changes:
1. Replacing fall through comments with the LLVM_FALLTHROUGH macro
2. Inserting 'break' before falling through into a case block consisting
of only 'break'.
We were already using this warning with GCC, but its warning behaves
slightly differently. In this patch, the following differences are
relevant:
1. GCC recognizes comments that say "fall through" as annotations, clang
doesn't
2. GCC doesn't warn on "case N: foo(); default: break;", clang does
3. GCC doesn't warn when the case contains a switch, but falls through
the outer case.
I will enable the warning separately in a follow-up patch so that it can
be cleanly reverted if necessary.
Reviewers: alexfh, rsmith, lattner, rtrieu, EricWF, bollu
Differential Revision: https://reviews.llvm.org/D53950
llvm-svn: 345882
2018-11-02 03:54:45 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2013-09-13 07:28:08 +08:00
|
|
|
case tok::greatergreater:
|
|
|
|
if (!getLangOpts().CPlusPlus11)
|
|
|
|
goto consume_token;
|
|
|
|
if (AngleCount) --AngleCount;
|
|
|
|
if (KnownTemplateCount) --KnownTemplateCount;
|
Fix clang -Wimplicit-fallthrough warnings across llvm, NFC
This patch should not introduce any behavior changes. It consists of
mostly one of two changes:
1. Replacing fall through comments with the LLVM_FALLTHROUGH macro
2. Inserting 'break' before falling through into a case block consisting
of only 'break'.
We were already using this warning with GCC, but its warning behaves
slightly differently. In this patch, the following differences are
relevant:
1. GCC recognizes comments that say "fall through" as annotations, clang
doesn't
2. GCC doesn't warn on "case N: foo(); default: break;", clang does
3. GCC doesn't warn when the case contains a switch, but falls through
the outer case.
I will enable the warning separately in a follow-up patch so that it can
be cleanly reverted if necessary.
Reviewers: alexfh, rsmith, lattner, rtrieu, EricWF, bollu
Differential Revision: https://reviews.llvm.org/D53950
llvm-svn: 345882
2018-11-02 03:54:45 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2013-09-13 07:28:08 +08:00
|
|
|
case tok::greater:
|
|
|
|
if (AngleCount) --AngleCount;
|
|
|
|
if (KnownTemplateCount) --KnownTemplateCount;
|
|
|
|
goto consume_token;
|
|
|
|
|
|
|
|
case tok::kw_template:
|
|
|
|
// 'template' identifier '<' is known to start a template argument list,
|
|
|
|
// and can be used to disambiguate the parse.
|
|
|
|
// FIXME: Support all forms of 'template' unqualified-id '<'.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
|
|
|
if (Tok.is(tok::identifier)) {
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
|
|
|
if (Tok.is(tok::less)) {
|
2014-07-27 13:38:12 +08:00
|
|
|
++AngleCount;
|
2013-09-13 07:28:08 +08:00
|
|
|
++KnownTemplateCount;
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case tok::kw_operator:
|
|
|
|
// If 'operator' precedes other punctuation, that punctuation loses
|
|
|
|
// its special behavior.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::comma:
|
|
|
|
case tok::greatergreatergreater:
|
|
|
|
case tok::greatergreater:
|
|
|
|
case tok::greater:
|
|
|
|
case tok::less:
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case tok::l_paren:
|
|
|
|
// Recursively consume properly-nested parens.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeParen();
|
|
|
|
ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
|
|
|
|
break;
|
|
|
|
case tok::l_square:
|
|
|
|
// Recursively consume properly-nested square brackets.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeBracket();
|
|
|
|
ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
|
|
|
|
break;
|
|
|
|
case tok::l_brace:
|
|
|
|
// Recursively consume properly-nested braces.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeBrace();
|
|
|
|
ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Okay, we found a ']' or '}' or ')', which we think should be balanced.
|
|
|
|
// Since the user wasn't looking for this token (if they were, it would
|
|
|
|
// already be handled), this isn't balanced. If there is a LHS token at a
|
|
|
|
// higher level, we will assume that this matches the unbalanced token
|
2014-08-27 11:23:12 +08:00
|
|
|
// and return it. Otherwise, this is a spurious RHS token, which we
|
|
|
|
// consume and pass on to downstream code to diagnose.
|
2013-09-13 07:28:08 +08:00
|
|
|
case tok::r_paren:
|
|
|
|
if (CIK == CIK_DefaultArgument)
|
|
|
|
return true; // End of the default argument.
|
2014-08-27 11:23:12 +08:00
|
|
|
if (ParenCount && !IsFirstToken)
|
|
|
|
return false;
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeParen();
|
|
|
|
continue;
|
2013-09-13 07:28:08 +08:00
|
|
|
case tok::r_square:
|
2014-08-27 11:23:12 +08:00
|
|
|
if (BracketCount && !IsFirstToken)
|
|
|
|
return false;
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeBracket();
|
|
|
|
continue;
|
2013-09-13 07:28:08 +08:00
|
|
|
case tok::r_brace:
|
2014-08-27 11:23:12 +08:00
|
|
|
if (BraceCount && !IsFirstToken)
|
|
|
|
return false;
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeBrace();
|
|
|
|
continue;
|
2013-09-13 07:28:08 +08:00
|
|
|
|
|
|
|
case tok::code_completion:
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeCodeCompletionToken();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case tok::string_literal:
|
|
|
|
case tok::wide_string_literal:
|
|
|
|
case tok::utf8_string_literal:
|
|
|
|
case tok::utf16_string_literal:
|
|
|
|
case tok::utf32_string_literal:
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeStringToken();
|
|
|
|
break;
|
|
|
|
case tok::semi:
|
|
|
|
if (CIK == CIK_DefaultInitializer)
|
|
|
|
return true; // End of the default initializer.
|
Fix clang -Wimplicit-fallthrough warnings across llvm, NFC
This patch should not introduce any behavior changes. It consists of
mostly one of two changes:
1. Replacing fall through comments with the LLVM_FALLTHROUGH macro
2. Inserting 'break' before falling through into a case block consisting
of only 'break'.
We were already using this warning with GCC, but its warning behaves
slightly differently. In this patch, the following differences are
relevant:
1. GCC recognizes comments that say "fall through" as annotations, clang
doesn't
2. GCC doesn't warn on "case N: foo(); default: break;", clang does
3. GCC doesn't warn when the case contains a switch, but falls through
the outer case.
I will enable the warning separately in a follow-up patch so that it can
be cleanly reverted if necessary.
Reviewers: alexfh, rsmith, lattner, rtrieu, EricWF, bollu
Differential Revision: https://reviews.llvm.org/D53950
llvm-svn: 345882
2018-11-02 03:54:45 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2013-09-13 07:28:08 +08:00
|
|
|
default:
|
|
|
|
consume_token:
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
|
|
|
break;
|
|
|
|
}
|
2014-08-27 11:23:12 +08:00
|
|
|
IsFirstToken = false;
|
2013-09-13 07:28:08 +08:00
|
|
|
}
|
|
|
|
}
|