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 | |
Douglas Gregor | 3f08d18 | 2008-11-10 16:59:40 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Diagnostic.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 | |
| 20 | /// ParseInlineCXXMethodDef - We parsed and verified that the specified |
| 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. |
| 23 | Parser::DeclTy * |
| 24 | Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, Declarator &D) { |
| 25 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 26 | "This isn't a function declarator!"); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 27 | assert((Tok.is(tok::l_brace) || Tok.is(tok::colon)) && |
| 28 | "Current token not a '{' or ':'!"); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 29 | |
| 30 | DeclTy *FnD = Actions.ActOnCXXMemberDeclarator(CurScope, AS, D, 0, 0, 0); |
| 31 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 32 | // Consume the tokens and store them for later parsing. |
| 33 | |
| 34 | getCurTopClassStack().push(LexedMethod(FnD)); |
| 35 | TokensTy &Toks = getCurTopClassStack().top().Toks; |
| 36 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 37 | // We may have a constructor initializer here. |
| 38 | if (Tok.is(tok::colon)) { |
| 39 | // Consume everything up to (and including) the left brace. |
Douglas Gregor | 3f08d18 | 2008-11-10 16:59:40 +0000 | [diff] [blame] | 40 | if (!ConsumeAndStoreUntil(tok::l_brace, Toks, tok::semi)) { |
| 41 | // We didn't find the left-brace we expected after the |
| 42 | // constructor initializer. |
| 43 | if (Tok.is(tok::semi)) { |
| 44 | // We found a semicolon; complain, consume the semicolon, and |
| 45 | // don't try to parse this method later. |
| 46 | Diag(Tok.getLocation(), diag::err_expected_lbrace); |
| 47 | ConsumeAnyToken(); |
| 48 | getCurTopClassStack().pop(); |
| 49 | return FnD; |
| 50 | } |
| 51 | } |
| 52 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 53 | } else { |
| 54 | // Begin by storing the '{' token. |
| 55 | Toks.push_back(Tok); |
| 56 | ConsumeBrace(); |
| 57 | } |
| 58 | // Consume everything up to (and including) the matching right brace. |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 59 | ConsumeAndStoreUntil(tok::r_brace, Toks); |
| 60 | |
| 61 | return FnD; |
| 62 | } |
| 63 | |
| 64 | /// ParseLexedMethodDefs - We finished parsing the member specification of a top |
| 65 | /// (non-nested) C++ class. Now go over the stack of lexed methods that were |
| 66 | /// collected during its parsing and parse them all. |
| 67 | void Parser::ParseLexedMethodDefs() { |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame^] | 68 | for (; !getCurTopClassStack().empty(); getCurTopClassStack().pop()) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 69 | LexedMethod &LM = getCurTopClassStack().top(); |
| 70 | |
| 71 | assert(!LM.Toks.empty() && "Empty body!"); |
| 72 | // Append the current token at the end of the new token stream so that it |
| 73 | // doesn't get lost. |
| 74 | LM.Toks.push_back(Tok); |
| 75 | PP.EnterTokenStream(&LM.Toks.front(), LM.Toks.size(), true, false); |
| 76 | |
| 77 | // Consume the previously pushed token. |
| 78 | ConsumeAnyToken(); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 79 | assert((Tok.is(tok::l_brace) || Tok.is(tok::colon)) && |
| 80 | "Inline method not starting with '{' or ':'"); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 81 | |
| 82 | // Parse the method body. Function body parsing code is similar enough |
| 83 | // to be re-used for method bodies as well. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame^] | 84 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 85 | Actions.ActOnStartOfFunctionDef(CurScope, LM.D); |
| 86 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 87 | if (Tok.is(tok::colon)) |
| 88 | ParseConstructorInitializer(LM.D); |
| 89 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 90 | ParseFunctionStatementBody(LM.D, Tok.getLocation(), Tok.getLocation()); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | /// ConsumeAndStoreUntil - Consume and store the token at the passed token |
| 95 | /// container until the token 'T' is reached (which gets consumed/stored too). |
Douglas Gregor | 3f08d18 | 2008-11-10 16:59:40 +0000 | [diff] [blame] | 96 | /// If EarlyAbortIf is specified, then we will stop early if we find that |
| 97 | /// token at the top level. |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 98 | /// Returns true if token 'T' was found. |
| 99 | /// NOTE: This is a specialized version of Parser::SkipUntil. |
Douglas Gregor | 3f08d18 | 2008-11-10 16:59:40 +0000 | [diff] [blame] | 100 | bool Parser::ConsumeAndStoreUntil(tok::TokenKind T, TokensTy &Toks, |
| 101 | tok::TokenKind EarlyAbortIf) { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 102 | // We always want this function to consume at least one token if the first |
| 103 | // token isn't T and if not at EOF. |
| 104 | bool isFirstTokenConsumed = true; |
| 105 | while (1) { |
| 106 | // If we found one of the tokens, stop and return true. |
| 107 | if (Tok.is(T)) { |
| 108 | Toks.push_back(Tok); |
| 109 | ConsumeAnyToken(); |
| 110 | return true; |
| 111 | } |
| 112 | |
Douglas Gregor | 3f08d18 | 2008-11-10 16:59:40 +0000 | [diff] [blame] | 113 | // If we found the early-abort token, return. |
| 114 | if (Tok.is(EarlyAbortIf)) |
| 115 | return false; |
| 116 | |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 117 | switch (Tok.getKind()) { |
| 118 | case tok::eof: |
| 119 | // Ran out of tokens. |
| 120 | return false; |
| 121 | |
| 122 | case tok::l_paren: |
| 123 | // Recursively consume properly-nested parens. |
| 124 | Toks.push_back(Tok); |
| 125 | ConsumeParen(); |
| 126 | ConsumeAndStoreUntil(tok::r_paren, Toks); |
| 127 | break; |
| 128 | case tok::l_square: |
| 129 | // Recursively consume properly-nested square brackets. |
| 130 | Toks.push_back(Tok); |
| 131 | ConsumeBracket(); |
| 132 | ConsumeAndStoreUntil(tok::r_square, Toks); |
| 133 | break; |
| 134 | case tok::l_brace: |
| 135 | // Recursively consume properly-nested braces. |
| 136 | Toks.push_back(Tok); |
| 137 | ConsumeBrace(); |
| 138 | ConsumeAndStoreUntil(tok::r_brace, Toks); |
| 139 | break; |
| 140 | |
| 141 | // Okay, we found a ']' or '}' or ')', which we think should be balanced. |
| 142 | // Since the user wasn't looking for this token (if they were, it would |
| 143 | // already be handled), this isn't balanced. If there is a LHS token at a |
| 144 | // higher level, we will assume that this matches the unbalanced token |
| 145 | // and return it. Otherwise, this is a spurious RHS token, which we skip. |
| 146 | case tok::r_paren: |
| 147 | if (ParenCount && !isFirstTokenConsumed) |
| 148 | return false; // Matches something. |
| 149 | Toks.push_back(Tok); |
| 150 | ConsumeParen(); |
| 151 | break; |
| 152 | case tok::r_square: |
| 153 | if (BracketCount && !isFirstTokenConsumed) |
| 154 | return false; // Matches something. |
| 155 | Toks.push_back(Tok); |
| 156 | ConsumeBracket(); |
| 157 | break; |
| 158 | case tok::r_brace: |
| 159 | if (BraceCount && !isFirstTokenConsumed) |
| 160 | return false; // Matches something. |
| 161 | Toks.push_back(Tok); |
| 162 | ConsumeBrace(); |
| 163 | break; |
| 164 | |
| 165 | case tok::string_literal: |
| 166 | case tok::wide_string_literal: |
| 167 | Toks.push_back(Tok); |
| 168 | ConsumeStringToken(); |
| 169 | break; |
| 170 | default: |
| 171 | // consume this token. |
| 172 | Toks.push_back(Tok); |
| 173 | ConsumeToken(); |
| 174 | break; |
| 175 | } |
| 176 | isFirstTokenConsumed = false; |
| 177 | } |
| 178 | } |