blob: 718c71be026e6b0928b9609880e96b48c5d19adf [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) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000025 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
26 "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));
Douglas Gregor37b372b2009-08-20 22:52:58 +000039 else // FIXME: pass template information through
Douglas Gregor23c94db2010-07-02 17:43:08 +000040 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Sebastian Redld1a78462009-11-24 23:38:44 +000041 move(TemplateParams), 0, 0,
42 /*IsDefinition*/true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000043
Eli Friedmand33133c2009-07-22 21:45:50 +000044 HandleMemberFunctionDefaultArgs(D, FnD);
45
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000046 // Consume the tokens and store them for later parsing.
47
Douglas Gregord54eb442010-10-12 16:25:54 +000048 LexedMethod* LM = new LexedMethod(this, FnD);
49 getCurrentClass().LateParsedDeclarations.push_back(LM);
50 LM->TemplateScope = getCurScope()->isTemplateParamScope();
51 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000052
Sebastian Redld3a413d2009-04-26 20:35:05 +000053 tok::TokenKind kind = Tok.getKind();
54 // We may have a constructor initializer or function-try-block here.
55 if (kind == tok::colon || kind == tok::kw_try) {
Douglas Gregor7ad83902008-11-05 04:29:56 +000056 // Consume everything up to (and including) the left brace.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +000057 if (!ConsumeAndStoreUntil(tok::l_brace, Toks)) {
Douglas Gregor3f08d182008-11-10 16:59:40 +000058 // We didn't find the left-brace we expected after the
Mike Stump1eb44332009-09-09 15:08:12 +000059 // constructor initializer.
Douglas Gregor3f08d182008-11-10 16:59:40 +000060 if (Tok.is(tok::semi)) {
61 // We found a semicolon; complain, consume the semicolon, and
62 // don't try to parse this method later.
63 Diag(Tok.getLocation(), diag::err_expected_lbrace);
64 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +000065 delete getCurrentClass().LateParsedDeclarations.back();
66 getCurrentClass().LateParsedDeclarations.pop_back();
Douglas Gregor3f08d182008-11-10 16:59:40 +000067 return FnD;
68 }
69 }
70
Douglas Gregor7ad83902008-11-05 04:29:56 +000071 } else {
Mike Stump1eb44332009-09-09 15:08:12 +000072 // Begin by storing the '{' token.
Douglas Gregor7ad83902008-11-05 04:29:56 +000073 Toks.push_back(Tok);
74 ConsumeBrace();
75 }
76 // Consume everything up to (and including) the matching right brace.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +000077 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000078
Sebastian Redld3a413d2009-04-26 20:35:05 +000079 // If we're in a function-try-block, we need to store all the catch blocks.
80 if (kind == tok::kw_try) {
81 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +000082 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
83 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redld3a413d2009-04-26 20:35:05 +000084 }
85 }
86
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000087 return FnD;
88}
89
Douglas Gregord54eb442010-10-12 16:25:54 +000090Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
91void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
92void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
93
94Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
95 : Self(P), Class(C) {}
96
97Parser::LateParsedClass::~LateParsedClass() {
98 Self->DeallocateParsedClasses(Class);
99}
100
101void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
102 Self->ParseLexedMethodDeclarations(*Class);
103}
104
105void Parser::LateParsedClass::ParseLexedMethodDefs() {
106 Self->ParseLexedMethodDefs(*Class);
107}
108
109void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
110 Self->ParseLexedMethodDeclaration(*this);
111}
112
113void Parser::LexedMethod::ParseLexedMethodDefs() {
114 Self->ParseLexedMethodDef(*this);
115}
116
Douglas Gregor72b505b2008-12-16 21:30:33 +0000117/// ParseLexedMethodDeclarations - We finished parsing the member
118/// specification of a top (non-nested) C++ class. Now go over the
119/// stack of method declarations with some parts for which parsing was
120/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +0000121void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
122 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000123 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000124 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000125 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000126
John McCall7a1dc562009-12-19 10:49:29 +0000127 // The current scope is still active if we're the top-level class.
128 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000129 bool HasClassScope = !Class.TopLevelClass;
Sean Hunt4cd84942010-04-14 23:07:37 +0000130 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
131 HasClassScope);
John McCall7a1dc562009-12-19 10:49:29 +0000132 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000133 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000134
Douglas Gregord54eb442010-10-12 16:25:54 +0000135 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
136 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000137 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000138
John McCall7a1dc562009-12-19 10:49:29 +0000139 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000140 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000141}
142
Douglas Gregord54eb442010-10-12 16:25:54 +0000143void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
144 // If this is a member template, introduce the template parameter scope.
145 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
146 if (LM.TemplateScope)
147 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
148
149 // Start the delayed C++ method declaration
150 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
151
152 // Introduce the parameters into scope and parse their default
153 // arguments.
154 ParseScope PrototypeScope(this,
155 Scope::FunctionPrototypeScope|Scope::DeclScope);
156 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
157 // Introduce the parameter into scope.
158 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
159
160 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
161 // Save the current token position.
162 SourceLocation origLoc = Tok.getLocation();
163
164 // Parse the default argument from its saved token stream.
165 Toks->push_back(Tok); // So that the current token doesn't get lost
166 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
167
168 // Consume the previously-pushed token.
169 ConsumeAnyToken();
170
171 // Consume the '='.
172 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
173 SourceLocation EqualLoc = ConsumeToken();
174
175 // The argument isn't actually potentially evaluated unless it is
176 // used.
177 EnterExpressionEvaluationContext Eval(Actions,
178 Sema::PotentiallyEvaluatedIfUsed);
179
180 ExprResult DefArgResult(ParseAssignmentExpression());
181 if (DefArgResult.isInvalid())
182 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
183 else {
184 if (Tok.is(tok::cxx_defaultarg_end))
185 ConsumeToken();
186 else
187 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
188 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
189 DefArgResult.take());
190 }
191
192 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
193 Tok.getLocation()) &&
194 "ParseAssignmentExpression went over the default arg tokens!");
195 // There could be leftover tokens (e.g. because of an error).
196 // Skip through until we reach the original token position.
197 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
198 ConsumeAnyToken();
199
200 delete Toks;
201 LM.DefaultArgs[I].Toks = 0;
202 }
203 }
204 PrototypeScope.Exit();
205
206 // Finish the delayed C++ method declaration.
207 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
208}
209
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000210/// ParseLexedMethodDefs - We finished parsing the member specification of a top
211/// (non-nested) C++ class. Now go over the stack of lexed methods that were
212/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000213void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
214 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000215 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000216 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000217 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000218
219 bool HasClassScope = !Class.TopLevelClass;
220 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
221 HasClassScope);
222
Douglas Gregord54eb442010-10-12 16:25:54 +0000223 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
224 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
225 }
226}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000227
Douglas Gregord54eb442010-10-12 16:25:54 +0000228void Parser::ParseLexedMethodDef(LexedMethod &LM) {
229 // If this is a member template, introduce the template parameter scope.
230 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
231 if (LM.TemplateScope)
232 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Douglas Gregord54eb442010-10-12 16:25:54 +0000234 // Save the current token position.
235 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000236
Douglas Gregord54eb442010-10-12 16:25:54 +0000237 assert(!LM.Toks.empty() && "Empty body!");
238 // Append the current token at the end of the new token stream so that it
239 // doesn't get lost.
240 LM.Toks.push_back(Tok);
241 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000242
Douglas Gregord54eb442010-10-12 16:25:54 +0000243 // Consume the previously pushed token.
244 ConsumeAnyToken();
245 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
246 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000247
Douglas Gregord54eb442010-10-12 16:25:54 +0000248 // Parse the method body. Function body parsing code is similar enough
249 // to be re-used for method bodies as well.
250 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
251 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000252
Douglas Gregord54eb442010-10-12 16:25:54 +0000253 if (Tok.is(tok::kw_try)) {
254 ParseFunctionTryBlock(LM.D);
255 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
256 Tok.getLocation()) &&
257 "ParseFunctionTryBlock went over the cached tokens!");
258 // There could be leftover tokens (e.g. because of an error).
259 // Skip through until we reach the original token position.
260 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
261 ConsumeAnyToken();
262 return;
263 }
264 if (Tok.is(tok::colon)) {
265 ParseConstructorInitializer(LM.D);
266
267 // Error recovery.
268 if (!Tok.is(tok::l_brace)) {
269 Actions.ActOnFinishFunctionBody(LM.D, 0);
270 return;
271 }
272 } else
273 Actions.ActOnDefaultCtorInitializers(LM.D);
274
275 ParseFunctionStatementBody(LM.D);
276
277 if (Tok.getLocation() != origLoc) {
278 // Due to parsing error, we either went over the cached tokens or
279 // there are still cached tokens left. If it's the latter case skip the
280 // leftover tokens.
281 // Since this is an uncommon situation that should be avoided, use the
282 // expensive isBeforeInTranslationUnit call.
283 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
284 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000285 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000286 ConsumeAnyToken();
John McCalld6ca8da2010-04-10 07:37:23 +0000287
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000288 }
289}
290
291/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000292/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000293/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000294/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000295/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000296/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000297bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
298 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000299 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000300 // We always want this function to consume at least one token if the first
301 // token isn't T and if not at EOF.
302 bool isFirstTokenConsumed = true;
303 while (1) {
304 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000305 if (Tok.is(T1) || Tok.is(T2)) {
306 if (ConsumeFinalToken) {
307 Toks.push_back(Tok);
308 ConsumeAnyToken();
309 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000310 return true;
311 }
312
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000313 switch (Tok.getKind()) {
314 case tok::eof:
315 // Ran out of tokens.
316 return false;
317
318 case tok::l_paren:
319 // Recursively consume properly-nested parens.
320 Toks.push_back(Tok);
321 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000322 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000323 break;
324 case tok::l_square:
325 // Recursively consume properly-nested square brackets.
326 Toks.push_back(Tok);
327 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000328 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000329 break;
330 case tok::l_brace:
331 // Recursively consume properly-nested braces.
332 Toks.push_back(Tok);
333 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000334 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000335 break;
336
337 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
338 // Since the user wasn't looking for this token (if they were, it would
339 // already be handled), this isn't balanced. If there is a LHS token at a
340 // higher level, we will assume that this matches the unbalanced token
341 // and return it. Otherwise, this is a spurious RHS token, which we skip.
342 case tok::r_paren:
343 if (ParenCount && !isFirstTokenConsumed)
344 return false; // Matches something.
345 Toks.push_back(Tok);
346 ConsumeParen();
347 break;
348 case tok::r_square:
349 if (BracketCount && !isFirstTokenConsumed)
350 return false; // Matches something.
351 Toks.push_back(Tok);
352 ConsumeBracket();
353 break;
354 case tok::r_brace:
355 if (BraceCount && !isFirstTokenConsumed)
356 return false; // Matches something.
357 Toks.push_back(Tok);
358 ConsumeBrace();
359 break;
360
361 case tok::string_literal:
362 case tok::wide_string_literal:
363 Toks.push_back(Tok);
364 ConsumeStringToken();
365 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000366 case tok::semi:
367 if (StopAtSemi)
368 return false;
369 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000370 default:
371 // consume this token.
372 Toks.push_back(Tok);
373 ConsumeToken();
374 break;
375 }
376 isFirstTokenConsumed = false;
377 }
378}