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" |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 19 | #include "RAIIObjectsForParser.h" |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 22 | /// ParseCXXInlineMethodDef - We parsed and verified that the specified |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 23 | /// Declarator is a well formed C++ inline method definition. Now lex its body |
| 24 | /// and store its tokens for parsing after the C++ class is complete. |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 25 | Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, |
| 26 | AttributeList *AccessAttrs, |
| 27 | ParsingDeclarator &D, |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 28 | const ParsedTemplateInfo &TemplateInfo, |
| 29 | const VirtSpecifiers& VS, |
| 30 | FunctionDefinitionKind DefinitionKind, |
| 31 | ExprResult& Init) { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 32 | assert(D.isFunctionDeclarator() && "This isn't a function declarator!"); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 33 | assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) || |
| 34 | Tok.is(tok::equal)) && |
| 35 | "Current token not a '{', ':', '=', or 'try'!"); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 36 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 37 | MultiTemplateParamsArg TemplateParams(Actions, |
Sean Hunt | 4cd8494 | 2010-04-14 23:07:37 +0000 | [diff] [blame] | 38 | TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0, |
| 39 | TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0); |
| 40 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 41 | Decl *FnD; |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 42 | D.setFunctionDefinitionKind(DefinitionKind); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 43 | if (D.getDeclSpec().isFriendSpecified()) |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 44 | FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, |
Sean Hunt | 4cd8494 | 2010-04-14 23:07:37 +0000 | [diff] [blame] | 45 | move(TemplateParams)); |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 46 | else { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 47 | FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D, |
Anders Carlsson | 69a8735 | 2011-01-20 03:57:25 +0000 | [diff] [blame] | 48 | move(TemplateParams), 0, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 49 | VS, ICIS_NoInit); |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 50 | if (FnD) { |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 51 | Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs, |
| 52 | false, true); |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 53 | bool TypeSpecContainsAuto |
| 54 | = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
Douglas Gregor | a2b4e5d | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 55 | if (Init.isUsable()) |
Douglas Gregor | 147545d | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 56 | Actions.AddInitializerToDecl(FnD, Init.get(), false, |
| 57 | TypeSpecContainsAuto); |
| 58 | else |
| 59 | Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto); |
| 60 | } |
Nico Weber | 4867347 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 61 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 62 | |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 63 | HandleMemberFunctionDeclDelays(D, FnD); |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 64 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 65 | D.complete(FnD); |
| 66 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 67 | if (Tok.is(tok::equal)) { |
| 68 | ConsumeToken(); |
| 69 | |
Richard Smith | c430ef4 | 2011-11-10 09:08:44 +0000 | [diff] [blame] | 70 | if (!FnD) { |
| 71 | SkipUntil(tok::semi); |
| 72 | return 0; |
| 73 | } |
| 74 | |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 75 | bool Delete = false; |
| 76 | SourceLocation KWLoc; |
| 77 | if (Tok.is(tok::kw_delete)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 78 | Diag(Tok, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 79 | diag::warn_cxx98_compat_deleted_function : |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 80 | diag::ext_deleted_function); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 81 | |
| 82 | KWLoc = ConsumeToken(); |
| 83 | Actions.SetDeclDeleted(FnD, KWLoc); |
| 84 | Delete = true; |
| 85 | } else if (Tok.is(tok::kw_default)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 86 | Diag(Tok, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 87 | diag::warn_cxx98_compat_defaulted_function : |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 88 | diag::ext_defaulted_function); |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 89 | |
| 90 | KWLoc = ConsumeToken(); |
| 91 | Actions.SetDeclDefaulted(FnD, KWLoc); |
| 92 | } else { |
| 93 | llvm_unreachable("function definition after = not 'delete' or 'default'"); |
| 94 | } |
| 95 | |
| 96 | if (Tok.is(tok::comma)) { |
| 97 | Diag(KWLoc, diag::err_default_delete_in_multiple_declaration) |
| 98 | << Delete; |
| 99 | SkipUntil(tok::semi); |
| 100 | } else { |
| 101 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after, |
| 102 | Delete ? "delete" : "default", tok::semi); |
| 103 | } |
| 104 | |
| 105 | return FnD; |
| 106 | } |
| 107 | |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 108 | // In delayed template parsing mode, if we are within a class template |
| 109 | // or if we are about to parse function member template then consume |
| 110 | // the tokens and store them for parsing at the end of the translation unit. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 111 | if (getLangOpts().DelayedTemplateParsing && |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 112 | ((Actions.CurContext->isDependentContext() || |
| 113 | TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) && |
Francois Pichet | 9d38dbc | 2011-11-18 23:47:17 +0000 | [diff] [blame] | 114 | !Actions.IsInsideALocalClassWithinATemplateFunction())) { |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 115 | |
| 116 | if (FnD) { |
Francois Pichet | e1fca50 | 2011-12-08 09:11:52 +0000 | [diff] [blame] | 117 | LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(FnD); |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 118 | |
| 119 | FunctionDecl *FD = 0; |
| 120 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD)) |
| 121 | FD = FunTmpl->getTemplatedDecl(); |
| 122 | else |
| 123 | FD = cast<FunctionDecl>(FnD); |
Chandler Carruth | 81542fd | 2011-04-25 07:09:43 +0000 | [diff] [blame] | 124 | Actions.CheckForFunctionRedefinition(FD); |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 125 | |
| 126 | LateParsedTemplateMap[FD] = LPT; |
| 127 | Actions.MarkAsLateParsedTemplate(FD); |
| 128 | LexTemplateFunctionForLateParsing(LPT->Toks); |
| 129 | } else { |
| 130 | CachedTokens Toks; |
| 131 | LexTemplateFunctionForLateParsing(Toks); |
| 132 | } |
| 133 | |
| 134 | return FnD; |
| 135 | } |
| 136 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 137 | // Consume the tokens and store them for later parsing. |
| 138 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 139 | LexedMethod* LM = new LexedMethod(this, FnD); |
| 140 | getCurrentClass().LateParsedDeclarations.push_back(LM); |
| 141 | LM->TemplateScope = getCurScope()->isTemplateParamScope(); |
| 142 | CachedTokens &Toks = LM->Toks; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 143 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 144 | tok::TokenKind kind = Tok.getKind(); |
Sebastian Redl | a891a32 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 145 | // Consume everything up to (and including) the left brace of the |
| 146 | // function body. |
| 147 | if (ConsumeAndStoreFunctionPrologue(Toks)) { |
| 148 | // We didn't find the left-brace we expected after the |
Eli Friedman | e9ee382 | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 149 | // constructor initializer; we already printed an error, and it's likely |
| 150 | // impossible to recover, so don't try to parse this method later. |
| 151 | // If we stopped at a semicolon, consume it to avoid an extra warning. |
| 152 | if (Tok.is(tok::semi)) |
| 153 | ConsumeToken(); |
| 154 | delete getCurrentClass().LateParsedDeclarations.back(); |
| 155 | getCurrentClass().LateParsedDeclarations.pop_back(); |
| 156 | return FnD; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 157 | } else { |
Sebastian Redl | a891a32 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 158 | // Consume everything up to (and including) the matching right brace. |
| 159 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 160 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 161 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 162 | // If we're in a function-try-block, we need to store all the catch blocks. |
| 163 | if (kind == tok::kw_try) { |
| 164 | while (Tok.is(tok::kw_catch)) { |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 165 | ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); |
| 166 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Douglas Gregor | 87f1064 | 2011-04-14 23:19:27 +0000 | [diff] [blame] | 170 | |
| 171 | if (!FnD) { |
| 172 | // If semantic analysis could not build a function declaration, |
| 173 | // just throw away the late-parsed declaration. |
| 174 | delete getCurrentClass().LateParsedDeclarations.back(); |
| 175 | getCurrentClass().LateParsedDeclarations.pop_back(); |
| 176 | } |
| 177 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 178 | return FnD; |
| 179 | } |
| 180 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 181 | /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the |
| 182 | /// specified Declarator is a well formed C++ non-static data member |
| 183 | /// declaration. Now lex its initializer and store its tokens for parsing |
| 184 | /// after the class is complete. |
| 185 | void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) { |
| 186 | assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) && |
| 187 | "Current token not a '{' or '='!"); |
| 188 | |
| 189 | LateParsedMemberInitializer *MI = |
| 190 | new LateParsedMemberInitializer(this, VarD); |
| 191 | getCurrentClass().LateParsedDeclarations.push_back(MI); |
| 192 | CachedTokens &Toks = MI->Toks; |
| 193 | |
| 194 | tok::TokenKind kind = Tok.getKind(); |
| 195 | if (kind == tok::equal) { |
| 196 | Toks.push_back(Tok); |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 197 | ConsumeToken(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | if (kind == tok::l_brace) { |
| 201 | // Begin by storing the '{' token. |
| 202 | Toks.push_back(Tok); |
| 203 | ConsumeBrace(); |
| 204 | |
| 205 | // Consume everything up to (and including) the matching right brace. |
| 206 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true); |
| 207 | } else { |
| 208 | // Consume everything up to (but excluding) the comma or semicolon. |
| 209 | ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true, |
| 210 | /*ConsumeFinalToken=*/false); |
| 211 | } |
| 212 | |
| 213 | // Store an artificial EOF token to ensure that we don't run off the end of |
| 214 | // the initializer when we come to parse it. |
| 215 | Token Eof; |
| 216 | Eof.startToken(); |
| 217 | Eof.setKind(tok::eof); |
| 218 | Eof.setLocation(Tok.getLocation()); |
| 219 | Toks.push_back(Eof); |
| 220 | } |
| 221 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 222 | Parser::LateParsedDeclaration::~LateParsedDeclaration() {} |
| 223 | void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {} |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 224 | void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {} |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 225 | void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {} |
| 226 | |
| 227 | Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C) |
| 228 | : Self(P), Class(C) {} |
| 229 | |
| 230 | Parser::LateParsedClass::~LateParsedClass() { |
| 231 | Self->DeallocateParsedClasses(Class); |
| 232 | } |
| 233 | |
| 234 | void Parser::LateParsedClass::ParseLexedMethodDeclarations() { |
| 235 | Self->ParseLexedMethodDeclarations(*Class); |
| 236 | } |
| 237 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 238 | void Parser::LateParsedClass::ParseLexedMemberInitializers() { |
| 239 | Self->ParseLexedMemberInitializers(*Class); |
| 240 | } |
| 241 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 242 | void Parser::LateParsedClass::ParseLexedMethodDefs() { |
| 243 | Self->ParseLexedMethodDefs(*Class); |
| 244 | } |
| 245 | |
| 246 | void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() { |
| 247 | Self->ParseLexedMethodDeclaration(*this); |
| 248 | } |
| 249 | |
| 250 | void Parser::LexedMethod::ParseLexedMethodDefs() { |
| 251 | Self->ParseLexedMethodDef(*this); |
| 252 | } |
| 253 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 254 | void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() { |
| 255 | Self->ParseLexedMemberInitializer(*this); |
| 256 | } |
| 257 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 258 | /// ParseLexedMethodDeclarations - We finished parsing the member |
| 259 | /// specification of a top (non-nested) C++ class. Now go over the |
| 260 | /// stack of method declarations with some parts for which parsing was |
| 261 | /// delayed (such as default arguments) and parse them. |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 262 | void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) { |
| 263 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 264 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 265 | if (HasTemplateScope) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 266 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 267 | |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 268 | // The current scope is still active if we're the top-level class. |
| 269 | // Otherwise we'll need to push and enter a new scope. |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 270 | bool HasClassScope = !Class.TopLevelClass; |
Sean Hunt | 4cd8494 | 2010-04-14 23:07:37 +0000 | [diff] [blame] | 271 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, |
| 272 | HasClassScope); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 273 | if (HasClassScope) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 274 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 275 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 276 | for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { |
| 277 | Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 278 | } |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 279 | |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 280 | if (HasClassScope) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 281 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 284 | void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) { |
| 285 | // If this is a member template, introduce the template parameter scope. |
| 286 | ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); |
| 287 | if (LM.TemplateScope) |
| 288 | Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method); |
| 289 | |
| 290 | // Start the delayed C++ method declaration |
| 291 | Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method); |
| 292 | |
| 293 | // Introduce the parameters into scope and parse their default |
| 294 | // arguments. |
| 295 | ParseScope PrototypeScope(this, |
| 296 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
| 297 | for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) { |
| 298 | // Introduce the parameter into scope. |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 299 | Actions.ActOnDelayedCXXMethodParameter(getCurScope(), |
| 300 | LM.DefaultArgs[I].Param); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 301 | |
| 302 | if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) { |
| 303 | // Save the current token position. |
| 304 | SourceLocation origLoc = Tok.getLocation(); |
| 305 | |
| 306 | // Parse the default argument from its saved token stream. |
| 307 | Toks->push_back(Tok); // So that the current token doesn't get lost |
| 308 | PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false); |
| 309 | |
| 310 | // Consume the previously-pushed token. |
| 311 | ConsumeAnyToken(); |
| 312 | |
| 313 | // Consume the '='. |
| 314 | assert(Tok.is(tok::equal) && "Default argument not starting with '='"); |
| 315 | SourceLocation EqualLoc = ConsumeToken(); |
| 316 | |
| 317 | // The argument isn't actually potentially evaluated unless it is |
| 318 | // used. |
| 319 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 320 | Sema::PotentiallyEvaluatedIfUsed, |
| 321 | LM.DefaultArgs[I].Param); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 322 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 323 | ExprResult DefArgResult; |
Sebastian Redl | ca89371 | 2012-03-20 21:24:03 +0000 | [diff] [blame] | 324 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) { |
| 325 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 326 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | ca89371 | 2012-03-20 21:24:03 +0000 | [diff] [blame] | 327 | } else |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 328 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 329 | if (DefArgResult.isInvalid()) |
| 330 | Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param); |
| 331 | else { |
| 332 | if (Tok.is(tok::cxx_defaultarg_end)) |
| 333 | ConsumeToken(); |
| 334 | else |
| 335 | Diag(Tok.getLocation(), diag::err_default_arg_unparsed); |
| 336 | Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc, |
| 337 | DefArgResult.take()); |
| 338 | } |
| 339 | |
| 340 | assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, |
| 341 | Tok.getLocation()) && |
| 342 | "ParseAssignmentExpression went over the default arg tokens!"); |
| 343 | // There could be leftover tokens (e.g. because of an error). |
| 344 | // Skip through until we reach the original token position. |
| 345 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
| 346 | ConsumeAnyToken(); |
| 347 | |
| 348 | delete Toks; |
| 349 | LM.DefaultArgs[I].Toks = 0; |
| 350 | } |
| 351 | } |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 352 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 353 | PrototypeScope.Exit(); |
| 354 | |
| 355 | // Finish the delayed C++ method declaration. |
| 356 | Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method); |
| 357 | } |
| 358 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 359 | /// ParseLexedMethodDefs - We finished parsing the member specification of a top |
| 360 | /// (non-nested) C++ class. Now go over the stack of lexed methods that were |
| 361 | /// collected during its parsing and parse them all. |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 362 | void Parser::ParseLexedMethodDefs(ParsingClass &Class) { |
| 363 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 364 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 365 | if (HasTemplateScope) |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 366 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 367 | |
| 368 | bool HasClassScope = !Class.TopLevelClass; |
| 369 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, |
| 370 | HasClassScope); |
| 371 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 372 | for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { |
| 373 | Class.LateParsedDeclarations[i]->ParseLexedMethodDefs(); |
| 374 | } |
| 375 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 376 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 377 | void Parser::ParseLexedMethodDef(LexedMethod &LM) { |
| 378 | // If this is a member template, introduce the template parameter scope. |
| 379 | ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); |
| 380 | if (LM.TemplateScope) |
| 381 | Actions.ActOnReenterTemplateScope(getCurScope(), LM.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 383 | // Save the current token position. |
| 384 | SourceLocation origLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | c50a5e0 | 2010-03-31 00:38:09 +0000 | [diff] [blame] | 385 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 386 | assert(!LM.Toks.empty() && "Empty body!"); |
| 387 | // Append the current token at the end of the new token stream so that it |
| 388 | // doesn't get lost. |
| 389 | LM.Toks.push_back(Tok); |
| 390 | PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 391 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 392 | // Consume the previously pushed token. |
| 393 | ConsumeAnyToken(); |
| 394 | assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) |
| 395 | && "Inline method not starting with '{', ':' or 'try'"); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 396 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 397 | // Parse the method body. Function body parsing code is similar enough |
| 398 | // to be re-used for method bodies as well. |
| 399 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope); |
| 400 | Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 401 | |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 402 | if (Tok.is(tok::kw_try)) { |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 403 | ParseFunctionTryBlock(LM.D, FnScope); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 404 | assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, |
| 405 | Tok.getLocation()) && |
| 406 | "ParseFunctionTryBlock went over the cached tokens!"); |
| 407 | // There could be leftover tokens (e.g. because of an error). |
| 408 | // Skip through until we reach the original token position. |
| 409 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
| 410 | ConsumeAnyToken(); |
| 411 | return; |
| 412 | } |
| 413 | if (Tok.is(tok::colon)) { |
| 414 | ParseConstructorInitializer(LM.D); |
| 415 | |
| 416 | // Error recovery. |
| 417 | if (!Tok.is(tok::l_brace)) { |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 418 | FnScope.Exit(); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 419 | Actions.ActOnFinishFunctionBody(LM.D, 0); |
Matt Beaumont-Gay | 1090452 | 2011-09-23 22:39:23 +0000 | [diff] [blame] | 420 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
| 421 | ConsumeAnyToken(); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | } else |
| 425 | Actions.ActOnDefaultCtorInitializers(LM.D); |
| 426 | |
Douglas Gregor | c9977d0 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 427 | ParseFunctionStatementBody(LM.D, FnScope); |
Douglas Gregor | d54eb44 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 428 | |
| 429 | if (Tok.getLocation() != origLoc) { |
| 430 | // Due to parsing error, we either went over the cached tokens or |
| 431 | // there are still cached tokens left. If it's the latter case skip the |
| 432 | // leftover tokens. |
| 433 | // Since this is an uncommon situation that should be avoided, use the |
| 434 | // expensive isBeforeInTranslationUnit call. |
| 435 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 436 | origLoc)) |
Argyrios Kyrtzidis | 8f9359f | 2010-06-19 19:58:34 +0000 | [diff] [blame] | 437 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
Argyrios Kyrtzidis | 7558cd0 | 2010-06-17 10:52:22 +0000 | [diff] [blame] | 438 | ConsumeAnyToken(); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 442 | /// ParseLexedMemberInitializers - We finished parsing the member specification |
| 443 | /// of a top (non-nested) C++ class. Now go over the stack of lexed data member |
| 444 | /// initializers that were collected during its parsing and parse them all. |
| 445 | void Parser::ParseLexedMemberInitializers(ParsingClass &Class) { |
| 446 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 447 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 448 | HasTemplateScope); |
| 449 | if (HasTemplateScope) |
| 450 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 451 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 452 | // Set or update the scope flags. |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 453 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 454 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 455 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 456 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 457 | |
| 458 | if (!AlreadyHasClassScope) |
| 459 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 460 | Class.TagOrTemplate); |
| 461 | |
Benjamin Kramer | 268efba | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 462 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 463 | // C++11 [expr.prim.general]p4: |
| 464 | // Otherwise, if a member-declarator declares a non-static data member |
| 465 | // (9.2) of a class X, the expression this is a prvalue of type "pointer |
| 466 | // to X" within the optional brace-or-equal-initializer. It shall not |
| 467 | // appear elsewhere in the member-declarator. |
| 468 | Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate, |
| 469 | /*TypeQuals=*/(unsigned)0); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 470 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 471 | for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { |
| 472 | Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers(); |
| 473 | } |
| 474 | } |
| 475 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 476 | if (!AlreadyHasClassScope) |
| 477 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 478 | Class.TagOrTemplate); |
| 479 | |
| 480 | Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate); |
| 481 | } |
| 482 | |
| 483 | void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) { |
Richard Smith | 1991b71 | 2011-09-29 19:42:27 +0000 | [diff] [blame] | 484 | if (!MI.Field || MI.Field->isInvalidDecl()) |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 485 | return; |
| 486 | |
| 487 | // Append the current token at the end of the new token stream so that it |
| 488 | // doesn't get lost. |
| 489 | MI.Toks.push_back(Tok); |
| 490 | PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false); |
| 491 | |
| 492 | // Consume the previously pushed token. |
| 493 | ConsumeAnyToken(); |
| 494 | |
| 495 | SourceLocation EqualLoc; |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 496 | |
Douglas Gregor | 552e299 | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 497 | ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false, |
| 498 | EqualLoc); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 499 | |
| 500 | Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release()); |
| 501 | |
| 502 | // The next token should be our artificial terminating EOF token. |
| 503 | if (Tok.isNot(tok::eof)) { |
| 504 | SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation); |
| 505 | if (!EndLoc.isValid()) |
| 506 | EndLoc = Tok.getLocation(); |
| 507 | // No fixit; we can't recover as if there were a semicolon here. |
| 508 | Diag(EndLoc, diag::err_expected_semi_decl_list); |
| 509 | |
| 510 | // Consume tokens until we hit the artificial EOF. |
| 511 | while (Tok.isNot(tok::eof)) |
| 512 | ConsumeAnyToken(); |
| 513 | } |
| 514 | ConsumeAnyToken(); |
| 515 | } |
| 516 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 517 | /// ConsumeAndStoreUntil - Consume and store the token at the passed token |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 518 | /// container until the token 'T' is reached (which gets |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | /// consumed/stored too, if ConsumeFinalToken). |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 520 | /// If StopAtSemi is true, then we will stop early at a ';' character. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 521 | /// Returns true if token 'T1' or 'T2' was found. |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 522 | /// NOTE: This is a specialized version of Parser::SkipUntil. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 523 | bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2, |
| 524 | CachedTokens &Toks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 525 | bool StopAtSemi, bool ConsumeFinalToken) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 526 | // We always want this function to consume at least one token if the first |
| 527 | // token isn't T and if not at EOF. |
| 528 | bool isFirstTokenConsumed = true; |
| 529 | while (1) { |
| 530 | // If we found one of the tokens, stop and return true. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 531 | if (Tok.is(T1) || Tok.is(T2)) { |
| 532 | if (ConsumeFinalToken) { |
| 533 | Toks.push_back(Tok); |
| 534 | ConsumeAnyToken(); |
| 535 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 536 | return true; |
| 537 | } |
| 538 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 539 | switch (Tok.getKind()) { |
| 540 | case tok::eof: |
| 541 | // Ran out of tokens. |
| 542 | return false; |
| 543 | |
| 544 | case tok::l_paren: |
| 545 | // Recursively consume properly-nested parens. |
| 546 | Toks.push_back(Tok); |
| 547 | ConsumeParen(); |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 548 | ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 549 | break; |
| 550 | case tok::l_square: |
| 551 | // Recursively consume properly-nested square brackets. |
| 552 | Toks.push_back(Tok); |
| 553 | ConsumeBracket(); |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 554 | ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 555 | break; |
| 556 | case tok::l_brace: |
| 557 | // Recursively consume properly-nested braces. |
| 558 | Toks.push_back(Tok); |
| 559 | ConsumeBrace(); |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 560 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 561 | break; |
| 562 | |
| 563 | // Okay, we found a ']' or '}' or ')', which we think should be balanced. |
| 564 | // Since the user wasn't looking for this token (if they were, it would |
| 565 | // already be handled), this isn't balanced. If there is a LHS token at a |
| 566 | // higher level, we will assume that this matches the unbalanced token |
| 567 | // and return it. Otherwise, this is a spurious RHS token, which we skip. |
| 568 | case tok::r_paren: |
| 569 | if (ParenCount && !isFirstTokenConsumed) |
| 570 | return false; // Matches something. |
| 571 | Toks.push_back(Tok); |
| 572 | ConsumeParen(); |
| 573 | break; |
| 574 | case tok::r_square: |
| 575 | if (BracketCount && !isFirstTokenConsumed) |
| 576 | return false; // Matches something. |
| 577 | Toks.push_back(Tok); |
| 578 | ConsumeBracket(); |
| 579 | break; |
| 580 | case tok::r_brace: |
| 581 | if (BraceCount && !isFirstTokenConsumed) |
| 582 | return false; // Matches something. |
| 583 | Toks.push_back(Tok); |
| 584 | ConsumeBrace(); |
| 585 | break; |
| 586 | |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 587 | case tok::code_completion: |
| 588 | Toks.push_back(Tok); |
| 589 | ConsumeCodeCompletionToken(); |
| 590 | break; |
| 591 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 592 | case tok::string_literal: |
| 593 | case tok::wide_string_literal: |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 594 | case tok::utf8_string_literal: |
| 595 | case tok::utf16_string_literal: |
| 596 | case tok::utf32_string_literal: |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 597 | Toks.push_back(Tok); |
| 598 | ConsumeStringToken(); |
| 599 | break; |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 600 | case tok::semi: |
| 601 | if (StopAtSemi) |
| 602 | return false; |
| 603 | // FALL THROUGH. |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 604 | default: |
| 605 | // consume this token. |
| 606 | Toks.push_back(Tok); |
| 607 | ConsumeToken(); |
| 608 | break; |
| 609 | } |
| 610 | isFirstTokenConsumed = false; |
| 611 | } |
| 612 | } |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 613 | |
| 614 | /// \brief Consume tokens and store them in the passed token container until |
| 615 | /// we've passed the try keyword and constructor initializers and have consumed |
Sebastian Redl | a891a32 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 616 | /// the opening brace of the function body. The opening brace will be consumed |
| 617 | /// if and only if there was no error. |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 618 | /// |
Sebastian Redl | a891a32 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 619 | /// \return True on error. |
| 620 | bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) { |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 621 | if (Tok.is(tok::kw_try)) { |
| 622 | Toks.push_back(Tok); |
| 623 | ConsumeToken(); |
| 624 | } |
Eli Friedman | e9ee382 | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 625 | bool ReadInitializer = false; |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 626 | if (Tok.is(tok::colon)) { |
| 627 | // Initializers can contain braces too. |
| 628 | Toks.push_back(Tok); |
| 629 | ConsumeToken(); |
| 630 | |
| 631 | while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) { |
| 632 | if (Tok.is(tok::eof) || Tok.is(tok::semi)) |
Eli Friedman | e9ee382 | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 633 | return Diag(Tok.getLocation(), diag::err_expected_lbrace); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 634 | |
| 635 | // Grab the identifier. |
| 636 | if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks, |
| 637 | /*StopAtSemi=*/true, |
| 638 | /*ConsumeFinalToken=*/false)) |
Eli Friedman | e9ee382 | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 639 | return Diag(Tok.getLocation(), diag::err_expected_lparen); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 640 | |
| 641 | tok::TokenKind kind = Tok.getKind(); |
| 642 | Toks.push_back(Tok); |
Eli Friedman | e9ee382 | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 643 | bool IsLParen = (kind == tok::l_paren); |
| 644 | SourceLocation LOpen = Tok.getLocation(); |
| 645 | |
| 646 | if (IsLParen) { |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 647 | ConsumeParen(); |
Eli Friedman | e9ee382 | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 648 | } else { |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 649 | assert(kind == tok::l_brace && "Must be left paren or brace here."); |
| 650 | ConsumeBrace(); |
Sebastian Redl | a891a32 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 651 | // In C++03, this has to be the start of the function body, which |
Eli Friedman | e9ee382 | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 652 | // means the initializer is malformed; we'll diagnose it later. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 653 | if (!getLangOpts().CPlusPlus0x) |
Sebastian Redl | a891a32 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 654 | return false; |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | // Grab the initializer |
Eli Friedman | e9ee382 | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 658 | if (!ConsumeAndStoreUntil(IsLParen ? tok::r_paren : tok::r_brace, |
| 659 | Toks, /*StopAtSemi=*/true)) { |
| 660 | Diag(Tok, IsLParen ? diag::err_expected_rparen : |
| 661 | diag::err_expected_rbrace); |
| 662 | Diag(LOpen, diag::note_matching) << (IsLParen ? "(" : "{"); |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 663 | return true; |
Eli Friedman | e9ee382 | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | // Grab pack ellipsis, if present |
| 667 | if (Tok.is(tok::ellipsis)) { |
| 668 | Toks.push_back(Tok); |
| 669 | ConsumeToken(); |
| 670 | } |
Sebastian Redl | a891a32 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 671 | |
| 672 | // Grab the separating comma, if any. |
| 673 | if (Tok.is(tok::comma)) { |
| 674 | Toks.push_back(Tok); |
| 675 | ConsumeToken(); |
Eli Friedman | e9ee382 | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 676 | } else if (Tok.isNot(tok::l_brace)) { |
| 677 | ReadInitializer = true; |
| 678 | break; |
Sebastian Redl | a891a32 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 679 | } |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
Sebastian Redl | a891a32 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 683 | // Grab any remaining garbage to be diagnosed later. We stop when we reach a |
| 684 | // brace: an opening one is the function body, while a closing one probably |
| 685 | // means we've reached the end of the class. |
Eli Friedman | e9ee382 | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 686 | ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks, |
| 687 | /*StopAtSemi=*/true, |
| 688 | /*ConsumeFinalToken=*/false); |
| 689 | if (Tok.isNot(tok::l_brace)) { |
| 690 | if (ReadInitializer) |
| 691 | return Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma); |
| 692 | return Diag(Tok.getLocation(), diag::err_expected_lbrace); |
| 693 | } |
Sebastian Redl | a891a32 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 694 | |
| 695 | Toks.push_back(Tok); |
| 696 | ConsumeBrace(); |
| 697 | return false; |
Sebastian Redl | 6df6548 | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 698 | } |