blob: dd339f53948b4d650a5ccd290d12af5e423920d3 [file] [log] [blame]
Chris Lattnereb8a28f2006-08-10 18:43:39 +00001//===--- Parser.cpp - C Language Family Parser ----------------------------===//
Chris Lattner0bb5f832006-07-31 01:59:18 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0bb5f832006-07-31 01:59:18 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000016#include "clang/Sema/DeclSpec.h"
17#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
Chris Lattnerbcfe4f72009-03-05 07:24:28 +000019#include "llvm/Support/raw_ostream.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Daniel Dunbar921b9682008-10-04 19:21:03 +000021#include "ParsePragma.h"
Francois Pichet1c229c02011-04-22 22:18:13 +000022#include "clang/AST/DeclTemplate.h"
Francois Picheta5b3fcb2011-05-07 17:30:27 +000023#include "clang/AST/ASTConsumer.h"
Chris Lattner0bb5f832006-07-31 01:59:18 +000024using namespace clang;
25
Douglas Gregor60060d62011-10-21 03:57:52 +000026IdentifierInfo *Parser::getSEHExceptKeyword() {
27 // __except is accepted as a (contextual) keyword
David Blaikiebbafb8a2012-03-11 07:00:24 +000028 if (!Ident__except && (getLangOpts().MicrosoftExt || getLangOpts().Borland))
Douglas Gregor60060d62011-10-21 03:57:52 +000029 Ident__except = PP.getIdentifierInfo("__except");
30
31 return Ident__except;
32}
33
John McCallfaf5fb42010-08-26 23:41:50 +000034Parser::Parser(Preprocessor &pp, Sema &actions)
Ted Kremenek4c9d46b2011-03-22 01:15:17 +000035 : PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
Douglas Gregore9bba4f2010-09-15 14:51:05 +000036 GreaterThanIsOperator(true), ColonIsSacred(false),
37 InMessageExpression(false), TemplateParameterDepth(0) {
Chris Lattner8c204872006-10-14 05:19:21 +000038 Tok.setKind(tok::eof);
Douglas Gregor0be31a22010-07-02 17:43:08 +000039 Actions.CurScope = 0;
Chris Lattner03928c72007-07-15 00:04:39 +000040 NumCachedScopes = 0;
Chris Lattnereec40f92006-08-06 21:55:29 +000041 ParenCount = BracketCount = BraceCount = 0;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +000042 CurParsedObjCImpl = 0;
Daniel Dunbar921b9682008-10-04 19:21:03 +000043
44 // Add #pragma handlers. These are removed and destroyed in the
45 // destructor.
Daniel Dunbarcb82acb2010-07-31 19:17:07 +000046 AlignHandler.reset(new PragmaAlignHandler(actions));
47 PP.AddPragmaHandler(AlignHandler.get());
48
Eli Friedman570024a2010-08-05 06:57:20 +000049 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler(actions));
50 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
51
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000052 OptionsHandler.reset(new PragmaOptionsHandler(actions));
53 PP.AddPragmaHandler(OptionsHandler.get());
Daniel Dunbar75c9be72010-05-26 23:29:06 +000054
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000055 PackHandler.reset(new PragmaPackHandler(actions));
56 PP.AddPragmaHandler(PackHandler.get());
Fariborz Jahanian743dda42011-04-25 18:49:15 +000057
58 MSStructHandler.reset(new PragmaMSStructHandler(actions));
59 PP.AddPragmaHandler(MSStructHandler.get());
Mike Stump11289f42009-09-09 15:08:12 +000060
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000061 UnusedHandler.reset(new PragmaUnusedHandler(actions, *this));
62 PP.AddPragmaHandler(UnusedHandler.get());
Eli Friedmanf5867dd2009-06-05 00:49:58 +000063
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +000064 WeakHandler.reset(new PragmaWeakHandler(actions));
65 PP.AddPragmaHandler(WeakHandler.get());
Peter Collingbourne564c0fa2011-02-14 01:42:35 +000066
David Chisnall0867d9c2012-02-18 16:12:34 +000067 RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler(actions));
68 PP.AddPragmaHandler(RedefineExtnameHandler.get());
69
Peter Collingbourne564c0fa2011-02-14 01:42:35 +000070 FPContractHandler.reset(new PragmaFPContractHandler(actions, *this));
71 PP.AddPragmaHandler("STDC", FPContractHandler.get());
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +000072
David Blaikiebbafb8a2012-03-11 07:00:24 +000073 if (getLangOpts().OpenCL) {
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +000074 OpenCLExtensionHandler.reset(
75 new PragmaOpenCLExtensionHandler(actions, *this));
76 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
77
78 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
79 }
Douglas Gregor3a7ad252010-08-24 19:08:16 +000080
81 PP.setCodeCompletionHandler(*this);
Chris Lattner971c6b62006-08-05 22:46:42 +000082}
83
Chris Lattnerbcfe4f72009-03-05 07:24:28 +000084/// If a crash happens while the parser is active, print out a line indicating
85/// what the current token is.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000086void PrettyStackTraceParserEntry::print(raw_ostream &OS) const {
Chris Lattnerbcfe4f72009-03-05 07:24:28 +000087 const Token &Tok = P.getCurToken();
Chris Lattnerf02db352009-03-05 07:27:50 +000088 if (Tok.is(tok::eof)) {
Chris Lattnerbcfe4f72009-03-05 07:24:28 +000089 OS << "<eof> parser at end of file\n";
90 return;
91 }
Mike Stump11289f42009-09-09 15:08:12 +000092
Chris Lattnerf02db352009-03-05 07:27:50 +000093 if (Tok.getLocation().isInvalid()) {
94 OS << "<unknown> parser at unknown location\n";
95 return;
96 }
Mike Stump11289f42009-09-09 15:08:12 +000097
Chris Lattnerbcfe4f72009-03-05 07:24:28 +000098 const Preprocessor &PP = P.getPreprocessor();
99 Tok.getLocation().print(OS, PP.getSourceManager());
Daniel Dunbar68d9d7b2009-10-17 06:13:04 +0000100 if (Tok.isAnnotation())
101 OS << ": at annotation token \n";
102 else
103 OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n";
Douglas Gregord7c4d982008-12-30 03:27:21 +0000104}
Chris Lattner0bb5f832006-07-31 01:59:18 +0000105
Chris Lattnerbcfe4f72009-03-05 07:24:28 +0000106
Chris Lattner427c9c12008-11-22 00:59:29 +0000107DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000108 return Diags.Report(Loc, DiagID);
Chris Lattner6d29c102008-11-18 07:48:38 +0000109}
110
Chris Lattner427c9c12008-11-22 00:59:29 +0000111DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000112 return Diag(Tok.getLocation(), DiagID);
Chris Lattner0bb5f832006-07-31 01:59:18 +0000113}
114
Douglas Gregor87f95b02009-02-26 21:00:50 +0000115/// \brief Emits a diagnostic suggesting parentheses surrounding a
116/// given range.
117///
118/// \param Loc The location where we'll emit the diagnostic.
119/// \param Loc The kind of diagnostic to emit.
120/// \param ParenRange Source range enclosing code that should be parenthesized.
121void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
122 SourceRange ParenRange) {
Douglas Gregor96977da2009-02-27 17:53:17 +0000123 SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
124 if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
Douglas Gregor87f95b02009-02-26 21:00:50 +0000125 // We can't display the parentheses, so just dig the
126 // warning/error and return.
127 Diag(Loc, DK);
128 return;
129 }
Mike Stump11289f42009-09-09 15:08:12 +0000130
131 Diag(Loc, DK)
Douglas Gregora771f462010-03-31 17:46:05 +0000132 << FixItHint::CreateInsertion(ParenRange.getBegin(), "(")
133 << FixItHint::CreateInsertion(EndLoc, ")");
Douglas Gregor87f95b02009-02-26 21:00:50 +0000134}
135
John McCall1ca73da2010-09-07 18:31:03 +0000136static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) {
137 switch (ExpectedTok) {
138 case tok::semi: return Tok.is(tok::colon); // : for ;
139 default: return false;
140 }
141}
142
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000143/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
144/// input. If so, it is consumed and false is returned.
145///
146/// If the input is malformed, this emits the specified diagnostic. Next, if
147/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
148/// returned.
149bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
Chris Lattner6d7e6342006-08-15 03:41:14 +0000150 const char *Msg, tok::TokenKind SkipToTok) {
Douglas Gregor6da3db42010-05-25 05:58:43 +0000151 if (Tok.is(ExpectedTok) || Tok.is(tok::code_completion)) {
Chris Lattner15a00da2006-08-15 04:10:31 +0000152 ConsumeAnyToken();
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000153 return false;
154 }
Mike Stump01e07652008-06-19 19:28:49 +0000155
John McCall1ca73da2010-09-07 18:31:03 +0000156 // Detect common single-character typos and resume.
157 if (IsCommonTypo(ExpectedTok, Tok)) {
158 SourceLocation Loc = Tok.getLocation();
159 Diag(Loc, DiagID)
160 << Msg
161 << FixItHint::CreateReplacement(SourceRange(Loc),
162 getTokenSimpleSpelling(ExpectedTok));
163 ConsumeAnyToken();
164
165 // Pretend there wasn't a problem.
166 return false;
167 }
168
Douglas Gregor87f95b02009-02-26 21:00:50 +0000169 const char *Spelling = 0;
Douglas Gregor96977da2009-02-27 17:53:17 +0000170 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
Mike Stump11289f42009-09-09 15:08:12 +0000171 if (EndLoc.isValid() &&
Douglas Gregor96977da2009-02-27 17:53:17 +0000172 (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) {
Douglas Gregor87f95b02009-02-26 21:00:50 +0000173 // Show what code to insert to fix this problem.
Mike Stump11289f42009-09-09 15:08:12 +0000174 Diag(EndLoc, DiagID)
Douglas Gregor87f95b02009-02-26 21:00:50 +0000175 << Msg
Douglas Gregora771f462010-03-31 17:46:05 +0000176 << FixItHint::CreateInsertion(EndLoc, Spelling);
Douglas Gregor87f95b02009-02-26 21:00:50 +0000177 } else
178 Diag(Tok, DiagID) << Msg;
179
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000180 if (SkipToTok != tok::unknown)
181 SkipUntil(SkipToTok);
182 return true;
183}
184
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000185bool Parser::ExpectAndConsumeSemi(unsigned DiagID) {
186 if (Tok.is(tok::semi) || Tok.is(tok::code_completion)) {
187 ConsumeAnyToken();
188 return false;
189 }
190
191 if ((Tok.is(tok::r_paren) || Tok.is(tok::r_square)) &&
192 NextToken().is(tok::semi)) {
193 Diag(Tok, diag::err_extraneous_token_before_semi)
194 << PP.getSpelling(Tok)
195 << FixItHint::CreateRemoval(Tok.getLocation());
196 ConsumeAnyToken(); // The ')' or ']'.
197 ConsumeToken(); // The ';'.
198 return false;
199 }
200
201 return ExpectAndConsume(tok::semi, DiagID);
202}
203
Chris Lattner70f32b72006-07-31 05:09:04 +0000204//===----------------------------------------------------------------------===//
Chris Lattnereec40f92006-08-06 21:55:29 +0000205// Error recovery.
206//===----------------------------------------------------------------------===//
207
208/// SkipUntil - Read tokens until we get to the specified token, then consume
Chris Lattner01e4b242007-07-24 17:03:04 +0000209/// it (unless DontConsume is true). Because we cannot guarantee that the
Chris Lattnereec40f92006-08-06 21:55:29 +0000210/// token will ever occur, this skips to the next token, or to some likely
211/// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
212/// character.
Mike Stump01e07652008-06-19 19:28:49 +0000213///
Chris Lattnereec40f92006-08-06 21:55:29 +0000214/// If SkipUntil finds the specified token, it returns true, otherwise it
Mike Stump01e07652008-06-19 19:28:49 +0000215/// returns false.
Chris Lattner83b94e02007-04-27 19:12:15 +0000216bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
Argyrios Kyrtzidis76dbe8c2011-01-03 19:44:02 +0000217 bool StopAtSemi, bool DontConsume,
218 bool StopAtCodeCompletion) {
Chris Lattner5bd57e02006-08-11 06:40:25 +0000219 // We always want this function to skip at least one token if the first token
220 // isn't T and if not at EOF.
221 bool isFirstTokenSkipped = true;
Chris Lattnereec40f92006-08-06 21:55:29 +0000222 while (1) {
Chris Lattner83b94e02007-04-27 19:12:15 +0000223 // If we found one of the tokens, stop and return true.
224 for (unsigned i = 0; i != NumToks; ++i) {
Chris Lattner0ab032a2007-10-09 17:23:58 +0000225 if (Tok.is(Toks[i])) {
Chris Lattner83b94e02007-04-27 19:12:15 +0000226 if (DontConsume) {
227 // Noop, don't consume the token.
228 } else {
229 ConsumeAnyToken();
230 }
231 return true;
Chris Lattnereec40f92006-08-06 21:55:29 +0000232 }
Chris Lattnereec40f92006-08-06 21:55:29 +0000233 }
Mike Stump01e07652008-06-19 19:28:49 +0000234
Chris Lattnereec40f92006-08-06 21:55:29 +0000235 switch (Tok.getKind()) {
236 case tok::eof:
237 // Ran out of tokens.
238 return false;
Douglas Gregor6da3db42010-05-25 05:58:43 +0000239
240 case tok::code_completion:
Argyrios Kyrtzidis76dbe8c2011-01-03 19:44:02 +0000241 if (!StopAtCodeCompletion)
242 ConsumeToken();
Douglas Gregor6da3db42010-05-25 05:58:43 +0000243 return false;
244
Chris Lattnereec40f92006-08-06 21:55:29 +0000245 case tok::l_paren:
246 // Recursively skip properly-nested parens.
247 ConsumeParen();
Argyrios Kyrtzidis76dbe8c2011-01-03 19:44:02 +0000248 SkipUntil(tok::r_paren, false, false, StopAtCodeCompletion);
Chris Lattnereec40f92006-08-06 21:55:29 +0000249 break;
250 case tok::l_square:
251 // Recursively skip properly-nested square brackets.
252 ConsumeBracket();
Argyrios Kyrtzidis76dbe8c2011-01-03 19:44:02 +0000253 SkipUntil(tok::r_square, false, false, StopAtCodeCompletion);
Chris Lattnereec40f92006-08-06 21:55:29 +0000254 break;
255 case tok::l_brace:
256 // Recursively skip properly-nested braces.
257 ConsumeBrace();
Argyrios Kyrtzidis76dbe8c2011-01-03 19:44:02 +0000258 SkipUntil(tok::r_brace, false, false, StopAtCodeCompletion);
Chris Lattnereec40f92006-08-06 21:55:29 +0000259 break;
Mike Stump01e07652008-06-19 19:28:49 +0000260
Chris Lattnereec40f92006-08-06 21:55:29 +0000261 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
262 // Since the user wasn't looking for this token (if they were, it would
263 // already be handled), this isn't balanced. If there is a LHS token at a
264 // higher level, we will assume that this matches the unbalanced token
265 // and return it. Otherwise, this is a spurious RHS token, which we skip.
266 case tok::r_paren:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000267 if (ParenCount && !isFirstTokenSkipped)
268 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000269 ConsumeParen();
270 break;
271 case tok::r_square:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000272 if (BracketCount && !isFirstTokenSkipped)
273 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000274 ConsumeBracket();
275 break;
276 case tok::r_brace:
Chris Lattner5bd57e02006-08-11 06:40:25 +0000277 if (BraceCount && !isFirstTokenSkipped)
278 return false; // Matches something.
Chris Lattnereec40f92006-08-06 21:55:29 +0000279 ConsumeBrace();
280 break;
Mike Stump01e07652008-06-19 19:28:49 +0000281
Chris Lattnereec40f92006-08-06 21:55:29 +0000282 case tok::string_literal:
Chris Lattnerd3e98952006-10-06 05:22:26 +0000283 case tok::wide_string_literal:
Douglas Gregorfb65e592011-07-27 05:40:30 +0000284 case tok::utf8_string_literal:
285 case tok::utf16_string_literal:
286 case tok::utf32_string_literal:
Chris Lattnereec40f92006-08-06 21:55:29 +0000287 ConsumeStringToken();
288 break;
Fariborz Jahanian82ff1e52011-02-23 00:11:21 +0000289
Chris Lattnereec40f92006-08-06 21:55:29 +0000290 case tok::semi:
291 if (StopAtSemi)
292 return false;
293 // FALL THROUGH.
294 default:
295 // Skip this token.
296 ConsumeToken();
297 break;
298 }
Chris Lattner5bd57e02006-08-11 06:40:25 +0000299 isFirstTokenSkipped = false;
Mike Stump01e07652008-06-19 19:28:49 +0000300 }
Chris Lattnereec40f92006-08-06 21:55:29 +0000301}
302
303//===----------------------------------------------------------------------===//
Chris Lattnere4e38592006-08-14 00:15:05 +0000304// Scope manipulation
305//===----------------------------------------------------------------------===//
306
307/// EnterScope - Start a new scope.
Chris Lattner33ad2ca2006-11-05 23:47:55 +0000308void Parser::EnterScope(unsigned ScopeFlags) {
Chris Lattner03928c72007-07-15 00:04:39 +0000309 if (NumCachedScopes) {
310 Scope *N = ScopeCache[--NumCachedScopes];
Douglas Gregor0be31a22010-07-02 17:43:08 +0000311 N->Init(getCurScope(), ScopeFlags);
312 Actions.CurScope = N;
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000313 } else {
Argyrios Kyrtzidis18653422010-11-19 00:19:12 +0000314 Actions.CurScope = new Scope(getCurScope(), ScopeFlags, Diags);
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000315 }
Chris Lattnere4e38592006-08-14 00:15:05 +0000316}
317
318/// ExitScope - Pop a scope off the scope stack.
319void Parser::ExitScope() {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000320 assert(getCurScope() && "Scope imbalance!");
Chris Lattnere4e38592006-08-14 00:15:05 +0000321
Chris Lattner87547e62007-10-09 20:37:18 +0000322 // Inform the actions module that this scope is going away if there are any
323 // decls in it.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000324 if (!getCurScope()->decl_empty())
325 Actions.ActOnPopScope(Tok.getLocation(), getCurScope());
Mike Stump01e07652008-06-19 19:28:49 +0000326
Douglas Gregor0be31a22010-07-02 17:43:08 +0000327 Scope *OldScope = getCurScope();
328 Actions.CurScope = OldScope->getParent();
Mike Stump01e07652008-06-19 19:28:49 +0000329
Chris Lattner03928c72007-07-15 00:04:39 +0000330 if (NumCachedScopes == ScopeCacheSize)
331 delete OldScope;
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000332 else
Chris Lattner03928c72007-07-15 00:04:39 +0000333 ScopeCache[NumCachedScopes++] = OldScope;
Chris Lattnere4e38592006-08-14 00:15:05 +0000334}
335
Richard Smith938f40b2011-06-11 17:19:42 +0000336/// Set the flags for the current scope to ScopeFlags. If ManageFlags is false,
337/// this object does nothing.
338Parser::ParseScopeFlags::ParseScopeFlags(Parser *Self, unsigned ScopeFlags,
339 bool ManageFlags)
340 : CurScope(ManageFlags ? Self->getCurScope() : 0) {
341 if (CurScope) {
342 OldFlags = CurScope->getFlags();
343 CurScope->setFlags(ScopeFlags);
344 }
345}
Chris Lattnere4e38592006-08-14 00:15:05 +0000346
Richard Smith938f40b2011-06-11 17:19:42 +0000347/// Restore the flags for the current scope to what they were before this
348/// object overrode them.
349Parser::ParseScopeFlags::~ParseScopeFlags() {
350 if (CurScope)
351 CurScope->setFlags(OldFlags);
352}
Chris Lattnere4e38592006-08-14 00:15:05 +0000353
354
355//===----------------------------------------------------------------------===//
Chris Lattner70f32b72006-07-31 05:09:04 +0000356// C99 6.9: External Definitions.
357//===----------------------------------------------------------------------===//
Chris Lattner0bb5f832006-07-31 01:59:18 +0000358
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000359Parser::~Parser() {
360 // If we still have scopes active, delete the scope tree.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000361 delete getCurScope();
362 Actions.CurScope = 0;
363
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000364 // Free the scope cache.
Chris Lattner03928c72007-07-15 00:04:39 +0000365 for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
366 delete ScopeCache[i];
Daniel Dunbar921b9682008-10-04 19:21:03 +0000367
Francois Pichet1c229c02011-04-22 22:18:13 +0000368 // Free LateParsedTemplatedFunction nodes.
369 for (LateParsedTemplateMapT::iterator it = LateParsedTemplateMap.begin();
370 it != LateParsedTemplateMap.end(); ++it)
371 delete it->second;
372
Daniel Dunbar921b9682008-10-04 19:21:03 +0000373 // Remove the pragma handlers we installed.
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000374 PP.RemovePragmaHandler(AlignHandler.get());
375 AlignHandler.reset();
Eli Friedman570024a2010-08-05 06:57:20 +0000376 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
377 GCCVisibilityHandler.reset();
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000378 PP.RemovePragmaHandler(OptionsHandler.get());
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000379 OptionsHandler.reset();
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000380 PP.RemovePragmaHandler(PackHandler.get());
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000381 PackHandler.reset();
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000382 PP.RemovePragmaHandler(MSStructHandler.get());
383 MSStructHandler.reset();
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000384 PP.RemovePragmaHandler(UnusedHandler.get());
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000385 UnusedHandler.reset();
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000386 PP.RemovePragmaHandler(WeakHandler.get());
Eli Friedmanf5867dd2009-06-05 00:49:58 +0000387 WeakHandler.reset();
David Chisnall0867d9c2012-02-18 16:12:34 +0000388 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
389 RedefineExtnameHandler.reset();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000390
David Blaikiebbafb8a2012-03-11 07:00:24 +0000391 if (getLangOpts().OpenCL) {
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000392 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
393 OpenCLExtensionHandler.reset();
394 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
395 }
396
Peter Collingbourne564c0fa2011-02-14 01:42:35 +0000397 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
398 FPContractHandler.reset();
Douglas Gregor3a7ad252010-08-24 19:08:16 +0000399 PP.clearCodeCompletionHandler();
Chris Lattnerb6a0e172006-11-06 00:22:42 +0000400}
401
Chris Lattner38ba3362006-08-17 07:04:37 +0000402/// Initialize - Warm up the parser.
403///
404void Parser::Initialize() {
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000405 // Create the translation unit scope. Install it as the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000406 assert(getCurScope() == 0 && "A scope is already active?");
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000407 EnterScope(Scope::DeclScope);
Douglas Gregorf11096c2010-08-25 18:07:12 +0000408 Actions.ActOnTranslationUnitScope(getCurScope());
409
410 // Prime the lexer look-ahead.
411 ConsumeToken();
Mike Stump01e07652008-06-19 19:28:49 +0000412
Chris Lattner0ab032a2007-10-09 17:23:58 +0000413 if (Tok.is(tok::eof) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000414 !getLangOpts().CPlusPlus) // Empty source file is an extension in C
Chris Lattnerbd638922006-11-10 05:19:25 +0000415 Diag(Tok, diag::ext_empty_source_file);
Mike Stump01e07652008-06-19 19:28:49 +0000416
Chris Lattner66782842007-08-29 22:54:08 +0000417 // Initialization for Objective-C context sensitive keywords recognition.
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000418 // Referenced in Parser::ParseObjCTypeQualifierList.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000419 if (getLangOpts().ObjC1) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000420 ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
421 ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
422 ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
423 ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
424 ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
425 ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
Chris Lattner66782842007-08-29 22:54:08 +0000426 }
Daniel Dunbar12c9ddc2008-08-14 22:04:54 +0000427
Douglas Gregorbab8a962011-09-08 01:46:34 +0000428 Ident_instancetype = 0;
Anders Carlsson428803b2011-01-20 03:47:08 +0000429 Ident_final = 0;
430 Ident_override = 0;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +0000431
Daniel Dunbar12c9ddc2008-08-14 22:04:54 +0000432 Ident_super = &PP.getIdentifierTable().get("super");
John Thompson22334602010-02-05 00:12:22 +0000433
David Blaikiebbafb8a2012-03-11 07:00:24 +0000434 if (getLangOpts().AltiVec) {
John Thompson22334602010-02-05 00:12:22 +0000435 Ident_vector = &PP.getIdentifierTable().get("vector");
436 Ident_pixel = &PP.getIdentifierTable().get("pixel");
437 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000438
439 Ident_introduced = 0;
440 Ident_deprecated = 0;
441 Ident_obsoleted = 0;
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000442 Ident_unavailable = 0;
John Wiegley1c0675e2011-04-28 01:08:34 +0000443
Douglas Gregor60060d62011-10-21 03:57:52 +0000444 Ident__except = 0;
445
John Wiegley1c0675e2011-04-28 01:08:34 +0000446 Ident__exception_code = Ident__exception_info = Ident__abnormal_termination = 0;
447 Ident___exception_code = Ident___exception_info = Ident___abnormal_termination = 0;
448 Ident_GetExceptionCode = Ident_GetExceptionInfo = Ident_AbnormalTermination = 0;
449
David Blaikiebbafb8a2012-03-11 07:00:24 +0000450 if(getLangOpts().Borland) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000451 Ident__exception_info = PP.getIdentifierInfo("_exception_info");
452 Ident___exception_info = PP.getIdentifierInfo("__exception_info");
453 Ident_GetExceptionInfo = PP.getIdentifierInfo("GetExceptionInformation");
454 Ident__exception_code = PP.getIdentifierInfo("_exception_code");
455 Ident___exception_code = PP.getIdentifierInfo("__exception_code");
456 Ident_GetExceptionCode = PP.getIdentifierInfo("GetExceptionCode");
457 Ident__abnormal_termination = PP.getIdentifierInfo("_abnormal_termination");
458 Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination");
459 Ident_AbnormalTermination = PP.getIdentifierInfo("AbnormalTermination");
460
461 PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block);
462 PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block);
463 PP.SetPoisonReason(Ident_GetExceptionCode,diag::err_seh___except_block);
464 PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter);
465 PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter);
466 PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter);
467 PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block);
468 PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block);
469 PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block);
470 }
Chris Lattner38ba3362006-08-17 07:04:37 +0000471}
472
473/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
474/// action tells us to. This returns true if the EOF was encountered.
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000475bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000476 DelayedCleanupPoint CleanupRAII(TopLevelDeclCleanupPool);
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000477
Axel Naumann2eb1d902012-03-16 10:40:17 +0000478 // Skip over the EOF token, flagging end of previous input for incremental
479 // processing
480 if (PP.isIncrementalProcessingEnabled() && Tok.is(tok::eof))
481 ConsumeToken();
482
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000483 while (Tok.is(tok::annot_pragma_unused))
484 HandlePragmaUnused();
485
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000486 Result = DeclGroupPtrTy();
Chris Lattnerf4404402008-08-23 03:19:52 +0000487 if (Tok.is(tok::eof)) {
Francois Pichet1c229c02011-04-22 22:18:13 +0000488 // Late template parsing can begin.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000489 if (getLangOpts().DelayedTemplateParsing)
Francois Pichet1c229c02011-04-22 22:18:13 +0000490 Actions.SetLateTemplateParser(LateTemplateParserCallback, this);
Axel Naumann2eb1d902012-03-16 10:40:17 +0000491 if (!PP.isIncrementalProcessingEnabled())
492 Actions.ActOnEndOfTranslationUnit();
493 //else don't tell Sema that we ended parsing: more input might come.
Francois Pichet1c229c02011-04-22 22:18:13 +0000494
Chris Lattnerf4404402008-08-23 03:19:52 +0000495 return true;
496 }
Mike Stump01e07652008-06-19 19:28:49 +0000497
John McCall084e83d2011-03-24 11:26:52 +0000498 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000499 MaybeParseCXX0XAttributes(attrs);
500 MaybeParseMicrosoftAttributes(attrs);
Axel Naumann2eb1d902012-03-16 10:40:17 +0000501
John McCall53fa7142010-12-24 02:08:15 +0000502 Result = ParseExternalDeclaration(attrs);
Chris Lattner38ba3362006-08-17 07:04:37 +0000503 return false;
504}
505
Chris Lattner38ba3362006-08-17 07:04:37 +0000506/// ParseTranslationUnit:
507/// translation-unit: [C99 6.9]
Mike Stump01e07652008-06-19 19:28:49 +0000508/// external-declaration
509/// translation-unit external-declaration
Chris Lattner38ba3362006-08-17 07:04:37 +0000510void Parser::ParseTranslationUnit() {
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000511 Initialize();
Mike Stump01e07652008-06-19 19:28:49 +0000512
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000513 DeclGroupPtrTy Res;
Steve Naroff205ec3d2007-11-29 23:05:20 +0000514 while (!ParseTopLevelDecl(Res))
Chris Lattner38ba3362006-08-17 07:04:37 +0000515 /*parse them all*/;
Mike Stump11289f42009-09-09 15:08:12 +0000516
Chris Lattner2cc35ae2008-08-23 02:00:52 +0000517 ExitScope();
Douglas Gregor0be31a22010-07-02 17:43:08 +0000518 assert(getCurScope() == 0 && "Scope imbalance!");
Chris Lattner38ba3362006-08-17 07:04:37 +0000519}
520
Chris Lattner0bb5f832006-07-31 01:59:18 +0000521/// ParseExternalDeclaration:
Chris Lattner46415262008-12-08 21:59:01 +0000522///
Douglas Gregor15799fd2008-11-21 16:10:08 +0000523/// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
Chris Lattnercccc3112007-08-10 20:57:02 +0000524/// function-definition
525/// declaration
Douglas Gregor8b9575f2009-08-24 12:17:54 +0000526/// [C++0x] empty-declaration
Chris Lattner6d7e6342006-08-15 03:41:14 +0000527/// [GNU] asm-definition
Chris Lattnercccc3112007-08-10 20:57:02 +0000528/// [GNU] __extension__ external-declaration
Chris Lattner40f16b52006-11-05 02:05:37 +0000529/// [OBJC] objc-class-definition
530/// [OBJC] objc-class-declaration
531/// [OBJC] objc-alias-declaration
532/// [OBJC] objc-protocol-definition
533/// [OBJC] objc-method-definition
534/// [OBJC] @end
Douglas Gregor15799fd2008-11-21 16:10:08 +0000535/// [C++] linkage-specification
Chris Lattner6d7e6342006-08-15 03:41:14 +0000536/// [GNU] asm-definition:
537/// simple-asm-expr ';'
538///
Douglas Gregor8b9575f2009-08-24 12:17:54 +0000539/// [C++0x] empty-declaration:
540/// ';'
541///
Douglas Gregor43e75172009-09-04 06:33:52 +0000542/// [C++0x/GNU] 'extern' 'template' declaration
John McCall53fa7142010-12-24 02:08:15 +0000543Parser::DeclGroupPtrTy
544Parser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs,
545 ParsingDeclSpec *DS) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000546 DelayedCleanupPoint CleanupRAII(TopLevelDeclCleanupPool);
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000547 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000548
549 if (PP.isCodeCompletionReached()) {
550 cutOffParsing();
551 return DeclGroupPtrTy();
552 }
553
John McCall48871652010-08-21 09:40:31 +0000554 Decl *SingleDecl = 0;
Chris Lattner0bb5f832006-07-31 01:59:18 +0000555 switch (Tok.getKind()) {
Rafael Espindola273fd772012-01-26 02:02:57 +0000556 case tok::annot_pragma_vis:
557 HandlePragmaVisibility();
558 return DeclGroupPtrTy();
Eli Friedmanec52f922012-02-23 23:47:16 +0000559 case tok::annot_pragma_pack:
560 HandlePragmaPack();
561 return DeclGroupPtrTy();
Chris Lattner0bb5f832006-07-31 01:59:18 +0000562 case tok::semi:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000563 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000564 diag::warn_cxx98_compat_top_level_semi : diag::ext_top_level_semi)
565 << FixItHint::CreateRemoval(Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000566
Chris Lattner0bb5f832006-07-31 01:59:18 +0000567 ConsumeToken();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000568 // TODO: Invoke action for top-level semicolon.
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000569 return DeclGroupPtrTy();
Chris Lattner46415262008-12-08 21:59:01 +0000570 case tok::r_brace:
Nico Webere1df10a2012-01-17 01:04:27 +0000571 Diag(Tok, diag::err_extraneous_closing_brace);
Chris Lattner46415262008-12-08 21:59:01 +0000572 ConsumeBrace();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000573 return DeclGroupPtrTy();
Chris Lattner46415262008-12-08 21:59:01 +0000574 case tok::eof:
575 Diag(Tok, diag::err_expected_external_declaration);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000576 return DeclGroupPtrTy();
Chris Lattnercccc3112007-08-10 20:57:02 +0000577 case tok::kw___extension__: {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000578 // __extension__ silences extension warnings in the subexpression.
579 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner1ff6e732008-10-20 06:51:33 +0000580 ConsumeToken();
John McCall53fa7142010-12-24 02:08:15 +0000581 return ParseExternalDeclaration(attrs);
Chris Lattnercccc3112007-08-10 20:57:02 +0000582 }
Anders Carlsson5c6c0592008-02-08 00:33:21 +0000583 case tok::kw_asm: {
John McCall53fa7142010-12-24 02:08:15 +0000584 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000585
Abramo Bagnara348823a2011-03-03 14:20:18 +0000586 SourceLocation StartLoc = Tok.getLocation();
587 SourceLocation EndLoc;
588 ExprResult Result(ParseSimpleAsm(&EndLoc));
Mike Stump01e07652008-06-19 19:28:49 +0000589
Anders Carlsson0fae4f52008-02-08 00:23:11 +0000590 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
591 "top-level asm block");
Anders Carlsson5c6c0592008-02-08 00:33:21 +0000592
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000593 if (Result.isInvalid())
594 return DeclGroupPtrTy();
Abramo Bagnara348823a2011-03-03 14:20:18 +0000595 SingleDecl = Actions.ActOnFileScopeAsmDecl(Result.get(), StartLoc, EndLoc);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000596 break;
Anders Carlsson5c6c0592008-02-08 00:33:21 +0000597 }
Steve Naroffb419d3a2006-10-27 23:18:49 +0000598 case tok::at:
Fariborz Jahanian3a039e32011-08-27 20:50:59 +0000599 return ParseObjCAtDirectives();
Steve Naroffb419d3a2006-10-27 23:18:49 +0000600 case tok::minus:
Steve Naroffb419d3a2006-10-27 23:18:49 +0000601 case tok::plus:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000602 if (!getLangOpts().ObjC1) {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000603 Diag(Tok, diag::err_expected_external_declaration);
604 ConsumeToken();
605 return DeclGroupPtrTy();
606 }
607 SingleDecl = ParseObjCMethodDefinition();
608 break;
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000609 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000610 Actions.CodeCompleteOrdinaryName(getCurScope(),
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +0000611 CurParsedObjCImpl? Sema::PCC_ObjCImplementation
John McCallfaf5fb42010-08-26 23:41:50 +0000612 : Sema::PCC_Namespace);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000613 cutOffParsing();
614 return DeclGroupPtrTy();
Douglas Gregord7c4d982008-12-30 03:27:21 +0000615 case tok::kw_using:
Chris Lattnera5235172007-08-25 06:57:03 +0000616 case tok::kw_namespace:
Chris Lattner302b4be2006-11-19 02:31:38 +0000617 case tok::kw_typedef:
Douglas Gregoreb31f392008-12-01 23:54:00 +0000618 case tok::kw_template:
619 case tok::kw_export: // As in 'export template'
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000620 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000621 case tok::kw__Static_assert:
Chris Lattner479ed3a2007-08-25 18:15:16 +0000622 // A function definition cannot start with a these keywords.
Chris Lattner49836b42009-04-02 04:16:50 +0000623 {
624 SourceLocation DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000625 StmtVector Stmts(Actions);
John McCall53fa7142010-12-24 02:08:15 +0000626 return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000627 }
Sebastian Redl67667942010-08-27 23:12:46 +0000628
Douglas Gregoraa49ecc2010-12-01 20:32:20 +0000629 case tok::kw_static:
630 // Parse (then ignore) 'static' prior to a template instantiation. This is
631 // a GCC extension that we intentionally do not support.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000632 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
Douglas Gregoraa49ecc2010-12-01 20:32:20 +0000633 Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
634 << 0;
Sebastian Redl67667942010-08-27 23:12:46 +0000635 SourceLocation DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000636 StmtVector Stmts(Actions);
John McCall53fa7142010-12-24 02:08:15 +0000637 return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
Douglas Gregoraa49ecc2010-12-01 20:32:20 +0000638 }
639 goto dont_know;
640
641 case tok::kw_inline:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000642 if (getLangOpts().CPlusPlus) {
Douglas Gregoraa49ecc2010-12-01 20:32:20 +0000643 tok::TokenKind NextKind = NextToken().getKind();
644
645 // Inline namespaces. Allowed as an extension even in C++03.
646 if (NextKind == tok::kw_namespace) {
647 SourceLocation DeclEnd;
648 StmtVector Stmts(Actions);
John McCall53fa7142010-12-24 02:08:15 +0000649 return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
Douglas Gregoraa49ecc2010-12-01 20:32:20 +0000650 }
651
652 // Parse (then ignore) 'inline' prior to a template instantiation. This is
653 // a GCC extension that we intentionally do not support.
654 if (NextKind == tok::kw_template) {
655 Diag(ConsumeToken(), diag::warn_static_inline_explicit_inst_ignored)
656 << 1;
657 SourceLocation DeclEnd;
658 StmtVector Stmts(Actions);
John McCall53fa7142010-12-24 02:08:15 +0000659 return ParseDeclaration(Stmts, Declarator::FileContext, DeclEnd, attrs);
Douglas Gregoraa49ecc2010-12-01 20:32:20 +0000660 }
Sebastian Redl67667942010-08-27 23:12:46 +0000661 }
662 goto dont_know;
663
Douglas Gregor43e75172009-09-04 06:33:52 +0000664 case tok::kw_extern:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000665 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_template)) {
Douglas Gregor43e75172009-09-04 06:33:52 +0000666 // Extern templates
667 SourceLocation ExternLoc = ConsumeToken();
668 SourceLocation TemplateLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000669 Diag(ExternLoc, getLangOpts().CPlusPlus0x ?
Richard Smithf4111962011-10-20 18:35:58 +0000670 diag::warn_cxx98_compat_extern_template :
671 diag::ext_extern_template) << SourceRange(ExternLoc, TemplateLoc);
Douglas Gregor43e75172009-09-04 06:33:52 +0000672 SourceLocation DeclEnd;
673 return Actions.ConvertDeclToDeclGroup(
Argyrios Kyrtzidis26440632011-12-23 02:16:45 +0000674 ParseExplicitInstantiation(Declarator::FileContext,
675 ExternLoc, TemplateLoc, DeclEnd));
Douglas Gregor43e75172009-09-04 06:33:52 +0000676 }
Douglas Gregor43e75172009-09-04 06:33:52 +0000677 // FIXME: Detect C++ linkage specifications here?
Sebastian Redl67667942010-08-27 23:12:46 +0000678 goto dont_know;
Mike Stump11289f42009-09-09 15:08:12 +0000679
Francois Picheta5b3fcb2011-05-07 17:30:27 +0000680 case tok::kw___if_exists:
681 case tok::kw___if_not_exists:
Francois Pichet8f981d52011-05-25 10:19:49 +0000682 ParseMicrosoftIfExistsExternalDeclaration();
Francois Picheta5b3fcb2011-05-07 17:30:27 +0000683 return DeclGroupPtrTy();
Douglas Gregor08142532011-08-26 23:56:07 +0000684
Chris Lattner0bb5f832006-07-31 01:59:18 +0000685 default:
Sebastian Redl67667942010-08-27 23:12:46 +0000686 dont_know:
Chris Lattner0bb5f832006-07-31 01:59:18 +0000687 // We can't tell whether this is a function-definition or declaration yet.
John McCall53fa7142010-12-24 02:08:15 +0000688 if (DS) {
689 DS->takeAttributesFrom(attrs);
690 return ParseDeclarationOrFunctionDefinition(*DS);
691 } else {
692 return ParseDeclarationOrFunctionDefinition(attrs);
693 }
Chris Lattner0bb5f832006-07-31 01:59:18 +0000694 }
Mike Stump11289f42009-09-09 15:08:12 +0000695
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000696 // This routine returns a DeclGroup, if the thing we parsed only contains a
697 // single decl, convert it now.
698 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner0bb5f832006-07-31 01:59:18 +0000699}
700
Douglas Gregor23996282009-05-12 21:31:51 +0000701/// \brief Determine whether the current token, if it occurs after a
702/// declarator, continues a declaration or declaration list.
Alexis Hunt5a7fa252011-05-12 06:15:49 +0000703bool Parser::isDeclarationAfterDeclarator() {
704 // Check for '= delete' or '= default'
David Blaikiebbafb8a2012-03-11 07:00:24 +0000705 if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +0000706 const Token &KW = NextToken();
707 if (KW.is(tok::kw_default) || KW.is(tok::kw_delete))
708 return false;
709 }
710
Douglas Gregor23996282009-05-12 21:31:51 +0000711 return Tok.is(tok::equal) || // int X()= -> not a function def
712 Tok.is(tok::comma) || // int X(), -> not a function def
713 Tok.is(tok::semi) || // int X(); -> not a function def
714 Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def
715 Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def
David Blaikiebbafb8a2012-03-11 07:00:24 +0000716 (getLangOpts().CPlusPlus &&
Douglas Gregor23996282009-05-12 21:31:51 +0000717 Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++]
718}
719
720/// \brief Determine whether the current token, if it occurs after a
721/// declarator, indicates the start of a function definition.
Chris Lattner13901342010-07-11 22:42:07 +0000722bool Parser::isStartOfFunctionDefinition(const ParsingDeclarator &Declarator) {
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000723 assert(Declarator.isFunctionDeclarator() && "Isn't a function declarator");
Chris Lattner8c56c492009-12-06 18:34:27 +0000724 if (Tok.is(tok::l_brace)) // int X() {}
725 return true;
726
Chris Lattner13901342010-07-11 22:42:07 +0000727 // Handle K&R C argument lists: int X(f) int f; {}
David Blaikiebbafb8a2012-03-11 07:00:24 +0000728 if (!getLangOpts().CPlusPlus &&
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000729 Declarator.getFunctionTypeInfo().isKNRPrototype())
Chris Lattner13901342010-07-11 22:42:07 +0000730 return isDeclarationSpecifier();
Alexis Hunt5a7fa252011-05-12 06:15:49 +0000731
David Blaikiebbafb8a2012-03-11 07:00:24 +0000732 if (getLangOpts().CPlusPlus && Tok.is(tok::equal)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +0000733 const Token &KW = NextToken();
734 return KW.is(tok::kw_default) || KW.is(tok::kw_delete);
735 }
Chris Lattner13901342010-07-11 22:42:07 +0000736
Chris Lattner8c56c492009-12-06 18:34:27 +0000737 return Tok.is(tok::colon) || // X() : Base() {} (used for ctors)
738 Tok.is(tok::kw_try); // X() try { ... }
Douglas Gregor23996282009-05-12 21:31:51 +0000739}
740
Chris Lattner0bb5f832006-07-31 01:59:18 +0000741/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
Chris Lattner70f32b72006-07-31 05:09:04 +0000742/// a declaration. We can't tell which we have until we read up to the
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000743/// compound-statement in function-definition. TemplateParams, if
744/// non-NULL, provides the template parameters when we're parsing a
Mike Stump11289f42009-09-09 15:08:12 +0000745/// C++ template-declaration.
Chris Lattner0bb5f832006-07-31 01:59:18 +0000746///
Chris Lattner70f32b72006-07-31 05:09:04 +0000747/// function-definition: [C99 6.9.1]
Chris Lattner94fc8062008-04-05 05:52:15 +0000748/// decl-specs declarator declaration-list[opt] compound-statement
749/// [C90] function-definition: [C99 6.7.1] - implicit int result
Mike Stump01e07652008-06-19 19:28:49 +0000750/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
Chris Lattner94fc8062008-04-05 05:52:15 +0000751///
Chris Lattner70f32b72006-07-31 05:09:04 +0000752/// declaration: [C99 6.7]
Chris Lattnerf2659392007-08-22 06:06:56 +0000753/// declaration-specifiers init-declarator-list[opt] ';'
754/// [!C99] init-declarator-list ';' [TODO: warn in c99 mode]
Chris Lattner70f32b72006-07-31 05:09:04 +0000755/// [OMP] threadprivate-directive [TODO]
756///
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000757Parser::DeclGroupPtrTy
Fariborz Jahanian26de2e52009-12-09 21:39:38 +0000758Parser::ParseDeclarationOrFunctionDefinition(ParsingDeclSpec &DS,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000759 AccessSpecifier AS) {
Chris Lattner70f32b72006-07-31 05:09:04 +0000760 // Parse the common declaration-specifiers piece.
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000761 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
Mike Stump01e07652008-06-19 19:28:49 +0000762
Chris Lattnerd2864882006-08-05 08:09:44 +0000763 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
Chris Lattner53361ac2006-08-10 05:19:57 +0000764 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner0ab032a2007-10-09 17:23:58 +0000765 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000766 ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000767 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
John McCall28a6aea2009-11-04 02:18:39 +0000768 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000769 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000770 }
Mike Stump01e07652008-06-19 19:28:49 +0000771
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000772 // ObjC2 allows prefix attributes on class interfaces and protocols.
773 // FIXME: This still needs better diagnostics. We should only accept
774 // attributes here, no types, etc.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000775 if (getLangOpts().ObjC2 && Tok.is(tok::at)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000776 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +0000777 if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000778 !Tok.isObjCAtKeyword(tok::objc_protocol)) {
779 Diag(Tok, diag::err_objc_unexpected_attr);
Chris Lattner5e530bc2007-12-27 19:57:00 +0000780 SkipUntil(tok::semi); // FIXME: better skip?
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000781 return DeclGroupPtrTy();
Chris Lattner5e530bc2007-12-27 19:57:00 +0000782 }
John McCalld5a36322009-11-03 19:26:08 +0000783
John McCall28a6aea2009-11-04 02:18:39 +0000784 DS.abort();
785
Fariborz Jahanian056e3a42008-01-02 19:17:38 +0000786 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000787 unsigned DiagID;
788 if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
789 Diag(AtLoc, DiagID) << PrevSpec;
Mike Stump11289f42009-09-09 15:08:12 +0000790
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000791 if (Tok.isObjCAtKeyword(tok::objc_protocol))
Douglas Gregorf6102672012-01-01 21:23:57 +0000792 return ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
793
794 return Actions.ConvertDeclToDeclGroup(
795 ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes()));
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000796 }
Mike Stump01e07652008-06-19 19:28:49 +0000797
Chris Lattner38376f12008-01-12 07:05:38 +0000798 // If the declspec consisted only of 'extern' and we have a string
799 // literal following it, this must be a C++ linkage specifier like
800 // 'extern "C"'.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000801 if (Tok.is(tok::string_literal) && getLangOpts().CPlusPlus &&
Chris Lattner38376f12008-01-12 07:05:38 +0000802 DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000803 DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
John McCall48871652010-08-21 09:40:31 +0000804 Decl *TheDecl = ParseLinkage(DS, Declarator::FileContext);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000805 return Actions.ConvertDeclToDeclGroup(TheDecl);
806 }
Chris Lattner38376f12008-01-12 07:05:38 +0000807
John McCalld5a36322009-11-03 19:26:08 +0000808 return ParseDeclGroup(DS, Declarator::FileContext, true);
Chris Lattner70f32b72006-07-31 05:09:04 +0000809}
810
Fariborz Jahanian26de2e52009-12-09 21:39:38 +0000811Parser::DeclGroupPtrTy
John McCall53fa7142010-12-24 02:08:15 +0000812Parser::ParseDeclarationOrFunctionDefinition(ParsedAttributes &attrs,
Fariborz Jahanian26de2e52009-12-09 21:39:38 +0000813 AccessSpecifier AS) {
814 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000815 DS.takeAttributesFrom(attrs);
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000816 // Must temporarily exit the objective-c container scope for
817 // parsing c constructs and re-enter objc container scope
818 // afterwards.
819 ObjCDeclContextSwitch ObjCDC(*this);
820
821 return ParseDeclarationOrFunctionDefinition(DS, AS);
Fariborz Jahanian26de2e52009-12-09 21:39:38 +0000822}
823
Chris Lattnerfff824f2006-08-07 06:31:38 +0000824/// ParseFunctionDefinition - We parsed and verified that the specified
825/// Declarator is well formed. If this is a K&R-style function, read the
826/// parameters declaration-list, then start the compound-statement.
827///
Chris Lattner94fc8062008-04-05 05:52:15 +0000828/// function-definition: [C99 6.9.1]
829/// decl-specs declarator declaration-list[opt] compound-statement
830/// [C90] function-definition: [C99 6.7.1] - implicit int result
Mike Stump01e07652008-06-19 19:28:49 +0000831/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
Douglas Gregore8381c02008-11-05 04:29:56 +0000832/// [C++] function-definition: [C++ 8.4]
Chris Lattnerefb0f112009-03-29 17:18:04 +0000833/// decl-specifier-seq[opt] declarator ctor-initializer[opt]
834/// function-body
Douglas Gregore8381c02008-11-05 04:29:56 +0000835/// [C++] function-definition: [C++ 8.4]
Sebastian Redla7b98a72009-04-26 20:35:05 +0000836/// decl-specifier-seq[opt] declarator function-try-block
Chris Lattnerfff824f2006-08-07 06:31:38 +0000837///
John McCall48871652010-08-21 09:40:31 +0000838Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000839 const ParsedTemplateInfo &TemplateInfo,
840 LateParsedAttrList *LateParsedAttrs) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000841 // Poison the SEH identifiers so they are flagged as illegal in function bodies
842 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000843 const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump01e07652008-06-19 19:28:49 +0000844
Chris Lattner94fc8062008-04-05 05:52:15 +0000845 // If this is C90 and the declspecs were completely missing, fudge in an
846 // implicit int. We do this here because this is the only place where
847 // declaration-specifiers are completely optional in the grammar.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000848 if (getLangOpts().ImplicitInt && D.getDeclSpec().isEmpty()) {
Chris Lattner94fc8062008-04-05 05:52:15 +0000849 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000850 unsigned DiagID;
Chris Lattnerfcc390a2008-10-20 02:01:34 +0000851 D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
852 D.getIdentifierLoc(),
John McCall49bfce42009-08-03 20:12:06 +0000853 PrevSpec, DiagID);
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000854 D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
Chris Lattner94fc8062008-04-05 05:52:15 +0000855 }
Mike Stump01e07652008-06-19 19:28:49 +0000856
Chris Lattnerfff824f2006-08-07 06:31:38 +0000857 // If this declaration was formed with a K&R-style identifier list for the
858 // arguments, parse declarations for all of the args next.
859 // int foo(a,b) int a; float b; {}
Chris Lattner13901342010-07-11 22:42:07 +0000860 if (FTI.isKNRPrototype())
Chris Lattner5c5fbcc2006-12-03 08:41:30 +0000861 ParseKNRParamDeclarations(D);
Chris Lattnerfff824f2006-08-07 06:31:38 +0000862
Douglas Gregore8381c02008-11-05 04:29:56 +0000863 // We should have either an opening brace or, in a C++ constructor,
864 // we may have a colon.
Douglas Gregor0fcaac92011-02-04 11:57:16 +0000865 if (Tok.isNot(tok::l_brace) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000866 (!getLangOpts().CPlusPlus ||
Alexis Hunt61ae8d32011-05-23 23:14:04 +0000867 (Tok.isNot(tok::colon) && Tok.isNot(tok::kw_try) &&
868 Tok.isNot(tok::equal)))) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000869 Diag(Tok, diag::err_expected_fn_body);
870
871 // Skip over garbage, until we get to '{'. Don't eat the '{'.
872 SkipUntil(tok::l_brace, true, true);
Mike Stump01e07652008-06-19 19:28:49 +0000873
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000874 // If we didn't find the '{', bail out.
Chris Lattner0ab032a2007-10-09 17:23:58 +0000875 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +0000876 return 0;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000877 }
Mike Stump01e07652008-06-19 19:28:49 +0000878
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000879 // Check to make sure that any normal attributes are allowed to be on
880 // a definition. Late parsed attributes are checked at the end.
881 if (Tok.isNot(tok::equal)) {
882 AttributeList *DtorAttrs = D.getAttributes();
883 while (DtorAttrs) {
884 if (!IsThreadSafetyAttribute(DtorAttrs->getName()->getName())) {
885 Diag(DtorAttrs->getLoc(), diag::warn_attribute_on_function_definition)
886 << DtorAttrs->getName()->getName();
887 }
888 DtorAttrs = DtorAttrs->getNext();
889 }
890 }
891
Francois Pichet1c229c02011-04-22 22:18:13 +0000892 // In delayed template parsing mode, for function template we consume the
893 // tokens and store them for late parsing at the end of the translation unit.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000894 if (getLangOpts().DelayedTemplateParsing &&
Francois Pichet1c229c02011-04-22 22:18:13 +0000895 TemplateInfo.Kind == ParsedTemplateInfo::Template) {
896 MultiTemplateParamsArg TemplateParameterLists(Actions,
897 TemplateInfo.TemplateParams->data(),
898 TemplateInfo.TemplateParams->size());
899
900 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
901 Scope *ParentScope = getCurScope()->getParent();
902
Douglas Gregor5d1b4e32011-11-07 20:56:01 +0000903 D.setFunctionDefinitionKind(FDK_Definition);
Francois Pichet1c229c02011-04-22 22:18:13 +0000904 Decl *DP = Actions.HandleDeclarator(ParentScope, D,
Kaelyn Uhrain4dc695d2011-10-11 00:28:45 +0000905 move(TemplateParameterLists));
Francois Pichet1c229c02011-04-22 22:18:13 +0000906 D.complete(DP);
907 D.getMutableDeclSpec().abort();
908
909 if (DP) {
Francois Pichet33786cb2011-12-08 09:11:52 +0000910 LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(DP);
Francois Pichet1c229c02011-04-22 22:18:13 +0000911
912 FunctionDecl *FnD = 0;
913 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(DP))
914 FnD = FunTmpl->getTemplatedDecl();
915 else
916 FnD = cast<FunctionDecl>(DP);
Francois Pichetdcb3ebe2011-04-22 23:20:44 +0000917 Actions.CheckForFunctionRedefinition(FnD);
Francois Pichet1c229c02011-04-22 22:18:13 +0000918
919 LateParsedTemplateMap[FnD] = LPT;
920 Actions.MarkAsLateParsedTemplate(FnD);
921 LexTemplateFunctionForLateParsing(LPT->Toks);
922 } else {
923 CachedTokens Toks;
924 LexTemplateFunctionForLateParsing(Toks);
925 }
926 return DP;
927 }
928
Chris Lattnera55a2cc2007-10-09 17:14:05 +0000929 // Enter a scope for the function body.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000930 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump01e07652008-06-19 19:28:49 +0000931
Chris Lattnera55a2cc2007-10-09 17:14:05 +0000932 // Tell the actions module that we have entered a function definition with the
933 // specified Declarator for the function.
John McCall48871652010-08-21 09:40:31 +0000934 Decl *Res = TemplateInfo.TemplateParams?
Douglas Gregor0be31a22010-07-02 17:43:08 +0000935 Actions.ActOnStartOfFunctionTemplateDef(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000936 MultiTemplateParamsArg(Actions,
Douglas Gregor17a7c122009-06-24 00:54:41 +0000937 TemplateInfo.TemplateParams->data(),
938 TemplateInfo.TemplateParams->size()),
939 D)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000940 : Actions.ActOnStartOfFunctionDef(getCurScope(), D);
Mike Stump01e07652008-06-19 19:28:49 +0000941
John McCall28a6aea2009-11-04 02:18:39 +0000942 // Break out of the ParsingDeclarator context before we parse the body.
943 D.complete(Res);
944
945 // Break out of the ParsingDeclSpec context, too. This const_cast is
946 // safe because we're always the sole owner.
947 D.getMutableDeclSpec().abort();
948
Alexis Hunt61ae8d32011-05-23 23:14:04 +0000949 if (Tok.is(tok::equal)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000950 assert(getLangOpts().CPlusPlus && "Only C++ function definitions have '='");
Alexis Hunt61ae8d32011-05-23 23:14:04 +0000951 ConsumeToken();
952
953 Actions.ActOnFinishFunctionBody(Res, 0, false);
954
955 bool Delete = false;
956 SourceLocation KWLoc;
957 if (Tok.is(tok::kw_delete)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000958 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000959 diag::warn_cxx98_compat_deleted_function :
Richard Smithe4345902011-12-29 21:57:33 +0000960 diag::ext_deleted_function);
Alexis Hunt61ae8d32011-05-23 23:14:04 +0000961
962 KWLoc = ConsumeToken();
963 Actions.SetDeclDeleted(Res, KWLoc);
964 Delete = true;
965 } else if (Tok.is(tok::kw_default)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000966 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000967 diag::warn_cxx98_compat_defaulted_function :
Richard Smithe4345902011-12-29 21:57:33 +0000968 diag::ext_defaulted_function);
Alexis Hunt61ae8d32011-05-23 23:14:04 +0000969
970 KWLoc = ConsumeToken();
971 Actions.SetDeclDefaulted(Res, KWLoc);
972 } else {
973 llvm_unreachable("function definition after = not 'delete' or 'default'");
974 }
975
976 if (Tok.is(tok::comma)) {
977 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
978 << Delete;
979 SkipUntil(tok::semi);
980 } else {
981 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
982 Delete ? "delete" : "default", tok::semi);
983 }
984
985 return Res;
986 }
987
Sebastian Redla7b98a72009-04-26 20:35:05 +0000988 if (Tok.is(tok::kw_try))
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000989 return ParseFunctionTryBlock(Res, BodyScope);
Sebastian Redla7b98a72009-04-26 20:35:05 +0000990
Douglas Gregore8381c02008-11-05 04:29:56 +0000991 // If we have a colon, then we're probably parsing a C++
992 // ctor-initializer.
John McCallbb7b6582010-04-10 07:37:23 +0000993 if (Tok.is(tok::colon)) {
Douglas Gregore8381c02008-11-05 04:29:56 +0000994 ParseConstructorInitializer(Res);
John McCallbb7b6582010-04-10 07:37:23 +0000995
996 // Recover from error.
997 if (!Tok.is(tok::l_brace)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000998 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +0000999 Actions.ActOnFinishFunctionBody(Res, 0);
John McCallbb7b6582010-04-10 07:37:23 +00001000 return Res;
1001 }
1002 } else
Fariborz Jahanianaee31ac2009-07-21 22:36:06 +00001003 Actions.ActOnDefaultCtorInitializers(Res);
Douglas Gregore8381c02008-11-05 04:29:56 +00001004
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001005 // Late attributes are parsed in the same scope as the function body.
1006 if (LateParsedAttrs)
1007 ParseLexedAttributeList(*LateParsedAttrs, Res, false, true);
1008
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001009 return ParseFunctionStatementBody(Res, BodyScope);
Chris Lattnerfff824f2006-08-07 06:31:38 +00001010}
1011
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001012/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
1013/// types for a function with a K&R-style identifier list for arguments.
1014void Parser::ParseKNRParamDeclarations(Declarator &D) {
1015 // We know that the top-level of this declarator is a function.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001016 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001017
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001018 // Enter function-declaration scope, limiting any declarators to the
1019 // function prototype scope, including parameter declarators.
Douglas Gregor658b9552009-01-09 22:42:13 +00001020 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001021
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001022 // Read all the argument declarations.
1023 while (isDeclarationSpecifier()) {
1024 SourceLocation DSStart = Tok.getLocation();
Mike Stump01e07652008-06-19 19:28:49 +00001025
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001026 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +00001027 DeclSpec DS(AttrFactory);
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001028 ParseDeclarationSpecifiers(DS);
Mike Stump01e07652008-06-19 19:28:49 +00001029
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001030 // C99 6.9.1p6: 'each declaration in the declaration list shall have at
1031 // least one declarator'.
1032 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with
1033 // the declarations though. It's trivial to ignore them, really hard to do
1034 // anything else with them.
Chris Lattner0ab032a2007-10-09 17:23:58 +00001035 if (Tok.is(tok::semi)) {
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001036 Diag(DSStart, diag::err_declaration_does_not_declare_param);
1037 ConsumeToken();
1038 continue;
1039 }
Mike Stump01e07652008-06-19 19:28:49 +00001040
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001041 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
1042 // than register.
1043 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
1044 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
1045 Diag(DS.getStorageClassSpecLoc(),
1046 diag::err_invalid_storage_class_in_func_decl);
1047 DS.ClearStorageClassSpecs();
1048 }
1049 if (DS.isThreadSpecified()) {
1050 Diag(DS.getThreadSpecLoc(),
1051 diag::err_invalid_storage_class_in_func_decl);
1052 DS.ClearStorageClassSpecs();
1053 }
Mike Stump01e07652008-06-19 19:28:49 +00001054
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001055 // Parse the first declarator attached to this declspec.
1056 Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
1057 ParseDeclarator(ParmDeclarator);
1058
1059 // Handle the full declarator list.
1060 while (1) {
1061 // If attributes are present, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001062 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump01e07652008-06-19 19:28:49 +00001063
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001064 // Ask the actions module to compute the type for this declarator.
John McCall48871652010-08-21 09:40:31 +00001065 Decl *Param =
Douglas Gregor0be31a22010-07-02 17:43:08 +00001066 Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
Steve Naroffacb1e742007-09-10 20:51:04 +00001067
Mike Stump01e07652008-06-19 19:28:49 +00001068 if (Param &&
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001069 // A missing identifier has already been diagnosed.
1070 ParmDeclarator.getIdentifier()) {
1071
1072 // Scan the argument list looking for the correct param to apply this
1073 // type.
1074 for (unsigned i = 0; ; ++i) {
1075 // C99 6.9.1p6: those declarators shall declare only identifiers from
1076 // the identifier list.
1077 if (i == FTI.NumArgs) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001078 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
Chris Lattner760d19ad2008-11-19 07:51:13 +00001079 << ParmDeclarator.getIdentifier();
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001080 break;
1081 }
Mike Stump01e07652008-06-19 19:28:49 +00001082
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001083 if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
1084 // Reject redefinitions of parameters.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001085 if (FTI.ArgInfo[i].Param) {
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001086 Diag(ParmDeclarator.getIdentifierLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00001087 diag::err_param_redefinition)
Chris Lattner760d19ad2008-11-19 07:51:13 +00001088 << ParmDeclarator.getIdentifier();
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001089 } else {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001090 FTI.ArgInfo[i].Param = Param;
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001091 }
1092 break;
1093 }
1094 }
1095 }
1096
1097 // If we don't have a comma, it is either the end of the list (a ';') or
1098 // an error, bail out.
Chris Lattner0ab032a2007-10-09 17:23:58 +00001099 if (Tok.isNot(tok::comma))
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001100 break;
Mike Stump01e07652008-06-19 19:28:49 +00001101
Richard Smith8d06f422012-01-12 23:53:29 +00001102 ParmDeclarator.clear();
1103
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001104 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00001105 ParmDeclarator.setCommaLoc(ConsumeToken());
Mike Stump01e07652008-06-19 19:28:49 +00001106
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001107 // Parse the next declarator.
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001108 ParseDeclarator(ParmDeclarator);
1109 }
Mike Stump01e07652008-06-19 19:28:49 +00001110
Chris Lattner0ab032a2007-10-09 17:23:58 +00001111 if (Tok.is(tok::semi)) {
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001112 ConsumeToken();
1113 } else {
1114 Diag(Tok, diag::err_parse_error);
1115 // Skip to end of block or statement
1116 SkipUntil(tok::semi, true);
Chris Lattner0ab032a2007-10-09 17:23:58 +00001117 if (Tok.is(tok::semi))
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001118 ConsumeToken();
1119 }
1120 }
Mike Stump01e07652008-06-19 19:28:49 +00001121
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001122 // The actions module must verify that all arguments were declared.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001123 Actions.ActOnFinishKNRParamDeclarations(getCurScope(), D, Tok.getLocation());
Chris Lattner5c5fbcc2006-12-03 08:41:30 +00001124}
1125
1126
Chris Lattner0116c472006-08-15 06:03:28 +00001127/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
1128/// allowed to be a wide string, and is not subject to character translation.
1129///
1130/// [GNU] asm-string-literal:
1131/// string-literal
1132///
John McCalldadc5752010-08-24 06:29:42 +00001133Parser::ExprResult Parser::ParseAsmStringLiteral() {
Ted Kremenek65cdbf52011-12-02 00:35:46 +00001134 switch (Tok.getKind()) {
1135 case tok::string_literal:
1136 break;
Richard Smithd67aea22012-03-06 03:21:47 +00001137 case tok::utf8_string_literal:
1138 case tok::utf16_string_literal:
1139 case tok::utf32_string_literal:
Ted Kremenek65cdbf52011-12-02 00:35:46 +00001140 case tok::wide_string_literal: {
1141 SourceLocation L = Tok.getLocation();
1142 Diag(Tok, diag::err_asm_operand_wide_string_literal)
Richard Smithd67aea22012-03-06 03:21:47 +00001143 << (Tok.getKind() == tok::wide_string_literal)
Ted Kremenek65cdbf52011-12-02 00:35:46 +00001144 << SourceRange(L, L);
1145 return ExprError();
1146 }
1147 default:
1148 Diag(Tok, diag::err_expected_string_literal);
1149 return ExprError();
Chris Lattner0116c472006-08-15 06:03:28 +00001150 }
Mike Stump01e07652008-06-19 19:28:49 +00001151
Richard Smithd67aea22012-03-06 03:21:47 +00001152 return ParseStringLiteralExpression();
Chris Lattner0116c472006-08-15 06:03:28 +00001153}
1154
Chris Lattner6d7e6342006-08-15 03:41:14 +00001155/// ParseSimpleAsm
1156///
1157/// [GNU] simple-asm-expr:
1158/// 'asm' '(' asm-string-literal ')'
Chris Lattner6d7e6342006-08-15 03:41:14 +00001159///
John McCalldadc5752010-08-24 06:29:42 +00001160Parser::ExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
Chris Lattner0ab032a2007-10-09 17:23:58 +00001161 assert(Tok.is(tok::kw_asm) && "Not an asm!");
Anders Carlsson5c6c0592008-02-08 00:33:21 +00001162 SourceLocation Loc = ConsumeToken();
Mike Stump01e07652008-06-19 19:28:49 +00001163
John McCall9dfb1622010-01-25 22:27:48 +00001164 if (Tok.is(tok::kw_volatile)) {
John McCall5cb52872010-01-25 23:12:50 +00001165 // Remove from the end of 'asm' to the end of 'volatile'.
1166 SourceRange RemovalRange(PP.getLocForEndOfToken(Loc),
1167 PP.getLocForEndOfToken(Tok.getLocation()));
1168
1169 Diag(Tok, diag::warn_file_asm_volatile)
Douglas Gregora771f462010-03-31 17:46:05 +00001170 << FixItHint::CreateRemoval(RemovalRange);
John McCall9dfb1622010-01-25 22:27:48 +00001171 ConsumeToken();
1172 }
1173
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001174 BalancedDelimiterTracker T(*this, tok::l_paren);
1175 if (T.consumeOpen()) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001176 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Sebastian Redl042ad952008-12-11 19:30:53 +00001177 return ExprError();
Chris Lattner6d7e6342006-08-15 03:41:14 +00001178 }
Mike Stump01e07652008-06-19 19:28:49 +00001179
John McCalldadc5752010-08-24 06:29:42 +00001180 ExprResult Result(ParseAsmStringLiteral());
Mike Stump01e07652008-06-19 19:28:49 +00001181
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001182 if (Result.isInvalid()) {
1183 SkipUntil(tok::r_paren, true, true);
1184 if (EndLoc)
1185 *EndLoc = Tok.getLocation();
1186 ConsumeAnyToken();
1187 } else {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001188 // Close the paren and get the location of the end bracket
1189 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001190 if (EndLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001191 *EndLoc = T.getCloseLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001192 }
Mike Stump01e07652008-06-19 19:28:49 +00001193
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001194 return move(Result);
Chris Lattner6d7e6342006-08-15 03:41:14 +00001195}
Steve Naroffb419d3a2006-10-27 23:18:49 +00001196
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001197/// \brief Get the TemplateIdAnnotation from the token and put it in the
1198/// cleanup pool so that it gets destroyed when parsing the current top level
1199/// declaration is finished.
1200TemplateIdAnnotation *Parser::takeTemplateIdAnnotation(const Token &tok) {
1201 assert(tok.is(tok::annot_template_id) && "Expected template-id token");
1202 TemplateIdAnnotation *
1203 Id = static_cast<TemplateIdAnnotation *>(tok.getAnnotationValue());
1204 TopLevelDeclCleanupPool.delayMemberFunc< TemplateIdAnnotation,
1205 &TemplateIdAnnotation::Destroy>(Id);
1206 return Id;
1207}
1208
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001209/// TryAnnotateTypeOrScopeToken - If the current token position is on a
1210/// typename (possibly qualified in C++) or a C++ scope specifier not followed
1211/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
1212/// with a single annotation token representing the typename or C++ scope
1213/// respectively.
1214/// This simplifies handling of C++ scope specifiers and allows efficient
1215/// backtracking without the need to re-parse and resolve nested-names and
1216/// typenames.
Argyrios Kyrtzidis0c4162a2008-11-26 21:51:07 +00001217/// It will mainly be called when we expect to treat identifiers as typenames
1218/// (if they are typenames). For example, in C we do not expect identifiers
1219/// inside expressions to be treated as typenames so it will not be called
1220/// for expressions in C.
1221/// The benefit for C/ObjC is that a typename will be annotated and
Steve Naroff16c8e592009-01-28 19:39:02 +00001222/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
Argyrios Kyrtzidis0c4162a2008-11-26 21:51:07 +00001223/// will not be called twice, once to check whether we have a declaration
1224/// specifier, and another one to get the actual type inside
1225/// ParseDeclarationSpecifiers).
Chris Lattner9a8968b2009-01-04 23:23:14 +00001226///
John McCall1f476a12010-02-26 08:45:28 +00001227/// This returns true if an error occurred.
Mike Stump11289f42009-09-09 15:08:12 +00001228///
Chris Lattner45ddec32009-01-05 00:13:00 +00001229/// Note that this routine emits an error if you call it with ::new or ::delete
1230/// as the current tokens, so only call it in contexts where these are invalid.
Kaelyn Uhrain85308c62011-10-11 01:02:41 +00001231bool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext, bool NeedType) {
Mike Stump11289f42009-09-09 15:08:12 +00001232 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
David Blaikie15a430a2011-12-04 05:04:18 +00001233 || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)
1234 || Tok.is(tok::kw_decltype)) && "Cannot be a type or scope token!");
Mike Stump11289f42009-09-09 15:08:12 +00001235
Douglas Gregor333489b2009-03-27 23:10:48 +00001236 if (Tok.is(tok::kw_typename)) {
1237 // Parse a C++ typename-specifier, e.g., "typename T::type".
1238 //
1239 // typename-specifier:
1240 // 'typename' '::' [opt] nested-name-specifier identifier
Mike Stump11289f42009-09-09 15:08:12 +00001241 // 'typename' '::' [opt] nested-name-specifier template [opt]
Douglas Gregordce2b622009-04-01 00:28:59 +00001242 // simple-template-id
Douglas Gregor333489b2009-03-27 23:10:48 +00001243 SourceLocation TypenameLoc = ConsumeToken();
1244 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00001245 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/ParsedType(),
1246 /*EnteringContext=*/false,
Francois Pichet4e7a2c02011-03-27 19:41:34 +00001247 0, /*IsTypename*/true))
John McCall1f476a12010-02-26 08:45:28 +00001248 return true;
1249 if (!SS.isSet()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001250 if (getLangOpts().MicrosoftExt)
Francois Pichetdd876122011-04-24 11:24:13 +00001251 Diag(Tok.getLocation(), diag::warn_expected_qualified_after_typename);
1252 else
1253 Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
John McCall1f476a12010-02-26 08:45:28 +00001254 return true;
Douglas Gregor333489b2009-03-27 23:10:48 +00001255 }
1256
1257 TypeResult Ty;
1258 if (Tok.is(tok::identifier)) {
1259 // FIXME: check whether the next token is '<', first!
Douglas Gregor0be31a22010-07-02 17:43:08 +00001260 Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
Douglas Gregorf7d77712010-06-16 22:31:08 +00001261 *Tok.getIdentifierInfo(),
Douglas Gregor333489b2009-03-27 23:10:48 +00001262 Tok.getLocation());
Douglas Gregordce2b622009-04-01 00:28:59 +00001263 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001264 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregordce2b622009-04-01 00:28:59 +00001265 if (TemplateId->Kind == TNK_Function_template) {
1266 Diag(Tok, diag::err_typename_refers_to_non_type_template)
1267 << Tok.getAnnotationRange();
John McCall1f476a12010-02-26 08:45:28 +00001268 return true;
Douglas Gregordce2b622009-04-01 00:28:59 +00001269 }
Douglas Gregor333489b2009-03-27 23:10:48 +00001270
Douglas Gregorb09518c2011-02-27 22:46:49 +00001271 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
1272 TemplateId->getTemplateArgs(),
1273 TemplateId->NumArgs);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +00001274
Douglas Gregorb09518c2011-02-27 22:46:49 +00001275 Ty = Actions.ActOnTypenameType(getCurScope(), TypenameLoc, SS,
Abramo Bagnarae0a70b22012-02-06 22:45:07 +00001276 TemplateId->TemplateKWLoc,
Douglas Gregorb09518c2011-02-27 22:46:49 +00001277 TemplateId->Template,
1278 TemplateId->TemplateNameLoc,
1279 TemplateId->LAngleLoc,
Abramo Bagnarae0a70b22012-02-06 22:45:07 +00001280 TemplateArgsPtr,
Douglas Gregorb09518c2011-02-27 22:46:49 +00001281 TemplateId->RAngleLoc);
Douglas Gregordce2b622009-04-01 00:28:59 +00001282 } else {
1283 Diag(Tok, diag::err_expected_type_name_after_typename)
1284 << SS.getRange();
John McCall1f476a12010-02-26 08:45:28 +00001285 return true;
Douglas Gregordce2b622009-04-01 00:28:59 +00001286 }
1287
Sebastian Redlb0e3e1b2010-02-08 19:35:18 +00001288 SourceLocation EndLoc = Tok.getLastLoc();
Douglas Gregordce2b622009-04-01 00:28:59 +00001289 Tok.setKind(tok::annot_typename);
John McCallba7bf592010-08-24 05:47:05 +00001290 setTypeAnnotation(Tok, Ty.isInvalid() ? ParsedType() : Ty.get());
Sebastian Redlb0e3e1b2010-02-08 19:35:18 +00001291 Tok.setAnnotationEndLoc(EndLoc);
Douglas Gregordce2b622009-04-01 00:28:59 +00001292 Tok.setLocation(TypenameLoc);
1293 PP.AnnotateCachedTokens(Tok);
John McCall1f476a12010-02-26 08:45:28 +00001294 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001295 }
1296
John McCalle2ade282009-12-19 00:35:18 +00001297 // Remembers whether the token was originally a scope annotation.
1298 bool wasScopeAnnotation = Tok.is(tok::annot_cxxscope);
1299
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001300 CXXScopeSpec SS;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001301 if (getLangOpts().CPlusPlus)
John McCallba7bf592010-08-24 05:47:05 +00001302 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall1f476a12010-02-26 08:45:28 +00001303 return true;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001304
1305 if (Tok.is(tok::identifier)) {
Kaelyn Uhrain85308c62011-10-11 01:02:41 +00001306 IdentifierInfo *CorrectedII = 0;
Chris Lattnerda030082009-01-05 01:49:50 +00001307 // Determine whether the identifier is a type name.
John McCallba7bf592010-08-24 05:47:05 +00001308 if (ParsedType Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
1309 Tok.getLocation(), getCurScope(),
Fariborz Jahanian87967422011-02-08 18:05:59 +00001310 &SS, false,
Douglas Gregor844cb502011-03-01 18:12:44 +00001311 NextToken().is(tok::period),
1312 ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00001313 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrain85308c62011-10-11 01:02:41 +00001314 /*NonTrivialTypeSourceInfo*/true,
1315 NeedType ? &CorrectedII : NULL)) {
1316 // A FixIt was applied as a result of typo correction
1317 if (CorrectedII)
1318 Tok.setIdentifierInfo(CorrectedII);
Chris Lattnerda030082009-01-05 01:49:50 +00001319 // This is a typename. Replace the current token in-place with an
1320 // annotation type token.
Chris Lattnera8a3f732009-01-06 05:06:21 +00001321 Tok.setKind(tok::annot_typename);
John McCallba7bf592010-08-24 05:47:05 +00001322 setTypeAnnotation(Tok, Ty);
Chris Lattnerda030082009-01-05 01:49:50 +00001323 Tok.setAnnotationEndLoc(Tok.getLocation());
1324 if (SS.isNotEmpty()) // it was a C++ qualified type name.
1325 Tok.setLocation(SS.getBeginLoc());
Mike Stump11289f42009-09-09 15:08:12 +00001326
Chris Lattnerda030082009-01-05 01:49:50 +00001327 // In case the tokens were cached, have Preprocessor replace
1328 // them with the annotation token.
1329 PP.AnnotateCachedTokens(Tok);
John McCall1f476a12010-02-26 08:45:28 +00001330 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001331 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001332
David Blaikiebbafb8a2012-03-11 07:00:24 +00001333 if (!getLangOpts().CPlusPlus) {
Chris Lattnerda030082009-01-05 01:49:50 +00001334 // If we're in C, we can't have :: tokens at all (the lexer won't return
1335 // them). If the identifier is not a type, then it can't be scope either,
Mike Stump11289f42009-09-09 15:08:12 +00001336 // just early exit.
Chris Lattnerda030082009-01-05 01:49:50 +00001337 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001338 }
Mike Stump11289f42009-09-09 15:08:12 +00001339
Douglas Gregor7f741122009-02-25 19:37:18 +00001340 // If this is a template-id, annotate with a template-id or type token.
Douglas Gregor8bf42052009-02-09 18:46:07 +00001341 if (NextToken().is(tok::less)) {
Douglas Gregordc572a32009-03-30 22:58:21 +00001342 TemplateTy Template;
Douglas Gregor3cf81312009-11-03 23:16:33 +00001343 UnqualifiedId TemplateName;
1344 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Douglas Gregor786123d2010-05-21 23:18:07 +00001345 bool MemberOfUnknownSpecialization;
Mike Stump11289f42009-09-09 15:08:12 +00001346 if (TemplateNameKind TNK
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001347 = Actions.isTemplateName(getCurScope(), SS,
1348 /*hasTemplateKeyword=*/false, TemplateName,
John McCallba7bf592010-08-24 05:47:05 +00001349 /*ObjectType=*/ ParsedType(),
1350 EnteringContext,
Abramo Bagnara7c5dee42010-08-06 12:11:11 +00001351 Template, MemberOfUnknownSpecialization)) {
Douglas Gregor71395fa2009-11-04 00:56:37 +00001352 // Consume the identifier.
1353 ConsumeToken();
Abramo Bagnara7945c982012-01-27 09:46:47 +00001354 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
1355 TemplateName)) {
Chris Lattner5558e9f2009-06-26 04:27:47 +00001356 // If an unrecoverable error occurred, we need to return true here,
1357 // because the token stream is in a damaged state. We may not return
1358 // a valid identifier.
John McCall1f476a12010-02-26 08:45:28 +00001359 return true;
Chris Lattner5558e9f2009-06-26 04:27:47 +00001360 }
Douglas Gregor71395fa2009-11-04 00:56:37 +00001361 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001362 }
Douglas Gregor55ad91f2008-12-18 19:37:40 +00001363
Douglas Gregor7f741122009-02-25 19:37:18 +00001364 // The current token, which is either an identifier or a
1365 // template-id, is not part of the annotation. Fall through to
1366 // push that token back into the stream and complete the C++ scope
1367 // specifier annotation.
Mike Stump11289f42009-09-09 15:08:12 +00001368 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001369
Douglas Gregor7f741122009-02-25 19:37:18 +00001370 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001371 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00001372 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001373 // A template-id that refers to a type was parsed into a
1374 // template-id annotation in a context where we weren't allowed
1375 // to produce a type annotation token. Update the template-id
1376 // annotation token to a type annotation token now.
Douglas Gregore7c20652011-03-02 00:47:37 +00001377 AnnotateTemplateIdTokenAsType();
John McCall1f476a12010-02-26 08:45:28 +00001378 return false;
Douglas Gregor7f741122009-02-25 19:37:18 +00001379 }
1380 }
Douglas Gregor55ad91f2008-12-18 19:37:40 +00001381
Chris Lattnerdfa1a452009-01-04 22:32:19 +00001382 if (SS.isEmpty())
John McCall1f476a12010-02-26 08:45:28 +00001383 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001384
Chris Lattnerdfa1a452009-01-04 22:32:19 +00001385 // A C++ scope specifier that isn't followed by a typename.
1386 // Push the current token back into the token stream (or revert it if it is
1387 // cached) and use an annotation scope token for current token.
1388 if (PP.isBacktrackEnabled())
1389 PP.RevertCachedTokens(1);
1390 else
1391 PP.EnterToken(Tok);
1392 Tok.setKind(tok::annot_cxxscope);
Douglas Gregor869ad452011-02-24 17:54:50 +00001393 Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
Chris Lattnerdfa1a452009-01-04 22:32:19 +00001394 Tok.setAnnotationRange(SS.getRange());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001395
John McCalle2ade282009-12-19 00:35:18 +00001396 // In case the tokens were cached, have Preprocessor replace them
1397 // with the annotation token. We don't need to do this if we've
1398 // just reverted back to the state we were in before being called.
1399 if (!wasScopeAnnotation)
1400 PP.AnnotateCachedTokens(Tok);
John McCall1f476a12010-02-26 08:45:28 +00001401 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001402}
1403
1404/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
Douglas Gregor7f741122009-02-25 19:37:18 +00001405/// annotates C++ scope specifiers and template-ids. This returns
Chris Lattner5558e9f2009-06-26 04:27:47 +00001406/// true if the token was annotated or there was an error that could not be
1407/// recovered from.
Mike Stump11289f42009-09-09 15:08:12 +00001408///
Chris Lattner45ddec32009-01-05 00:13:00 +00001409/// Note that this routine emits an error if you call it with ::new or ::delete
1410/// as the current tokens, so only call it in contexts where these are invalid.
Douglas Gregore861bac2009-08-25 22:51:20 +00001411bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001412 assert(getLangOpts().CPlusPlus &&
Chris Lattnerdfa1a452009-01-04 22:32:19 +00001413 "Call sites of this function should be guarded by checking for C++");
Douglas Gregor8b02cd02011-04-27 04:48:22 +00001414 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
David Blaikie15a430a2011-12-04 05:04:18 +00001415 (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) ||
1416 Tok.is(tok::kw_decltype)) && "Cannot be a type or scope token!");
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001417
Argyrios Kyrtzidisace521a2008-11-26 21:41:52 +00001418 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001419 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall1f476a12010-02-26 08:45:28 +00001420 return true;
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00001421 if (SS.isEmpty())
John McCall1f476a12010-02-26 08:45:28 +00001422 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001423
Chris Lattnerdfa1a452009-01-04 22:32:19 +00001424 // Push the current token back into the token stream (or revert it if it is
1425 // cached) and use an annotation scope token for current token.
1426 if (PP.isBacktrackEnabled())
1427 PP.RevertCachedTokens(1);
1428 else
1429 PP.EnterToken(Tok);
1430 Tok.setKind(tok::annot_cxxscope);
Douglas Gregor869ad452011-02-24 17:54:50 +00001431 Tok.setAnnotationValue(Actions.SaveNestedNameSpecifierAnnotation(SS));
Chris Lattnerdfa1a452009-01-04 22:32:19 +00001432 Tok.setAnnotationRange(SS.getRange());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001433
Chris Lattnerdfa1a452009-01-04 22:32:19 +00001434 // In case the tokens were cached, have Preprocessor replace them with the
1435 // annotation token.
1436 PP.AnnotateCachedTokens(Tok);
John McCall1f476a12010-02-26 08:45:28 +00001437 return false;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001438}
John McCall37958aa2009-11-03 19:33:12 +00001439
Richard Trieu4972a6d2012-01-19 22:01:51 +00001440bool Parser::isTokenEqualOrEqualTypo() {
1441 tok::TokenKind Kind = Tok.getKind();
1442 switch (Kind) {
1443 default:
Richard Trieuc64d3232012-01-18 22:54:52 +00001444 return false;
Richard Trieu4972a6d2012-01-19 22:01:51 +00001445 case tok::ampequal: // &=
1446 case tok::starequal: // *=
1447 case tok::plusequal: // +=
1448 case tok::minusequal: // -=
1449 case tok::exclaimequal: // !=
1450 case tok::slashequal: // /=
1451 case tok::percentequal: // %=
1452 case tok::lessequal: // <=
1453 case tok::lesslessequal: // <<=
1454 case tok::greaterequal: // >=
1455 case tok::greatergreaterequal: // >>=
1456 case tok::caretequal: // ^=
1457 case tok::pipeequal: // |=
1458 case tok::equalequal: // ==
1459 Diag(Tok, diag::err_invalid_token_after_declarator_suggest_equal)
1460 << getTokenSimpleSpelling(Kind)
1461 << FixItHint::CreateReplacement(SourceRange(Tok.getLocation()), "=");
1462 case tok::equal:
1463 return true;
1464 }
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001465}
1466
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001467SourceLocation Parser::handleUnexpectedCodeCompletionToken() {
1468 assert(Tok.is(tok::code_completion));
1469 PrevTokLocation = Tok.getLocation();
1470
Douglas Gregor0be31a22010-07-02 17:43:08 +00001471 for (Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor6da3db42010-05-25 05:58:43 +00001472 if (S->getFlags() & Scope::FnScope) {
John McCallfaf5fb42010-08-26 23:41:50 +00001473 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_RecoveryInFunction);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001474 cutOffParsing();
1475 return PrevTokLocation;
Douglas Gregor6da3db42010-05-25 05:58:43 +00001476 }
1477
1478 if (S->getFlags() & Scope::ClassScope) {
John McCallfaf5fb42010-08-26 23:41:50 +00001479 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Class);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001480 cutOffParsing();
1481 return PrevTokLocation;
Douglas Gregor6da3db42010-05-25 05:58:43 +00001482 }
1483 }
1484
John McCallfaf5fb42010-08-26 23:41:50 +00001485 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Namespace);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001486 cutOffParsing();
1487 return PrevTokLocation;
Douglas Gregor6da3db42010-05-25 05:58:43 +00001488}
1489
John McCall37958aa2009-11-03 19:33:12 +00001490// Anchor the Parser::FieldCallback vtable to this translation unit.
1491// We use a spurious method instead of the destructor because
1492// destroying FieldCallbacks can actually be slightly
1493// performance-sensitive.
1494void Parser::FieldCallback::_anchor() {
1495}
Douglas Gregor3a7ad252010-08-24 19:08:16 +00001496
1497// Code-completion pass-through functions
1498
1499void Parser::CodeCompleteDirective(bool InConditional) {
Douglas Gregorec00a262010-08-24 22:20:20 +00001500 Actions.CodeCompletePreprocessorDirective(InConditional);
Douglas Gregor3a7ad252010-08-24 19:08:16 +00001501}
1502
1503void Parser::CodeCompleteInConditionalExclusion() {
1504 Actions.CodeCompleteInPreprocessorConditionalExclusion(getCurScope());
1505}
Douglas Gregor12785102010-08-24 20:21:13 +00001506
1507void Parser::CodeCompleteMacroName(bool IsDefinition) {
Douglas Gregorec00a262010-08-24 22:20:20 +00001508 Actions.CodeCompletePreprocessorMacroName(IsDefinition);
1509}
1510
1511void Parser::CodeCompletePreprocessorExpression() {
1512 Actions.CodeCompletePreprocessorExpression();
1513}
1514
1515void Parser::CodeCompleteMacroArgument(IdentifierInfo *Macro,
1516 MacroInfo *MacroInfo,
1517 unsigned ArgumentIndex) {
1518 Actions.CodeCompletePreprocessorMacroArgument(getCurScope(), Macro, MacroInfo,
1519 ArgumentIndex);
Douglas Gregor12785102010-08-24 20:21:13 +00001520}
Douglas Gregor11583702010-08-25 17:04:25 +00001521
1522void Parser::CodeCompleteNaturalLanguage() {
1523 Actions.CodeCompleteNaturalLanguage();
1524}
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001525
Douglas Gregor43edb322011-10-24 22:31:10 +00001526bool Parser::ParseMicrosoftIfExistsCondition(IfExistsCondition& Result) {
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001527 assert((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists)) &&
1528 "Expected '__if_exists' or '__if_not_exists'");
Douglas Gregor43edb322011-10-24 22:31:10 +00001529 Result.IsIfExists = Tok.is(tok::kw___if_exists);
1530 Result.KeywordLoc = ConsumeToken();
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001531
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001532 BalancedDelimiterTracker T(*this, tok::l_paren);
1533 if (T.consumeOpen()) {
Douglas Gregor43edb322011-10-24 22:31:10 +00001534 Diag(Tok, diag::err_expected_lparen_after)
1535 << (Result.IsIfExists? "__if_exists" : "__if_not_exists");
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001536 return true;
1537 }
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001538
1539 // Parse nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +00001540 ParseOptionalCXXScopeSpecifier(Result.SS, ParsedType(),
1541 /*EnteringContext=*/false);
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001542
1543 // Check nested-name specifier.
Douglas Gregor43edb322011-10-24 22:31:10 +00001544 if (Result.SS.isInvalid()) {
1545 T.skipToEnd();
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001546 return true;
1547 }
1548
Abramo Bagnara7945c982012-01-27 09:46:47 +00001549 // Parse the unqualified-id.
1550 SourceLocation TemplateKWLoc; // FIXME: parsed, but unused.
1551 if (ParseUnqualifiedId(Result.SS, false, true, true, ParsedType(),
1552 TemplateKWLoc, Result.Name)) {
Douglas Gregor43edb322011-10-24 22:31:10 +00001553 T.skipToEnd();
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001554 return true;
1555 }
1556
Douglas Gregor43edb322011-10-24 22:31:10 +00001557 if (T.consumeClose())
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001558 return true;
Douglas Gregor43edb322011-10-24 22:31:10 +00001559
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001560 // Check if the symbol exists.
Douglas Gregor4a2a8f72011-10-25 03:44:56 +00001561 switch (Actions.CheckMicrosoftIfExistsSymbol(getCurScope(), Result.KeywordLoc,
1562 Result.IsIfExists, Result.SS,
Douglas Gregor43edb322011-10-24 22:31:10 +00001563 Result.Name)) {
1564 case Sema::IER_Exists:
1565 Result.Behavior = Result.IsIfExists ? IEB_Parse : IEB_Skip;
1566 break;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001567
Douglas Gregor43edb322011-10-24 22:31:10 +00001568 case Sema::IER_DoesNotExist:
1569 Result.Behavior = !Result.IsIfExists ? IEB_Parse : IEB_Skip;
1570 break;
1571
1572 case Sema::IER_Dependent:
1573 Result.Behavior = IEB_Dependent;
1574 break;
Douglas Gregor4a2a8f72011-10-25 03:44:56 +00001575
1576 case Sema::IER_Error:
1577 return true;
Douglas Gregor43edb322011-10-24 22:31:10 +00001578 }
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001579
1580 return false;
1581}
1582
Francois Pichet8f981d52011-05-25 10:19:49 +00001583void Parser::ParseMicrosoftIfExistsExternalDeclaration() {
Douglas Gregor43edb322011-10-24 22:31:10 +00001584 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001585 if (ParseMicrosoftIfExistsCondition(Result))
1586 return;
1587
Douglas Gregor43edb322011-10-24 22:31:10 +00001588 BalancedDelimiterTracker Braces(*this, tok::l_brace);
1589 if (Braces.consumeOpen()) {
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001590 Diag(Tok, diag::err_expected_lbrace);
1591 return;
1592 }
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001593
Douglas Gregor43edb322011-10-24 22:31:10 +00001594 switch (Result.Behavior) {
1595 case IEB_Parse:
1596 // Parse declarations below.
1597 break;
1598
1599 case IEB_Dependent:
1600 llvm_unreachable("Cannot have a dependent external declaration");
1601
1602 case IEB_Skip:
1603 Braces.skipToEnd();
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001604 return;
1605 }
1606
Douglas Gregor43edb322011-10-24 22:31:10 +00001607 // Parse the declarations.
1608 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001609 ParsedAttributesWithRange attrs(AttrFactory);
1610 MaybeParseCXX0XAttributes(attrs);
1611 MaybeParseMicrosoftAttributes(attrs);
1612 DeclGroupPtrTy Result = ParseExternalDeclaration(attrs);
1613 if (Result && !getCurScope()->getParent())
1614 Actions.getASTConsumer().HandleTopLevelDecl(Result.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00001615 }
1616 Braces.consumeClose();
Francois Picheta5b3fcb2011-05-07 17:30:27 +00001617}
Douglas Gregor08142532011-08-26 23:56:07 +00001618
Douglas Gregor22d09742012-01-03 18:04:46 +00001619Parser::DeclGroupPtrTy Parser::ParseModuleImport(SourceLocation AtLoc) {
Ted Kremenekc1e4dd02012-03-01 22:07:04 +00001620 assert(Tok.isObjCAtKeyword(tok::objc___experimental_modules_import) &&
Douglas Gregorca975892011-08-31 18:19:09 +00001621 "Improper start to module import");
Douglas Gregor08142532011-08-26 23:56:07 +00001622 SourceLocation ImportLoc = ConsumeToken();
1623
Douglas Gregor71944202011-11-30 00:36:36 +00001624 llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
Douglas Gregor08142532011-08-26 23:56:07 +00001625
Douglas Gregor71944202011-11-30 00:36:36 +00001626 // Parse the module path.
1627 do {
1628 if (!Tok.is(tok::identifier)) {
Douglas Gregor07f43572012-01-29 18:15:03 +00001629 if (Tok.is(tok::code_completion)) {
1630 Actions.CodeCompleteModuleImport(ImportLoc, Path);
1631 ConsumeCodeCompletionToken();
1632 SkipUntil(tok::semi);
1633 return DeclGroupPtrTy();
1634 }
1635
Douglas Gregor71944202011-11-30 00:36:36 +00001636 Diag(Tok, diag::err_module_expected_ident);
1637 SkipUntil(tok::semi);
1638 return DeclGroupPtrTy();
1639 }
1640
1641 // Record this part of the module path.
1642 Path.push_back(std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()));
1643 ConsumeToken();
1644
1645 if (Tok.is(tok::period)) {
1646 ConsumeToken();
1647 continue;
1648 }
1649
1650 break;
1651 } while (true);
1652
Douglas Gregor22d09742012-01-03 18:04:46 +00001653 DeclResult Import = Actions.ActOnModuleImport(AtLoc, ImportLoc, Path);
Douglas Gregor08142532011-08-26 23:56:07 +00001654 ExpectAndConsumeSemi(diag::err_module_expected_semi);
1655 if (Import.isInvalid())
1656 return DeclGroupPtrTy();
1657
1658 return Actions.ConvertDeclToDeclGroup(Import.get());
1659}
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001660
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001661bool Parser::BalancedDelimiterTracker::diagnoseOverflow() {
1662 P.Diag(P.Tok, diag::err_parser_impl_limit_overflow);
1663 P.SkipUntil(tok::eof);
1664 return true;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001665}
1666
1667bool Parser::BalancedDelimiterTracker::expectAndConsume(unsigned DiagID,
1668 const char *Msg,
1669 tok::TokenKind SkipToToc ) {
1670 LOpen = P.Tok.getLocation();
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001671 if (P.ExpectAndConsume(Kind, DiagID, Msg, SkipToToc))
1672 return true;
1673
1674 if (getDepth() < MaxDepth)
1675 return false;
1676
1677 return diagnoseOverflow();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001678}
1679
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001680bool Parser::BalancedDelimiterTracker::diagnoseMissingClose() {
1681 assert(!P.Tok.is(Close) && "Should have consumed closing delimiter");
1682
1683 const char *LHSName = "unknown";
1684 diag::kind DID = diag::err_parse_error;
1685 switch (Close) {
1686 default: break;
1687 case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
1688 case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
1689 case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001690 }
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001691 P.Diag(P.Tok, DID);
1692 P.Diag(LOpen, diag::note_matching) << LHSName;
1693 if (P.SkipUntil(Close))
1694 LClose = P.Tok.getLocation();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001695 return true;
1696}
Douglas Gregor43edb322011-10-24 22:31:10 +00001697
1698void Parser::BalancedDelimiterTracker::skipToEnd() {
1699 P.SkipUntil(Close, false);
Douglas Gregor43edb322011-10-24 22:31:10 +00001700}