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