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" |
| 16 | #include "clang/Parse/DeclSpec.h" |
| 17 | #include "clang/Parse/Scope.h" |
| 18 | using namespace clang; |
| 19 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 20 | /// ParseCXXInlineMethodDef - We parsed and verified that the specified |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 21 | /// Declarator is a well formed C++ inline method definition. Now lex its body |
| 22 | /// and store its tokens for parsing after the C++ class is complete. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 23 | Parser::DeclPtrTy |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 24 | Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, Declarator &D, |
| 25 | const ParsedTemplateInfo &TemplateInfo) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 26 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 27 | "This isn't a function declarator!"); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 28 | assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) && |
| 29 | "Current token not a '{', ':' or 'try'!"); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 30 | |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 31 | Action::MultiTemplateParamsArg TemplateParams(Actions, |
Sean Hunt | 4cd8494 | 2010-04-14 23:07:37 +0000 | [diff] [blame] | 32 | TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0, |
| 33 | TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0); |
| 34 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 35 | DeclPtrTy FnD; |
| 36 | if (D.getDeclSpec().isFriendSpecified()) |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 37 | // FIXME: Friend templates |
Sean Hunt | 4cd8494 | 2010-04-14 23:07:37 +0000 | [diff] [blame] | 38 | FnD = Actions.ActOnFriendFunctionDecl(CurScope, D, true, |
| 39 | move(TemplateParams)); |
Douglas Gregor | 37b372b | 2009-08-20 22:52:58 +0000 | [diff] [blame] | 40 | else // FIXME: pass template information through |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | FnD = Actions.ActOnCXXMemberDeclarator(CurScope, AS, D, |
Sebastian Redl | d1a7846 | 2009-11-24 23:38:44 +0000 | [diff] [blame] | 42 | move(TemplateParams), 0, 0, |
| 43 | /*IsDefinition*/true); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 44 | |
Eli Friedman | d33133c | 2009-07-22 21:45:50 +0000 | [diff] [blame] | 45 | HandleMemberFunctionDefaultArgs(D, FnD); |
| 46 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 47 | // Consume the tokens and store them for later parsing. |
| 48 | |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 49 | getCurrentClass().MethodDefs.push_back(LexedMethod(FnD)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | getCurrentClass().MethodDefs.back().TemplateScope |
Douglas Gregor | d83d040 | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 51 | = CurScope->isTemplateParamScope(); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 52 | CachedTokens &Toks = getCurrentClass().MethodDefs.back().Toks; |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 53 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 54 | tok::TokenKind kind = Tok.getKind(); |
| 55 | // We may have a constructor initializer or function-try-block here. |
| 56 | if (kind == tok::colon || kind == tok::kw_try) { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 57 | // Consume everything up to (and including) the left brace. |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 58 | if (!ConsumeAndStoreUntil(tok::l_brace, Toks)) { |
Douglas Gregor | 3f08d18 | 2008-11-10 16:59:40 +0000 | [diff] [blame] | 59 | // We didn't find the left-brace we expected after the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | // constructor initializer. |
Douglas Gregor | 3f08d18 | 2008-11-10 16:59:40 +0000 | [diff] [blame] | 61 | if (Tok.is(tok::semi)) { |
| 62 | // We found a semicolon; complain, consume the semicolon, and |
| 63 | // don't try to parse this method later. |
| 64 | Diag(Tok.getLocation(), diag::err_expected_lbrace); |
| 65 | ConsumeAnyToken(); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 66 | getCurrentClass().MethodDefs.pop_back(); |
Douglas Gregor | 3f08d18 | 2008-11-10 16:59:40 +0000 | [diff] [blame] | 67 | return FnD; |
| 68 | } |
| 69 | } |
| 70 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 71 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | // Begin by storing the '{' token. |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 73 | Toks.push_back(Tok); |
| 74 | ConsumeBrace(); |
| 75 | } |
| 76 | // Consume everything up to (and including) the matching right brace. |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 77 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 78 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 79 | // If we're in a function-try-block, we need to store all the catch blocks. |
| 80 | if (kind == tok::kw_try) { |
| 81 | while (Tok.is(tok::kw_catch)) { |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 82 | ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); |
| 83 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 87 | return FnD; |
| 88 | } |
| 89 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 90 | /// ParseLexedMethodDeclarations - We finished parsing the member |
| 91 | /// specification of a top (non-nested) C++ class. Now go over the |
| 92 | /// stack of method declarations with some parts for which parsing was |
| 93 | /// delayed (such as default arguments) and parse them. |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 94 | void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) { |
| 95 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 96 | ParseScope TemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 97 | if (HasTemplateScope) |
| 98 | Actions.ActOnReenterTemplateScope(CurScope, Class.TagOrTemplate); |
| 99 | |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 100 | // The current scope is still active if we're the top-level class. |
| 101 | // Otherwise we'll need to push and enter a new scope. |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 102 | bool HasClassScope = !Class.TopLevelClass; |
Sean Hunt | 4cd8494 | 2010-04-14 23:07:37 +0000 | [diff] [blame] | 103 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, |
| 104 | HasClassScope); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 105 | if (HasClassScope) |
| 106 | Actions.ActOnStartDelayedMemberDeclarations(CurScope, Class.TagOrTemplate); |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 107 | |
| 108 | for (; !Class.MethodDecls.empty(); Class.MethodDecls.pop_front()) { |
| 109 | LateParsedMethodDeclaration &LM = Class.MethodDecls.front(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | |
Douglas Gregor | d83d040 | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 111 | // If this is a member template, introduce the template parameter scope. |
| 112 | ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); |
| 113 | if (LM.TemplateScope) |
| 114 | Actions.ActOnReenterTemplateScope(CurScope, LM.Method); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 116 | // Start the delayed C++ method declaration |
| 117 | Actions.ActOnStartDelayedCXXMethodDeclaration(CurScope, LM.Method); |
| 118 | |
| 119 | // Introduce the parameters into scope and parse their default |
| 120 | // arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | ParseScope PrototypeScope(this, |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 122 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 123 | for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) { |
| 124 | // Introduce the parameter into scope. |
| 125 | Actions.ActOnDelayedCXXMethodParameter(CurScope, LM.DefaultArgs[I].Param); |
| 126 | |
| 127 | if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) { |
Argyrios Kyrtzidis | 7fd3a64 | 2010-03-30 22:14:32 +0000 | [diff] [blame] | 128 | // Save the current token position. |
| 129 | SourceLocation origLoc = Tok.getLocation(); |
| 130 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 131 | // Parse the default argument from its saved token stream. |
| 132 | Toks->push_back(Tok); // So that the current token doesn't get lost |
| 133 | PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false); |
| 134 | |
| 135 | // Consume the previously-pushed token. |
| 136 | ConsumeAnyToken(); |
| 137 | |
| 138 | // Consume the '='. |
| 139 | assert(Tok.is(tok::equal) && "Default argument not starting with '='"); |
| 140 | SourceLocation EqualLoc = ConsumeToken(); |
| 141 | |
| 142 | OwningExprResult DefArgResult(ParseAssignmentExpression()); |
| 143 | if (DefArgResult.isInvalid()) |
| 144 | Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param); |
| 145 | else |
| 146 | Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 147 | move(DefArgResult)); |
Argyrios Kyrtzidis | 7fd3a64 | 2010-03-30 22:14:32 +0000 | [diff] [blame] | 148 | |
| 149 | assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, |
| 150 | Tok.getLocation()) && |
| 151 | "ParseAssignmentExpression went over the default arg tokens!"); |
| 152 | // There could be leftover tokens (e.g. because of an error). |
| 153 | // Skip through until we reach the original token position. |
Argyrios Kyrtzidis | 8f9359f | 2010-06-19 19:58:34 +0000 | [diff] [blame^] | 154 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
Argyrios Kyrtzidis | 7fd3a64 | 2010-03-30 22:14:32 +0000 | [diff] [blame] | 155 | ConsumeAnyToken(); |
| 156 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 157 | delete Toks; |
| 158 | LM.DefaultArgs[I].Toks = 0; |
| 159 | } |
| 160 | } |
| 161 | PrototypeScope.Exit(); |
| 162 | |
| 163 | // Finish the delayed C++ method declaration. |
| 164 | Actions.ActOnFinishDelayedCXXMethodDeclaration(CurScope, LM.Method); |
| 165 | } |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 166 | |
| 167 | for (unsigned I = 0, N = Class.NestedClasses.size(); I != N; ++I) |
| 168 | ParseLexedMethodDeclarations(*Class.NestedClasses[I]); |
John McCall | 7a1dc56 | 2009-12-19 10:49:29 +0000 | [diff] [blame] | 169 | |
| 170 | if (HasClassScope) |
| 171 | Actions.ActOnFinishDelayedMemberDeclarations(CurScope, Class.TagOrTemplate); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 174 | /// ParseLexedMethodDefs - We finished parsing the member specification of a top |
| 175 | /// (non-nested) C++ class. Now go over the stack of lexed methods that were |
| 176 | /// collected during its parsing and parse them all. |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 177 | void Parser::ParseLexedMethodDefs(ParsingClass &Class) { |
| 178 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 179 | ParseScope TemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 180 | if (HasTemplateScope) |
| 181 | Actions.ActOnReenterTemplateScope(CurScope, Class.TagOrTemplate); |
| 182 | |
| 183 | bool HasClassScope = !Class.TopLevelClass; |
| 184 | ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, |
| 185 | HasClassScope); |
| 186 | |
| 187 | for (; !Class.MethodDefs.empty(); Class.MethodDefs.pop_front()) { |
| 188 | LexedMethod &LM = Class.MethodDefs.front(); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 189 | |
Douglas Gregor | d83d040 | 2009-08-22 00:34:47 +0000 | [diff] [blame] | 190 | // If this is a member template, introduce the template parameter scope. |
| 191 | ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); |
| 192 | if (LM.TemplateScope) |
| 193 | Actions.ActOnReenterTemplateScope(CurScope, LM.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Argyrios Kyrtzidis | c50a5e0 | 2010-03-31 00:38:09 +0000 | [diff] [blame] | 195 | // Save the current token position. |
| 196 | SourceLocation origLoc = Tok.getLocation(); |
| 197 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 198 | assert(!LM.Toks.empty() && "Empty body!"); |
| 199 | // Append the current token at the end of the new token stream so that it |
| 200 | // doesn't get lost. |
| 201 | LM.Toks.push_back(Tok); |
Douglas Gregor | efd5bda | 2009-08-24 11:57:43 +0000 | [diff] [blame] | 202 | PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 203 | |
| 204 | // Consume the previously pushed token. |
| 205 | ConsumeAnyToken(); |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 206 | assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) |
| 207 | && "Inline method not starting with '{', ':' or 'try'"); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 208 | |
| 209 | // Parse the method body. Function body parsing code is similar enough |
| 210 | // to be re-used for method bodies as well. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 211 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 212 | Actions.ActOnStartOfFunctionDef(CurScope, LM.D); |
| 213 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 214 | if (Tok.is(tok::kw_try)) { |
| 215 | ParseFunctionTryBlock(LM.D); |
Argyrios Kyrtzidis | c50a5e0 | 2010-03-31 00:38:09 +0000 | [diff] [blame] | 216 | assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, |
| 217 | Tok.getLocation()) && |
| 218 | "ParseFunctionTryBlock went over the cached tokens!"); |
Argyrios Kyrtzidis | 7558cd0 | 2010-06-17 10:52:22 +0000 | [diff] [blame] | 219 | // There could be leftover tokens (e.g. because of an error). |
| 220 | // Skip through until we reach the original token position. |
Argyrios Kyrtzidis | 8f9359f | 2010-06-19 19:58:34 +0000 | [diff] [blame^] | 221 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
Argyrios Kyrtzidis | 7558cd0 | 2010-06-17 10:52:22 +0000 | [diff] [blame] | 222 | ConsumeAnyToken(); |
Sebastian Redl | de1b60a | 2009-04-26 21:08:36 +0000 | [diff] [blame] | 223 | continue; |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 224 | } |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 225 | if (Tok.is(tok::colon)) { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 226 | ParseConstructorInitializer(LM.D); |
John McCall | d6ca8da | 2010-04-10 07:37:23 +0000 | [diff] [blame] | 227 | |
| 228 | // Error recovery. |
| 229 | if (!Tok.is(tok::l_brace)) { |
| 230 | Actions.ActOnFinishFunctionBody(LM.D, Action::StmtArg(Actions)); |
| 231 | continue; |
| 232 | } |
| 233 | } else |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 234 | Actions.ActOnDefaultCtorInitializers(LM.D); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 40e9bc8 | 2009-03-05 00:49:17 +0000 | [diff] [blame] | 236 | ParseFunctionStatementBody(LM.D); |
Ted Kremenek | 252485e | 2010-06-17 00:59:17 +0000 | [diff] [blame] | 237 | |
Argyrios Kyrtzidis | 7558cd0 | 2010-06-17 10:52:22 +0000 | [diff] [blame] | 238 | if (Tok.getLocation() != origLoc) { |
| 239 | // Due to parsing error, we either went over the cached tokens or |
| 240 | // there are still cached tokens left. If it's the latter case skip the |
| 241 | // leftover tokens. |
| 242 | // Since this is an uncommon situation that should be avoided, use the |
| 243 | // expensive isBeforeInTranslationUnit call. |
| 244 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 245 | origLoc)) |
Argyrios Kyrtzidis | 8f9359f | 2010-06-19 19:58:34 +0000 | [diff] [blame^] | 246 | while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) |
Argyrios Kyrtzidis | 7558cd0 | 2010-06-17 10:52:22 +0000 | [diff] [blame] | 247 | ConsumeAnyToken(); |
| 248 | |
| 249 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 250 | } |
Douglas Gregor | 6569d68 | 2009-05-27 23:11:45 +0000 | [diff] [blame] | 251 | |
| 252 | for (unsigned I = 0, N = Class.NestedClasses.size(); I != N; ++I) |
| 253 | ParseLexedMethodDefs(*Class.NestedClasses[I]); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /// ConsumeAndStoreUntil - Consume and store the token at the passed token |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 257 | /// container until the token 'T' is reached (which gets |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 258 | /// consumed/stored too, if ConsumeFinalToken). |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 259 | /// If StopAtSemi is true, then we will stop early at a ';' character. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 260 | /// Returns true if token 'T1' or 'T2' was found. |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 261 | /// NOTE: This is a specialized version of Parser::SkipUntil. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 262 | bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2, |
| 263 | CachedTokens &Toks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 264 | bool StopAtSemi, bool ConsumeFinalToken) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 265 | // We always want this function to consume at least one token if the first |
| 266 | // token isn't T and if not at EOF. |
| 267 | bool isFirstTokenConsumed = true; |
| 268 | while (1) { |
| 269 | // If we found one of the tokens, stop and return true. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 270 | if (Tok.is(T1) || Tok.is(T2)) { |
| 271 | if (ConsumeFinalToken) { |
| 272 | Toks.push_back(Tok); |
| 273 | ConsumeAnyToken(); |
| 274 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 275 | return true; |
| 276 | } |
| 277 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 278 | switch (Tok.getKind()) { |
| 279 | case tok::eof: |
| 280 | // Ran out of tokens. |
| 281 | return false; |
| 282 | |
| 283 | case tok::l_paren: |
| 284 | // Recursively consume properly-nested parens. |
| 285 | Toks.push_back(Tok); |
| 286 | ConsumeParen(); |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 287 | ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 288 | break; |
| 289 | case tok::l_square: |
| 290 | // Recursively consume properly-nested square brackets. |
| 291 | Toks.push_back(Tok); |
| 292 | ConsumeBracket(); |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 293 | ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 294 | break; |
| 295 | case tok::l_brace: |
| 296 | // Recursively consume properly-nested braces. |
| 297 | Toks.push_back(Tok); |
| 298 | ConsumeBrace(); |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 299 | ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 300 | break; |
| 301 | |
| 302 | // Okay, we found a ']' or '}' or ')', which we think should be balanced. |
| 303 | // Since the user wasn't looking for this token (if they were, it would |
| 304 | // already be handled), this isn't balanced. If there is a LHS token at a |
| 305 | // higher level, we will assume that this matches the unbalanced token |
| 306 | // and return it. Otherwise, this is a spurious RHS token, which we skip. |
| 307 | case tok::r_paren: |
| 308 | if (ParenCount && !isFirstTokenConsumed) |
| 309 | return false; // Matches something. |
| 310 | Toks.push_back(Tok); |
| 311 | ConsumeParen(); |
| 312 | break; |
| 313 | case tok::r_square: |
| 314 | if (BracketCount && !isFirstTokenConsumed) |
| 315 | return false; // Matches something. |
| 316 | Toks.push_back(Tok); |
| 317 | ConsumeBracket(); |
| 318 | break; |
| 319 | case tok::r_brace: |
| 320 | if (BraceCount && !isFirstTokenConsumed) |
| 321 | return false; // Matches something. |
| 322 | Toks.push_back(Tok); |
| 323 | ConsumeBrace(); |
| 324 | break; |
| 325 | |
| 326 | case tok::string_literal: |
| 327 | case tok::wide_string_literal: |
| 328 | Toks.push_back(Tok); |
| 329 | ConsumeStringToken(); |
| 330 | break; |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 331 | case tok::semi: |
| 332 | if (StopAtSemi) |
| 333 | return false; |
| 334 | // FALL THROUGH. |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 335 | default: |
| 336 | // consume this token. |
| 337 | Toks.push_back(Tok); |
| 338 | ConsumeToken(); |
| 339 | break; |
| 340 | } |
| 341 | isFirstTokenConsumed = false; |
| 342 | } |
| 343 | } |