blob: 52a812d4ba1f48523f0e909fbea8cb8802bcaa25 [file] [log] [blame]
Argiris Kirtzidis9d784332008-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 Lattner545f39e2009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Argiris Kirtzidis9d784332008-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 Redlbc9ef252009-04-26 20:35:05 +000020/// ParseCXXInlineMethodDef - We parsed and verified that the specified
Argiris Kirtzidis9d784332008-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 Lattner5261d0c2009-03-28 19:18:32 +000023Parser::DeclPtrTy
Argiris Kirtzidis9d784332008-06-24 22:12:16 +000024Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, Declarator &D) {
25 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
26 "This isn't a function declarator!");
Sebastian Redlbc9ef252009-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'!");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +000029
John McCall140607b2009-08-06 02:15:43 +000030 DeclPtrTy FnD;
31 if (D.getDeclSpec().isFriendSpecified())
32 FnD = Actions.ActOnFriendDecl(CurScope, &D);
33 else
34 FnD = Actions.ActOnCXXMemberDeclarator(CurScope, AS, D, 0, 0);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +000035
Eli Friedman40035e22009-07-22 21:45:50 +000036 HandleMemberFunctionDefaultArgs(D, FnD);
37
Argiris Kirtzidis9d784332008-06-24 22:12:16 +000038 // Consume the tokens and store them for later parsing.
39
Douglas Gregora376cbd2009-05-27 23:11:45 +000040 getCurrentClass().MethodDefs.push_back(LexedMethod(FnD));
41 CachedTokens &Toks = getCurrentClass().MethodDefs.back().Toks;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +000042
Sebastian Redlbc9ef252009-04-26 20:35:05 +000043 tok::TokenKind kind = Tok.getKind();
44 // We may have a constructor initializer or function-try-block here.
45 if (kind == tok::colon || kind == tok::kw_try) {
Douglas Gregora65e8dd2008-11-05 04:29:56 +000046 // Consume everything up to (and including) the left brace.
Douglas Gregor605de8d2008-12-16 21:30:33 +000047 if (!ConsumeAndStoreUntil(tok::l_brace, tok::unknown, Toks, tok::semi)) {
Douglas Gregord2c14ad2008-11-10 16:59:40 +000048 // We didn't find the left-brace we expected after the
49 // constructor initializer.
50 if (Tok.is(tok::semi)) {
51 // We found a semicolon; complain, consume the semicolon, and
52 // don't try to parse this method later.
53 Diag(Tok.getLocation(), diag::err_expected_lbrace);
54 ConsumeAnyToken();
Douglas Gregora376cbd2009-05-27 23:11:45 +000055 getCurrentClass().MethodDefs.pop_back();
Douglas Gregord2c14ad2008-11-10 16:59:40 +000056 return FnD;
57 }
58 }
59
Douglas Gregora65e8dd2008-11-05 04:29:56 +000060 } else {
61 // Begin by storing the '{' token.
62 Toks.push_back(Tok);
63 ConsumeBrace();
64 }
65 // Consume everything up to (and including) the matching right brace.
Douglas Gregor605de8d2008-12-16 21:30:33 +000066 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +000067
Sebastian Redlbc9ef252009-04-26 20:35:05 +000068 // If we're in a function-try-block, we need to store all the catch blocks.
69 if (kind == tok::kw_try) {
70 while (Tok.is(tok::kw_catch)) {
71 ConsumeAndStoreUntil(tok::l_brace, tok::unknown, Toks);
72 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
73 }
74 }
75
Argiris Kirtzidis9d784332008-06-24 22:12:16 +000076 return FnD;
77}
78
Douglas Gregor605de8d2008-12-16 21:30:33 +000079/// ParseLexedMethodDeclarations - We finished parsing the member
80/// specification of a top (non-nested) C++ class. Now go over the
81/// stack of method declarations with some parts for which parsing was
82/// delayed (such as default arguments) and parse them.
Douglas Gregora376cbd2009-05-27 23:11:45 +000083void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
84 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
85 ParseScope TemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
86 if (HasTemplateScope)
87 Actions.ActOnReenterTemplateScope(CurScope, Class.TagOrTemplate);
88
89 bool HasClassScope = !Class.TopLevelClass;
90 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
91 HasClassScope);
92
93 for (; !Class.MethodDecls.empty(); Class.MethodDecls.pop_front()) {
94 LateParsedMethodDeclaration &LM = Class.MethodDecls.front();
Douglas Gregor605de8d2008-12-16 21:30:33 +000095
Douglas Gregora376cbd2009-05-27 23:11:45 +000096 // FIXME: For member function templates, we'll need to introduce a
97 // scope for the template parameters.
98
Douglas Gregor605de8d2008-12-16 21:30:33 +000099 // Start the delayed C++ method declaration
100 Actions.ActOnStartDelayedCXXMethodDeclaration(CurScope, LM.Method);
101
102 // Introduce the parameters into scope and parse their default
103 // arguments.
Douglas Gregorcab994d2009-01-09 22:42:13 +0000104 ParseScope PrototypeScope(this,
105 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor605de8d2008-12-16 21:30:33 +0000106 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
107 // Introduce the parameter into scope.
108 Actions.ActOnDelayedCXXMethodParameter(CurScope, LM.DefaultArgs[I].Param);
109
110 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
111 // Parse the default argument from its saved token stream.
112 Toks->push_back(Tok); // So that the current token doesn't get lost
113 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
114
115 // Consume the previously-pushed token.
116 ConsumeAnyToken();
117
118 // Consume the '='.
119 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
120 SourceLocation EqualLoc = ConsumeToken();
121
122 OwningExprResult DefArgResult(ParseAssignmentExpression());
123 if (DefArgResult.isInvalid())
124 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
125 else
126 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
Sebastian Redl76bb8ec2009-03-15 17:47:39 +0000127 move(DefArgResult));
Douglas Gregor605de8d2008-12-16 21:30:33 +0000128 delete Toks;
129 LM.DefaultArgs[I].Toks = 0;
130 }
131 }
132 PrototypeScope.Exit();
133
134 // Finish the delayed C++ method declaration.
135 Actions.ActOnFinishDelayedCXXMethodDeclaration(CurScope, LM.Method);
136 }
Douglas Gregora376cbd2009-05-27 23:11:45 +0000137
138 for (unsigned I = 0, N = Class.NestedClasses.size(); I != N; ++I)
139 ParseLexedMethodDeclarations(*Class.NestedClasses[I]);
Douglas Gregor605de8d2008-12-16 21:30:33 +0000140}
141
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000142/// ParseLexedMethodDefs - We finished parsing the member specification of a top
143/// (non-nested) C++ class. Now go over the stack of lexed methods that were
144/// collected during its parsing and parse them all.
Douglas Gregora376cbd2009-05-27 23:11:45 +0000145void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
146 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
147 ParseScope TemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
148 if (HasTemplateScope)
149 Actions.ActOnReenterTemplateScope(CurScope, Class.TagOrTemplate);
150
151 bool HasClassScope = !Class.TopLevelClass;
152 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
153 HasClassScope);
154
155 for (; !Class.MethodDefs.empty(); Class.MethodDefs.pop_front()) {
156 LexedMethod &LM = Class.MethodDefs.front();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000157
158 assert(!LM.Toks.empty() && "Empty body!");
159 // Append the current token at the end of the new token stream so that it
160 // doesn't get lost.
161 LM.Toks.push_back(Tok);
162 PP.EnterTokenStream(&LM.Toks.front(), LM.Toks.size(), true, false);
163
164 // Consume the previously pushed token.
165 ConsumeAnyToken();
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000166 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
167 && "Inline method not starting with '{', ':' or 'try'");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000168
169 // Parse the method body. Function body parsing code is similar enough
170 // to be re-used for method bodies as well.
Douglas Gregor95d40792008-12-10 06:34:36 +0000171 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000172 Actions.ActOnStartOfFunctionDef(CurScope, LM.D);
173
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000174 if (Tok.is(tok::kw_try)) {
175 ParseFunctionTryBlock(LM.D);
Sebastian Redl8b772d32009-04-26 21:08:36 +0000176 continue;
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000177 }
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000178 if (Tok.is(tok::colon))
179 ParseConstructorInitializer(LM.D);
Fariborz Jahanianbc397fc2009-07-14 20:06:22 +0000180 else
Fariborz Jahanian4e127232009-07-21 22:36:06 +0000181 Actions.ActOnDefaultCtorInitializers(LM.D);
Fariborz Jahanian9294d192009-07-14 18:24:21 +0000182
Chris Lattner0818a7a2009-03-05 00:49:17 +0000183 // FIXME: What if ParseConstructorInitializer doesn't leave us with a '{'??
184 ParseFunctionStatementBody(LM.D);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000185 }
Douglas Gregora376cbd2009-05-27 23:11:45 +0000186
187 for (unsigned I = 0, N = Class.NestedClasses.size(); I != N; ++I)
188 ParseLexedMethodDefs(*Class.NestedClasses[I]);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000189}
190
191/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor605de8d2008-12-16 21:30:33 +0000192/// container until the token 'T' is reached (which gets
193/// consumed/stored too, if ConsumeFinalToken).
Douglas Gregord2c14ad2008-11-10 16:59:40 +0000194/// If EarlyAbortIf is specified, then we will stop early if we find that
195/// token at the top level.
Douglas Gregor605de8d2008-12-16 21:30:33 +0000196/// Returns true if token 'T1' or 'T2' was found.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000197/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor605de8d2008-12-16 21:30:33 +0000198bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
199 CachedTokens &Toks,
200 tok::TokenKind EarlyAbortIf,
201 bool ConsumeFinalToken) {
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000202 // We always want this function to consume at least one token if the first
203 // token isn't T and if not at EOF.
204 bool isFirstTokenConsumed = true;
205 while (1) {
206 // If we found one of the tokens, stop and return true.
Douglas Gregor605de8d2008-12-16 21:30:33 +0000207 if (Tok.is(T1) || Tok.is(T2)) {
208 if (ConsumeFinalToken) {
209 Toks.push_back(Tok);
210 ConsumeAnyToken();
211 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000212 return true;
213 }
214
Douglas Gregord2c14ad2008-11-10 16:59:40 +0000215 // If we found the early-abort token, return.
216 if (Tok.is(EarlyAbortIf))
217 return false;
218
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000219 switch (Tok.getKind()) {
220 case tok::eof:
221 // Ran out of tokens.
222 return false;
223
224 case tok::l_paren:
225 // Recursively consume properly-nested parens.
226 Toks.push_back(Tok);
227 ConsumeParen();
Douglas Gregor605de8d2008-12-16 21:30:33 +0000228 ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000229 break;
230 case tok::l_square:
231 // Recursively consume properly-nested square brackets.
232 Toks.push_back(Tok);
233 ConsumeBracket();
Douglas Gregor605de8d2008-12-16 21:30:33 +0000234 ConsumeAndStoreUntil(tok::r_square, tok::unknown, Toks);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000235 break;
236 case tok::l_brace:
237 // Recursively consume properly-nested braces.
238 Toks.push_back(Tok);
239 ConsumeBrace();
Douglas Gregor605de8d2008-12-16 21:30:33 +0000240 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000241 break;
242
243 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
244 // Since the user wasn't looking for this token (if they were, it would
245 // already be handled), this isn't balanced. If there is a LHS token at a
246 // higher level, we will assume that this matches the unbalanced token
247 // and return it. Otherwise, this is a spurious RHS token, which we skip.
248 case tok::r_paren:
249 if (ParenCount && !isFirstTokenConsumed)
250 return false; // Matches something.
251 Toks.push_back(Tok);
252 ConsumeParen();
253 break;
254 case tok::r_square:
255 if (BracketCount && !isFirstTokenConsumed)
256 return false; // Matches something.
257 Toks.push_back(Tok);
258 ConsumeBracket();
259 break;
260 case tok::r_brace:
261 if (BraceCount && !isFirstTokenConsumed)
262 return false; // Matches something.
263 Toks.push_back(Tok);
264 ConsumeBrace();
265 break;
266
267 case tok::string_literal:
268 case tok::wide_string_literal:
269 Toks.push_back(Tok);
270 ConsumeStringToken();
271 break;
272 default:
273 // consume this token.
274 Toks.push_back(Tok);
275 ConsumeToken();
276 break;
277 }
278 isFirstTokenConsumed = false;
279 }
280}