blob: a9712fe9ed406960c5de4daf0cebd85547be1c17 [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
Douglas Gregor3f08d182008-11-10 16:59:40 +000014#include "clang/Basic/Diagnostic.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.
78 ParseScope PrototypeScope(this, Scope::FnScope|Scope::DeclScope);
79 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
80 // Introduce the parameter into scope.
81 Actions.ActOnDelayedCXXMethodParameter(CurScope, LM.DefaultArgs[I].Param);
82
83 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
84 // Parse the default argument from its saved token stream.
85 Toks->push_back(Tok); // So that the current token doesn't get lost
86 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
87
88 // Consume the previously-pushed token.
89 ConsumeAnyToken();
90
91 // Consume the '='.
92 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
93 SourceLocation EqualLoc = ConsumeToken();
94
95 OwningExprResult DefArgResult(ParseAssignmentExpression());
96 if (DefArgResult.isInvalid())
97 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
98 else
99 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
100 DefArgResult.release());
101 delete Toks;
102 LM.DefaultArgs[I].Toks = 0;
103 }
104 }
105 PrototypeScope.Exit();
106
107 // Finish the delayed C++ method declaration.
108 Actions.ActOnFinishDelayedCXXMethodDeclaration(CurScope, LM.Method);
109 }
110}
111
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000112/// ParseLexedMethodDefs - We finished parsing the member specification of a top
113/// (non-nested) C++ class. Now go over the stack of lexed methods that were
114/// collected during its parsing and parse them all.
115void Parser::ParseLexedMethodDefs() {
Douglas Gregor72b505b2008-12-16 21:30:33 +0000116 for (; !getCurTopClassStack().MethodDefs.empty();
117 getCurTopClassStack().MethodDefs.pop_front()) {
118 LexedMethod &LM = getCurTopClassStack().MethodDefs.front();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000119
120 assert(!LM.Toks.empty() && "Empty body!");
121 // Append the current token at the end of the new token stream so that it
122 // doesn't get lost.
123 LM.Toks.push_back(Tok);
124 PP.EnterTokenStream(&LM.Toks.front(), LM.Toks.size(), true, false);
125
126 // Consume the previously pushed token.
127 ConsumeAnyToken();
Douglas Gregor7ad83902008-11-05 04:29:56 +0000128 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon)) &&
129 "Inline method not starting with '{' or ':'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000130
131 // Parse the method body. Function body parsing code is similar enough
132 // to be re-used for method bodies as well.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000133 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000134 Actions.ActOnStartOfFunctionDef(CurScope, LM.D);
135
Douglas Gregor7ad83902008-11-05 04:29:56 +0000136 if (Tok.is(tok::colon))
137 ParseConstructorInitializer(LM.D);
138
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000139 ParseFunctionStatementBody(LM.D, Tok.getLocation(), Tok.getLocation());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000140 }
141}
142
143/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000144/// container until the token 'T' is reached (which gets
145/// consumed/stored too, if ConsumeFinalToken).
Douglas Gregor3f08d182008-11-10 16:59:40 +0000146/// If EarlyAbortIf is specified, then we will stop early if we find that
147/// token at the top level.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000148/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000149/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000150bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
151 CachedTokens &Toks,
152 tok::TokenKind EarlyAbortIf,
153 bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000154 // We always want this function to consume at least one token if the first
155 // token isn't T and if not at EOF.
156 bool isFirstTokenConsumed = true;
157 while (1) {
158 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000159 if (Tok.is(T1) || Tok.is(T2)) {
160 if (ConsumeFinalToken) {
161 Toks.push_back(Tok);
162 ConsumeAnyToken();
163 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000164 return true;
165 }
166
Douglas Gregor3f08d182008-11-10 16:59:40 +0000167 // If we found the early-abort token, return.
168 if (Tok.is(EarlyAbortIf))
169 return false;
170
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000171 switch (Tok.getKind()) {
172 case tok::eof:
173 // Ran out of tokens.
174 return false;
175
176 case tok::l_paren:
177 // Recursively consume properly-nested parens.
178 Toks.push_back(Tok);
179 ConsumeParen();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000180 ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000181 break;
182 case tok::l_square:
183 // Recursively consume properly-nested square brackets.
184 Toks.push_back(Tok);
185 ConsumeBracket();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000186 ConsumeAndStoreUntil(tok::r_square, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000187 break;
188 case tok::l_brace:
189 // Recursively consume properly-nested braces.
190 Toks.push_back(Tok);
191 ConsumeBrace();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000192 ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000193 break;
194
195 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
196 // Since the user wasn't looking for this token (if they were, it would
197 // already be handled), this isn't balanced. If there is a LHS token at a
198 // higher level, we will assume that this matches the unbalanced token
199 // and return it. Otherwise, this is a spurious RHS token, which we skip.
200 case tok::r_paren:
201 if (ParenCount && !isFirstTokenConsumed)
202 return false; // Matches something.
203 Toks.push_back(Tok);
204 ConsumeParen();
205 break;
206 case tok::r_square:
207 if (BracketCount && !isFirstTokenConsumed)
208 return false; // Matches something.
209 Toks.push_back(Tok);
210 ConsumeBracket();
211 break;
212 case tok::r_brace:
213 if (BraceCount && !isFirstTokenConsumed)
214 return false; // Matches something.
215 Toks.push_back(Tok);
216 ConsumeBrace();
217 break;
218
219 case tok::string_literal:
220 case tok::wide_string_literal:
221 Toks.push_back(Tok);
222 ConsumeStringToken();
223 break;
224 default:
225 // consume this token.
226 Toks.push_back(Tok);
227 ConsumeToken();
228 break;
229 }
230 isFirstTokenConsumed = false;
231 }
232}