blob: 2f5014ba50403d14b8712060a293e2cbcde3fba6 [file] [log] [blame]
Argiris Kirtzidis9d784332008-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 Gregord2c14ad2008-11-10 16:59:40 +000014#include "clang/Basic/Diagnostic.h"
Argiris Kirtzidis9d784332008-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 Gregora65e8dd2008-11-05 04:29:56 +000027 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon)) &&
28 "Current token not a '{' or ':'!");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +000029
30 DeclTy *FnD = Actions.ActOnCXXMemberDeclarator(CurScope, AS, D, 0, 0, 0);
31
Argiris Kirtzidis9d784332008-06-24 22:12:16 +000032 // Consume the tokens and store them for later parsing.
33
34 getCurTopClassStack().push(LexedMethod(FnD));
35 TokensTy &Toks = getCurTopClassStack().top().Toks;
36
Douglas Gregora65e8dd2008-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 Gregord2c14ad2008-11-10 16:59:40 +000040 if (!ConsumeAndStoreUntil(tok::l_brace, Toks, tok::semi)) {
41 // 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();
48 getCurTopClassStack().pop();
49 return FnD;
50 }
51 }
52
Douglas Gregora65e8dd2008-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.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +000059 ConsumeAndStoreUntil(tok::r_brace, Toks);
60
61 return FnD;
62}
63
64/// ParseLexedMethodDefs - We finished parsing the member specification of a top
65/// (non-nested) C++ class. Now go over the stack of lexed methods that were
66/// collected during its parsing and parse them all.
67void Parser::ParseLexedMethodDefs() {
68 while (!getCurTopClassStack().empty()) {
69 LexedMethod &LM = getCurTopClassStack().top();
70
71 assert(!LM.Toks.empty() && "Empty body!");
72 // Append the current token at the end of the new token stream so that it
73 // doesn't get lost.
74 LM.Toks.push_back(Tok);
75 PP.EnterTokenStream(&LM.Toks.front(), LM.Toks.size(), true, false);
76
77 // Consume the previously pushed token.
78 ConsumeAnyToken();
Douglas Gregora65e8dd2008-11-05 04:29:56 +000079 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon)) &&
80 "Inline method not starting with '{' or ':'");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +000081
82 // Parse the method body. Function body parsing code is similar enough
83 // to be re-used for method bodies as well.
84 EnterScope(Scope::FnScope|Scope::DeclScope);
85 Actions.ActOnStartOfFunctionDef(CurScope, LM.D);
86
Douglas Gregora65e8dd2008-11-05 04:29:56 +000087 if (Tok.is(tok::colon))
88 ParseConstructorInitializer(LM.D);
89
Argiris Kirtzidis9d784332008-06-24 22:12:16 +000090 ParseFunctionStatementBody(LM.D, Tok.getLocation(), Tok.getLocation());
91
92 getCurTopClassStack().pop();
93 }
94}
95
96/// ConsumeAndStoreUntil - Consume and store the token at the passed token
97/// container until the token 'T' is reached (which gets consumed/stored too).
Douglas Gregord2c14ad2008-11-10 16:59:40 +000098/// If EarlyAbortIf is specified, then we will stop early if we find that
99/// token at the top level.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000100/// Returns true if token 'T' was found.
101/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregord2c14ad2008-11-10 16:59:40 +0000102bool Parser::ConsumeAndStoreUntil(tok::TokenKind T, TokensTy &Toks,
103 tok::TokenKind EarlyAbortIf) {
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000104 // We always want this function to consume at least one token if the first
105 // token isn't T and if not at EOF.
106 bool isFirstTokenConsumed = true;
107 while (1) {
108 // If we found one of the tokens, stop and return true.
109 if (Tok.is(T)) {
110 Toks.push_back(Tok);
111 ConsumeAnyToken();
112 return true;
113 }
114
Douglas Gregord2c14ad2008-11-10 16:59:40 +0000115 // If we found the early-abort token, return.
116 if (Tok.is(EarlyAbortIf))
117 return false;
118
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000119 switch (Tok.getKind()) {
120 case tok::eof:
121 // Ran out of tokens.
122 return false;
123
124 case tok::l_paren:
125 // Recursively consume properly-nested parens.
126 Toks.push_back(Tok);
127 ConsumeParen();
128 ConsumeAndStoreUntil(tok::r_paren, Toks);
129 break;
130 case tok::l_square:
131 // Recursively consume properly-nested square brackets.
132 Toks.push_back(Tok);
133 ConsumeBracket();
134 ConsumeAndStoreUntil(tok::r_square, Toks);
135 break;
136 case tok::l_brace:
137 // Recursively consume properly-nested braces.
138 Toks.push_back(Tok);
139 ConsumeBrace();
140 ConsumeAndStoreUntil(tok::r_brace, Toks);
141 break;
142
143 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
144 // Since the user wasn't looking for this token (if they were, it would
145 // already be handled), this isn't balanced. If there is a LHS token at a
146 // higher level, we will assume that this matches the unbalanced token
147 // and return it. Otherwise, this is a spurious RHS token, which we skip.
148 case tok::r_paren:
149 if (ParenCount && !isFirstTokenConsumed)
150 return false; // Matches something.
151 Toks.push_back(Tok);
152 ConsumeParen();
153 break;
154 case tok::r_square:
155 if (BracketCount && !isFirstTokenConsumed)
156 return false; // Matches something.
157 Toks.push_back(Tok);
158 ConsumeBracket();
159 break;
160 case tok::r_brace:
161 if (BraceCount && !isFirstTokenConsumed)
162 return false; // Matches something.
163 Toks.push_back(Tok);
164 ConsumeBrace();
165 break;
166
167 case tok::string_literal:
168 case tok::wide_string_literal:
169 Toks.push_back(Tok);
170 ConsumeStringToken();
171 break;
172 default:
173 // consume this token.
174 Toks.push_back(Tok);
175 ConsumeToken();
176 break;
177 }
178 isFirstTokenConsumed = false;
179 }
180}