blob: 8dcf24f86a51ac4e5c476e8d303a39bcba0e5c89 [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
Argyrios Kyrtzidis4cc18a42008-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 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
Chris Lattner682bf922009-03-29 16:50:03 +000030 DeclPtrTy FnD = Actions.ActOnCXXMemberDeclarator(CurScope, AS, D, 0, 0);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000031
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000032 // Consume the tokens and store them for later parsing.
33
Douglas Gregor6569d682009-05-27 23:11:45 +000034 getCurrentClass().MethodDefs.push_back(LexedMethod(FnD));
35 CachedTokens &Toks = getCurrentClass().MethodDefs.back().Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000036
Sebastian Redld3a413d2009-04-26 20:35:05 +000037 tok::TokenKind kind = Tok.getKind();
38 // We may have a constructor initializer or function-try-block here.
39 if (kind == tok::colon || kind == tok::kw_try) {
Douglas Gregor7ad83902008-11-05 04:29:56 +000040 // Consume everything up to (and including) the left brace.
Douglas Gregor72b505b2008-12-16 21:30:33 +000041 if (!ConsumeAndStoreUntil(tok::l_brace, tok::unknown, Toks, tok::semi)) {
Douglas Gregor3f08d182008-11-10 16:59:40 +000042 // We didn't find the left-brace we expected after the
43 // constructor initializer.
44 if (Tok.is(tok::semi)) {
45 // We found a semicolon; complain, consume the semicolon, and
46 // don't try to parse this method later.
47 Diag(Tok.getLocation(), diag::err_expected_lbrace);
48 ConsumeAnyToken();
Douglas Gregor6569d682009-05-27 23:11:45 +000049 getCurrentClass().MethodDefs.pop_back();
Douglas Gregor3f08d182008-11-10 16:59:40 +000050 return FnD;
51 }
52 }
53
Douglas Gregor7ad83902008-11-05 04:29:56 +000054 } else {
55 // Begin by storing the '{' token.
56 Toks.push_back(Tok);
57 ConsumeBrace();
58 }
59 // Consume everything up to (and including) the matching right brace.
Douglas Gregor72b505b2008-12-16 21:30:33 +000060 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000061
Sebastian Redld3a413d2009-04-26 20:35:05 +000062 // If we're in a function-try-block, we need to store all the catch blocks.
63 if (kind == tok::kw_try) {
64 while (Tok.is(tok::kw_catch)) {
65 ConsumeAndStoreUntil(tok::l_brace, tok::unknown, Toks);
66 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
67 }
68 }
69
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000070 return FnD;
71}
72
Douglas Gregor72b505b2008-12-16 21:30:33 +000073/// ParseLexedMethodDeclarations - We finished parsing the member
74/// specification of a top (non-nested) C++ class. Now go over the
75/// stack of method declarations with some parts for which parsing was
76/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +000077void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
78 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
79 ParseScope TemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
80 if (HasTemplateScope)
81 Actions.ActOnReenterTemplateScope(CurScope, Class.TagOrTemplate);
82
83 bool HasClassScope = !Class.TopLevelClass;
84 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
85 HasClassScope);
86
87 for (; !Class.MethodDecls.empty(); Class.MethodDecls.pop_front()) {
88 LateParsedMethodDeclaration &LM = Class.MethodDecls.front();
Douglas Gregor72b505b2008-12-16 21:30:33 +000089
Douglas Gregor6569d682009-05-27 23:11:45 +000090 // FIXME: For member function templates, we'll need to introduce a
91 // scope for the template parameters.
92
Douglas Gregor72b505b2008-12-16 21:30:33 +000093 // Start the delayed C++ method declaration
94 Actions.ActOnStartDelayedCXXMethodDeclaration(CurScope, LM.Method);
95
96 // Introduce the parameters into scope and parse their default
97 // arguments.
Douglas Gregor3218c4b2009-01-09 22:42:13 +000098 ParseScope PrototypeScope(this,
99 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000100 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
101 // Introduce the parameter into scope.
102 Actions.ActOnDelayedCXXMethodParameter(CurScope, LM.DefaultArgs[I].Param);
103
104 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
105 // Parse the default argument from its saved token stream.
106 Toks->push_back(Tok); // So that the current token doesn't get lost
107 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
108
109 // Consume the previously-pushed token.
110 ConsumeAnyToken();
111
112 // Consume the '='.
113 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
114 SourceLocation EqualLoc = ConsumeToken();
115
116 OwningExprResult DefArgResult(ParseAssignmentExpression());
117 if (DefArgResult.isInvalid())
118 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
119 else
120 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000121 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +0000122 delete Toks;
123 LM.DefaultArgs[I].Toks = 0;
124 }
125 }
126 PrototypeScope.Exit();
127
128 // Finish the delayed C++ method declaration.
129 Actions.ActOnFinishDelayedCXXMethodDeclaration(CurScope, LM.Method);
130 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000131
132 for (unsigned I = 0, N = Class.NestedClasses.size(); I != N; ++I)
133 ParseLexedMethodDeclarations(*Class.NestedClasses[I]);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000134}
135
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000136/// ParseLexedMethodDefs - We finished parsing the member specification of a top
137/// (non-nested) C++ class. Now go over the stack of lexed methods that were
138/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000139void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
140 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
141 ParseScope TemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
142 if (HasTemplateScope)
143 Actions.ActOnReenterTemplateScope(CurScope, Class.TagOrTemplate);
144
145 bool HasClassScope = !Class.TopLevelClass;
146 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
147 HasClassScope);
148
149 for (; !Class.MethodDefs.empty(); Class.MethodDefs.pop_front()) {
150 LexedMethod &LM = Class.MethodDefs.front();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000151
152 assert(!LM.Toks.empty() && "Empty body!");
153 // Append the current token at the end of the new token stream so that it
154 // doesn't get lost.
155 LM.Toks.push_back(Tok);
156 PP.EnterTokenStream(&LM.Toks.front(), LM.Toks.size(), true, false);
157
158 // Consume the previously pushed token.
159 ConsumeAnyToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000160 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
161 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000162
163 // Parse the method body. Function body parsing code is similar enough
164 // to be re-used for method bodies as well.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000165 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000166 Actions.ActOnStartOfFunctionDef(CurScope, LM.D);
167
Sebastian Redld3a413d2009-04-26 20:35:05 +0000168 if (Tok.is(tok::kw_try)) {
169 ParseFunctionTryBlock(LM.D);
Sebastian Redlde1b60a2009-04-26 21:08:36 +0000170 continue;
Sebastian Redld3a413d2009-04-26 20:35:05 +0000171 }
Douglas Gregor7ad83902008-11-05 04:29:56 +0000172 if (Tok.is(tok::colon))
173 ParseConstructorInitializer(LM.D);
Fariborz Jahanian0849d382009-07-14 20:06:22 +0000174 else
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000175 Actions.ActOnDefaultInitializers(LM.D);
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000176
Chris Lattner40e9bc82009-03-05 00:49:17 +0000177 // FIXME: What if ParseConstructorInitializer doesn't leave us with a '{'??
178 ParseFunctionStatementBody(LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000179 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000180
181 for (unsigned I = 0, N = Class.NestedClasses.size(); I != N; ++I)
182 ParseLexedMethodDefs(*Class.NestedClasses[I]);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000183}
184
185/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000186/// container until the token 'T' is reached (which gets
187/// consumed/stored too, if ConsumeFinalToken).
Douglas Gregor3f08d182008-11-10 16:59:40 +0000188/// If EarlyAbortIf is specified, then we will stop early if we find that
189/// token at the top level.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000190/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000191/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000192bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
193 CachedTokens &Toks,
194 tok::TokenKind EarlyAbortIf,
195 bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000196 // We always want this function to consume at least one token if the first
197 // token isn't T and if not at EOF.
198 bool isFirstTokenConsumed = true;
199 while (1) {
200 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000201 if (Tok.is(T1) || Tok.is(T2)) {
202 if (ConsumeFinalToken) {
203 Toks.push_back(Tok);
204 ConsumeAnyToken();
205 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000206 return true;
207 }
208
Douglas Gregor3f08d182008-11-10 16:59:40 +0000209 // If we found the early-abort token, return.
210 if (Tok.is(EarlyAbortIf))
211 return false;
212
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000213 switch (Tok.getKind()) {
214 case tok::eof:
215 // Ran out of tokens.
216 return false;
217
218 case tok::l_paren:
219 // Recursively consume properly-nested parens.
220 Toks.push_back(Tok);
221 ConsumeParen();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000222 ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000223 break;
224 case tok::l_square:
225 // Recursively consume properly-nested square brackets.
226 Toks.push_back(Tok);
227 ConsumeBracket();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000228 ConsumeAndStoreUntil(tok::r_square, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000229 break;
230 case tok::l_brace:
231 // Recursively consume properly-nested braces.
232 Toks.push_back(Tok);
233 ConsumeBrace();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000234 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000235 break;
236
237 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
238 // Since the user wasn't looking for this token (if they were, it would
239 // already be handled), this isn't balanced. If there is a LHS token at a
240 // higher level, we will assume that this matches the unbalanced token
241 // and return it. Otherwise, this is a spurious RHS token, which we skip.
242 case tok::r_paren:
243 if (ParenCount && !isFirstTokenConsumed)
244 return false; // Matches something.
245 Toks.push_back(Tok);
246 ConsumeParen();
247 break;
248 case tok::r_square:
249 if (BracketCount && !isFirstTokenConsumed)
250 return false; // Matches something.
251 Toks.push_back(Tok);
252 ConsumeBracket();
253 break;
254 case tok::r_brace:
255 if (BraceCount && !isFirstTokenConsumed)
256 return false; // Matches something.
257 Toks.push_back(Tok);
258 ConsumeBrace();
259 break;
260
261 case tok::string_literal:
262 case tok::wide_string_literal:
263 Toks.push_back(Tok);
264 ConsumeStringToken();
265 break;
266 default:
267 // consume this token.
268 Toks.push_back(Tok);
269 ConsumeToken();
270 break;
271 }
272 isFirstTokenConsumed = false;
273 }
274}