blob: 2341ad39f073b26f1157fa9a682ecd738dabe2a7 [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 McCalld226f652010-08-21 09:40:31 +000023Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, Declarator &D,
Douglas Gregor37b372b2009-08-20 22:52:58 +000024 const ParsedTemplateInfo &TemplateInfo) {
Abramo Bagnara075f8f12010-12-10 16:29:40 +000025 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
Sebastian Redld3a413d2009-04-26 20:35:05 +000026 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) &&
27 "Current token not a '{', ':' or 'try'!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000028
John McCallf312b1e2010-08-26 23:41:50 +000029 MultiTemplateParamsArg TemplateParams(Actions,
Sean Hunt4cd84942010-04-14 23:07:37 +000030 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
31 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
32
John McCalld226f652010-08-21 09:40:31 +000033 Decl *FnD;
John McCall67d1a672009-08-06 02:15:43 +000034 if (D.getDeclSpec().isFriendSpecified())
Douglas Gregor37b372b2009-08-20 22:52:58 +000035 // FIXME: Friend templates
Douglas Gregor23c94db2010-07-02 17:43:08 +000036 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, true,
Sean Hunt4cd84942010-04-14 23:07:37 +000037 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +000038 else // FIXME: pass template information through
Douglas Gregor23c94db2010-07-02 17:43:08 +000039 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Sebastian Redld1a78462009-11-24 23:38:44 +000040 move(TemplateParams), 0, 0,
41 /*IsDefinition*/true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000042
Eli Friedmand33133c2009-07-22 21:45:50 +000043 HandleMemberFunctionDefaultArgs(D, FnD);
44
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000045 // Consume the tokens and store them for later parsing.
46
Douglas Gregord54eb442010-10-12 16:25:54 +000047 LexedMethod* LM = new LexedMethod(this, FnD);
48 getCurrentClass().LateParsedDeclarations.push_back(LM);
49 LM->TemplateScope = getCurScope()->isTemplateParamScope();
50 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000051
Sebastian Redld3a413d2009-04-26 20:35:05 +000052 tok::TokenKind kind = Tok.getKind();
53 // We may have a constructor initializer or function-try-block here.
54 if (kind == tok::colon || kind == tok::kw_try) {
Douglas Gregor7ad83902008-11-05 04:29:56 +000055 // Consume everything up to (and including) the left brace.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +000056 if (!ConsumeAndStoreUntil(tok::l_brace, Toks)) {
Douglas Gregor3f08d182008-11-10 16:59:40 +000057 // We didn't find the left-brace we expected after the
Mike Stump1eb44332009-09-09 15:08:12 +000058 // constructor initializer.
Douglas Gregor3f08d182008-11-10 16:59:40 +000059 if (Tok.is(tok::semi)) {
60 // We found a semicolon; complain, consume the semicolon, and
61 // don't try to parse this method later.
62 Diag(Tok.getLocation(), diag::err_expected_lbrace);
63 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +000064 delete getCurrentClass().LateParsedDeclarations.back();
65 getCurrentClass().LateParsedDeclarations.pop_back();
Douglas Gregor3f08d182008-11-10 16:59:40 +000066 return FnD;
67 }
68 }
69
Douglas Gregor7ad83902008-11-05 04:29:56 +000070 } else {
Mike Stump1eb44332009-09-09 15:08:12 +000071 // Begin by storing the '{' token.
Douglas Gregor7ad83902008-11-05 04:29:56 +000072 Toks.push_back(Tok);
73 ConsumeBrace();
74 }
75 // Consume everything up to (and including) the matching right brace.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +000076 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000077
Sebastian Redld3a413d2009-04-26 20:35:05 +000078 // If we're in a function-try-block, we need to store all the catch blocks.
79 if (kind == tok::kw_try) {
80 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +000081 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
82 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redld3a413d2009-04-26 20:35:05 +000083 }
84 }
85
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000086 return FnD;
87}
88
Douglas Gregord54eb442010-10-12 16:25:54 +000089Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
90void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
91void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
92
93Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
94 : Self(P), Class(C) {}
95
96Parser::LateParsedClass::~LateParsedClass() {
97 Self->DeallocateParsedClasses(Class);
98}
99
100void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
101 Self->ParseLexedMethodDeclarations(*Class);
102}
103
104void Parser::LateParsedClass::ParseLexedMethodDefs() {
105 Self->ParseLexedMethodDefs(*Class);
106}
107
108void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
109 Self->ParseLexedMethodDeclaration(*this);
110}
111
112void Parser::LexedMethod::ParseLexedMethodDefs() {
113 Self->ParseLexedMethodDef(*this);
114}
115
Douglas Gregor72b505b2008-12-16 21:30:33 +0000116/// ParseLexedMethodDeclarations - We finished parsing the member
117/// specification of a top (non-nested) C++ class. Now go over the
118/// stack of method declarations with some parts for which parsing was
119/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +0000120void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
121 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000122 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000123 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000124 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000125
John McCall7a1dc562009-12-19 10:49:29 +0000126 // The current scope is still active if we're the top-level class.
127 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000128 bool HasClassScope = !Class.TopLevelClass;
Sean Hunt4cd84942010-04-14 23:07:37 +0000129 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
130 HasClassScope);
John McCall7a1dc562009-12-19 10:49:29 +0000131 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000132 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000133
Douglas Gregord54eb442010-10-12 16:25:54 +0000134 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
135 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000136 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000137
John McCall7a1dc562009-12-19 10:49:29 +0000138 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000139 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000140}
141
Douglas Gregord54eb442010-10-12 16:25:54 +0000142void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
143 // If this is a member template, introduce the template parameter scope.
144 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
145 if (LM.TemplateScope)
146 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
147
148 // Start the delayed C++ method declaration
149 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
150
151 // Introduce the parameters into scope and parse their default
152 // arguments.
153 ParseScope PrototypeScope(this,
154 Scope::FunctionPrototypeScope|Scope::DeclScope);
155 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
156 // Introduce the parameter into scope.
157 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
158
159 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
160 // Save the current token position.
161 SourceLocation origLoc = Tok.getLocation();
162
163 // Parse the default argument from its saved token stream.
164 Toks->push_back(Tok); // So that the current token doesn't get lost
165 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
166
167 // Consume the previously-pushed token.
168 ConsumeAnyToken();
169
170 // Consume the '='.
171 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
172 SourceLocation EqualLoc = ConsumeToken();
173
174 // The argument isn't actually potentially evaluated unless it is
175 // used.
176 EnterExpressionEvaluationContext Eval(Actions,
177 Sema::PotentiallyEvaluatedIfUsed);
178
179 ExprResult DefArgResult(ParseAssignmentExpression());
180 if (DefArgResult.isInvalid())
181 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
182 else {
183 if (Tok.is(tok::cxx_defaultarg_end))
184 ConsumeToken();
185 else
186 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
187 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
188 DefArgResult.take());
189 }
190
191 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
192 Tok.getLocation()) &&
193 "ParseAssignmentExpression went over the default arg tokens!");
194 // There could be leftover tokens (e.g. because of an error).
195 // Skip through until we reach the original token position.
196 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
197 ConsumeAnyToken();
198
199 delete Toks;
200 LM.DefaultArgs[I].Toks = 0;
201 }
202 }
203 PrototypeScope.Exit();
204
205 // Finish the delayed C++ method declaration.
206 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
207}
208
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000209/// ParseLexedMethodDefs - We finished parsing the member specification of a top
210/// (non-nested) C++ class. Now go over the stack of lexed methods that were
211/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000212void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
213 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000214 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000215 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000216 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000217
218 bool HasClassScope = !Class.TopLevelClass;
219 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
220 HasClassScope);
221
Douglas Gregord54eb442010-10-12 16:25:54 +0000222 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
223 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
224 }
225}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000226
Douglas Gregord54eb442010-10-12 16:25:54 +0000227void Parser::ParseLexedMethodDef(LexedMethod &LM) {
228 // If this is a member template, introduce the template parameter scope.
229 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
230 if (LM.TemplateScope)
231 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Douglas Gregord54eb442010-10-12 16:25:54 +0000233 // Save the current token position.
234 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000235
Douglas Gregord54eb442010-10-12 16:25:54 +0000236 assert(!LM.Toks.empty() && "Empty body!");
237 // Append the current token at the end of the new token stream so that it
238 // doesn't get lost.
239 LM.Toks.push_back(Tok);
240 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000241
Douglas Gregord54eb442010-10-12 16:25:54 +0000242 // Consume the previously pushed token.
243 ConsumeAnyToken();
244 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
245 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000246
Douglas Gregord54eb442010-10-12 16:25:54 +0000247 // Parse the method body. Function body parsing code is similar enough
248 // to be re-used for method bodies as well.
249 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
250 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000251
Douglas Gregord54eb442010-10-12 16:25:54 +0000252 if (Tok.is(tok::kw_try)) {
253 ParseFunctionTryBlock(LM.D);
254 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
255 Tok.getLocation()) &&
256 "ParseFunctionTryBlock went over the cached tokens!");
257 // There could be leftover tokens (e.g. because of an error).
258 // Skip through until we reach the original token position.
259 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
260 ConsumeAnyToken();
261 return;
262 }
263 if (Tok.is(tok::colon)) {
264 ParseConstructorInitializer(LM.D);
265
266 // Error recovery.
267 if (!Tok.is(tok::l_brace)) {
268 Actions.ActOnFinishFunctionBody(LM.D, 0);
269 return;
270 }
271 } else
272 Actions.ActOnDefaultCtorInitializers(LM.D);
273
274 ParseFunctionStatementBody(LM.D);
275
276 if (Tok.getLocation() != origLoc) {
277 // Due to parsing error, we either went over the cached tokens or
278 // there are still cached tokens left. If it's the latter case skip the
279 // leftover tokens.
280 // Since this is an uncommon situation that should be avoided, use the
281 // expensive isBeforeInTranslationUnit call.
282 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
283 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000284 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000285 ConsumeAnyToken();
John McCalld6ca8da2010-04-10 07:37:23 +0000286
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000287 }
288}
289
290/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000291/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000292/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000293/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000294/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000295/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000296bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
297 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000298 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000299 // We always want this function to consume at least one token if the first
300 // token isn't T and if not at EOF.
301 bool isFirstTokenConsumed = true;
302 while (1) {
303 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000304 if (Tok.is(T1) || Tok.is(T2)) {
305 if (ConsumeFinalToken) {
306 Toks.push_back(Tok);
307 ConsumeAnyToken();
308 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000309 return true;
310 }
311
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000312 switch (Tok.getKind()) {
313 case tok::eof:
314 // Ran out of tokens.
315 return false;
316
317 case tok::l_paren:
318 // Recursively consume properly-nested parens.
319 Toks.push_back(Tok);
320 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000321 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000322 break;
323 case tok::l_square:
324 // Recursively consume properly-nested square brackets.
325 Toks.push_back(Tok);
326 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000327 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000328 break;
329 case tok::l_brace:
330 // Recursively consume properly-nested braces.
331 Toks.push_back(Tok);
332 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000333 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000334 break;
335
336 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
337 // Since the user wasn't looking for this token (if they were, it would
338 // already be handled), this isn't balanced. If there is a LHS token at a
339 // higher level, we will assume that this matches the unbalanced token
340 // and return it. Otherwise, this is a spurious RHS token, which we skip.
341 case tok::r_paren:
342 if (ParenCount && !isFirstTokenConsumed)
343 return false; // Matches something.
344 Toks.push_back(Tok);
345 ConsumeParen();
346 break;
347 case tok::r_square:
348 if (BracketCount && !isFirstTokenConsumed)
349 return false; // Matches something.
350 Toks.push_back(Tok);
351 ConsumeBracket();
352 break;
353 case tok::r_brace:
354 if (BraceCount && !isFirstTokenConsumed)
355 return false; // Matches something.
356 Toks.push_back(Tok);
357 ConsumeBrace();
358 break;
359
360 case tok::string_literal:
361 case tok::wide_string_literal:
362 Toks.push_back(Tok);
363 ConsumeStringToken();
364 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000365 case tok::semi:
366 if (StopAtSemi)
367 return false;
368 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000369 default:
370 // consume this token.
371 Toks.push_back(Tok);
372 ConsumeToken();
373 break;
374 }
375 isFirstTokenConsumed = false;
376 }
377}