blob: 8afffe3815e8cd900f33fd5f1ce8302067973b12 [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
Eli Friedmand33133c2009-07-22 21:45:50 +000032 HandleMemberFunctionDefaultArgs(D, FnD);
33
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000034 // Consume the tokens and store them for later parsing.
35
Douglas Gregor6569d682009-05-27 23:11:45 +000036 getCurrentClass().MethodDefs.push_back(LexedMethod(FnD));
37 CachedTokens &Toks = getCurrentClass().MethodDefs.back().Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000038
Sebastian Redld3a413d2009-04-26 20:35:05 +000039 tok::TokenKind kind = Tok.getKind();
40 // We may have a constructor initializer or function-try-block here.
41 if (kind == tok::colon || kind == tok::kw_try) {
Douglas Gregor7ad83902008-11-05 04:29:56 +000042 // Consume everything up to (and including) the left brace.
Douglas Gregor72b505b2008-12-16 21:30:33 +000043 if (!ConsumeAndStoreUntil(tok::l_brace, tok::unknown, Toks, tok::semi)) {
Douglas Gregor3f08d182008-11-10 16:59:40 +000044 // We didn't find the left-brace we expected after the
45 // constructor initializer.
46 if (Tok.is(tok::semi)) {
47 // We found a semicolon; complain, consume the semicolon, and
48 // don't try to parse this method later.
49 Diag(Tok.getLocation(), diag::err_expected_lbrace);
50 ConsumeAnyToken();
Douglas Gregor6569d682009-05-27 23:11:45 +000051 getCurrentClass().MethodDefs.pop_back();
Douglas Gregor3f08d182008-11-10 16:59:40 +000052 return FnD;
53 }
54 }
55
Douglas Gregor7ad83902008-11-05 04:29:56 +000056 } else {
57 // Begin by storing the '{' token.
58 Toks.push_back(Tok);
59 ConsumeBrace();
60 }
61 // Consume everything up to (and including) the matching right brace.
Douglas Gregor72b505b2008-12-16 21:30:33 +000062 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000063
Sebastian Redld3a413d2009-04-26 20:35:05 +000064 // If we're in a function-try-block, we need to store all the catch blocks.
65 if (kind == tok::kw_try) {
66 while (Tok.is(tok::kw_catch)) {
67 ConsumeAndStoreUntil(tok::l_brace, tok::unknown, Toks);
68 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
69 }
70 }
71
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000072 return FnD;
73}
74
Douglas Gregor72b505b2008-12-16 21:30:33 +000075/// ParseLexedMethodDeclarations - We finished parsing the member
76/// specification of a top (non-nested) C++ class. Now go over the
77/// stack of method declarations with some parts for which parsing was
78/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +000079void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
80 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
81 ParseScope TemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
82 if (HasTemplateScope)
83 Actions.ActOnReenterTemplateScope(CurScope, Class.TagOrTemplate);
84
85 bool HasClassScope = !Class.TopLevelClass;
86 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
87 HasClassScope);
88
89 for (; !Class.MethodDecls.empty(); Class.MethodDecls.pop_front()) {
90 LateParsedMethodDeclaration &LM = Class.MethodDecls.front();
Douglas Gregor72b505b2008-12-16 21:30:33 +000091
Douglas Gregor6569d682009-05-27 23:11:45 +000092 // FIXME: For member function templates, we'll need to introduce a
93 // scope for the template parameters.
94
Douglas Gregor72b505b2008-12-16 21:30:33 +000095 // Start the delayed C++ method declaration
96 Actions.ActOnStartDelayedCXXMethodDeclaration(CurScope, LM.Method);
97
98 // Introduce the parameters into scope and parse their default
99 // arguments.
Douglas Gregor3218c4b2009-01-09 22:42:13 +0000100 ParseScope PrototypeScope(this,
101 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000102 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
103 // Introduce the parameter into scope.
104 Actions.ActOnDelayedCXXMethodParameter(CurScope, LM.DefaultArgs[I].Param);
105
106 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
107 // Parse the default argument from its saved token stream.
108 Toks->push_back(Tok); // So that the current token doesn't get lost
109 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
110
111 // Consume the previously-pushed token.
112 ConsumeAnyToken();
113
114 // Consume the '='.
115 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
116 SourceLocation EqualLoc = ConsumeToken();
117
118 OwningExprResult DefArgResult(ParseAssignmentExpression());
119 if (DefArgResult.isInvalid())
120 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
121 else
122 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000123 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +0000124 delete Toks;
125 LM.DefaultArgs[I].Toks = 0;
126 }
127 }
128 PrototypeScope.Exit();
129
130 // Finish the delayed C++ method declaration.
131 Actions.ActOnFinishDelayedCXXMethodDeclaration(CurScope, LM.Method);
132 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000133
134 for (unsigned I = 0, N = Class.NestedClasses.size(); I != N; ++I)
135 ParseLexedMethodDeclarations(*Class.NestedClasses[I]);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000136}
137
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000138/// ParseLexedMethodDefs - We finished parsing the member specification of a top
139/// (non-nested) C++ class. Now go over the stack of lexed methods that were
140/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000141void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
142 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
143 ParseScope TemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
144 if (HasTemplateScope)
145 Actions.ActOnReenterTemplateScope(CurScope, Class.TagOrTemplate);
146
147 bool HasClassScope = !Class.TopLevelClass;
148 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
149 HasClassScope);
150
151 for (; !Class.MethodDefs.empty(); Class.MethodDefs.pop_front()) {
152 LexedMethod &LM = Class.MethodDefs.front();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000153
154 assert(!LM.Toks.empty() && "Empty body!");
155 // Append the current token at the end of the new token stream so that it
156 // doesn't get lost.
157 LM.Toks.push_back(Tok);
158 PP.EnterTokenStream(&LM.Toks.front(), LM.Toks.size(), true, false);
159
160 // Consume the previously pushed token.
161 ConsumeAnyToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000162 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
163 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000164
165 // Parse the method body. Function body parsing code is similar enough
166 // to be re-used for method bodies as well.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000167 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000168 Actions.ActOnStartOfFunctionDef(CurScope, LM.D);
169
Sebastian Redld3a413d2009-04-26 20:35:05 +0000170 if (Tok.is(tok::kw_try)) {
171 ParseFunctionTryBlock(LM.D);
Sebastian Redlde1b60a2009-04-26 21:08:36 +0000172 continue;
Sebastian Redld3a413d2009-04-26 20:35:05 +0000173 }
Douglas Gregor7ad83902008-11-05 04:29:56 +0000174 if (Tok.is(tok::colon))
175 ParseConstructorInitializer(LM.D);
Fariborz Jahanian0849d382009-07-14 20:06:22 +0000176 else
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000177 Actions.ActOnDefaultCtorInitializers(LM.D);
Fariborz Jahaniand01c9152009-07-14 18:24:21 +0000178
Chris Lattner40e9bc82009-03-05 00:49:17 +0000179 // FIXME: What if ParseConstructorInitializer doesn't leave us with a '{'??
180 ParseFunctionStatementBody(LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000181 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000182
183 for (unsigned I = 0, N = Class.NestedClasses.size(); I != N; ++I)
184 ParseLexedMethodDefs(*Class.NestedClasses[I]);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000185}
186
187/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000188/// container until the token 'T' is reached (which gets
189/// consumed/stored too, if ConsumeFinalToken).
Douglas Gregor3f08d182008-11-10 16:59:40 +0000190/// If EarlyAbortIf is specified, then we will stop early if we find that
191/// token at the top level.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000192/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000193/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000194bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
195 CachedTokens &Toks,
196 tok::TokenKind EarlyAbortIf,
197 bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000198 // We always want this function to consume at least one token if the first
199 // token isn't T and if not at EOF.
200 bool isFirstTokenConsumed = true;
201 while (1) {
202 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000203 if (Tok.is(T1) || Tok.is(T2)) {
204 if (ConsumeFinalToken) {
205 Toks.push_back(Tok);
206 ConsumeAnyToken();
207 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000208 return true;
209 }
210
Douglas Gregor3f08d182008-11-10 16:59:40 +0000211 // If we found the early-abort token, return.
212 if (Tok.is(EarlyAbortIf))
213 return false;
214
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000215 switch (Tok.getKind()) {
216 case tok::eof:
217 // Ran out of tokens.
218 return false;
219
220 case tok::l_paren:
221 // Recursively consume properly-nested parens.
222 Toks.push_back(Tok);
223 ConsumeParen();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000224 ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000225 break;
226 case tok::l_square:
227 // Recursively consume properly-nested square brackets.
228 Toks.push_back(Tok);
229 ConsumeBracket();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000230 ConsumeAndStoreUntil(tok::r_square, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000231 break;
232 case tok::l_brace:
233 // Recursively consume properly-nested braces.
234 Toks.push_back(Tok);
235 ConsumeBrace();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000236 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000237 break;
238
239 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
240 // Since the user wasn't looking for this token (if they were, it would
241 // already be handled), this isn't balanced. If there is a LHS token at a
242 // higher level, we will assume that this matches the unbalanced token
243 // and return it. Otherwise, this is a spurious RHS token, which we skip.
244 case tok::r_paren:
245 if (ParenCount && !isFirstTokenConsumed)
246 return false; // Matches something.
247 Toks.push_back(Tok);
248 ConsumeParen();
249 break;
250 case tok::r_square:
251 if (BracketCount && !isFirstTokenConsumed)
252 return false; // Matches something.
253 Toks.push_back(Tok);
254 ConsumeBracket();
255 break;
256 case tok::r_brace:
257 if (BraceCount && !isFirstTokenConsumed)
258 return false; // Matches something.
259 Toks.push_back(Tok);
260 ConsumeBrace();
261 break;
262
263 case tok::string_literal:
264 case tok::wide_string_literal:
265 Toks.push_back(Tok);
266 ConsumeStringToken();
267 break;
268 default:
269 // consume this token.
270 Toks.push_back(Tok);
271 ConsumeToken();
272 break;
273 }
274 isFirstTokenConsumed = false;
275 }
276}