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