Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1 | //===--- ParseCXXInlineMethods.cpp - C++ class inline methods parsing------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements parsing for C++ class inline methods. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Chandler Carruth | 3a02247 | 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 | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 18 | #include "clang/Sema/DeclSpec.h" |
| 19 | #include "clang/Sema/Scope.h" |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 22 | /// ParseCXXInlineMethodDef - We parsed and verified that the specified |
Argyrios Kyrtzidis | 7bbb20e | 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 | c2453dd | 2013-01-08 21:00:12 +0000 | [diff] [blame] | 25 | NamedDecl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 26 | AttributeList *AccessAttrs, |
| 27 | ParsingDeclarator &D, |
Douglas Gregor | 5d1b4e3 | 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 | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 32 | assert(D.isFunctionDeclarator() && "This isn't a function declarator!"); |
Alexis Hunt | 5a7fa25 | 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 | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 36 | |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 37 | MultiTemplateParamsArg TemplateParams( |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 38 | TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() |
| 39 | : nullptr, |
Nico Weber | 77c76c5 | 2014-05-10 17:43:15 +0000 | [diff] [blame] | 40 | TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0); |
Alexis Hunt | 1deb972 | 2010-04-14 23:07:37 +0000 | [diff] [blame] | 41 | |
Rafael Espindola | c2453dd | 2013-01-08 21:00:12 +0000 | [diff] [blame] | 42 | NamedDecl *FnD; |
Douglas Gregor | 5d1b4e3 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 43 | D.setFunctionDefinitionKind(DefinitionKind); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 44 | if (D.getDeclSpec().isFriendSpecified()) |
Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 45 | FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 46 | TemplateParams); |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 47 | else { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 48 | FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D, |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 49 | TemplateParams, nullptr, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 50 | VS, ICIS_NoInit); |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 51 | if (FnD) { |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 52 | Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs); |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 53 | bool TypeSpecContainsAuto = D.getDeclSpec().containsPlaceholderType(); |
Douglas Gregor | 50cefbf | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 54 | if (Init.isUsable()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 55 | Actions.AddInitializerToDecl(FnD, Init.get(), false, |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 56 | TypeSpecContainsAuto); |
| 57 | else |
| 58 | Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto); |
| 59 | } |
Nico Weber | 24b2a82 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 60 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 61 | |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 62 | HandleMemberFunctionDeclDelays(D, FnD); |
Eli Friedman | 3af2a77 | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 63 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 64 | D.complete(FnD); |
| 65 | |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 66 | if (TryConsumeToken(tok::equal)) { |
Richard Smith | 1c70473 | 2011-11-10 09:08:44 +0000 | [diff] [blame] | 67 | if (!FnD) { |
| 68 | SkipUntil(tok::semi); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 69 | return nullptr; |
Richard Smith | 1c70473 | 2011-11-10 09:08:44 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 72 | bool Delete = false; |
| 73 | SourceLocation KWLoc; |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 74 | if (TryConsumeToken(tok::kw_delete, KWLoc)) { |
| 75 | Diag(KWLoc, getLangOpts().CPlusPlus11 |
| 76 | ? diag::warn_cxx98_compat_deleted_function |
| 77 | : diag::ext_deleted_function); |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 78 | Actions.SetDeclDeleted(FnD, KWLoc); |
| 79 | Delete = true; |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 80 | } else if (TryConsumeToken(tok::kw_default, KWLoc)) { |
| 81 | Diag(KWLoc, getLangOpts().CPlusPlus11 |
| 82 | ? diag::warn_cxx98_compat_defaulted_function |
| 83 | : diag::ext_defaulted_function); |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 84 | Actions.SetDeclDefaulted(FnD, KWLoc); |
| 85 | } else { |
| 86 | llvm_unreachable("function definition after = not 'delete' or 'default'"); |
| 87 | } |
| 88 | |
| 89 | if (Tok.is(tok::comma)) { |
| 90 | Diag(KWLoc, diag::err_default_delete_in_multiple_declaration) |
| 91 | << Delete; |
| 92 | SkipUntil(tok::semi); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 93 | } else if (ExpectAndConsume(tok::semi, diag::err_expected_after, |
| 94 | Delete ? "delete" : "default")) { |
| 95 | SkipUntil(tok::semi); |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | return FnD; |
| 99 | } |
Faisal Vali | b9657033 | 2013-11-01 02:01:01 +0000 | [diff] [blame] | 100 | |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 101 | // In delayed template parsing mode, if we are within a class template |
| 102 | // or if we are about to parse function member template then consume |
| 103 | // the tokens and store them for parsing at the end of the translation unit. |
David Majnemer | 90b1729 | 2013-09-14 05:46:42 +0000 | [diff] [blame] | 104 | if (getLangOpts().DelayedTemplateParsing && |
| 105 | DefinitionKind == FDK_Definition && |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 106 | !D.getDeclSpec().isConstexprSpecified() && |
| 107 | !(FnD && FnD->getAsFunction() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 108 | FnD->getAsFunction()->getReturnType()->getContainedAutoType()) && |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 109 | ((Actions.CurContext->isDependentContext() || |
David Majnemer | 90b1729 | 2013-09-14 05:46:42 +0000 | [diff] [blame] | 110 | (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
| 111 | TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) && |
| 112 | !Actions.IsInsideALocalClassWithinATemplateFunction())) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 113 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 114 | CachedTokens Toks; |
| 115 | LexTemplateFunctionForLateParsing(Toks); |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 116 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 117 | if (FnD) { |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 118 | FunctionDecl *FD = FnD->getAsFunction(); |
Chandler Carruth | bc0f9ae | 2011-04-25 07:09:43 +0000 | [diff] [blame] | 119 | Actions.CheckForFunctionRedefinition(FD); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 120 | Actions.MarkAsLateParsedTemplate(FD, FnD, Toks); |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | return FnD; |
| 124 | } |
| 125 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 126 | // Consume the tokens and store them for later parsing. |
| 127 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 128 | LexedMethod* LM = new LexedMethod(this, FnD); |
| 129 | getCurrentClass().LateParsedDeclarations.push_back(LM); |
| 130 | LM->TemplateScope = getCurScope()->isTemplateParamScope(); |
| 131 | CachedTokens &Toks = LM->Toks; |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 132 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 133 | tok::TokenKind kind = Tok.getKind(); |
Sebastian Redl | 0d16401 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 134 | // Consume everything up to (and including) the left brace of the |
| 135 | // function body. |
| 136 | if (ConsumeAndStoreFunctionPrologue(Toks)) { |
| 137 | // We didn't find the left-brace we expected after the |
Eli Friedman | 7cd4a9b | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 138 | // constructor initializer; we already printed an error, and it's likely |
| 139 | // impossible to recover, so don't try to parse this method later. |
Richard Smith | cde3fd8 | 2013-07-04 00:13:48 +0000 | [diff] [blame] | 140 | // Skip over the rest of the decl and back to somewhere that looks |
| 141 | // reasonable. |
| 142 | SkipMalformedDecl(); |
Eli Friedman | 7cd4a9b | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 143 | delete getCurrentClass().LateParsedDeclarations.back(); |
| 144 | getCurrentClass().LateParsedDeclarations.pop_back(); |
| 145 | return FnD; |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 146 | } else { |
Sebastian Redl | 0d16401 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 147 | // Consume everything up to (and including) the matching right brace. |
| 148 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 149 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 150 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 151 | // If we're in a function-try-block, we need to store all the catch blocks. |
| 152 | if (kind == tok::kw_try) { |
| 153 | while (Tok.is(tok::kw_catch)) { |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 154 | ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); |
| 155 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Stephen Lin | 9354fc5 | 2013-06-23 07:37:13 +0000 | [diff] [blame] | 159 | if (FnD) { |
| 160 | // If this is a friend function, mark that it's late-parsed so that |
| 161 | // it's still known to be a definition even before we attach the |
| 162 | // parsed body. Sema needs to treat friend function definitions |
| 163 | // differently during template instantiation, and it's possible for |
| 164 | // the containing class to be instantiated before all its member |
| 165 | // function definitions are parsed. |
| 166 | // |
| 167 | // If you remove this, you can remove the code that clears the flag |
| 168 | // after parsing the member. |
| 169 | if (D.getDeclSpec().isFriendSpecified()) { |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 170 | FunctionDecl *FD = FnD->getAsFunction(); |
Alp Toker | 19bff32 | 2013-10-18 05:54:24 +0000 | [diff] [blame] | 171 | Actions.CheckForFunctionRedefinition(FD); |
| 172 | FD->setLateTemplateParsed(true); |
Stephen Lin | 9354fc5 | 2013-06-23 07:37:13 +0000 | [diff] [blame] | 173 | } |
| 174 | } else { |
Douglas Gregor | 6ca6410 | 2011-04-14 23:19:27 +0000 | [diff] [blame] | 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 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 181 | return FnD; |
| 182 | } |
| 183 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 184 | /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the |
| 185 | /// specified Declarator is a well formed C++ non-static data member |
| 186 | /// declaration. Now lex its initializer and store its tokens for parsing |
| 187 | /// after the class is complete. |
| 188 | void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) { |
| 189 | assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) && |
| 190 | "Current token not a '{' or '='!"); |
| 191 | |
| 192 | LateParsedMemberInitializer *MI = |
| 193 | new LateParsedMemberInitializer(this, VarD); |
| 194 | getCurrentClass().LateParsedDeclarations.push_back(MI); |
| 195 | CachedTokens &Toks = MI->Toks; |
| 196 | |
| 197 | tok::TokenKind kind = Tok.getKind(); |
| 198 | if (kind == tok::equal) { |
| 199 | Toks.push_back(Tok); |
Douglas Gregor | 0cf55e9 | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 200 | ConsumeToken(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | if (kind == tok::l_brace) { |
| 204 | // Begin by storing the '{' token. |
| 205 | Toks.push_back(Tok); |
| 206 | ConsumeBrace(); |
| 207 | |
| 208 | // Consume everything up to (and including) the matching right brace. |
| 209 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true); |
| 210 | } else { |
| 211 | // Consume everything up to (but excluding) the comma or semicolon. |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 212 | ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // Store an artificial EOF token to ensure that we don't run off the end of |
| 216 | // the initializer when we come to parse it. |
| 217 | Token Eof; |
| 218 | Eof.startToken(); |
| 219 | Eof.setKind(tok::eof); |
| 220 | Eof.setLocation(Tok.getLocation()); |
| 221 | Toks.push_back(Eof); |
| 222 | } |
| 223 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 224 | Parser::LateParsedDeclaration::~LateParsedDeclaration() {} |
| 225 | void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {} |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 226 | void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {} |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 227 | void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {} |
| 228 | |
| 229 | Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C) |
| 230 | : Self(P), Class(C) {} |
| 231 | |
| 232 | Parser::LateParsedClass::~LateParsedClass() { |
| 233 | Self->DeallocateParsedClasses(Class); |
| 234 | } |
| 235 | |
| 236 | void Parser::LateParsedClass::ParseLexedMethodDeclarations() { |
| 237 | Self->ParseLexedMethodDeclarations(*Class); |
| 238 | } |
| 239 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 240 | void Parser::LateParsedClass::ParseLexedMemberInitializers() { |
| 241 | Self->ParseLexedMemberInitializers(*Class); |
| 242 | } |
| 243 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 244 | void Parser::LateParsedClass::ParseLexedMethodDefs() { |
| 245 | Self->ParseLexedMethodDefs(*Class); |
| 246 | } |
| 247 | |
| 248 | void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() { |
| 249 | Self->ParseLexedMethodDeclaration(*this); |
| 250 | } |
| 251 | |
| 252 | void Parser::LexedMethod::ParseLexedMethodDefs() { |
| 253 | Self->ParseLexedMethodDef(*this); |
| 254 | } |
| 255 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 256 | void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() { |
| 257 | Self->ParseLexedMemberInitializer(*this); |
| 258 | } |
| 259 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 260 | /// ParseLexedMethodDeclarations - We finished parsing the member |
| 261 | /// specification of a top (non-nested) C++ class. Now go over the |
| 262 | /// stack of method declarations with some parts for which parsing was |
| 263 | /// delayed (such as default arguments) and parse them. |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 264 | void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) { |
| 265 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
Nico Weber | 77c76c5 | 2014-05-10 17:43:15 +0000 | [diff] [blame] | 266 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 267 | HasTemplateScope); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 268 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); |
| 269 | if (HasTemplateScope) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 270 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 271 | ++CurTemplateDepthTracker; |
| 272 | } |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 273 | |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 274 | // The current scope is still active if we're the top-level class. |
| 275 | // Otherwise we'll need to push and enter a new scope. |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 276 | bool HasClassScope = !Class.TopLevelClass; |
Alexis Hunt | 1deb972 | 2010-04-14 23:07:37 +0000 | [diff] [blame] | 277 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, |
| 278 | HasClassScope); |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 279 | if (HasClassScope) |
Nico Weber | 77c76c5 | 2014-05-10 17:43:15 +0000 | [diff] [blame] | 280 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 281 | Class.TagOrTemplate); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 282 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 283 | for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { |
| 284 | Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations(); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 285 | } |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 286 | |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 287 | if (HasClassScope) |
Nico Weber | 77c76c5 | 2014-05-10 17:43:15 +0000 | [diff] [blame] | 288 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 289 | Class.TagOrTemplate); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 292 | void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) { |
| 293 | // If this is a member template, introduce the template parameter scope. |
| 294 | ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 295 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); |
| 296 | if (LM.TemplateScope) { |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 297 | Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 298 | ++CurTemplateDepthTracker; |
| 299 | } |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 300 | // Start the delayed C++ method declaration |
| 301 | Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method); |
| 302 | |
| 303 | // Introduce the parameters into scope and parse their default |
| 304 | // arguments. |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 305 | ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope | |
| 306 | Scope::FunctionDeclarationScope | Scope::DeclScope); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 307 | for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) { |
| 308 | // Introduce the parameter into scope. |
Nico Weber | 77c76c5 | 2014-05-10 17:43:15 +0000 | [diff] [blame] | 309 | Actions.ActOnDelayedCXXMethodParameter(getCurScope(), |
Douglas Gregor | 7fcbd90 | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 310 | LM.DefaultArgs[I].Param); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 311 | |
| 312 | if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) { |
| 313 | // Save the current token position. |
| 314 | SourceLocation origLoc = Tok.getLocation(); |
| 315 | |
| 316 | // Parse the default argument from its saved token stream. |
| 317 | Toks->push_back(Tok); // So that the current token doesn't get lost |
| 318 | PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false); |
| 319 | |
| 320 | // Consume the previously-pushed token. |
| 321 | ConsumeAnyToken(); |
| 322 | |
| 323 | // Consume the '='. |
| 324 | assert(Tok.is(tok::equal) && "Default argument not starting with '='"); |
| 325 | SourceLocation EqualLoc = ConsumeToken(); |
| 326 | |
| 327 | // The argument isn't actually potentially evaluated unless it is |
| 328 | // used. |
| 329 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | 7fcbd90 | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 330 | Sema::PotentiallyEvaluatedIfUsed, |
| 331 | LM.DefaultArgs[I].Param); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 332 | |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 333 | ExprResult DefArgResult; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 334 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 6db0b1b | 2012-03-20 21:24:03 +0000 | [diff] [blame] | 335 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 336 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 6db0b1b | 2012-03-20 21:24:03 +0000 | [diff] [blame] | 337 | } else |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 338 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 339 | if (DefArgResult.isInvalid()) |
Serge Pavlov | b4b3578 | 2014-07-22 01:54:49 +0000 | [diff] [blame] | 340 | Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param, |
| 341 | EqualLoc); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 342 | else { |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 343 | if (!TryConsumeToken(tok::cxx_defaultarg_end)) { |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 344 | // The last two tokens are the terminator and the saved value of |
| 345 | // Tok; the last token in the default argument is the one before |
| 346 | // those. |
| 347 | assert(Toks->size() >= 3 && "expected a token in default arg"); |
| 348 | Diag(Tok.getLocation(), diag::err_default_arg_unparsed) |
| 349 | << SourceRange(Tok.getLocation(), |
| 350 | (*Toks)[Toks->size() - 3].getLocation()); |
| 351 | } |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 352 | Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 353 | DefArgResult.get()); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 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; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 365 | LM.DefaultArgs[I].Toks = nullptr; |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 368 | |
Douglas Gregor | efc4695 | 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 | 7bbb20e | 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 | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 378 | void Parser::ParseLexedMethodDefs(ParsingClass &Class) { |
| 379 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 380 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 381 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); |
| 382 | if (HasTemplateScope) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 383 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 384 | ++CurTemplateDepthTracker; |
| 385 | } |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 386 | bool HasClassScope = !Class.TopLevelClass; |
| 387 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, |
| 388 | HasClassScope); |
| 389 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 390 | for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { |
| 391 | Class.LateParsedDeclarations[i]->ParseLexedMethodDefs(); |
| 392 | } |
| 393 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 394 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 395 | void Parser::ParseLexedMethodDef(LexedMethod &LM) { |
| 396 | // If this is a member template, introduce the template parameter scope. |
| 397 | ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 398 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); |
| 399 | if (LM.TemplateScope) { |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 400 | Actions.ActOnReenterTemplateScope(getCurScope(), LM.D); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 401 | ++CurTemplateDepthTracker; |
| 402 | } |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 403 | // Save the current token position. |
| 404 | SourceLocation origLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 0204197 | 2010-03-31 00:38:09 +0000 | [diff] [blame] | 405 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 406 | assert(!LM.Toks.empty() && "Empty body!"); |
| 407 | // Append the current token at the end of the new token stream so that it |
| 408 | // doesn't get lost. |
| 409 | LM.Toks.push_back(Tok); |
| 410 | PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 411 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 412 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | c36633c | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 413 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 414 | assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) |
| 415 | && "Inline method not starting with '{', ':' or 'try'"); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 416 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 417 | // Parse the method body. Function body parsing code is similar enough |
| 418 | // to be re-used for method bodies as well. |
| 419 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope); |
| 420 | Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 421 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 422 | if (Tok.is(tok::kw_try)) { |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 423 | ParseFunctionTryBlock(LM.D, FnScope); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 424 | assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, |
| 425 | Tok.getLocation()) && |
| 426 | "ParseFunctionTryBlock went over the cached tokens!"); |
| 427 | // There could be leftover tokens (e.g. because of an error). |
| 428 | // Skip through until we reach the original token position. |
| 429 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
| 430 | ConsumeAnyToken(); |
| 431 | return; |
| 432 | } |
| 433 | if (Tok.is(tok::colon)) { |
| 434 | ParseConstructorInitializer(LM.D); |
| 435 | |
| 436 | // Error recovery. |
| 437 | if (!Tok.is(tok::l_brace)) { |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 438 | FnScope.Exit(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 439 | Actions.ActOnFinishFunctionBody(LM.D, nullptr); |
Matt Beaumont-Gay | d045792 | 2011-09-23 22:39:23 +0000 | [diff] [blame] | 440 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
| 441 | ConsumeAnyToken(); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 442 | return; |
| 443 | } |
| 444 | } else |
| 445 | Actions.ActOnDefaultCtorInitializers(LM.D); |
| 446 | |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 447 | assert((Actions.getDiagnostics().hasErrorOccurred() || |
| 448 | !isa<FunctionTemplateDecl>(LM.D) || |
| 449 | cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth() |
| 450 | < TemplateParameterDepth) && |
| 451 | "TemplateParameterDepth should be greater than the depth of " |
| 452 | "current template being instantiated!"); |
| 453 | |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 454 | ParseFunctionStatementBody(LM.D, FnScope); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 455 | |
John McCall | e68672f | 2013-03-14 05:13:41 +0000 | [diff] [blame] | 456 | // Clear the late-template-parsed bit if we set it before. |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 457 | if (LM.D) |
| 458 | LM.D->getAsFunction()->setLateTemplateParsed(false); |
John McCall | e68672f | 2013-03-14 05:13:41 +0000 | [diff] [blame] | 459 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 460 | if (Tok.getLocation() != origLoc) { |
| 461 | // Due to parsing error, we either went over the cached tokens or |
| 462 | // there are still cached tokens left. If it's the latter case skip the |
| 463 | // leftover tokens. |
| 464 | // Since this is an uncommon situation that should be avoided, use the |
| 465 | // expensive isBeforeInTranslationUnit call. |
| 466 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 467 | origLoc)) |
Argyrios Kyrtzidis | e1224c8 | 2010-06-19 19:58:34 +0000 | [diff] [blame] | 468 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
Argyrios Kyrtzidis | e9b76af | 2010-06-17 10:52:22 +0000 | [diff] [blame] | 469 | ConsumeAnyToken(); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 470 | } |
Hans Wennborg | a926d84 | 2014-05-23 20:37:38 +0000 | [diff] [blame] | 471 | |
| 472 | if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(LM.D)) |
| 473 | Actions.ActOnFinishInlineMethodDef(MD); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 476 | /// ParseLexedMemberInitializers - We finished parsing the member specification |
| 477 | /// of a top (non-nested) C++ class. Now go over the stack of lexed data member |
| 478 | /// initializers that were collected during its parsing and parse them all. |
| 479 | void Parser::ParseLexedMemberInitializers(ParsingClass &Class) { |
| 480 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 481 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 482 | HasTemplateScope); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 483 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); |
| 484 | if (HasTemplateScope) { |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 485 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 486 | ++CurTemplateDepthTracker; |
| 487 | } |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 488 | // Set or update the scope flags. |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 489 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 490 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 491 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 492 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 493 | |
| 494 | if (!AlreadyHasClassScope) |
| 495 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 496 | Class.TagOrTemplate); |
| 497 | |
Benjamin Kramer | 1d373c6 | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 498 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 499 | // C++11 [expr.prim.general]p4: |
| 500 | // Otherwise, if a member-declarator declares a non-static data member |
| 501 | // (9.2) of a class X, the expression this is a prvalue of type "pointer |
| 502 | // to X" within the optional brace-or-equal-initializer. It shall not |
| 503 | // appear elsewhere in the member-declarator. |
| 504 | Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate, |
| 505 | /*TypeQuals=*/(unsigned)0); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 506 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 507 | for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { |
| 508 | Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers(); |
| 509 | } |
| 510 | } |
| 511 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 512 | if (!AlreadyHasClassScope) |
| 513 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 514 | Class.TagOrTemplate); |
| 515 | |
| 516 | Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate); |
| 517 | } |
| 518 | |
| 519 | void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) { |
Richard Smith | 1a526fd | 2011-09-29 19:42:27 +0000 | [diff] [blame] | 520 | if (!MI.Field || MI.Field->isInvalidDecl()) |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 521 | return; |
| 522 | |
| 523 | // Append the current token at the end of the new token stream so that it |
| 524 | // doesn't get lost. |
| 525 | MI.Toks.push_back(Tok); |
| 526 | PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false); |
| 527 | |
| 528 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | c36633c | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 529 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 530 | |
| 531 | SourceLocation EqualLoc; |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 532 | |
Richard Smith | 7410817 | 2014-01-17 03:11:34 +0000 | [diff] [blame] | 533 | Actions.ActOnStartCXXInClassMemberInitializer(); |
| 534 | |
Douglas Gregor | 926410d | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 535 | ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false, |
| 536 | EqualLoc); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 537 | |
Richard Smith | 7410817 | 2014-01-17 03:11:34 +0000 | [diff] [blame] | 538 | Actions.ActOnFinishCXXInClassMemberInitializer(MI.Field, EqualLoc, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 539 | Init.get()); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 540 | |
| 541 | // The next token should be our artificial terminating EOF token. |
| 542 | if (Tok.isNot(tok::eof)) { |
| 543 | SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation); |
| 544 | if (!EndLoc.isValid()) |
| 545 | EndLoc = Tok.getLocation(); |
| 546 | // No fixit; we can't recover as if there were a semicolon here. |
| 547 | Diag(EndLoc, diag::err_expected_semi_decl_list); |
| 548 | |
| 549 | // Consume tokens until we hit the artificial EOF. |
| 550 | while (Tok.isNot(tok::eof)) |
| 551 | ConsumeAnyToken(); |
| 552 | } |
| 553 | ConsumeAnyToken(); |
| 554 | } |
| 555 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 556 | /// ConsumeAndStoreUntil - Consume and store the token at the passed token |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 557 | /// container until the token 'T' is reached (which gets |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | /// consumed/stored too, if ConsumeFinalToken). |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 559 | /// If StopAtSemi is true, then we will stop early at a ';' character. |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 560 | /// Returns true if token 'T1' or 'T2' was found. |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 561 | /// NOTE: This is a specialized version of Parser::SkipUntil. |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 562 | bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2, |
| 563 | CachedTokens &Toks, |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 564 | bool StopAtSemi, bool ConsumeFinalToken) { |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 565 | // We always want this function to consume at least one token if the first |
| 566 | // token isn't T and if not at EOF. |
| 567 | bool isFirstTokenConsumed = true; |
| 568 | while (1) { |
| 569 | // If we found one of the tokens, stop and return true. |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 570 | if (Tok.is(T1) || Tok.is(T2)) { |
| 571 | if (ConsumeFinalToken) { |
| 572 | Toks.push_back(Tok); |
| 573 | ConsumeAnyToken(); |
| 574 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 575 | return true; |
| 576 | } |
| 577 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 578 | switch (Tok.getKind()) { |
| 579 | case tok::eof: |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 580 | case tok::annot_module_begin: |
| 581 | case tok::annot_module_end: |
| 582 | case tok::annot_module_include: |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 583 | // Ran out of tokens. |
| 584 | return false; |
| 585 | |
| 586 | case tok::l_paren: |
| 587 | // Recursively consume properly-nested parens. |
| 588 | Toks.push_back(Tok); |
| 589 | ConsumeParen(); |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 590 | ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 591 | break; |
| 592 | case tok::l_square: |
| 593 | // Recursively consume properly-nested square brackets. |
| 594 | Toks.push_back(Tok); |
| 595 | ConsumeBracket(); |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 596 | ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 597 | break; |
| 598 | case tok::l_brace: |
| 599 | // Recursively consume properly-nested braces. |
| 600 | Toks.push_back(Tok); |
| 601 | ConsumeBrace(); |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 602 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 603 | break; |
| 604 | |
| 605 | // Okay, we found a ']' or '}' or ')', which we think should be balanced. |
| 606 | // Since the user wasn't looking for this token (if they were, it would |
| 607 | // already be handled), this isn't balanced. If there is a LHS token at a |
| 608 | // higher level, we will assume that this matches the unbalanced token |
| 609 | // and return it. Otherwise, this is a spurious RHS token, which we skip. |
| 610 | case tok::r_paren: |
| 611 | if (ParenCount && !isFirstTokenConsumed) |
| 612 | return false; // Matches something. |
| 613 | Toks.push_back(Tok); |
| 614 | ConsumeParen(); |
| 615 | break; |
| 616 | case tok::r_square: |
| 617 | if (BracketCount && !isFirstTokenConsumed) |
| 618 | return false; // Matches something. |
| 619 | Toks.push_back(Tok); |
| 620 | ConsumeBracket(); |
| 621 | break; |
| 622 | case tok::r_brace: |
| 623 | if (BraceCount && !isFirstTokenConsumed) |
| 624 | return false; // Matches something. |
| 625 | Toks.push_back(Tok); |
| 626 | ConsumeBrace(); |
| 627 | break; |
| 628 | |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 629 | case tok::code_completion: |
| 630 | Toks.push_back(Tok); |
| 631 | ConsumeCodeCompletionToken(); |
| 632 | break; |
| 633 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 634 | case tok::string_literal: |
| 635 | case tok::wide_string_literal: |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 636 | case tok::utf8_string_literal: |
| 637 | case tok::utf16_string_literal: |
| 638 | case tok::utf32_string_literal: |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 639 | Toks.push_back(Tok); |
| 640 | ConsumeStringToken(); |
| 641 | break; |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 642 | case tok::semi: |
| 643 | if (StopAtSemi) |
| 644 | return false; |
| 645 | // FALL THROUGH. |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 646 | default: |
| 647 | // consume this token. |
| 648 | Toks.push_back(Tok); |
| 649 | ConsumeToken(); |
| 650 | break; |
| 651 | } |
| 652 | isFirstTokenConsumed = false; |
| 653 | } |
| 654 | } |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 655 | |
| 656 | /// \brief Consume tokens and store them in the passed token container until |
| 657 | /// we've passed the try keyword and constructor initializers and have consumed |
Sebastian Redl | 0d16401 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 658 | /// the opening brace of the function body. The opening brace will be consumed |
| 659 | /// if and only if there was no error. |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 660 | /// |
Richard Smith | cde3fd8 | 2013-07-04 00:13:48 +0000 | [diff] [blame] | 661 | /// \return True on error. |
Sebastian Redl | 0d16401 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 662 | bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) { |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 663 | if (Tok.is(tok::kw_try)) { |
| 664 | Toks.push_back(Tok); |
| 665 | ConsumeToken(); |
| 666 | } |
Richard Smith | cde3fd8 | 2013-07-04 00:13:48 +0000 | [diff] [blame] | 667 | |
| 668 | if (Tok.isNot(tok::colon)) { |
| 669 | // Easy case, just a function body. |
| 670 | |
| 671 | // Grab any remaining garbage to be diagnosed later. We stop when we reach a |
| 672 | // brace: an opening one is the function body, while a closing one probably |
| 673 | // means we've reached the end of the class. |
| 674 | ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks, |
| 675 | /*StopAtSemi=*/true, |
| 676 | /*ConsumeFinalToken=*/false); |
| 677 | if (Tok.isNot(tok::l_brace)) |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 678 | return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace; |
Richard Smith | cde3fd8 | 2013-07-04 00:13:48 +0000 | [diff] [blame] | 679 | |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 680 | Toks.push_back(Tok); |
Richard Smith | cde3fd8 | 2013-07-04 00:13:48 +0000 | [diff] [blame] | 681 | ConsumeBrace(); |
| 682 | return false; |
Eli Friedman | 7cd4a9b | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 683 | } |
Sebastian Redl | 0d16401 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 684 | |
| 685 | Toks.push_back(Tok); |
Richard Smith | cde3fd8 | 2013-07-04 00:13:48 +0000 | [diff] [blame] | 686 | ConsumeToken(); |
| 687 | |
| 688 | // We can't reliably skip over a mem-initializer-id, because it could be |
| 689 | // a template-id involving not-yet-declared names. Given: |
| 690 | // |
| 691 | // S ( ) : a < b < c > ( e ) |
| 692 | // |
| 693 | // 'e' might be an initializer or part of a template argument, depending |
| 694 | // on whether 'b' is a template. |
| 695 | |
| 696 | // Track whether we might be inside a template argument. We can give |
| 697 | // significantly better diagnostics if we know that we're not. |
| 698 | bool MightBeTemplateArgument = false; |
| 699 | |
| 700 | while (true) { |
| 701 | // Skip over the mem-initializer-id, if possible. |
| 702 | if (Tok.is(tok::kw_decltype)) { |
| 703 | Toks.push_back(Tok); |
| 704 | SourceLocation OpenLoc = ConsumeToken(); |
| 705 | if (Tok.isNot(tok::l_paren)) |
| 706 | return Diag(Tok.getLocation(), diag::err_expected_lparen_after) |
| 707 | << "decltype"; |
| 708 | Toks.push_back(Tok); |
| 709 | ConsumeParen(); |
| 710 | if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 711 | Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren; |
| 712 | Diag(OpenLoc, diag::note_matching) << tok::l_paren; |
Richard Smith | cde3fd8 | 2013-07-04 00:13:48 +0000 | [diff] [blame] | 713 | return true; |
| 714 | } |
| 715 | } |
| 716 | do { |
| 717 | // Walk over a component of a nested-name-specifier. |
| 718 | if (Tok.is(tok::coloncolon)) { |
| 719 | Toks.push_back(Tok); |
| 720 | ConsumeToken(); |
| 721 | |
| 722 | if (Tok.is(tok::kw_template)) { |
| 723 | Toks.push_back(Tok); |
| 724 | ConsumeToken(); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_template)) { |
| 729 | Toks.push_back(Tok); |
| 730 | ConsumeToken(); |
| 731 | } else if (Tok.is(tok::code_completion)) { |
| 732 | Toks.push_back(Tok); |
| 733 | ConsumeCodeCompletionToken(); |
| 734 | // Consume the rest of the initializers permissively. |
| 735 | // FIXME: We should be able to perform code-completion here even if |
| 736 | // there isn't a subsequent '{' token. |
| 737 | MightBeTemplateArgument = true; |
| 738 | break; |
| 739 | } else { |
| 740 | break; |
| 741 | } |
| 742 | } while (Tok.is(tok::coloncolon)); |
| 743 | |
| 744 | if (Tok.is(tok::less)) |
| 745 | MightBeTemplateArgument = true; |
| 746 | |
| 747 | if (MightBeTemplateArgument) { |
| 748 | // We may be inside a template argument list. Grab up to the start of the |
| 749 | // next parenthesized initializer or braced-init-list. This *might* be the |
| 750 | // initializer, or it might be a subexpression in the template argument |
| 751 | // list. |
| 752 | // FIXME: Count angle brackets, and clear MightBeTemplateArgument |
| 753 | // if all angles are closed. |
| 754 | if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks, |
| 755 | /*StopAtSemi=*/true, |
| 756 | /*ConsumeFinalToken=*/false)) { |
| 757 | // We're not just missing the initializer, we're also missing the |
| 758 | // function body! |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 759 | return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace; |
Richard Smith | cde3fd8 | 2013-07-04 00:13:48 +0000 | [diff] [blame] | 760 | } |
| 761 | } else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) { |
| 762 | // We found something weird in a mem-initializer-id. |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 763 | if (getLangOpts().CPlusPlus11) |
| 764 | return Diag(Tok.getLocation(), diag::err_expected_either) |
| 765 | << tok::l_paren << tok::l_brace; |
| 766 | else |
| 767 | return Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren; |
Richard Smith | cde3fd8 | 2013-07-04 00:13:48 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | tok::TokenKind kind = Tok.getKind(); |
| 771 | Toks.push_back(Tok); |
| 772 | bool IsLParen = (kind == tok::l_paren); |
| 773 | SourceLocation OpenLoc = Tok.getLocation(); |
| 774 | |
| 775 | if (IsLParen) { |
| 776 | ConsumeParen(); |
| 777 | } else { |
| 778 | assert(kind == tok::l_brace && "Must be left paren or brace here."); |
| 779 | ConsumeBrace(); |
| 780 | // In C++03, this has to be the start of the function body, which |
| 781 | // means the initializer is malformed; we'll diagnose it later. |
| 782 | if (!getLangOpts().CPlusPlus11) |
| 783 | return false; |
| 784 | } |
| 785 | |
| 786 | // Grab the initializer (or the subexpression of the template argument). |
| 787 | // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false |
| 788 | // if we might be inside the braces of a lambda-expression. |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 789 | tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace; |
| 790 | if (!ConsumeAndStoreUntil(CloseKind, Toks, /*StopAtSemi=*/true)) { |
| 791 | Diag(Tok, diag::err_expected) << CloseKind; |
| 792 | Diag(OpenLoc, diag::note_matching) << kind; |
Richard Smith | cde3fd8 | 2013-07-04 00:13:48 +0000 | [diff] [blame] | 793 | return true; |
| 794 | } |
| 795 | |
| 796 | // Grab pack ellipsis, if present. |
| 797 | if (Tok.is(tok::ellipsis)) { |
| 798 | Toks.push_back(Tok); |
| 799 | ConsumeToken(); |
| 800 | } |
| 801 | |
| 802 | // If we know we just consumed a mem-initializer, we must have ',' or '{' |
| 803 | // next. |
| 804 | if (Tok.is(tok::comma)) { |
| 805 | Toks.push_back(Tok); |
| 806 | ConsumeToken(); |
| 807 | } else if (Tok.is(tok::l_brace)) { |
| 808 | // This is the function body if the ')' or '}' is immediately followed by |
| 809 | // a '{'. That cannot happen within a template argument, apart from the |
| 810 | // case where a template argument contains a compound literal: |
| 811 | // |
| 812 | // S ( ) : a < b < c > ( d ) { } |
| 813 | // // End of declaration, or still inside the template argument? |
| 814 | // |
| 815 | // ... and the case where the template argument contains a lambda: |
| 816 | // |
| 817 | // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; } |
| 818 | // ( ) > ( ) { } |
| 819 | // |
| 820 | // FIXME: Disambiguate these cases. Note that the latter case is probably |
| 821 | // going to be made ill-formed by core issue 1607. |
| 822 | Toks.push_back(Tok); |
| 823 | ConsumeBrace(); |
| 824 | return false; |
| 825 | } else if (!MightBeTemplateArgument) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 826 | return Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace |
| 827 | << tok::comma; |
Richard Smith | cde3fd8 | 2013-07-04 00:13:48 +0000 | [diff] [blame] | 828 | } |
| 829 | } |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 830 | } |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 831 | |
| 832 | /// \brief Consume and store tokens from the '?' to the ':' in a conditional |
| 833 | /// expression. |
| 834 | bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) { |
| 835 | // Consume '?'. |
| 836 | assert(Tok.is(tok::question)); |
| 837 | Toks.push_back(Tok); |
| 838 | ConsumeToken(); |
| 839 | |
| 840 | while (Tok.isNot(tok::colon)) { |
Nico Weber | 77c76c5 | 2014-05-10 17:43:15 +0000 | [diff] [blame] | 841 | if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks, |
| 842 | /*StopAtSemi=*/true, |
| 843 | /*ConsumeFinalToken=*/false)) |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 844 | return false; |
| 845 | |
| 846 | // If we found a nested conditional, consume it. |
| 847 | if (Tok.is(tok::question) && !ConsumeAndStoreConditional(Toks)) |
| 848 | return false; |
| 849 | } |
| 850 | |
| 851 | // Consume ':'. |
| 852 | Toks.push_back(Tok); |
| 853 | ConsumeToken(); |
| 854 | return true; |
| 855 | } |
| 856 | |
| 857 | /// \brief A tentative parsing action that can also revert token annotations. |
| 858 | class Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction { |
| 859 | public: |
| 860 | explicit UnannotatedTentativeParsingAction(Parser &Self, |
| 861 | tok::TokenKind EndKind) |
| 862 | : TentativeParsingAction(Self), Self(Self), EndKind(EndKind) { |
| 863 | // Stash away the old token stream, so we can restore it once the |
| 864 | // tentative parse is complete. |
| 865 | TentativeParsingAction Inner(Self); |
| 866 | Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false); |
| 867 | Inner.Revert(); |
| 868 | } |
| 869 | |
| 870 | void RevertAnnotations() { |
| 871 | Revert(); |
| 872 | |
| 873 | // Put back the original tokens. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 874 | Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch); |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 875 | if (Toks.size()) { |
| 876 | Token *Buffer = new Token[Toks.size()]; |
| 877 | std::copy(Toks.begin() + 1, Toks.end(), Buffer); |
| 878 | Buffer[Toks.size() - 1] = Self.Tok; |
| 879 | Self.PP.EnterTokenStream(Buffer, Toks.size(), true, /*Owned*/true); |
| 880 | |
| 881 | Self.Tok = Toks.front(); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | private: |
| 886 | Parser &Self; |
| 887 | CachedTokens Toks; |
| 888 | tok::TokenKind EndKind; |
| 889 | }; |
| 890 | |
| 891 | /// ConsumeAndStoreInitializer - Consume and store the token at the passed token |
| 892 | /// container until the end of the current initializer expression (either a |
| 893 | /// default argument or an in-class initializer for a non-static data member). |
| 894 | /// The final token is not consumed. |
| 895 | bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks, |
| 896 | CachedInitKind CIK) { |
| 897 | // We always want this function to consume at least one token if not at EOF. |
| 898 | bool IsFirstTokenConsumed = true; |
| 899 | |
| 900 | // Number of possible unclosed <s we've seen so far. These might be templates, |
| 901 | // and might not, but if there were none of them (or we know for sure that |
| 902 | // we're within a template), we can avoid a tentative parse. |
| 903 | unsigned AngleCount = 0; |
| 904 | unsigned KnownTemplateCount = 0; |
| 905 | |
| 906 | while (1) { |
| 907 | switch (Tok.getKind()) { |
| 908 | case tok::comma: |
| 909 | // If we might be in a template, perform a tentative parse to check. |
| 910 | if (!AngleCount) |
| 911 | // Not a template argument: this is the end of the initializer. |
| 912 | return true; |
| 913 | if (KnownTemplateCount) |
| 914 | goto consume_token; |
| 915 | |
| 916 | // We hit a comma inside angle brackets. This is the hard case. The |
| 917 | // rule we follow is: |
| 918 | // * For a default argument, if the tokens after the comma form a |
| 919 | // syntactically-valid parameter-declaration-clause, in which each |
| 920 | // parameter has an initializer, then this comma ends the default |
| 921 | // argument. |
| 922 | // * For a default initializer, if the tokens after the comma form a |
| 923 | // syntactically-valid init-declarator-list, then this comma ends |
| 924 | // the default initializer. |
| 925 | { |
| 926 | UnannotatedTentativeParsingAction PA(*this, |
| 927 | CIK == CIK_DefaultInitializer |
| 928 | ? tok::semi : tok::r_paren); |
| 929 | Sema::TentativeAnalysisScope Scope(Actions); |
| 930 | |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 931 | TPResult Result = TPResult::Error; |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 932 | ConsumeToken(); |
| 933 | switch (CIK) { |
| 934 | case CIK_DefaultInitializer: |
| 935 | Result = TryParseInitDeclaratorList(); |
| 936 | // If we parsed a complete, ambiguous init-declarator-list, this |
| 937 | // is only syntactically-valid if it's followed by a semicolon. |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 938 | if (Result == TPResult::Ambiguous && Tok.isNot(tok::semi)) |
| 939 | Result = TPResult::False; |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 940 | break; |
| 941 | |
| 942 | case CIK_DefaultArgument: |
| 943 | bool InvalidAsDeclaration = false; |
| 944 | Result = TryParseParameterDeclarationClause( |
| 945 | &InvalidAsDeclaration, /*VersusTemplateArgument*/true); |
| 946 | // If this is an expression or a declaration with a missing |
| 947 | // 'typename', assume it's not a declaration. |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 948 | if (Result == TPResult::Ambiguous && InvalidAsDeclaration) |
| 949 | Result = TPResult::False; |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 950 | break; |
| 951 | } |
| 952 | |
| 953 | // If what follows could be a declaration, it is a declaration. |
Richard Smith | ee39043 | 2014-05-16 01:56:53 +0000 | [diff] [blame] | 954 | if (Result != TPResult::False && Result != TPResult::Error) { |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 955 | PA.Revert(); |
| 956 | return true; |
| 957 | } |
| 958 | |
| 959 | // In the uncommon case that we decide the following tokens are part |
| 960 | // of a template argument, revert any annotations we've performed in |
| 961 | // those tokens. We're not going to look them up until we've parsed |
| 962 | // the rest of the class, and that might add more declarations. |
| 963 | PA.RevertAnnotations(); |
| 964 | } |
| 965 | |
| 966 | // Keep going. We know we're inside a template argument list now. |
| 967 | ++KnownTemplateCount; |
| 968 | goto consume_token; |
| 969 | |
| 970 | case tok::eof: |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 971 | case tok::annot_module_begin: |
| 972 | case tok::annot_module_end: |
| 973 | case tok::annot_module_include: |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 974 | // Ran out of tokens. |
| 975 | return false; |
| 976 | |
| 977 | case tok::less: |
| 978 | // FIXME: A '<' can only start a template-id if it's preceded by an |
| 979 | // identifier, an operator-function-id, or a literal-operator-id. |
| 980 | ++AngleCount; |
| 981 | goto consume_token; |
| 982 | |
| 983 | case tok::question: |
| 984 | // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does, |
| 985 | // that is *never* the end of the initializer. Skip to the ':'. |
| 986 | if (!ConsumeAndStoreConditional(Toks)) |
| 987 | return false; |
| 988 | break; |
| 989 | |
| 990 | case tok::greatergreatergreater: |
| 991 | if (!getLangOpts().CPlusPlus11) |
| 992 | goto consume_token; |
| 993 | if (AngleCount) --AngleCount; |
| 994 | if (KnownTemplateCount) --KnownTemplateCount; |
| 995 | // Fall through. |
| 996 | case tok::greatergreater: |
| 997 | if (!getLangOpts().CPlusPlus11) |
| 998 | goto consume_token; |
| 999 | if (AngleCount) --AngleCount; |
| 1000 | if (KnownTemplateCount) --KnownTemplateCount; |
| 1001 | // Fall through. |
| 1002 | case tok::greater: |
| 1003 | if (AngleCount) --AngleCount; |
| 1004 | if (KnownTemplateCount) --KnownTemplateCount; |
| 1005 | goto consume_token; |
| 1006 | |
| 1007 | case tok::kw_template: |
| 1008 | // 'template' identifier '<' is known to start a template argument list, |
| 1009 | // and can be used to disambiguate the parse. |
| 1010 | // FIXME: Support all forms of 'template' unqualified-id '<'. |
| 1011 | Toks.push_back(Tok); |
| 1012 | ConsumeToken(); |
| 1013 | if (Tok.is(tok::identifier)) { |
| 1014 | Toks.push_back(Tok); |
| 1015 | ConsumeToken(); |
| 1016 | if (Tok.is(tok::less)) { |
Richard Smith | 9303357 | 2014-07-27 05:38:12 +0000 | [diff] [blame^] | 1017 | ++AngleCount; |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 1018 | ++KnownTemplateCount; |
| 1019 | Toks.push_back(Tok); |
| 1020 | ConsumeToken(); |
| 1021 | } |
| 1022 | } |
| 1023 | break; |
| 1024 | |
| 1025 | case tok::kw_operator: |
| 1026 | // If 'operator' precedes other punctuation, that punctuation loses |
| 1027 | // its special behavior. |
| 1028 | Toks.push_back(Tok); |
| 1029 | ConsumeToken(); |
| 1030 | switch (Tok.getKind()) { |
| 1031 | case tok::comma: |
| 1032 | case tok::greatergreatergreater: |
| 1033 | case tok::greatergreater: |
| 1034 | case tok::greater: |
| 1035 | case tok::less: |
| 1036 | Toks.push_back(Tok); |
| 1037 | ConsumeToken(); |
| 1038 | break; |
| 1039 | default: |
| 1040 | break; |
| 1041 | } |
| 1042 | break; |
| 1043 | |
| 1044 | case tok::l_paren: |
| 1045 | // Recursively consume properly-nested parens. |
| 1046 | Toks.push_back(Tok); |
| 1047 | ConsumeParen(); |
| 1048 | ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); |
| 1049 | break; |
| 1050 | case tok::l_square: |
| 1051 | // Recursively consume properly-nested square brackets. |
| 1052 | Toks.push_back(Tok); |
| 1053 | ConsumeBracket(); |
| 1054 | ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false); |
| 1055 | break; |
| 1056 | case tok::l_brace: |
| 1057 | // Recursively consume properly-nested braces. |
| 1058 | Toks.push_back(Tok); |
| 1059 | ConsumeBrace(); |
| 1060 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
| 1061 | break; |
| 1062 | |
| 1063 | // Okay, we found a ']' or '}' or ')', which we think should be balanced. |
| 1064 | // Since the user wasn't looking for this token (if they were, it would |
| 1065 | // already be handled), this isn't balanced. If there is a LHS token at a |
| 1066 | // higher level, we will assume that this matches the unbalanced token |
| 1067 | // and return it. Otherwise, this is a spurious RHS token, which we skip. |
| 1068 | case tok::r_paren: |
| 1069 | if (CIK == CIK_DefaultArgument) |
| 1070 | return true; // End of the default argument. |
| 1071 | if (ParenCount && !IsFirstTokenConsumed) |
| 1072 | return false; // Matches something. |
| 1073 | goto consume_token; |
| 1074 | case tok::r_square: |
| 1075 | if (BracketCount && !IsFirstTokenConsumed) |
| 1076 | return false; // Matches something. |
| 1077 | goto consume_token; |
| 1078 | case tok::r_brace: |
| 1079 | if (BraceCount && !IsFirstTokenConsumed) |
| 1080 | return false; // Matches something. |
| 1081 | goto consume_token; |
| 1082 | |
| 1083 | case tok::code_completion: |
| 1084 | Toks.push_back(Tok); |
| 1085 | ConsumeCodeCompletionToken(); |
| 1086 | break; |
| 1087 | |
| 1088 | case tok::string_literal: |
| 1089 | case tok::wide_string_literal: |
| 1090 | case tok::utf8_string_literal: |
| 1091 | case tok::utf16_string_literal: |
| 1092 | case tok::utf32_string_literal: |
| 1093 | Toks.push_back(Tok); |
| 1094 | ConsumeStringToken(); |
| 1095 | break; |
| 1096 | case tok::semi: |
| 1097 | if (CIK == CIK_DefaultInitializer) |
| 1098 | return true; // End of the default initializer. |
| 1099 | // FALL THROUGH. |
| 1100 | default: |
| 1101 | consume_token: |
| 1102 | Toks.push_back(Tok); |
| 1103 | ConsumeToken(); |
| 1104 | break; |
| 1105 | } |
| 1106 | IsFirstTokenConsumed = false; |
| 1107 | } |
| 1108 | } |