blob: 5b5452402b3bfaa13e0942c6a51c7837bae02bcf [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,
32 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
33 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
John McCall67d1a672009-08-06 02:15:43 +000034 DeclPtrTy FnD;
35 if (D.getDeclSpec().isFriendSpecified())
Douglas Gregor37b372b2009-08-20 22:52:58 +000036 // FIXME: Friend templates
John McCallbbbcdd92009-09-11 21:02:39 +000037 FnD = Actions.ActOnFriendFunctionDecl(CurScope, D, true, move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +000038 else // FIXME: pass template information through
Mike Stump1eb44332009-09-09 15:08:12 +000039 FnD = Actions.ActOnCXXMemberDeclarator(CurScope, 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 Gregor6569d682009-05-27 23:11:45 +000047 getCurrentClass().MethodDefs.push_back(LexedMethod(FnD));
Mike Stump1eb44332009-09-09 15:08:12 +000048 getCurrentClass().MethodDefs.back().TemplateScope
Douglas Gregord83d0402009-08-22 00:34:47 +000049 = CurScope->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +000050 CachedTokens &Toks = getCurrentClass().MethodDefs.back().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.
Douglas Gregor72b505b2008-12-16 21:30:33 +000056 if (!ConsumeAndStoreUntil(tok::l_brace, tok::unknown, Toks, tok::semi)) {
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 Gregor6569d682009-05-27 23:11:45 +000064 getCurrentClass().MethodDefs.pop_back();
Douglas Gregor3f08d182008-11-10 16:59:40 +000065 return FnD;
66 }
67 }
68
Douglas Gregor7ad83902008-11-05 04:29:56 +000069 } else {
Mike Stump1eb44332009-09-09 15:08:12 +000070 // Begin by storing the '{' token.
Douglas Gregor7ad83902008-11-05 04:29:56 +000071 Toks.push_back(Tok);
72 ConsumeBrace();
73 }
74 // Consume everything up to (and including) the matching right brace.
Douglas Gregor72b505b2008-12-16 21:30:33 +000075 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000076
Sebastian Redld3a413d2009-04-26 20:35:05 +000077 // If we're in a function-try-block, we need to store all the catch blocks.
78 if (kind == tok::kw_try) {
79 while (Tok.is(tok::kw_catch)) {
80 ConsumeAndStoreUntil(tok::l_brace, tok::unknown, Toks);
81 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
82 }
83 }
84
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000085 return FnD;
86}
87
Douglas Gregor72b505b2008-12-16 21:30:33 +000088/// ParseLexedMethodDeclarations - We finished parsing the member
89/// specification of a top (non-nested) C++ class. Now go over the
90/// stack of method declarations with some parts for which parsing was
91/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +000092void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
93 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
94 ParseScope TemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
95 if (HasTemplateScope)
96 Actions.ActOnReenterTemplateScope(CurScope, Class.TagOrTemplate);
97
John McCall7a1dc562009-12-19 10:49:29 +000098 // The current scope is still active if we're the top-level class.
99 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000100 bool HasClassScope = !Class.TopLevelClass;
John McCall7a1dc562009-12-19 10:49:29 +0000101 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, HasClassScope);
102 if (HasClassScope)
103 Actions.ActOnStartDelayedMemberDeclarations(CurScope, Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000104
105 for (; !Class.MethodDecls.empty(); Class.MethodDecls.pop_front()) {
106 LateParsedMethodDeclaration &LM = Class.MethodDecls.front();
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Douglas Gregord83d0402009-08-22 00:34:47 +0000108 // If this is a member template, introduce the template parameter scope.
109 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
110 if (LM.TemplateScope)
111 Actions.ActOnReenterTemplateScope(CurScope, LM.Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Douglas Gregor72b505b2008-12-16 21:30:33 +0000113 // Start the delayed C++ method declaration
114 Actions.ActOnStartDelayedCXXMethodDeclaration(CurScope, LM.Method);
115
116 // Introduce the parameters into scope and parse their default
117 // arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000118 ParseScope PrototypeScope(this,
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000119 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000120 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
121 // Introduce the parameter into scope.
122 Actions.ActOnDelayedCXXMethodParameter(CurScope, LM.DefaultArgs[I].Param);
123
124 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
Argyrios Kyrtzidis7fd3a642010-03-30 22:14:32 +0000125 // Save the current token position.
126 SourceLocation origLoc = Tok.getLocation();
127
Douglas Gregor72b505b2008-12-16 21:30:33 +0000128 // Parse the default argument from its saved token stream.
129 Toks->push_back(Tok); // So that the current token doesn't get lost
130 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
131
132 // Consume the previously-pushed token.
133 ConsumeAnyToken();
134
135 // Consume the '='.
136 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
137 SourceLocation EqualLoc = ConsumeToken();
138
139 OwningExprResult DefArgResult(ParseAssignmentExpression());
140 if (DefArgResult.isInvalid())
141 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
142 else
143 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000144 move(DefArgResult));
Argyrios Kyrtzidis7fd3a642010-03-30 22:14:32 +0000145
146 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
147 Tok.getLocation()) &&
148 "ParseAssignmentExpression went over the default arg tokens!");
149 // There could be leftover tokens (e.g. because of an error).
150 // Skip through until we reach the original token position.
151 while (Tok.getLocation() != origLoc)
152 ConsumeAnyToken();
153
Douglas Gregor72b505b2008-12-16 21:30:33 +0000154 delete Toks;
155 LM.DefaultArgs[I].Toks = 0;
156 }
157 }
158 PrototypeScope.Exit();
159
160 // Finish the delayed C++ method declaration.
161 Actions.ActOnFinishDelayedCXXMethodDeclaration(CurScope, LM.Method);
162 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000163
164 for (unsigned I = 0, N = Class.NestedClasses.size(); I != N; ++I)
165 ParseLexedMethodDeclarations(*Class.NestedClasses[I]);
John McCall7a1dc562009-12-19 10:49:29 +0000166
167 if (HasClassScope)
168 Actions.ActOnFinishDelayedMemberDeclarations(CurScope, Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000169}
170
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000171/// ParseLexedMethodDefs - We finished parsing the member specification of a top
172/// (non-nested) C++ class. Now go over the stack of lexed methods that were
173/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000174void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
175 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
176 ParseScope TemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
177 if (HasTemplateScope)
178 Actions.ActOnReenterTemplateScope(CurScope, Class.TagOrTemplate);
179
180 bool HasClassScope = !Class.TopLevelClass;
181 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
182 HasClassScope);
183
184 for (; !Class.MethodDefs.empty(); Class.MethodDefs.pop_front()) {
185 LexedMethod &LM = Class.MethodDefs.front();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000186
Douglas Gregord83d0402009-08-22 00:34:47 +0000187 // If this is a member template, introduce the template parameter scope.
188 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
189 if (LM.TemplateScope)
190 Actions.ActOnReenterTemplateScope(CurScope, LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000192 // Save the current token position.
193 SourceLocation origLoc = Tok.getLocation();
194
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000195 assert(!LM.Toks.empty() && "Empty body!");
196 // Append the current token at the end of the new token stream so that it
197 // doesn't get lost.
198 LM.Toks.push_back(Tok);
Douglas Gregorefd5bda2009-08-24 11:57:43 +0000199 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000200
201 // Consume the previously pushed token.
202 ConsumeAnyToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000203 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
204 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000205
206 // Parse the method body. Function body parsing code is similar enough
207 // to be re-used for method bodies as well.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000208 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000209 Actions.ActOnStartOfFunctionDef(CurScope, LM.D);
210
Sebastian Redld3a413d2009-04-26 20:35:05 +0000211 if (Tok.is(tok::kw_try)) {
212 ParseFunctionTryBlock(LM.D);
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000213 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
214 Tok.getLocation()) &&
215 "ParseFunctionTryBlock went over the cached tokens!");
216 assert(Tok.getLocation() == origLoc &&
217 "ParseFunctionTryBlock left tokens in the token stream!");
Sebastian Redlde1b60a2009-04-26 21:08:36 +0000218 continue;
Sebastian Redld3a413d2009-04-26 20:35:05 +0000219 }
John McCalld6ca8da2010-04-10 07:37:23 +0000220 if (Tok.is(tok::colon)) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000221 ParseConstructorInitializer(LM.D);
John McCalld6ca8da2010-04-10 07:37:23 +0000222
223 // Error recovery.
224 if (!Tok.is(tok::l_brace)) {
225 Actions.ActOnFinishFunctionBody(LM.D, Action::StmtArg(Actions));
226 continue;
227 }
228 } else
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000229 Actions.ActOnDefaultCtorInitializers(LM.D);
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000230
Chris Lattner40e9bc82009-03-05 00:49:17 +0000231 ParseFunctionStatementBody(LM.D);
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000232 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
233 Tok.getLocation()) &&
234 "We consumed more than the cached tokens!");
235 assert(Tok.getLocation() == origLoc &&
236 "Tokens were left in the token stream!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000237 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000238
239 for (unsigned I = 0, N = Class.NestedClasses.size(); I != N; ++I)
240 ParseLexedMethodDefs(*Class.NestedClasses[I]);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000241}
242
243/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000244/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000245/// consumed/stored too, if ConsumeFinalToken).
Douglas Gregor3f08d182008-11-10 16:59:40 +0000246/// If EarlyAbortIf is specified, then we will stop early if we find that
247/// token at the top level.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000248/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000249/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000250bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
251 CachedTokens &Toks,
252 tok::TokenKind EarlyAbortIf,
253 bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000254 // We always want this function to consume at least one token if the first
255 // token isn't T and if not at EOF.
256 bool isFirstTokenConsumed = true;
257 while (1) {
258 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000259 if (Tok.is(T1) || Tok.is(T2)) {
260 if (ConsumeFinalToken) {
261 Toks.push_back(Tok);
262 ConsumeAnyToken();
263 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000264 return true;
265 }
266
Douglas Gregor3f08d182008-11-10 16:59:40 +0000267 // If we found the early-abort token, return.
268 if (Tok.is(EarlyAbortIf))
269 return false;
270
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000271 switch (Tok.getKind()) {
272 case tok::eof:
273 // Ran out of tokens.
274 return false;
275
276 case tok::l_paren:
277 // Recursively consume properly-nested parens.
278 Toks.push_back(Tok);
279 ConsumeParen();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000280 ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000281 break;
282 case tok::l_square:
283 // Recursively consume properly-nested square brackets.
284 Toks.push_back(Tok);
285 ConsumeBracket();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000286 ConsumeAndStoreUntil(tok::r_square, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000287 break;
288 case tok::l_brace:
289 // Recursively consume properly-nested braces.
290 Toks.push_back(Tok);
291 ConsumeBrace();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000292 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000293 break;
294
295 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
296 // Since the user wasn't looking for this token (if they were, it would
297 // already be handled), this isn't balanced. If there is a LHS token at a
298 // higher level, we will assume that this matches the unbalanced token
299 // and return it. Otherwise, this is a spurious RHS token, which we skip.
300 case tok::r_paren:
301 if (ParenCount && !isFirstTokenConsumed)
302 return false; // Matches something.
303 Toks.push_back(Tok);
304 ConsumeParen();
305 break;
306 case tok::r_square:
307 if (BracketCount && !isFirstTokenConsumed)
308 return false; // Matches something.
309 Toks.push_back(Tok);
310 ConsumeBracket();
311 break;
312 case tok::r_brace:
313 if (BraceCount && !isFirstTokenConsumed)
314 return false; // Matches something.
315 Toks.push_back(Tok);
316 ConsumeBrace();
317 break;
318
319 case tok::string_literal:
320 case tok::wide_string_literal:
321 Toks.push_back(Tok);
322 ConsumeStringToken();
323 break;
324 default:
325 // consume this token.
326 Toks.push_back(Tok);
327 ConsumeToken();
328 break;
329 }
330 isFirstTokenConsumed = false;
331 }
332}