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