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