2006-08-11 02:43:39 +08:00
|
|
|
//===--- Parser.cpp - C Language Family Parser ----------------------------===//
|
2006-07-31 09:59:18 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-07-31 09:59:18 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Parser interfaces.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Parse/Parser.h"
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Parse/ParseDiagnostic.h"
|
2010-08-21 02:27:03 +08:00
|
|
|
#include "clang/Sema/DeclSpec.h"
|
|
|
|
#include "clang/Sema/Scope.h"
|
|
|
|
#include "clang/Sema/ParsedTemplate.h"
|
2009-03-05 15:24:28 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-12-10 08:21:05 +08:00
|
|
|
#include "RAIIObjectsForParser.h"
|
2008-10-05 03:21:03 +08:00
|
|
|
#include "ParsePragma.h"
|
2011-04-23 06:18:13 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2011-05-08 01:30:27 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2006-07-31 09:59:18 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2011-10-21 11:57:52 +08:00
|
|
|
IdentifierInfo *Parser::getSEHExceptKeyword() {
|
|
|
|
// __except is accepted as a (contextual) keyword
|
|
|
|
if (!Ident__except && (getLang().MicrosoftExt || getLang().Borland))
|
|
|
|
Ident__except = PP.getIdentifierInfo("__except");
|
|
|
|
|
|
|
|
return Ident__except;
|
|
|
|
}
|
|
|
|
|
2010-08-27 07:41:50 +08:00
|
|
|
Parser::Parser(Preprocessor &pp, Sema &actions)
|
2011-03-22 09:15:17 +08:00
|
|
|
: PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
|
2010-09-15 22:51:05 +08:00
|
|
|
GreaterThanIsOperator(true), ColonIsSacred(false),
|
|
|
|
InMessageExpression(false), TemplateParameterDepth(0) {
|
2006-10-14 13:19:21 +08:00
|
|
|
Tok.setKind(tok::eof);
|
2010-07-03 01:43:08 +08:00
|
|
|
Actions.CurScope = 0;
|
2007-07-15 08:04:39 +08:00
|
|
|
NumCachedScopes = 0;
|
2006-08-07 05:55:29 +08:00
|
|
|
ParenCount = BracketCount = BraceCount = 0;
|
2010-08-21 17:40:31 +08:00
|
|
|
ObjCImpDecl = 0;
|
2008-10-05 03:21:03 +08:00
|
|
|
|
|
|
|
// Add #pragma handlers. These are removed and destroyed in the
|
|
|
|
// destructor.
|
2010-08-01 03:17:07 +08:00
|
|
|
AlignHandler.reset(new PragmaAlignHandler(actions));
|
|
|
|
PP.AddPragmaHandler(AlignHandler.get());
|
|
|
|
|
2010-08-05 14:57:20 +08:00
|
|
|
GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler(actions));
|
|
|
|
PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
|
|
|
|
|
2010-07-13 17:07:17 +08:00
|
|
|
OptionsHandler.reset(new PragmaOptionsHandler(actions));
|
|
|
|
PP.AddPragmaHandler(OptionsHandler.get());
|
|
|
|
|
|
|
|
PackHandler.reset(new PragmaPackHandler(actions));
|
|
|
|
PP.AddPragmaHandler(PackHandler.get());
|
2011-04-26 02:49:15 +08:00
|
|
|
|
|
|
|
MSStructHandler.reset(new PragmaMSStructHandler(actions));
|
|
|
|
PP.AddPragmaHandler(MSStructHandler.get());
|
2010-07-13 17:07:17 +08:00
|
|
|
|
|
|
|
UnusedHandler.reset(new PragmaUnusedHandler(actions, *this));
|
|
|
|
PP.AddPragmaHandler(UnusedHandler.get());
|
|
|
|
|
|
|
|
WeakHandler.reset(new PragmaWeakHandler(actions));
|
|
|
|
PP.AddPragmaHandler(WeakHandler.get());
|
2011-02-14 09:42:35 +08:00
|
|
|
|
|
|
|
FPContractHandler.reset(new PragmaFPContractHandler(actions, *this));
|
|
|
|
PP.AddPragmaHandler("STDC", FPContractHandler.get());
|
2011-02-14 09:42:53 +08:00
|
|
|
|
|
|
|
if (getLang().OpenCL) {
|
|
|
|
OpenCLExtensionHandler.reset(
|
|
|
|
new PragmaOpenCLExtensionHandler(actions, *this));
|
|
|
|
PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
|
|
|
|
|
|
|
|
PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
|
|
|
|
}
|
2010-08-25 03:08:16 +08:00
|
|
|
|
|
|
|
PP.setCodeCompletionHandler(*this);
|
2006-08-06 06:46:42 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 15:24:28 +08:00
|
|
|
/// If a crash happens while the parser is active, print out a line indicating
|
|
|
|
/// what the current token is.
|
2011-07-23 18:55:15 +08:00
|
|
|
void PrettyStackTraceParserEntry::print(raw_ostream &OS) const {
|
2009-03-05 15:24:28 +08:00
|
|
|
const Token &Tok = P.getCurToken();
|
2009-03-05 15:27:50 +08:00
|
|
|
if (Tok.is(tok::eof)) {
|
2009-03-05 15:24:28 +08:00
|
|
|
OS << "<eof> parser at end of file\n";
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-05 15:27:50 +08:00
|
|
|
if (Tok.getLocation().isInvalid()) {
|
|
|
|
OS << "<unknown> parser at unknown location\n";
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-05 15:24:28 +08:00
|
|
|
const Preprocessor &PP = P.getPreprocessor();
|
|
|
|
Tok.getLocation().print(OS, PP.getSourceManager());
|
2009-10-17 14:13:04 +08:00
|
|
|
if (Tok.isAnnotation())
|
|
|
|
OS << ": at annotation token \n";
|
|
|
|
else
|
|
|
|
OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n";
|
2008-12-30 11:27:21 +08:00
|
|
|
}
|
2006-07-31 09:59:18 +08:00
|
|
|
|
2009-03-05 15:24:28 +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
|
|
|
}
|
|
|
|
|
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
|
|
|
/// \brief Emits a diagnostic suggesting parentheses surrounding a
|
|
|
|
/// given range.
|
|
|
|
///
|
|
|
|
/// \param Loc The location where we'll emit the diagnostic.
|
|
|
|
/// \param Loc The kind of diagnostic to emit.
|
|
|
|
/// \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) {
|
|
|
|
case tok::semi: return Tok.is(tok::colon); // : for ;
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-13 03:26:13 +08:00
|
|
|
/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
|
|
|
|
/// input. If so, it is consumed and false is returned.
|
|
|
|
///
|
|
|
|
/// If the input is malformed, this emits the specified diagnostic. Next, if
|
|
|
|
/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
|
|
|
|
/// returned.
|
|
|
|
bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
|
2006-08-15 11:41:14 +08:00
|
|
|
const char *Msg, tok::TokenKind SkipToTok) {
|
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();
|
|
|
|
Diag(Loc, DiagID)
|
|
|
|
<< Msg
|
|
|
|
<< FixItHint::CreateReplacement(SourceRange(Loc),
|
|
|
|
getTokenSimpleSpelling(ExpectedTok));
|
|
|
|
ConsumeAnyToken();
|
|
|
|
|
|
|
|
// Pretend there wasn't a problem.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
const char *Spelling = 0;
|
2009-02-28 01:53:17 +08:00
|
|
|
SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
|
2009-09-09 23:08:12 +08:00
|
|
|
if (EndLoc.isValid() &&
|
2009-02-28 01:53:17 +08:00
|
|
|
(Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) {
|
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
|
|
|
// Show what code to insert to fix this problem.
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(EndLoc, DiagID)
|
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
|
|
|
<< Msg
|
2010-04-01 01:46:05 +08:00
|
|
|
<< FixItHint::CreateInsertion(EndLoc, Spelling);
|
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
|
|
|
} else
|
|
|
|
Diag(Tok, DiagID) << Msg;
|
|
|
|
|
2006-08-13 03:26:13 +08:00
|
|
|
if (SkipToTok != tok::unknown)
|
|
|
|
SkipUntil(SkipToTok);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-07 23:23:11 +08:00
|
|
|
bool Parser::ExpectAndConsumeSemi(unsigned DiagID) {
|
|
|
|
if (Tok.is(tok::semi) || Tok.is(tok::code_completion)) {
|
|
|
|
ConsumeAnyToken();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) &&
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExpectAndConsume(tok::semi, DiagID);
|
|
|
|
}
|
|
|
|
|
2006-08-07 05:55:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Error recovery.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// SkipUntil - Read tokens until we get to the specified token, then consume
|
2007-07-25 01:03:04 +08:00
|
|
|
/// it (unless DontConsume is true). 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.
|
2007-04-28 03:12:15 +08:00
|
|
|
bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
|
2011-01-04 03:44:02 +08:00
|
|
|
bool StopAtSemi, bool DontConsume,
|
|
|
|
bool StopAtCodeCompletion) {
|
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.
|
|
|
|
for (unsigned i = 0; i != NumToks; ++i) {
|
2007-10-10 01:23:58 +08:00
|
|
|
if (Tok.is(Toks[i])) {
|
2007-04-28 03:12:15 +08:00
|
|
|
if (DontConsume) {
|
|
|
|
// 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
|
|
|
|
2006-08-07 05:55:29 +08:00
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::eof:
|
|
|
|
// Ran out of tokens.
|
|
|
|
return false;
|
2010-05-25 13:58:43 +08:00
|
|
|
|
|
|
|
case tok::code_completion:
|
2011-01-04 03:44:02 +08:00
|
|
|
if (!StopAtCodeCompletion)
|
|
|
|
ConsumeToken();
|
2010-05-25 13:58:43 +08:00
|
|
|
return false;
|
|
|
|
|
2006-08-07 05:55:29 +08:00
|
|
|
case tok::l_paren:
|
|
|
|
// Recursively skip properly-nested parens.
|
|
|
|
ConsumeParen();
|
2011-01-04 03:44:02 +08:00
|
|
|
SkipUntil(tok::r_paren, false, false, StopAtCodeCompletion);
|
2006-08-07 05:55:29 +08:00
|
|
|
break;
|
|
|
|
case tok::l_square:
|
|
|
|
// Recursively skip properly-nested square brackets.
|
|
|
|
ConsumeBracket();
|
2011-01-04 03:44:02 +08:00
|
|
|
SkipUntil(tok::r_square, false, false, StopAtCodeCompletion);
|
2006-08-07 05:55:29 +08:00
|
|
|
break;
|
|
|
|
case tok::l_brace:
|
|
|
|
// Recursively skip properly-nested braces.
|
|
|
|
ConsumeBrace();
|
2011-01-04 03:44:02 +08:00
|
|
|
SkipUntil(tok::r_brace, false, false, StopAtCodeCompletion);
|
2006-08-07 05:55:29 +08:00
|
|
|
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:
|
2006-08-11 14:40:25 +08:00
|
|
|
if (ParenCount && !isFirstTokenSkipped)
|
|
|
|
return false; // Matches something.
|
2006-08-07 05:55:29 +08:00
|
|
|
ConsumeParen();
|
|
|
|
break;
|
|
|
|
case tok::r_square:
|
2006-08-11 14:40:25 +08:00
|
|
|
if (BracketCount && !isFirstTokenSkipped)
|
|
|
|
return false; // Matches something.
|
2006-08-07 05:55:29 +08:00
|
|
|
ConsumeBracket();
|
|
|
|
break;
|
|
|
|
case tok::r_brace:
|
2006-08-11 14:40:25 +08:00
|
|
|
if (BraceCount && !isFirstTokenSkipped)
|
|
|
|
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::string_literal:
|
2006-10-06 13:22:26 +08:00
|
|
|
case tok::wide_string_literal:
|
2011-07-27 13:40:30 +08:00
|
|
|
case tok::utf8_string_literal:
|
|
|
|
case tok::utf16_string_literal:
|
|
|
|
case tok::utf32_string_literal:
|
2006-08-07 05:55:29 +08:00
|
|
|
ConsumeStringToken();
|
|
|
|
break;
|
2011-02-23 08:11:21 +08:00
|
|
|
|
|
|
|
case tok::at:
|
|
|
|
return false;
|
|
|
|
|
2006-08-07 05:55:29 +08:00
|
|
|
case tok::semi:
|
|
|
|
if (StopAtSemi)
|
|
|
|
return false;
|
|
|
|
// FALL THROUGH.
|
|
|
|
default:
|
|
|
|
// Skip this token.
|
|
|
|
ConsumeToken();
|
|
|
|
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.
|
2010-07-03 01:43:08 +08:00
|
|
|
if (!getCurScope()->decl_empty())
|
|
|
|
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)
|
|
|
|
: CurScope(ManageFlags ? Self->getCurScope() : 0) {
|
|
|
|
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();
|
|
|
|
Actions.CurScope = 0;
|
|
|
|
|
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
|
|
|
|
2011-04-23 06:18:13 +08:00
|
|
|
// Free LateParsedTemplatedFunction nodes.
|
|
|
|
for (LateParsedTemplateMapT::iterator it = LateParsedTemplateMap.begin();
|
|
|
|
it != LateParsedTemplateMap.end(); ++it)
|
|
|
|
delete it->second;
|
|
|
|
|
2011-11-29 16:14:54 +08:00
|
|
|
clearLateParsedObjCMethods();
|
|
|
|
|
2008-10-05 03:21:03 +08:00
|
|
|
// Remove the pragma handlers we installed.
|
2010-08-01 03:17:07 +08:00
|
|
|
PP.RemovePragmaHandler(AlignHandler.get());
|
|
|
|
AlignHandler.reset();
|
2010-08-05 14:57:20 +08:00
|
|
|
PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
|
|
|
|
GCCVisibilityHandler.reset();
|
2010-07-13 17:07:17 +08:00
|
|
|
PP.RemovePragmaHandler(OptionsHandler.get());
|
2010-05-27 07:29:06 +08:00
|
|
|
OptionsHandler.reset();
|
2010-07-13 17:07:17 +08:00
|
|
|
PP.RemovePragmaHandler(PackHandler.get());
|
2009-03-24 06:28:25 +08:00
|
|
|
PackHandler.reset();
|
2011-04-26 02:49:15 +08:00
|
|
|
PP.RemovePragmaHandler(MSStructHandler.get());
|
|
|
|
MSStructHandler.reset();
|
2010-07-13 17:07:17 +08:00
|
|
|
PP.RemovePragmaHandler(UnusedHandler.get());
|
2009-03-24 06:28:25 +08:00
|
|
|
UnusedHandler.reset();
|
2010-07-13 17:07:17 +08:00
|
|
|
PP.RemovePragmaHandler(WeakHandler.get());
|
2009-06-05 08:49:58 +08:00
|
|
|
WeakHandler.reset();
|
2011-02-14 09:42:53 +08:00
|
|
|
|
|
|
|
if (getLang().OpenCL) {
|
|
|
|
PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
|
|
|
|
OpenCLExtensionHandler.reset();
|
|
|
|
PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
|
|
|
|
}
|
|
|
|
|
2011-02-14 09:42:35 +08:00
|
|
|
PP.RemovePragmaHandler("STDC", FPContractHandler.get());
|
|
|
|
FPContractHandler.reset();
|
2010-08-25 03:08:16 +08:00
|
|
|
PP.clearCodeCompletionHandler();
|
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.
|
2010-07-03 01:43:08 +08:00
|
|
|
assert(getCurScope() == 0 && "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());
|
|
|
|
|
|
|
|
// Prime the lexer look-ahead.
|
|
|
|
ConsumeToken();
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2007-10-10 01:23:58 +08:00
|
|
|
if (Tok.is(tok::eof) &&
|
2007-08-25 13:47:03 +08:00
|
|
|
!getLang().CPlusPlus) // Empty source file is an extension in C
|
2006-11-10 13:19:25 +08:00
|
|
|
Diag(Tok, diag::ext_empty_source_file);
|
2008-06-20 03:28:49 +08:00
|
|
|
|
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.
|
2007-08-30 06:54:08 +08:00
|
|
|
if (getLang().ObjC1) {
|
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");
|
2007-08-30 06:54:08 +08:00
|
|
|
}
|
2008-08-15 06:04:54 +08:00
|
|
|
|
2011-09-08 09:46:34 +08:00
|
|
|
Ident_instancetype = 0;
|
2011-01-20 11:47:08 +08:00
|
|
|
Ident_final = 0;
|
|
|
|
Ident_override = 0;
|
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
|
|
|
|
|
|
|
if (getLang().AltiVec) {
|
|
|
|
Ident_vector = &PP.getIdentifierTable().get("vector");
|
|
|
|
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
|
|
|
|
|
|
|
Ident_introduced = 0;
|
|
|
|
Ident_deprecated = 0;
|
|
|
|
Ident_obsoleted = 0;
|
2011-03-26 11:35:55 +08:00
|
|
|
Ident_unavailable = 0;
|
2011-04-28 09:08:34 +08:00
|
|
|
|
2011-10-21 11:57:52 +08:00
|
|
|
Ident__except = 0;
|
|
|
|
|
2011-04-28 09:08:34 +08:00
|
|
|
Ident__exception_code = Ident__exception_info = Ident__abnormal_termination = 0;
|
|
|
|
Ident___exception_code = Ident___exception_info = Ident___abnormal_termination = 0;
|
|
|
|
Ident_GetExceptionCode = Ident_GetExceptionInfo = Ident_AbnormalTermination = 0;
|
|
|
|
|
|
|
|
if(getLang().Borland) {
|
|
|
|
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);
|
|
|
|
}
|
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.
|
2009-03-30 00:50:03 +08:00
|
|
|
bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) {
|
2011-06-22 14:09:49 +08:00
|
|
|
DelayedCleanupPoint CleanupRAII(TopLevelDeclCleanupPool);
|
2011-01-18 02:58:44 +08:00
|
|
|
|
|
|
|
while (Tok.is(tok::annot_pragma_unused))
|
|
|
|
HandlePragmaUnused();
|
|
|
|
|
2009-03-30 00:50:03 +08:00
|
|
|
Result = DeclGroupPtrTy();
|
2008-08-23 11:19:52 +08:00
|
|
|
if (Tok.is(tok::eof)) {
|
2011-04-23 06:18:13 +08:00
|
|
|
// Late template parsing can begin.
|
|
|
|
if (getLang().DelayedTemplateParsing)
|
|
|
|
Actions.SetLateTemplateParser(LateTemplateParserCallback, this);
|
|
|
|
|
2008-08-23 11:19:52 +08:00
|
|
|
Actions.ActOnEndOfTranslationUnit();
|
|
|
|
return true;
|
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2011-03-24 19:26:52 +08:00
|
|
|
ParsedAttributesWithRange attrs(AttrFactory);
|
2010-12-24 10:08:15 +08:00
|
|
|
MaybeParseCXX0XAttributes(attrs);
|
|
|
|
MaybeParseMicrosoftAttributes(attrs);
|
2010-10-11 20:59:39 +08:00
|
|
|
|
2010-12-24 10:08:15 +08:00
|
|
|
Result = ParseExternalDeclaration(attrs);
|
2006-08-17 15:04:37 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseTranslationUnit:
|
|
|
|
/// translation-unit: [C99 6.9]
|
2008-06-20 03:28:49 +08:00
|
|
|
/// external-declaration
|
|
|
|
/// translation-unit external-declaration
|
2006-08-17 15:04:37 +08:00
|
|
|
void Parser::ParseTranslationUnit() {
|
2008-12-10 14:34:36 +08:00
|
|
|
Initialize();
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2009-03-30 00:50:03 +08:00
|
|
|
DeclGroupPtrTy Res;
|
2007-11-30 07:05:20 +08:00
|
|
|
while (!ParseTopLevelDecl(Res))
|
2006-08-17 15:04:37 +08:00
|
|
|
/*parse them all*/;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-23 10:00:52 +08:00
|
|
|
ExitScope();
|
2010-07-03 01:43:08 +08:00
|
|
|
assert(getCurScope() == 0 && "Scope imbalance!");
|
2006-08-17 15:04:37 +08:00
|
|
|
}
|
|
|
|
|
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
|
2009-08-24 20:17:54 +08:00
|
|
|
/// [C++0x] empty-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 ';'
|
|
|
|
///
|
2009-08-24 20:17:54 +08:00
|
|
|
/// [C++0x] empty-declaration:
|
|
|
|
/// ';'
|
|
|
|
///
|
2009-09-04 14:33:52 +08:00
|
|
|
/// [C++0x/GNU] 'extern' 'template' declaration
|
2010-12-24 10:08:15 +08:00
|
|
|
Parser::DeclGroupPtrTy
|
|
|
|
Parser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
|
|
|
|
ParsingDeclSpec *DS) {
|
2011-06-22 14:09:49 +08:00
|
|
|
DelayedCleanupPoint CleanupRAII(TopLevelDeclCleanupPool);
|
2010-06-17 18:52:18 +08:00
|
|
|
ParenBraceBracketBalancer BalancerRAIIObj(*this);
|
2011-09-04 11:32:15 +08:00
|
|
|
|
|
|
|
if (PP.isCodeCompletionReached()) {
|
|
|
|
cutOffParsing();
|
|
|
|
return DeclGroupPtrTy();
|
|
|
|
}
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *SingleDecl = 0;
|
2006-07-31 09:59:18 +08:00
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::semi:
|
2011-10-15 13:09:34 +08:00
|
|
|
Diag(Tok, getLang().CPlusPlus0x ?
|
|
|
|
diag::warn_cxx98_compat_top_level_semi : diag::ext_top_level_semi)
|
|
|
|
<< FixItHint::CreateRemoval(Tok.getLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-31 09:59:18 +08:00
|
|
|
ConsumeToken();
|
2006-10-16 08:33:54 +08:00
|
|
|
// TODO: Invoke action for top-level semicolon.
|
2009-03-30 00:50:03 +08:00
|
|
|
return DeclGroupPtrTy();
|
2008-12-09 05:59:01 +08:00
|
|
|
case tok::r_brace:
|
|
|
|
Diag(Tok, diag::err_expected_external_declaration);
|
|
|
|
ConsumeBrace();
|
2009-03-30 00:50:03 +08:00
|
|
|
return DeclGroupPtrTy();
|
2008-12-09 05:59:01 +08:00
|
|
|
case tok::eof:
|
|
|
|
Diag(Tok, diag::err_expected_external_declaration);
|
2009-03-30 00:50:03 +08:00
|
|
|
return DeclGroupPtrTy();
|
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;
|
|
|
|
ExprResult Result(ParseSimpleAsm(&EndLoc));
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2008-02-08 08:23:11 +08:00
|
|
|
ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
|
|
|
|
"top-level asm block");
|
2008-02-08 08:33:21 +08:00
|
|
|
|
2009-03-30 00:50:03 +08:00
|
|
|
if (Result.isInvalid())
|
|
|
|
return DeclGroupPtrTy();
|
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:
|
2011-08-28 04:50:59 +08:00
|
|
|
return ParseObjCAtDirectives();
|
2009-03-30 00:50:03 +08:00
|
|
|
break;
|
2006-10-28 07:18:49 +08:00
|
|
|
case tok::minus:
|
|
|
|
case tok::plus:
|
2009-03-30 00:50:03 +08:00
|
|
|
if (!getLang().ObjC1) {
|
|
|
|
Diag(Tok, diag::err_expected_external_declaration);
|
|
|
|
ConsumeToken();
|
|
|
|
return DeclGroupPtrTy();
|
|
|
|
}
|
|
|
|
SingleDecl = ParseObjCMethodDefinition();
|
|
|
|
break;
|
2009-09-22 04:51:25 +08:00
|
|
|
case tok::code_completion:
|
2010-07-03 01:43:08 +08:00
|
|
|
Actions.CodeCompleteOrdinaryName(getCurScope(),
|
2010-08-27 07:41:50 +08:00
|
|
|
ObjCImpDecl? Sema::PCC_ObjCImplementation
|
|
|
|
: Sema::PCC_Namespace);
|
2011-09-04 11:32:15 +08:00
|
|
|
cutOffParsing();
|
|
|
|
return DeclGroupPtrTy();
|
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:
|
|
|
|
case tok::kw_export: // As in 'export 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:
|
2007-08-26 02:15:16 +08:00
|
|
|
// A function definition cannot start with a 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;
|
2010-09-29 04:42:35 +08:00
|
|
|
StmtVector Stmts(Actions);
|
2010-12-24 10:08:15 +08:00
|
|
|
return ParseDeclaration(Stmts, Declarator::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.
|
|
|
|
if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) {
|
|
|
|
Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
|
|
|
|
<< 0;
|
2010-08-28 07:12:46 +08:00
|
|
|
SourceLocation DeclEnd;
|
2010-09-29 04:42:35 +08:00
|
|
|
StmtVector Stmts(Actions);
|
2010-12-24 10:08:15 +08:00
|
|
|
return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
|
2010-12-02 04:32:20 +08:00
|
|
|
}
|
|
|
|
goto dont_know;
|
|
|
|
|
|
|
|
case tok::kw_inline:
|
|
|
|
if (getLang().CPlusPlus) {
|
|
|
|
tok::TokenKind NextKind = NextToken().getKind();
|
|
|
|
|
|
|
|
// Inline namespaces. Allowed as an extension even in C++03.
|
|
|
|
if (NextKind == tok::kw_namespace) {
|
|
|
|
SourceLocation DeclEnd;
|
|
|
|
StmtVector Stmts(Actions);
|
2010-12-24 10:08:15 +08:00
|
|
|
return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
|
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;
|
|
|
|
StmtVector Stmts(Actions);
|
2010-12-24 10:08:15 +08:00
|
|
|
return ParseDeclaration(Stmts, Declarator::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:
|
|
|
|
if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) {
|
|
|
|
// Extern templates
|
|
|
|
SourceLocation ExternLoc = ConsumeToken();
|
|
|
|
SourceLocation TemplateLoc = ConsumeToken();
|
2011-10-21 02:35:58 +08:00
|
|
|
Diag(ExternLoc, getLang().CPlusPlus0x ?
|
|
|
|
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(
|
|
|
|
ParseExplicitInstantiation(ExternLoc, TemplateLoc, DeclEnd));
|
|
|
|
}
|
|
|
|
// FIXME: Detect C++ linkage specifications here?
|
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();
|
2011-05-08 01:30:27 +08:00
|
|
|
return DeclGroupPtrTy();
|
|
|
|
|
2011-09-01 02:19:09 +08:00
|
|
|
case tok::kw___import_module__:
|
2011-08-27 07:56:07 +08:00
|
|
|
return ParseModuleImport();
|
|
|
|
|
2006-07-31 09:59:18 +08:00
|
|
|
default:
|
2010-08-28 07:12:46 +08:00
|
|
|
dont_know:
|
2006-07-31 09:59:18 +08:00
|
|
|
// We can't tell whether this is a function-definition or declaration yet.
|
2010-12-24 10:08:15 +08:00
|
|
|
if (DS) {
|
|
|
|
DS->takeAttributesFrom(attrs);
|
|
|
|
return ParseDeclarationOrFunctionDefinition(*DS);
|
|
|
|
} else {
|
|
|
|
return ParseDeclarationOrFunctionDefinition(attrs);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2009-05-13 05:31:51 +08:00
|
|
|
/// \brief Determine whether the current token, if it occurs after a
|
|
|
|
/// declarator, continues a declaration or declaration list.
|
2011-05-12 14:15:49 +08:00
|
|
|
bool Parser::isDeclarationAfterDeclarator() {
|
|
|
|
// Check for '= delete' or '= default'
|
|
|
|
if (getLang().CPlusPlus && Tok.is(tok::equal)) {
|
|
|
|
const Token &KW = NextToken();
|
|
|
|
if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
(getLang().CPlusPlus &&
|
|
|
|
Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Determine whether the current token, if it occurs after a
|
|
|
|
/// 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;
|
|
|
|
|
2010-07-12 06:42:07 +08:00
|
|
|
// Handle K&R C argument lists: int X(f) int f; {}
|
|
|
|
if (!getLang().CPlusPlus &&
|
2010-12-11 00:29:40 +08:00
|
|
|
Declarator.getFunctionTypeInfo().isKNRPrototype())
|
2010-07-12 06:42:07 +08:00
|
|
|
return isDeclarationSpecifier();
|
2011-05-12 14:15:49 +08:00
|
|
|
|
|
|
|
if (getLang().CPlusPlus && Tok.is(tok::equal)) {
|
|
|
|
const Token &KW = NextToken();
|
|
|
|
return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
|
|
|
|
}
|
2010-07-12 06:42:07 +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
|
|
|
}
|
|
|
|
|
2006-07-31 09:59:18 +08:00
|
|
|
/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
|
2006-07-31 13:09:04 +08:00
|
|
|
/// a declaration. We can't tell which we have until we read up to the
|
2008-12-24 10:52:09 +08:00
|
|
|
/// compound-statement in function-definition. TemplateParams, if
|
|
|
|
/// non-NULL, provides the template parameters when we're parsing a
|
2009-09-09 23:08:12 +08:00
|
|
|
/// 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]
|
2006-07-31 13:09:04 +08:00
|
|
|
/// [OMP] threadprivate-directive [TODO]
|
|
|
|
///
|
2009-03-30 00:50:03 +08:00
|
|
|
Parser::DeclGroupPtrTy
|
2009-12-10 05:39:38 +08:00
|
|
|
Parser::ParseDeclarationOrFunctionDefinition(ParsingDeclSpec &DS,
|
2009-11-21 16:43:09 +08:00
|
|
|
AccessSpecifier AS) {
|
2006-07-31 13:09:04 +08:00
|
|
|
// Parse the common declaration-specifiers piece.
|
2010-01-14 01:31:36 +08:00
|
|
|
ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
|
2008-06-20 03:28:49 +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)) {
|
2006-08-14 03:58:17 +08:00
|
|
|
ConsumeToken();
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
|
2009-11-04 10:18:39 +08:00
|
|
|
DS.complete(TheDecl);
|
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
|
|
|
|
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.
|
2007-10-10 01:23:58 +08:00
|
|
|
if (getLang().ObjC2 && 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) &&
|
2008-09-26 12:48:09 +08:00
|
|
|
!Tok.isObjCAtKeyword(tok::objc_protocol)) {
|
|
|
|
Diag(Tok, diag::err_objc_unexpected_attr);
|
2007-12-28 03:57:00 +08:00
|
|
|
SkipUntil(tok::semi); // FIXME: better skip?
|
2009-03-30 00:50:03 +08:00
|
|
|
return DeclGroupPtrTy();
|
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();
|
|
|
|
|
2008-01-03 03:17:38 +08:00
|
|
|
const char *PrevSpec = 0;
|
2009-08-04 04:12:06 +08:00
|
|
|
unsigned DiagID;
|
|
|
|
if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
|
|
|
|
Diag(AtLoc, DiagID) << PrevSpec;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *TheDecl = 0;
|
2008-09-26 12:48:09 +08:00
|
|
|
if (Tok.isObjCAtKeyword(tok::objc_protocol))
|
2009-03-30 00:50:03 +08:00
|
|
|
TheDecl = ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
|
|
|
|
else
|
|
|
|
TheDecl = ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
|
|
|
|
return Actions.ConvertDeclToDeclGroup(TheDecl);
|
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"'.
|
2008-01-12 15:08:43 +08:00
|
|
|
if (Tok.is(tok::string_literal) && getLang().CPlusPlus &&
|
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) {
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext);
|
2009-03-30 00:50:03 +08:00
|
|
|
return Actions.ConvertDeclToDeclGroup(TheDecl);
|
|
|
|
}
|
2008-01-12 15:05:38 +08:00
|
|
|
|
2009-11-04 03:26:08 +08:00
|
|
|
return ParseDeclGroup(DS, Declarator::FileContext, true);
|
2006-07-31 13:09:04 +08:00
|
|
|
}
|
|
|
|
|
2009-12-10 05:39:38 +08:00
|
|
|
Parser::DeclGroupPtrTy
|
2010-12-24 10:08:15 +08:00
|
|
|
Parser::ParseDeclarationOrFunctionDefinition(ParsedAttributes &attrs,
|
2009-12-10 05:39:38 +08:00
|
|
|
AccessSpecifier AS) {
|
|
|
|
ParsingDeclSpec DS(*this);
|
2010-12-24 10:08:15 +08:00
|
|
|
DS.takeAttributesFrom(attrs);
|
2011-08-23 01:59:19 +08:00
|
|
|
// Must temporarily exit the objective-c container scope for
|
|
|
|
// parsing c constructs and re-enter objc container scope
|
|
|
|
// afterwards.
|
|
|
|
ObjCDeclContextSwitch ObjCDC(*this);
|
|
|
|
|
|
|
|
return ParseDeclarationOrFunctionDefinition(DS, 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,
|
2010-12-11 00:29:40 +08:00
|
|
|
const ParsedTemplateInfo &TemplateInfo) {
|
2011-04-28 09:08:34 +08:00
|
|
|
// Poison the SEH identifiers so they are flagged as illegal in function bodies
|
|
|
|
PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
|
2010-12-11 00:29:40 +08:00
|
|
|
const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
|
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.
|
2009-02-28 02:35:46 +08:00
|
|
|
if (getLang().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;
|
2008-10-20 10:01:34 +08:00
|
|
|
D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
|
|
|
|
D.getIdentifierLoc(),
|
2009-08-04 04:12:06 +08:00
|
|
|
PrevSpec, DiagID);
|
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
|
|
|
|
2011-05-12 14:15:49 +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.
|
2011-02-04 19:57:16 +08:00
|
|
|
if (Tok.isNot(tok::l_brace) &&
|
|
|
|
(!getLang().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 '{'.
|
|
|
|
SkipUntil(tok::l_brace, true, true);
|
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))
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2006-08-09 13:47:47 +08:00
|
|
|
}
|
2008-06-20 03:28:49 +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.
|
|
|
|
if (getLang().DelayedTemplateParsing &&
|
|
|
|
TemplateInfo.Kind == ParsedTemplateInfo::Template) {
|
|
|
|
MultiTemplateParamsArg TemplateParameterLists(Actions,
|
|
|
|
TemplateInfo.TemplateParams->data(),
|
|
|
|
TemplateInfo.TemplateParams->size());
|
|
|
|
|
|
|
|
ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
|
|
|
|
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,
|
2011-10-11 08:28:45 +08:00
|
|
|
move(TemplateParameterLists));
|
2011-04-23 06:18:13 +08:00
|
|
|
D.complete(DP);
|
|
|
|
D.getMutableDeclSpec().abort();
|
|
|
|
|
|
|
|
if (DP) {
|
|
|
|
LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(this, DP);
|
|
|
|
|
|
|
|
FunctionDecl *FnD = 0;
|
|
|
|
if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(DP))
|
|
|
|
FnD = FunTmpl->getTemplatedDecl();
|
|
|
|
else
|
|
|
|
FnD = cast<FunctionDecl>(DP);
|
2011-04-23 07:20:44 +08:00
|
|
|
Actions.CheckForFunctionRedefinition(FnD);
|
2011-04-23 06:18:13 +08:00
|
|
|
|
|
|
|
LateParsedTemplateMap[FnD] = LPT;
|
|
|
|
Actions.MarkAsLateParsedTemplate(FnD);
|
|
|
|
LexTemplateFunctionForLateParsing(LPT->Toks);
|
|
|
|
} else {
|
|
|
|
CachedTokens Toks;
|
|
|
|
LexTemplateFunctionForLateParsing(Toks);
|
|
|
|
}
|
|
|
|
return DP;
|
|
|
|
}
|
|
|
|
|
2007-10-10 01:14:05 +08:00
|
|
|
// Enter a scope for the function body.
|
2008-12-10 14:34:36 +08:00
|
|
|
ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
|
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.
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Res = TemplateInfo.TemplateParams?
|
2010-07-03 01:43:08 +08:00
|
|
|
Actions.ActOnStartOfFunctionTemplateDef(getCurScope(),
|
2010-08-27 07:41:50 +08:00
|
|
|
MultiTemplateParamsArg(Actions,
|
2009-06-24 08:54:41 +08:00
|
|
|
TemplateInfo.TemplateParams->data(),
|
|
|
|
TemplateInfo.TemplateParams->size()),
|
|
|
|
D)
|
2010-07-03 01:43:08 +08:00
|
|
|
: Actions.ActOnStartOfFunctionDef(getCurScope(), D);
|
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);
|
|
|
|
|
|
|
|
// Break out of the ParsingDeclSpec context, too. This const_cast is
|
|
|
|
// safe because we're always the sole owner.
|
|
|
|
D.getMutableDeclSpec().abort();
|
|
|
|
|
2011-05-24 07:14:04 +08:00
|
|
|
if (Tok.is(tok::equal)) {
|
|
|
|
assert(getLang().CPlusPlus && "Only C++ function definitions have '='");
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
Actions.ActOnFinishFunctionBody(Res, 0, false);
|
|
|
|
|
|
|
|
bool Delete = false;
|
|
|
|
SourceLocation KWLoc;
|
|
|
|
if (Tok.is(tok::kw_delete)) {
|
2011-10-15 13:09:34 +08:00
|
|
|
Diag(Tok, getLang().CPlusPlus0x ?
|
|
|
|
diag::warn_cxx98_compat_deleted_function :
|
|
|
|
diag::warn_deleted_function_accepted_as_extension);
|
2011-05-24 07:14:04 +08:00
|
|
|
|
|
|
|
KWLoc = ConsumeToken();
|
|
|
|
Actions.SetDeclDeleted(Res, KWLoc);
|
|
|
|
Delete = true;
|
|
|
|
} else if (Tok.is(tok::kw_default)) {
|
2011-10-15 13:09:34 +08:00
|
|
|
Diag(Tok, getLang().CPlusPlus0x ?
|
|
|
|
diag::warn_cxx98_compat_defaulted_function :
|
|
|
|
diag::warn_defaulted_function_accepted_as_extension);
|
2011-05-24 07:14:04 +08:00
|
|
|
|
|
|
|
KWLoc = ConsumeToken();
|
|
|
|
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);
|
|
|
|
} else {
|
|
|
|
ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
|
|
|
|
Delete ? "delete" : "default", tok::semi);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
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();
|
2010-08-24 07:25:46 +08:00
|
|
|
Actions.ActOnFinishFunctionBody(Res, 0);
|
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
|
|
|
|
2011-03-17 01:05:57 +08:00
|
|
|
return ParseFunctionStatementBody(Res, BodyScope);
|
2006-08-07 14:31:38 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2009-01-10 06:42:13 +08:00
|
|
|
ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|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);
|
2006-12-03 16:41:30 +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.
|
2007-10-10 01:23:58 +08:00
|
|
|
if (Tok.is(tok::semi)) {
|
2006-12-03 16:41:30 +08:00
|
|
|
Diag(DSStart, diag::err_declaration_does_not_declare_param);
|
|
|
|
ConsumeToken();
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
if (DS.isThreadSpecified()) {
|
|
|
|
Diag(DS.getThreadSpecLoc(),
|
|
|
|
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.
|
|
|
|
Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
|
|
|
|
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);
|
2007-09-11 04:51:04 +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.
|
|
|
|
if (i == FTI.NumArgs) {
|
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
|
|
|
|
2006-12-03 16:41:30 +08:00
|
|
|
if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
|
|
|
|
// Reject redefinitions of parameters.
|
2008-04-08 12:40:51 +08:00
|
|
|
if (FTI.ArgInfo[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 {
|
2008-04-08 12:40:51 +08:00
|
|
|
FTI.ArgInfo[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
|
|
|
|
2006-12-03 16:41:30 +08:00
|
|
|
// Consume the comma.
|
|
|
|
ConsumeToken();
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2006-12-03 16:41:30 +08:00
|
|
|
// Parse the next declarator.
|
|
|
|
ParmDeclarator.clear();
|
|
|
|
ParseDeclarator(ParmDeclarator);
|
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2007-10-10 01:23:58 +08:00
|
|
|
if (Tok.is(tok::semi)) {
|
2006-12-03 16:41:30 +08:00
|
|
|
ConsumeToken();
|
|
|
|
} else {
|
|
|
|
Diag(Tok, diag::err_parse_error);
|
|
|
|
// Skip to end of block or statement
|
|
|
|
SkipUntil(tok::semi, true);
|
2007-10-10 01:23:58 +08:00
|
|
|
if (Tok.is(tok::semi))
|
2006-12-03 16:41:30 +08:00
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
///
|
|
|
|
/// [GNU] asm-string-literal:
|
|
|
|
/// string-literal
|
|
|
|
///
|
2010-08-24 14:29:42 +08:00
|
|
|
Parser::ExprResult Parser::ParseAsmStringLiteral() {
|
2011-12-02 08:35:46 +08:00
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::string_literal:
|
|
|
|
break;
|
|
|
|
case tok::wide_string_literal: {
|
|
|
|
SourceLocation L = Tok.getLocation();
|
|
|
|
Diag(Tok, diag::err_asm_operand_wide_string_literal)
|
|
|
|
<< SourceRange(L, L);
|
|
|
|
return ExprError();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
Diag(Tok, diag::err_expected_string_literal);
|
|
|
|
return ExprError();
|
2006-08-15 14:03:28 +08:00
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult Res(ParseStringLiteralExpression());
|
2008-12-10 08:02:53 +08:00
|
|
|
if (Res.isInvalid()) return move(Res);
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2008-12-10 08:02:53 +08:00
|
|
|
return move(Res);
|
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 ')'
|
|
|
|
///
|
2010-08-24 14:29:42 +08:00
|
|
|
Parser::ExprResult Parser::ParseSimpleAsm(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
|
|
|
|
2010-01-26 06:27:48 +08:00
|
|
|
if (Tok.is(tok::kw_volatile)) {
|
2010-01-26 07:12:50 +08:00
|
|
|
// Remove from the end of 'asm' to the end of 'volatile'.
|
|
|
|
SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
|
|
|
|
PP.getLocForEndOfToken(Tok.getLocation()));
|
|
|
|
|
|
|
|
Diag(Tok, diag::warn_file_asm_volatile)
|
2010-04-01 01:46:05 +08:00
|
|
|
<< 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
|
|
|
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult Result(ParseAsmStringLiteral());
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2009-02-10 02:23:29 +08:00
|
|
|
if (Result.isInvalid()) {
|
|
|
|
SkipUntil(tok::r_paren, true, true);
|
|
|
|
if (EndLoc)
|
|
|
|
*EndLoc = Tok.getLocation();
|
|
|
|
ConsumeAnyToken();
|
|
|
|
} else {
|
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();
|
2009-02-10 02:23:29 +08:00
|
|
|
}
|
2008-06-20 03:28:49 +08:00
|
|
|
|
2008-12-10 08:02:53 +08:00
|
|
|
return move(Result);
|
2006-08-15 11:41:14 +08:00
|
|
|
}
|
2006-10-28 07:18:49 +08:00
|
|
|
|
2011-06-22 14:09:49 +08:00
|
|
|
/// \brief Get the TemplateIdAnnotation from the token and put it in the
|
|
|
|
/// 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());
|
|
|
|
TopLevelDeclCleanupPool.delayMemberFunc< TemplateIdAnnotation,
|
|
|
|
&TemplateIdAnnotation::Destroy>(Id);
|
|
|
|
return Id;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
/// 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.
|
2011-10-11 09:02:41 +08:00
|
|
|
bool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext, bool NeedType) {
|
2009-09-09 23:08:12 +08:00
|
|
|
assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
|
2011-12-04 13:04:18 +08:00
|
|
|
|| Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)
|
|
|
|
|| Tok.is(tok::kw_decltype)) && "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)) {
|
|
|
|
// 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;
|
2011-11-08 01:33:42 +08:00
|
|
|
if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(),
|
|
|
|
/*EnteringContext=*/false,
|
2011-03-28 03:41:34 +08:00
|
|
|
0, /*IsTypename*/true))
|
2010-02-26 16:45:28 +08:00
|
|
|
return true;
|
|
|
|
if (!SS.isSet()) {
|
2011-09-18 01:15:52 +08:00
|
|
|
if (getLang().MicrosoftExt)
|
2011-04-24 19:24:13 +08:00
|
|
|
Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename);
|
|
|
|
else
|
|
|
|
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!
|
2010-07-03 01:43:08 +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);
|
2009-04-01 08:28:59 +08:00
|
|
|
if (TemplateId->Kind == TNK_Function_template) {
|
|
|
|
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
|
|
|
|
2011-02-28 06:46:49 +08:00
|
|
|
ASTTemplateArgsPtr TemplateArgsPtr(Actions,
|
|
|
|
TemplateId->getTemplateArgs(),
|
|
|
|
TemplateId->NumArgs);
|
|
|
|
|
|
|
|
Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
|
|
|
|
/*FIXME:*/SourceLocation(),
|
|
|
|
TemplateId->Template,
|
|
|
|
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);
|
2010-08-24 13:47:05 +08:00
|
|
|
setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get());
|
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.
|
|
|
|
bool wasScopeAnnotation = Tok.is(tok::annot_cxxscope);
|
|
|
|
|
2008-11-09 00:45:02 +08:00
|
|
|
CXXScopeSpec SS;
|
2008-11-27 05:41:52 +08:00
|
|
|
if (getLang().CPlusPlus)
|
2010-08-24 13:47:05 +08:00
|
|
|
if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
|
2010-02-26 16:45:28 +08:00
|
|
|
return true;
|
2008-11-09 00:45:02 +08:00
|
|
|
|
|
|
|
if (Tok.is(tok::identifier)) {
|
2011-10-11 09:02:41 +08:00
|
|
|
IdentifierInfo *CorrectedII = 0;
|
2009-01-05 09:49:50 +08:00
|
|
|
// Determine whether the identifier is a type name.
|
2010-08-24 13:47:05 +08:00
|
|
|
if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
|
|
|
|
Tok.getLocation(), getCurScope(),
|
2011-02-09 02:05:59 +08:00
|
|
|
&SS, false,
|
2011-03-02 02:12:44 +08:00
|
|
|
NextToken().is(tok::period),
|
|
|
|
ParsedType(),
|
2011-10-11 09:02:41 +08:00
|
|
|
/*NonTrivialTypeSourceInfo*/true,
|
|
|
|
NeedType ? &CorrectedII : NULL)) {
|
|
|
|
// A FixIt was applied as a result of typo correction
|
|
|
|
if (CorrectedII)
|
|
|
|
Tok.setIdentifierInfo(CorrectedII);
|
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());
|
|
|
|
if (SS.isNotEmpty()) // it was a C++ qualified type name.
|
|
|
|
Tok.setLocation(SS.getBeginLoc());
|
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
|
|
|
|
|
|
|
if (!getLang().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.
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
if (TemplateNameKind TNK
|
2010-08-06 20:11:11 +08:00
|
|
|
= Actions.isTemplateName(getCurScope(), SS,
|
|
|
|
/*hasTemplateKeyword=*/false, TemplateName,
|
2010-08-24 13:47:05 +08:00
|
|
|
/*ObjectType=*/ ParsedType(),
|
|
|
|
EnteringContext,
|
2010-08-06 20:11:11 +08:00
|
|
|
Template, MemberOfUnknownSpecialization)) {
|
2009-11-04 08:56:37 +08:00
|
|
|
// Consume the identifier.
|
|
|
|
ConsumeToken();
|
2011-03-02 08:47:37 +08:00
|
|
|
if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName)) {
|
2009-06-26 12:27:47 +08:00
|
|
|
// 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.
|
2010-02-26 16:45:28 +08:00
|
|
|
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.
|
2011-03-02 08:47:37 +08:00
|
|
|
AnnotateTemplateIdTokenAsType();
|
2010-02-26 16:45:28 +08:00
|
|
|
return false;
|
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.
|
|
|
|
// 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
|
|
|
|
PP.EnterToken(Tok);
|
|
|
|
Tok.setKind(tok::annot_cxxscope);
|
2011-02-25 01:54:50 +08:00
|
|
|
Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
|
2009-01-05 06:32:19 +08:00
|
|
|
Tok.setAnnotationRange(SS.getRange());
|
|
|
|
|
2009-12-19 08:35:18 +08:00
|
|
|
// 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 the state we were in before being called.
|
|
|
|
if (!wasScopeAnnotation)
|
|
|
|
PP.AnnotateCachedTokens(Tok);
|
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
|
2009-06-26 12:27:47 +08:00
|
|
|
/// true if the token was annotated or 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) {
|
2008-11-27 05:41:52 +08:00
|
|
|
assert(getLang().CPlusPlus &&
|
2009-01-05 06:32:19 +08:00
|
|
|
"Call sites of this function should be guarded by checking for C++");
|
2011-04-27 12:48:22 +08:00
|
|
|
assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
|
2011-12-04 13:04:18 +08:00
|
|
|
(Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) ||
|
|
|
|
Tok.is(tok::kw_decltype)) && "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;
|
2010-08-24 13:47:05 +08:00
|
|
|
if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), 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
|
|
|
|
2009-01-05 06:32:19 +08:00
|
|
|
// 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
|
|
|
|
PP.EnterToken(Tok);
|
|
|
|
Tok.setKind(tok::annot_cxxscope);
|
2011-02-25 01:54:50 +08:00
|
|
|
Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
|
2009-01-05 06:32:19 +08:00
|
|
|
Tok.setAnnotationRange(SS.getRange());
|
|
|
|
|
|
|
|
// 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;
|
2008-11-09 00:45:02 +08:00
|
|
|
}
|
2009-11-04 03:33:12 +08:00
|
|
|
|
2010-10-08 10:39:23 +08:00
|
|
|
bool Parser::isTokenEqualOrMistypedEqualEqual(unsigned DiagID) {
|
|
|
|
if (Tok.is(tok::equalequal)) {
|
|
|
|
// We have '==' in a context that we would expect a '='.
|
|
|
|
// The user probably made a typo, intending to type '='. Emit diagnostic,
|
|
|
|
// fixit hint to turn '==' -> '=' and continue as if the user typed '='.
|
|
|
|
Diag(Tok, DiagID)
|
|
|
|
<< FixItHint::CreateReplacement(SourceRange(Tok.getLocation()),
|
|
|
|
getTokenSimpleSpelling(tok::equal));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Tok.is(tok::equal);
|
|
|
|
}
|
|
|
|
|
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) {
|
2010-08-27 07:41:50 +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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-11-04 03:33:12 +08:00
|
|
|
// Anchor the Parser::FieldCallback vtable to this translation unit.
|
|
|
|
// We use a spurious method instead of the destructor because
|
|
|
|
// destroying FieldCallbacks can actually be slightly
|
|
|
|
// performance-sensitive.
|
|
|
|
void Parser::FieldCallback::_anchor() {
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::CodeCompletePreprocessorExpression() {
|
|
|
|
Actions.CodeCompletePreprocessorExpression();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
|
|
|
|
MacroInfo *MacroInfo,
|
|
|
|
unsigned ArgumentIndex) {
|
|
|
|
Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
|
|
|
|
ArgumentIndex);
|
2010-08-25 04:21:13 +08:00
|
|
|
}
|
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()) {
|
2011-10-25 06:31:10 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after)
|
|
|
|
<< (Result.IsIfExists? "__if_exists" : "__if_not_exists");
|
2011-05-08 01:30:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse nested-name-specifier.
|
2011-11-08 01:33:42 +08:00
|
|
|
ParseOptionalCXXScopeSpecifier(Result.SS, ParsedType(),
|
|
|
|
/*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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the unqualified-id.
|
2011-10-25 06:31:10 +08:00
|
|
|
if (ParseUnqualifiedId(Result.SS, false, true, true, ParsedType(),
|
|
|
|
Result.Name)) {
|
|
|
|
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;
|
2011-10-25 06:31:10 +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,
|
|
|
|
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;
|
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;
|
|
|
|
|
2011-10-25 06:31:10 +08:00
|
|
|
BalancedDelimiterTracker Braces(*this, tok::l_brace);
|
|
|
|
if (Braces.consumeOpen()) {
|
2011-05-08 01:30:27 +08:00
|
|
|
Diag(Tok, diag::err_expected_lbrace);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-25 06:31:10 +08:00
|
|
|
switch (Result.Behavior) {
|
|
|
|
case IEB_Parse:
|
|
|
|
// Parse declarations below.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IEB_Dependent:
|
|
|
|
llvm_unreachable("Cannot have a dependent external declaration");
|
|
|
|
|
|
|
|
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.
|
|
|
|
while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
|
2011-05-08 01:30:27 +08:00
|
|
|
ParsedAttributesWithRange attrs(AttrFactory);
|
|
|
|
MaybeParseCXX0XAttributes(attrs);
|
|
|
|
MaybeParseMicrosoftAttributes(attrs);
|
|
|
|
DeclGroupPtrTy Result = ParseExternalDeclaration(attrs);
|
|
|
|
if (Result && !getCurScope()->getParent())
|
|
|
|
Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
|
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
|
|
|
|
|
|
|
Parser::DeclGroupPtrTy Parser::ParseModuleImport() {
|
2011-09-01 02:19:09 +08:00
|
|
|
assert(Tok.is(tok::kw___import_module__) &&
|
|
|
|
"Improper start to module import");
|
2011-08-27 07:56:07 +08:00
|
|
|
SourceLocation ImportLoc = ConsumeToken();
|
|
|
|
|
2011-11-30 08:36:36 +08:00
|
|
|
llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
|
|
|
|
|
|
|
|
// Parse the module path.
|
|
|
|
do {
|
|
|
|
if (!Tok.is(tok::identifier)) {
|
|
|
|
Diag(Tok, diag::err_module_expected_ident);
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
return DeclGroupPtrTy();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record this part of the module path.
|
|
|
|
Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()));
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
if (Tok.is(tok::period)) {
|
|
|
|
ConsumeToken();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
} while (true);
|
2011-08-27 07:56:07 +08:00
|
|
|
|
2011-11-30 08:36:36 +08:00
|
|
|
DeclResult Import = Actions.ActOnModuleImport(ImportLoc, Path);
|
2011-08-27 07:56:07 +08:00
|
|
|
ExpectAndConsumeSemi(diag::err_module_expected_semi);
|
|
|
|
if (Import.isInvalid())
|
|
|
|
return DeclGroupPtrTy();
|
|
|
|
|
|
|
|
return Actions.ConvertDeclToDeclGroup(Import.get());
|
|
|
|
}
|
2011-10-13 00:37:45 +08:00
|
|
|
|
|
|
|
bool Parser::BalancedDelimiterTracker::consumeOpen() {
|
|
|
|
// Try to consume the token we are holding
|
|
|
|
if (P.Tok.is(Kind)) {
|
|
|
|
P.QuantityTracker.push(Kind);
|
|
|
|
Cleanup = true;
|
|
|
|
if (P.QuantityTracker.getDepth(Kind) < MaxDepth) {
|
|
|
|
LOpen = P.ConsumeAnyToken();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
P.Diag(P.Tok, diag::err_parser_impl_limit_overflow);
|
|
|
|
P.SkipUntil(tok::eof);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Parser::BalancedDelimiterTracker::expectAndConsume(unsigned DiagID,
|
|
|
|
const char *Msg,
|
|
|
|
tok::TokenKind SkipToToc ) {
|
|
|
|
LOpen = P.Tok.getLocation();
|
|
|
|
if (!P.ExpectAndConsume(Kind, DiagID, Msg, SkipToToc)) {
|
|
|
|
P.QuantityTracker.push(Kind);
|
|
|
|
Cleanup = true;
|
|
|
|
if (P.QuantityTracker.getDepth(Kind) < MaxDepth) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
P.Diag(P.Tok, diag::err_parser_impl_limit_overflow);
|
|
|
|
P.SkipUntil(tok::eof);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Parser::BalancedDelimiterTracker::consumeClose() {
|
|
|
|
if (P.Tok.is(Close)) {
|
|
|
|
LClose = P.ConsumeAnyToken();
|
|
|
|
if (Cleanup)
|
|
|
|
P.QuantityTracker.pop(Kind);
|
|
|
|
|
|
|
|
Cleanup = false;
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
const char *LHSName = "unknown";
|
|
|
|
diag::kind DID = diag::err_parse_error;
|
|
|
|
switch (Close) {
|
|
|
|
default: break;
|
|
|
|
case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
|
|
|
|
case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
|
|
|
|
case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
|
|
|
|
case tok::greater: LHSName = "<"; DID = diag::err_expected_greater; break;
|
|
|
|
case tok::greatergreatergreater:
|
|
|
|
LHSName = "<<<"; DID = diag::err_expected_ggg; break;
|
|
|
|
}
|
|
|
|
P.Diag(P.Tok, DID);
|
|
|
|
P.Diag(LOpen, diag::note_matching) << LHSName;
|
|
|
|
if (P.SkipUntil(Close))
|
|
|
|
LClose = P.Tok.getLocation();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2011-10-25 06:31:10 +08:00
|
|
|
|
|
|
|
void Parser::BalancedDelimiterTracker::skipToEnd() {
|
|
|
|
P.SkipUntil(Close, false);
|
|
|
|
Cleanup = false;
|
|
|
|
}
|