blob: f2bc303acd698d7f017ffaceafa5f0a792be5662 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Parser.cpp - C Language Family Parser ----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/Parse/DeclSpec.h"
17#include "clang/Parse/Scope.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000018#include "clang/Parse/Template.h"
Chris Lattner0102c302009-03-05 07:24:28 +000019#include "llvm/Support/raw_ostream.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +000021#include "ParsePragma.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Douglas Gregor2e222532009-07-02 17:08:52 +000024/// \brief A comment handler that passes comments found by the preprocessor
25/// to the parser action.
26class ActionCommentHandler : public CommentHandler {
27 Action &Actions;
Mike Stump1eb44332009-09-09 15:08:12 +000028
Douglas Gregor2e222532009-07-02 17:08:52 +000029public:
30 explicit ActionCommentHandler(Action &Actions) : Actions(Actions) { }
Mike Stump1eb44332009-09-09 15:08:12 +000031
Chris Lattner046c2272010-01-18 22:35:47 +000032 virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) {
Douglas Gregor2e222532009-07-02 17:08:52 +000033 Actions.ActOnComment(Comment);
Chris Lattner046c2272010-01-18 22:35:47 +000034 return false;
Douglas Gregor2e222532009-07-02 17:08:52 +000035 }
36};
37
Reid Spencer5f016e22007-07-11 17:01:13 +000038Parser::Parser(Preprocessor &pp, Action &actions)
Mike Stump1eb44332009-09-09 15:08:12 +000039 : CrashInfo(*this), PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
Chris Lattner08d92ec2009-12-10 00:32:41 +000040 GreaterThanIsOperator(true), ColonIsSacred(false),
41 TemplateParameterDepth(0) {
Reid Spencer5f016e22007-07-11 17:01:13 +000042 Tok.setKind(tok::eof);
43 CurScope = 0;
Chris Lattner9e344c62007-07-15 00:04:39 +000044 NumCachedScopes = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000045 ParenCount = BracketCount = BraceCount = 0;
Chris Lattnerb28317a2009-03-28 19:18:32 +000046 ObjCImpDecl = DeclPtrTy();
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +000047
48 // Add #pragma handlers. These are removed and destroyed in the
49 // destructor.
Ted Kremenek4726d032009-03-23 22:28:25 +000050 PackHandler.reset(new
51 PragmaPackHandler(&PP.getIdentifierTable().get("pack"), actions));
52 PP.AddPragmaHandler(0, PackHandler.get());
Mike Stump1eb44332009-09-09 15:08:12 +000053
Ted Kremenek4726d032009-03-23 22:28:25 +000054 UnusedHandler.reset(new
55 PragmaUnusedHandler(&PP.getIdentifierTable().get("unused"), actions,
56 *this));
57 PP.AddPragmaHandler(0, UnusedHandler.get());
Eli Friedman99914792009-06-05 00:49:58 +000058
59 WeakHandler.reset(new
60 PragmaWeakHandler(&PP.getIdentifierTable().get("weak"), actions));
61 PP.AddPragmaHandler(0, WeakHandler.get());
Mike Stump1eb44332009-09-09 15:08:12 +000062
Douglas Gregor2e222532009-07-02 17:08:52 +000063 CommentHandler.reset(new ActionCommentHandler(actions));
Mike Stump1eb44332009-09-09 15:08:12 +000064 PP.AddCommentHandler(CommentHandler.get());
Reid Spencer5f016e22007-07-11 17:01:13 +000065}
66
Chris Lattner0102c302009-03-05 07:24:28 +000067/// If a crash happens while the parser is active, print out a line indicating
68/// what the current token is.
69void PrettyStackTraceParserEntry::print(llvm::raw_ostream &OS) const {
70 const Token &Tok = P.getCurToken();
Chris Lattnerddcbc0a2009-03-05 07:27:50 +000071 if (Tok.is(tok::eof)) {
Chris Lattner0102c302009-03-05 07:24:28 +000072 OS << "<eof> parser at end of file\n";
73 return;
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Chris Lattnerddcbc0a2009-03-05 07:27:50 +000076 if (Tok.getLocation().isInvalid()) {
77 OS << "<unknown> parser at unknown location\n";
78 return;
79 }
Mike Stump1eb44332009-09-09 15:08:12 +000080
Chris Lattner0102c302009-03-05 07:24:28 +000081 const Preprocessor &PP = P.getPreprocessor();
82 Tok.getLocation().print(OS, PP.getSourceManager());
Daniel Dunbar9fa31dd2009-10-17 06:13:04 +000083 if (Tok.isAnnotation())
84 OS << ": at annotation token \n";
85 else
86 OS << ": current parser token '" << PP.getSpelling(Tok) << "'\n";
Douglas Gregorf780abc2008-12-30 03:27:21 +000087}
Reid Spencer5f016e22007-07-11 17:01:13 +000088
Chris Lattner0102c302009-03-05 07:24:28 +000089
Chris Lattner3cbfe2c2008-11-22 00:59:29 +000090DiagnosticBuilder Parser::Diag(SourceLocation Loc, unsigned DiagID) {
Chris Lattner0102c302009-03-05 07:24:28 +000091 return Diags.Report(FullSourceLoc(Loc, PP.getSourceManager()), DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +000092}
93
Chris Lattner3cbfe2c2008-11-22 00:59:29 +000094DiagnosticBuilder Parser::Diag(const Token &Tok, unsigned DiagID) {
Chris Lattner1ab3b962008-11-18 07:48:38 +000095 return Diag(Tok.getLocation(), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +000096}
97
Douglas Gregor4b2d3f72009-02-26 21:00:50 +000098/// \brief Emits a diagnostic suggesting parentheses surrounding a
99/// given range.
100///
101/// \param Loc The location where we'll emit the diagnostic.
102/// \param Loc The kind of diagnostic to emit.
103/// \param ParenRange Source range enclosing code that should be parenthesized.
104void Parser::SuggestParentheses(SourceLocation Loc, unsigned DK,
105 SourceRange ParenRange) {
Douglas Gregorb2fb6de2009-02-27 17:53:17 +0000106 SourceLocation EndLoc = PP.getLocForEndOfToken(ParenRange.getEnd());
107 if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) {
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000108 // We can't display the parentheses, so just dig the
109 // warning/error and return.
110 Diag(Loc, DK);
111 return;
112 }
Mike Stump1eb44332009-09-09 15:08:12 +0000113
114 Diag(Loc, DK)
Douglas Gregorb2fb6de2009-02-27 17:53:17 +0000115 << CodeModificationHint::CreateInsertion(ParenRange.getBegin(), "(")
116 << CodeModificationHint::CreateInsertion(EndLoc, ")");
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000117}
118
Reid Spencer5f016e22007-07-11 17:01:13 +0000119/// MatchRHSPunctuation - For punctuation with a LHS and RHS (e.g. '['/']'),
120/// this helper function matches and consumes the specified RHS token if
121/// present. If not present, it emits the specified diagnostic indicating
122/// that the parser failed to match the RHS of the token at LHSLoc. LHSName
123/// should be the name of the unmatched LHS token.
124SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
125 SourceLocation LHSLoc) {
Mike Stumpa6f01772008-06-19 19:28:49 +0000126
Chris Lattner00073222007-10-09 17:23:58 +0000127 if (Tok.is(RHSTok))
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 return ConsumeAnyToken();
Mike Stumpa6f01772008-06-19 19:28:49 +0000129
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 SourceLocation R = Tok.getLocation();
131 const char *LHSName = "unknown";
132 diag::kind DID = diag::err_parse_error;
133 switch (RHSTok) {
134 default: break;
135 case tok::r_paren : LHSName = "("; DID = diag::err_expected_rparen; break;
136 case tok::r_brace : LHSName = "{"; DID = diag::err_expected_rbrace; break;
137 case tok::r_square: LHSName = "["; DID = diag::err_expected_rsquare; break;
138 case tok::greater: LHSName = "<"; DID = diag::err_expected_greater; break;
139 }
140 Diag(Tok, DID);
Chris Lattner28eb7e92008-11-23 23:17:07 +0000141 Diag(LHSLoc, diag::note_matching) << LHSName;
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 SkipUntil(RHSTok);
143 return R;
144}
145
146/// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
147/// input. If so, it is consumed and false is returned.
148///
149/// If the input is malformed, this emits the specified diagnostic. Next, if
150/// SkipToTok is specified, it calls SkipUntil(SkipToTok). Finally, true is
151/// returned.
152bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
153 const char *Msg, tok::TokenKind SkipToTok) {
Chris Lattner00073222007-10-09 17:23:58 +0000154 if (Tok.is(ExpectedTok)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 ConsumeAnyToken();
156 return false;
157 }
Mike Stumpa6f01772008-06-19 19:28:49 +0000158
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000159 const char *Spelling = 0;
Douglas Gregorb2fb6de2009-02-27 17:53:17 +0000160 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
Mike Stump1eb44332009-09-09 15:08:12 +0000161 if (EndLoc.isValid() &&
Douglas Gregorb2fb6de2009-02-27 17:53:17 +0000162 (Spelling = tok::getTokenSimpleSpelling(ExpectedTok))) {
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000163 // Show what code to insert to fix this problem.
Mike Stump1eb44332009-09-09 15:08:12 +0000164 Diag(EndLoc, DiagID)
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000165 << Msg
Douglas Gregorb2fb6de2009-02-27 17:53:17 +0000166 << CodeModificationHint::CreateInsertion(EndLoc, Spelling);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000167 } else
168 Diag(Tok, DiagID) << Msg;
169
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 if (SkipToTok != tok::unknown)
171 SkipUntil(SkipToTok);
172 return true;
173}
174
175//===----------------------------------------------------------------------===//
176// Error recovery.
177//===----------------------------------------------------------------------===//
178
179/// SkipUntil - Read tokens until we get to the specified token, then consume
Chris Lattner012cf462007-07-24 17:03:04 +0000180/// it (unless DontConsume is true). Because we cannot guarantee that the
Reid Spencer5f016e22007-07-11 17:01:13 +0000181/// token will ever occur, this skips to the next token, or to some likely
182/// good stopping point. If StopAtSemi is true, skipping will stop at a ';'
183/// character.
Mike Stumpa6f01772008-06-19 19:28:49 +0000184///
Reid Spencer5f016e22007-07-11 17:01:13 +0000185/// If SkipUntil finds the specified token, it returns true, otherwise it
Mike Stumpa6f01772008-06-19 19:28:49 +0000186/// returns false.
Reid Spencer5f016e22007-07-11 17:01:13 +0000187bool Parser::SkipUntil(const tok::TokenKind *Toks, unsigned NumToks,
188 bool StopAtSemi, bool DontConsume) {
189 // We always want this function to skip at least one token if the first token
190 // isn't T and if not at EOF.
191 bool isFirstTokenSkipped = true;
192 while (1) {
193 // If we found one of the tokens, stop and return true.
194 for (unsigned i = 0; i != NumToks; ++i) {
Chris Lattner00073222007-10-09 17:23:58 +0000195 if (Tok.is(Toks[i])) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 if (DontConsume) {
197 // Noop, don't consume the token.
198 } else {
199 ConsumeAnyToken();
200 }
201 return true;
202 }
203 }
Mike Stumpa6f01772008-06-19 19:28:49 +0000204
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 switch (Tok.getKind()) {
206 case tok::eof:
207 // Ran out of tokens.
208 return false;
Mike Stumpa6f01772008-06-19 19:28:49 +0000209
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 case tok::l_paren:
211 // Recursively skip properly-nested parens.
212 ConsumeParen();
213 SkipUntil(tok::r_paren, false);
214 break;
215 case tok::l_square:
216 // Recursively skip properly-nested square brackets.
217 ConsumeBracket();
218 SkipUntil(tok::r_square, false);
219 break;
220 case tok::l_brace:
221 // Recursively skip properly-nested braces.
222 ConsumeBrace();
223 SkipUntil(tok::r_brace, false);
224 break;
Mike Stumpa6f01772008-06-19 19:28:49 +0000225
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
227 // Since the user wasn't looking for this token (if they were, it would
228 // already be handled), this isn't balanced. If there is a LHS token at a
229 // higher level, we will assume that this matches the unbalanced token
230 // and return it. Otherwise, this is a spurious RHS token, which we skip.
231 case tok::r_paren:
232 if (ParenCount && !isFirstTokenSkipped)
233 return false; // Matches something.
234 ConsumeParen();
235 break;
236 case tok::r_square:
237 if (BracketCount && !isFirstTokenSkipped)
238 return false; // Matches something.
239 ConsumeBracket();
240 break;
241 case tok::r_brace:
242 if (BraceCount && !isFirstTokenSkipped)
243 return false; // Matches something.
244 ConsumeBrace();
245 break;
Mike Stumpa6f01772008-06-19 19:28:49 +0000246
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 case tok::string_literal:
248 case tok::wide_string_literal:
249 ConsumeStringToken();
250 break;
251 case tok::semi:
252 if (StopAtSemi)
253 return false;
254 // FALL THROUGH.
255 default:
256 // Skip this token.
257 ConsumeToken();
258 break;
259 }
260 isFirstTokenSkipped = false;
Mike Stumpa6f01772008-06-19 19:28:49 +0000261 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000262}
263
264//===----------------------------------------------------------------------===//
265// Scope manipulation
266//===----------------------------------------------------------------------===//
267
Reid Spencer5f016e22007-07-11 17:01:13 +0000268/// EnterScope - Start a new scope.
269void Parser::EnterScope(unsigned ScopeFlags) {
Chris Lattner9e344c62007-07-15 00:04:39 +0000270 if (NumCachedScopes) {
271 Scope *N = ScopeCache[--NumCachedScopes];
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 N->Init(CurScope, ScopeFlags);
273 CurScope = N;
274 } else {
275 CurScope = new Scope(CurScope, ScopeFlags);
276 }
277}
278
279/// ExitScope - Pop a scope off the scope stack.
280void Parser::ExitScope() {
281 assert(CurScope && "Scope imbalance!");
282
Chris Lattner90ae68a2007-10-09 20:37:18 +0000283 // Inform the actions module that this scope is going away if there are any
284 // decls in it.
285 if (!CurScope->decl_empty())
Steve Naroffb216c882007-10-09 22:01:59 +0000286 Actions.ActOnPopScope(Tok.getLocation(), CurScope);
Mike Stumpa6f01772008-06-19 19:28:49 +0000287
Chris Lattner9e344c62007-07-15 00:04:39 +0000288 Scope *OldScope = CurScope;
289 CurScope = OldScope->getParent();
Mike Stumpa6f01772008-06-19 19:28:49 +0000290
Chris Lattner9e344c62007-07-15 00:04:39 +0000291 if (NumCachedScopes == ScopeCacheSize)
292 delete OldScope;
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 else
Chris Lattner9e344c62007-07-15 00:04:39 +0000294 ScopeCache[NumCachedScopes++] = OldScope;
Reid Spencer5f016e22007-07-11 17:01:13 +0000295}
296
297
298
299
300//===----------------------------------------------------------------------===//
301// C99 6.9: External Definitions.
302//===----------------------------------------------------------------------===//
303
304Parser::~Parser() {
305 // If we still have scopes active, delete the scope tree.
306 delete CurScope;
Mike Stumpa6f01772008-06-19 19:28:49 +0000307
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 // Free the scope cache.
Chris Lattner9e344c62007-07-15 00:04:39 +0000309 for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
310 delete ScopeCache[i];
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000311
312 // Remove the pragma handlers we installed.
Ted Kremenek4726d032009-03-23 22:28:25 +0000313 PP.RemovePragmaHandler(0, PackHandler.get());
314 PackHandler.reset();
315 PP.RemovePragmaHandler(0, UnusedHandler.get());
316 UnusedHandler.reset();
Eli Friedman99914792009-06-05 00:49:58 +0000317 PP.RemovePragmaHandler(0, WeakHandler.get());
318 WeakHandler.reset();
Douglas Gregor2e222532009-07-02 17:08:52 +0000319 PP.RemoveCommentHandler(CommentHandler.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000320}
321
322/// Initialize - Warm up the parser.
323///
324void Parser::Initialize() {
325 // Prime the lexer look-ahead.
326 ConsumeToken();
Mike Stumpa6f01772008-06-19 19:28:49 +0000327
Chris Lattner31e05722007-08-26 06:24:45 +0000328 // Create the translation unit scope. Install it as the current scope.
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 assert(CurScope == 0 && "A scope is already active?");
Chris Lattner31e05722007-08-26 06:24:45 +0000330 EnterScope(Scope::DeclScope);
Steve Naroffb216c882007-10-09 22:01:59 +0000331 Actions.ActOnTranslationUnitScope(Tok.getLocation(), CurScope);
Mike Stumpa6f01772008-06-19 19:28:49 +0000332
Chris Lattner00073222007-10-09 17:23:58 +0000333 if (Tok.is(tok::eof) &&
Chris Lattnerf7261752007-08-25 05:47:03 +0000334 !getLang().CPlusPlus) // Empty source file is an extension in C
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 Diag(Tok, diag::ext_empty_source_file);
Mike Stumpa6f01772008-06-19 19:28:49 +0000336
Chris Lattner34870da2007-08-29 22:54:08 +0000337 // Initialization for Objective-C context sensitive keywords recognition.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000338 // Referenced in Parser::ParseObjCTypeQualifierList.
Chris Lattner34870da2007-08-29 22:54:08 +0000339 if (getLang().ObjC1) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000340 ObjCTypeQuals[objc_in] = &PP.getIdentifierTable().get("in");
341 ObjCTypeQuals[objc_out] = &PP.getIdentifierTable().get("out");
342 ObjCTypeQuals[objc_inout] = &PP.getIdentifierTable().get("inout");
343 ObjCTypeQuals[objc_oneway] = &PP.getIdentifierTable().get("oneway");
344 ObjCTypeQuals[objc_bycopy] = &PP.getIdentifierTable().get("bycopy");
345 ObjCTypeQuals[objc_byref] = &PP.getIdentifierTable().get("byref");
Chris Lattner34870da2007-08-29 22:54:08 +0000346 }
Daniel Dunbar662e8b52008-08-14 22:04:54 +0000347
348 Ident_super = &PP.getIdentifierTable().get("super");
Reid Spencer5f016e22007-07-11 17:01:13 +0000349}
350
351/// ParseTopLevelDecl - Parse one top-level declaration, return whatever the
352/// action tells us to. This returns true if the EOF was encountered.
Chris Lattner682bf922009-03-29 16:50:03 +0000353bool Parser::ParseTopLevelDecl(DeclGroupPtrTy &Result) {
354 Result = DeclGroupPtrTy();
Chris Lattner9299f3f2008-08-23 03:19:52 +0000355 if (Tok.is(tok::eof)) {
356 Actions.ActOnEndOfTranslationUnit();
357 return true;
358 }
Mike Stumpa6f01772008-06-19 19:28:49 +0000359
Sean Huntbbd37c62009-11-21 08:43:09 +0000360 CXX0XAttributeList Attr;
361 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
362 Attr = ParseCXX0XAttributes();
363 Result = ParseExternalDeclaration(Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 return false;
365}
366
Reid Spencer5f016e22007-07-11 17:01:13 +0000367/// ParseTranslationUnit:
368/// translation-unit: [C99 6.9]
Mike Stumpa6f01772008-06-19 19:28:49 +0000369/// external-declaration
370/// translation-unit external-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000371void Parser::ParseTranslationUnit() {
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000372 Initialize();
Mike Stumpa6f01772008-06-19 19:28:49 +0000373
Chris Lattner682bf922009-03-29 16:50:03 +0000374 DeclGroupPtrTy Res;
Steve Naroff89307ff2007-11-29 23:05:20 +0000375 while (!ParseTopLevelDecl(Res))
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 /*parse them all*/;
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Chris Lattner06f54852008-08-23 02:00:52 +0000378 ExitScope();
379 assert(CurScope == 0 && "Scope imbalance!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000380}
381
382/// ParseExternalDeclaration:
Chris Lattner90b93d62008-12-08 21:59:01 +0000383///
Douglas Gregorc19923d2008-11-21 16:10:08 +0000384/// external-declaration: [C99 6.9], declaration: [C++ dcl.dcl]
Chris Lattnerc3018152007-08-10 20:57:02 +0000385/// function-definition
386/// declaration
Douglas Gregora1d71ae2009-08-24 12:17:54 +0000387/// [C++0x] empty-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000388/// [GNU] asm-definition
Chris Lattnerc3018152007-08-10 20:57:02 +0000389/// [GNU] __extension__ external-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000390/// [OBJC] objc-class-definition
391/// [OBJC] objc-class-declaration
392/// [OBJC] objc-alias-declaration
393/// [OBJC] objc-protocol-definition
394/// [OBJC] objc-method-definition
395/// [OBJC] @end
Douglas Gregorc19923d2008-11-21 16:10:08 +0000396/// [C++] linkage-specification
Reid Spencer5f016e22007-07-11 17:01:13 +0000397/// [GNU] asm-definition:
398/// simple-asm-expr ';'
399///
Douglas Gregora1d71ae2009-08-24 12:17:54 +0000400/// [C++0x] empty-declaration:
401/// ';'
402///
Douglas Gregor45f96552009-09-04 06:33:52 +0000403/// [C++0x/GNU] 'extern' 'template' declaration
Sean Huntbbd37c62009-11-21 08:43:09 +0000404Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(CXX0XAttributeList Attr) {
Chris Lattner682bf922009-03-29 16:50:03 +0000405 DeclPtrTy SingleDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 switch (Tok.getKind()) {
407 case tok::semi:
Douglas Gregora1d71ae2009-08-24 12:17:54 +0000408 if (!getLang().CPlusPlus0x)
409 Diag(Tok, diag::ext_top_level_semi)
Chris Lattner29d9c1a2009-12-06 17:36:05 +0000410 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 ConsumeToken();
413 // TODO: Invoke action for top-level semicolon.
Chris Lattner682bf922009-03-29 16:50:03 +0000414 return DeclGroupPtrTy();
Chris Lattner90b93d62008-12-08 21:59:01 +0000415 case tok::r_brace:
416 Diag(Tok, diag::err_expected_external_declaration);
417 ConsumeBrace();
Chris Lattner682bf922009-03-29 16:50:03 +0000418 return DeclGroupPtrTy();
Chris Lattner90b93d62008-12-08 21:59:01 +0000419 case tok::eof:
420 Diag(Tok, diag::err_expected_external_declaration);
Chris Lattner682bf922009-03-29 16:50:03 +0000421 return DeclGroupPtrTy();
Chris Lattnerc3018152007-08-10 20:57:02 +0000422 case tok::kw___extension__: {
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000423 // __extension__ silences extension warnings in the subexpression.
424 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Chris Lattner39146d62008-10-20 06:51:33 +0000425 ConsumeToken();
Sean Huntbbd37c62009-11-21 08:43:09 +0000426 return ParseExternalDeclaration(Attr);
Chris Lattnerc3018152007-08-10 20:57:02 +0000427 }
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000428 case tok::kw_asm: {
Sean Huntbbd37c62009-11-21 08:43:09 +0000429 if (Attr.HasAttr)
430 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
431 << Attr.Range;
432
Sebastian Redleffa8d12008-12-10 00:02:53 +0000433 OwningExprResult Result(ParseSimpleAsm());
Mike Stumpa6f01772008-06-19 19:28:49 +0000434
Anders Carlsson3f9424f2008-02-08 00:23:11 +0000435 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
436 "top-level asm block");
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000437
Chris Lattner682bf922009-03-29 16:50:03 +0000438 if (Result.isInvalid())
439 return DeclGroupPtrTy();
440 SingleDecl = Actions.ActOnFileScopeAsmDecl(Tok.getLocation(), move(Result));
441 break;
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000442 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 case tok::at:
Chris Lattner682bf922009-03-29 16:50:03 +0000444 // @ is not a legal token unless objc is enabled, no need to check for ObjC.
445 /// FIXME: ParseObjCAtDirectives should return a DeclGroup for things like
446 /// @class foo, bar;
447 SingleDecl = ParseObjCAtDirectives();
448 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000449 case tok::minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000450 case tok::plus:
Chris Lattner682bf922009-03-29 16:50:03 +0000451 if (!getLang().ObjC1) {
452 Diag(Tok, diag::err_expected_external_declaration);
453 ConsumeToken();
454 return DeclGroupPtrTy();
455 }
456 SingleDecl = ParseObjCMethodDefinition();
457 break;
Douglas Gregor791215b2009-09-21 20:51:25 +0000458 case tok::code_completion:
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000459 Actions.CodeCompleteOrdinaryName(CurScope,
460 ObjCImpDecl? Action::CCC_ObjCImplementation
461 : Action::CCC_Namespace);
Douglas Gregor791215b2009-09-21 20:51:25 +0000462 ConsumeToken();
Sean Huntbbd37c62009-11-21 08:43:09 +0000463 return ParseExternalDeclaration(Attr);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000464 case tok::kw_using:
Chris Lattner8f08cb72007-08-25 06:57:03 +0000465 case tok::kw_namespace:
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 case tok::kw_typedef:
Douglas Gregoradcac882008-12-01 23:54:00 +0000467 case tok::kw_template:
468 case tok::kw_export: // As in 'export template'
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000469 case tok::kw_static_assert:
Chris Lattnerbae35112007-08-25 18:15:16 +0000470 // A function definition cannot start with a these keywords.
Chris Lattner97144fc2009-04-02 04:16:50 +0000471 {
472 SourceLocation DeclEnd;
Sean Huntbbd37c62009-11-21 08:43:09 +0000473 return ParseDeclaration(Declarator::FileContext, DeclEnd, Attr);
Chris Lattner97144fc2009-04-02 04:16:50 +0000474 }
Douglas Gregor45f96552009-09-04 06:33:52 +0000475 case tok::kw_extern:
476 if (getLang().CPlusPlus && NextToken().is(tok::kw_template)) {
477 // Extern templates
478 SourceLocation ExternLoc = ConsumeToken();
479 SourceLocation TemplateLoc = ConsumeToken();
480 SourceLocation DeclEnd;
481 return Actions.ConvertDeclToDeclGroup(
482 ParseExplicitInstantiation(ExternLoc, TemplateLoc, DeclEnd));
483 }
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Douglas Gregor45f96552009-09-04 06:33:52 +0000485 // FIXME: Detect C++ linkage specifications here?
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Douglas Gregor45f96552009-09-04 06:33:52 +0000487 // Fall through to handle other declarations or function definitions.
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Reid Spencer5f016e22007-07-11 17:01:13 +0000489 default:
490 // We can't tell whether this is a function-definition or declaration yet.
Sean Huntbbd37c62009-11-21 08:43:09 +0000491 return ParseDeclarationOrFunctionDefinition(Attr.AttrList);
Reid Spencer5f016e22007-07-11 17:01:13 +0000492 }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Chris Lattner682bf922009-03-29 16:50:03 +0000494 // This routine returns a DeclGroup, if the thing we parsed only contains a
495 // single decl, convert it now.
496 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000497}
498
Douglas Gregor1426e532009-05-12 21:31:51 +0000499/// \brief Determine whether the current token, if it occurs after a
500/// declarator, continues a declaration or declaration list.
501bool Parser::isDeclarationAfterDeclarator() {
502 return Tok.is(tok::equal) || // int X()= -> not a function def
503 Tok.is(tok::comma) || // int X(), -> not a function def
504 Tok.is(tok::semi) || // int X(); -> not a function def
505 Tok.is(tok::kw_asm) || // int X() __asm__ -> not a function def
506 Tok.is(tok::kw___attribute) || // int X() __attr__ -> not a function def
507 (getLang().CPlusPlus &&
508 Tok.is(tok::l_paren)); // int X(0) -> not a function def [C++]
509}
510
511/// \brief Determine whether the current token, if it occurs after a
512/// declarator, indicates the start of a function definition.
513bool Parser::isStartOfFunctionDefinition() {
Chris Lattner5d1c6192009-12-06 18:34:27 +0000514 if (Tok.is(tok::l_brace)) // int X() {}
515 return true;
516
517 if (!getLang().CPlusPlus)
518 return isDeclarationSpecifier(); // int X(f) int f; {}
519 return Tok.is(tok::colon) || // X() : Base() {} (used for ctors)
520 Tok.is(tok::kw_try); // X() try { ... }
Douglas Gregor1426e532009-05-12 21:31:51 +0000521}
522
Reid Spencer5f016e22007-07-11 17:01:13 +0000523/// ParseDeclarationOrFunctionDefinition - Parse either a function-definition or
524/// a declaration. We can't tell which we have until we read up to the
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000525/// compound-statement in function-definition. TemplateParams, if
526/// non-NULL, provides the template parameters when we're parsing a
Mike Stump1eb44332009-09-09 15:08:12 +0000527/// C++ template-declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000528///
529/// function-definition: [C99 6.9.1]
Chris Lattnera798ebc2008-04-05 05:52:15 +0000530/// decl-specs declarator declaration-list[opt] compound-statement
531/// [C90] function-definition: [C99 6.7.1] - implicit int result
Mike Stumpa6f01772008-06-19 19:28:49 +0000532/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
Chris Lattnera798ebc2008-04-05 05:52:15 +0000533///
Reid Spencer5f016e22007-07-11 17:01:13 +0000534/// declaration: [C99 6.7]
Chris Lattner697e15f2007-08-22 06:06:56 +0000535/// declaration-specifiers init-declarator-list[opt] ';'
536/// [!C99] init-declarator-list ';' [TODO: warn in c99 mode]
Reid Spencer5f016e22007-07-11 17:01:13 +0000537/// [OMP] threadprivate-directive [TODO]
538///
Chris Lattner682bf922009-03-29 16:50:03 +0000539Parser::DeclGroupPtrTy
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000540Parser::ParseDeclarationOrFunctionDefinition(ParsingDeclSpec &DS,
541 AttributeList *Attr,
Sean Huntbbd37c62009-11-21 08:43:09 +0000542 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 // Parse the common declaration-specifiers piece.
Sean Huntbbd37c62009-11-21 08:43:09 +0000544 if (Attr)
545 DS.AddAttributes(Attr);
546
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000547 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC_top_level);
Mike Stumpa6f01772008-06-19 19:28:49 +0000548
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
550 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner00073222007-10-09 17:23:58 +0000551 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000553 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall54abf7d2009-11-04 02:18:39 +0000554 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000555 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 }
Mike Stumpa6f01772008-06-19 19:28:49 +0000557
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000558 // ObjC2 allows prefix attributes on class interfaces and protocols.
559 // FIXME: This still needs better diagnostics. We should only accept
560 // attributes here, no types, etc.
Chris Lattner00073222007-10-09 17:23:58 +0000561 if (getLang().ObjC2 && Tok.is(tok::at)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000562 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +0000563 if (!Tok.isObjCAtKeyword(tok::objc_interface) &&
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000564 !Tok.isObjCAtKeyword(tok::objc_protocol)) {
565 Diag(Tok, diag::err_objc_unexpected_attr);
Chris Lattnercb53b362007-12-27 19:57:00 +0000566 SkipUntil(tok::semi); // FIXME: better skip?
Chris Lattner682bf922009-03-29 16:50:03 +0000567 return DeclGroupPtrTy();
Chris Lattnercb53b362007-12-27 19:57:00 +0000568 }
John McCalld8ac0572009-11-03 19:26:08 +0000569
John McCall54abf7d2009-11-04 02:18:39 +0000570 DS.abort();
571
Fariborz Jahanian0de2ae22008-01-02 19:17:38 +0000572 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000573 unsigned DiagID;
574 if (DS.SetTypeSpecType(DeclSpec::TST_unspecified, AtLoc, PrevSpec, DiagID))
575 Diag(AtLoc, DiagID) << PrevSpec;
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Chris Lattner682bf922009-03-29 16:50:03 +0000577 DeclPtrTy TheDecl;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000578 if (Tok.isObjCAtKeyword(tok::objc_protocol))
Chris Lattner682bf922009-03-29 16:50:03 +0000579 TheDecl = ParseObjCAtProtocolDeclaration(AtLoc, DS.getAttributes());
580 else
581 TheDecl = ParseObjCAtInterfaceDeclaration(AtLoc, DS.getAttributes());
582 return Actions.ConvertDeclToDeclGroup(TheDecl);
Steve Naroffdac269b2007-08-20 21:31:48 +0000583 }
Mike Stumpa6f01772008-06-19 19:28:49 +0000584
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000585 // If the declspec consisted only of 'extern' and we have a string
586 // literal following it, this must be a C++ linkage specifier like
587 // 'extern "C"'.
Chris Lattner3c6f6a72008-01-12 07:08:43 +0000588 if (Tok.is(tok::string_literal) && getLang().CPlusPlus &&
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000589 DS.getStorageClassSpec() == DeclSpec::SCS_extern &&
Chris Lattner682bf922009-03-29 16:50:03 +0000590 DS.getParsedSpecifiers() == DeclSpec::PQ_StorageClassSpecifier) {
John McCall54abf7d2009-11-04 02:18:39 +0000591 DS.abort();
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000592 DeclPtrTy TheDecl = ParseLinkage(DS, Declarator::FileContext);
Chris Lattner682bf922009-03-29 16:50:03 +0000593 return Actions.ConvertDeclToDeclGroup(TheDecl);
594 }
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000595
John McCalld8ac0572009-11-03 19:26:08 +0000596 return ParseDeclGroup(DS, Declarator::FileContext, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000597}
598
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000599Parser::DeclGroupPtrTy
600Parser::ParseDeclarationOrFunctionDefinition(AttributeList *Attr,
601 AccessSpecifier AS) {
602 ParsingDeclSpec DS(*this);
603 return ParseDeclarationOrFunctionDefinition(DS, Attr, AS);
604}
605
Reid Spencer5f016e22007-07-11 17:01:13 +0000606/// ParseFunctionDefinition - We parsed and verified that the specified
607/// Declarator is well formed. If this is a K&R-style function, read the
608/// parameters declaration-list, then start the compound-statement.
609///
Chris Lattnera798ebc2008-04-05 05:52:15 +0000610/// function-definition: [C99 6.9.1]
611/// decl-specs declarator declaration-list[opt] compound-statement
612/// [C90] function-definition: [C99 6.7.1] - implicit int result
Mike Stumpa6f01772008-06-19 19:28:49 +0000613/// [C90] decl-specs[opt] declarator declaration-list[opt] compound-statement
Douglas Gregor7ad83902008-11-05 04:29:56 +0000614/// [C++] function-definition: [C++ 8.4]
Chris Lattner23c4b182009-03-29 17:18:04 +0000615/// decl-specifier-seq[opt] declarator ctor-initializer[opt]
616/// function-body
Douglas Gregor7ad83902008-11-05 04:29:56 +0000617/// [C++] function-definition: [C++ 8.4]
Sebastian Redld3a413d2009-04-26 20:35:05 +0000618/// decl-specifier-seq[opt] declarator function-try-block
Reid Spencer5f016e22007-07-11 17:01:13 +0000619///
John McCall54abf7d2009-11-04 02:18:39 +0000620Parser::DeclPtrTy Parser::ParseFunctionDefinition(ParsingDeclarator &D,
Douglas Gregor52591bf2009-06-24 00:54:41 +0000621 const ParsedTemplateInfo &TemplateInfo) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 const DeclaratorChunk &FnTypeInfo = D.getTypeObject(0);
623 assert(FnTypeInfo.Kind == DeclaratorChunk::Function &&
624 "This isn't a function declarator!");
625 const DeclaratorChunk::FunctionTypeInfo &FTI = FnTypeInfo.Fun;
Mike Stumpa6f01772008-06-19 19:28:49 +0000626
Chris Lattnera798ebc2008-04-05 05:52:15 +0000627 // If this is C90 and the declspecs were completely missing, fudge in an
628 // implicit int. We do this here because this is the only place where
629 // declaration-specifiers are completely optional in the grammar.
Chris Lattner2a327d12009-02-27 18:35:46 +0000630 if (getLang().ImplicitInt && D.getDeclSpec().isEmpty()) {
Chris Lattnera798ebc2008-04-05 05:52:15 +0000631 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000632 unsigned DiagID;
Chris Lattner31c28682008-10-20 02:01:34 +0000633 D.getMutableDeclSpec().SetTypeSpecType(DeclSpec::TST_int,
634 D.getIdentifierLoc(),
John McCallfec54012009-08-03 20:12:06 +0000635 PrevSpec, DiagID);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000636 D.SetRangeBegin(D.getDeclSpec().getSourceRange().getBegin());
Chris Lattnera798ebc2008-04-05 05:52:15 +0000637 }
Mike Stumpa6f01772008-06-19 19:28:49 +0000638
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 // If this declaration was formed with a K&R-style identifier list for the
640 // arguments, parse declarations for all of the args next.
641 // int foo(a,b) int a; float b; {}
642 if (!FTI.hasPrototype && FTI.NumArgs != 0)
643 ParseKNRParamDeclarations(D);
644
Douglas Gregor7ad83902008-11-05 04:29:56 +0000645 // We should have either an opening brace or, in a C++ constructor,
646 // we may have a colon.
Sebastian Redld3a413d2009-04-26 20:35:05 +0000647 if (Tok.isNot(tok::l_brace) && Tok.isNot(tok::colon) &&
648 Tok.isNot(tok::kw_try)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 Diag(Tok, diag::err_expected_fn_body);
650
651 // Skip over garbage, until we get to '{'. Don't eat the '{'.
652 SkipUntil(tok::l_brace, true, true);
Mike Stumpa6f01772008-06-19 19:28:49 +0000653
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 // If we didn't find the '{', bail out.
Chris Lattner00073222007-10-09 17:23:58 +0000655 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000656 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 }
Mike Stumpa6f01772008-06-19 19:28:49 +0000658
Chris Lattnerb652cea2007-10-09 17:14:05 +0000659 // Enter a scope for the function body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000660 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stumpa6f01772008-06-19 19:28:49 +0000661
Chris Lattnerb652cea2007-10-09 17:14:05 +0000662 // Tell the actions module that we have entered a function definition with the
663 // specified Declarator for the function.
Mike Stump1eb44332009-09-09 15:08:12 +0000664 DeclPtrTy Res = TemplateInfo.TemplateParams?
Douglas Gregor52591bf2009-06-24 00:54:41 +0000665 Actions.ActOnStartOfFunctionTemplateDef(CurScope,
666 Action::MultiTemplateParamsArg(Actions,
667 TemplateInfo.TemplateParams->data(),
668 TemplateInfo.TemplateParams->size()),
669 D)
670 : Actions.ActOnStartOfFunctionDef(CurScope, D);
Mike Stumpa6f01772008-06-19 19:28:49 +0000671
John McCall54abf7d2009-11-04 02:18:39 +0000672 // Break out of the ParsingDeclarator context before we parse the body.
673 D.complete(Res);
674
675 // Break out of the ParsingDeclSpec context, too. This const_cast is
676 // safe because we're always the sole owner.
677 D.getMutableDeclSpec().abort();
678
Sebastian Redld3a413d2009-04-26 20:35:05 +0000679 if (Tok.is(tok::kw_try))
680 return ParseFunctionTryBlock(Res);
681
Douglas Gregor7ad83902008-11-05 04:29:56 +0000682 // If we have a colon, then we're probably parsing a C++
683 // ctor-initializer.
684 if (Tok.is(tok::colon))
685 ParseConstructorInitializer(Res);
Fariborz Jahanian0849d382009-07-14 20:06:22 +0000686 else
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000687 Actions.ActOnDefaultCtorInitializers(Res);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000688
Chris Lattner40e9bc82009-03-05 00:49:17 +0000689 return ParseFunctionStatementBody(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000690}
691
692/// ParseKNRParamDeclarations - Parse 'declaration-list[opt]' which provides
693/// types for a function with a K&R-style identifier list for arguments.
694void Parser::ParseKNRParamDeclarations(Declarator &D) {
695 // We know that the top-level of this declarator is a function.
696 DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
697
Chris Lattner04421082008-04-08 04:40:51 +0000698 // Enter function-declaration scope, limiting any declarators to the
699 // function prototype scope, including parameter declarators.
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000700 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner04421082008-04-08 04:40:51 +0000701
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 // Read all the argument declarations.
703 while (isDeclarationSpecifier()) {
704 SourceLocation DSStart = Tok.getLocation();
Mike Stumpa6f01772008-06-19 19:28:49 +0000705
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 // Parse the common declaration-specifiers piece.
707 DeclSpec DS;
708 ParseDeclarationSpecifiers(DS);
Mike Stumpa6f01772008-06-19 19:28:49 +0000709
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 // C99 6.9.1p6: 'each declaration in the declaration list shall have at
711 // least one declarator'.
712 // NOTE: GCC just makes this an ext-warn. It's not clear what it does with
713 // the declarations though. It's trivial to ignore them, really hard to do
714 // anything else with them.
Chris Lattner00073222007-10-09 17:23:58 +0000715 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 Diag(DSStart, diag::err_declaration_does_not_declare_param);
717 ConsumeToken();
718 continue;
719 }
Mike Stumpa6f01772008-06-19 19:28:49 +0000720
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 // C99 6.9.1p6: Declarations shall contain no storage-class specifiers other
722 // than register.
723 if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified &&
724 DS.getStorageClassSpec() != DeclSpec::SCS_register) {
725 Diag(DS.getStorageClassSpecLoc(),
726 diag::err_invalid_storage_class_in_func_decl);
727 DS.ClearStorageClassSpecs();
728 }
729 if (DS.isThreadSpecified()) {
730 Diag(DS.getThreadSpecLoc(),
731 diag::err_invalid_storage_class_in_func_decl);
732 DS.ClearStorageClassSpecs();
733 }
Mike Stumpa6f01772008-06-19 19:28:49 +0000734
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 // Parse the first declarator attached to this declspec.
736 Declarator ParmDeclarator(DS, Declarator::KNRTypeListContext);
737 ParseDeclarator(ParmDeclarator);
738
739 // Handle the full declarator list.
740 while (1) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000741 Action::AttrTy *AttrList;
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 // If attributes are present, parse them.
Chris Lattner00073222007-10-09 17:23:58 +0000743 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 // FIXME: attach attributes too.
Sean Huntbbd37c62009-11-21 08:43:09 +0000745 AttrList = ParseGNUAttributes();
Mike Stumpa6f01772008-06-19 19:28:49 +0000746
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 // Ask the actions module to compute the type for this declarator.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000748 Action::DeclPtrTy Param =
Chris Lattner04421082008-04-08 04:40:51 +0000749 Actions.ActOnParamDeclarator(CurScope, ParmDeclarator);
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000750
Mike Stumpa6f01772008-06-19 19:28:49 +0000751 if (Param &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 // A missing identifier has already been diagnosed.
753 ParmDeclarator.getIdentifier()) {
754
755 // Scan the argument list looking for the correct param to apply this
756 // type.
757 for (unsigned i = 0; ; ++i) {
758 // C99 6.9.1p6: those declarators shall declare only identifiers from
759 // the identifier list.
760 if (i == FTI.NumArgs) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000761 Diag(ParmDeclarator.getIdentifierLoc(), diag::err_no_matching_param)
Chris Lattner6898e332008-11-19 07:51:13 +0000762 << ParmDeclarator.getIdentifier();
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 break;
764 }
Mike Stumpa6f01772008-06-19 19:28:49 +0000765
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 if (FTI.ArgInfo[i].Ident == ParmDeclarator.getIdentifier()) {
767 // Reject redefinitions of parameters.
Chris Lattner04421082008-04-08 04:40:51 +0000768 if (FTI.ArgInfo[i].Param) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 Diag(ParmDeclarator.getIdentifierLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +0000770 diag::err_param_redefinition)
Chris Lattner6898e332008-11-19 07:51:13 +0000771 << ParmDeclarator.getIdentifier();
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 } else {
Chris Lattner04421082008-04-08 04:40:51 +0000773 FTI.ArgInfo[i].Param = Param;
Reid Spencer5f016e22007-07-11 17:01:13 +0000774 }
775 break;
776 }
777 }
778 }
779
780 // If we don't have a comma, it is either the end of the list (a ';') or
781 // an error, bail out.
Chris Lattner00073222007-10-09 17:23:58 +0000782 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 break;
Mike Stumpa6f01772008-06-19 19:28:49 +0000784
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 // Consume the comma.
786 ConsumeToken();
Mike Stumpa6f01772008-06-19 19:28:49 +0000787
Reid Spencer5f016e22007-07-11 17:01:13 +0000788 // Parse the next declarator.
789 ParmDeclarator.clear();
790 ParseDeclarator(ParmDeclarator);
791 }
Mike Stumpa6f01772008-06-19 19:28:49 +0000792
Chris Lattner00073222007-10-09 17:23:58 +0000793 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000794 ConsumeToken();
795 } else {
796 Diag(Tok, diag::err_parse_error);
797 // Skip to end of block or statement
798 SkipUntil(tok::semi, true);
Chris Lattner00073222007-10-09 17:23:58 +0000799 if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 ConsumeToken();
801 }
802 }
Mike Stumpa6f01772008-06-19 19:28:49 +0000803
Reid Spencer5f016e22007-07-11 17:01:13 +0000804 // The actions module must verify that all arguments were declared.
Douglas Gregora3a83512009-04-01 23:51:29 +0000805 Actions.ActOnFinishKNRParamDeclarations(CurScope, D, Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000806}
807
808
809/// ParseAsmStringLiteral - This is just a normal string-literal, but is not
810/// allowed to be a wide string, and is not subject to character translation.
811///
812/// [GNU] asm-string-literal:
813/// string-literal
814///
Sebastian Redleffa8d12008-12-10 00:02:53 +0000815Parser::OwningExprResult Parser::ParseAsmStringLiteral() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 if (!isTokenStringLiteral()) {
817 Diag(Tok, diag::err_expected_string_literal);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000818 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 }
Mike Stumpa6f01772008-06-19 19:28:49 +0000820
Sebastian Redl20df9b72008-12-11 22:51:44 +0000821 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redleffa8d12008-12-10 00:02:53 +0000822 if (Res.isInvalid()) return move(Res);
Mike Stumpa6f01772008-06-19 19:28:49 +0000823
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 // TODO: Diagnose: wide string literal in 'asm'
Mike Stumpa6f01772008-06-19 19:28:49 +0000825
Sebastian Redleffa8d12008-12-10 00:02:53 +0000826 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000827}
828
829/// ParseSimpleAsm
830///
831/// [GNU] simple-asm-expr:
832/// 'asm' '(' asm-string-literal ')'
833///
Sebastian Redlab197ba2009-02-09 18:23:29 +0000834Parser::OwningExprResult Parser::ParseSimpleAsm(SourceLocation *EndLoc) {
Chris Lattner00073222007-10-09 17:23:58 +0000835 assert(Tok.is(tok::kw_asm) && "Not an asm!");
Anders Carlssondfab6cb2008-02-08 00:33:21 +0000836 SourceLocation Loc = ConsumeToken();
Mike Stumpa6f01772008-06-19 19:28:49 +0000837
Chris Lattner00073222007-10-09 17:23:58 +0000838 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000839 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Sebastian Redl61364dd2008-12-11 19:30:53 +0000840 return ExprError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 }
Mike Stumpa6f01772008-06-19 19:28:49 +0000842
Sebastian Redlab197ba2009-02-09 18:23:29 +0000843 Loc = ConsumeParen();
Mike Stumpa6f01772008-06-19 19:28:49 +0000844
Sebastian Redleffa8d12008-12-10 00:02:53 +0000845 OwningExprResult Result(ParseAsmStringLiteral());
Mike Stumpa6f01772008-06-19 19:28:49 +0000846
Sebastian Redlab197ba2009-02-09 18:23:29 +0000847 if (Result.isInvalid()) {
848 SkipUntil(tok::r_paren, true, true);
849 if (EndLoc)
850 *EndLoc = Tok.getLocation();
851 ConsumeAnyToken();
852 } else {
853 Loc = MatchRHSPunctuation(tok::r_paren, Loc);
854 if (EndLoc)
855 *EndLoc = Loc;
856 }
Mike Stumpa6f01772008-06-19 19:28:49 +0000857
Sebastian Redleffa8d12008-12-10 00:02:53 +0000858 return move(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000859}
860
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000861/// TryAnnotateTypeOrScopeToken - If the current token position is on a
862/// typename (possibly qualified in C++) or a C++ scope specifier not followed
863/// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens
864/// with a single annotation token representing the typename or C++ scope
865/// respectively.
866/// This simplifies handling of C++ scope specifiers and allows efficient
867/// backtracking without the need to re-parse and resolve nested-names and
868/// typenames.
Argyrios Kyrtzidis44802cc2008-11-26 21:51:07 +0000869/// It will mainly be called when we expect to treat identifiers as typenames
870/// (if they are typenames). For example, in C we do not expect identifiers
871/// inside expressions to be treated as typenames so it will not be called
872/// for expressions in C.
873/// The benefit for C/ObjC is that a typename will be annotated and
Steve Naroffb43a50f2009-01-28 19:39:02 +0000874/// Actions.getTypeName will not be needed to be called again (e.g. getTypeName
Argyrios Kyrtzidis44802cc2008-11-26 21:51:07 +0000875/// will not be called twice, once to check whether we have a declaration
876/// specifier, and another one to get the actual type inside
877/// ParseDeclarationSpecifiers).
Chris Lattnera7bc7c82009-01-04 23:23:14 +0000878///
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000879/// This returns true if the token was annotated or an unrecoverable error
880/// occurs.
Mike Stump1eb44332009-09-09 15:08:12 +0000881///
Chris Lattner55a7cef2009-01-05 00:13:00 +0000882/// Note that this routine emits an error if you call it with ::new or ::delete
883/// as the current tokens, so only call it in contexts where these are invalid.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000884bool Parser::TryAnnotateTypeOrScopeToken(bool EnteringContext) {
Mike Stump1eb44332009-09-09 15:08:12 +0000885 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)
John McCallae03cb52009-12-19 00:35:18 +0000886 || Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope)) &&
Chris Lattner7452c6f2009-01-05 01:24:05 +0000887 "Cannot be a type or scope token!");
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Douglas Gregord57959a2009-03-27 23:10:48 +0000889 if (Tok.is(tok::kw_typename)) {
890 // Parse a C++ typename-specifier, e.g., "typename T::type".
891 //
892 // typename-specifier:
893 // 'typename' '::' [opt] nested-name-specifier identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000894 // 'typename' '::' [opt] nested-name-specifier template [opt]
Douglas Gregor17343172009-04-01 00:28:59 +0000895 // simple-template-id
Douglas Gregord57959a2009-03-27 23:10:48 +0000896 SourceLocation TypenameLoc = ConsumeToken();
897 CXXScopeSpec SS;
Mike Stump1eb44332009-09-09 15:08:12 +0000898 bool HadNestedNameSpecifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000899 = ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregord57959a2009-03-27 23:10:48 +0000900 if (!HadNestedNameSpecifier) {
901 Diag(Tok.getLocation(), diag::err_expected_qualified_after_typename);
902 return false;
903 }
904
905 TypeResult Ty;
906 if (Tok.is(tok::identifier)) {
907 // FIXME: check whether the next token is '<', first!
Mike Stump1eb44332009-09-09 15:08:12 +0000908 Ty = Actions.ActOnTypenameType(TypenameLoc, SS, *Tok.getIdentifierInfo(),
Douglas Gregord57959a2009-03-27 23:10:48 +0000909 Tok.getLocation());
Douglas Gregor17343172009-04-01 00:28:59 +0000910 } else if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000911 TemplateIdAnnotation *TemplateId
Douglas Gregor17343172009-04-01 00:28:59 +0000912 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
913 if (TemplateId->Kind == TNK_Function_template) {
914 Diag(Tok, diag::err_typename_refers_to_non_type_template)
915 << Tok.getAnnotationRange();
916 return false;
917 }
Douglas Gregord57959a2009-03-27 23:10:48 +0000918
Douglas Gregor31a19b62009-04-01 21:51:26 +0000919 AnnotateTemplateIdTokenAsType(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000920 assert(Tok.is(tok::annot_typename) &&
Douglas Gregor17343172009-04-01 00:28:59 +0000921 "AnnotateTemplateIdTokenAsType isn't working properly");
Douglas Gregor31a19b62009-04-01 21:51:26 +0000922 if (Tok.getAnnotationValue())
923 Ty = Actions.ActOnTypenameType(TypenameLoc, SS, SourceLocation(),
924 Tok.getAnnotationValue());
925 else
926 Ty = true;
Douglas Gregor17343172009-04-01 00:28:59 +0000927 } else {
928 Diag(Tok, diag::err_expected_type_name_after_typename)
929 << SS.getRange();
930 return false;
931 }
932
Douglas Gregor17343172009-04-01 00:28:59 +0000933 Tok.setKind(tok::annot_typename);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000934 Tok.setAnnotationValue(Ty.isInvalid()? 0 : Ty.get());
Douglas Gregor17343172009-04-01 00:28:59 +0000935 Tok.setAnnotationEndLoc(Tok.getLocation());
936 Tok.setLocation(TypenameLoc);
937 PP.AnnotateCachedTokens(Tok);
938 return true;
Douglas Gregord57959a2009-03-27 23:10:48 +0000939 }
940
John McCallae03cb52009-12-19 00:35:18 +0000941 // Remembers whether the token was originally a scope annotation.
942 bool wasScopeAnnotation = Tok.is(tok::annot_cxxscope);
943
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000944 CXXScopeSpec SS;
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +0000945 if (getLang().CPlusPlus)
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000946 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, EnteringContext);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000947
948 if (Tok.is(tok::identifier)) {
Chris Lattner608d1fc2009-01-05 01:49:50 +0000949 // Determine whether the identifier is a type name.
Mike Stump1eb44332009-09-09 15:08:12 +0000950 if (TypeTy *Ty = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +0000951 Tok.getLocation(), CurScope, &SS)) {
Chris Lattner608d1fc2009-01-05 01:49:50 +0000952 // This is a typename. Replace the current token in-place with an
953 // annotation type token.
Chris Lattnerb31757b2009-01-06 05:06:21 +0000954 Tok.setKind(tok::annot_typename);
Chris Lattner608d1fc2009-01-05 01:49:50 +0000955 Tok.setAnnotationValue(Ty);
956 Tok.setAnnotationEndLoc(Tok.getLocation());
957 if (SS.isNotEmpty()) // it was a C++ qualified type name.
958 Tok.setLocation(SS.getBeginLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Chris Lattner608d1fc2009-01-05 01:49:50 +0000960 // In case the tokens were cached, have Preprocessor replace
961 // them with the annotation token.
962 PP.AnnotateCachedTokens(Tok);
963 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000964 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000965
966 if (!getLang().CPlusPlus) {
Chris Lattner608d1fc2009-01-05 01:49:50 +0000967 // If we're in C, we can't have :: tokens at all (the lexer won't return
968 // them). If the identifier is not a type, then it can't be scope either,
Mike Stump1eb44332009-09-09 15:08:12 +0000969 // just early exit.
Chris Lattner608d1fc2009-01-05 01:49:50 +0000970 return false;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000971 }
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Douglas Gregor39a8de12009-02-25 19:37:18 +0000973 // If this is a template-id, annotate with a template-id or type token.
Douglas Gregor55f6b142009-02-09 18:46:07 +0000974 if (NextToken().is(tok::less)) {
Douglas Gregor7532dc62009-03-30 22:58:21 +0000975 TemplateTy Template;
Douglas Gregor014e88d2009-11-03 23:16:33 +0000976 UnqualifiedId TemplateName;
977 TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000978 if (TemplateNameKind TNK
Douglas Gregor014e88d2009-11-03 23:16:33 +0000979 = Actions.isTemplateName(CurScope, SS, TemplateName,
Mike Stump1eb44332009-09-09 15:08:12 +0000980 /*ObjectType=*/0, EnteringContext,
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000981 Template)) {
982 // Consume the identifier.
983 ConsumeToken();
984 if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName)) {
Chris Lattnerc8e27cc2009-06-26 04:27:47 +0000985 // If an unrecoverable error occurred, we need to return true here,
986 // because the token stream is in a damaged state. We may not return
987 // a valid identifier.
988 return Tok.isNot(tok::identifier);
989 }
Douglas Gregorca1bdd72009-11-04 00:56:37 +0000990 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000991 }
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000992
Douglas Gregor39a8de12009-02-25 19:37:18 +0000993 // The current token, which is either an identifier or a
994 // template-id, is not part of the annotation. Fall through to
995 // push that token back into the stream and complete the C++ scope
996 // specifier annotation.
Mike Stump1eb44332009-09-09 15:08:12 +0000997 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000998
Douglas Gregor39a8de12009-02-25 19:37:18 +0000999 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001000 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001001 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001002 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001003 // A template-id that refers to a type was parsed into a
1004 // template-id annotation in a context where we weren't allowed
1005 // to produce a type annotation token. Update the template-id
1006 // annotation token to a type annotation token now.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001007 AnnotateTemplateIdTokenAsType(&SS);
1008 return true;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001009 }
1010 }
Douglas Gregord6fb7ef2008-12-18 19:37:40 +00001011
Chris Lattner6ec76d42009-01-04 22:32:19 +00001012 if (SS.isEmpty())
Eli Friedman3c9028a2009-06-27 08:17:02 +00001013 return Tok.isNot(tok::identifier) && Tok.isNot(tok::coloncolon);
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Chris Lattner6ec76d42009-01-04 22:32:19 +00001015 // A C++ scope specifier that isn't followed by a typename.
1016 // Push the current token back into the token stream (or revert it if it is
1017 // cached) and use an annotation scope token for current token.
1018 if (PP.isBacktrackEnabled())
1019 PP.RevertCachedTokens(1);
1020 else
1021 PP.EnterToken(Tok);
1022 Tok.setKind(tok::annot_cxxscope);
Douglas Gregor35073692009-03-26 23:56:24 +00001023 Tok.setAnnotationValue(SS.getScopeRep());
Chris Lattner6ec76d42009-01-04 22:32:19 +00001024 Tok.setAnnotationRange(SS.getRange());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001025
John McCallae03cb52009-12-19 00:35:18 +00001026 // In case the tokens were cached, have Preprocessor replace them
1027 // with the annotation token. We don't need to do this if we've
1028 // just reverted back to the state we were in before being called.
1029 if (!wasScopeAnnotation)
1030 PP.AnnotateCachedTokens(Tok);
Chris Lattnera7bc7c82009-01-04 23:23:14 +00001031 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001032}
1033
1034/// TryAnnotateScopeToken - Like TryAnnotateTypeOrScopeToken but only
Douglas Gregor39a8de12009-02-25 19:37:18 +00001035/// annotates C++ scope specifiers and template-ids. This returns
Chris Lattnerc8e27cc2009-06-26 04:27:47 +00001036/// true if the token was annotated or there was an error that could not be
1037/// recovered from.
Mike Stump1eb44332009-09-09 15:08:12 +00001038///
Chris Lattner55a7cef2009-01-05 00:13:00 +00001039/// Note that this routine emits an error if you call it with ::new or ::delete
1040/// as the current tokens, so only call it in contexts where these are invalid.
Douglas Gregor495c35d2009-08-25 22:51:20 +00001041bool Parser::TryAnnotateCXXScopeToken(bool EnteringContext) {
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +00001042 assert(getLang().CPlusPlus &&
Chris Lattner6ec76d42009-01-04 22:32:19 +00001043 "Call sites of this function should be guarded by checking for C++");
Chris Lattner7452c6f2009-01-05 01:24:05 +00001044 assert((Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
1045 "Cannot be a type or scope token!");
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001046
Argyrios Kyrtzidis4bdd91c2008-11-26 21:41:52 +00001047 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001048 if (!ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, EnteringContext))
Chris Lattnerbd87c0b2009-12-07 00:48:47 +00001049 // If the token left behind is not an identifier, we either had an error or
1050 // successfully turned it into an annotation token.
1051 return Tok.isNot(tok::identifier);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001052
Chris Lattner6ec76d42009-01-04 22:32:19 +00001053 // Push the current token back into the token stream (or revert it if it is
1054 // cached) and use an annotation scope token for current token.
1055 if (PP.isBacktrackEnabled())
1056 PP.RevertCachedTokens(1);
1057 else
1058 PP.EnterToken(Tok);
1059 Tok.setKind(tok::annot_cxxscope);
Douglas Gregor35073692009-03-26 23:56:24 +00001060 Tok.setAnnotationValue(SS.getScopeRep());
Chris Lattner6ec76d42009-01-04 22:32:19 +00001061 Tok.setAnnotationRange(SS.getRange());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001062
Chris Lattner6ec76d42009-01-04 22:32:19 +00001063 // In case the tokens were cached, have Preprocessor replace them with the
1064 // annotation token.
1065 PP.AnnotateCachedTokens(Tok);
Chris Lattner5e02c472009-01-05 00:07:25 +00001066 return true;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001067}
John McCall6c94a6d2009-11-03 19:33:12 +00001068
1069// Anchor the Parser::FieldCallback vtable to this translation unit.
1070// We use a spurious method instead of the destructor because
1071// destroying FieldCallbacks can actually be slightly
1072// performance-sensitive.
1073void Parser::FieldCallback::_anchor() {
1074}