blob: 82b7da9f68e5ef5ae546ebf6f848a9fb8292c95d [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 McCall3f9a8a62009-08-11 06:59:38 +000037 FnD = Actions.ActOnFriendDecl(CurScope, &D, /*IsDefinition*/ true);
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,
Douglas Gregor37b372b2009-08-20 22:52:58 +000040 move(TemplateParams), 0, 0);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000041
Eli Friedmand33133c2009-07-22 21:45:50 +000042 HandleMemberFunctionDefaultArgs(D, FnD);
43
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000044 // Consume the tokens and store them for later parsing.
45
Douglas Gregor6569d682009-05-27 23:11:45 +000046 getCurrentClass().MethodDefs.push_back(LexedMethod(FnD));
Mike Stump1eb44332009-09-09 15:08:12 +000047 getCurrentClass().MethodDefs.back().TemplateScope
Douglas Gregord83d0402009-08-22 00:34:47 +000048 = CurScope->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +000049 CachedTokens &Toks = getCurrentClass().MethodDefs.back().Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000050
Sebastian Redld3a413d2009-04-26 20:35:05 +000051 tok::TokenKind kind = Tok.getKind();
52 // We may have a constructor initializer or function-try-block here.
53 if (kind == tok::colon || kind == tok::kw_try) {
Douglas Gregor7ad83902008-11-05 04:29:56 +000054 // Consume everything up to (and including) the left brace.
Douglas Gregor72b505b2008-12-16 21:30:33 +000055 if (!ConsumeAndStoreUntil(tok::l_brace, tok::unknown, Toks, tok::semi)) {
Douglas Gregor3f08d182008-11-10 16:59:40 +000056 // We didn't find the left-brace we expected after the
Mike Stump1eb44332009-09-09 15:08:12 +000057 // constructor initializer.
Douglas Gregor3f08d182008-11-10 16:59:40 +000058 if (Tok.is(tok::semi)) {
59 // We found a semicolon; complain, consume the semicolon, and
60 // don't try to parse this method later.
61 Diag(Tok.getLocation(), diag::err_expected_lbrace);
62 ConsumeAnyToken();
Douglas Gregor6569d682009-05-27 23:11:45 +000063 getCurrentClass().MethodDefs.pop_back();
Douglas Gregor3f08d182008-11-10 16:59:40 +000064 return FnD;
65 }
66 }
67
Douglas Gregor7ad83902008-11-05 04:29:56 +000068 } else {
Mike Stump1eb44332009-09-09 15:08:12 +000069 // Begin by storing the '{' token.
Douglas Gregor7ad83902008-11-05 04:29:56 +000070 Toks.push_back(Tok);
71 ConsumeBrace();
72 }
73 // Consume everything up to (and including) the matching right brace.
Douglas Gregor72b505b2008-12-16 21:30:33 +000074 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000075
Sebastian Redld3a413d2009-04-26 20:35:05 +000076 // If we're in a function-try-block, we need to store all the catch blocks.
77 if (kind == tok::kw_try) {
78 while (Tok.is(tok::kw_catch)) {
79 ConsumeAndStoreUntil(tok::l_brace, tok::unknown, Toks);
80 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
81 }
82 }
83
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000084 return FnD;
85}
86
Douglas Gregor72b505b2008-12-16 21:30:33 +000087/// ParseLexedMethodDeclarations - We finished parsing the member
88/// specification of a top (non-nested) C++ class. Now go over the
89/// stack of method declarations with some parts for which parsing was
90/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +000091void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
92 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
93 ParseScope TemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
94 if (HasTemplateScope)
95 Actions.ActOnReenterTemplateScope(CurScope, Class.TagOrTemplate);
96
97 bool HasClassScope = !Class.TopLevelClass;
98 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
99 HasClassScope);
100
101 for (; !Class.MethodDecls.empty(); Class.MethodDecls.pop_front()) {
102 LateParsedMethodDeclaration &LM = Class.MethodDecls.front();
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Douglas Gregord83d0402009-08-22 00:34:47 +0000104 // If this is a member template, introduce the template parameter scope.
105 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
106 if (LM.TemplateScope)
107 Actions.ActOnReenterTemplateScope(CurScope, LM.Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Douglas Gregor72b505b2008-12-16 21:30:33 +0000109 // Start the delayed C++ method declaration
110 Actions.ActOnStartDelayedCXXMethodDeclaration(CurScope, LM.Method);
111
112 // Introduce the parameters into scope and parse their default
113 // arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000114 ParseScope PrototypeScope(this,
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000115 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000116 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
117 // Introduce the parameter into scope.
118 Actions.ActOnDelayedCXXMethodParameter(CurScope, LM.DefaultArgs[I].Param);
119
120 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
121 // Parse the default argument from its saved token stream.
122 Toks->push_back(Tok); // So that the current token doesn't get lost
123 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
124
125 // Consume the previously-pushed token.
126 ConsumeAnyToken();
127
128 // Consume the '='.
129 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
130 SourceLocation EqualLoc = ConsumeToken();
131
132 OwningExprResult DefArgResult(ParseAssignmentExpression());
133 if (DefArgResult.isInvalid())
134 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
135 else
136 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000137 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +0000138 delete Toks;
139 LM.DefaultArgs[I].Toks = 0;
140 }
141 }
142 PrototypeScope.Exit();
143
144 // Finish the delayed C++ method declaration.
145 Actions.ActOnFinishDelayedCXXMethodDeclaration(CurScope, LM.Method);
146 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000147
148 for (unsigned I = 0, N = Class.NestedClasses.size(); I != N; ++I)
149 ParseLexedMethodDeclarations(*Class.NestedClasses[I]);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000150}
151
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000152/// ParseLexedMethodDefs - We finished parsing the member specification of a top
153/// (non-nested) C++ class. Now go over the stack of lexed methods that were
154/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000155void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
156 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
157 ParseScope TemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
158 if (HasTemplateScope)
159 Actions.ActOnReenterTemplateScope(CurScope, Class.TagOrTemplate);
160
161 bool HasClassScope = !Class.TopLevelClass;
162 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
163 HasClassScope);
164
165 for (; !Class.MethodDefs.empty(); Class.MethodDefs.pop_front()) {
166 LexedMethod &LM = Class.MethodDefs.front();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000167
Douglas Gregord83d0402009-08-22 00:34:47 +0000168 // If this is a member template, introduce the template parameter scope.
169 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
170 if (LM.TemplateScope)
171 Actions.ActOnReenterTemplateScope(CurScope, LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000173 assert(!LM.Toks.empty() && "Empty body!");
174 // Append the current token at the end of the new token stream so that it
175 // doesn't get lost.
176 LM.Toks.push_back(Tok);
Douglas Gregorefd5bda2009-08-24 11:57:43 +0000177 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000178
179 // Consume the previously pushed token.
180 ConsumeAnyToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000181 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
182 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000183
184 // Parse the method body. Function body parsing code is similar enough
185 // to be re-used for method bodies as well.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000186 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000187 Actions.ActOnStartOfFunctionDef(CurScope, LM.D);
188
Sebastian Redld3a413d2009-04-26 20:35:05 +0000189 if (Tok.is(tok::kw_try)) {
190 ParseFunctionTryBlock(LM.D);
Sebastian Redlde1b60a2009-04-26 21:08:36 +0000191 continue;
Sebastian Redld3a413d2009-04-26 20:35:05 +0000192 }
Douglas Gregor7ad83902008-11-05 04:29:56 +0000193 if (Tok.is(tok::colon))
194 ParseConstructorInitializer(LM.D);
Fariborz Jahanian0849d382009-07-14 20:06:22 +0000195 else
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000196 Actions.ActOnDefaultCtorInitializers(LM.D);
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000197
Chris Lattner40e9bc82009-03-05 00:49:17 +0000198 // FIXME: What if ParseConstructorInitializer doesn't leave us with a '{'??
199 ParseFunctionStatementBody(LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000200 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000201
202 for (unsigned I = 0, N = Class.NestedClasses.size(); I != N; ++I)
203 ParseLexedMethodDefs(*Class.NestedClasses[I]);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000204}
205
206/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000207/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000208/// consumed/stored too, if ConsumeFinalToken).
Douglas Gregor3f08d182008-11-10 16:59:40 +0000209/// If EarlyAbortIf is specified, then we will stop early if we find that
210/// token at the top level.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000211/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000212/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000213bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
214 CachedTokens &Toks,
215 tok::TokenKind EarlyAbortIf,
216 bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000217 // We always want this function to consume at least one token if the first
218 // token isn't T and if not at EOF.
219 bool isFirstTokenConsumed = true;
220 while (1) {
221 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000222 if (Tok.is(T1) || Tok.is(T2)) {
223 if (ConsumeFinalToken) {
224 Toks.push_back(Tok);
225 ConsumeAnyToken();
226 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000227 return true;
228 }
229
Douglas Gregor3f08d182008-11-10 16:59:40 +0000230 // If we found the early-abort token, return.
231 if (Tok.is(EarlyAbortIf))
232 return false;
233
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000234 switch (Tok.getKind()) {
235 case tok::eof:
236 // Ran out of tokens.
237 return false;
238
239 case tok::l_paren:
240 // Recursively consume properly-nested parens.
241 Toks.push_back(Tok);
242 ConsumeParen();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000243 ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000244 break;
245 case tok::l_square:
246 // Recursively consume properly-nested square brackets.
247 Toks.push_back(Tok);
248 ConsumeBracket();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000249 ConsumeAndStoreUntil(tok::r_square, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000250 break;
251 case tok::l_brace:
252 // Recursively consume properly-nested braces.
253 Toks.push_back(Tok);
254 ConsumeBrace();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000255 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000256 break;
257
258 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
259 // Since the user wasn't looking for this token (if they were, it would
260 // already be handled), this isn't balanced. If there is a LHS token at a
261 // higher level, we will assume that this matches the unbalanced token
262 // and return it. Otherwise, this is a spurious RHS token, which we skip.
263 case tok::r_paren:
264 if (ParenCount && !isFirstTokenConsumed)
265 return false; // Matches something.
266 Toks.push_back(Tok);
267 ConsumeParen();
268 break;
269 case tok::r_square:
270 if (BracketCount && !isFirstTokenConsumed)
271 return false; // Matches something.
272 Toks.push_back(Tok);
273 ConsumeBracket();
274 break;
275 case tok::r_brace:
276 if (BraceCount && !isFirstTokenConsumed)
277 return false; // Matches something.
278 Toks.push_back(Tok);
279 ConsumeBrace();
280 break;
281
282 case tok::string_literal:
283 case tok::wide_string_literal:
284 Toks.push_back(Tok);
285 ConsumeStringToken();
286 break;
287 default:
288 // consume this token.
289 Toks.push_back(Tok);
290 ConsumeToken();
291 break;
292 }
293 isFirstTokenConsumed = false;
294 }
295}