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