2006-08-11 02:43:39 +08:00
|
|
|
//===--- Parser.cpp - C Language Family Parser ----------------------------===//
|
2006-07-31 09:59:18 +08:00
|
|
|
//
|
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
|
2006-07-31 09:59:18 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Parser interfaces.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Parse/Parser.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2014-01-15 17:15:43 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2020-03-01 01:10:42 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2009-01-29 13:15:15 +08:00
|
|
|
#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/ParsedTemplate.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Sema/Scope.h"
|
2018-06-28 04:29:36 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2006-07-31 09:59:18 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2012-10-27 17:05:45 +08:00
|
|
|
|
2012-07-13 21:25:11 +08:00
|
|
|
namespace {
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A comment handler that passes comments found by the preprocessor
|
2012-06-20 08:34:58 +08:00
|
|
|
/// to the parser action.
|
|
|
|
class ActionCommentHandler : public CommentHandler {
|
|
|
|
Sema &S;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ActionCommentHandler(Sema &S) : S(S) { }
|
|
|
|
|
2014-03-12 13:09:18 +08:00
|
|
|
bool HandleComment(Preprocessor &PP, SourceRange Comment) override {
|
2012-06-20 08:34:58 +08:00
|
|
|
S.ActOnComment(Comment);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2012-07-13 21:25:11 +08:00
|
|
|
} // end anonymous namespace
|
2012-06-20 08:34:58 +08:00
|
|
|
|
2011-10-21 11:57:52 +08:00
|
|
|
IdentifierInfo *Parser::getSEHExceptKeyword() {
|
2018-07-31 03:24:48 +08:00
|
|
|
// __except is accepted as a (contextual) keyword
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!Ident__except && (getLangOpts().MicrosoftExt || getLangOpts().Borland))
|
2011-10-21 11:57:52 +08:00
|
|
|
Ident__except = PP.getIdentifierInfo("__except");
|
|
|
|
|
|
|
|
return Ident__except;
|
|
|
|
}
|
|
|
|
|
2012-11-01 01:29:22 +08:00
|
|
|
Parser::Parser(Preprocessor &pp, Sema &actions, bool skipFunctionBodies)
|
2011-03-22 09:15:17 +08:00
|
|
|
: PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
|
2018-07-31 03:24:48 +08:00
|
|
|
GreaterThanIsOperator(true), ColonIsSacred(false),
|
2012-04-12 18:11:59 +08:00
|
|
|
InMessageExpression(false), TemplateParameterDepth(0),
|
2012-11-01 01:29:22 +08:00
|
|
|
ParsingInObjCContainer(false) {
|
|
|
|
SkipFunctionBodies = pp.isCodeCompletionEnabled() || skipFunctionBodies;
|
2012-10-28 03:49:20 +08:00
|
|
|
Tok.startToken();
|
2006-10-14 13:19:21 +08:00
|
|
|
Tok.setKind(tok::eof);
|
2014-05-21 14:02:52 +08:00
|
|
|
Actions.CurScope = nullptr;
|
2007-07-15 08:04:39 +08:00
|
|
|
NumCachedScopes = 0;
|
2014-05-21 14:02:52 +08:00
|
|
|
CurParsedObjCImpl = nullptr;
|
2008-10-05 03:21:03 +08:00
|
|
|
|
|
|
|
// Add #pragma handlers. These are removed and destroyed in the
|
|
|
|
// destructor.
|
2014-02-21 06:52:09 +08:00
|
|
|
initializePragmaHandlers();
|
2013-05-07 05:02:12 +08:00
|
|
|
|
2012-06-20 09:06:08 +08:00
|
|
|
CommentSemaHandler.reset(new ActionCommentHandler(actions));
|
|
|
|
PP.addCommentHandler(CommentSemaHandler.get());
|
2012-06-20 08:34:58 +08:00
|
|
|
|
2010-08-25 03:08:16 +08:00
|
|
|
PP.setCodeCompletionHandler(*this);
|
2006-08-06 06:46:42 +08:00
|
|
|
}
|
|
|
|
|
2008-11-22 08:59:29 +08:00
|
|
|
DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
|
2010-11-19 04:06:41 +08:00
|
|
|
return Diags.Report(Loc, DiagID);
|
2008-11-18 15:48:38 +08:00
|
|
|
}
|
|
|
|
|
2008-11-22 08:59:29 +08:00
|
|
|
DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
|
2008-11-18 15:48:38 +08:00
|
|
|
return Diag(Tok.getLocation(), DiagID);
|
2006-07-31 09:59:18 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Emits a diagnostic suggesting parentheses surrounding a
|
Introduce code modification hints into the diagnostics system. When we
know how to recover from an error, we can attach a hint to the
diagnostic that states how to modify the code, which can be one of:
- Insert some new code (a text string) at a particular source
location
- Remove the code within a given range
- Replace the code within a given range with some new code (a text
string)
Right now, we use these hints to annotate diagnostic information. For
example, if one uses the '>>' in a template argument in C++98, as in
this code:
template<int I> class B { };
B<1000 >> 2> *b1;
we'll warn that the behavior will change in C++0x. The fix is to
insert parenthese, so we use code insertion annotations to illustrate
where the parentheses go:
test.cpp:10:10: warning: use of right-shift operator ('>>') in template
argument will require parentheses in C++0x
B<1000 >> 2> *b1;
^
( )
Use of these annotations is partially implemented for HTML
diagnostics, but it's not (yet) producing valid HTML, which may be
related to PR2386, so it has been #if 0'd out.
In this future, we could consider hooking this mechanism up to the
rewriter to actually try to fix these problems during compilation (or,
after a compilation whose only errors have fixes). For now, however, I
suggest that we use these code modification hints whenever we can, so
that we get better diagnostics now and will have better coverage when
we find better ways to use this information.
This also fixes PR3410 by placing the complaint about missing tokens
just after the previous token (rather than at the location of the next
token).
llvm-svn: 65570
2009-02-27 05:00:50 +08:00
|
|
|
/// given range.
|
|
|
|
///
|
|
|
|
/// \param Loc The location where we'll emit the diagnostic.
|
2012-08-24 01:58:28 +08:00
|
|
|
/// \param DK The kind of diagnostic to emit.
|
Introduce code modification hints into the diagnostics system. When we
know how to recover from an error, we can attach a hint to the
diagnostic that states how to modify the code, which can be one of:
- Insert some new code (a text string) at a particular source
location
- Remove the code within a given range
- Replace the code within a given range with some new code (a text
string)
Right now, we use these hints to annotate diagnostic information. For
example, if one uses the '>>' in a template argument in C++98, as in
this code:
template<int I> class B { };
B<1000 >> 2> *b1;
we'll warn that the behavior will change in C++0x. The fix is to
insert parenthese, so we use code insertion annotations to illustrate
where the parentheses go:
test.cpp:10:10: warning: use of right-shift operator ('>>') in template
argument will require parentheses in C++0x
B<1000 >> 2> *b1;
^
( )
Use of these annotations is partially implemented for HTML
diagnostics, but it's not (yet) producing valid HTML, which may be
related to PR2386, so it has been #if 0'd out.
In this future, we could consider hooking this mechanism up to the
rewriter to actually try to fix these problems during compilation (or,
after a compilation whose only errors have fixes). For now, however, I
suggest that we use these code modification hints whenever we can, so
that we get better diagnostics now and will have better coverage when
we find better ways to use this information.
This also fixes PR3410 by placing the complaint about missing tokens
just after the previous token (rather than at the location of the next
token).
llvm-svn: 65570
2009-02-27 05:00:50 +08:00
|
|
|
/// \param ParenRange Source range enclosing code that should be parenthesized.
|
|
|
|
void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
|
|
|
|
SourceRange ParenRange) {
|
2009-02-28 01:53:17 +08:00
|
|
|
SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
|
|
|
|
if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
|
Introduce code modification hints into the diagnostics system. When we
know how to recover from an error, we can attach a hint to the
diagnostic that states how to modify the code, which can be one of:
- Insert some new code (a text string) at a particular source
location
- Remove the code within a given range
- Replace the code within a given range with some new code (a text
string)
Right now, we use these hints to annotate diagnostic information. For
example, if one uses the '>>' in a template argument in C++98, as in
this code:
template<int I> class B { };
B<1000 >> 2> *b1;
we'll warn that the behavior will change in C++0x. The fix is to
insert parenthese, so we use code insertion annotations to illustrate
where the parentheses go:
test.cpp:10:10: warning: use of right-shift operator ('>>') in template
argument will require parentheses in C++0x
B<1000 >> 2> *b1;
^
( )
Use of these annotations is partially implemented for HTML
diagnostics, but it's not (yet) producing valid HTML, which may be
related to PR2386, so it has been #if 0'd out.
In this future, we could consider hooking this mechanism up to the
rewriter to actually try to fix these problems during compilation (or,
after a compilation whose only errors have fixes). For now, however, I
suggest that we use these code modification hints whenever we can, so
that we get better diagnostics now and will have better coverage when
we find better ways to use this information.
This also fixes PR3410 by placing the complaint about missing tokens
just after the previous token (rather than at the location of the next
token).
llvm-svn: 65570
2009-02-27 05:00:50 +08:00
|
|
|
// We can't display the parentheses, so just dig the
|
|
|
|
// warning/error and return.
|
|
|
|
Diag(Loc, DK);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
Diag(Loc, DK)
|
2010-04-01 01:46:05 +08:00
|
|
|
<< FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
|
|
|
|
<< FixItHint::CreateInsertion(EndLoc, ")");
|
Introduce code modification hints into the diagnostics system. When we
know how to recover from an error, we can attach a hint to the
diagnostic that states how to modify the code, which can be one of:
- Insert some new code (a text string) at a particular source
location
- Remove the code within a given range
- Replace the code within a given range with some new code (a text
string)
Right now, we use these hints to annotate diagnostic information. For
example, if one uses the '>>' in a template argument in C++98, as in
this code:
template<int I> class B { };
B<1000 >> 2> *b1;
we'll warn that the behavior will change in C++0x. The fix is to
insert parenthese, so we use code insertion annotations to illustrate
where the parentheses go:
test.cpp:10:10: warning: use of right-shift operator ('>>') in template
argument will require parentheses in C++0x
B<1000 >> 2> *b1;
^
( )
Use of these annotations is partially implemented for HTML
diagnostics, but it's not (yet) producing valid HTML, which may be
related to PR2386, so it has been #if 0'd out.
In this future, we could consider hooking this mechanism up to the
rewriter to actually try to fix these problems during compilation (or,
after a compilation whose only errors have fixes). For now, however, I
suggest that we use these code modification hints whenever we can, so
that we get better diagnostics now and will have better coverage when
we find better ways to use this information.
This also fixes PR3410 by placing the complaint about missing tokens
just after the previous token (rather than at the location of the next
token).
llvm-svn: 65570
2009-02-27 05:00:50 +08:00
|
|
|
}
|
|
|
|
|
2010-09-08 02:31:03 +08:00
|
|
|
static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) {
|
|
|
|
switch (ExpectedTok) {
|
2012-09-18 08:52:05 +08:00
|
|
|
case tok::semi:
|
|
|
|
return Tok.is(tok::colon) || Tok.is(tok::comma); // : or , for ;
|
2010-09-08 02:31:03 +08:00
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-13 03:26:13 +08:00
|
|
|
bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
|
2014-07-22 08:53:05 +08:00
|
|
|
StringRef Msg) {
|
2010-05-25 13:58:43 +08:00
|
|
|
if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) {
|
2006-08-15 12:10:31 +08:00
|
|
|
ConsumeAnyToken();
|
2006-08-13 03:26:13 +08:00
|
|
|
return false;
|
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2010-09-08 02:31:03 +08:00
|
|
|
// Detect common single-character typos and resume.
|
|
|
|
if (IsCommonTypo(ExpectedTok, Tok)) {
|
|
|
|
SourceLocation Loc = Tok.getLocation();
|
2014-02-23 11:45:03 +08:00
|
|
|
{
|
|
|
|
DiagnosticBuilder DB = Diag(Loc, DiagID);
|
|
|
|
DB << FixItHint::CreateReplacement(
|
|
|
|
SourceRange(Loc), tok::getPunctuatorSpelling(ExpectedTok));
|
|
|
|
if (DiagID == diag::err_expected)
|
|
|
|
DB << ExpectedTok;
|
|
|
|
else if (DiagID == diag::err_expected_after)
|
|
|
|
DB << Msg << ExpectedTok;
|
|
|
|
else
|
|
|
|
DB << Msg;
|
|
|
|
}
|
2010-09-08 02:31:03 +08:00
|
|
|
|
|
|
|
// Pretend there wasn't a problem.
|
2014-02-23 11:45:03 +08:00
|
|
|
ConsumeAnyToken();
|
2010-09-08 02:31:03 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-28 01:53:17 +08:00
|
|
|
SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
|
2014-05-21 14:02:52 +08:00
|
|
|
const char *Spelling = nullptr;
|
2014-01-01 11:08:43 +08:00
|
|
|
if (EndLoc.isValid())
|
2014-01-06 20:54:07 +08:00
|
|
|
Spelling = tok::getPunctuatorSpelling(ExpectedTok);
|
2014-01-01 11:08:43 +08:00
|
|
|
|
|
|
|
DiagnosticBuilder DB =
|
|
|
|
Spelling
|
|
|
|
? Diag(EndLoc, DiagID) << FixItHint::CreateInsertion(EndLoc, Spelling)
|
|
|
|
: Diag(Tok, DiagID);
|
|
|
|
if (DiagID == diag::err_expected)
|
|
|
|
DB << ExpectedTok;
|
|
|
|
else if (DiagID == diag::err_expected_after)
|
|
|
|
DB << Msg << ExpectedTok;
|
|
|
|
else
|
|
|
|
DB << Msg;
|
Introduce code modification hints into the diagnostics system. When we
know how to recover from an error, we can attach a hint to the
diagnostic that states how to modify the code, which can be one of:
- Insert some new code (a text string) at a particular source
location
- Remove the code within a given range
- Replace the code within a given range with some new code (a text
string)
Right now, we use these hints to annotate diagnostic information. For
example, if one uses the '>>' in a template argument in C++98, as in
this code:
template<int I> class B { };
B<1000 >> 2> *b1;
we'll warn that the behavior will change in C++0x. The fix is to
insert parenthese, so we use code insertion annotations to illustrate
where the parentheses go:
test.cpp:10:10: warning: use of right-shift operator ('>>') in template
argument will require parentheses in C++0x
B<1000 >> 2> *b1;
^
( )
Use of these annotations is partially implemented for HTML
diagnostics, but it's not (yet) producing valid HTML, which may be
related to PR2386, so it has been #if 0'd out.
In this future, we could consider hooking this mechanism up to the
rewriter to actually try to fix these problems during compilation (or,
after a compilation whose only errors have fixes). For now, however, I
suggest that we use these code modification hints whenever we can, so
that we get better diagnostics now and will have better coverage when
we find better ways to use this information.
This also fixes PR3410 by placing the complaint about missing tokens
just after the previous token (rather than at the location of the next
token).
llvm-svn: 65570
2009-02-27 05:00:50 +08:00
|
|
|
|
2006-08-13 03:26:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-07 23:23:11 +08:00
|
|
|
bool Parser::ExpectAndConsumeSemi(unsigned DiagID) {
|
2013-12-24 17:48:30 +08:00
|
|
|
if (TryConsumeToken(tok::semi))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Tok.is(tok::code_completion)) {
|
|
|
|
handleUnexpectedCodeCompletionToken();
|
2010-09-07 23:23:11 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) &&
|
2010-09-07 23:23:11 +08:00
|
|
|
NextToken().is(tok::semi)) {
|
|
|
|
Diag(Tok, diag::err_extraneous_token_before_semi)
|
|
|
|
<< PP.getSpelling(Tok)
|
|
|
|
<< FixItHint::CreateRemoval(Tok.getLocation());
|
|
|
|
ConsumeAnyToken(); // The ')' or ']'.
|
|
|
|
ConsumeToken(); // The ';'.
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-09-07 23:23:11 +08:00
|
|
|
return ExpectAndConsume(tok::semi, DiagID);
|
|
|
|
}
|
|
|
|
|
2019-08-01 19:46:28 +08:00
|
|
|
void Parser::ConsumeExtraSemi(ExtraSemiKind Kind, DeclSpec::TST TST) {
|
2012-05-17 03:04:59 +08:00
|
|
|
if (!Tok.is(tok::semi)) return;
|
|
|
|
|
2012-07-23 13:45:25 +08:00
|
|
|
bool HadMultipleSemis = false;
|
2012-05-17 03:04:59 +08:00
|
|
|
SourceLocation StartLoc = Tok.getLocation();
|
|
|
|
SourceLocation EndLoc = Tok.getLocation();
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
while ((Tok.is(tok::semi) && !Tok.isAtStartOfLine())) {
|
2012-07-23 13:45:25 +08:00
|
|
|
HadMultipleSemis = true;
|
2012-05-17 03:04:59 +08:00
|
|
|
EndLoc = Tok.getLocation();
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
|
2012-07-23 13:45:25 +08:00
|
|
|
// C++11 allows extra semicolons at namespace scope, but not in any of the
|
|
|
|
// other contexts.
|
|
|
|
if (Kind == OutsideFunction && getLangOpts().CPlusPlus) {
|
2013-01-02 19:42:31 +08:00
|
|
|
if (getLangOpts().CPlusPlus11)
|
2012-07-23 13:45:25 +08:00
|
|
|
Diag(StartLoc, diag::warn_cxx98_compat_top_level_semi)
|
|
|
|
<< FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
|
|
|
|
else
|
|
|
|
Diag(StartLoc, diag::ext_extra_semi_cxx11)
|
|
|
|
<< FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
|
2012-05-17 03:04:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-23 13:45:25 +08:00
|
|
|
if (Kind != AfterMemberFunctionDefinition || HadMultipleSemis)
|
|
|
|
Diag(StartLoc, diag::ext_extra_semi)
|
2019-08-01 19:46:28 +08:00
|
|
|
<< Kind << DeclSpec::getSpecifierName(TST,
|
2014-01-15 17:15:43 +08:00
|
|
|
Actions.getASTContext().getPrintingPolicy())
|
2012-07-23 13:45:25 +08:00
|
|
|
<< FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
|
|
|
|
else
|
|
|
|
// A single semicolon is valid after a member function definition.
|
|
|
|
Diag(StartLoc, diag::warn_extra_semi_after_mem_fn_def)
|
|
|
|
<< FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
|
2012-05-17 03:04:59 +08:00
|
|
|
}
|
|
|
|
|
2017-04-11 23:01:53 +08:00
|
|
|
bool Parser::expectIdentifier() {
|
|
|
|
if (Tok.is(tok::identifier))
|
|
|
|
return false;
|
|
|
|
if (const auto *II = Tok.getIdentifierInfo()) {
|
|
|
|
if (II->isCPlusPlusKeyword(getLangOpts())) {
|
|
|
|
Diag(Tok, diag::err_expected_token_instead_of_objcxx_keyword)
|
|
|
|
<< tok::identifier << Tok.getIdentifierInfo();
|
|
|
|
// Objective-C++: Recover by treating this keyword as a valid identifier.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Diag(Tok, diag::err_expected) << tok::identifier;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-28 07:08:11 +08:00
|
|
|
void Parser::checkCompoundToken(SourceLocation FirstTokLoc,
|
|
|
|
tok::TokenKind FirstTokKind, CompoundToken Op) {
|
|
|
|
if (FirstTokLoc.isInvalid())
|
|
|
|
return;
|
|
|
|
SourceLocation SecondTokLoc = Tok.getLocation();
|
|
|
|
|
|
|
|
// We expect both tokens to come from the same FileID.
|
|
|
|
FileID FirstID = PP.getSourceManager().getFileID(FirstTokLoc);
|
|
|
|
FileID SecondID = PP.getSourceManager().getFileID(SecondTokLoc);
|
|
|
|
if (FirstID != SecondID) {
|
|
|
|
Diag(FirstTokLoc, (FirstTokLoc.isMacroID() || SecondTokLoc.isMacroID())
|
|
|
|
? diag::warn_compound_token_split_by_macro
|
|
|
|
: diag::warn_compound_token_split_by_file)
|
|
|
|
<< (FirstTokKind == Tok.getKind()) << FirstTokKind << Tok.getKind()
|
|
|
|
<< static_cast<int>(Op) << SourceRange(FirstTokLoc);
|
|
|
|
Diag(SecondTokLoc, diag::note_compound_token_split_second_token_here)
|
|
|
|
<< (FirstTokKind == Tok.getKind()) << Tok.getKind()
|
|
|
|
<< SourceRange(SecondTokLoc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect the tokens to abut.
|
|
|
|
if (Tok.hasLeadingSpace()) {
|
|
|
|
SourceLocation SpaceLoc = PP.getLocForEndOfToken(FirstTokLoc);
|
|
|
|
if (SpaceLoc.isInvalid())
|
|
|
|
SpaceLoc = FirstTokLoc;
|
|
|
|
Diag(SpaceLoc, diag::warn_compound_token_split_by_whitespace)
|
|
|
|
<< (FirstTokKind == Tok.getKind()) << FirstTokKind << Tok.getKind()
|
|
|
|
<< static_cast<int>(Op) << SourceRange(FirstTokLoc, SecondTokLoc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-07 05:55:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Error recovery.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-11-19 00:50:24 +08:00
|
|
|
static bool HasFlagsSet(Parser::SkipUntilFlags L, Parser::SkipUntilFlags R) {
|
2013-11-18 16:17:37 +08:00
|
|
|
return (static_cast<unsigned>(L) & static_cast<unsigned>(R)) != 0;
|
|
|
|
}
|
|
|
|
|
2006-08-07 05:55:29 +08:00
|
|
|
/// SkipUntil - Read tokens until we get to the specified token, then consume
|
2013-11-18 16:17:37 +08:00
|
|
|
/// it (unless no flag StopBeforeMatch). Because we cannot guarantee that the
|
2006-08-07 05:55:29 +08:00
|
|
|
/// token will ever occur, this skips to the next token, or to some likely
|
|
|
|
/// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
|
|
|
|
/// character.
|
2008-06-20 03:28:49 +08:00
|
|
|
///
|
2006-08-07 05:55:29 +08:00
|
|
|
/// If SkipUntil finds the specified token, it returns true, otherwise it
|
2008-06-20 03:28:49 +08:00
|
|
|
/// returns false.
|
2013-11-18 16:17:37 +08:00
|
|
|
bool Parser::SkipUntil(ArrayRef<tok::TokenKind> Toks, SkipUntilFlags Flags) {
|
2006-08-11 14:40:25 +08:00
|
|
|
// We always want this function to skip at least one token if the first token
|
|
|
|
// isn't T and if not at EOF.
|
|
|
|
bool isFirstTokenSkipped = true;
|
2006-08-07 05:55:29 +08:00
|
|
|
while (1) {
|
2007-04-28 03:12:15 +08:00
|
|
|
// If we found one of the tokens, stop and return true.
|
2012-04-10 00:37:11 +08:00
|
|
|
for (unsigned i = 0, NumToks = Toks.size(); i != NumToks; ++i) {
|
2007-10-10 01:23:58 +08:00
|
|
|
if (Tok.is(Toks[i])) {
|
2013-11-18 16:17:37 +08:00
|
|
|
if (HasFlagsSet(Flags, StopBeforeMatch)) {
|
2007-04-28 03:12:15 +08:00
|
|
|
// Noop, don't consume the token.
|
|
|
|
} else {
|
|
|
|
ConsumeAnyToken();
|
|
|
|
}
|
|
|
|
return true;
|
2006-08-07 05:55:29 +08:00
|
|
|
}
|
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2013-07-25 10:11:20 +08:00
|
|
|
// Important special case: The caller has given up and just wants us to
|
|
|
|
// skip the rest of the file. Do this without recursing, since we can
|
|
|
|
// get here precisely because the caller detected too much recursion.
|
2013-11-18 16:17:37 +08:00
|
|
|
if (Toks.size() == 1 && Toks[0] == tok::eof &&
|
|
|
|
!HasFlagsSet(Flags, StopAtSemi) &&
|
|
|
|
!HasFlagsSet(Flags, StopAtCodeCompletion)) {
|
2013-11-23 12:06:09 +08:00
|
|
|
while (Tok.isNot(tok::eof))
|
2013-07-25 10:11:20 +08:00
|
|
|
ConsumeAnyToken();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-08-07 05:55:29 +08:00
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::eof:
|
|
|
|
// Ran out of tokens.
|
|
|
|
return false;
|
2013-11-23 12:06:09 +08:00
|
|
|
|
2015-10-19 14:40:17 +08:00
|
|
|
case tok::annot_pragma_openmp:
|
2013-12-19 03:10:54 +08:00
|
|
|
case tok::annot_pragma_openmp_end:
|
|
|
|
// Stop before an OpenMP pragma boundary.
|
2019-12-14 05:05:30 +08:00
|
|
|
if (OpenMPDirectiveParsing)
|
|
|
|
return false;
|
|
|
|
ConsumeAnnotationToken();
|
|
|
|
break;
|
2013-11-23 12:06:09 +08:00
|
|
|
case tok::annot_module_begin:
|
|
|
|
case tok::annot_module_end:
|
|
|
|
case tok::annot_module_include:
|
|
|
|
// Stop before we change submodules. They generally indicate a "good"
|
|
|
|
// place to pick up parsing again (except in the special case where
|
|
|
|
// we're trying to skip to EOF).
|
|
|
|
return false;
|
|
|
|
|
2010-05-25 13:58:43 +08:00
|
|
|
case tok::code_completion:
|
2013-11-18 16:17:37 +08:00
|
|
|
if (!HasFlagsSet(Flags, StopAtCodeCompletion))
|
2013-12-24 17:48:30 +08:00
|
|
|
handleUnexpectedCodeCompletionToken();
|
2010-05-25 13:58:43 +08:00
|
|
|
return false;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2006-08-07 05:55:29 +08:00
|
|
|
case tok::l_paren:
|
|
|
|
// Recursively skip properly-nested parens.
|
|
|
|
ConsumeParen();
|
2013-12-19 03:10:49 +08:00
|
|
|
if (HasFlagsSet(Flags, StopAtCodeCompletion))
|
|
|
|
SkipUntil(tok::r_paren, StopAtCodeCompletion);
|
|
|
|
else
|
|
|
|
SkipUntil(tok::r_paren);
|
2006-08-07 05:55:29 +08:00
|
|
|
break;
|
|
|
|
case tok::l_square:
|
|
|
|
// Recursively skip properly-nested square brackets.
|
|
|
|
ConsumeBracket();
|
2013-12-19 03:10:49 +08:00
|
|
|
if (HasFlagsSet(Flags, StopAtCodeCompletion))
|
|
|
|
SkipUntil(tok::r_square, StopAtCodeCompletion);
|
|
|
|
else
|
|
|
|
SkipUntil(tok::r_square);
|
2006-08-07 05:55:29 +08:00
|
|
|
break;
|
|
|
|
case tok::l_brace:
|
|
|
|
// Recursively skip properly-nested braces.
|
|
|
|
ConsumeBrace();
|
2013-12-19 03:10:49 +08:00
|
|
|
if (HasFlagsSet(Flags, StopAtCodeCompletion))
|
|
|
|
SkipUntil(tok::r_brace, StopAtCodeCompletion);
|
|
|
|
else
|
|
|
|
SkipUntil(tok::r_brace);
|
2006-08-07 05:55:29 +08:00
|
|
|
break;
|
2019-05-09 11:31:27 +08:00
|
|
|
case tok::question:
|
|
|
|
// Recursively skip ? ... : pairs; these function as brackets. But
|
|
|
|
// still stop at a semicolon if requested.
|
|
|
|
ConsumeToken();
|
|
|
|
SkipUntil(tok::colon,
|
|
|
|
SkipUntilFlags(unsigned(Flags) &
|
|
|
|
unsigned(StopAtCodeCompletion | StopAtSemi)));
|
|
|
|
break;
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2006-08-07 05:55:29 +08:00
|
|
|
// 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:
|
2013-12-19 03:10:49 +08:00
|
|
|
if (ParenCount && !isFirstTokenSkipped)
|
2006-08-11 14:40:25 +08:00
|
|
|
return false; // Matches something.
|
2006-08-07 05:55:29 +08:00
|
|
|
ConsumeParen();
|
|
|
|
break;
|
|
|
|
case tok::r_square:
|
2013-12-19 03:10:49 +08:00
|
|
|
if (BracketCount && !isFirstTokenSkipped)
|
2006-08-11 14:40:25 +08:00
|
|
|
return false; // Matches something.
|
2006-08-07 05:55:29 +08:00
|
|
|
ConsumeBracket();
|
|
|
|
break;
|
|
|
|
case tok::r_brace:
|
2013-12-19 03:10:49 +08:00
|
|
|
if (BraceCount && !isFirstTokenSkipped)
|
2006-08-11 14:40:25 +08:00
|
|
|
return false; // Matches something.
|
2006-08-07 05:55:29 +08:00
|
|
|
ConsumeBrace();
|
|
|
|
break;
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2006-08-07 05:55:29 +08:00
|
|
|
case tok::semi:
|
2013-11-18 16:17:37 +08:00
|
|
|
if (HasFlagsSet(Flags, StopAtSemi))
|
2006-08-07 05:55:29 +08:00
|
|
|
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;
|
2006-08-07 05:55:29 +08:00
|
|
|
default:
|
|
|
|
// Skip this token.
|
2017-05-19 03:21:48 +08:00
|
|
|
ConsumeAnyToken();
|
2006-08-07 05:55:29 +08:00
|
|
|
break;
|
|
|
|
}
|
2006-08-11 14:40:25 +08:00
|
|
|
isFirstTokenSkipped = false;
|
2008-06-20 03:28:49 +08:00
|
|
|
}
|
2006-08-07 05:55:29 +08:00
|
|
|
}
|
|
|
|
|
2006-08-14 08:15:05 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Scope manipulation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// EnterScope - Start a new scope.
|
2006-11-06 07:47:55 +08:00
|
|
|
void Parser::EnterScope(unsigned ScopeFlags) {
|
2007-07-15 08:04:39 +08:00
|
|
|
if (NumCachedScopes) {
|
|
|
|
Scope *N = ScopeCache[--NumCachedScopes];
|
2010-07-03 01:43:08 +08:00
|
|
|
N->Init(getCurScope(), ScopeFlags);
|
|
|
|
Actions.CurScope = N;
|
2006-11-06 08:22:42 +08:00
|
|
|
} else {
|
2010-11-19 08:19:12 +08:00
|
|
|
Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
|
2006-11-06 08:22:42 +08:00
|
|
|
}
|
2006-08-14 08:15:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ExitScope - Pop a scope off the scope stack.
|
|
|
|
void Parser::ExitScope() {
|
2010-07-03 01:43:08 +08:00
|
|
|
assert(getCurScope() && "Scope imbalance!");
|
2006-08-14 08:15:05 +08:00
|
|
|
|
2007-10-10 04:37:18 +08:00
|
|
|
// Inform the actions module that this scope is going away if there are any
|
|
|
|
// decls in it.
|
2014-05-03 08:41:18 +08:00
|
|
|
Actions.ActOnPopScope(Tok.getLocation(), getCurScope());
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2010-07-03 01:43:08 +08:00
|
|
|
Scope *OldScope = getCurScope();
|
|
|
|
Actions.CurScope = OldScope->getParent();
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2007-07-15 08:04:39 +08:00
|
|
|
if (NumCachedScopes == ScopeCacheSize)
|
|
|
|
delete OldScope;
|
2006-11-06 08:22:42 +08:00
|
|
|
else
|
2007-07-15 08:04:39 +08:00
|
|
|
ScopeCache[NumCachedScopes++] = OldScope;
|
2006-08-14 08:15:05 +08:00
|
|
|
}
|
|
|
|
|
2011-06-12 01:19:42 +08:00
|
|
|
/// Set the flags for the current scope to ScopeFlags. If ManageFlags is false,
|
|
|
|
/// this object does nothing.
|
|
|
|
Parser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags,
|
|
|
|
bool ManageFlags)
|
2014-05-21 14:02:52 +08:00
|
|
|
: CurScope(ManageFlags ? Self->getCurScope() : nullptr) {
|
2011-06-12 01:19:42 +08:00
|
|
|
if (CurScope) {
|
|
|
|
OldFlags = CurScope->getFlags();
|
|
|
|
CurScope->setFlags(ScopeFlags);
|
|
|
|
}
|
|
|
|
}
|
2006-08-14 08:15:05 +08:00
|
|
|
|
2011-06-12 01:19:42 +08:00
|
|
|
/// Restore the flags for the current scope to what they were before this
|
|
|
|
/// object overrode them.
|
|
|
|
Parser::ParseScopeFlags::~ParseScopeFlags() {
|
|
|
|
if (CurScope)
|
|
|
|
CurScope->setFlags(OldFlags);
|
|
|
|
}
|
2006-08-14 08:15:05 +08:00
|
|
|
|
|
|
|
|
2006-07-31 13:09:04 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// C99 6.9: External Definitions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2006-07-31 09:59:18 +08:00
|
|
|
|
2006-11-06 08:22:42 +08:00
|
|
|
Parser::~Parser() {
|
|
|
|
// If we still have scopes active, delete the scope tree.
|
2010-07-03 01:43:08 +08:00
|
|
|
delete getCurScope();
|
2014-05-21 14:02:52 +08:00
|
|
|
Actions.CurScope = nullptr;
|
|
|
|
|
2006-11-06 08:22:42 +08:00
|
|
|
// Free the scope cache.
|
2007-07-15 08:04:39 +08:00
|
|
|
for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
|
|
|
|
delete ScopeCache[i];
|
2008-10-05 03:21:03 +08:00
|
|
|
|
2014-02-21 06:52:09 +08:00
|
|
|
resetPragmaHandlers();
|
2012-06-20 08:34:58 +08:00
|
|
|
|
2012-06-20 09:06:08 +08:00
|
|
|
PP.removeCommentHandler(CommentSemaHandler.get());
|
2012-06-20 08:34:58 +08:00
|
|
|
|
2010-08-25 03:08:16 +08:00
|
|
|
PP.clearCodeCompletionHandler();
|
2012-04-14 20:14:03 +08:00
|
|
|
|
2020-04-06 14:10:08 +08:00
|
|
|
DestroyTemplateIds();
|
2006-11-06 08:22:42 +08:00
|
|
|
}
|
|
|
|
|
2006-08-17 15:04:37 +08:00
|
|
|
/// Initialize - Warm up the parser.
|
|
|
|
///
|
|
|
|
void Parser::Initialize() {
|
2007-08-26 14:24:45 +08:00
|
|
|
// Create the translation unit scope. Install it as the current scope.
|
2014-05-21 14:02:52 +08:00
|
|
|
assert(getCurScope() == nullptr && "A scope is already active?");
|
2007-08-26 14:24:45 +08:00
|
|
|
EnterScope(Scope::DeclScope);
|
2010-08-26 02:07:12 +08:00
|
|
|
Actions.ActOnTranslationUnitScope(getCurScope());
|
|
|
|
|
2007-08-30 06:54:08 +08:00
|
|
|
// Initialization for Objective-C context sensitive keywords recognition.
|
2008-01-08 03:49:32 +08:00
|
|
|
// Referenced in Parser::ParseObjCTypeQualifierList.
|
2018-10-31 04:31:30 +08:00
|
|
|
if (getLangOpts().ObjC) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
|
|
|
|
ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
|
|
|
|
ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
|
|
|
|
ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
|
|
|
|
ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
|
|
|
|
ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
|
2015-06-20 02:14:38 +08:00
|
|
|
ObjCTypeQuals[objc_nonnull] = &PP.getIdentifierTable().get("nonnull");
|
|
|
|
ObjCTypeQuals[objc_nullable] = &PP.getIdentifierTable().get("nullable");
|
|
|
|
ObjCTypeQuals[objc_null_unspecified]
|
|
|
|
= &PP.getIdentifierTable().get("null_unspecified");
|
2007-08-30 06:54:08 +08:00
|
|
|
}
|
2008-08-15 06:04:54 +08:00
|
|
|
|
2014-05-21 14:02:52 +08:00
|
|
|
Ident_instancetype = nullptr;
|
|
|
|
Ident_final = nullptr;
|
|
|
|
Ident_sealed = nullptr;
|
|
|
|
Ident_override = nullptr;
|
2016-07-30 04:01:12 +08:00
|
|
|
Ident_GNU_final = nullptr;
|
2019-04-14 16:06:59 +08:00
|
|
|
Ident_import = nullptr;
|
|
|
|
Ident_module = nullptr;
|
2011-01-17 07:56:42 +08:00
|
|
|
|
2008-08-15 06:04:54 +08:00
|
|
|
Ident_super = &PP.getIdentifierTable().get("super");
|
2010-02-05 08:12:22 +08:00
|
|
|
|
2015-07-30 23:53:58 +08:00
|
|
|
Ident_vector = nullptr;
|
|
|
|
Ident_bool = nullptr;
|
|
|
|
Ident_pixel = nullptr;
|
2015-07-30 22:08:36 +08:00
|
|
|
if (getLangOpts().AltiVec || getLangOpts().ZVector) {
|
2010-02-05 08:12:22 +08:00
|
|
|
Ident_vector = &PP.getIdentifierTable().get("vector");
|
2013-07-04 04:54:09 +08:00
|
|
|
Ident_bool = &PP.getIdentifierTable().get("bool");
|
2010-02-05 08:12:22 +08:00
|
|
|
}
|
2015-07-30 22:08:36 +08:00
|
|
|
if (getLangOpts().AltiVec)
|
|
|
|
Ident_pixel = &PP.getIdentifierTable().get("pixel");
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
|
2014-05-21 14:02:52 +08:00
|
|
|
Ident_introduced = nullptr;
|
|
|
|
Ident_deprecated = nullptr;
|
|
|
|
Ident_obsoleted = nullptr;
|
|
|
|
Ident_unavailable = nullptr;
|
2016-02-22 12:47:24 +08:00
|
|
|
Ident_strict = nullptr;
|
2016-03-22 01:30:55 +08:00
|
|
|
Ident_replacement = nullptr;
|
2011-04-28 09:08:34 +08:00
|
|
|
|
2017-03-02 02:06:25 +08:00
|
|
|
Ident_language = Ident_defined_in = Ident_generated_declaration = nullptr;
|
|
|
|
|
2014-05-21 14:02:52 +08:00
|
|
|
Ident__except = nullptr;
|
|
|
|
|
|
|
|
Ident__exception_code = Ident__exception_info = nullptr;
|
|
|
|
Ident__abnormal_termination = Ident___exception_code = nullptr;
|
|
|
|
Ident___exception_info = Ident___abnormal_termination = nullptr;
|
|
|
|
Ident_GetExceptionCode = Ident_GetExceptionInfo = nullptr;
|
|
|
|
Ident_AbnormalTermination = nullptr;
|
2011-04-28 09:08:34 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if(getLangOpts().Borland) {
|
2011-04-28 09:08:34 +08:00
|
|
|
Ident__exception_info = PP.getIdentifierInfo("_exception_info");
|
|
|
|
Ident___exception_info = PP.getIdentifierInfo("__exception_info");
|
|
|
|
Ident_GetExceptionInfo = PP.getIdentifierInfo("GetExceptionInformation");
|
|
|
|
Ident__exception_code = PP.getIdentifierInfo("_exception_code");
|
|
|
|
Ident___exception_code = PP.getIdentifierInfo("__exception_code");
|
|
|
|
Ident_GetExceptionCode = PP.getIdentifierInfo("GetExceptionCode");
|
|
|
|
Ident__abnormal_termination = PP.getIdentifierInfo("_abnormal_termination");
|
|
|
|
Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination");
|
|
|
|
Ident_AbnormalTermination = PP.getIdentifierInfo("AbnormalTermination");
|
|
|
|
|
|
|
|
PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block);
|
|
|
|
PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block);
|
|
|
|
PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block);
|
|
|
|
PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter);
|
|
|
|
PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter);
|
|
|
|
PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter);
|
|
|
|
PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block);
|
|
|
|
PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block);
|
|
|
|
PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block);
|
|
|
|
}
|
2012-11-06 07:58:27 +08:00
|
|
|
|
2019-04-14 16:06:59 +08:00
|
|
|
if (getLangOpts().CPlusPlusModules) {
|
|
|
|
Ident_import = PP.getIdentifierInfo("import");
|
|
|
|
Ident_module = PP.getIdentifierInfo("module");
|
|
|
|
}
|
|
|
|
|
2012-11-06 07:58:27 +08:00
|
|
|
Actions.Initialize();
|
|
|
|
|
|
|
|
// Prime the lexer look-ahead.
|
|
|
|
ConsumeToken();
|
2006-08-17 15:04:37 +08:00
|
|
|
}
|
|
|
|
|
2020-04-06 14:10:08 +08:00
|
|
|
void Parser::DestroyTemplateIds() {
|
|
|
|
for (TemplateIdAnnotation *Id : TemplateIds)
|
|
|
|
Id->Destroy();
|
|
|
|
TemplateIds.clear();
|
2014-10-23 01:50:19 +08:00
|
|
|
}
|
|
|
|
|
2019-04-14 16:06:59 +08:00
|
|
|
/// Parse the first top-level declaration in a translation unit.
|
|
|
|
///
|
|
|
|
/// translation-unit:
|
|
|
|
/// [C] external-declaration
|
|
|
|
/// [C] translation-unit external-declaration
|
|
|
|
/// [C++] top-level-declaration-seq[opt]
|
|
|
|
/// [C++20] global-module-fragment[opt] module-declaration
|
|
|
|
/// top-level-declaration-seq[opt] private-module-fragment[opt]
|
|
|
|
///
|
|
|
|
/// Note that in C, it is an error if there is no first declaration.
|
2016-08-19 09:43:06 +08:00
|
|
|
bool Parser::ParseFirstTopLevelDecl(DeclGroupPtrTy &Result) {
|
2017-07-05 09:42:07 +08:00
|
|
|
Actions.ActOnStartOfTranslationUnit();
|
|
|
|
|
2016-08-26 08:14:38 +08:00
|
|
|
// C11 6.9p1 says translation units must have at least one top-level
|
|
|
|
// declaration. C++ doesn't have this restriction. We also don't want to
|
|
|
|
// complain if we have a precompiled header, although technically if the PCH
|
|
|
|
// is empty we should still emit the (pedantic) diagnostic.
|
2020-08-12 06:06:52 +08:00
|
|
|
// If the main file is a header, we're only pretending it's a TU; don't warn.
|
2019-04-14 16:06:59 +08:00
|
|
|
bool NoTopLevelDecls = ParseTopLevelDecl(Result, true);
|
2016-08-26 08:14:38 +08:00
|
|
|
if (NoTopLevelDecls && !Actions.getASTContext().getExternalSource() &&
|
2020-08-12 06:06:52 +08:00
|
|
|
!getLangOpts().CPlusPlus && !getLangOpts().IsHeaderFile)
|
2016-08-26 08:14:38 +08:00
|
|
|
Diag(diag::ext_empty_translation_unit);
|
|
|
|
|
|
|
|
return NoTopLevelDecls;
|
2016-08-19 09:43:06 +08:00
|
|
|
}
|
|
|
|
|
2006-08-17 15:04:37 +08:00
|
|
|
/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
|
|
|
|
/// action tells us to. This returns true if the EOF was encountered.
|
2019-04-14 16:06:59 +08:00
|
|
|
///
|
|
|
|
/// top-level-declaration:
|
|
|
|
/// declaration
|
|
|
|
/// [C++20] module-import-declaration
|
|
|
|
bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result, bool IsFirstDecl) {
|
2020-04-06 14:10:08 +08:00
|
|
|
DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this);
|
2011-01-18 02:58:44 +08:00
|
|
|
|
2013-11-23 12:06:09 +08:00
|
|
|
// Skip over the EOF token, flagging end of previous input for incremental
|
2012-03-16 18:40:17 +08:00
|
|
|
// processing
|
2013-11-24 10:12:18 +08:00
|
|
|
if (PP.isIncrementalProcessingEnabled() && Tok.is(tok::eof))
|
2012-03-16 18:40:17 +08:00
|
|
|
ConsumeToken();
|
|
|
|
|
2016-01-16 07:43:25 +08:00
|
|
|
Result = nullptr;
|
2013-11-15 12:24:58 +08:00
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::annot_pragma_unused:
|
2011-01-18 02:58:44 +08:00
|
|
|
HandlePragmaUnused();
|
2013-11-15 12:24:58 +08:00
|
|
|
return false;
|
2011-01-18 02:58:44 +08:00
|
|
|
|
2017-04-22 06:39:18 +08:00
|
|
|
case tok::kw_export:
|
2019-04-14 16:06:59 +08:00
|
|
|
switch (NextToken().getKind()) {
|
|
|
|
case tok::kw_module:
|
|
|
|
goto module_decl;
|
|
|
|
|
|
|
|
// Note: no need to handle kw_import here. We only form kw_import under
|
|
|
|
// the Modules TS, and in that case 'export import' is parsed as an
|
|
|
|
// export-declaration containing an import-declaration.
|
|
|
|
|
|
|
|
// Recognize context-sensitive C++20 'export module' and 'export import'
|
|
|
|
// declarations.
|
|
|
|
case tok::identifier: {
|
|
|
|
IdentifierInfo *II = NextToken().getIdentifierInfo();
|
|
|
|
if ((II == Ident_module || II == Ident_import) &&
|
|
|
|
GetLookAheadToken(2).isNot(tok::coloncolon)) {
|
|
|
|
if (II == Ident_module)
|
|
|
|
goto module_decl;
|
|
|
|
else
|
|
|
|
goto import_decl;
|
|
|
|
}
|
2017-04-22 06:39:18 +08:00
|
|
|
break;
|
2019-04-14 16:06:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-04-22 06:39:18 +08:00
|
|
|
case tok::kw_module:
|
2019-04-14 16:06:59 +08:00
|
|
|
module_decl:
|
|
|
|
Result = ParseModuleDecl(IsFirstDecl);
|
2017-04-22 06:39:18 +08:00
|
|
|
return false;
|
|
|
|
|
2019-04-14 16:06:59 +08:00
|
|
|
// tok::kw_import is handled by ParseExternalDeclaration. (Under the Modules
|
|
|
|
// TS, an import can occur within an export block.)
|
|
|
|
import_decl: {
|
|
|
|
Decl *ImportDecl = ParseModuleImport(SourceLocation());
|
|
|
|
Result = Actions.ConvertDeclToDeclGroup(ImportDecl);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-15 12:24:58 +08:00
|
|
|
case tok::annot_module_include:
|
|
|
|
Actions.ActOnModuleInclude(Tok.getLocation(),
|
|
|
|
reinterpret_cast<Module *>(
|
|
|
|
Tok.getAnnotationValue()));
|
2017-05-19 03:21:48 +08:00
|
|
|
ConsumeAnnotationToken();
|
2013-11-15 12:24:58 +08:00
|
|
|
return false;
|
|
|
|
|
2013-11-23 12:06:09 +08:00
|
|
|
case tok::annot_module_begin:
|
2015-05-16 04:05:43 +08:00
|
|
|
Actions.ActOnModuleBegin(Tok.getLocation(), reinterpret_cast<Module *>(
|
|
|
|
Tok.getAnnotationValue()));
|
2017-05-19 03:21:48 +08:00
|
|
|
ConsumeAnnotationToken();
|
2015-05-16 04:05:43 +08:00
|
|
|
return false;
|
|
|
|
|
2013-11-23 12:06:09 +08:00
|
|
|
case tok::annot_module_end:
|
2015-05-16 04:05:43 +08:00
|
|
|
Actions.ActOnModuleEnd(Tok.getLocation(), reinterpret_cast<Module *>(
|
|
|
|
Tok.getAnnotationValue()));
|
2017-05-19 03:21:48 +08:00
|
|
|
ConsumeAnnotationToken();
|
2013-11-23 12:06:09 +08:00
|
|
|
return false;
|
|
|
|
|
2013-11-15 12:24:58 +08:00
|
|
|
case tok::eof:
|
2020-02-04 09:07:41 +08:00
|
|
|
// Check whether -fmax-tokens= was reached.
|
2019-10-09 21:22:38 +08:00
|
|
|
if (PP.getMaxTokens() != 0 && PP.getTokenCount() > PP.getMaxTokens()) {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_max_tokens_total)
|
|
|
|
<< PP.getTokenCount() << PP.getMaxTokens();
|
|
|
|
SourceLocation OverrideLoc = PP.getMaxTokensOverrideLoc();
|
|
|
|
if (OverrideLoc.isValid()) {
|
|
|
|
PP.Diag(OverrideLoc, diag::note_max_tokens_total_override);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-23 06:18:13 +08:00
|
|
|
// Late template parsing can begin.
|
2020-06-09 23:13:34 +08:00
|
|
|
Actions.SetLateTemplateParser(LateTemplateParserCallback, nullptr, this);
|
2012-03-16 18:40:17 +08:00
|
|
|
if (!PP.isIncrementalProcessingEnabled())
|
|
|
|
Actions.ActOnEndOfTranslationUnit();
|
|
|
|
//else don't tell Sema that we ended parsing: more input might come.
|
2008-08-23 11:19:52 +08:00
|
|
|
return true;
|
2013-11-15 12:24:58 +08:00
|
|
|
|
2019-04-14 16:06:59 +08:00
|
|
|
case tok::identifier:
|
|
|
|
// C++2a [basic.link]p3:
|
|
|
|
// A token sequence beginning with 'export[opt] module' or
|
|
|
|
// 'export[opt] import' and not immediately followed by '::'
|
|
|
|
// is never interpreted as the declaration of a top-level-declaration.
|
|
|
|
if ((Tok.getIdentifierInfo() == Ident_module ||
|
|
|
|
Tok.getIdentifierInfo() == Ident_import) &&
|
|
|
|
NextToken().isNot(tok::coloncolon)) {
|
|
|
|
if (Tok.getIdentifierInfo() == Ident_module)
|
|
|
|
goto module_decl;
|
|
|
|
else
|
|
|
|
goto import_decl;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-11-15 12:24:58 +08:00
|
|
|
default:
|
|
|
|
break;
|
2008-08-23 11:19:52 +08:00
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2011-03-24 19:26:52 +08:00
|
|
|
ParsedAttributesWithRange attrs(AttrFactory);
|
2013-01-02 20:01:23 +08:00
|
|
|
MaybeParseCXX11Attributes(attrs);
|
2012-03-16 18:40:17 +08:00
|
|
|
|
2010-12-24 10:08:15 +08:00
|
|
|
Result = ParseExternalDeclaration(attrs);
|
2006-08-17 15:04:37 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-07-31 09:59:18 +08:00
|
|
|
/// ParseExternalDeclaration:
|
2008-12-09 05:59:01 +08:00
|
|
|
///
|
2008-11-22 00:10:08 +08:00
|
|
|
/// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
|
2007-08-11 04:57:02 +08:00
|
|
|
/// function-definition
|
|
|
|
/// declaration
|
2006-08-15 11:41:14 +08:00
|
|
|
/// [GNU] asm-definition
|
2007-08-11 04:57:02 +08:00
|
|
|
/// [GNU] __extension__ external-declaration
|
2006-11-05 10:05:37 +08:00
|
|
|
/// [OBJC] objc-class-definition
|
|
|
|
/// [OBJC] objc-class-declaration
|
|
|
|
/// [OBJC] objc-alias-declaration
|
|
|
|
/// [OBJC] objc-protocol-definition
|
|
|
|
/// [OBJC] objc-method-definition
|
|
|
|
/// [OBJC] @end
|
2008-11-22 00:10:08 +08:00
|
|
|
/// [C++] linkage-specification
|
2006-08-15 11:41:14 +08:00
|
|
|
/// [GNU] asm-definition:
|
|
|
|
/// simple-asm-expr ';'
|
2013-02-21 03:22:51 +08:00
|
|
|
/// [C++11] empty-declaration
|
|
|
|
/// [C++11] attribute-declaration
|
2006-08-15 11:41:14 +08:00
|
|
|
///
|
2013-02-21 03:22:51 +08:00
|
|
|
/// [C++11] empty-declaration:
|
2009-08-24 20:17:54 +08:00
|
|
|
/// ';'
|
|
|
|
///
|
2009-09-04 14:33:52 +08:00
|
|
|
/// [C++0x/GNU] 'extern' 'template' declaration
|
2017-11-21 17:42:42 +08:00
|
|
|
///
|
|
|
|
/// [Modules-TS] module-import-declaration
|
|
|
|
///
|
2010-12-24 10:08:15 +08:00
|
|
|
Parser::DeclGroupPtrTy
|
|
|
|
Parser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
|
|
|
|
ParsingDeclSpec *DS) {
|
2020-04-06 14:10:08 +08:00
|
|
|
DestroyTemplateIdAnnotationsRAIIObj CleanupRAII(*this);
|
2010-06-17 18:52:18 +08:00
|
|
|
ParenBraceBracketBalancer BalancerRAIIObj(*this);
|
2011-09-04 11:32:15 +08:00
|
|
|
|
|
|
|
if (PP.isCodeCompletionReached()) {
|
|
|
|
cutOffParsing();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2011-09-04 11:32:15 +08:00
|
|
|
}
|
|
|
|
|
2014-05-21 14:02:52 +08:00
|
|
|
Decl *SingleDecl = nullptr;
|
2006-07-31 09:59:18 +08:00
|
|
|
switch (Tok.getKind()) {
|
2012-01-26 10:02:57 +08:00
|
|
|
case tok::annot_pragma_vis:
|
|
|
|
HandlePragmaVisibility();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2012-02-24 07:47:16 +08:00
|
|
|
case tok::annot_pragma_pack:
|
|
|
|
HandlePragmaPack();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2012-10-04 10:36:51 +08:00
|
|
|
case tok::annot_pragma_msstruct:
|
|
|
|
HandlePragmaMSStruct();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2012-10-04 10:36:51 +08:00
|
|
|
case tok::annot_pragma_align:
|
|
|
|
HandlePragmaAlign();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2012-10-04 10:36:51 +08:00
|
|
|
case tok::annot_pragma_weak:
|
|
|
|
HandlePragmaWeak();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2012-10-04 10:36:51 +08:00
|
|
|
case tok::annot_pragma_weakalias:
|
|
|
|
HandlePragmaWeakAlias();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2012-10-04 10:36:51 +08:00
|
|
|
case tok::annot_pragma_redefine_extname:
|
|
|
|
HandlePragmaRedefineExtname();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2012-10-04 10:36:51 +08:00
|
|
|
case tok::annot_pragma_fp_contract:
|
|
|
|
HandlePragmaFPContract();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2018-08-15 01:06:56 +08:00
|
|
|
case tok::annot_pragma_fenv_access:
|
|
|
|
HandlePragmaFEnvAccess();
|
|
|
|
return nullptr;
|
2020-05-02 01:32:06 +08:00
|
|
|
case tok::annot_pragma_float_control:
|
|
|
|
HandlePragmaFloatControl();
|
|
|
|
return nullptr;
|
2017-04-05 05:18:36 +08:00
|
|
|
case tok::annot_pragma_fp:
|
|
|
|
HandlePragmaFP();
|
|
|
|
break;
|
2012-10-04 10:36:51 +08:00
|
|
|
case tok::annot_pragma_opencl_extension:
|
|
|
|
HandlePragmaOpenCLExtension();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2016-03-30 18:43:55 +08:00
|
|
|
case tok::annot_pragma_openmp: {
|
|
|
|
AccessSpecifier AS = AS_none;
|
|
|
|
return ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, attrs);
|
|
|
|
}
|
2014-02-11 03:50:15 +08:00
|
|
|
case tok::annot_pragma_ms_pointers_to_members:
|
|
|
|
HandlePragmaMSPointersToMembers();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2014-02-13 07:50:26 +08:00
|
|
|
case tok::annot_pragma_ms_vtordisp:
|
|
|
|
HandlePragmaMSVtorDisp();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2014-04-09 06:30:47 +08:00
|
|
|
case tok::annot_pragma_ms_pragma:
|
|
|
|
HandlePragmaMSPragma();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2016-01-13 05:59:26 +08:00
|
|
|
case tok::annot_pragma_dump:
|
|
|
|
HandlePragmaDump();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2019-03-14 02:30:59 +08:00
|
|
|
case tok::annot_pragma_attribute:
|
|
|
|
HandlePragmaAttribute();
|
|
|
|
return nullptr;
|
2006-07-31 09:59:18 +08:00
|
|
|
case tok::semi:
|
2013-02-21 03:22:51 +08:00
|
|
|
// Either a C++11 empty-declaration or attribute-declaration.
|
2018-07-13 05:09:05 +08:00
|
|
|
SingleDecl =
|
|
|
|
Actions.ActOnEmptyDeclaration(getCurScope(), attrs, Tok.getLocation());
|
2012-05-17 03:04:59 +08:00
|
|
|
ConsumeExtraSemi(OutsideFunction);
|
2013-02-23 01:15:32 +08:00
|
|
|
break;
|
2008-12-09 05:59:01 +08:00
|
|
|
case tok::r_brace:
|
2012-01-17 09:04:27 +08:00
|
|
|
Diag(Tok, diag::err_extraneous_closing_brace);
|
2008-12-09 05:59:01 +08:00
|
|
|
ConsumeBrace();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2008-12-09 05:59:01 +08:00
|
|
|
case tok::eof:
|
|
|
|
Diag(Tok, diag::err_expected_external_declaration);
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2007-08-11 04:57:02 +08:00
|
|
|
case tok::kw___extension__: {
|
2008-10-20 14:45:43 +08:00
|
|
|
// __extension__ silences extension warnings in the subexpression.
|
|
|
|
ExtensionRAIIObject O(Diags); // Use RAII to do this.
|
2008-10-20 14:51:33 +08:00
|
|
|
ConsumeToken();
|
2010-12-24 10:08:15 +08:00
|
|
|
return ParseExternalDeclaration(attrs);
|
2007-08-11 04:57:02 +08:00
|
|
|
}
|
2008-02-08 08:33:21 +08:00
|
|
|
case tok::kw_asm: {
|
2010-12-24 10:08:15 +08:00
|
|
|
ProhibitAttributes(attrs);
|
2009-11-21 16:43:09 +08:00
|
|
|
|
2011-03-03 22:20:18 +08:00
|
|
|
SourceLocation StartLoc = Tok.getLocation();
|
|
|
|
SourceLocation EndLoc;
|
2015-05-12 05:14:09 +08:00
|
|
|
|
2020-01-08 21:38:02 +08:00
|
|
|
ExprResult Result(ParseSimpleAsm(/*ForAsmLabel*/ false, &EndLoc));
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2015-05-12 08:16:37 +08:00
|
|
|
// Check if GNU-style InlineAsm is disabled.
|
|
|
|
// Empty asm string is allowed because it will not introduce
|
|
|
|
// any assembly code.
|
|
|
|
if (!(getLangOpts().GNUAsm || Result.isInvalid())) {
|
|
|
|
const auto *SL = cast<StringLiteral>(Result.get());
|
|
|
|
if (!SL->getString().trim().empty())
|
|
|
|
Diag(StartLoc, diag::err_gnu_inline_asm_disabled);
|
|
|
|
}
|
|
|
|
|
2014-01-01 11:08:43 +08:00
|
|
|
ExpectAndConsume(tok::semi, diag::err_expected_after,
|
2008-02-08 08:23:11 +08:00
|
|
|
"top-level asm block");
|
2008-02-08 08:33:21 +08:00
|
|
|
|
2009-03-30 00:50:03 +08:00
|
|
|
if (Result.isInvalid())
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2011-03-03 22:20:18 +08:00
|
|
|
SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc);
|
2009-03-30 00:50:03 +08:00
|
|
|
break;
|
2008-02-08 08:33:21 +08:00
|
|
|
}
|
2006-10-28 07:18:49 +08:00
|
|
|
case tok::at:
|
2018-02-12 21:38:25 +08:00
|
|
|
return ParseObjCAtDirectives(attrs);
|
2006-10-28 07:18:49 +08:00
|
|
|
case tok::minus:
|
|
|
|
case tok::plus:
|
2018-10-31 04:31:30 +08:00
|
|
|
if (!getLangOpts().ObjC) {
|
2009-03-30 00:50:03 +08:00
|
|
|
Diag(Tok, diag::err_expected_external_declaration);
|
|
|
|
ConsumeToken();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2009-03-30 00:50:03 +08:00
|
|
|
}
|
|
|
|
SingleDecl = ParseObjCMethodDefinition();
|
|
|
|
break;
|
2009-09-22 04:51:25 +08:00
|
|
|
case tok::code_completion:
|
2017-10-25 00:39:37 +08:00
|
|
|
if (CurParsedObjCImpl) {
|
|
|
|
// Code-complete Objective-C methods even without leading '-'/'+' prefix.
|
|
|
|
Actions.CodeCompleteObjCMethodDecl(getCurScope(),
|
|
|
|
/*IsInstanceMethod=*/None,
|
|
|
|
/*ReturnType=*/nullptr);
|
|
|
|
}
|
|
|
|
Actions.CodeCompleteOrdinaryName(
|
|
|
|
getCurScope(),
|
|
|
|
CurParsedObjCImpl ? Sema::PCC_ObjCImplementation : Sema::PCC_Namespace);
|
2011-09-04 11:32:15 +08:00
|
|
|
cutOffParsing();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2017-11-21 17:42:42 +08:00
|
|
|
case tok::kw_import:
|
|
|
|
SingleDecl = ParseModuleImport(SourceLocation());
|
|
|
|
break;
|
2016-09-09 07:14:54 +08:00
|
|
|
case tok::kw_export:
|
2019-04-14 19:11:37 +08:00
|
|
|
if (getLangOpts().CPlusPlusModules || getLangOpts().ModulesTS) {
|
2016-09-09 07:14:54 +08:00
|
|
|
SingleDecl = ParseExportDeclaration();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// This must be 'export template'. Parse it so we can diagnose our lack
|
|
|
|
// of support.
|
2017-06-03 14:29:16 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2008-12-30 11:27:21 +08:00
|
|
|
case tok::kw_using:
|
2007-08-25 14:57:03 +08:00
|
|
|
case tok::kw_namespace:
|
2006-11-19 10:31:38 +08:00
|
|
|
case tok::kw_typedef:
|
2008-12-02 07:54:00 +08:00
|
|
|
case tok::kw_template:
|
2009-03-12 00:27:10 +08:00
|
|
|
case tok::kw_static_assert:
|
2011-04-15 08:35:57 +08:00
|
|
|
case tok::kw__Static_assert:
|
2012-04-26 06:51:41 +08:00
|
|
|
// A function definition cannot start with any of these keywords.
|
fix a FIXME, providing accurate source range info for DeclStmt's. The end
of the range is now the ';' location. For something like this:
$ cat t2.c
#define bool int
void f(int x, int y) {
bool b = !x && y;
}
We used to produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14> <----
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <line:4:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
Now we produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14, line:4:17> <------
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <col:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
llvm-svn: 68288
2009-04-02 12:16:50 +08:00
|
|
|
{
|
|
|
|
SourceLocation DeclEnd;
|
2017-12-29 13:41:00 +08:00
|
|
|
return ParseDeclaration(DeclaratorContext::FileContext, DeclEnd, attrs);
|
fix a FIXME, providing accurate source range info for DeclStmt's. The end
of the range is now the ';' location. For something like this:
$ cat t2.c
#define bool int
void f(int x, int y) {
bool b = !x && y;
}
We used to produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14> <----
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <line:4:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
Now we produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14, line:4:17> <------
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <col:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
llvm-svn: 68288
2009-04-02 12:16:50 +08:00
|
|
|
}
|
2010-08-28 07:12:46 +08:00
|
|
|
|
2010-12-02 04:32:20 +08:00
|
|
|
case tok::kw_static:
|
|
|
|
// Parse (then ignore) 'static' prior to a template instantiation. This is
|
|
|
|
// a GCC extension that we intentionally do not support.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
|
2010-12-02 04:32:20 +08:00
|
|
|
Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
|
|
|
|
<< 0;
|
2010-08-28 07:12:46 +08:00
|
|
|
SourceLocation DeclEnd;
|
2017-12-29 13:41:00 +08:00
|
|
|
return ParseDeclaration(DeclaratorContext::FileContext, DeclEnd, attrs);
|
2010-12-02 04:32:20 +08:00
|
|
|
}
|
|
|
|
goto dont_know;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-12-02 04:32:20 +08:00
|
|
|
case tok::kw_inline:
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().CPlusPlus) {
|
2010-12-02 04:32:20 +08:00
|
|
|
tok::TokenKind NextKind = NextToken().getKind();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-12-02 04:32:20 +08:00
|
|
|
// Inline namespaces. Allowed as an extension even in C++03.
|
|
|
|
if (NextKind == tok::kw_namespace) {
|
|
|
|
SourceLocation DeclEnd;
|
2017-12-29 13:41:00 +08:00
|
|
|
return ParseDeclaration(DeclaratorContext::FileContext, DeclEnd, attrs);
|
2010-12-02 04:32:20 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-12-02 04:32:20 +08:00
|
|
|
// Parse (then ignore) 'inline' prior to a template instantiation. This is
|
|
|
|
// a GCC extension that we intentionally do not support.
|
|
|
|
if (NextKind == tok::kw_template) {
|
|
|
|
Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
|
|
|
|
<< 1;
|
|
|
|
SourceLocation DeclEnd;
|
2017-12-29 13:41:00 +08:00
|
|
|
return ParseDeclaration(DeclaratorContext::FileContext, DeclEnd, attrs);
|
2010-12-02 04:32:20 +08:00
|
|
|
}
|
2010-08-28 07:12:46 +08:00
|
|
|
}
|
|
|
|
goto dont_know;
|
|
|
|
|
2009-09-04 14:33:52 +08:00
|
|
|
case tok::kw_extern:
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
|
2009-09-04 14:33:52 +08:00
|
|
|
// Extern templates
|
|
|
|
SourceLocation ExternLoc = ConsumeToken();
|
|
|
|
SourceLocation TemplateLoc = ConsumeToken();
|
2013-01-02 19:42:31 +08:00
|
|
|
Diag(ExternLoc, getLangOpts().CPlusPlus11 ?
|
2011-10-21 02:35:58 +08:00
|
|
|
diag::warn_cxx98_compat_extern_template :
|
|
|
|
diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc);
|
2009-09-04 14:33:52 +08:00
|
|
|
SourceLocation DeclEnd;
|
|
|
|
return Actions.ConvertDeclToDeclGroup(
|
2018-07-13 05:09:05 +08:00
|
|
|
ParseExplicitInstantiation(DeclaratorContext::FileContext, ExternLoc,
|
|
|
|
TemplateLoc, DeclEnd, attrs));
|
2009-09-04 14:33:52 +08:00
|
|
|
}
|
2010-08-28 07:12:46 +08:00
|
|
|
goto dont_know;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-05-08 01:30:27 +08:00
|
|
|
case tok::kw___if_exists:
|
|
|
|
case tok::kw___if_not_exists:
|
2011-05-25 18:19:49 +08:00
|
|
|
ParseMicrosoftIfExistsExternalDeclaration();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2016-08-26 08:14:38 +08:00
|
|
|
case tok::kw_module:
|
|
|
|
Diag(Tok, diag::err_unexpected_module_decl);
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
return nullptr;
|
|
|
|
|
2006-07-31 09:59:18 +08:00
|
|
|
default:
|
2010-08-28 07:12:46 +08:00
|
|
|
dont_know:
|
2017-04-19 16:58:56 +08:00
|
|
|
if (Tok.isEditorPlaceholder()) {
|
|
|
|
ConsumeToken();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2006-07-31 09:59:18 +08:00
|
|
|
// We can't tell whether this is a function-definition or declaration yet.
|
2012-12-29 09:09:46 +08:00
|
|
|
return ParseDeclarationOrFunctionDefinition(attrs, DS);
|
2006-07-31 09:59:18 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-30 00:50:03 +08:00
|
|
|
// This routine returns a DeclGroup, if the thing we parsed only contains a
|
|
|
|
// single decl, convert it now.
|
|
|
|
return Actions.ConvertDeclToDeclGroup(SingleDecl);
|
2006-07-31 09:59:18 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Determine whether the current token, if it occurs after a
|
2009-05-13 05:31:51 +08:00
|
|
|
/// declarator, continues a declaration or declaration list.
|
2011-05-12 14:15:49 +08:00
|
|
|
bool Parser::isDeclarationAfterDeclarator() {
|
|
|
|
// Check for '= delete' or '= default'
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
|
2011-05-12 14:15:49 +08:00
|
|
|
const Token &KW = NextToken();
|
|
|
|
if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2009-05-13 05:31:51 +08:00
|
|
|
return Tok.is(tok::equal) || // int X()= -> not a function def
|
|
|
|
Tok.is(tok::comma) || // int X(), -> not a function def
|
|
|
|
Tok.is(tok::semi) || // int X(); -> not a function def
|
|
|
|
Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def
|
|
|
|
Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def
|
2012-03-11 15:00:24 +08:00
|
|
|
(getLangOpts().CPlusPlus &&
|
2012-07-06 03:34:20 +08:00
|
|
|
Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++]
|
2009-05-13 05:31:51 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Determine whether the current token, if it occurs after a
|
2009-05-13 05:31:51 +08:00
|
|
|
/// declarator, indicates the start of a function definition.
|
2010-07-12 06:42:07 +08:00
|
|
|
bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
|
2010-12-11 00:29:40 +08:00
|
|
|
assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
|
2009-12-07 02:34:27 +08:00
|
|
|
if (Tok.is(tok::l_brace)) // int X() {}
|
|
|
|
return true;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-07-12 06:42:07 +08:00
|
|
|
// Handle K&R C argument lists: int X(f) int f; {}
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!getLangOpts().CPlusPlus &&
|
2018-07-31 03:24:48 +08:00
|
|
|
Declarator.getFunctionTypeInfo().isKNRPrototype())
|
2010-07-12 06:42:07 +08:00
|
|
|
return isDeclarationSpecifier();
|
2011-05-12 14:15:49 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
|
2011-05-12 14:15:49 +08:00
|
|
|
const Token &KW = NextToken();
|
|
|
|
return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2009-12-07 02:34:27 +08:00
|
|
|
return Tok.is(tok::colon) || // X() : Base() {} (used for ctors)
|
|
|
|
Tok.is(tok::kw_try); // X() try { ... }
|
2009-05-13 05:31:51 +08:00
|
|
|
}
|
|
|
|
|
2016-09-03 10:41:17 +08:00
|
|
|
/// Parse either a function-definition or a declaration. We can't tell which
|
|
|
|
/// we have until we read up to the compound-statement in function-definition.
|
|
|
|
/// TemplateParams, if non-NULL, provides the template parameters when we're
|
|
|
|
/// parsing a C++ template-declaration.
|
2006-07-31 09:59:18 +08:00
|
|
|
///
|
2006-07-31 13:09:04 +08:00
|
|
|
/// function-definition: [C99 6.9.1]
|
2008-04-05 13:52:15 +08:00
|
|
|
/// decl-specs declarator declaration-list[opt] compound-statement
|
|
|
|
/// [C90] function-definition: [C99 6.7.1] - implicit int result
|
2008-06-20 03:28:49 +08:00
|
|
|
/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
|
2008-04-05 13:52:15 +08:00
|
|
|
///
|
2006-07-31 13:09:04 +08:00
|
|
|
/// declaration: [C99 6.7]
|
2007-08-22 14:06:56 +08:00
|
|
|
/// declaration-specifiers init-declarator-list[opt] ';'
|
|
|
|
/// [!C99] init-declarator-list ';' [TODO: warn in c99 mode]
|
2019-03-08 01:54:44 +08:00
|
|
|
/// [OMP] threadprivate-directive
|
|
|
|
/// [OMP] allocate-directive [TODO]
|
2006-07-31 13:09:04 +08:00
|
|
|
///
|
2009-03-30 00:50:03 +08:00
|
|
|
Parser::DeclGroupPtrTy
|
2012-06-23 13:07:58 +08:00
|
|
|
Parser::ParseDeclOrFunctionDefInternal(ParsedAttributesWithRange &attrs,
|
|
|
|
ParsingDeclSpec &DS,
|
|
|
|
AccessSpecifier AS) {
|
2016-09-03 10:48:03 +08:00
|
|
|
MaybeParseMicrosoftAttributes(DS.getAttributes());
|
2006-07-31 13:09:04 +08:00
|
|
|
// Parse the common declaration-specifiers piece.
|
2018-04-26 08:42:40 +08:00
|
|
|
ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS,
|
2017-12-31 08:06:40 +08:00
|
|
|
DeclSpecContext::DSC_top_level);
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2013-11-20 06:47:36 +08:00
|
|
|
// If we had a free-standing type definition with a missing semicolon, we
|
|
|
|
// may get this far before the problem becomes obvious.
|
2017-12-31 08:06:40 +08:00
|
|
|
if (DS.hasTagDefinition() && DiagnoseMissingSemiAfterTagDefinition(
|
|
|
|
DS, AS, DeclSpecContext::DSC_top_level))
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2013-11-20 06:47:36 +08:00
|
|
|
|
2006-08-05 16:09:44 +08:00
|
|
|
// C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
|
2006-08-10 13:19:57 +08:00
|
|
|
// declaration-specifiers init-declarator-list[opt] ';'
|
2007-10-10 01:23:58 +08:00
|
|
|
if (Tok.is(tok::semi)) {
|
2017-12-26 06:23:20 +08:00
|
|
|
auto LengthOfTSTToken = [](DeclSpec::TST TKind) {
|
|
|
|
assert(DeclSpec::isDeclRep(TKind));
|
|
|
|
switch(TKind) {
|
|
|
|
case DeclSpec::TST_class:
|
|
|
|
return 5;
|
|
|
|
case DeclSpec::TST_struct:
|
|
|
|
return 6;
|
|
|
|
case DeclSpec::TST_union:
|
|
|
|
return 5;
|
|
|
|
case DeclSpec::TST_enum:
|
|
|
|
return 4;
|
|
|
|
case DeclSpec::TST_interface:
|
|
|
|
return 9;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("we only expect to get the length of the class/struct/union/enum");
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2017-12-26 06:23:20 +08:00
|
|
|
};
|
|
|
|
// Suggest correct location to fix '[[attrib]] struct' to 'struct [[attrib]]'
|
|
|
|
SourceLocation CorrectLocationForAttributes =
|
|
|
|
DeclSpec::isDeclRep(DS.getTypeSpecType())
|
|
|
|
? DS.getTypeSpecTypeLoc().getLocWithOffset(
|
|
|
|
LengthOfTSTToken(DS.getTypeSpecType()))
|
|
|
|
: SourceLocation();
|
|
|
|
ProhibitAttributes(attrs, CorrectLocationForAttributes);
|
2006-08-14 03:58:17 +08:00
|
|
|
ConsumeToken();
|
2016-01-29 03:25:00 +08:00
|
|
|
RecordDecl *AnonRecord = nullptr;
|
|
|
|
Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
|
|
|
|
DS, AnonRecord);
|
2009-11-04 10:18:39 +08:00
|
|
|
DS.complete(TheDecl);
|
2016-12-18 13:18:55 +08:00
|
|
|
if (getLangOpts().OpenCL)
|
|
|
|
Actions.setCurrentOpenCLExtensionForDecl(TheDecl);
|
2016-01-29 03:25:00 +08:00
|
|
|
if (AnonRecord) {
|
|
|
|
Decl* decls[] = {AnonRecord, TheDecl};
|
2017-01-12 10:27:38 +08:00
|
|
|
return Actions.BuildDeclaratorGroup(decls);
|
2016-01-29 03:25:00 +08:00
|
|
|
}
|
2009-03-30 00:50:03 +08:00
|
|
|
return Actions.ConvertDeclToDeclGroup(TheDecl);
|
2006-08-14 03:58:17 +08:00
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2012-06-23 13:07:58 +08:00
|
|
|
DS.takeAttributesFrom(attrs);
|
|
|
|
|
2008-09-26 12:48:09 +08:00
|
|
|
// ObjC2 allows prefix attributes on class interfaces and protocols.
|
|
|
|
// FIXME: This still needs better diagnostics. We should only accept
|
|
|
|
// attributes here, no types, etc.
|
2018-10-31 04:31:30 +08:00
|
|
|
if (getLangOpts().ObjC && Tok.is(tok::at)) {
|
2007-08-21 05:31:48 +08:00
|
|
|
SourceLocation AtLoc = ConsumeToken(); // the "@"
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
|
2019-04-12 01:55:30 +08:00
|
|
|
!Tok.isObjCAtKeyword(tok::objc_protocol) &&
|
|
|
|
!Tok.isObjCAtKeyword(tok::objc_implementation)) {
|
2008-09-26 12:48:09 +08:00
|
|
|
Diag(Tok, diag::err_objc_unexpected_attr);
|
2019-04-12 01:55:30 +08:00
|
|
|
SkipUntil(tok::semi);
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2007-12-28 03:57:00 +08:00
|
|
|
}
|
2009-11-04 03:26:08 +08:00
|
|
|
|
2009-11-04 10:18:39 +08:00
|
|
|
DS.abort();
|
|
|
|
|
2014-05-21 14:02:52 +08:00
|
|
|
const char *PrevSpec = nullptr;
|
2009-08-04 04:12:06 +08:00
|
|
|
unsigned DiagID;
|
2014-01-15 17:15:43 +08:00
|
|
|
if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID,
|
|
|
|
Actions.getASTContext().getPrintingPolicy()))
|
2009-08-04 04:12:06 +08:00
|
|
|
Diag(AtLoc, DiagID) << PrevSpec;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-26 12:48:09 +08:00
|
|
|
if (Tok.isObjCAtKeyword(tok::objc_protocol))
|
2012-01-02 05:23:57 +08:00
|
|
|
return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
|
|
|
|
|
2019-04-12 01:55:30 +08:00
|
|
|
if (Tok.isObjCAtKeyword(tok::objc_implementation))
|
|
|
|
return ParseObjCAtImplementationDeclaration(AtLoc, DS.getAttributes());
|
|
|
|
|
2012-01-02 05:23:57 +08:00
|
|
|
return Actions.ConvertDeclToDeclGroup(
|
|
|
|
ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()));
|
2007-08-21 05:31:48 +08:00
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2008-01-12 15:05:38 +08:00
|
|
|
// If the declspec consisted only of 'extern' and we have a string
|
|
|
|
// literal following it, this must be a C++ linkage specifier like
|
|
|
|
// 'extern "C"'.
|
2014-02-18 07:25:27 +08:00
|
|
|
if (getLangOpts().CPlusPlus && isTokenStringLiteral() &&
|
2008-01-12 15:05:38 +08:00
|
|
|
DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
|
2009-03-30 00:50:03 +08:00
|
|
|
DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
|
2017-12-29 13:41:00 +08:00
|
|
|
Decl *TheDecl = ParseLinkage(DS, DeclaratorContext::FileContext);
|
2009-03-30 00:50:03 +08:00
|
|
|
return Actions.ConvertDeclToDeclGroup(TheDecl);
|
|
|
|
}
|
2008-01-12 15:05:38 +08:00
|
|
|
|
2017-12-29 13:41:00 +08:00
|
|
|
return ParseDeclGroup(DS, DeclaratorContext::FileContext);
|
2006-07-31 13:09:04 +08:00
|
|
|
}
|
|
|
|
|
2009-12-10 05:39:38 +08:00
|
|
|
Parser::DeclGroupPtrTy
|
2012-06-23 13:07:58 +08:00
|
|
|
Parser::ParseDeclarationOrFunctionDefinition(ParsedAttributesWithRange &attrs,
|
|
|
|
ParsingDeclSpec *DS,
|
2009-12-10 05:39:38 +08:00
|
|
|
AccessSpecifier AS) {
|
2012-06-23 13:07:58 +08:00
|
|
|
if (DS) {
|
|
|
|
return ParseDeclOrFunctionDefInternal(attrs, *DS, AS);
|
|
|
|
} else {
|
|
|
|
ParsingDeclSpec PDS(*this);
|
|
|
|
// Must temporarily exit the objective-c container scope for
|
|
|
|
// parsing c constructs and re-enter objc container scope
|
|
|
|
// afterwards.
|
|
|
|
ObjCDeclContextSwitch ObjCDC(*this);
|
2016-09-03 10:41:17 +08:00
|
|
|
|
2012-06-23 13:07:58 +08:00
|
|
|
return ParseDeclOrFunctionDefInternal(attrs, PDS, AS);
|
|
|
|
}
|
2009-12-10 05:39:38 +08:00
|
|
|
}
|
|
|
|
|
2006-08-07 14:31:38 +08:00
|
|
|
/// ParseFunctionDefinition - We parsed and verified that the specified
|
|
|
|
/// Declarator is well formed. If this is a K&R-style function, read the
|
|
|
|
/// parameters declaration-list, then start the compound-statement.
|
|
|
|
///
|
2008-04-05 13:52:15 +08:00
|
|
|
/// function-definition: [C99 6.9.1]
|
|
|
|
/// decl-specs declarator declaration-list[opt] compound-statement
|
|
|
|
/// [C90] function-definition: [C99 6.7.1] - implicit int result
|
2008-06-20 03:28:49 +08:00
|
|
|
/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
|
2008-11-05 12:29:56 +08:00
|
|
|
/// [C++] function-definition: [C++ 8.4]
|
2009-03-30 01:18:04 +08:00
|
|
|
/// decl-specifier-seq[opt] declarator ctor-initializer[opt]
|
|
|
|
/// function-body
|
2008-11-05 12:29:56 +08:00
|
|
|
/// [C++] function-definition: [C++ 8.4]
|
2009-04-27 04:35:05 +08:00
|
|
|
/// decl-specifier-seq[opt] declarator function-try-block
|
2006-08-07 14:31:38 +08:00
|
|
|
///
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
|
2012-02-17 00:50:43 +08:00
|
|
|
const ParsedTemplateInfo &TemplateInfo,
|
|
|
|
LateParsedAttrList *LateParsedAttrs) {
|
2015-01-04 08:47:22 +08:00
|
|
|
// Poison SEH identifiers so they are flagged as illegal in function bodies.
|
2011-04-28 09:08:34 +08:00
|
|
|
PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
|
2010-12-11 00:29:40 +08:00
|
|
|
const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
|
2020-01-22 08:03:05 +08:00
|
|
|
TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2008-04-05 13:52:15 +08:00
|
|
|
// If this is C90 and the declspecs were completely missing, fudge in an
|
|
|
|
// implicit int. We do this here because this is the only place where
|
|
|
|
// declaration-specifiers are completely optional in the grammar.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ImplicitInt && D.getDeclSpec().isEmpty()) {
|
2008-04-05 13:52:15 +08:00
|
|
|
const char *PrevSpec;
|
2009-08-04 04:12:06 +08:00
|
|
|
unsigned DiagID;
|
2014-01-15 17:15:43 +08:00
|
|
|
const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
|
2008-10-20 10:01:34 +08:00
|
|
|
D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
|
|
|
|
D.getIdentifierLoc(),
|
2014-01-15 17:15:43 +08:00
|
|
|
PrevSpec, DiagID,
|
|
|
|
Policy);
|
2009-02-10 02:23:29 +08:00
|
|
|
D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
|
2008-04-05 13:52:15 +08:00
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2006-08-07 14:31:38 +08:00
|
|
|
// If this declaration was formed with a K&R-style identifier list for the
|
|
|
|
// arguments, parse declarations for all of the args next.
|
|
|
|
// int foo(a,b) int a; float b; {}
|
2010-07-12 06:42:07 +08:00
|
|
|
if (FTI.isKNRPrototype())
|
2006-12-03 16:41:30 +08:00
|
|
|
ParseKNRParamDeclarations(D);
|
2006-08-07 14:31:38 +08:00
|
|
|
|
2008-11-05 12:29:56 +08:00
|
|
|
// We should have either an opening brace or, in a C++ constructor,
|
|
|
|
// we may have a colon.
|
2018-07-31 03:24:48 +08:00
|
|
|
if (Tok.isNot(tok::l_brace) &&
|
2012-03-11 15:00:24 +08:00
|
|
|
(!getLangOpts().CPlusPlus ||
|
2011-05-24 07:14:04 +08:00
|
|
|
(Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) &&
|
|
|
|
Tok.isNot(tok::equal)))) {
|
2006-08-09 13:47:47 +08:00
|
|
|
Diag(Tok, diag::err_expected_fn_body);
|
|
|
|
|
|
|
|
// Skip over garbage, until we get to '{'. Don't eat the '{'.
|
2013-11-18 16:17:37 +08:00
|
|
|
SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2006-08-09 13:47:47 +08:00
|
|
|
// If we didn't find the '{', bail out.
|
2007-10-10 01:23:58 +08:00
|
|
|
if (Tok.isNot(tok::l_brace))
|
2014-05-21 14:02:52 +08:00
|
|
|
return nullptr;
|
2006-08-09 13:47:47 +08:00
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2012-02-17 00:50:43 +08:00
|
|
|
// Check to make sure that any normal attributes are allowed to be on
|
|
|
|
// a definition. Late parsed attributes are checked at the end.
|
|
|
|
if (Tok.isNot(tok::equal)) {
|
2018-07-13 23:07:47 +08:00
|
|
|
for (const ParsedAttr &AL : D.getAttributes())
|
2018-07-13 05:09:05 +08:00
|
|
|
if (AL.isKnownToGCC() && !AL.isCXX11Attribute())
|
2019-09-14 01:39:31 +08:00
|
|
|
Diag(AL.getLoc(), diag::warn_attribute_on_function_definition) << AL;
|
2012-02-17 00:50:43 +08:00
|
|
|
}
|
|
|
|
|
2011-04-23 06:18:13 +08:00
|
|
|
// In delayed template parsing mode, for function template we consume the
|
|
|
|
// tokens and store them for late parsing at the end of the translation unit.
|
2013-10-24 05:31:20 +08:00
|
|
|
if (getLangOpts().DelayedTemplateParsing && Tok.isNot(tok::equal) &&
|
|
|
|
TemplateInfo.Kind == ParsedTemplateInfo::Template &&
|
2014-03-13 07:14:33 +08:00
|
|
|
Actions.canDelayFunctionBody(D)) {
|
2012-08-24 07:38:35 +08:00
|
|
|
MultiTemplateParamsArg TemplateParameterLists(*TemplateInfo.TemplateParams);
|
2017-08-10 23:43:06 +08:00
|
|
|
|
|
|
|
ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
|
|
|
|
Scope::CompoundStmtScope);
|
2011-04-23 06:18:13 +08:00
|
|
|
Scope *ParentScope = getCurScope()->getParent();
|
|
|
|
|
2011-11-08 04:56:01 +08:00
|
|
|
D.setFunctionDefinitionKind(FDK_Definition);
|
2011-04-23 06:18:13 +08:00
|
|
|
Decl *DP = Actions.HandleDeclarator(ParentScope, D,
|
2012-08-24 05:35:17 +08:00
|
|
|
TemplateParameterLists);
|
2011-04-23 06:18:13 +08:00
|
|
|
D.complete(DP);
|
|
|
|
D.getMutableDeclSpec().abort();
|
|
|
|
|
2016-06-17 05:40:06 +08:00
|
|
|
if (SkipFunctionBodies && (!DP || Actions.canSkipFunctionBody(DP)) &&
|
|
|
|
trySkippingFunctionBody()) {
|
|
|
|
BodyScope.Exit();
|
|
|
|
return Actions.ActOnSkippedFunctionBody(DP);
|
|
|
|
}
|
|
|
|
|
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 (DP) {
|
2014-01-22 15:29:52 +08:00
|
|
|
FunctionDecl *FnD = DP->getAsFunction();
|
2013-08-08 05:41:30 +08:00
|
|
|
Actions.CheckForFunctionRedefinition(FnD);
|
|
|
|
Actions.MarkAsLateParsedTemplate(FnD, DP, Toks);
|
2011-04-23 06:18:13 +08:00
|
|
|
}
|
|
|
|
return DP;
|
2016-06-15 19:24:54 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
else if (CurParsedObjCImpl &&
|
2016-06-15 19:24:54 +08:00
|
|
|
!TemplateInfo.TemplateParams &&
|
|
|
|
(Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
|
2018-07-31 03:24:48 +08:00
|
|
|
Tok.is(tok::colon)) &&
|
2016-06-15 19:24:54 +08:00
|
|
|
Actions.CurContext->isTranslationUnit()) {
|
2017-08-10 23:43:06 +08:00
|
|
|
ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
|
|
|
|
Scope::CompoundStmtScope);
|
2012-08-10 23:54:40 +08:00
|
|
|
Scope *ParentScope = getCurScope()->getParent();
|
2014-03-13 07:14:33 +08:00
|
|
|
|
2012-08-10 23:54:40 +08:00
|
|
|
D.setFunctionDefinitionKind(FDK_Definition);
|
|
|
|
Decl *FuncDecl = Actions.HandleDeclarator(ParentScope, D,
|
2012-08-24 07:38:35 +08:00
|
|
|
MultiTemplateParamsArg());
|
2012-08-10 23:54:40 +08:00
|
|
|
D.complete(FuncDecl);
|
|
|
|
D.getMutableDeclSpec().abort();
|
|
|
|
if (FuncDecl) {
|
|
|
|
// Consume the tokens and store them for later parsing.
|
|
|
|
StashAwayMethodOrFunctionBodyTokens(FuncDecl);
|
|
|
|
CurParsedObjCImpl->HasCFunction = true;
|
|
|
|
return FuncDecl;
|
|
|
|
}
|
2014-03-13 07:14:33 +08:00
|
|
|
// FIXME: Should we really fall through here?
|
2012-08-10 23:54:40 +08:00
|
|
|
}
|
2014-03-13 07:14:33 +08:00
|
|
|
|
2007-10-10 01:14:05 +08:00
|
|
|
// Enter a scope for the function body.
|
2017-08-10 23:43:06 +08:00
|
|
|
ParseScope BodyScope(this, Scope::FnScope | Scope::DeclScope |
|
|
|
|
Scope::CompoundStmtScope);
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2007-10-10 01:14:05 +08:00
|
|
|
// Tell the actions module that we have entered a function definition with the
|
|
|
|
// specified Declarator for the function.
|
2015-08-21 11:04:33 +08:00
|
|
|
Sema::SkipBodyInfo SkipBody;
|
|
|
|
Decl *Res = Actions.ActOnStartOfFunctionDef(getCurScope(), D,
|
|
|
|
TemplateInfo.TemplateParams
|
|
|
|
? *TemplateInfo.TemplateParams
|
|
|
|
: MultiTemplateParamsArg(),
|
|
|
|
&SkipBody);
|
|
|
|
|
|
|
|
if (SkipBody.ShouldSkip) {
|
|
|
|
SkipFunctionBody();
|
|
|
|
return Res;
|
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2009-11-04 10:18:39 +08:00
|
|
|
// Break out of the ParsingDeclarator context before we parse the body.
|
|
|
|
D.complete(Res);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2009-11-04 10:18:39 +08:00
|
|
|
// Break out of the ParsingDeclSpec context, too. This const_cast is
|
|
|
|
// safe because we're always the sole owner.
|
|
|
|
D.getMutableDeclSpec().abort();
|
|
|
|
|
2020-01-22 08:03:05 +08:00
|
|
|
// With abbreviated function templates - we need to explicitly add depth to
|
|
|
|
// account for the implicit template parameter list induced by the template.
|
|
|
|
if (auto *Template = dyn_cast_or_null<FunctionTemplateDecl>(Res))
|
|
|
|
if (Template->isAbbreviated() &&
|
|
|
|
Template->getTemplateParameters()->getParam(0)->isImplicit())
|
|
|
|
// First template parameter is implicit - meaning no explicit template
|
|
|
|
// parameter list was specified.
|
|
|
|
CurTemplateDepthTracker.addDepth(1);
|
|
|
|
|
2014-01-01 11:08:43 +08:00
|
|
|
if (TryConsumeToken(tok::equal)) {
|
2012-03-11 15:00:24 +08:00
|
|
|
assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='");
|
2014-05-21 14:02:52 +08:00
|
|
|
|
2011-05-24 07:14:04 +08:00
|
|
|
bool Delete = false;
|
|
|
|
SourceLocation KWLoc;
|
2014-01-01 11:08:43 +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-24 07:14:04 +08:00
|
|
|
Actions.SetDeclDeleted(Res, KWLoc);
|
|
|
|
Delete = true;
|
2014-01-01 11:08:43 +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-24 07:14:04 +08:00
|
|
|
Actions.SetDeclDefaulted(Res, KWLoc);
|
|
|
|
} 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-24 07:14:04 +08:00
|
|
|
}
|
|
|
|
|
2015-03-12 22:28:47 +08:00
|
|
|
Stmt *GeneratedBody = Res ? Res->getBody() : nullptr;
|
|
|
|
Actions.ActOnFinishFunctionBody(Res, GeneratedBody, false);
|
2011-05-24 07:14:04 +08:00
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2016-06-17 05:40:06 +08:00
|
|
|
if (SkipFunctionBodies && (!Res || Actions.canSkipFunctionBody(Res)) &&
|
|
|
|
trySkippingFunctionBody()) {
|
|
|
|
BodyScope.Exit();
|
|
|
|
Actions.ActOnSkippedFunctionBody(Res);
|
|
|
|
return Actions.ActOnFinishFunctionBody(Res, nullptr, false);
|
|
|
|
}
|
|
|
|
|
2009-04-27 04:35:05 +08:00
|
|
|
if (Tok.is(tok::kw_try))
|
2011-03-17 01:05:57 +08:00
|
|
|
return ParseFunctionTryBlock(Res, BodyScope);
|
2009-04-27 04:35:05 +08:00
|
|
|
|
2008-11-05 12:29:56 +08:00
|
|
|
// If we have a colon, then we're probably parsing a C++
|
|
|
|
// ctor-initializer.
|
2010-04-10 15:37:23 +08:00
|
|
|
if (Tok.is(tok::colon)) {
|
2008-11-05 12:29:56 +08:00
|
|
|
ParseConstructorInitializer(Res);
|
2010-04-10 15:37:23 +08:00
|
|
|
|
|
|
|
// Recover from error.
|
|
|
|
if (!Tok.is(tok::l_brace)) {
|
2011-03-17 01:05:57 +08:00
|
|
|
BodyScope.Exit();
|
2014-05-21 14:02:52 +08:00
|
|
|
Actions.ActOnFinishFunctionBody(Res, nullptr);
|
2010-04-10 15:37:23 +08:00
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
} else
|
2009-07-22 06:36:06 +08:00
|
|
|
Actions.ActOnDefaultCtorInitializers(Res);
|
2008-11-05 12:29:56 +08:00
|
|
|
|
2012-02-17 00:50:43 +08:00
|
|
|
// Late attributes are parsed in the same scope as the function body.
|
|
|
|
if (LateParsedAttrs)
|
|
|
|
ParseLexedAttributeList(*LateParsedAttrs, Res, false, true);
|
|
|
|
|
2011-03-17 01:05:57 +08:00
|
|
|
return ParseFunctionStatementBody(Res, BodyScope);
|
2006-08-07 14:31:38 +08:00
|
|
|
}
|
|
|
|
|
2015-08-21 11:04:33 +08:00
|
|
|
void Parser::SkipFunctionBody() {
|
|
|
|
if (Tok.is(tok::equal)) {
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsFunctionTryBlock = Tok.is(tok::kw_try);
|
|
|
|
if (IsFunctionTryBlock)
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
CachedTokens Skipped;
|
|
|
|
if (ConsumeAndStoreFunctionPrologue(Skipped))
|
|
|
|
SkipMalformedDecl();
|
|
|
|
else {
|
|
|
|
SkipUntil(tok::r_brace);
|
|
|
|
while (IsFunctionTryBlock && Tok.is(tok::kw_catch)) {
|
|
|
|
SkipUntil(tok::l_brace);
|
|
|
|
SkipUntil(tok::r_brace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-03 16:41:30 +08:00
|
|
|
/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
|
|
|
|
/// types for a function with a K&R-style identifier list for arguments.
|
|
|
|
void Parser::ParseKNRParamDeclarations(Declarator &D) {
|
|
|
|
// We know that the top-level of this declarator is a function.
|
2010-12-11 00:29:40 +08:00
|
|
|
DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
|
2006-12-03 16:41:30 +08:00
|
|
|
|
2008-04-08 12:40:51 +08:00
|
|
|
// Enter function-declaration scope, limiting any declarators to the
|
|
|
|
// function prototype scope, including parameter declarators.
|
2013-01-29 06:42:45 +08:00
|
|
|
ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
|
|
|
|
Scope::FunctionDeclarationScope | Scope::DeclScope);
|
2008-04-08 12:40:51 +08:00
|
|
|
|
2006-12-03 16:41:30 +08:00
|
|
|
// Read all the argument declarations.
|
|
|
|
while (isDeclarationSpecifier()) {
|
|
|
|
SourceLocation DSStart = Tok.getLocation();
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2006-12-03 16:41:30 +08:00
|
|
|
// Parse the common declaration-specifiers piece.
|
2011-03-24 19:26:52 +08:00
|
|
|
DeclSpec DS(AttrFactory);
|
2018-04-26 08:42:40 +08:00
|
|
|
ParseDeclarationSpecifiers(DS);
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2006-12-03 16:41:30 +08:00
|
|
|
// C99 6.9.1p6: 'each declaration in the declaration list shall have at
|
|
|
|
// least one declarator'.
|
|
|
|
// NOTE: GCC just makes this an ext-warn. It's not clear what it does with
|
|
|
|
// the declarations though. It's trivial to ignore them, really hard to do
|
|
|
|
// anything else with them.
|
2014-01-05 11:27:11 +08:00
|
|
|
if (TryConsumeToken(tok::semi)) {
|
2006-12-03 16:41:30 +08:00
|
|
|
Diag(DSStart, diag::err_declaration_does_not_declare_param);
|
|
|
|
continue;
|
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2006-12-03 16:41:30 +08:00
|
|
|
// C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
|
|
|
|
// than register.
|
|
|
|
if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
|
|
|
|
DS.getStorageClassSpec() != DeclSpec::SCS_register) {
|
|
|
|
Diag(DS.getStorageClassSpecLoc(),
|
|
|
|
diag::err_invalid_storage_class_in_func_decl);
|
|
|
|
DS.ClearStorageClassSpecs();
|
|
|
|
}
|
2013-04-13 06:46:28 +08:00
|
|
|
if (DS.getThreadStorageClassSpec() != DeclSpec::TSCS_unspecified) {
|
|
|
|
Diag(DS.getThreadStorageClassSpecLoc(),
|
2006-12-03 16:41:30 +08:00
|
|
|
diag::err_invalid_storage_class_in_func_decl);
|
|
|
|
DS.ClearStorageClassSpecs();
|
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2006-12-03 16:41:30 +08:00
|
|
|
// Parse the first declarator attached to this declspec.
|
2017-12-29 13:41:00 +08:00
|
|
|
Declarator ParmDeclarator(DS, DeclaratorContext::KNRTypeListContext);
|
2006-12-03 16:41:30 +08:00
|
|
|
ParseDeclarator(ParmDeclarator);
|
|
|
|
|
|
|
|
// Handle the full declarator list.
|
|
|
|
while (1) {
|
|
|
|
// If attributes are present, parse them.
|
2010-12-24 10:08:15 +08:00
|
|
|
MaybeParseGNUAttributes(ParmDeclarator);
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2006-12-03 16:41:30 +08:00
|
|
|
// Ask the actions module to compute the type for this declarator.
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Param =
|
2010-07-03 01:43:08 +08:00
|
|
|
Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
|
2013-08-22 20:12:24 +08:00
|
|
|
|
2008-06-20 03:28:49 +08:00
|
|
|
if (Param &&
|
2006-12-03 16:41:30 +08:00
|
|
|
// A missing identifier has already been diagnosed.
|
|
|
|
ParmDeclarator.getIdentifier()) {
|
|
|
|
|
|
|
|
// Scan the argument list looking for the correct param to apply this
|
|
|
|
// type.
|
|
|
|
for (unsigned i = 0; ; ++i) {
|
|
|
|
// C99 6.9.1p6: those declarators shall declare only identifiers from
|
|
|
|
// the identifier list.
|
2014-02-27 06:27:52 +08:00
|
|
|
if (i == FTI.NumParams) {
|
2008-11-18 15:48:38 +08:00
|
|
|
Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
|
2008-11-19 15:51:13 +08:00
|
|
|
<< ParmDeclarator.getIdentifier();
|
2006-12-03 16:41:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2014-02-27 06:27:52 +08:00
|
|
|
if (FTI.Params[i].Ident == ParmDeclarator.getIdentifier()) {
|
2006-12-03 16:41:30 +08:00
|
|
|
// Reject redefinitions of parameters.
|
2014-02-27 06:27:52 +08:00
|
|
|
if (FTI.Params[i].Param) {
|
2006-12-03 16:41:30 +08:00
|
|
|
Diag(ParmDeclarator.getIdentifierLoc(),
|
2008-11-18 15:48:38 +08:00
|
|
|
diag::err_param_redefinition)
|
2008-11-19 15:51:13 +08:00
|
|
|
<< ParmDeclarator.getIdentifier();
|
2006-12-03 16:41:30 +08:00
|
|
|
} else {
|
2014-02-27 06:27:52 +08:00
|
|
|
FTI.Params[i].Param = Param;
|
2006-12-03 16:41:30 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have a comma, it is either the end of the list (a ';') or
|
|
|
|
// an error, bail out.
|
2007-10-10 01:23:58 +08:00
|
|
|
if (Tok.isNot(tok::comma))
|
2006-12-03 16:41:30 +08:00
|
|
|
break;
|
2008-06-20 03:28:49 +08:00
|
|
|
|
Improve 0-argument -Wvexing-parse diagnostic by adding notes with fix-its:
- If the declarator is at the start of a line, and the previous line contained
another declarator and ended with a comma, then that comma was probably a
typo for a semicolon:
int n = 0, m = 1, l = 2, // k = 5;
myImportantFunctionCall(); // oops!
- If removing the parentheses would correctly initialize the object, then
produce a note suggesting that fix.
- Otherwise, if there is a simple initializer we can suggest which performs
value-initialization, then provide a note suggesting a correction to that
initializer.
Sema::Declarator now tracks the location of the comma prior to the declarator in
the declaration, if there is one, to facilitate providing the note. The code to
determine an appropriate initializer from the -Wuninitialized warning has been
factored out to allow use in both that and -Wvexing-parse.
llvm-svn: 148072
2012-01-13 07:53:29 +08:00
|
|
|
ParmDeclarator.clear();
|
|
|
|
|
2006-12-03 16:41:30 +08:00
|
|
|
// Consume the comma.
|
Improve 0-argument -Wvexing-parse diagnostic by adding notes with fix-its:
- If the declarator is at the start of a line, and the previous line contained
another declarator and ended with a comma, then that comma was probably a
typo for a semicolon:
int n = 0, m = 1, l = 2, // k = 5;
myImportantFunctionCall(); // oops!
- If removing the parentheses would correctly initialize the object, then
produce a note suggesting that fix.
- Otherwise, if there is a simple initializer we can suggest which performs
value-initialization, then provide a note suggesting a correction to that
initializer.
Sema::Declarator now tracks the location of the comma prior to the declarator in
the declaration, if there is one, to facilitate providing the note. The code to
determine an appropriate initializer from the -Wuninitialized warning has been
factored out to allow use in both that and -Wvexing-parse.
llvm-svn: 148072
2012-01-13 07:53:29 +08:00
|
|
|
ParmDeclarator.setCommaLoc(ConsumeToken());
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2006-12-03 16:41:30 +08:00
|
|
|
// Parse the next declarator.
|
|
|
|
ParseDeclarator(ParmDeclarator);
|
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2014-01-05 12:17:27 +08:00
|
|
|
// Consume ';' and continue parsing.
|
|
|
|
if (!ExpectAndConsumeSemi(diag::err_expected_semi_declaration))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Otherwise recover by skipping to next semi or mandatory function body.
|
|
|
|
if (SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch))
|
|
|
|
break;
|
|
|
|
TryConsumeToken(tok::semi);
|
2006-12-03 16:41:30 +08:00
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2006-12-03 16:41:30 +08:00
|
|
|
// The actions module must verify that all arguments were declared.
|
2010-07-03 01:43:08 +08:00
|
|
|
Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
|
2006-12-03 16:41:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-15 14:03:28 +08:00
|
|
|
/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
|
|
|
|
/// allowed to be a wide string, and is not subject to character translation.
|
2020-01-08 21:38:02 +08:00
|
|
|
/// Unlike GCC, we also diagnose an empty string literal when parsing for an
|
|
|
|
/// asm label as opposed to an asm statement, because such a construct does not
|
|
|
|
/// behave well.
|
2006-08-15 14:03:28 +08:00
|
|
|
///
|
|
|
|
/// [GNU] asm-string-literal:
|
|
|
|
/// string-literal
|
|
|
|
///
|
2020-01-08 21:38:02 +08:00
|
|
|
ExprResult Parser::ParseAsmStringLiteral(bool ForAsmLabel) {
|
2014-12-11 09:00:48 +08:00
|
|
|
if (!isTokenStringLiteral()) {
|
|
|
|
Diag(Tok, diag::err_expected_string_literal)
|
|
|
|
<< /*Source='in...'*/0 << "'asm'";
|
|
|
|
return ExprError();
|
|
|
|
}
|
|
|
|
|
|
|
|
ExprResult AsmString(ParseStringLiteralExpression());
|
|
|
|
if (!AsmString.isInvalid()) {
|
|
|
|
const auto *SL = cast<StringLiteral>(AsmString.get());
|
|
|
|
if (!SL->isAscii()) {
|
2011-12-02 08:35:46 +08:00
|
|
|
Diag(Tok, diag::err_asm_operand_wide_string_literal)
|
2014-12-11 09:00:48 +08:00
|
|
|
<< SL->isWide()
|
|
|
|
<< SL->getSourceRange();
|
2011-12-02 08:35:46 +08:00
|
|
|
return ExprError();
|
|
|
|
}
|
2020-01-08 21:38:02 +08:00
|
|
|
if (ForAsmLabel && SL->getString().empty()) {
|
|
|
|
Diag(Tok, diag::err_asm_operand_wide_string_literal)
|
|
|
|
<< 2 /* an empty */ << SL->getSourceRange();
|
|
|
|
return ExprError();
|
|
|
|
}
|
2006-08-15 14:03:28 +08:00
|
|
|
}
|
2014-12-11 09:00:48 +08:00
|
|
|
return AsmString;
|
2006-08-15 14:03:28 +08:00
|
|
|
}
|
|
|
|
|
2006-08-15 11:41:14 +08:00
|
|
|
/// ParseSimpleAsm
|
|
|
|
///
|
|
|
|
/// [GNU] simple-asm-expr:
|
|
|
|
/// 'asm' '(' asm-string-literal ')'
|
|
|
|
///
|
2020-01-08 21:38:02 +08:00
|
|
|
ExprResult Parser::ParseSimpleAsm(bool ForAsmLabel, SourceLocation *EndLoc) {
|
2007-10-10 01:23:58 +08:00
|
|
|
assert(Tok.is(tok::kw_asm) && "Not an asm!");
|
2008-02-08 08:33:21 +08:00
|
|
|
SourceLocation Loc = ConsumeToken();
|
2008-06-20 03:28:49 +08:00
|
|
|
|
[clang][Parse] properly parse asm-qualifiers, asm inline
Summary:
The parsing of GNU C extended asm statements was a little brittle and
had a few issues:
- It was using Parse::ParseTypeQualifierListOpt to parse the `volatile`
qualifier. That parser is really meant for TypeQualifiers; an asm
statement doesn't really have a type qualifier. This is still maybe
nice to have, but not necessary. We now can check for the `volatile`
token by properly expanding the grammer, rather than abusing
Parse::ParseTypeQualifierListOpt.
- The parsing of `goto` was position dependent, so `asm goto volatile`
wouldn't parse. The qualifiers should be position independent to one
another. Now they are.
- We would warn on duplicate `volatile`, but the parse error for
duplicate `goto` was a generic parse error and wasn't clear.
- We need to add support for the recent GNU C extension `asm inline`.
Adding support to the parser with the above issues highlighted the
need for this refactoring.
Link: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
Reviewers: aaron.ballman
Reviewed By: aaron.ballman
Subscribers: aheejin, jfb, nathanchance, cfe-commits, echristo, efriedma, rsmith, chandlerc, craig.topper, erichkeane, jyu2, void, srhines
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75563
2020-03-13 06:13:55 +08:00
|
|
|
if (isGNUAsmQualifier(Tok)) {
|
|
|
|
// Remove from the end of 'asm' to the end of the asm qualifier.
|
2010-01-26 07:12:50 +08:00
|
|
|
SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
|
|
|
|
PP.getLocForEndOfToken(Tok.getLocation()));
|
[clang][Parse] properly parse asm-qualifiers, asm inline
Summary:
The parsing of GNU C extended asm statements was a little brittle and
had a few issues:
- It was using Parse::ParseTypeQualifierListOpt to parse the `volatile`
qualifier. That parser is really meant for TypeQualifiers; an asm
statement doesn't really have a type qualifier. This is still maybe
nice to have, but not necessary. We now can check for the `volatile`
token by properly expanding the grammer, rather than abusing
Parse::ParseTypeQualifierListOpt.
- The parsing of `goto` was position dependent, so `asm goto volatile`
wouldn't parse. The qualifiers should be position independent to one
another. Now they are.
- We would warn on duplicate `volatile`, but the parse error for
duplicate `goto` was a generic parse error and wasn't clear.
- We need to add support for the recent GNU C extension `asm inline`.
Adding support to the parser with the above issues highlighted the
need for this refactoring.
Link: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
Reviewers: aaron.ballman
Reviewed By: aaron.ballman
Subscribers: aheejin, jfb, nathanchance, cfe-commits, echristo, efriedma, rsmith, chandlerc, craig.topper, erichkeane, jyu2, void, srhines
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75563
2020-03-13 06:13:55 +08:00
|
|
|
Diag(Tok, diag::err_global_asm_qualifier_ignored)
|
|
|
|
<< GNUAsmQualifiers::getQualifierName(getGNUAsmQualifier(Tok))
|
|
|
|
<< FixItHint::CreateRemoval(RemovalRange);
|
2010-01-26 06:27:48 +08:00
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
|
2011-10-13 00:37:45 +08:00
|
|
|
BalancedDelimiterTracker T(*this, tok::l_paren);
|
|
|
|
if (T.consumeOpen()) {
|
2008-11-18 15:48:38 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after) << "asm";
|
2008-12-12 03:30:53 +08:00
|
|
|
return ExprError();
|
2006-08-15 11:41:14 +08:00
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2020-01-08 21:38:02 +08:00
|
|
|
ExprResult Result(ParseAsmStringLiteral(ForAsmLabel));
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2013-12-11 05:29:48 +08:00
|
|
|
if (!Result.isInvalid()) {
|
2011-10-13 00:37:45 +08:00
|
|
|
// Close the paren and get the location of the end bracket
|
|
|
|
T.consumeClose();
|
2009-02-10 02:23:29 +08:00
|
|
|
if (EndLoc)
|
2011-10-13 00:37:45 +08:00
|
|
|
*EndLoc = T.getCloseLocation();
|
2013-12-11 05:29:48 +08:00
|
|
|
} else if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
|
|
|
|
if (EndLoc)
|
|
|
|
*EndLoc = Tok.getLocation();
|
|
|
|
ConsumeParen();
|
2009-02-10 02:23:29 +08:00
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2012-08-24 05:35:17 +08:00
|
|
|
return Result;
|
2006-08-15 11:41:14 +08:00
|
|
|
}
|
2006-10-28 07:18:49 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Get the TemplateIdAnnotation from the token and put it in the
|
2011-06-22 14:09:49 +08:00
|
|
|
/// cleanup pool so that it gets destroyed when parsing the current top level
|
|
|
|
/// declaration is finished.
|
|
|
|
TemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) {
|
|
|
|
assert(tok.is(tok::annot_template_id) && "Expected template-id token");
|
|
|
|
TemplateIdAnnotation *
|
|
|
|
Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue());
|
|
|
|
return Id;
|
|
|
|
}
|
|
|
|
|
2012-08-18 08:55:03 +08:00
|
|
|
void Parser::AnnotateScopeToken(CXXScopeSpec &SS, bool IsNewAnnotation) {
|
|
|
|
// Push the current token back into the token stream (or revert it if it is
|
|
|
|
// cached) and use an annotation scope token for current token.
|
|
|
|
if (PP.isBacktrackEnabled())
|
|
|
|
PP.RevertCachedTokens(1);
|
|
|
|
else
|
2019-05-17 17:32:05 +08:00
|
|
|
PP.EnterToken(Tok, /*IsReinject=*/true);
|
2012-08-18 08:55:03 +08:00
|
|
|
Tok.setKind(tok::annot_cxxscope);
|
|
|
|
Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
|
|
|
|
Tok.setAnnotationRange(SS.getRange());
|
|
|
|
|
|
|
|
// In case the tokens were cached, have Preprocessor replace them
|
|
|
|
// with the annotation token. We don't need to do this if we've
|
|
|
|
// just reverted back to a prior state.
|
|
|
|
if (IsNewAnnotation)
|
|
|
|
PP.AnnotateCachedTokens(Tok);
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Attempt to classify the name at the current token position. This may
|
2012-08-18 08:55:03 +08:00
|
|
|
/// form a type, scope or primary expression annotation, or replace the token
|
|
|
|
/// with a typo-corrected keyword. This is only appropriate when the current
|
|
|
|
/// name must refer to an entity which has already been declared.
|
|
|
|
///
|
|
|
|
/// \param CCC Indicates how to perform typo-correction for this name. If NULL,
|
|
|
|
/// no typo correction will be performed.
|
|
|
|
Parser::AnnotatedNameKind
|
2019-10-15 05:53:03 +08:00
|
|
|
Parser::TryAnnotateName(CorrectionCandidateCallback *CCC) {
|
2012-08-18 08:55:03 +08:00
|
|
|
assert(Tok.is(tok::identifier) || Tok.is(tok::annot_cxxscope));
|
|
|
|
|
|
|
|
const bool EnteringContext = false;
|
|
|
|
const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
|
|
|
|
|
|
|
|
CXXScopeSpec SS;
|
|
|
|
if (getLangOpts().CPlusPlus &&
|
2020-03-19 16:12:29 +08:00
|
|
|
ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
|
|
|
|
/*ObjectHadErrors=*/false,
|
|
|
|
EnteringContext))
|
2012-08-18 08:55:03 +08:00
|
|
|
return ANK_Error;
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::identifier) || SS.isInvalid()) {
|
2017-01-11 06:59:18 +08:00
|
|
|
if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation))
|
2012-08-18 08:55:03 +08:00
|
|
|
return ANK_Error;
|
|
|
|
return ANK_Unresolved;
|
|
|
|
}
|
|
|
|
|
|
|
|
IdentifierInfo *Name = Tok.getIdentifierInfo();
|
|
|
|
SourceLocation NameLoc = Tok.getLocation();
|
|
|
|
|
|
|
|
// FIXME: Move the tentative declaration logic into ClassifyName so we can
|
|
|
|
// typo-correct to tentatively-declared identifiers.
|
|
|
|
if (isTentativelyDeclared(Name)) {
|
|
|
|
// Identifier has been tentatively declared, and thus cannot be resolved as
|
|
|
|
// an expression. Fall back to annotating it as a type.
|
2017-01-11 06:59:18 +08:00
|
|
|
if (TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation))
|
2012-08-18 08:55:03 +08:00
|
|
|
return ANK_Error;
|
|
|
|
return Tok.is(tok::annot_typename) ? ANK_Success : ANK_TentativeDecl;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token Next = NextToken();
|
|
|
|
|
|
|
|
// Look up and classify the identifier. We don't perform any typo-correction
|
|
|
|
// after a scope specifier, because in general we can't recover from typos
|
Misc typos fixes in ./lib folder
Summary: Found via `codespell -q 3 -I ../clang-whitelist.txt -L uint,importd,crasher,gonna,cant,ue,ons,orign,ned`
Reviewers: teemperor
Reviewed By: teemperor
Subscribers: teemperor, jholewinski, jvesely, nhaehnle, whisperity, jfb, cfe-commits
Differential Revision: https://reviews.llvm.org/D55475
llvm-svn: 348755
2018-12-10 20:37:46 +08:00
|
|
|
// there (eg, after correcting 'A::template B<X>::C' [sic], we would need to
|
2013-12-18 01:25:19 +08:00
|
|
|
// jump back into scope specifier parsing).
|
2019-10-15 05:53:03 +08:00
|
|
|
Sema::NameClassification Classification = Actions.ClassifyName(
|
|
|
|
getCurScope(), SS, Name, NameLoc, Next, SS.isEmpty() ? CCC : nullptr);
|
2012-08-18 08:55:03 +08:00
|
|
|
|
2019-05-09 11:31:27 +08:00
|
|
|
// If name lookup found nothing and we guessed that this was a template name,
|
|
|
|
// double-check before committing to that interpretation. C++20 requires that
|
|
|
|
// we interpret this as a template-id if it can be, but if it can't be, then
|
|
|
|
// this is an error recovery case.
|
|
|
|
if (Classification.getKind() == Sema::NC_UndeclaredTemplate &&
|
|
|
|
isTemplateArgumentList(1) == TPResult::False) {
|
|
|
|
// It's not a template-id; re-classify without the '<' as a hint.
|
|
|
|
Token FakeNext = Next;
|
|
|
|
FakeNext.setKind(tok::unknown);
|
|
|
|
Classification =
|
|
|
|
Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, FakeNext,
|
2019-10-15 05:53:03 +08:00
|
|
|
SS.isEmpty() ? CCC : nullptr);
|
2019-05-09 11:31:27 +08:00
|
|
|
}
|
|
|
|
|
2012-08-18 08:55:03 +08:00
|
|
|
switch (Classification.getKind()) {
|
|
|
|
case Sema::NC_Error:
|
|
|
|
return ANK_Error;
|
|
|
|
|
|
|
|
case Sema::NC_Keyword:
|
|
|
|
// The identifier was typo-corrected to a keyword.
|
|
|
|
Tok.setIdentifierInfo(Name);
|
|
|
|
Tok.setKind(Name->getTokenID());
|
|
|
|
PP.TypoCorrectToken(Tok);
|
|
|
|
if (SS.isNotEmpty())
|
|
|
|
AnnotateScopeToken(SS, !WasScopeAnnotation);
|
|
|
|
// We've "annotated" this as a keyword.
|
|
|
|
return ANK_Success;
|
|
|
|
|
|
|
|
case Sema::NC_Unknown:
|
|
|
|
// It's not something we know about. Leave it unannotated.
|
|
|
|
break;
|
|
|
|
|
2015-07-07 11:58:14 +08:00
|
|
|
case Sema::NC_Type: {
|
|
|
|
SourceLocation BeginLoc = NameLoc;
|
2012-08-18 08:55:03 +08:00
|
|
|
if (SS.isNotEmpty())
|
2015-07-07 11:58:14 +08:00
|
|
|
BeginLoc = SS.getBeginLoc();
|
|
|
|
|
|
|
|
/// An Objective-C object type followed by '<' is a specialization of
|
|
|
|
/// a parameterized class type or a protocol-qualified type.
|
|
|
|
ParsedType Ty = Classification.getType();
|
2018-10-31 04:31:30 +08:00
|
|
|
if (getLangOpts().ObjC && NextToken().is(tok::less) &&
|
2015-07-07 11:58:14 +08:00
|
|
|
(Ty.get()->isObjCObjectType() ||
|
|
|
|
Ty.get()->isObjCObjectPointerType())) {
|
|
|
|
// Consume the name.
|
|
|
|
SourceLocation IdentifierLoc = ConsumeToken();
|
|
|
|
SourceLocation NewEndLoc;
|
|
|
|
TypeResult NewType
|
|
|
|
= parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty,
|
|
|
|
/*consumeLastToken=*/false,
|
|
|
|
NewEndLoc);
|
|
|
|
if (NewType.isUsable())
|
|
|
|
Ty = NewType.get();
|
2016-09-14 04:04:35 +08:00
|
|
|
else if (Tok.is(tok::eof)) // Nothing to do here, bail out...
|
|
|
|
return ANK_Error;
|
2015-07-07 11:58:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Tok.setKind(tok::annot_typename);
|
|
|
|
setTypeAnnotation(Tok, Ty);
|
|
|
|
Tok.setAnnotationEndLoc(Tok.getLocation());
|
|
|
|
Tok.setLocation(BeginLoc);
|
2012-08-18 08:55:03 +08:00
|
|
|
PP.AnnotateCachedTokens(Tok);
|
|
|
|
return ANK_Success;
|
2015-07-07 11:58:14 +08:00
|
|
|
}
|
2012-08-18 08:55:03 +08:00
|
|
|
|
2020-07-28 08:28:06 +08:00
|
|
|
case Sema::NC_OverloadSet:
|
|
|
|
Tok.setKind(tok::annot_overload_set);
|
2012-08-18 08:55:03 +08:00
|
|
|
setExprAnnotation(Tok, Classification.getExpression());
|
|
|
|
Tok.setAnnotationEndLoc(NameLoc);
|
|
|
|
if (SS.isNotEmpty())
|
|
|
|
Tok.setLocation(SS.getBeginLoc());
|
|
|
|
PP.AnnotateCachedTokens(Tok);
|
|
|
|
return ANK_Success;
|
|
|
|
|
2019-10-15 05:53:03 +08:00
|
|
|
case Sema::NC_NonType:
|
|
|
|
Tok.setKind(tok::annot_non_type);
|
|
|
|
setNonTypeAnnotation(Tok, Classification.getNonTypeDecl());
|
|
|
|
Tok.setLocation(NameLoc);
|
|
|
|
Tok.setAnnotationEndLoc(NameLoc);
|
|
|
|
PP.AnnotateCachedTokens(Tok);
|
|
|
|
if (SS.isNotEmpty())
|
|
|
|
AnnotateScopeToken(SS, !WasScopeAnnotation);
|
|
|
|
return ANK_Success;
|
|
|
|
|
|
|
|
case Sema::NC_UndeclaredNonType:
|
|
|
|
case Sema::NC_DependentNonType:
|
|
|
|
Tok.setKind(Classification.getKind() == Sema::NC_UndeclaredNonType
|
|
|
|
? tok::annot_non_type_undeclared
|
|
|
|
: tok::annot_non_type_dependent);
|
|
|
|
setIdentifierAnnotation(Tok, Name);
|
|
|
|
Tok.setLocation(NameLoc);
|
|
|
|
Tok.setAnnotationEndLoc(NameLoc);
|
|
|
|
PP.AnnotateCachedTokens(Tok);
|
|
|
|
if (SS.isNotEmpty())
|
|
|
|
AnnotateScopeToken(SS, !WasScopeAnnotation);
|
|
|
|
return ANK_Success;
|
|
|
|
|
2012-08-18 08:55:03 +08:00
|
|
|
case Sema::NC_TypeTemplate:
|
|
|
|
if (Next.isNot(tok::less)) {
|
|
|
|
// This may be a type template being used as a template template argument.
|
|
|
|
if (SS.isNotEmpty())
|
|
|
|
AnnotateScopeToken(SS, !WasScopeAnnotation);
|
|
|
|
return ANK_TemplateName;
|
|
|
|
}
|
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-08-06 09:03:05 +08:00
|
|
|
case Sema::NC_VarTemplate:
|
2019-05-09 11:31:27 +08:00
|
|
|
case Sema::NC_FunctionTemplate:
|
|
|
|
case Sema::NC_UndeclaredTemplate: {
|
2013-08-06 09:03:05 +08:00
|
|
|
// We have a type, variable or function template followed by '<'.
|
2012-08-18 08:55:03 +08:00
|
|
|
ConsumeToken();
|
|
|
|
UnqualifiedId Id;
|
|
|
|
Id.setIdentifier(Name, NameLoc);
|
|
|
|
if (AnnotateTemplateIdToken(
|
|
|
|
TemplateTy::make(Classification.getTemplateName()),
|
|
|
|
Classification.getTemplateNameKind(), SS, SourceLocation(), Id))
|
|
|
|
return ANK_Error;
|
|
|
|
return ANK_Success;
|
|
|
|
}
|
2020-01-22 08:03:05 +08:00
|
|
|
case Sema::NC_Concept: {
|
|
|
|
UnqualifiedId Id;
|
|
|
|
Id.setIdentifier(Name, NameLoc);
|
|
|
|
if (Next.is(tok::less))
|
|
|
|
// We have a concept name followed by '<'. Consume the identifier token so
|
|
|
|
// we reach the '<' and annotate it.
|
|
|
|
ConsumeToken();
|
|
|
|
if (AnnotateTemplateIdToken(
|
|
|
|
TemplateTy::make(Classification.getTemplateName()),
|
|
|
|
Classification.getTemplateNameKind(), SS, SourceLocation(), Id,
|
|
|
|
/*AllowTypeAnnotation=*/false, /*TypeConstraint=*/true))
|
|
|
|
return ANK_Error;
|
|
|
|
return ANK_Success;
|
|
|
|
}
|
2012-08-18 08:55:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unable to classify the name, but maybe we can annotate a scope specifier.
|
|
|
|
if (SS.isNotEmpty())
|
|
|
|
AnnotateScopeToken(SS, !WasScopeAnnotation);
|
|
|
|
return ANK_Unresolved;
|
|
|
|
}
|
|
|
|
|
2013-12-03 14:13:01 +08:00
|
|
|
bool Parser::TryKeywordIdentFallback(bool DisableKeyword) {
|
2014-09-23 12:09:56 +08:00
|
|
|
assert(Tok.isNot(tok::identifier));
|
2013-12-03 14:13:01 +08:00
|
|
|
Diag(Tok, diag::ext_keyword_as_ident)
|
|
|
|
<< PP.getSpelling(Tok)
|
|
|
|
<< DisableKeyword;
|
2014-09-23 12:09:56 +08:00
|
|
|
if (DisableKeyword)
|
2015-07-20 05:41:12 +08:00
|
|
|
Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
|
2013-12-03 14:13:01 +08:00
|
|
|
Tok.setKind(tok::identifier);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-11-09 00:45:02 +08:00
|
|
|
/// TryAnnotateTypeOrScopeToken - If the current token position is on a
|
|
|
|
/// typename (possibly qualified in C++) or a C++ scope specifier not followed
|
|
|
|
/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
|
|
|
|
/// with a single annotation token representing the typename or C++ scope
|
|
|
|
/// respectively.
|
|
|
|
/// This simplifies handling of C++ scope specifiers and allows efficient
|
|
|
|
/// backtracking without the need to re-parse and resolve nested-names and
|
|
|
|
/// typenames.
|
2008-11-27 05:51:07 +08:00
|
|
|
/// It will mainly be called when we expect to treat identifiers as typenames
|
|
|
|
/// (if they are typenames). For example, in C we do not expect identifiers
|
|
|
|
/// inside expressions to be treated as typenames so it will not be called
|
|
|
|
/// for expressions in C.
|
|
|
|
/// The benefit for C/ObjC is that a typename will be annotated and
|
2009-01-29 03:39:02 +08:00
|
|
|
/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
|
2008-11-27 05:51:07 +08:00
|
|
|
/// will not be called twice, once to check whether we have a declaration
|
|
|
|
/// specifier, and another one to get the actual type inside
|
2018-04-26 08:42:40 +08:00
|
|
|
/// ParseDeclarationSpecifiers).
|
2009-01-05 07:23:14 +08:00
|
|
|
///
|
2010-02-26 16:45:28 +08:00
|
|
|
/// This returns true if an error occurred.
|
2009-09-09 23:08:12 +08:00
|
|
|
///
|
2009-01-05 08:13:00 +08:00
|
|
|
/// Note that this routine emits an error if you call it with ::new or ::delete
|
|
|
|
/// as the current tokens, so only call it in contexts where these are invalid.
|
2017-01-11 06:59:18 +08:00
|
|
|
bool Parser::TryAnnotateTypeOrScopeToken() {
|
2014-09-26 08:28:20 +08:00
|
|
|
assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
|
|
|
|
Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope) ||
|
|
|
|
Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id) ||
|
|
|
|
Tok.is(tok::kw___super)) &&
|
|
|
|
"Cannot be a type or scope token!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-28 07:10:48 +08:00
|
|
|
if (Tok.is(tok::kw_typename)) {
|
2013-09-04 06:36:22 +08:00
|
|
|
// MSVC lets you do stuff like:
|
|
|
|
// typename typedef T_::D D;
|
|
|
|
//
|
|
|
|
// We will consume the typedef token here and put it back after we have
|
|
|
|
// parsed the first identifier, transforming it into something more like:
|
|
|
|
// typename T_::D typedef D;
|
2014-01-14 20:51:41 +08:00
|
|
|
if (getLangOpts().MSVCCompat && NextToken().is(tok::kw_typedef)) {
|
2013-09-04 06:36:22 +08:00
|
|
|
Token TypedefToken;
|
|
|
|
PP.Lex(TypedefToken);
|
2017-01-11 06:59:18 +08:00
|
|
|
bool Result = TryAnnotateTypeOrScopeToken();
|
2019-05-17 17:32:05 +08:00
|
|
|
PP.EnterToken(Tok, /*IsReinject=*/true);
|
2013-09-04 06:36:22 +08:00
|
|
|
Tok = TypedefToken;
|
|
|
|
if (!Result)
|
|
|
|
Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2009-03-28 07:10:48 +08:00
|
|
|
// Parse a C++ typename-specifier, e.g., "typename T::type".
|
|
|
|
//
|
|
|
|
// typename-specifier:
|
|
|
|
// 'typename' '::' [opt] nested-name-specifier identifier
|
2009-09-09 23:08:12 +08:00
|
|
|
// 'typename' '::' [opt] nested-name-specifier template [opt]
|
2009-04-01 08:28:59 +08:00
|
|
|
// simple-template-id
|
2009-03-28 07:10:48 +08:00
|
|
|
SourceLocation TypenameLoc = ConsumeToken();
|
|
|
|
CXXScopeSpec SS;
|
2016-01-16 07:43:34 +08:00
|
|
|
if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
|
2020-03-19 16:12:29 +08:00
|
|
|
/*ObjectHadErrors=*/false,
|
2016-01-16 07:43:34 +08:00
|
|
|
/*EnteringContext=*/false, nullptr,
|
|
|
|
/*IsTypename*/ true))
|
2010-02-26 16:45:28 +08:00
|
|
|
return true;
|
2020-01-18 07:42:11 +08:00
|
|
|
if (SS.isEmpty()) {
|
2012-07-22 23:10:57 +08:00
|
|
|
if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id) ||
|
|
|
|
Tok.is(tok::annot_decltype)) {
|
2012-05-15 06:43:34 +08:00
|
|
|
// Attempt to recover by skipping the invalid 'typename'
|
2012-07-22 23:10:57 +08:00
|
|
|
if (Tok.is(tok::annot_decltype) ||
|
2017-01-11 06:59:18 +08:00
|
|
|
(!TryAnnotateTypeOrScopeToken() && Tok.isAnnotation())) {
|
2012-05-15 06:43:34 +08:00
|
|
|
unsigned DiagID = diag::err_expected_qualified_after_typename;
|
|
|
|
// MS compatibility: MSVC permits using known types with typename.
|
|
|
|
// e.g. "typedef typename T* pointer_type"
|
|
|
|
if (getLangOpts().MicrosoftExt)
|
|
|
|
DiagID = diag::warn_expected_qualified_after_typename;
|
|
|
|
Diag(Tok.getLocation(), DiagID);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 16:58:56 +08:00
|
|
|
if (Tok.isEditorPlaceholder())
|
|
|
|
return true;
|
2012-05-15 06:43:34 +08:00
|
|
|
|
|
|
|
Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
|
2010-02-26 16:45:28 +08:00
|
|
|
return true;
|
2009-03-28 07:10:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TypeResult Ty;
|
|
|
|
if (Tok.is(tok::identifier)) {
|
|
|
|
// FIXME: check whether the next token is '<', first!
|
2018-07-31 03:24:48 +08:00
|
|
|
Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
|
2010-06-17 06:31:08 +08:00
|
|
|
*Tok.getIdentifierInfo(),
|
2009-03-28 07:10:48 +08:00
|
|
|
Tok.getLocation());
|
2009-04-01 08:28:59 +08:00
|
|
|
} else if (Tok.is(tok::annot_template_id)) {
|
2011-06-22 14:09:49 +08:00
|
|
|
TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
|
2020-03-28 11:59:49 +08:00
|
|
|
if (!TemplateId->mightBeType()) {
|
2009-04-01 08:28:59 +08:00
|
|
|
Diag(Tok, diag::err_typename_refers_to_non_type_template)
|
|
|
|
<< Tok.getAnnotationRange();
|
2010-02-26 16:45:28 +08:00
|
|
|
return true;
|
2009-04-01 08:28:59 +08:00
|
|
|
}
|
2009-03-28 07:10:48 +08:00
|
|
|
|
2012-08-24 07:38:35 +08:00
|
|
|
ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
|
2011-02-28 06:46:49 +08:00
|
|
|
TemplateId->NumArgs);
|
2012-02-07 06:45:07 +08:00
|
|
|
|
2020-03-28 11:59:49 +08:00
|
|
|
Ty = TemplateId->isInvalid()
|
|
|
|
? TypeError()
|
|
|
|
: Actions.ActOnTypenameType(
|
|
|
|
getCurScope(), TypenameLoc, SS, TemplateId->TemplateKWLoc,
|
|
|
|
TemplateId->Template, TemplateId->Name,
|
|
|
|
TemplateId->TemplateNameLoc, TemplateId->LAngleLoc,
|
|
|
|
TemplateArgsPtr, TemplateId->RAngleLoc);
|
2009-04-01 08:28:59 +08:00
|
|
|
} else {
|
|
|
|
Diag(Tok, diag::err_expected_type_name_after_typename)
|
|
|
|
<< SS.getRange();
|
2010-02-26 16:45:28 +08:00
|
|
|
return true;
|
2009-04-01 08:28:59 +08:00
|
|
|
}
|
|
|
|
|
2010-02-09 03:35:18 +08:00
|
|
|
SourceLocation EndLoc = Tok.getLastLoc();
|
2009-04-01 08:28:59 +08:00
|
|
|
Tok.setKind(tok::annot_typename);
|
2020-03-31 08:19:30 +08:00
|
|
|
setTypeAnnotation(Tok, Ty);
|
2010-02-09 03:35:18 +08:00
|
|
|
Tok.setAnnotationEndLoc(EndLoc);
|
2009-04-01 08:28:59 +08:00
|
|
|
Tok.setLocation(TypenameLoc);
|
|
|
|
PP.AnnotateCachedTokens(Tok);
|
2010-02-26 16:45:28 +08:00
|
|
|
return false;
|
2009-03-28 07:10:48 +08:00
|
|
|
}
|
|
|
|
|
2009-12-19 08:35:18 +08:00
|
|
|
// Remembers whether the token was originally a scope annotation.
|
2012-08-18 08:55:03 +08:00
|
|
|
bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
|
2009-12-19 08:35:18 +08:00
|
|
|
|
2008-11-09 00:45:02 +08:00
|
|
|
CXXScopeSpec SS;
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().CPlusPlus)
|
2020-03-19 16:12:29 +08:00
|
|
|
if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
|
|
|
|
/*ObjectHadErrors=*/false,
|
|
|
|
/*EnteringContext*/ false))
|
2010-02-26 16:45:28 +08:00
|
|
|
return true;
|
2008-11-09 00:45:02 +08:00
|
|
|
|
2017-01-11 06:59:18 +08:00
|
|
|
return TryAnnotateTypeOrScopeTokenAfterScopeSpec(SS, !WasScopeAnnotation);
|
2012-08-18 08:55:03 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Try to annotate a type or scope token, having already parsed an
|
2012-08-18 08:55:03 +08:00
|
|
|
/// optional scope specifier. \p IsNewScope should be \c true unless the scope
|
|
|
|
/// specifier was extracted from an existing tok::annot_cxxscope annotation.
|
2017-01-11 06:59:18 +08:00
|
|
|
bool Parser::TryAnnotateTypeOrScopeTokenAfterScopeSpec(CXXScopeSpec &SS,
|
2012-08-18 08:55:03 +08:00
|
|
|
bool IsNewScope) {
|
2008-11-09 00:45:02 +08:00
|
|
|
if (Tok.is(tok::identifier)) {
|
2009-01-05 09:49:50 +08:00
|
|
|
// Determine whether the identifier is a type name.
|
2016-01-16 07:43:34 +08:00
|
|
|
if (ParsedType Ty = Actions.getTypeName(
|
|
|
|
*Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), &SS,
|
|
|
|
false, NextToken().is(tok::period), nullptr,
|
|
|
|
/*IsCtorOrDtorName=*/false,
|
2018-02-28 11:02:23 +08:00
|
|
|
/*NonTrivialTypeSourceInfo*/true,
|
|
|
|
/*IsClassTemplateDeductionContext*/true)) {
|
2015-07-07 11:58:14 +08:00
|
|
|
SourceLocation BeginLoc = Tok.getLocation();
|
|
|
|
if (SS.isNotEmpty()) // it was a C++ qualified type name.
|
|
|
|
BeginLoc = SS.getBeginLoc();
|
|
|
|
|
|
|
|
/// An Objective-C object type followed by '<' is a specialization of
|
|
|
|
/// a parameterized class type or a protocol-qualified type.
|
2018-10-31 04:31:30 +08:00
|
|
|
if (getLangOpts().ObjC && NextToken().is(tok::less) &&
|
2015-07-07 11:58:14 +08:00
|
|
|
(Ty.get()->isObjCObjectType() ||
|
|
|
|
Ty.get()->isObjCObjectPointerType())) {
|
|
|
|
// Consume the name.
|
|
|
|
SourceLocation IdentifierLoc = ConsumeToken();
|
|
|
|
SourceLocation NewEndLoc;
|
|
|
|
TypeResult NewType
|
|
|
|
= parseObjCTypeArgsAndProtocolQualifiers(IdentifierLoc, Ty,
|
|
|
|
/*consumeLastToken=*/false,
|
|
|
|
NewEndLoc);
|
|
|
|
if (NewType.isUsable())
|
|
|
|
Ty = NewType.get();
|
2016-09-14 04:04:35 +08:00
|
|
|
else if (Tok.is(tok::eof)) // Nothing to do here, bail out...
|
|
|
|
return false;
|
2015-07-07 11:58:14 +08:00
|
|
|
}
|
|
|
|
|
2009-01-05 09:49:50 +08:00
|
|
|
// This is a typename. Replace the current token in-place with an
|
|
|
|
// annotation type token.
|
2009-01-06 13:06:21 +08:00
|
|
|
Tok.setKind(tok::annot_typename);
|
2010-08-24 13:47:05 +08:00
|
|
|
setTypeAnnotation(Tok, Ty);
|
2009-01-05 09:49:50 +08:00
|
|
|
Tok.setAnnotationEndLoc(Tok.getLocation());
|
2015-07-07 11:58:14 +08:00
|
|
|
Tok.setLocation(BeginLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-05 09:49:50 +08:00
|
|
|
// In case the tokens were cached, have Preprocessor replace
|
|
|
|
// them with the annotation token.
|
|
|
|
PP.AnnotateCachedTokens(Tok);
|
2010-02-26 16:45:28 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!getLangOpts().CPlusPlus) {
|
2009-01-05 09:49:50 +08:00
|
|
|
// If we're in C, we can't have :: tokens at all (the lexer won't return
|
|
|
|
// them). If the identifier is not a type, then it can't be scope either,
|
2009-09-09 23:08:12 +08:00
|
|
|
// just early exit.
|
2009-01-05 09:49:50 +08:00
|
|
|
return false;
|
2008-11-09 00:45:02 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
// If this is a template-id, annotate with a template-id or type token.
|
2019-05-09 11:31:27 +08:00
|
|
|
// FIXME: This appears to be dead code. We already have formed template-id
|
|
|
|
// tokens when parsing the scope specifier; this can never form a new one.
|
2009-02-10 02:46:07 +08:00
|
|
|
if (NextToken().is(tok::less)) {
|
2009-03-31 06:58:21 +08:00
|
|
|
TemplateTy Template;
|
2009-11-04 07:16:33 +08:00
|
|
|
UnqualifiedId TemplateName;
|
|
|
|
TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
|
2010-05-22 07:18:07 +08:00
|
|
|
bool MemberOfUnknownSpecialization;
|
2017-01-11 06:59:18 +08:00
|
|
|
if (TemplateNameKind TNK = Actions.isTemplateName(
|
|
|
|
getCurScope(), SS,
|
|
|
|
/*hasTemplateKeyword=*/false, TemplateName,
|
|
|
|
/*ObjectType=*/nullptr, /*EnteringContext*/false, Template,
|
|
|
|
MemberOfUnknownSpecialization)) {
|
2019-05-09 11:31:27 +08:00
|
|
|
// Only annotate an undeclared template name as a template-id if the
|
|
|
|
// following tokens have the form of a template argument list.
|
|
|
|
if (TNK != TNK_Undeclared_template ||
|
|
|
|
isTemplateArgumentList(1) != TPResult::False) {
|
|
|
|
// Consume the identifier.
|
|
|
|
ConsumeToken();
|
|
|
|
if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
|
|
|
|
TemplateName)) {
|
|
|
|
// If an unrecoverable error occurred, we need to return true here,
|
|
|
|
// because the token stream is in a damaged state. We may not
|
|
|
|
// return a valid identifier.
|
|
|
|
return true;
|
|
|
|
}
|
2009-06-26 12:27:47 +08:00
|
|
|
}
|
2009-11-04 08:56:37 +08:00
|
|
|
}
|
2009-02-10 02:46:07 +08:00
|
|
|
}
|
2008-12-19 03:37:40 +08:00
|
|
|
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
// The current token, which is either an identifier or a
|
|
|
|
// template-id, is not part of the annotation. Fall through to
|
|
|
|
// push that token back into the stream and complete the C++ scope
|
|
|
|
// specifier annotation.
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-11-09 00:45:02 +08:00
|
|
|
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
if (Tok.is(tok::annot_template_id)) {
|
2011-06-22 14:09:49 +08:00
|
|
|
TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
|
2009-03-31 08:43:58 +08:00
|
|
|
if (TemplateId->Kind == TNK_Type_template) {
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
// A template-id that refers to a type was parsed into a
|
|
|
|
// template-id annotation in a context where we weren't allowed
|
|
|
|
// to produce a type annotation token. Update the template-id
|
|
|
|
// annotation token to a type annotation token now.
|
2020-01-18 07:42:11 +08:00
|
|
|
AnnotateTemplateIdTokenAsType(SS);
|
2010-02-26 16:45:28 +08:00
|
|
|
return false;
|
2013-12-04 09:01:51 +08:00
|
|
|
}
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
}
|
2008-12-19 03:37:40 +08:00
|
|
|
|
2009-01-05 06:32:19 +08:00
|
|
|
if (SS.isEmpty())
|
2010-02-26 16:45:28 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-05 06:32:19 +08:00
|
|
|
// A C++ scope specifier that isn't followed by a typename.
|
2012-08-18 08:55:03 +08:00
|
|
|
AnnotateScopeToken(SS, IsNewScope);
|
2010-02-26 16:45:28 +08:00
|
|
|
return false;
|
2008-11-09 00:45:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
/// annotates C++ scope specifiers and template-ids. This returns
|
2012-05-09 16:23:23 +08:00
|
|
|
/// true if there was an error that could not be recovered from.
|
2009-09-09 23:08:12 +08:00
|
|
|
///
|
2009-01-05 08:13:00 +08:00
|
|
|
/// Note that this routine emits an error if you call it with ::new or ::delete
|
|
|
|
/// as the current tokens, so only call it in contexts where these are invalid.
|
Improve support for out-of-line definitions of nested templates and
their members, including member class template, member function
templates, and member classes and functions of member templates.
To actually parse the nested-name-specifiers that qualify the name of
an out-of-line definition of a member template, e.g.,
template<typename X> template<typename Y>
X Outer<X>::Inner1<Y>::foo(Y) {
return X();
}
we need to look for the template names (e.g., "Inner1") as a member of
the current instantiation (Outer<X>), even before we have entered the
scope of the current instantiation. Since we can't do this in general
(i.e., we should not be looking into all dependent
nested-name-specifiers as if they were the current instantiation), we
rely on the parser to tell us when it is parsing a declaration
specifier sequence, and, therefore, when we should consider the
current scope specifier to be a current instantiation.
Printing of complicated, dependent nested-name-specifiers may be
somewhat broken by this commit; I'll add tests for this issue and fix
the problem (if it still exists) in a subsequent commit.
llvm-svn: 80044
2009-08-26 06:51:20 +08:00
|
|
|
bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
|
2012-03-11 15:00:24 +08:00
|
|
|
assert(getLangOpts().CPlusPlus &&
|
2009-01-05 06:32:19 +08:00
|
|
|
"Call sites of this function should be guarded by checking for C++");
|
2020-01-16 10:37:32 +08:00
|
|
|
assert(MightBeCXXScopeToken() && "Cannot be a type or scope token!");
|
2008-11-09 00:45:02 +08:00
|
|
|
|
2008-11-27 05:41:52 +08:00
|
|
|
CXXScopeSpec SS;
|
2020-03-19 16:12:29 +08:00
|
|
|
if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
|
|
|
|
/*ObjectHadErrors=*/false,
|
|
|
|
EnteringContext))
|
2010-02-26 16:45:28 +08:00
|
|
|
return true;
|
2010-04-08 07:29:58 +08:00
|
|
|
if (SS.isEmpty())
|
2010-02-26 16:45:28 +08:00
|
|
|
return false;
|
2008-11-09 00:45:02 +08:00
|
|
|
|
2012-08-18 08:55:03 +08:00
|
|
|
AnnotateScopeToken(SS, true);
|
2010-02-26 16:45:28 +08:00
|
|
|
return false;
|
2008-11-09 00:45:02 +08:00
|
|
|
}
|
2009-11-04 03:33:12 +08:00
|
|
|
|
Extend the error of invalid token after declarations to include fixits for
!=, %=, ^=, &=, *=, -=, |=, /=, <<=, <=, >=, and >>= to =.
llvm-svn: 148499
2012-01-20 06:01:51 +08:00
|
|
|
bool Parser::isTokenEqualOrEqualTypo() {
|
|
|
|
tok::TokenKind Kind = Tok.getKind();
|
|
|
|
switch (Kind) {
|
|
|
|
default:
|
2012-01-19 06:54:52 +08:00
|
|
|
return false;
|
Extend the error of invalid token after declarations to include fixits for
!=, %=, ^=, &=, *=, -=, |=, /=, <<=, <=, >=, and >>= to =.
llvm-svn: 148499
2012-01-20 06:01:51 +08:00
|
|
|
case tok::ampequal: // &=
|
|
|
|
case tok::starequal: // *=
|
|
|
|
case tok::plusequal: // +=
|
|
|
|
case tok::minusequal: // -=
|
|
|
|
case tok::exclaimequal: // !=
|
|
|
|
case tok::slashequal: // /=
|
|
|
|
case tok::percentequal: // %=
|
|
|
|
case tok::lessequal: // <=
|
|
|
|
case tok::lesslessequal: // <<=
|
|
|
|
case tok::greaterequal: // >=
|
|
|
|
case tok::greatergreaterequal: // >>=
|
|
|
|
case tok::caretequal: // ^=
|
|
|
|
case tok::pipeequal: // |=
|
|
|
|
case tok::equalequal: // ==
|
|
|
|
Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal)
|
2013-12-24 17:48:30 +08:00
|
|
|
<< Kind
|
|
|
|
<< FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "=");
|
2017-06-03 14:29:16 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
Extend the error of invalid token after declarations to include fixits for
!=, %=, ^=, &=, *=, -=, |=, /=, <<=, <=, >=, and >>= to =.
llvm-svn: 148499
2012-01-20 06:01:51 +08:00
|
|
|
case tok::equal:
|
|
|
|
return true;
|
|
|
|
}
|
2010-10-08 10:39:23 +08:00
|
|
|
}
|
|
|
|
|
2011-09-04 11:32:15 +08:00
|
|
|
SourceLocation Parser::handleUnexpectedCodeCompletionToken() {
|
|
|
|
assert(Tok.is(tok::code_completion));
|
|
|
|
PrevTokLocation = Tok.getLocation();
|
|
|
|
|
2010-07-03 01:43:08 +08:00
|
|
|
for (Scope *S = getCurScope(); S; S = S->getParent()) {
|
2010-05-25 13:58:43 +08:00
|
|
|
if (S->getFlags() & Scope::FnScope) {
|
2015-01-04 08:47:22 +08:00
|
|
|
Actions.CodeCompleteOrdinaryName(getCurScope(),
|
|
|
|
Sema::PCC_RecoveryInFunction);
|
2011-09-04 11:32:15 +08:00
|
|
|
cutOffParsing();
|
|
|
|
return PrevTokLocation;
|
2010-05-25 13:58:43 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-05-25 13:58:43 +08:00
|
|
|
if (S->getFlags() & Scope::ClassScope) {
|
2010-08-27 07:41:50 +08:00
|
|
|
Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class);
|
2011-09-04 11:32:15 +08:00
|
|
|
cutOffParsing();
|
|
|
|
return PrevTokLocation;
|
2010-05-25 13:58:43 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-08-27 07:41:50 +08:00
|
|
|
Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
|
2011-09-04 11:32:15 +08:00
|
|
|
cutOffParsing();
|
|
|
|
return PrevTokLocation;
|
2010-05-25 13:58:43 +08:00
|
|
|
}
|
|
|
|
|
2010-08-25 03:08:16 +08:00
|
|
|
// Code-completion pass-through functions
|
|
|
|
|
|
|
|
void Parser::CodeCompleteDirective(bool InConditional) {
|
2010-08-25 06:20:20 +08:00
|
|
|
Actions.CodeCompletePreprocessorDirective(InConditional);
|
2010-08-25 03:08:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::CodeCompleteInConditionalExclusion() {
|
|
|
|
Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope());
|
|
|
|
}
|
2010-08-25 04:21:13 +08:00
|
|
|
|
|
|
|
void Parser::CodeCompleteMacroName(bool IsDefinition) {
|
2010-08-25 06:20:20 +08:00
|
|
|
Actions.CodeCompletePreprocessorMacroName(IsDefinition);
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
void Parser::CodeCompletePreprocessorExpression() {
|
2010-08-25 06:20:20 +08:00
|
|
|
Actions.CodeCompletePreprocessorExpression();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
|
|
|
|
MacroInfo *MacroInfo,
|
|
|
|
unsigned ArgumentIndex) {
|
2015-01-04 08:47:22 +08:00
|
|
|
Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
|
2010-08-25 06:20:20 +08:00
|
|
|
ArgumentIndex);
|
2010-08-25 04:21:13 +08:00
|
|
|
}
|
2010-08-26 01:04:25 +08:00
|
|
|
|
2018-09-18 16:40:41 +08:00
|
|
|
void Parser::CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled) {
|
|
|
|
Actions.CodeCompleteIncludedFile(Dir, IsAngled);
|
|
|
|
}
|
|
|
|
|
2010-08-26 01:04:25 +08:00
|
|
|
void Parser::CodeCompleteNaturalLanguage() {
|
|
|
|
Actions.CodeCompleteNaturalLanguage();
|
|
|
|
}
|
2011-05-08 01:30:27 +08:00
|
|
|
|
2011-10-25 06:31:10 +08:00
|
|
|
bool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) {
|
2011-05-08 01:30:27 +08:00
|
|
|
assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
|
|
|
|
"Expected '__if_exists' or '__if_not_exists'");
|
2011-10-25 06:31:10 +08:00
|
|
|
Result.IsIfExists = Tok.is(tok::kw___if_exists);
|
|
|
|
Result.KeywordLoc = ConsumeToken();
|
2011-05-08 01:30:27 +08:00
|
|
|
|
2011-10-13 00:37:45 +08:00
|
|
|
BalancedDelimiterTracker T(*this, tok::l_paren);
|
|
|
|
if (T.consumeOpen()) {
|
2018-07-31 03:24:48 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after)
|
2011-10-25 06:31:10 +08:00
|
|
|
<< (Result.IsIfExists? "__if_exists" : "__if_not_exists");
|
2011-05-08 01:30:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-05-08 01:30:27 +08:00
|
|
|
// Parse nested-name-specifier.
|
2014-11-25 01:29:35 +08:00
|
|
|
if (getLangOpts().CPlusPlus)
|
2020-03-19 16:12:29 +08:00
|
|
|
ParseOptionalCXXScopeSpecifier(Result.SS, /*ObjectType=*/nullptr,
|
|
|
|
/*ObjectHadErrors=*/false,
|
2014-11-25 01:29:35 +08:00
|
|
|
/*EnteringContext=*/false);
|
2011-05-08 01:30:27 +08:00
|
|
|
|
|
|
|
// Check nested-name specifier.
|
2011-10-25 06:31:10 +08:00
|
|
|
if (Result.SS.isInvalid()) {
|
|
|
|
T.skipToEnd();
|
2011-05-08 01:30:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-27 17:46:47 +08:00
|
|
|
// Parse the unqualified-id.
|
|
|
|
SourceLocation TemplateKWLoc; // FIXME: parsed, but unused.
|
2020-03-19 16:12:29 +08:00
|
|
|
if (ParseUnqualifiedId(Result.SS, /*ObjectType=*/nullptr,
|
|
|
|
/*ObjectHadErrors=*/false, /*EnteringContext*/ false,
|
|
|
|
/*AllowDestructorName*/ true,
|
|
|
|
/*AllowConstructorName*/ true,
|
|
|
|
/*AllowDeductionGuide*/ false, &TemplateKWLoc,
|
|
|
|
Result.Name)) {
|
2011-10-25 06:31:10 +08:00
|
|
|
T.skipToEnd();
|
2011-05-08 01:30:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-25 06:31:10 +08:00
|
|
|
if (T.consumeClose())
|
2011-05-08 01:30:27 +08:00
|
|
|
return true;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-05-08 01:30:27 +08:00
|
|
|
// Check if the symbol exists.
|
2011-10-25 11:44:56 +08:00
|
|
|
switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc,
|
2015-01-04 08:47:22 +08:00
|
|
|
Result.IsIfExists, Result.SS,
|
2011-10-25 06:31:10 +08:00
|
|
|
Result.Name)) {
|
|
|
|
case Sema::IER_Exists:
|
|
|
|
Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip;
|
|
|
|
break;
|
2011-05-08 01:30:27 +08:00
|
|
|
|
2011-10-25 06:31:10 +08:00
|
|
|
case Sema::IER_DoesNotExist:
|
|
|
|
Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Sema::IER_Dependent:
|
|
|
|
Result.Behavior = IEB_Dependent;
|
|
|
|
break;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-10-25 11:44:56 +08:00
|
|
|
case Sema::IER_Error:
|
|
|
|
return true;
|
2011-10-25 06:31:10 +08:00
|
|
|
}
|
2011-05-08 01:30:27 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-25 18:19:49 +08:00
|
|
|
void Parser::ParseMicrosoftIfExistsExternalDeclaration() {
|
2011-10-25 06:31:10 +08:00
|
|
|
IfExistsCondition Result;
|
2011-05-08 01:30:27 +08:00
|
|
|
if (ParseMicrosoftIfExistsCondition(Result))
|
|
|
|
return;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-10-25 06:31:10 +08:00
|
|
|
BalancedDelimiterTracker Braces(*this, tok::l_brace);
|
|
|
|
if (Braces.consumeOpen()) {
|
2013-12-24 17:48:30 +08:00
|
|
|
Diag(Tok, diag::err_expected) << tok::l_brace;
|
2011-05-08 01:30:27 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-25 06:31:10 +08:00
|
|
|
switch (Result.Behavior) {
|
|
|
|
case IEB_Parse:
|
|
|
|
// Parse declarations below.
|
|
|
|
break;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-10-25 06:31:10 +08:00
|
|
|
case IEB_Dependent:
|
|
|
|
llvm_unreachable("Cannot have a dependent external declaration");
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-10-25 06:31:10 +08:00
|
|
|
case IEB_Skip:
|
|
|
|
Braces.skipToEnd();
|
2011-05-08 01:30:27 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-25 06:31:10 +08:00
|
|
|
// Parse the declarations.
|
2013-11-23 12:06:09 +08:00
|
|
|
// FIXME: Support module import within __if_exists?
|
|
|
|
while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
|
2011-05-08 01:30:27 +08:00
|
|
|
ParsedAttributesWithRange attrs(AttrFactory);
|
2013-01-02 20:01:23 +08:00
|
|
|
MaybeParseCXX11Attributes(attrs);
|
2011-05-08 01:30:27 +08:00
|
|
|
DeclGroupPtrTy Result = ParseExternalDeclaration(attrs);
|
|
|
|
if (Result && !getCurScope()->getParent())
|
|
|
|
Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
|
2013-11-23 12:06:09 +08:00
|
|
|
}
|
2011-10-25 06:31:10 +08:00
|
|
|
Braces.consumeClose();
|
2011-05-08 01:30:27 +08:00
|
|
|
}
|
2011-08-27 07:56:07 +08:00
|
|
|
|
2019-04-14 16:06:59 +08:00
|
|
|
/// Parse a declaration beginning with the 'module' keyword or C++20
|
|
|
|
/// context-sensitive keyword (optionally preceded by 'export').
|
2016-08-19 09:43:06 +08:00
|
|
|
///
|
2019-04-14 16:06:59 +08:00
|
|
|
/// module-declaration: [Modules TS + P0629R0]
|
|
|
|
/// 'export'[opt] 'module' module-name attribute-specifier-seq[opt] ';'
|
2016-08-19 09:43:06 +08:00
|
|
|
///
|
2019-04-14 16:06:59 +08:00
|
|
|
/// global-module-fragment: [C++2a]
|
|
|
|
/// 'module' ';' top-level-declaration-seq[opt]
|
|
|
|
/// module-declaration: [C++2a]
|
|
|
|
/// 'export'[opt] 'module' module-name module-partition[opt]
|
|
|
|
/// attribute-specifier-seq[opt] ';'
|
|
|
|
/// private-module-fragment: [C++2a]
|
|
|
|
/// 'module' ':' 'private' ';' top-level-declaration-seq[opt]
|
|
|
|
Parser::DeclGroupPtrTy Parser::ParseModuleDecl(bool IsFirstDecl) {
|
2017-04-22 06:39:18 +08:00
|
|
|
SourceLocation StartLoc = Tok.getLocation();
|
|
|
|
|
|
|
|
Sema::ModuleDeclKind MDK = TryConsumeToken(tok::kw_export)
|
2017-10-11 06:35:27 +08:00
|
|
|
? Sema::ModuleDeclKind::Interface
|
2017-04-22 06:39:18 +08:00
|
|
|
: Sema::ModuleDeclKind::Implementation;
|
|
|
|
|
2019-04-14 16:06:59 +08:00
|
|
|
assert(
|
|
|
|
(Tok.is(tok::kw_module) ||
|
|
|
|
(Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_module)) &&
|
|
|
|
"not a module declaration");
|
2016-08-19 09:43:06 +08:00
|
|
|
SourceLocation ModuleLoc = ConsumeToken();
|
|
|
|
|
2019-04-14 16:06:59 +08:00
|
|
|
// Attributes appear after the module name, not before.
|
2019-04-16 07:55:58 +08:00
|
|
|
// FIXME: Suggest moving the attributes later with a fixit.
|
|
|
|
DiagnoseAndSkipCXX11Attributes();
|
2019-04-14 16:06:59 +08:00
|
|
|
|
|
|
|
// Parse a global-module-fragment, if present.
|
|
|
|
if (getLangOpts().CPlusPlusModules && Tok.is(tok::semi)) {
|
|
|
|
SourceLocation SemiLoc = ConsumeToken();
|
|
|
|
if (!IsFirstDecl) {
|
|
|
|
Diag(StartLoc, diag::err_global_module_introducer_not_at_start)
|
|
|
|
<< SourceRange(StartLoc, SemiLoc);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (MDK == Sema::ModuleDeclKind::Interface) {
|
|
|
|
Diag(StartLoc, diag::err_module_fragment_exported)
|
|
|
|
<< /*global*/0 << FixItHint::CreateRemoval(StartLoc);
|
|
|
|
}
|
|
|
|
return Actions.ActOnGlobalModuleFragmentDecl(ModuleLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse a private-module-fragment, if present.
|
|
|
|
if (getLangOpts().CPlusPlusModules && Tok.is(tok::colon) &&
|
|
|
|
NextToken().is(tok::kw_private)) {
|
|
|
|
if (MDK == Sema::ModuleDeclKind::Interface) {
|
|
|
|
Diag(StartLoc, diag::err_module_fragment_exported)
|
|
|
|
<< /*private*/1 << FixItHint::CreateRemoval(StartLoc);
|
|
|
|
}
|
2016-08-19 09:43:06 +08:00
|
|
|
ConsumeToken();
|
2019-04-14 16:06:59 +08:00
|
|
|
SourceLocation PrivateLoc = ConsumeToken();
|
2019-04-16 07:55:58 +08:00
|
|
|
DiagnoseAndSkipCXX11Attributes();
|
2019-04-14 16:06:59 +08:00
|
|
|
ExpectAndConsumeSemi(diag::err_private_module_fragment_expected_semi);
|
|
|
|
return Actions.ActOnPrivateModuleFragmentDecl(ModuleLoc, PrivateLoc);
|
2016-08-19 09:43:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
|
|
|
|
if (ParseModuleName(ModuleLoc, Path, /*IsImport*/false))
|
|
|
|
return nullptr;
|
|
|
|
|
2019-04-14 16:06:59 +08:00
|
|
|
// Parse the optional module-partition.
|
|
|
|
if (Tok.is(tok::colon)) {
|
|
|
|
SourceLocation ColonLoc = ConsumeToken();
|
|
|
|
SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Partition;
|
|
|
|
if (ParseModuleName(ModuleLoc, Partition, /*IsImport*/false))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// FIXME: Support module partition declarations.
|
|
|
|
Diag(ColonLoc, diag::err_unsupported_module_partition)
|
|
|
|
<< SourceRange(ColonLoc, Partition.back().second);
|
|
|
|
// Recover by parsing as a non-partition.
|
|
|
|
}
|
|
|
|
|
2017-04-22 06:39:18 +08:00
|
|
|
// We don't support any module attributes yet; just parse them and diagnose.
|
2016-08-19 09:43:06 +08:00
|
|
|
ParsedAttributesWithRange Attrs(AttrFactory);
|
|
|
|
MaybeParseCXX11Attributes(Attrs);
|
|
|
|
ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_module_attr);
|
|
|
|
|
|
|
|
ExpectAndConsumeSemi(diag::err_module_expected_semi);
|
|
|
|
|
2019-04-14 16:06:59 +08:00
|
|
|
return Actions.ActOnModuleDecl(StartLoc, ModuleLoc, MDK, Path, IsFirstDecl);
|
2016-08-19 09:43:06 +08:00
|
|
|
}
|
|
|
|
|
2016-08-19 05:59:42 +08:00
|
|
|
/// Parse a module import declaration. This is essentially the same for
|
|
|
|
/// Objective-C and the C++ Modules TS, except for the leading '@' (in ObjC)
|
|
|
|
/// and the trailing optional attributes (in C++).
|
2018-07-31 03:24:48 +08:00
|
|
|
///
|
2016-08-19 05:59:42 +08:00
|
|
|
/// [ObjC] @import declaration:
|
2016-08-19 09:43:06 +08:00
|
|
|
/// '@' 'import' module-name ';'
|
2016-08-19 05:59:42 +08:00
|
|
|
/// [ModTS] module-import-declaration:
|
2016-08-19 09:43:06 +08:00
|
|
|
/// 'import' module-name attribute-specifier-seq[opt] ';'
|
2019-04-14 16:06:59 +08:00
|
|
|
/// [C++2a] module-import-declaration:
|
|
|
|
/// 'export'[opt] 'import' module-name
|
|
|
|
/// attribute-specifier-seq[opt] ';'
|
|
|
|
/// 'export'[opt] 'import' module-partition
|
|
|
|
/// attribute-specifier-seq[opt] ';'
|
|
|
|
/// 'export'[opt] 'import' header-name
|
|
|
|
/// attribute-specifier-seq[opt] ';'
|
2017-11-21 17:42:42 +08:00
|
|
|
Decl *Parser::ParseModuleImport(SourceLocation AtLoc) {
|
2019-04-14 16:06:59 +08:00
|
|
|
SourceLocation StartLoc = AtLoc.isInvalid() ? Tok.getLocation() : AtLoc;
|
|
|
|
|
|
|
|
SourceLocation ExportLoc;
|
|
|
|
TryConsumeToken(tok::kw_export, ExportLoc);
|
|
|
|
|
|
|
|
assert((AtLoc.isInvalid() ? Tok.isOneOf(tok::kw_import, tok::identifier)
|
2016-08-19 05:59:42 +08:00
|
|
|
: Tok.isObjCAtKeyword(tok::objc_import)) &&
|
2011-09-01 02:19:09 +08:00
|
|
|
"Improper start to module import");
|
2018-06-28 04:29:36 +08:00
|
|
|
bool IsObjCAtImport = Tok.isObjCAtKeyword(tok::objc_import);
|
2011-08-27 07:56:07 +08:00
|
|
|
SourceLocation ImportLoc = ConsumeToken();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
|
2019-04-14 16:06:59 +08:00
|
|
|
Module *HeaderUnit = nullptr;
|
|
|
|
|
|
|
|
if (Tok.is(tok::header_name)) {
|
|
|
|
// This is a header import that the preprocessor decided we should skip
|
|
|
|
// because it was malformed in some way. Parse and ignore it; it's already
|
|
|
|
// been diagnosed.
|
|
|
|
ConsumeToken();
|
|
|
|
} else if (Tok.is(tok::annot_header_unit)) {
|
|
|
|
// This is a header import that the preprocessor mapped to a module import.
|
|
|
|
HeaderUnit = reinterpret_cast<Module *>(Tok.getAnnotationValue());
|
|
|
|
ConsumeAnnotationToken();
|
|
|
|
} else if (getLangOpts().CPlusPlusModules && Tok.is(tok::colon)) {
|
|
|
|
SourceLocation ColonLoc = ConsumeToken();
|
|
|
|
if (ParseModuleName(ImportLoc, Path, /*IsImport*/true))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// FIXME: Support module partition import.
|
|
|
|
Diag(ColonLoc, diag::err_unsupported_module_partition)
|
|
|
|
<< SourceRange(ColonLoc, Path.back().second);
|
2016-08-19 09:43:06 +08:00
|
|
|
return nullptr;
|
2019-04-14 16:06:59 +08:00
|
|
|
} else {
|
|
|
|
if (ParseModuleName(ImportLoc, Path, /*IsImport*/true))
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-08-19 05:59:42 +08:00
|
|
|
|
|
|
|
ParsedAttributesWithRange Attrs(AttrFactory);
|
|
|
|
MaybeParseCXX11Attributes(Attrs);
|
|
|
|
// We don't support any module import attributes yet.
|
|
|
|
ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_import_attr);
|
2013-05-24 13:44:08 +08:00
|
|
|
|
|
|
|
if (PP.hadModuleLoaderFatalFailure()) {
|
|
|
|
// With a fatal failure in the module loader, we abort parsing.
|
|
|
|
cutOffParsing();
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
2013-05-24 13:44:08 +08:00
|
|
|
}
|
|
|
|
|
2019-04-14 16:06:59 +08:00
|
|
|
DeclResult Import;
|
|
|
|
if (HeaderUnit)
|
|
|
|
Import =
|
|
|
|
Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, HeaderUnit);
|
|
|
|
else if (!Path.empty())
|
|
|
|
Import = Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Path);
|
2011-08-27 07:56:07 +08:00
|
|
|
ExpectAndConsumeSemi(diag::err_module_expected_semi);
|
|
|
|
if (Import.isInvalid())
|
2016-01-16 07:43:25 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2018-06-28 04:29:36 +08:00
|
|
|
// Using '@import' in framework headers requires modules to be enabled so that
|
|
|
|
// the header is parseable. Emit a warning to make the user aware.
|
|
|
|
if (IsObjCAtImport && AtLoc.isValid()) {
|
|
|
|
auto &SrcMgr = PP.getSourceManager();
|
|
|
|
auto *FE = SrcMgr.getFileEntryForID(SrcMgr.getFileID(AtLoc));
|
|
|
|
if (FE && llvm::sys::path::parent_path(FE->getDir()->getName())
|
|
|
|
.endswith(".framework"))
|
|
|
|
Diags.Report(AtLoc, diag::warn_atimport_in_framework_header);
|
|
|
|
}
|
|
|
|
|
2017-11-21 17:42:42 +08:00
|
|
|
return Import.get();
|
2011-08-27 07:56:07 +08:00
|
|
|
}
|
2011-10-13 00:37:45 +08:00
|
|
|
|
2016-08-19 09:43:06 +08:00
|
|
|
/// Parse a C++ Modules TS / Objective-C module name (both forms use the same
|
|
|
|
/// grammar).
|
|
|
|
///
|
|
|
|
/// module-name:
|
|
|
|
/// module-name-qualifier[opt] identifier
|
|
|
|
/// module-name-qualifier:
|
|
|
|
/// module-name-qualifier[opt] identifier '.'
|
|
|
|
bool Parser::ParseModuleName(
|
|
|
|
SourceLocation UseLoc,
|
|
|
|
SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>> &Path,
|
|
|
|
bool IsImport) {
|
|
|
|
// Parse the module path.
|
|
|
|
while (true) {
|
|
|
|
if (!Tok.is(tok::identifier)) {
|
|
|
|
if (Tok.is(tok::code_completion)) {
|
|
|
|
Actions.CodeCompleteModuleImport(UseLoc, Path);
|
|
|
|
cutOffParsing();
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2016-08-19 09:43:06 +08:00
|
|
|
Diag(Tok, diag::err_module_expected_ident) << IsImport;
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2016-08-19 09:43:06 +08:00
|
|
|
// Record this part of the module path.
|
|
|
|
Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()));
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::period))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Try recover parser when module annotation appears where it must not
|
2015-09-19 13:32:57 +08:00
|
|
|
/// be found.
|
|
|
|
/// \returns false if the recover was successful and parsing may be continued, or
|
|
|
|
/// true if parser must bail out to top level and handle the token there.
|
|
|
|
bool Parser::parseMisplacedModuleImport() {
|
|
|
|
while (true) {
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::annot_module_end:
|
2016-12-06 08:12:39 +08:00
|
|
|
// If we recovered from a misplaced module begin, we expect to hit a
|
|
|
|
// misplaced module end too. Stay in the current context when this
|
|
|
|
// happens.
|
|
|
|
if (MisplacedModuleBeginCount) {
|
|
|
|
--MisplacedModuleBeginCount;
|
|
|
|
Actions.ActOnModuleEnd(Tok.getLocation(),
|
|
|
|
reinterpret_cast<Module *>(
|
|
|
|
Tok.getAnnotationValue()));
|
2017-05-19 03:21:48 +08:00
|
|
|
ConsumeAnnotationToken();
|
2016-12-06 08:12:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
2015-09-19 13:32:57 +08:00
|
|
|
// Inform caller that recovery failed, the error must be handled at upper
|
2016-12-06 08:12:39 +08:00
|
|
|
// level. This will generate the desired "missing '}' at end of module"
|
|
|
|
// diagnostics on the way out.
|
2015-09-19 13:32:57 +08:00
|
|
|
return true;
|
|
|
|
case tok::annot_module_begin:
|
2016-12-06 08:12:39 +08:00
|
|
|
// Recover by entering the module (Sema will diagnose).
|
|
|
|
Actions.ActOnModuleBegin(Tok.getLocation(),
|
|
|
|
reinterpret_cast<Module *>(
|
|
|
|
Tok.getAnnotationValue()));
|
2017-05-19 03:21:48 +08:00
|
|
|
ConsumeAnnotationToken();
|
2016-12-06 08:12:39 +08:00
|
|
|
++MisplacedModuleBeginCount;
|
|
|
|
continue;
|
2015-09-19 13:32:57 +08:00
|
|
|
case tok::annot_module_include:
|
|
|
|
// Module import found where it should not be, for instance, inside a
|
|
|
|
// namespace. Recover by importing the module.
|
|
|
|
Actions.ActOnModuleInclude(Tok.getLocation(),
|
|
|
|
reinterpret_cast<Module *>(
|
2016-12-06 08:12:39 +08:00
|
|
|
Tok.getAnnotationValue()));
|
2017-05-19 03:21:48 +08:00
|
|
|
ConsumeAnnotationToken();
|
2015-09-19 13:32:57 +08:00
|
|
|
// If there is another module import, process it.
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-07 05:18:07 +08:00
|
|
|
bool BalancedDelimiterTracker::diagnoseOverflow() {
|
2013-02-22 09:59:51 +08:00
|
|
|
P.Diag(P.Tok, diag::err_bracket_depth_exceeded)
|
|
|
|
<< P.getLangOpts().BracketDepth;
|
|
|
|
P.Diag(P.Tok, diag::note_bracket_depth);
|
2013-11-23 12:06:09 +08:00
|
|
|
P.cutOffParsing();
|
|
|
|
return true;
|
2011-10-13 00:37:45 +08:00
|
|
|
}
|
|
|
|
|
2012-06-07 05:18:07 +08:00
|
|
|
bool BalancedDelimiterTracker::expectAndConsume(unsigned DiagID,
|
2014-01-01 11:08:43 +08:00
|
|
|
const char *Msg,
|
|
|
|
tok::TokenKind SkipToTok) {
|
2011-10-13 00:37:45 +08:00
|
|
|
LOpen = P.Tok.getLocation();
|
2014-01-01 11:08:43 +08:00
|
|
|
if (P.ExpectAndConsume(Kind, DiagID, Msg)) {
|
|
|
|
if (SkipToTok != tok::unknown)
|
|
|
|
P.SkipUntil(SkipToTok, Parser::StopAtSemi);
|
2012-03-08 09:00:17 +08:00
|
|
|
return true;
|
2014-01-01 11:08:43 +08:00
|
|
|
}
|
|
|
|
|
2018-03-17 04:17:28 +08:00
|
|
|
if (getDepth() < P.getLangOpts().BracketDepth)
|
2012-03-08 09:00:17 +08:00
|
|
|
return false;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2012-03-08 09:00:17 +08:00
|
|
|
return diagnoseOverflow();
|
2011-10-13 00:37:45 +08:00
|
|
|
}
|
|
|
|
|
2012-06-07 05:18:07 +08:00
|
|
|
bool BalancedDelimiterTracker::diagnoseMissingClose() {
|
2012-03-08 09:00:17 +08:00
|
|
|
assert(!P.Tok.is(Close) && "Should have consumed closing delimiter");
|
2013-12-24 17:48:30 +08:00
|
|
|
|
2015-09-19 13:32:57 +08:00
|
|
|
if (P.Tok.is(tok::annot_module_end))
|
|
|
|
P.Diag(P.Tok, diag::err_missing_before_module_end) << Close;
|
|
|
|
else
|
|
|
|
P.Diag(P.Tok, diag::err_expected) << Close;
|
2013-12-24 17:48:30 +08:00
|
|
|
P.Diag(LOpen, diag::note_matching) << Kind;
|
2013-10-15 09:34:54 +08:00
|
|
|
|
|
|
|
// If we're not already at some kind of closing bracket, skip to our closing
|
|
|
|
// token.
|
|
|
|
if (P.Tok.isNot(tok::r_paren) && P.Tok.isNot(tok::r_brace) &&
|
|
|
|
P.Tok.isNot(tok::r_square) &&
|
2015-01-12 11:36:37 +08:00
|
|
|
P.SkipUntil(Close, FinalToken,
|
2013-12-19 03:10:49 +08:00
|
|
|
Parser::StopAtSemi | Parser::StopBeforeMatch) &&
|
2013-10-15 09:34:54 +08:00
|
|
|
P.Tok.is(Close))
|
2012-11-08 03:08:05 +08:00
|
|
|
LClose = P.ConsumeAnyToken();
|
2011-10-13 00:37:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
2011-10-25 06:31:10 +08:00
|
|
|
|
2012-06-07 05:18:07 +08:00
|
|
|
void BalancedDelimiterTracker::skipToEnd() {
|
2013-12-19 03:10:49 +08:00
|
|
|
P.SkipUntil(Close, Parser::StopBeforeMatch);
|
2013-10-15 09:34:54 +08:00
|
|
|
consumeClose();
|
2011-10-25 06:31:10 +08:00
|
|
|
}
|