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