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