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