Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1 | //===--- 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 Lattner | 60f3622 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 14 | #include "clang/Parse/ParseDiagnostic.h" |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Parser.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 16 | #include "clang/Sema/DeclSpec.h" |
| 17 | #include "clang/Sema/Scope.h" |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 20 | /// ParseCXXInlineMethodDef - We parsed and verified that the specified |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 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. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 23 | Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, ParsingDeclarator &D, |
Nico Weber | 24b2a82 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 24 | const ParsedTemplateInfo &TemplateInfo, |
| 25 | const VirtSpecifiers& VS) { |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 26 | assert(D.isFunctionDeclarator() && "This isn't a function declarator!"); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 27 | assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) && |
| 28 | "Current token not a '{', ':' or 'try'!"); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 29 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 30 | MultiTemplateParamsArg TemplateParams(Actions, |
Alexis Hunt | 1deb972 | 2010-04-14 23:07:37 +0000 | [diff] [blame] | 31 | TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0, |
| 32 | TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0); |
| 33 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 34 | Decl *FnD; |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 35 | if (D.getDeclSpec().isFriendSpecified()) |
Douglas Gregor | 3447e76 | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 36 | // FIXME: Friend templates |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 37 | FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, true, |
Alexis Hunt | 1deb972 | 2010-04-14 23:07:37 +0000 | [diff] [blame] | 38 | move(TemplateParams)); |
Nico Weber | 24b2a82 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 39 | else { // FIXME: pass template information through |
| 40 | if (VS.isOverrideSpecified()) |
| 41 | Diag(VS.getOverrideLoc(), diag::ext_override_inline) << "override"; |
| 42 | if (VS.isFinalSpecified()) |
| 43 | Diag(VS.getFinalLoc(), diag::ext_override_inline) << "final"; |
| 44 | if (VS.isNewSpecified()) |
| 45 | Diag(VS.getNewLoc(), diag::ext_override_inline) << "new"; |
| 46 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 47 | FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D, |
Anders Carlsson | db36b80 | 2011-01-20 03:57:25 +0000 | [diff] [blame] | 48 | move(TemplateParams), 0, |
Nico Weber | 24b2a82 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 49 | VS, 0, /*IsDefinition*/true); |
| 50 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 51 | |
Eli Friedman | 3af2a77 | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 52 | HandleMemberFunctionDefaultArgs(D, FnD); |
| 53 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 54 | D.complete(FnD); |
| 55 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 56 | // Consume the tokens and store them for later parsing. |
| 57 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 58 | LexedMethod* LM = new LexedMethod(this, FnD); |
| 59 | getCurrentClass().LateParsedDeclarations.push_back(LM); |
| 60 | LM->TemplateScope = getCurScope()->isTemplateParamScope(); |
| 61 | CachedTokens &Toks = LM->Toks; |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 62 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 63 | tok::TokenKind kind = Tok.getKind(); |
| 64 | // We may have a constructor initializer or function-try-block here. |
| 65 | if (kind == tok::colon || kind == tok::kw_try) { |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 66 | // Consume everything up to (and including) the left brace. |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 67 | if (!ConsumeAndStoreUntil(tok::l_brace, Toks)) { |
Douglas Gregor | 49de534 | 2008-11-10 16:59:40 +0000 | [diff] [blame] | 68 | // We didn't find the left-brace we expected after the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | // constructor initializer. |
Douglas Gregor | 49de534 | 2008-11-10 16:59:40 +0000 | [diff] [blame] | 70 | if (Tok.is(tok::semi)) { |
| 71 | // We found a semicolon; complain, consume the semicolon, and |
| 72 | // don't try to parse this method later. |
| 73 | Diag(Tok.getLocation(), diag::err_expected_lbrace); |
| 74 | ConsumeAnyToken(); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 75 | delete getCurrentClass().LateParsedDeclarations.back(); |
| 76 | getCurrentClass().LateParsedDeclarations.pop_back(); |
Douglas Gregor | 49de534 | 2008-11-10 16:59:40 +0000 | [diff] [blame] | 77 | return FnD; |
| 78 | } |
| 79 | } |
| 80 | |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 81 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | // Begin by storing the '{' token. |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 83 | Toks.push_back(Tok); |
| 84 | ConsumeBrace(); |
| 85 | } |
| 86 | // Consume everything up to (and including) the matching right brace. |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 87 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 88 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 89 | // If we're in a function-try-block, we need to store all the catch blocks. |
| 90 | if (kind == tok::kw_try) { |
| 91 | while (Tok.is(tok::kw_catch)) { |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 92 | ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); |
| 93 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 97 | return FnD; |
| 98 | } |
| 99 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 100 | Parser::LateParsedDeclaration::~LateParsedDeclaration() {} |
| 101 | void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {} |
| 102 | void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {} |
| 103 | |
| 104 | Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C) |
| 105 | : Self(P), Class(C) {} |
| 106 | |
| 107 | Parser::LateParsedClass::~LateParsedClass() { |
| 108 | Self->DeallocateParsedClasses(Class); |
| 109 | } |
| 110 | |
| 111 | void Parser::LateParsedClass::ParseLexedMethodDeclarations() { |
| 112 | Self->ParseLexedMethodDeclarations(*Class); |
| 113 | } |
| 114 | |
| 115 | void Parser::LateParsedClass::ParseLexedMethodDefs() { |
| 116 | Self->ParseLexedMethodDefs(*Class); |
| 117 | } |
| 118 | |
| 119 | void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() { |
| 120 | Self->ParseLexedMethodDeclaration(*this); |
| 121 | } |
| 122 | |
| 123 | void Parser::LexedMethod::ParseLexedMethodDefs() { |
| 124 | Self->ParseLexedMethodDef(*this); |
| 125 | } |
| 126 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 127 | /// ParseLexedMethodDeclarations - We finished parsing the member |
| 128 | /// specification of a top (non-nested) C++ class. Now go over the |
| 129 | /// stack of method declarations with some parts for which parsing was |
| 130 | /// delayed (such as default arguments) and parse them. |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 131 | void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) { |
| 132 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 133 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 134 | if (HasTemplateScope) |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 135 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 136 | |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 137 | // The current scope is still active if we're the top-level class. |
| 138 | // Otherwise we'll need to push and enter a new scope. |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 139 | bool HasClassScope = !Class.TopLevelClass; |
Alexis Hunt | 1deb972 | 2010-04-14 23:07:37 +0000 | [diff] [blame] | 140 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, |
| 141 | HasClassScope); |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 142 | if (HasClassScope) |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 143 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 144 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 145 | for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { |
| 146 | Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations(); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 147 | } |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 148 | |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 149 | if (HasClassScope) |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 150 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 153 | void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) { |
| 154 | // If this is a member template, introduce the template parameter scope. |
| 155 | ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); |
| 156 | if (LM.TemplateScope) |
| 157 | Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method); |
| 158 | |
| 159 | // Start the delayed C++ method declaration |
| 160 | Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method); |
| 161 | |
| 162 | // Introduce the parameters into scope and parse their default |
| 163 | // arguments. |
| 164 | ParseScope PrototypeScope(this, |
| 165 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
| 166 | for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) { |
| 167 | // Introduce the parameter into scope. |
| 168 | Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param); |
| 169 | |
| 170 | if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) { |
| 171 | // Save the current token position. |
| 172 | SourceLocation origLoc = Tok.getLocation(); |
| 173 | |
| 174 | // Parse the default argument from its saved token stream. |
| 175 | Toks->push_back(Tok); // So that the current token doesn't get lost |
| 176 | PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false); |
| 177 | |
| 178 | // Consume the previously-pushed token. |
| 179 | ConsumeAnyToken(); |
| 180 | |
| 181 | // Consume the '='. |
| 182 | assert(Tok.is(tok::equal) && "Default argument not starting with '='"); |
| 183 | SourceLocation EqualLoc = ConsumeToken(); |
| 184 | |
| 185 | // The argument isn't actually potentially evaluated unless it is |
| 186 | // used. |
| 187 | EnterExpressionEvaluationContext Eval(Actions, |
| 188 | Sema::PotentiallyEvaluatedIfUsed); |
| 189 | |
| 190 | ExprResult DefArgResult(ParseAssignmentExpression()); |
| 191 | if (DefArgResult.isInvalid()) |
| 192 | Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param); |
| 193 | else { |
| 194 | if (Tok.is(tok::cxx_defaultarg_end)) |
| 195 | ConsumeToken(); |
| 196 | else |
| 197 | Diag(Tok.getLocation(), diag::err_default_arg_unparsed); |
| 198 | Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc, |
| 199 | DefArgResult.take()); |
| 200 | } |
| 201 | |
| 202 | assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, |
| 203 | Tok.getLocation()) && |
| 204 | "ParseAssignmentExpression went over the default arg tokens!"); |
| 205 | // There could be leftover tokens (e.g. because of an error). |
| 206 | // Skip through until we reach the original token position. |
| 207 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
| 208 | ConsumeAnyToken(); |
| 209 | |
| 210 | delete Toks; |
| 211 | LM.DefaultArgs[I].Toks = 0; |
| 212 | } |
| 213 | } |
| 214 | PrototypeScope.Exit(); |
| 215 | |
| 216 | // Finish the delayed C++ method declaration. |
| 217 | Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method); |
| 218 | } |
| 219 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 220 | /// ParseLexedMethodDefs - We finished parsing the member specification of a top |
| 221 | /// (non-nested) C++ class. Now go over the stack of lexed methods that were |
| 222 | /// collected during its parsing and parse them all. |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 223 | void Parser::ParseLexedMethodDefs(ParsingClass &Class) { |
| 224 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 225 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 226 | if (HasTemplateScope) |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 227 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 228 | |
| 229 | bool HasClassScope = !Class.TopLevelClass; |
| 230 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, |
| 231 | HasClassScope); |
| 232 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 233 | for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { |
| 234 | Class.LateParsedDeclarations[i]->ParseLexedMethodDefs(); |
| 235 | } |
| 236 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 237 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 238 | void Parser::ParseLexedMethodDef(LexedMethod &LM) { |
| 239 | // If this is a member template, introduce the template parameter scope. |
| 240 | ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); |
| 241 | if (LM.TemplateScope) |
| 242 | Actions.ActOnReenterTemplateScope(getCurScope(), LM.D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 244 | // Save the current token position. |
| 245 | SourceLocation origLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 0204197 | 2010-03-31 00:38:09 +0000 | [diff] [blame] | 246 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 247 | assert(!LM.Toks.empty() && "Empty body!"); |
| 248 | // Append the current token at the end of the new token stream so that it |
| 249 | // doesn't get lost. |
| 250 | LM.Toks.push_back(Tok); |
| 251 | PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 252 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 253 | // Consume the previously pushed token. |
| 254 | ConsumeAnyToken(); |
| 255 | assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) |
| 256 | && "Inline method not starting with '{', ':' or 'try'"); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 257 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 258 | // Parse the method body. Function body parsing code is similar enough |
| 259 | // to be re-used for method bodies as well. |
| 260 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope); |
| 261 | Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 262 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 263 | if (Tok.is(tok::kw_try)) { |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame^] | 264 | ParseFunctionTryBlock(LM.D, FnScope); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 265 | assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, |
| 266 | Tok.getLocation()) && |
| 267 | "ParseFunctionTryBlock went over the cached tokens!"); |
| 268 | // There could be leftover tokens (e.g. because of an error). |
| 269 | // Skip through until we reach the original token position. |
| 270 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
| 271 | ConsumeAnyToken(); |
| 272 | return; |
| 273 | } |
| 274 | if (Tok.is(tok::colon)) { |
| 275 | ParseConstructorInitializer(LM.D); |
| 276 | |
| 277 | // Error recovery. |
| 278 | if (!Tok.is(tok::l_brace)) { |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame^] | 279 | FnScope.Exit(); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 280 | Actions.ActOnFinishFunctionBody(LM.D, 0); |
| 281 | return; |
| 282 | } |
| 283 | } else |
| 284 | Actions.ActOnDefaultCtorInitializers(LM.D); |
| 285 | |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame^] | 286 | ParseFunctionStatementBody(LM.D, FnScope); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 287 | |
| 288 | if (Tok.getLocation() != origLoc) { |
| 289 | // Due to parsing error, we either went over the cached tokens or |
| 290 | // there are still cached tokens left. If it's the latter case skip the |
| 291 | // leftover tokens. |
| 292 | // Since this is an uncommon situation that should be avoided, use the |
| 293 | // expensive isBeforeInTranslationUnit call. |
| 294 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 295 | origLoc)) |
Argyrios Kyrtzidis | e1224c8 | 2010-06-19 19:58:34 +0000 | [diff] [blame] | 296 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
Argyrios Kyrtzidis | e9b76af | 2010-06-17 10:52:22 +0000 | [diff] [blame] | 297 | ConsumeAnyToken(); |
John McCall | bb7b658 | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 298 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | /// ConsumeAndStoreUntil - Consume and store the token at the passed token |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 303 | /// container until the token 'T' is reached (which gets |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | /// consumed/stored too, if ConsumeFinalToken). |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 305 | /// If StopAtSemi is true, then we will stop early at a ';' character. |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 306 | /// Returns true if token 'T1' or 'T2' was found. |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 307 | /// NOTE: This is a specialized version of Parser::SkipUntil. |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 308 | bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2, |
| 309 | CachedTokens &Toks, |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 310 | bool StopAtSemi, bool ConsumeFinalToken) { |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 311 | // We always want this function to consume at least one token if the first |
| 312 | // token isn't T and if not at EOF. |
| 313 | bool isFirstTokenConsumed = true; |
| 314 | while (1) { |
| 315 | // If we found one of the tokens, stop and return true. |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 316 | if (Tok.is(T1) || Tok.is(T2)) { |
| 317 | if (ConsumeFinalToken) { |
| 318 | Toks.push_back(Tok); |
| 319 | ConsumeAnyToken(); |
| 320 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 321 | return true; |
| 322 | } |
| 323 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 324 | switch (Tok.getKind()) { |
| 325 | case tok::eof: |
| 326 | // Ran out of tokens. |
| 327 | return false; |
| 328 | |
| 329 | case tok::l_paren: |
| 330 | // Recursively consume properly-nested parens. |
| 331 | Toks.push_back(Tok); |
| 332 | ConsumeParen(); |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 333 | ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 334 | break; |
| 335 | case tok::l_square: |
| 336 | // Recursively consume properly-nested square brackets. |
| 337 | Toks.push_back(Tok); |
| 338 | ConsumeBracket(); |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 339 | ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 340 | break; |
| 341 | case tok::l_brace: |
| 342 | // Recursively consume properly-nested braces. |
| 343 | Toks.push_back(Tok); |
| 344 | ConsumeBrace(); |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 345 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 346 | break; |
| 347 | |
| 348 | // Okay, we found a ']' or '}' or ')', which we think should be balanced. |
| 349 | // Since the user wasn't looking for this token (if they were, it would |
| 350 | // already be handled), this isn't balanced. If there is a LHS token at a |
| 351 | // higher level, we will assume that this matches the unbalanced token |
| 352 | // and return it. Otherwise, this is a spurious RHS token, which we skip. |
| 353 | case tok::r_paren: |
| 354 | if (ParenCount && !isFirstTokenConsumed) |
| 355 | return false; // Matches something. |
| 356 | Toks.push_back(Tok); |
| 357 | ConsumeParen(); |
| 358 | break; |
| 359 | case tok::r_square: |
| 360 | if (BracketCount && !isFirstTokenConsumed) |
| 361 | return false; // Matches something. |
| 362 | Toks.push_back(Tok); |
| 363 | ConsumeBracket(); |
| 364 | break; |
| 365 | case tok::r_brace: |
| 366 | if (BraceCount && !isFirstTokenConsumed) |
| 367 | return false; // Matches something. |
| 368 | Toks.push_back(Tok); |
| 369 | ConsumeBrace(); |
| 370 | break; |
| 371 | |
| 372 | case tok::string_literal: |
| 373 | case tok::wide_string_literal: |
| 374 | Toks.push_back(Tok); |
| 375 | ConsumeStringToken(); |
| 376 | break; |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 377 | case tok::semi: |
| 378 | if (StopAtSemi) |
| 379 | return false; |
| 380 | // FALL THROUGH. |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 381 | default: |
| 382 | // consume this token. |
| 383 | Toks.push_back(Tok); |
| 384 | ConsumeToken(); |
| 385 | break; |
| 386 | } |
| 387 | isFirstTokenConsumed = false; |
| 388 | } |
| 389 | } |