blob: a2f93b46cabd40ba1feca1c7b54607c5530101fa [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 Gregor72b505b2008-12-16 21:30:33 +000034 getCurTopClassStack().MethodDefs.push_back(LexedMethod(FnD));
35 CachedTokens &Toks = getCurTopClassStack().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 Gregor72b505b2008-12-16 21:30:33 +000049 getCurTopClassStack().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.
77void Parser::ParseLexedMethodDeclarations() {
78 for (; !getCurTopClassStack().MethodDecls.empty();
79 getCurTopClassStack().MethodDecls.pop_front()) {
80 LateParsedMethodDeclaration &LM = getCurTopClassStack().MethodDecls.front();
81
82 // Start the delayed C++ method declaration
83 Actions.ActOnStartDelayedCXXMethodDeclaration(CurScope, LM.Method);
84
85 // Introduce the parameters into scope and parse their default
86 // arguments.
Douglas Gregor3218c4b2009-01-09 22:42:13 +000087 ParseScope PrototypeScope(this,
88 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor72b505b2008-12-16 21:30:33 +000089 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
90 // Introduce the parameter into scope.
91 Actions.ActOnDelayedCXXMethodParameter(CurScope, LM.DefaultArgs[I].Param);
92
93 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
94 // Parse the default argument from its saved token stream.
95 Toks->push_back(Tok); // So that the current token doesn't get lost
96 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
97
98 // Consume the previously-pushed token.
99 ConsumeAnyToken();
100
101 // Consume the '='.
102 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
103 SourceLocation EqualLoc = ConsumeToken();
104
105 OwningExprResult DefArgResult(ParseAssignmentExpression());
106 if (DefArgResult.isInvalid())
107 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
108 else
109 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000110 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +0000111 delete Toks;
112 LM.DefaultArgs[I].Toks = 0;
113 }
114 }
115 PrototypeScope.Exit();
116
117 // Finish the delayed C++ method declaration.
118 Actions.ActOnFinishDelayedCXXMethodDeclaration(CurScope, LM.Method);
119 }
120}
121
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000122/// ParseLexedMethodDefs - We finished parsing the member specification of a top
123/// (non-nested) C++ class. Now go over the stack of lexed methods that were
124/// collected during its parsing and parse them all.
125void Parser::ParseLexedMethodDefs() {
Douglas Gregor72b505b2008-12-16 21:30:33 +0000126 for (; !getCurTopClassStack().MethodDefs.empty();
127 getCurTopClassStack().MethodDefs.pop_front()) {
128 LexedMethod &LM = getCurTopClassStack().MethodDefs.front();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000129
130 assert(!LM.Toks.empty() && "Empty body!");
131 // Append the current token at the end of the new token stream so that it
132 // doesn't get lost.
133 LM.Toks.push_back(Tok);
134 PP.EnterTokenStream(&LM.Toks.front(), LM.Toks.size(), true, false);
135
136 // Consume the previously pushed token.
137 ConsumeAnyToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000138 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
139 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000140
141 // Parse the method body. Function body parsing code is similar enough
142 // to be re-used for method bodies as well.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000143 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000144 Actions.ActOnStartOfFunctionDef(CurScope, LM.D);
145
Sebastian Redld3a413d2009-04-26 20:35:05 +0000146 if (Tok.is(tok::kw_try)) {
147 ParseFunctionTryBlock(LM.D);
148 return;
149 }
Douglas Gregor7ad83902008-11-05 04:29:56 +0000150 if (Tok.is(tok::colon))
151 ParseConstructorInitializer(LM.D);
Chris Lattner40e9bc82009-03-05 00:49:17 +0000152 // FIXME: What if ParseConstructorInitializer doesn't leave us with a '{'??
153 ParseFunctionStatementBody(LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000154 }
155}
156
157/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000158/// container until the token 'T' is reached (which gets
159/// consumed/stored too, if ConsumeFinalToken).
Douglas Gregor3f08d182008-11-10 16:59:40 +0000160/// If EarlyAbortIf is specified, then we will stop early if we find that
161/// token at the top level.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000162/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000163/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000164bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
165 CachedTokens &Toks,
166 tok::TokenKind EarlyAbortIf,
167 bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000168 // We always want this function to consume at least one token if the first
169 // token isn't T and if not at EOF.
170 bool isFirstTokenConsumed = true;
171 while (1) {
172 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000173 if (Tok.is(T1) || Tok.is(T2)) {
174 if (ConsumeFinalToken) {
175 Toks.push_back(Tok);
176 ConsumeAnyToken();
177 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000178 return true;
179 }
180
Douglas Gregor3f08d182008-11-10 16:59:40 +0000181 // If we found the early-abort token, return.
182 if (Tok.is(EarlyAbortIf))
183 return false;
184
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000185 switch (Tok.getKind()) {
186 case tok::eof:
187 // Ran out of tokens.
188 return false;
189
190 case tok::l_paren:
191 // Recursively consume properly-nested parens.
192 Toks.push_back(Tok);
193 ConsumeParen();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000194 ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000195 break;
196 case tok::l_square:
197 // Recursively consume properly-nested square brackets.
198 Toks.push_back(Tok);
199 ConsumeBracket();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000200 ConsumeAndStoreUntil(tok::r_square, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000201 break;
202 case tok::l_brace:
203 // Recursively consume properly-nested braces.
204 Toks.push_back(Tok);
205 ConsumeBrace();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000206 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000207 break;
208
209 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
210 // Since the user wasn't looking for this token (if they were, it would
211 // already be handled), this isn't balanced. If there is a LHS token at a
212 // higher level, we will assume that this matches the unbalanced token
213 // and return it. Otherwise, this is a spurious RHS token, which we skip.
214 case tok::r_paren:
215 if (ParenCount && !isFirstTokenConsumed)
216 return false; // Matches something.
217 Toks.push_back(Tok);
218 ConsumeParen();
219 break;
220 case tok::r_square:
221 if (BracketCount && !isFirstTokenConsumed)
222 return false; // Matches something.
223 Toks.push_back(Tok);
224 ConsumeBracket();
225 break;
226 case tok::r_brace:
227 if (BraceCount && !isFirstTokenConsumed)
228 return false; // Matches something.
229 Toks.push_back(Tok);
230 ConsumeBrace();
231 break;
232
233 case tok::string_literal:
234 case tok::wide_string_literal:
235 Toks.push_back(Tok);
236 ConsumeStringToken();
237 break;
238 default:
239 // consume this token.
240 Toks.push_back(Tok);
241 ConsumeToken();
242 break;
243 }
244 isFirstTokenConsumed = false;
245 }
246}