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