blob: e139cf9c3ef93969115d8cec8f4fef2c32b9cdc5 [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
20/// ParseInlineCXXMethodDef - We parsed and verified that the specified
21/// 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!");
Douglas Gregor7ad83902008-11-05 04:29:56 +000027 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon)) &&
28 "Current token not a '{' or ':'!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000029
Chris Lattnerb28317a2009-03-28 19:18:32 +000030 DeclPtrTy FnD = Actions.ActOnCXXMemberDeclarator(CurScope, AS, D, 0, 0,
31 DeclPtrTy());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000032
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000033 // Consume the tokens and store them for later parsing.
34
Douglas Gregor72b505b2008-12-16 21:30:33 +000035 getCurTopClassStack().MethodDefs.push_back(LexedMethod(FnD));
36 CachedTokens &Toks = getCurTopClassStack().MethodDefs.back().Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000037
Douglas Gregor7ad83902008-11-05 04:29:56 +000038 // We may have a constructor initializer here.
39 if (Tok.is(tok::colon)) {
40 // 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
62 return FnD;
63}
64
Douglas Gregor72b505b2008-12-16 21:30:33 +000065/// ParseLexedMethodDeclarations - We finished parsing the member
66/// specification of a top (non-nested) C++ class. Now go over the
67/// stack of method declarations with some parts for which parsing was
68/// delayed (such as default arguments) and parse them.
69void Parser::ParseLexedMethodDeclarations() {
70 for (; !getCurTopClassStack().MethodDecls.empty();
71 getCurTopClassStack().MethodDecls.pop_front()) {
72 LateParsedMethodDeclaration &LM = getCurTopClassStack().MethodDecls.front();
73
74 // Start the delayed C++ method declaration
75 Actions.ActOnStartDelayedCXXMethodDeclaration(CurScope, LM.Method);
76
77 // Introduce the parameters into scope and parse their default
78 // arguments.
Douglas Gregor3218c4b2009-01-09 22:42:13 +000079 ParseScope PrototypeScope(this,
80 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor72b505b2008-12-16 21:30:33 +000081 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
82 // Introduce the parameter into scope.
83 Actions.ActOnDelayedCXXMethodParameter(CurScope, LM.DefaultArgs[I].Param);
84
85 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
86 // Parse the default argument from its saved token stream.
87 Toks->push_back(Tok); // So that the current token doesn't get lost
88 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
89
90 // Consume the previously-pushed token.
91 ConsumeAnyToken();
92
93 // Consume the '='.
94 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
95 SourceLocation EqualLoc = ConsumeToken();
96
97 OwningExprResult DefArgResult(ParseAssignmentExpression());
98 if (DefArgResult.isInvalid())
99 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
100 else
101 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +0000102 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +0000103 delete Toks;
104 LM.DefaultArgs[I].Toks = 0;
105 }
106 }
107 PrototypeScope.Exit();
108
109 // Finish the delayed C++ method declaration.
110 Actions.ActOnFinishDelayedCXXMethodDeclaration(CurScope, LM.Method);
111 }
112}
113
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000114/// ParseLexedMethodDefs - We finished parsing the member specification of a top
115/// (non-nested) C++ class. Now go over the stack of lexed methods that were
116/// collected during its parsing and parse them all.
117void Parser::ParseLexedMethodDefs() {
Douglas Gregor72b505b2008-12-16 21:30:33 +0000118 for (; !getCurTopClassStack().MethodDefs.empty();
119 getCurTopClassStack().MethodDefs.pop_front()) {
120 LexedMethod &LM = getCurTopClassStack().MethodDefs.front();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000121
122 assert(!LM.Toks.empty() && "Empty body!");
123 // Append the current token at the end of the new token stream so that it
124 // doesn't get lost.
125 LM.Toks.push_back(Tok);
126 PP.EnterTokenStream(&LM.Toks.front(), LM.Toks.size(), true, false);
127
128 // Consume the previously pushed token.
129 ConsumeAnyToken();
Douglas Gregor7ad83902008-11-05 04:29:56 +0000130 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon)) &&
131 "Inline method not starting with '{' or ':'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000132
133 // Parse the method body. Function body parsing code is similar enough
134 // to be re-used for method bodies as well.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000135 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000136 Actions.ActOnStartOfFunctionDef(CurScope, LM.D);
137
Douglas Gregor7ad83902008-11-05 04:29:56 +0000138 if (Tok.is(tok::colon))
139 ParseConstructorInitializer(LM.D);
Chris Lattner40e9bc82009-03-05 00:49:17 +0000140 // FIXME: What if ParseConstructorInitializer doesn't leave us with a '{'??
141 ParseFunctionStatementBody(LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000142 }
143}
144
145/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000146/// container until the token 'T' is reached (which gets
147/// consumed/stored too, if ConsumeFinalToken).
Douglas Gregor3f08d182008-11-10 16:59:40 +0000148/// If EarlyAbortIf is specified, then we will stop early if we find that
149/// token at the top level.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000150/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000151/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000152bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
153 CachedTokens &Toks,
154 tok::TokenKind EarlyAbortIf,
155 bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000156 // We always want this function to consume at least one token if the first
157 // token isn't T and if not at EOF.
158 bool isFirstTokenConsumed = true;
159 while (1) {
160 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000161 if (Tok.is(T1) || Tok.is(T2)) {
162 if (ConsumeFinalToken) {
163 Toks.push_back(Tok);
164 ConsumeAnyToken();
165 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000166 return true;
167 }
168
Douglas Gregor3f08d182008-11-10 16:59:40 +0000169 // If we found the early-abort token, return.
170 if (Tok.is(EarlyAbortIf))
171 return false;
172
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000173 switch (Tok.getKind()) {
174 case tok::eof:
175 // Ran out of tokens.
176 return false;
177
178 case tok::l_paren:
179 // Recursively consume properly-nested parens.
180 Toks.push_back(Tok);
181 ConsumeParen();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000182 ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000183 break;
184 case tok::l_square:
185 // Recursively consume properly-nested square brackets.
186 Toks.push_back(Tok);
187 ConsumeBracket();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000188 ConsumeAndStoreUntil(tok::r_square, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000189 break;
190 case tok::l_brace:
191 // Recursively consume properly-nested braces.
192 Toks.push_back(Tok);
193 ConsumeBrace();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000194 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000195 break;
196
197 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
198 // Since the user wasn't looking for this token (if they were, it would
199 // already be handled), this isn't balanced. If there is a LHS token at a
200 // higher level, we will assume that this matches the unbalanced token
201 // and return it. Otherwise, this is a spurious RHS token, which we skip.
202 case tok::r_paren:
203 if (ParenCount && !isFirstTokenConsumed)
204 return false; // Matches something.
205 Toks.push_back(Tok);
206 ConsumeParen();
207 break;
208 case tok::r_square:
209 if (BracketCount && !isFirstTokenConsumed)
210 return false; // Matches something.
211 Toks.push_back(Tok);
212 ConsumeBracket();
213 break;
214 case tok::r_brace:
215 if (BraceCount && !isFirstTokenConsumed)
216 return false; // Matches something.
217 Toks.push_back(Tok);
218 ConsumeBrace();
219 break;
220
221 case tok::string_literal:
222 case tok::wide_string_literal:
223 Toks.push_back(Tok);
224 ConsumeStringToken();
225 break;
226 default:
227 // consume this token.
228 Toks.push_back(Tok);
229 ConsumeToken();
230 break;
231 }
232 isFirstTokenConsumed = false;
233 }
234}