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) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 58 | Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs, false, |
| 59 | true); |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 60 | bool TypeSpecContainsAuto = D.getDeclSpec().containsPlaceholderType(); |
Douglas Gregor | 50cefbf | 2011-10-17 17:09:53 +0000 | [diff] [blame] | 61 | if (Init.isUsable()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 62 | Actions.AddInitializerToDecl(FnD, Init.get(), false, |
Douglas Gregor | 728d00b | 2011-10-10 14:49:18 +0000 | [diff] [blame] | 63 | TypeSpecContainsAuto); |
| 64 | else |
| 65 | Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto); |
| 66 | } |
Nico Weber | 24b2a82 | 2011-01-28 06:07:34 +0000 | [diff] [blame] | 67 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 68 | |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 69 | HandleMemberFunctionDeclDelays(D, FnD); |
Eli Friedman | 3af2a77 | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 70 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 71 | D.complete(FnD); |
| 72 | |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 73 | if (Tok.is(tok::equal)) { |
| 74 | ConsumeToken(); |
| 75 | |
Richard Smith | 1c70473 | 2011-11-10 09:08:44 +0000 | [diff] [blame] | 76 | if (!FnD) { |
| 77 | SkipUntil(tok::semi); |
| 78 | return 0; |
| 79 | } |
| 80 | |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 81 | bool Delete = false; |
| 82 | SourceLocation KWLoc; |
| 83 | if (Tok.is(tok::kw_delete)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 84 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 85 | diag::warn_cxx98_compat_deleted_function : |
Richard Smith | e434590 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 86 | diag::ext_deleted_function); |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 87 | |
| 88 | KWLoc = ConsumeToken(); |
| 89 | Actions.SetDeclDeleted(FnD, KWLoc); |
| 90 | Delete = true; |
| 91 | } else if (Tok.is(tok::kw_default)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 92 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 93 | diag::warn_cxx98_compat_defaulted_function : |
Richard Smith | e434590 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 94 | diag::ext_defaulted_function); |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 95 | |
| 96 | KWLoc = ConsumeToken(); |
| 97 | Actions.SetDeclDefaulted(FnD, KWLoc); |
| 98 | } else { |
| 99 | llvm_unreachable("function definition after = not 'delete' or 'default'"); |
| 100 | } |
| 101 | |
| 102 | if (Tok.is(tok::comma)) { |
| 103 | Diag(KWLoc, diag::err_default_delete_in_multiple_declaration) |
| 104 | << Delete; |
| 105 | SkipUntil(tok::semi); |
| 106 | } else { |
| 107 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after, |
| 108 | Delete ? "delete" : "default", tok::semi); |
| 109 | } |
| 110 | |
| 111 | return FnD; |
| 112 | } |
| 113 | |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 114 | // In delayed template parsing mode, if we are within a class template |
| 115 | // or if we are about to parse function member template then consume |
| 116 | // the tokens and store them for parsing at the end of the translation unit. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 117 | if (getLangOpts().DelayedTemplateParsing && |
Douglas Gregor | 1edf576 | 2012-06-28 21:43:01 +0000 | [diff] [blame] | 118 | DefinitionKind == FDK_Definition && |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 119 | ((Actions.CurContext->isDependentContext() || |
| 120 | TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) && |
Francois Pichet | 6dc4c16 | 2011-11-18 23:47:17 +0000 | [diff] [blame] | 121 | !Actions.IsInsideALocalClassWithinATemplateFunction())) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 122 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame^] | 123 | CachedTokens Toks; |
| 124 | LexTemplateFunctionForLateParsing(Toks); |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 125 | |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame^] | 126 | if (FnD) { |
John McCall | e68672f | 2013-03-14 05:13:41 +0000 | [diff] [blame] | 127 | FunctionDecl *FD = getFunctionDecl(FnD); |
Chandler Carruth | bc0f9ae | 2011-04-25 07:09:43 +0000 | [diff] [blame] | 128 | Actions.CheckForFunctionRedefinition(FD); |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame^] | 129 | Actions.MarkAsLateParsedTemplate(FD, FnD, Toks); |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | return FnD; |
| 133 | } |
| 134 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 135 | // Consume the tokens and store them for later parsing. |
| 136 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 137 | LexedMethod* LM = new LexedMethod(this, FnD); |
| 138 | getCurrentClass().LateParsedDeclarations.push_back(LM); |
| 139 | LM->TemplateScope = getCurScope()->isTemplateParamScope(); |
| 140 | CachedTokens &Toks = LM->Toks; |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 141 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 142 | tok::TokenKind kind = Tok.getKind(); |
Sebastian Redl | 0d16401 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 143 | // Consume everything up to (and including) the left brace of the |
| 144 | // function body. |
| 145 | if (ConsumeAndStoreFunctionPrologue(Toks)) { |
| 146 | // We didn't find the left-brace we expected after the |
Eli Friedman | 7cd4a9b | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 147 | // constructor initializer; we already printed an error, and it's likely |
| 148 | // impossible to recover, so don't try to parse this method later. |
Richard Smith | cde3fd8 | 2013-07-04 00:13:48 +0000 | [diff] [blame] | 149 | // Skip over the rest of the decl and back to somewhere that looks |
| 150 | // reasonable. |
| 151 | SkipMalformedDecl(); |
Eli Friedman | 7cd4a9b | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 152 | delete getCurrentClass().LateParsedDeclarations.back(); |
| 153 | getCurrentClass().LateParsedDeclarations.pop_back(); |
| 154 | return FnD; |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 155 | } else { |
Sebastian Redl | 0d16401 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 156 | // Consume everything up to (and including) the matching right brace. |
| 157 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 158 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 159 | |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 160 | // If we're in a function-try-block, we need to store all the catch blocks. |
| 161 | if (kind == tok::kw_try) { |
| 162 | while (Tok.is(tok::kw_catch)) { |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 163 | ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); |
| 164 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Sebastian Redl | a7b98a7 | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Stephen Lin | 9354fc5 | 2013-06-23 07:37:13 +0000 | [diff] [blame] | 168 | if (FnD) { |
| 169 | // If this is a friend function, mark that it's late-parsed so that |
| 170 | // it's still known to be a definition even before we attach the |
| 171 | // parsed body. Sema needs to treat friend function definitions |
| 172 | // differently during template instantiation, and it's possible for |
| 173 | // the containing class to be instantiated before all its member |
| 174 | // function definitions are parsed. |
| 175 | // |
| 176 | // If you remove this, you can remove the code that clears the flag |
| 177 | // after parsing the member. |
| 178 | if (D.getDeclSpec().isFriendSpecified()) { |
| 179 | getFunctionDecl(FnD)->setLateTemplateParsed(true); |
| 180 | } |
| 181 | } else { |
Douglas Gregor | 6ca6410 | 2011-04-14 23:19:27 +0000 | [diff] [blame] | 182 | // If semantic analysis could not build a function declaration, |
| 183 | // just throw away the late-parsed declaration. |
| 184 | delete getCurrentClass().LateParsedDeclarations.back(); |
| 185 | getCurrentClass().LateParsedDeclarations.pop_back(); |
| 186 | } |
| 187 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 188 | return FnD; |
| 189 | } |
| 190 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 191 | /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the |
| 192 | /// specified Declarator is a well formed C++ non-static data member |
| 193 | /// declaration. Now lex its initializer and store its tokens for parsing |
| 194 | /// after the class is complete. |
| 195 | void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) { |
| 196 | assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) && |
| 197 | "Current token not a '{' or '='!"); |
| 198 | |
| 199 | LateParsedMemberInitializer *MI = |
| 200 | new LateParsedMemberInitializer(this, VarD); |
| 201 | getCurrentClass().LateParsedDeclarations.push_back(MI); |
| 202 | CachedTokens &Toks = MI->Toks; |
| 203 | |
| 204 | tok::TokenKind kind = Tok.getKind(); |
| 205 | if (kind == tok::equal) { |
| 206 | Toks.push_back(Tok); |
Douglas Gregor | 0cf55e9 | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 207 | ConsumeToken(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | if (kind == tok::l_brace) { |
| 211 | // Begin by storing the '{' token. |
| 212 | Toks.push_back(Tok); |
| 213 | ConsumeBrace(); |
| 214 | |
| 215 | // Consume everything up to (and including) the matching right brace. |
| 216 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true); |
| 217 | } else { |
| 218 | // Consume everything up to (but excluding) the comma or semicolon. |
| 219 | ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true, |
| 220 | /*ConsumeFinalToken=*/false); |
| 221 | } |
| 222 | |
| 223 | // Store an artificial EOF token to ensure that we don't run off the end of |
| 224 | // the initializer when we come to parse it. |
| 225 | Token Eof; |
| 226 | Eof.startToken(); |
| 227 | Eof.setKind(tok::eof); |
| 228 | Eof.setLocation(Tok.getLocation()); |
| 229 | Toks.push_back(Eof); |
| 230 | } |
| 231 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 232 | Parser::LateParsedDeclaration::~LateParsedDeclaration() {} |
| 233 | void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {} |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 234 | void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {} |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 235 | void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {} |
| 236 | |
| 237 | Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C) |
| 238 | : Self(P), Class(C) {} |
| 239 | |
| 240 | Parser::LateParsedClass::~LateParsedClass() { |
| 241 | Self->DeallocateParsedClasses(Class); |
| 242 | } |
| 243 | |
| 244 | void Parser::LateParsedClass::ParseLexedMethodDeclarations() { |
| 245 | Self->ParseLexedMethodDeclarations(*Class); |
| 246 | } |
| 247 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 248 | void Parser::LateParsedClass::ParseLexedMemberInitializers() { |
| 249 | Self->ParseLexedMemberInitializers(*Class); |
| 250 | } |
| 251 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 252 | void Parser::LateParsedClass::ParseLexedMethodDefs() { |
| 253 | Self->ParseLexedMethodDefs(*Class); |
| 254 | } |
| 255 | |
| 256 | void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() { |
| 257 | Self->ParseLexedMethodDeclaration(*this); |
| 258 | } |
| 259 | |
| 260 | void Parser::LexedMethod::ParseLexedMethodDefs() { |
| 261 | Self->ParseLexedMethodDef(*this); |
| 262 | } |
| 263 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 264 | void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() { |
| 265 | Self->ParseLexedMemberInitializer(*this); |
| 266 | } |
| 267 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 268 | /// ParseLexedMethodDeclarations - We finished parsing the member |
| 269 | /// specification of a top (non-nested) C++ class. Now go over the |
| 270 | /// stack of method declarations with some parts for which parsing was |
| 271 | /// delayed (such as default arguments) and parse them. |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 272 | void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) { |
| 273 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 274 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 275 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); |
| 276 | if (HasTemplateScope) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 277 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 278 | ++CurTemplateDepthTracker; |
| 279 | } |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 280 | |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 281 | // The current scope is still active if we're the top-level class. |
| 282 | // Otherwise we'll need to push and enter a new scope. |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 283 | bool HasClassScope = !Class.TopLevelClass; |
Alexis Hunt | 1deb972 | 2010-04-14 23:07:37 +0000 | [diff] [blame] | 284 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, |
| 285 | HasClassScope); |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 286 | if (HasClassScope) |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 287 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate); |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 288 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 289 | for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { |
| 290 | Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations(); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 291 | } |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 292 | |
John McCall | 6df5fef | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 293 | if (HasClassScope) |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 294 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 297 | void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) { |
| 298 | // If this is a member template, introduce the template parameter scope. |
| 299 | ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 300 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); |
| 301 | if (LM.TemplateScope) { |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 302 | Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 303 | ++CurTemplateDepthTracker; |
| 304 | } |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 305 | // Start the delayed C++ method declaration |
| 306 | Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method); |
| 307 | |
| 308 | // Introduce the parameters into scope and parse their default |
| 309 | // arguments. |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 310 | ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope | |
| 311 | Scope::FunctionDeclarationScope | Scope::DeclScope); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 312 | for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) { |
| 313 | // Introduce the parameter into scope. |
Douglas Gregor | 7fcbd90 | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 314 | Actions.ActOnDelayedCXXMethodParameter(getCurScope(), |
| 315 | LM.DefaultArgs[I].Param); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 316 | |
| 317 | if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) { |
| 318 | // Save the current token position. |
| 319 | SourceLocation origLoc = Tok.getLocation(); |
| 320 | |
| 321 | // Parse the default argument from its saved token stream. |
| 322 | Toks->push_back(Tok); // So that the current token doesn't get lost |
| 323 | PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false); |
| 324 | |
| 325 | // Consume the previously-pushed token. |
| 326 | ConsumeAnyToken(); |
| 327 | |
| 328 | // Consume the '='. |
| 329 | assert(Tok.is(tok::equal) && "Default argument not starting with '='"); |
| 330 | SourceLocation EqualLoc = ConsumeToken(); |
| 331 | |
| 332 | // The argument isn't actually potentially evaluated unless it is |
| 333 | // used. |
| 334 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | 7fcbd90 | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 335 | Sema::PotentiallyEvaluatedIfUsed, |
| 336 | LM.DefaultArgs[I].Param); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 337 | |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 338 | ExprResult DefArgResult; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 339 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 6db0b1b | 2012-03-20 21:24:03 +0000 | [diff] [blame] | 340 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 341 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 6db0b1b | 2012-03-20 21:24:03 +0000 | [diff] [blame] | 342 | } else |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 343 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 344 | if (DefArgResult.isInvalid()) |
| 345 | Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param); |
| 346 | else { |
| 347 | if (Tok.is(tok::cxx_defaultarg_end)) |
| 348 | ConsumeToken(); |
| 349 | else |
| 350 | Diag(Tok.getLocation(), diag::err_default_arg_unparsed); |
| 351 | Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc, |
| 352 | DefArgResult.take()); |
| 353 | } |
| 354 | |
| 355 | assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, |
| 356 | Tok.getLocation()) && |
| 357 | "ParseAssignmentExpression went over the default arg tokens!"); |
| 358 | // There could be leftover tokens (e.g. because of an error). |
| 359 | // Skip through until we reach the original token position. |
| 360 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
| 361 | ConsumeAnyToken(); |
| 362 | |
| 363 | delete Toks; |
| 364 | LM.DefaultArgs[I].Toks = 0; |
| 365 | } |
| 366 | } |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 367 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 368 | PrototypeScope.Exit(); |
| 369 | |
| 370 | // Finish the delayed C++ method declaration. |
| 371 | Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method); |
| 372 | } |
| 373 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 374 | /// ParseLexedMethodDefs - We finished parsing the member specification of a top |
| 375 | /// (non-nested) C++ class. Now go over the stack of lexed methods that were |
| 376 | /// collected during its parsing and parse them all. |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 377 | void Parser::ParseLexedMethodDefs(ParsingClass &Class) { |
| 378 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 379 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 380 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); |
| 381 | if (HasTemplateScope) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 382 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 383 | ++CurTemplateDepthTracker; |
| 384 | } |
Douglas Gregor | e44a2ad | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 385 | bool HasClassScope = !Class.TopLevelClass; |
| 386 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, |
| 387 | HasClassScope); |
| 388 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 389 | for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { |
| 390 | Class.LateParsedDeclarations[i]->ParseLexedMethodDefs(); |
| 391 | } |
| 392 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 393 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 394 | void Parser::ParseLexedMethodDef(LexedMethod &LM) { |
| 395 | // If this is a member template, introduce the template parameter scope. |
| 396 | ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 397 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); |
| 398 | if (LM.TemplateScope) { |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 399 | Actions.ActOnReenterTemplateScope(getCurScope(), LM.D); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 400 | ++CurTemplateDepthTracker; |
| 401 | } |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 402 | // Save the current token position. |
| 403 | SourceLocation origLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | 0204197 | 2010-03-31 00:38:09 +0000 | [diff] [blame] | 404 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 405 | assert(!LM.Toks.empty() && "Empty body!"); |
| 406 | // Append the current token at the end of the new token stream so that it |
| 407 | // doesn't get lost. |
| 408 | LM.Toks.push_back(Tok); |
| 409 | PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 410 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 411 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | c36633c | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 412 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 413 | assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) |
| 414 | && "Inline method not starting with '{', ':' or 'try'"); |
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 | // Parse the method body. Function body parsing code is similar enough |
| 417 | // to be re-used for method bodies as well. |
| 418 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope); |
| 419 | Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D); |
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 | if (Tok.is(tok::kw_try)) { |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 422 | ParseFunctionTryBlock(LM.D, FnScope); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 423 | assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, |
| 424 | Tok.getLocation()) && |
| 425 | "ParseFunctionTryBlock went over the cached tokens!"); |
| 426 | // There could be leftover tokens (e.g. because of an error). |
| 427 | // Skip through until we reach the original token position. |
| 428 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
| 429 | ConsumeAnyToken(); |
| 430 | return; |
| 431 | } |
| 432 | if (Tok.is(tok::colon)) { |
| 433 | ParseConstructorInitializer(LM.D); |
| 434 | |
| 435 | // Error recovery. |
| 436 | if (!Tok.is(tok::l_brace)) { |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 437 | FnScope.Exit(); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 438 | Actions.ActOnFinishFunctionBody(LM.D, 0); |
Matt Beaumont-Gay | d045792 | 2011-09-23 22:39:23 +0000 | [diff] [blame] | 439 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
| 440 | ConsumeAnyToken(); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 441 | return; |
| 442 | } |
| 443 | } else |
| 444 | Actions.ActOnDefaultCtorInitializers(LM.D); |
| 445 | |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 446 | assert((Actions.getDiagnostics().hasErrorOccurred() || |
| 447 | !isa<FunctionTemplateDecl>(LM.D) || |
| 448 | cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth() |
| 449 | < TemplateParameterDepth) && |
| 450 | "TemplateParameterDepth should be greater than the depth of " |
| 451 | "current template being instantiated!"); |
| 452 | |
Douglas Gregor | a0ff0c3 | 2011-03-16 17:05:57 +0000 | [diff] [blame] | 453 | ParseFunctionStatementBody(LM.D, FnScope); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 454 | |
John McCall | e68672f | 2013-03-14 05:13:41 +0000 | [diff] [blame] | 455 | // Clear the late-template-parsed bit if we set it before. |
| 456 | if (LM.D) getFunctionDecl(LM.D)->setLateTemplateParsed(false); |
| 457 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 458 | if (Tok.getLocation() != origLoc) { |
| 459 | // Due to parsing error, we either went over the cached tokens or |
| 460 | // there are still cached tokens left. If it's the latter case skip the |
| 461 | // leftover tokens. |
| 462 | // Since this is an uncommon situation that should be avoided, use the |
| 463 | // expensive isBeforeInTranslationUnit call. |
| 464 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 465 | origLoc)) |
Argyrios Kyrtzidis | e1224c8 | 2010-06-19 19:58:34 +0000 | [diff] [blame] | 466 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
Argyrios Kyrtzidis | e9b76af | 2010-06-17 10:52:22 +0000 | [diff] [blame] | 467 | ConsumeAnyToken(); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 471 | /// ParseLexedMemberInitializers - We finished parsing the member specification |
| 472 | /// of a top (non-nested) C++ class. Now go over the stack of lexed data member |
| 473 | /// initializers that were collected during its parsing and parse them all. |
| 474 | void Parser::ParseLexedMemberInitializers(ParsingClass &Class) { |
| 475 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 476 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 477 | HasTemplateScope); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 478 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); |
| 479 | if (HasTemplateScope) { |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 480 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
Richard Smith | c837895 | 2013-04-29 11:55:38 +0000 | [diff] [blame] | 481 | ++CurTemplateDepthTracker; |
| 482 | } |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 483 | // Set or update the scope flags. |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 484 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 485 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 486 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 487 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 488 | |
| 489 | if (!AlreadyHasClassScope) |
| 490 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 491 | Class.TagOrTemplate); |
| 492 | |
Benjamin Kramer | 1d373c6 | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 493 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 494 | // C++11 [expr.prim.general]p4: |
| 495 | // Otherwise, if a member-declarator declares a non-static data member |
| 496 | // (9.2) of a class X, the expression this is a prvalue of type "pointer |
| 497 | // to X" within the optional brace-or-equal-initializer. It shall not |
| 498 | // appear elsewhere in the member-declarator. |
| 499 | Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate, |
| 500 | /*TypeQuals=*/(unsigned)0); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 501 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 502 | for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { |
| 503 | Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers(); |
| 504 | } |
| 505 | } |
| 506 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 507 | if (!AlreadyHasClassScope) |
| 508 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 509 | Class.TagOrTemplate); |
| 510 | |
| 511 | Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate); |
| 512 | } |
| 513 | |
| 514 | void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) { |
Richard Smith | 1a526fd | 2011-09-29 19:42:27 +0000 | [diff] [blame] | 515 | if (!MI.Field || MI.Field->isInvalidDecl()) |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 516 | return; |
| 517 | |
| 518 | // Append the current token at the end of the new token stream so that it |
| 519 | // doesn't get lost. |
| 520 | MI.Toks.push_back(Tok); |
| 521 | PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false); |
| 522 | |
| 523 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | c36633c | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 524 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 525 | |
| 526 | SourceLocation EqualLoc; |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 527 | |
Douglas Gregor | 926410d | 2012-02-21 02:22:07 +0000 | [diff] [blame] | 528 | ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false, |
| 529 | EqualLoc); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 530 | |
| 531 | Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release()); |
| 532 | |
| 533 | // The next token should be our artificial terminating EOF token. |
| 534 | if (Tok.isNot(tok::eof)) { |
| 535 | SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation); |
| 536 | if (!EndLoc.isValid()) |
| 537 | EndLoc = Tok.getLocation(); |
| 538 | // No fixit; we can't recover as if there were a semicolon here. |
| 539 | Diag(EndLoc, diag::err_expected_semi_decl_list); |
| 540 | |
| 541 | // Consume tokens until we hit the artificial EOF. |
| 542 | while (Tok.isNot(tok::eof)) |
| 543 | ConsumeAnyToken(); |
| 544 | } |
| 545 | ConsumeAnyToken(); |
| 546 | } |
| 547 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 548 | /// ConsumeAndStoreUntil - Consume and store the token at the passed token |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 549 | /// container until the token 'T' is reached (which gets |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | /// consumed/stored too, if ConsumeFinalToken). |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 551 | /// If StopAtSemi is true, then we will stop early at a ';' character. |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 552 | /// Returns true if token 'T1' or 'T2' was found. |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 553 | /// NOTE: This is a specialized version of Parser::SkipUntil. |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 554 | bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2, |
| 555 | CachedTokens &Toks, |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 556 | bool StopAtSemi, bool ConsumeFinalToken) { |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 557 | // We always want this function to consume at least one token if the first |
| 558 | // token isn't T and if not at EOF. |
| 559 | bool isFirstTokenConsumed = true; |
| 560 | while (1) { |
| 561 | // If we found one of the tokens, stop and return true. |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 562 | if (Tok.is(T1) || Tok.is(T2)) { |
| 563 | if (ConsumeFinalToken) { |
| 564 | Toks.push_back(Tok); |
| 565 | ConsumeAnyToken(); |
| 566 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 567 | return true; |
| 568 | } |
| 569 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 570 | switch (Tok.getKind()) { |
| 571 | case tok::eof: |
| 572 | // Ran out of tokens. |
| 573 | return false; |
| 574 | |
| 575 | case tok::l_paren: |
| 576 | // Recursively consume properly-nested parens. |
| 577 | Toks.push_back(Tok); |
| 578 | ConsumeParen(); |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 579 | ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 580 | break; |
| 581 | case tok::l_square: |
| 582 | // Recursively consume properly-nested square brackets. |
| 583 | Toks.push_back(Tok); |
| 584 | ConsumeBracket(); |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 585 | ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 586 | break; |
| 587 | case tok::l_brace: |
| 588 | // Recursively consume properly-nested braces. |
| 589 | Toks.push_back(Tok); |
| 590 | ConsumeBrace(); |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 591 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 592 | break; |
| 593 | |
| 594 | // Okay, we found a ']' or '}' or ')', which we think should be balanced. |
| 595 | // Since the user wasn't looking for this token (if they were, it would |
| 596 | // already be handled), this isn't balanced. If there is a LHS token at a |
| 597 | // higher level, we will assume that this matches the unbalanced token |
| 598 | // and return it. Otherwise, this is a spurious RHS token, which we skip. |
| 599 | case tok::r_paren: |
| 600 | if (ParenCount && !isFirstTokenConsumed) |
| 601 | return false; // Matches something. |
| 602 | Toks.push_back(Tok); |
| 603 | ConsumeParen(); |
| 604 | break; |
| 605 | case tok::r_square: |
| 606 | if (BracketCount && !isFirstTokenConsumed) |
| 607 | return false; // Matches something. |
| 608 | Toks.push_back(Tok); |
| 609 | ConsumeBracket(); |
| 610 | break; |
| 611 | case tok::r_brace: |
| 612 | if (BraceCount && !isFirstTokenConsumed) |
| 613 | return false; // Matches something. |
| 614 | Toks.push_back(Tok); |
| 615 | ConsumeBrace(); |
| 616 | break; |
| 617 | |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 618 | case tok::code_completion: |
| 619 | Toks.push_back(Tok); |
| 620 | ConsumeCodeCompletionToken(); |
| 621 | break; |
| 622 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 623 | case tok::string_literal: |
| 624 | case tok::wide_string_literal: |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 625 | case tok::utf8_string_literal: |
| 626 | case tok::utf16_string_literal: |
| 627 | case tok::utf32_string_literal: |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 628 | Toks.push_back(Tok); |
| 629 | ConsumeStringToken(); |
| 630 | break; |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 631 | case tok::semi: |
| 632 | if (StopAtSemi) |
| 633 | return false; |
| 634 | // FALL THROUGH. |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 635 | default: |
| 636 | // consume this token. |
| 637 | Toks.push_back(Tok); |
| 638 | ConsumeToken(); |
| 639 | break; |
| 640 | } |
| 641 | isFirstTokenConsumed = false; |
| 642 | } |
| 643 | } |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 644 | |
| 645 | /// \brief Consume tokens and store them in the passed token container until |
| 646 | /// we've passed the try keyword and constructor initializers and have consumed |
Sebastian Redl | 0d16401 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 647 | /// the opening brace of the function body. The opening brace will be consumed |
| 648 | /// if and only if there was no error. |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 649 | /// |
Richard Smith | cde3fd8 | 2013-07-04 00:13:48 +0000 | [diff] [blame] | 650 | /// \return True on error. |
Sebastian Redl | 0d16401 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 651 | bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) { |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 652 | if (Tok.is(tok::kw_try)) { |
| 653 | Toks.push_back(Tok); |
| 654 | ConsumeToken(); |
| 655 | } |
Richard Smith | cde3fd8 | 2013-07-04 00:13:48 +0000 | [diff] [blame] | 656 | |
| 657 | if (Tok.isNot(tok::colon)) { |
| 658 | // Easy case, just a function body. |
| 659 | |
| 660 | // Grab any remaining garbage to be diagnosed later. We stop when we reach a |
| 661 | // brace: an opening one is the function body, while a closing one probably |
| 662 | // means we've reached the end of the class. |
| 663 | ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks, |
| 664 | /*StopAtSemi=*/true, |
| 665 | /*ConsumeFinalToken=*/false); |
| 666 | if (Tok.isNot(tok::l_brace)) |
| 667 | return Diag(Tok.getLocation(), diag::err_expected_lbrace); |
| 668 | |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 669 | Toks.push_back(Tok); |
Richard Smith | cde3fd8 | 2013-07-04 00:13:48 +0000 | [diff] [blame] | 670 | ConsumeBrace(); |
| 671 | return false; |
Eli Friedman | 7cd4a9b | 2012-02-22 04:49:04 +0000 | [diff] [blame] | 672 | } |
Sebastian Redl | 0d16401 | 2011-09-30 08:32:17 +0000 | [diff] [blame] | 673 | |
| 674 | Toks.push_back(Tok); |
Richard Smith | cde3fd8 | 2013-07-04 00:13:48 +0000 | [diff] [blame] | 675 | ConsumeToken(); |
| 676 | |
| 677 | // We can't reliably skip over a mem-initializer-id, because it could be |
| 678 | // a template-id involving not-yet-declared names. Given: |
| 679 | // |
| 680 | // S ( ) : a < b < c > ( e ) |
| 681 | // |
| 682 | // 'e' might be an initializer or part of a template argument, depending |
| 683 | // on whether 'b' is a template. |
| 684 | |
| 685 | // Track whether we might be inside a template argument. We can give |
| 686 | // significantly better diagnostics if we know that we're not. |
| 687 | bool MightBeTemplateArgument = false; |
| 688 | |
| 689 | while (true) { |
| 690 | // Skip over the mem-initializer-id, if possible. |
| 691 | if (Tok.is(tok::kw_decltype)) { |
| 692 | Toks.push_back(Tok); |
| 693 | SourceLocation OpenLoc = ConsumeToken(); |
| 694 | if (Tok.isNot(tok::l_paren)) |
| 695 | return Diag(Tok.getLocation(), diag::err_expected_lparen_after) |
| 696 | << "decltype"; |
| 697 | Toks.push_back(Tok); |
| 698 | ConsumeParen(); |
| 699 | if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) { |
| 700 | Diag(Tok.getLocation(), diag::err_expected_rparen); |
| 701 | Diag(OpenLoc, diag::note_matching) << "("; |
| 702 | return true; |
| 703 | } |
| 704 | } |
| 705 | do { |
| 706 | // Walk over a component of a nested-name-specifier. |
| 707 | if (Tok.is(tok::coloncolon)) { |
| 708 | Toks.push_back(Tok); |
| 709 | ConsumeToken(); |
| 710 | |
| 711 | if (Tok.is(tok::kw_template)) { |
| 712 | Toks.push_back(Tok); |
| 713 | ConsumeToken(); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_template)) { |
| 718 | Toks.push_back(Tok); |
| 719 | ConsumeToken(); |
| 720 | } else if (Tok.is(tok::code_completion)) { |
| 721 | Toks.push_back(Tok); |
| 722 | ConsumeCodeCompletionToken(); |
| 723 | // Consume the rest of the initializers permissively. |
| 724 | // FIXME: We should be able to perform code-completion here even if |
| 725 | // there isn't a subsequent '{' token. |
| 726 | MightBeTemplateArgument = true; |
| 727 | break; |
| 728 | } else { |
| 729 | break; |
| 730 | } |
| 731 | } while (Tok.is(tok::coloncolon)); |
| 732 | |
| 733 | if (Tok.is(tok::less)) |
| 734 | MightBeTemplateArgument = true; |
| 735 | |
| 736 | if (MightBeTemplateArgument) { |
| 737 | // We may be inside a template argument list. Grab up to the start of the |
| 738 | // next parenthesized initializer or braced-init-list. This *might* be the |
| 739 | // initializer, or it might be a subexpression in the template argument |
| 740 | // list. |
| 741 | // FIXME: Count angle brackets, and clear MightBeTemplateArgument |
| 742 | // if all angles are closed. |
| 743 | if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks, |
| 744 | /*StopAtSemi=*/true, |
| 745 | /*ConsumeFinalToken=*/false)) { |
| 746 | // We're not just missing the initializer, we're also missing the |
| 747 | // function body! |
| 748 | return Diag(Tok.getLocation(), diag::err_expected_lbrace); |
| 749 | } |
| 750 | } else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) { |
| 751 | // We found something weird in a mem-initializer-id. |
| 752 | return Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 |
| 753 | ? diag::err_expected_lparen_or_lbrace |
| 754 | : diag::err_expected_lparen); |
| 755 | } |
| 756 | |
| 757 | tok::TokenKind kind = Tok.getKind(); |
| 758 | Toks.push_back(Tok); |
| 759 | bool IsLParen = (kind == tok::l_paren); |
| 760 | SourceLocation OpenLoc = Tok.getLocation(); |
| 761 | |
| 762 | if (IsLParen) { |
| 763 | ConsumeParen(); |
| 764 | } else { |
| 765 | assert(kind == tok::l_brace && "Must be left paren or brace here."); |
| 766 | ConsumeBrace(); |
| 767 | // In C++03, this has to be the start of the function body, which |
| 768 | // means the initializer is malformed; we'll diagnose it later. |
| 769 | if (!getLangOpts().CPlusPlus11) |
| 770 | return false; |
| 771 | } |
| 772 | |
| 773 | // Grab the initializer (or the subexpression of the template argument). |
| 774 | // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false |
| 775 | // if we might be inside the braces of a lambda-expression. |
| 776 | if (!ConsumeAndStoreUntil(IsLParen ? tok::r_paren : tok::r_brace, |
| 777 | Toks, /*StopAtSemi=*/true)) { |
| 778 | Diag(Tok, IsLParen ? diag::err_expected_rparen : |
| 779 | diag::err_expected_rbrace); |
| 780 | Diag(OpenLoc, diag::note_matching) << (IsLParen ? "(" : "{"); |
| 781 | return true; |
| 782 | } |
| 783 | |
| 784 | // Grab pack ellipsis, if present. |
| 785 | if (Tok.is(tok::ellipsis)) { |
| 786 | Toks.push_back(Tok); |
| 787 | ConsumeToken(); |
| 788 | } |
| 789 | |
| 790 | // If we know we just consumed a mem-initializer, we must have ',' or '{' |
| 791 | // next. |
| 792 | if (Tok.is(tok::comma)) { |
| 793 | Toks.push_back(Tok); |
| 794 | ConsumeToken(); |
| 795 | } else if (Tok.is(tok::l_brace)) { |
| 796 | // This is the function body if the ')' or '}' is immediately followed by |
| 797 | // a '{'. That cannot happen within a template argument, apart from the |
| 798 | // case where a template argument contains a compound literal: |
| 799 | // |
| 800 | // S ( ) : a < b < c > ( d ) { } |
| 801 | // // End of declaration, or still inside the template argument? |
| 802 | // |
| 803 | // ... and the case where the template argument contains a lambda: |
| 804 | // |
| 805 | // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; } |
| 806 | // ( ) > ( ) { } |
| 807 | // |
| 808 | // FIXME: Disambiguate these cases. Note that the latter case is probably |
| 809 | // going to be made ill-formed by core issue 1607. |
| 810 | Toks.push_back(Tok); |
| 811 | ConsumeBrace(); |
| 812 | return false; |
| 813 | } else if (!MightBeTemplateArgument) { |
| 814 | return Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma); |
| 815 | } |
| 816 | } |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 817 | } |