blob: fc1902ca7fd19400697847e3b98863b43986b2f9 [file] [log] [blame]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001//===--- ParseCXXInlineMethods.cpp - C++ class inline methods parsing------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements parsing for C++ class inline methods.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000015#include "clang/Parse/Parser.h"
John McCall19510852010-08-20 18:27:03 +000016#include "clang/Sema/DeclSpec.h"
17#include "clang/Sema/Scope.h"
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000018using namespace clang;
19
Sebastian Redld3a413d2009-04-26 20:35:05 +000020/// ParseCXXInlineMethodDef - We parsed and verified that the specified
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000021/// Declarator is a well formed C++ inline method definition. Now lex its body
22/// and store its tokens for parsing after the C++ class is complete.
John McCalleee1d542011-02-14 07:13:47 +000023Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, ParsingDeclarator &D,
Nico Weber48673472011-01-28 06:07:34 +000024 const ParsedTemplateInfo &TemplateInfo,
25 const VirtSpecifiers& VS) {
Abramo Bagnara075f8f12010-12-10 16:29:40 +000026 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
Sebastian Redld3a413d2009-04-26 20:35:05 +000027 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) &&
28 "Current token not a '{', ':' or 'try'!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000029
John McCallf312b1e2010-08-26 23:41:50 +000030 MultiTemplateParamsArg TemplateParams(Actions,
Sean Hunt4cd84942010-04-14 23:07:37 +000031 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
32 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
33
John McCalld226f652010-08-21 09:40:31 +000034 Decl *FnD;
John McCall67d1a672009-08-06 02:15:43 +000035 if (D.getDeclSpec().isFriendSpecified())
Douglas Gregor37b372b2009-08-20 22:52:58 +000036 // FIXME: Friend templates
Douglas Gregor23c94db2010-07-02 17:43:08 +000037 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, true,
Sean Hunt4cd84942010-04-14 23:07:37 +000038 move(TemplateParams));
Nico Weber48673472011-01-28 06:07:34 +000039 else { // FIXME: pass template information through
Douglas Gregor23c94db2010-07-02 17:43:08 +000040 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Anders Carlsson69a87352011-01-20 03:57:25 +000041 move(TemplateParams), 0,
Nico Weber48673472011-01-28 06:07:34 +000042 VS, 0, /*IsDefinition*/true);
43 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000044
Eli Friedmand33133c2009-07-22 21:45:50 +000045 HandleMemberFunctionDefaultArgs(D, FnD);
46
John McCalleee1d542011-02-14 07:13:47 +000047 D.complete(FnD);
48
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000049 // Consume the tokens and store them for later parsing.
50
Douglas Gregord54eb442010-10-12 16:25:54 +000051 LexedMethod* LM = new LexedMethod(this, FnD);
52 getCurrentClass().LateParsedDeclarations.push_back(LM);
53 LM->TemplateScope = getCurScope()->isTemplateParamScope();
54 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000055
Sebastian Redld3a413d2009-04-26 20:35:05 +000056 tok::TokenKind kind = Tok.getKind();
57 // We may have a constructor initializer or function-try-block here.
58 if (kind == tok::colon || kind == tok::kw_try) {
Douglas Gregor7ad83902008-11-05 04:29:56 +000059 // Consume everything up to (and including) the left brace.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +000060 if (!ConsumeAndStoreUntil(tok::l_brace, Toks)) {
Douglas Gregor3f08d182008-11-10 16:59:40 +000061 // We didn't find the left-brace we expected after the
Mike Stump1eb44332009-09-09 15:08:12 +000062 // constructor initializer.
Douglas Gregor3f08d182008-11-10 16:59:40 +000063 if (Tok.is(tok::semi)) {
64 // We found a semicolon; complain, consume the semicolon, and
65 // don't try to parse this method later.
66 Diag(Tok.getLocation(), diag::err_expected_lbrace);
67 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +000068 delete getCurrentClass().LateParsedDeclarations.back();
69 getCurrentClass().LateParsedDeclarations.pop_back();
Douglas Gregor3f08d182008-11-10 16:59:40 +000070 return FnD;
71 }
72 }
73
Douglas Gregor7ad83902008-11-05 04:29:56 +000074 } else {
Mike Stump1eb44332009-09-09 15:08:12 +000075 // Begin by storing the '{' token.
Douglas Gregor7ad83902008-11-05 04:29:56 +000076 Toks.push_back(Tok);
77 ConsumeBrace();
78 }
79 // Consume everything up to (and including) the matching right brace.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +000080 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000081
Sebastian Redld3a413d2009-04-26 20:35:05 +000082 // If we're in a function-try-block, we need to store all the catch blocks.
83 if (kind == tok::kw_try) {
84 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +000085 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
86 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redld3a413d2009-04-26 20:35:05 +000087 }
88 }
89
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000090 return FnD;
91}
92
Douglas Gregord54eb442010-10-12 16:25:54 +000093Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
94void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
95void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
96
97Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
98 : Self(P), Class(C) {}
99
100Parser::LateParsedClass::~LateParsedClass() {
101 Self->DeallocateParsedClasses(Class);
102}
103
104void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
105 Self->ParseLexedMethodDeclarations(*Class);
106}
107
108void Parser::LateParsedClass::ParseLexedMethodDefs() {
109 Self->ParseLexedMethodDefs(*Class);
110}
111
112void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
113 Self->ParseLexedMethodDeclaration(*this);
114}
115
116void Parser::LexedMethod::ParseLexedMethodDefs() {
117 Self->ParseLexedMethodDef(*this);
118}
119
Douglas Gregor72b505b2008-12-16 21:30:33 +0000120/// ParseLexedMethodDeclarations - We finished parsing the member
121/// specification of a top (non-nested) C++ class. Now go over the
122/// stack of method declarations with some parts for which parsing was
123/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +0000124void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
125 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000126 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000127 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000128 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000129
John McCall7a1dc562009-12-19 10:49:29 +0000130 // The current scope is still active if we're the top-level class.
131 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000132 bool HasClassScope = !Class.TopLevelClass;
Sean Hunt4cd84942010-04-14 23:07:37 +0000133 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
134 HasClassScope);
John McCall7a1dc562009-12-19 10:49:29 +0000135 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000136 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000137
Douglas Gregord54eb442010-10-12 16:25:54 +0000138 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
139 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000140 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000141
John McCall7a1dc562009-12-19 10:49:29 +0000142 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000143 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000144}
145
Douglas Gregord54eb442010-10-12 16:25:54 +0000146void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
147 // If this is a member template, introduce the template parameter scope.
148 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
149 if (LM.TemplateScope)
150 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
151
152 // Start the delayed C++ method declaration
153 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
154
155 // Introduce the parameters into scope and parse their default
156 // arguments.
157 ParseScope PrototypeScope(this,
158 Scope::FunctionPrototypeScope|Scope::DeclScope);
159 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
160 // Introduce the parameter into scope.
161 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
162
163 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
164 // Save the current token position.
165 SourceLocation origLoc = Tok.getLocation();
166
167 // Parse the default argument from its saved token stream.
168 Toks->push_back(Tok); // So that the current token doesn't get lost
169 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
170
171 // Consume the previously-pushed token.
172 ConsumeAnyToken();
173
174 // Consume the '='.
175 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
176 SourceLocation EqualLoc = ConsumeToken();
177
178 // The argument isn't actually potentially evaluated unless it is
179 // used.
180 EnterExpressionEvaluationContext Eval(Actions,
181 Sema::PotentiallyEvaluatedIfUsed);
182
183 ExprResult DefArgResult(ParseAssignmentExpression());
184 if (DefArgResult.isInvalid())
185 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
186 else {
187 if (Tok.is(tok::cxx_defaultarg_end))
188 ConsumeToken();
189 else
190 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
191 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
192 DefArgResult.take());
193 }
194
195 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
196 Tok.getLocation()) &&
197 "ParseAssignmentExpression went over the default arg tokens!");
198 // There could be leftover tokens (e.g. because of an error).
199 // Skip through until we reach the original token position.
200 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
201 ConsumeAnyToken();
202
203 delete Toks;
204 LM.DefaultArgs[I].Toks = 0;
205 }
206 }
207 PrototypeScope.Exit();
208
209 // Finish the delayed C++ method declaration.
210 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
211}
212
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000213/// ParseLexedMethodDefs - We finished parsing the member specification of a top
214/// (non-nested) C++ class. Now go over the stack of lexed methods that were
215/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000216void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
217 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000218 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000219 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000220 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000221
222 bool HasClassScope = !Class.TopLevelClass;
223 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
224 HasClassScope);
225
Douglas Gregord54eb442010-10-12 16:25:54 +0000226 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
227 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
228 }
229}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000230
Douglas Gregord54eb442010-10-12 16:25:54 +0000231void Parser::ParseLexedMethodDef(LexedMethod &LM) {
232 // If this is a member template, introduce the template parameter scope.
233 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
234 if (LM.TemplateScope)
235 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Douglas Gregord54eb442010-10-12 16:25:54 +0000237 // Save the current token position.
238 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000239
Douglas Gregord54eb442010-10-12 16:25:54 +0000240 assert(!LM.Toks.empty() && "Empty body!");
241 // Append the current token at the end of the new token stream so that it
242 // doesn't get lost.
243 LM.Toks.push_back(Tok);
244 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000245
Douglas Gregord54eb442010-10-12 16:25:54 +0000246 // Consume the previously pushed token.
247 ConsumeAnyToken();
248 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
249 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000250
Douglas Gregord54eb442010-10-12 16:25:54 +0000251 // Parse the method body. Function body parsing code is similar enough
252 // to be re-used for method bodies as well.
253 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
254 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000255
Douglas Gregord54eb442010-10-12 16:25:54 +0000256 if (Tok.is(tok::kw_try)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000257 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000258 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
259 Tok.getLocation()) &&
260 "ParseFunctionTryBlock went over the cached tokens!");
261 // There could be leftover tokens (e.g. because of an error).
262 // Skip through until we reach the original token position.
263 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
264 ConsumeAnyToken();
265 return;
266 }
267 if (Tok.is(tok::colon)) {
268 ParseConstructorInitializer(LM.D);
269
270 // Error recovery.
271 if (!Tok.is(tok::l_brace)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000272 FnScope.Exit();
Douglas Gregord54eb442010-10-12 16:25:54 +0000273 Actions.ActOnFinishFunctionBody(LM.D, 0);
274 return;
275 }
276 } else
277 Actions.ActOnDefaultCtorInitializers(LM.D);
278
Douglas Gregorc9977d02011-03-16 17:05:57 +0000279 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000280
281 if (Tok.getLocation() != origLoc) {
282 // Due to parsing error, we either went over the cached tokens or
283 // there are still cached tokens left. If it's the latter case skip the
284 // leftover tokens.
285 // Since this is an uncommon situation that should be avoided, use the
286 // expensive isBeforeInTranslationUnit call.
287 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
288 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000289 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000290 ConsumeAnyToken();
John McCalld6ca8da2010-04-10 07:37:23 +0000291
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000292 }
293}
294
295/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000296/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000297/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000298/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000299/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000300/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000301bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
302 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000303 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000304 // We always want this function to consume at least one token if the first
305 // token isn't T and if not at EOF.
306 bool isFirstTokenConsumed = true;
307 while (1) {
308 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000309 if (Tok.is(T1) || Tok.is(T2)) {
310 if (ConsumeFinalToken) {
311 Toks.push_back(Tok);
312 ConsumeAnyToken();
313 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000314 return true;
315 }
316
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000317 switch (Tok.getKind()) {
318 case tok::eof:
319 // Ran out of tokens.
320 return false;
321
322 case tok::l_paren:
323 // Recursively consume properly-nested parens.
324 Toks.push_back(Tok);
325 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000326 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000327 break;
328 case tok::l_square:
329 // Recursively consume properly-nested square brackets.
330 Toks.push_back(Tok);
331 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000332 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000333 break;
334 case tok::l_brace:
335 // Recursively consume properly-nested braces.
336 Toks.push_back(Tok);
337 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000338 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000339 break;
340
341 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
342 // Since the user wasn't looking for this token (if they were, it would
343 // already be handled), this isn't balanced. If there is a LHS token at a
344 // higher level, we will assume that this matches the unbalanced token
345 // and return it. Otherwise, this is a spurious RHS token, which we skip.
346 case tok::r_paren:
347 if (ParenCount && !isFirstTokenConsumed)
348 return false; // Matches something.
349 Toks.push_back(Tok);
350 ConsumeParen();
351 break;
352 case tok::r_square:
353 if (BracketCount && !isFirstTokenConsumed)
354 return false; // Matches something.
355 Toks.push_back(Tok);
356 ConsumeBracket();
357 break;
358 case tok::r_brace:
359 if (BraceCount && !isFirstTokenConsumed)
360 return false; // Matches something.
361 Toks.push_back(Tok);
362 ConsumeBrace();
363 break;
364
365 case tok::string_literal:
366 case tok::wide_string_literal:
367 Toks.push_back(Tok);
368 ConsumeStringToken();
369 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000370 case tok::semi:
371 if (StopAtSemi)
372 return false;
373 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000374 default:
375 // consume this token.
376 Toks.push_back(Tok);
377 ConsumeToken();
378 break;
379 }
380 isFirstTokenConsumed = false;
381 }
382}