blob: 824847af63b09689aa97ffc94eb27d8a91ee8aaa [file] [log] [blame]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001//===--- 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 Gregor3f08d182008-11-10 16:59:40 +000014#include "clang/Basic/Diagnostic.h"
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000015#include "clang/Parse/Parser.h"
16#include "clang/Parse/DeclSpec.h"
17#include "clang/Parse/Scope.h"
18using 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.
23Parser::DeclTy *
24Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, Declarator &D) {
25 assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
26 "This isn't a function declarator!");
Douglas Gregor7ad83902008-11-05 04:29:56 +000027 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon)) &&
28 "Current token not a '{' or ':'!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000029
30 DeclTy *FnD = Actions.ActOnCXXMemberDeclarator(CurScope, AS, D, 0, 0, 0);
31
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000032 // Consume the tokens and store them for later parsing.
33
34 getCurTopClassStack().push(LexedMethod(FnD));
35 TokensTy &Toks = getCurTopClassStack().top().Toks;
36
Douglas Gregor7ad83902008-11-05 04:29:56 +000037 // We may have a constructor initializer here.
38 if (Tok.is(tok::colon)) {
39 // Consume everything up to (and including) the left brace.
Douglas Gregor3f08d182008-11-10 16:59:40 +000040 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 Gregor7ad83902008-11-05 04:29:56 +000053 } 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 Kyrtzidis4cc18a42008-06-24 22:12:16 +000059 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.
67void Parser::ParseLexedMethodDefs() {
Douglas Gregor8935b8b2008-12-10 06:34:36 +000068 for (; !getCurTopClassStack().empty(); getCurTopClassStack().pop()) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000069 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 Gregor7ad83902008-11-05 04:29:56 +000079 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon)) &&
80 "Inline method not starting with '{' or ':'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000081
82 // Parse the method body. Function body parsing code is similar enough
83 // to be re-used for method bodies as well.
Douglas Gregor8935b8b2008-12-10 06:34:36 +000084 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000085 Actions.ActOnStartOfFunctionDef(CurScope, LM.D);
86
Douglas Gregor7ad83902008-11-05 04:29:56 +000087 if (Tok.is(tok::colon))
88 ParseConstructorInitializer(LM.D);
89
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000090 ParseFunctionStatementBody(LM.D, Tok.getLocation(), Tok.getLocation());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000091 }
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 Gregor3f08d182008-11-10 16:59:40 +000096/// If EarlyAbortIf is specified, then we will stop early if we find that
97/// token at the top level.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000098/// Returns true if token 'T' was found.
99/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor3f08d182008-11-10 16:59:40 +0000100bool Parser::ConsumeAndStoreUntil(tok::TokenKind T, TokensTy &Toks,
101 tok::TokenKind EarlyAbortIf) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000102 // 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 Gregor3f08d182008-11-10 16:59:40 +0000113 // If we found the early-abort token, return.
114 if (Tok.is(EarlyAbortIf))
115 return false;
116
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000117 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}