blob: 4aa923ca009f08ce04c1b1a66612ba982fcb4510 [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.
23Parser::DeclTy *
24Parser::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
30 DeclTy *FnD = Actions.ActOnCXXMemberDeclarator(CurScope, AS, D, 0, 0, 0);
31
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
Douglas Gregor7ad83902008-11-05 04:29:56 +000037 // We may have a constructor initializer here.
38 if (Tok.is(tok::colon)) {
39 // Consume everything up to (and including) the left brace.
Douglas Gregor72b505b2008-12-16 21:30:33 +000040 if (!ConsumeAndStoreUntil(tok::l_brace, tok::unknown, Toks, tok::semi)) {
Douglas Gregor3f08d182008-11-10 16:59:40 +000041 // We didn't find the left-brace we expected after the
42 // constructor initializer.
43 if (Tok.is(tok::semi)) {
44 // We found a semicolon; complain, consume the semicolon, and
45 // don't try to parse this method later.
46 Diag(Tok.getLocation(), diag::err_expected_lbrace);
47 ConsumeAnyToken();
Douglas Gregor72b505b2008-12-16 21:30:33 +000048 getCurTopClassStack().MethodDefs.pop_back();
Douglas Gregor3f08d182008-11-10 16:59:40 +000049 return FnD;
50 }
51 }
52
Douglas Gregor7ad83902008-11-05 04:29:56 +000053 } else {
54 // Begin by storing the '{' token.
55 Toks.push_back(Tok);
56 ConsumeBrace();
57 }
58 // Consume everything up to (and including) the matching right brace.
Douglas Gregor72b505b2008-12-16 21:30:33 +000059 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000060
61 return FnD;
62}
63
Douglas Gregor72b505b2008-12-16 21:30:33 +000064/// ParseLexedMethodDeclarations - We finished parsing the member
65/// specification of a top (non-nested) C++ class. Now go over the
66/// stack of method declarations with some parts for which parsing was
67/// delayed (such as default arguments) and parse them.
68void Parser::ParseLexedMethodDeclarations() {
69 for (; !getCurTopClassStack().MethodDecls.empty();
70 getCurTopClassStack().MethodDecls.pop_front()) {
71 LateParsedMethodDeclaration &LM = getCurTopClassStack().MethodDecls.front();
72
73 // Start the delayed C++ method declaration
74 Actions.ActOnStartDelayedCXXMethodDeclaration(CurScope, LM.Method);
75
76 // Introduce the parameters into scope and parse their default
77 // arguments.
Douglas Gregor3218c4b2009-01-09 22:42:13 +000078 ParseScope PrototypeScope(this,
79 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor72b505b2008-12-16 21:30:33 +000080 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
81 // Introduce the parameter into scope.
82 Actions.ActOnDelayedCXXMethodParameter(CurScope, LM.DefaultArgs[I].Param);
83
84 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
85 // Parse the default argument from its saved token stream.
86 Toks->push_back(Tok); // So that the current token doesn't get lost
87 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
88
89 // Consume the previously-pushed token.
90 ConsumeAnyToken();
91
92 // Consume the '='.
93 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
94 SourceLocation EqualLoc = ConsumeToken();
95
96 OwningExprResult DefArgResult(ParseAssignmentExpression());
97 if (DefArgResult.isInvalid())
98 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
99 else
100 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
101 DefArgResult.release());
102 delete Toks;
103 LM.DefaultArgs[I].Toks = 0;
104 }
105 }
106 PrototypeScope.Exit();
107
108 // Finish the delayed C++ method declaration.
109 Actions.ActOnFinishDelayedCXXMethodDeclaration(CurScope, LM.Method);
110 }
111}
112
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000113/// ParseLexedMethodDefs - We finished parsing the member specification of a top
114/// (non-nested) C++ class. Now go over the stack of lexed methods that were
115/// collected during its parsing and parse them all.
116void Parser::ParseLexedMethodDefs() {
Douglas Gregor72b505b2008-12-16 21:30:33 +0000117 for (; !getCurTopClassStack().MethodDefs.empty();
118 getCurTopClassStack().MethodDefs.pop_front()) {
119 LexedMethod &LM = getCurTopClassStack().MethodDefs.front();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000120
121 assert(!LM.Toks.empty() && "Empty body!");
122 // Append the current token at the end of the new token stream so that it
123 // doesn't get lost.
124 LM.Toks.push_back(Tok);
125 PP.EnterTokenStream(&LM.Toks.front(), LM.Toks.size(), true, false);
126
127 // Consume the previously pushed token.
128 ConsumeAnyToken();
Douglas Gregor7ad83902008-11-05 04:29:56 +0000129 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon)) &&
130 "Inline method not starting with '{' or ':'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000131
132 // Parse the method body. Function body parsing code is similar enough
133 // to be re-used for method bodies as well.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000134 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000135 Actions.ActOnStartOfFunctionDef(CurScope, LM.D);
136
Douglas Gregor7ad83902008-11-05 04:29:56 +0000137 if (Tok.is(tok::colon))
138 ParseConstructorInitializer(LM.D);
139
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000140 ParseFunctionStatementBody(LM.D, Tok.getLocation(), Tok.getLocation());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000141 }
142}
143
144/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000145/// container until the token 'T' is reached (which gets
146/// consumed/stored too, if ConsumeFinalToken).
Douglas Gregor3f08d182008-11-10 16:59:40 +0000147/// If EarlyAbortIf is specified, then we will stop early if we find that
148/// token at the top level.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000149/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000150/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000151bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
152 CachedTokens &Toks,
153 tok::TokenKind EarlyAbortIf,
154 bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000155 // We always want this function to consume at least one token if the first
156 // token isn't T and if not at EOF.
157 bool isFirstTokenConsumed = true;
158 while (1) {
159 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000160 if (Tok.is(T1) || Tok.is(T2)) {
161 if (ConsumeFinalToken) {
162 Toks.push_back(Tok);
163 ConsumeAnyToken();
164 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000165 return true;
166 }
167
Douglas Gregor3f08d182008-11-10 16:59:40 +0000168 // If we found the early-abort token, return.
169 if (Tok.is(EarlyAbortIf))
170 return false;
171
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000172 switch (Tok.getKind()) {
173 case tok::eof:
174 // Ran out of tokens.
175 return false;
176
177 case tok::l_paren:
178 // Recursively consume properly-nested parens.
179 Toks.push_back(Tok);
180 ConsumeParen();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000181 ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000182 break;
183 case tok::l_square:
184 // Recursively consume properly-nested square brackets.
185 Toks.push_back(Tok);
186 ConsumeBracket();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000187 ConsumeAndStoreUntil(tok::r_square, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000188 break;
189 case tok::l_brace:
190 // Recursively consume properly-nested braces.
191 Toks.push_back(Tok);
192 ConsumeBrace();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000193 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000194 break;
195
196 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
197 // Since the user wasn't looking for this token (if they were, it would
198 // already be handled), this isn't balanced. If there is a LHS token at a
199 // higher level, we will assume that this matches the unbalanced token
200 // and return it. Otherwise, this is a spurious RHS token, which we skip.
201 case tok::r_paren:
202 if (ParenCount && !isFirstTokenConsumed)
203 return false; // Matches something.
204 Toks.push_back(Tok);
205 ConsumeParen();
206 break;
207 case tok::r_square:
208 if (BracketCount && !isFirstTokenConsumed)
209 return false; // Matches something.
210 Toks.push_back(Tok);
211 ConsumeBracket();
212 break;
213 case tok::r_brace:
214 if (BraceCount && !isFirstTokenConsumed)
215 return false; // Matches something.
216 Toks.push_back(Tok);
217 ConsumeBrace();
218 break;
219
220 case tok::string_literal:
221 case tok::wide_string_literal:
222 Toks.push_back(Tok);
223 ConsumeStringToken();
224 break;
225 default:
226 // consume this token.
227 Toks.push_back(Tok);
228 ConsumeToken();
229 break;
230 }
231 isFirstTokenConsumed = false;
232 }
233}