blob: bc95049ec5bf981836506cf0ea89849f95aa9430 [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"
16#include "clang/Parse/DeclSpec.h"
17#include "clang/Parse/Scope.h"
18using 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.
Chris Lattnerb28317a2009-03-28 19:18:32 +000023Parser::DeclPtrTy
Douglas Gregor37b372b2009-08-20 22:52:58 +000024Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, Declarator &D,
25 const ParsedTemplateInfo &TemplateInfo) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000026 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
27 "This isn't a function declarator!");
Sebastian Redld3a413d2009-04-26 20:35:05 +000028 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) &&
29 "Current token not a '{', ':' or 'try'!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000030
Douglas Gregor37b372b2009-08-20 22:52:58 +000031 Action::MultiTemplateParamsArg TemplateParams(Actions,
Sean Hunt4cd84942010-04-14 23:07:37 +000032 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
33 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
34
John McCall67d1a672009-08-06 02:15:43 +000035 DeclPtrTy FnD;
36 if (D.getDeclSpec().isFriendSpecified())
Douglas Gregor37b372b2009-08-20 22:52:58 +000037 // FIXME: Friend templates
Sean Hunt4cd84942010-04-14 23:07:37 +000038 FnD = Actions.ActOnFriendFunctionDecl(CurScope, D, true,
39 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +000040 else // FIXME: pass template information through
Mike Stump1eb44332009-09-09 15:08:12 +000041 FnD = Actions.ActOnCXXMemberDeclarator(CurScope, AS, D,
Sebastian Redld1a78462009-11-24 23:38:44 +000042 move(TemplateParams), 0, 0,
43 /*IsDefinition*/true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000044
Eli Friedmand33133c2009-07-22 21:45:50 +000045 HandleMemberFunctionDefaultArgs(D, FnD);
46
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000047 // Consume the tokens and store them for later parsing.
48
Douglas Gregor6569d682009-05-27 23:11:45 +000049 getCurrentClass().MethodDefs.push_back(LexedMethod(FnD));
Mike Stump1eb44332009-09-09 15:08:12 +000050 getCurrentClass().MethodDefs.back().TemplateScope
Douglas Gregord83d0402009-08-22 00:34:47 +000051 = CurScope->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +000052 CachedTokens &Toks = getCurrentClass().MethodDefs.back().Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000053
Sebastian Redld3a413d2009-04-26 20:35:05 +000054 tok::TokenKind kind = Tok.getKind();
55 // We may have a constructor initializer or function-try-block here.
56 if (kind == tok::colon || kind == tok::kw_try) {
Douglas Gregor7ad83902008-11-05 04:29:56 +000057 // Consume everything up to (and including) the left brace.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +000058 if (!ConsumeAndStoreUntil(tok::l_brace, Toks)) {
Douglas Gregor3f08d182008-11-10 16:59:40 +000059 // We didn't find the left-brace we expected after the
Mike Stump1eb44332009-09-09 15:08:12 +000060 // constructor initializer.
Douglas Gregor3f08d182008-11-10 16:59:40 +000061 if (Tok.is(tok::semi)) {
62 // We found a semicolon; complain, consume the semicolon, and
63 // don't try to parse this method later.
64 Diag(Tok.getLocation(), diag::err_expected_lbrace);
65 ConsumeAnyToken();
Douglas Gregor6569d682009-05-27 23:11:45 +000066 getCurrentClass().MethodDefs.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 Gregor72b505b2008-12-16 21:30:33 +000090/// ParseLexedMethodDeclarations - We finished parsing the member
91/// specification of a top (non-nested) C++ class. Now go over the
92/// stack of method declarations with some parts for which parsing was
93/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +000094void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
95 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
96 ParseScope TemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
97 if (HasTemplateScope)
98 Actions.ActOnReenterTemplateScope(CurScope, Class.TagOrTemplate);
99
John McCall7a1dc562009-12-19 10:49:29 +0000100 // The current scope is still active if we're the top-level class.
101 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000102 bool HasClassScope = !Class.TopLevelClass;
Sean Hunt4cd84942010-04-14 23:07:37 +0000103 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
104 HasClassScope);
John McCall7a1dc562009-12-19 10:49:29 +0000105 if (HasClassScope)
106 Actions.ActOnStartDelayedMemberDeclarations(CurScope, Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000107
108 for (; !Class.MethodDecls.empty(); Class.MethodDecls.pop_front()) {
109 LateParsedMethodDeclaration &LM = Class.MethodDecls.front();
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Douglas Gregord83d0402009-08-22 00:34:47 +0000111 // If this is a member template, introduce the template parameter scope.
112 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
113 if (LM.TemplateScope)
114 Actions.ActOnReenterTemplateScope(CurScope, LM.Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Douglas Gregor72b505b2008-12-16 21:30:33 +0000116 // Start the delayed C++ method declaration
117 Actions.ActOnStartDelayedCXXMethodDeclaration(CurScope, LM.Method);
118
119 // Introduce the parameters into scope and parse their default
120 // arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000121 ParseScope PrototypeScope(this,
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000122 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000123 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
124 // Introduce the parameter into scope.
125 Actions.ActOnDelayedCXXMethodParameter(CurScope, LM.DefaultArgs[I].Param);
126
127 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
Argyrios Kyrtzidis7fd3a642010-03-30 22:14:32 +0000128 // Save the current token position.
129 SourceLocation origLoc = Tok.getLocation();
130
Douglas Gregor72b505b2008-12-16 21:30:33 +0000131 // Parse the default argument from its saved token stream.
132 Toks->push_back(Tok); // So that the current token doesn't get lost
133 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
134
135 // Consume the previously-pushed token.
136 ConsumeAnyToken();
137
138 // Consume the '='.
139 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
140 SourceLocation EqualLoc = ConsumeToken();
141
142 OwningExprResult DefArgResult(ParseAssignmentExpression());
143 if (DefArgResult.isInvalid())
144 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
145 else
146 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000147 move(DefArgResult));
Argyrios Kyrtzidis7fd3a642010-03-30 22:14:32 +0000148
149 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
150 Tok.getLocation()) &&
151 "ParseAssignmentExpression went over the default arg tokens!");
152 // There could be leftover tokens (e.g. because of an error).
153 // Skip through until we reach the original token position.
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000154 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7fd3a642010-03-30 22:14:32 +0000155 ConsumeAnyToken();
156
Douglas Gregor72b505b2008-12-16 21:30:33 +0000157 delete Toks;
158 LM.DefaultArgs[I].Toks = 0;
159 }
160 }
161 PrototypeScope.Exit();
162
163 // Finish the delayed C++ method declaration.
164 Actions.ActOnFinishDelayedCXXMethodDeclaration(CurScope, LM.Method);
165 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000166
167 for (unsigned I = 0, N = Class.NestedClasses.size(); I != N; ++I)
168 ParseLexedMethodDeclarations(*Class.NestedClasses[I]);
John McCall7a1dc562009-12-19 10:49:29 +0000169
170 if (HasClassScope)
171 Actions.ActOnFinishDelayedMemberDeclarations(CurScope, Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000172}
173
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000174/// ParseLexedMethodDefs - We finished parsing the member specification of a top
175/// (non-nested) C++ class. Now go over the stack of lexed methods that were
176/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000177void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
178 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
179 ParseScope TemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
180 if (HasTemplateScope)
181 Actions.ActOnReenterTemplateScope(CurScope, Class.TagOrTemplate);
182
183 bool HasClassScope = !Class.TopLevelClass;
184 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
185 HasClassScope);
186
187 for (; !Class.MethodDefs.empty(); Class.MethodDefs.pop_front()) {
188 LexedMethod &LM = Class.MethodDefs.front();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000189
Douglas Gregord83d0402009-08-22 00:34:47 +0000190 // If this is a member template, introduce the template parameter scope.
191 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
192 if (LM.TemplateScope)
193 Actions.ActOnReenterTemplateScope(CurScope, LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000195 // Save the current token position.
196 SourceLocation origLoc = Tok.getLocation();
197
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000198 assert(!LM.Toks.empty() && "Empty body!");
199 // Append the current token at the end of the new token stream so that it
200 // doesn't get lost.
201 LM.Toks.push_back(Tok);
Douglas Gregorefd5bda2009-08-24 11:57:43 +0000202 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000203
204 // Consume the previously pushed token.
205 ConsumeAnyToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000206 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
207 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000208
209 // Parse the method body. Function body parsing code is similar enough
210 // to be re-used for method bodies as well.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000211 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000212 Actions.ActOnStartOfFunctionDef(CurScope, LM.D);
213
Sebastian Redld3a413d2009-04-26 20:35:05 +0000214 if (Tok.is(tok::kw_try)) {
215 ParseFunctionTryBlock(LM.D);
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000216 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
217 Tok.getLocation()) &&
218 "ParseFunctionTryBlock went over the cached tokens!");
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000219 // There could be leftover tokens (e.g. because of an error).
220 // Skip through until we reach the original token position.
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000221 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000222 ConsumeAnyToken();
Sebastian Redlde1b60a2009-04-26 21:08:36 +0000223 continue;
Sebastian Redld3a413d2009-04-26 20:35:05 +0000224 }
John McCalld6ca8da2010-04-10 07:37:23 +0000225 if (Tok.is(tok::colon)) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000226 ParseConstructorInitializer(LM.D);
John McCalld6ca8da2010-04-10 07:37:23 +0000227
228 // Error recovery.
229 if (!Tok.is(tok::l_brace)) {
230 Actions.ActOnFinishFunctionBody(LM.D, Action::StmtArg(Actions));
231 continue;
232 }
233 } else
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000234 Actions.ActOnDefaultCtorInitializers(LM.D);
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000235
Chris Lattner40e9bc82009-03-05 00:49:17 +0000236 ParseFunctionStatementBody(LM.D);
Ted Kremenek252485e2010-06-17 00:59:17 +0000237
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000238 if (Tok.getLocation() != origLoc) {
239 // Due to parsing error, we either went over the cached tokens or
240 // there are still cached tokens left. If it's the latter case skip the
241 // leftover tokens.
242 // Since this is an uncommon situation that should be avoided, use the
243 // expensive isBeforeInTranslationUnit call.
244 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
245 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000246 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000247 ConsumeAnyToken();
248
249 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000250 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000251
252 for (unsigned I = 0, N = Class.NestedClasses.size(); I != N; ++I)
253 ParseLexedMethodDefs(*Class.NestedClasses[I]);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000254}
255
256/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000257/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000258/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000259/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000260/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000261/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000262bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
263 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000264 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000265 // We always want this function to consume at least one token if the first
266 // token isn't T and if not at EOF.
267 bool isFirstTokenConsumed = true;
268 while (1) {
269 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000270 if (Tok.is(T1) || Tok.is(T2)) {
271 if (ConsumeFinalToken) {
272 Toks.push_back(Tok);
273 ConsumeAnyToken();
274 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000275 return true;
276 }
277
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000278 switch (Tok.getKind()) {
279 case tok::eof:
280 // Ran out of tokens.
281 return false;
282
283 case tok::l_paren:
284 // Recursively consume properly-nested parens.
285 Toks.push_back(Tok);
286 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000287 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000288 break;
289 case tok::l_square:
290 // Recursively consume properly-nested square brackets.
291 Toks.push_back(Tok);
292 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000293 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000294 break;
295 case tok::l_brace:
296 // Recursively consume properly-nested braces.
297 Toks.push_back(Tok);
298 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000299 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000300 break;
301
302 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
303 // Since the user wasn't looking for this token (if they were, it would
304 // already be handled), this isn't balanced. If there is a LHS token at a
305 // higher level, we will assume that this matches the unbalanced token
306 // and return it. Otherwise, this is a spurious RHS token, which we skip.
307 case tok::r_paren:
308 if (ParenCount && !isFirstTokenConsumed)
309 return false; // Matches something.
310 Toks.push_back(Tok);
311 ConsumeParen();
312 break;
313 case tok::r_square:
314 if (BracketCount && !isFirstTokenConsumed)
315 return false; // Matches something.
316 Toks.push_back(Tok);
317 ConsumeBracket();
318 break;
319 case tok::r_brace:
320 if (BraceCount && !isFirstTokenConsumed)
321 return false; // Matches something.
322 Toks.push_back(Tok);
323 ConsumeBrace();
324 break;
325
326 case tok::string_literal:
327 case tok::wide_string_literal:
328 Toks.push_back(Tok);
329 ConsumeStringToken();
330 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000331 case tok::semi:
332 if (StopAtSemi)
333 return false;
334 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000335 default:
336 // consume this token.
337 Toks.push_back(Tok);
338 ConsumeToken();
339 break;
340 }
341 isFirstTokenConsumed = false;
342 }
343}