blob: 176cb35889d43c9ccadf84ce8068ae51d956abff [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson0c6139d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor1b7f8982008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregore37ac4f2008-04-13 21:30:24 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000018#include "clang/Parse/Scope.h"
Chris Lattnerbc8d5642008-12-18 01:12:00 +000019#include "ExtensionRAIIObject.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000020using namespace clang;
21
22/// ParseNamespace - We know that the current token is a namespace keyword. This
23/// may either be a top level namespace or a block-level namespace alias.
24///
25/// namespace-definition: [C++ 7.3: basic.namespace]
26/// named-namespace-definition
27/// unnamed-namespace-definition
28///
29/// unnamed-namespace-definition:
30/// 'namespace' attributes[opt] '{' namespace-body '}'
31///
32/// named-namespace-definition:
33/// original-namespace-definition
34/// extension-namespace-definition
35///
36/// original-namespace-definition:
37/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
38///
39/// extension-namespace-definition:
40/// 'namespace' original-namespace-name '{' namespace-body '}'
41///
42/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
43/// 'namespace' identifier '=' qualified-namespace-specifier ';'
44///
Chris Lattner97144fc2009-04-02 04:16:50 +000045Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
46 SourceLocation &DeclEnd) {
Chris Lattner04d66662007-10-09 17:33:22 +000047 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000048 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
49
50 SourceLocation IdentLoc;
51 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000052
53 Token attrTok;
Chris Lattner8f08cb72007-08-25 06:57:03 +000054
Chris Lattner04d66662007-10-09 17:33:22 +000055 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000056 Ident = Tok.getIdentifierInfo();
57 IdentLoc = ConsumeToken(); // eat the identifier.
58 }
59
60 // Read label attributes, if present.
Chris Lattnerb28317a2009-03-28 19:18:32 +000061 Action::AttrTy *AttrList = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000062 if (Tok.is(tok::kw___attribute)) {
63 attrTok = Tok;
64
Chris Lattner8f08cb72007-08-25 06:57:03 +000065 // FIXME: save these somewhere.
66 AttrList = ParseAttributes();
Douglas Gregor6a588dd2009-06-17 19:49:00 +000067 }
Chris Lattner8f08cb72007-08-25 06:57:03 +000068
Douglas Gregor6a588dd2009-06-17 19:49:00 +000069 if (Tok.is(tok::equal)) {
70 if (AttrList)
71 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
72
Chris Lattner97144fc2009-04-02 04:16:50 +000073 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000074 }
Anders Carlssonf67606a2009-03-28 04:07:16 +000075
Chris Lattner51448322009-03-29 14:02:43 +000076 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +000077 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000078 diag::err_expected_ident_lbrace);
79 return DeclPtrTy();
Chris Lattner8f08cb72007-08-25 06:57:03 +000080 }
81
Chris Lattner51448322009-03-29 14:02:43 +000082 SourceLocation LBrace = ConsumeBrace();
83
84 // Enter a scope for the namespace.
85 ParseScope NamespaceScope(this, Scope::DeclScope);
86
87 DeclPtrTy NamespcDecl =
88 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
89
90 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
91 PP.getSourceManager(),
92 "parsing namespace");
93
94 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
95 ParseExternalDeclaration();
96
97 // Leave the namespace scope.
98 NamespaceScope.Exit();
99
Chris Lattner97144fc2009-04-02 04:16:50 +0000100 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
101 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000102
Chris Lattner97144fc2009-04-02 04:16:50 +0000103 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000104 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000105}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000106
Anders Carlssonf67606a2009-03-28 04:07:16 +0000107/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
108/// alias definition.
109///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000110Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
111 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000112 IdentifierInfo *Alias,
113 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000114 assert(Tok.is(tok::equal) && "Not equal token");
115
116 ConsumeToken(); // eat the '='.
117
118 CXXScopeSpec SS;
119 // Parse (optional) nested-name-specifier.
120 ParseOptionalCXXScopeSpecifier(SS);
121
122 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
123 Diag(Tok, diag::err_expected_namespace_name);
124 // Skip to end of the definition and eat the ';'.
125 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000126 return DeclPtrTy();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000127 }
128
129 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000130 IdentifierInfo *Ident = Tok.getIdentifierInfo();
131 SourceLocation IdentLoc = ConsumeToken();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000132
133 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000134 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000135 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
136 "", tok::semi);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000137
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000138 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
139 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000140}
141
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000142/// ParseLinkage - We know that the current token is a string_literal
143/// and just before that, that extern was seen.
144///
145/// linkage-specification: [C++ 7.5p2: dcl.link]
146/// 'extern' string-literal '{' declaration-seq[opt] '}'
147/// 'extern' string-literal declaration
148///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000149Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000150 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000151 llvm::SmallVector<char, 8> LangBuffer;
152 // LangBuffer is guaranteed to be big enough.
153 LangBuffer.resize(Tok.getLength());
154 const char *LangBufPtr = &LangBuffer[0];
155 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
156
157 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000158
Douglas Gregor074149e2009-01-05 19:45:36 +0000159 ParseScope LinkageScope(this, Scope::DeclScope);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000160 DeclPtrTy LinkageSpec
Douglas Gregor074149e2009-01-05 19:45:36 +0000161 = Actions.ActOnStartLinkageSpecification(CurScope,
162 /*FIXME: */SourceLocation(),
163 Loc, LangBufPtr, StrSize,
164 Tok.is(tok::l_brace)? Tok.getLocation()
165 : SourceLocation());
166
167 if (Tok.isNot(tok::l_brace)) {
168 ParseDeclarationOrFunctionDefinition();
169 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
170 SourceLocation());
Douglas Gregorf44515a2008-12-16 22:23:02 +0000171 }
172
173 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000174 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000175 ParseExternalDeclaration();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000176 }
177
Douglas Gregorf44515a2008-12-16 22:23:02 +0000178 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000179 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000180}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000181
Douglas Gregorf780abc2008-12-30 03:27:21 +0000182/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
183/// using-directive. Assumes that current token is 'using'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000184Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
185 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000186 assert(Tok.is(tok::kw_using) && "Not using token");
187
188 // Eat 'using'.
189 SourceLocation UsingLoc = ConsumeToken();
190
Chris Lattner2f274772009-01-06 06:55:51 +0000191 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000192 // Next token after 'using' is 'namespace' so it must be using-directive
Chris Lattner97144fc2009-04-02 04:16:50 +0000193 return ParseUsingDirective(Context, UsingLoc, DeclEnd);
Chris Lattner2f274772009-01-06 06:55:51 +0000194
195 // Otherwise, it must be using-declaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000196 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000197}
198
199/// ParseUsingDirective - Parse C++ using-directive, assumes
200/// that current token is 'namespace' and 'using' was already parsed.
201///
202/// using-directive: [C++ 7.3.p4: namespace.udir]
203/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
204/// namespace-name ;
205/// [GNU] using-directive:
206/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
207/// namespace-name attributes[opt] ;
208///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000209Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000210 SourceLocation UsingLoc,
211 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000212 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
213
214 // Eat 'namespace'.
215 SourceLocation NamespcLoc = ConsumeToken();
216
217 CXXScopeSpec SS;
218 // Parse (optional) nested-name-specifier.
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000219 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000220
221 AttributeList *AttrList = 0;
222 IdentifierInfo *NamespcName = 0;
223 SourceLocation IdentLoc = SourceLocation();
224
225 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000226 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000227 Diag(Tok, diag::err_expected_namespace_name);
228 // If there was invalid namespace name, skip to end of decl, and eat ';'.
229 SkipUntil(tok::semi);
230 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattnerb28317a2009-03-28 19:18:32 +0000231 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000232 }
Chris Lattner823c44e2009-01-06 07:27:21 +0000233
234 // Parse identifier.
235 NamespcName = Tok.getIdentifierInfo();
236 IdentLoc = ConsumeToken();
237
238 // Parse (optional) attributes (most likely GNU strong-using extension).
239 if (Tok.is(tok::kw___attribute))
240 AttrList = ParseAttributes();
241
242 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000243 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000244 ExpectAndConsume(tok::semi,
245 AttrList ? diag::err_expected_semi_after_attribute_list :
246 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000247
248 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner823c44e2009-01-06 07:27:21 +0000249 IdentLoc, NamespcName, AttrList);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000250}
251
252/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
253/// 'using' was already seen.
254///
255/// using-declaration: [C++ 7.3.p3: namespace.udecl]
256/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000257/// unqualified-id
258/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000259///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000260Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000261 SourceLocation UsingLoc,
262 SourceLocation &DeclEnd) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000263 CXXScopeSpec SS;
264 bool IsTypeName;
265
266 // Ignore optional 'typename'.
267 if (Tok.is(tok::kw_typename)) {
268 ConsumeToken();
269 IsTypeName = true;
270 }
271 else
272 IsTypeName = false;
273
274 // Parse nested-name-specifier.
275 ParseOptionalCXXScopeSpecifier(SS);
276
277 AttributeList *AttrList = 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000278
279 // Check nested-name specifier.
280 if (SS.isInvalid()) {
281 SkipUntil(tok::semi);
282 return DeclPtrTy();
283 }
284 if (Tok.is(tok::annot_template_id)) {
285 Diag(Tok, diag::err_unexpected_template_spec_in_using);
286 SkipUntil(tok::semi);
287 return DeclPtrTy();
288 }
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000289
290 IdentifierInfo *TargetName = 0;
291 OverloadedOperatorKind Op = OO_None;
292 SourceLocation IdentLoc;
293
294 if (Tok.is(tok::kw_operator)) {
295 IdentLoc = Tok.getLocation();
296
297 Op = TryParseOperatorFunctionId();
298 if (!Op) {
299 // If there was an invalid operator, skip to end of decl, and eat ';'.
300 SkipUntil(tok::semi);
301 return DeclPtrTy();
302 }
303 } else if (Tok.is(tok::identifier)) {
304 // Parse identifier.
305 TargetName = Tok.getIdentifierInfo();
306 IdentLoc = ConsumeToken();
307 } else {
308 // FIXME: Use a better diagnostic here.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000309 Diag(Tok, diag::err_expected_ident_in_using);
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000310
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000311 // If there was invalid identifier, skip to end of decl, and eat ';'.
312 SkipUntil(tok::semi);
313 return DeclPtrTy();
314 }
315
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000316 // Parse (optional) attributes (most likely GNU strong-using extension).
317 if (Tok.is(tok::kw___attribute))
318 AttrList = ParseAttributes();
319
320 // Eat ';'.
321 DeclEnd = Tok.getLocation();
322 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
323 AttrList ? "attributes list" : "namespace name", tok::semi);
324
325 return Actions.ActOnUsingDeclaration(CurScope, UsingLoc, SS,
Anders Carlsson0c6139d2009-06-27 00:27:47 +0000326 IdentLoc, TargetName, Op,
327 AttrList, IsTypeName);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000328}
329
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000330/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
331///
332/// static_assert-declaration:
333/// static_assert ( constant-expression , string-literal ) ;
334///
Chris Lattner97144fc2009-04-02 04:16:50 +0000335Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000336 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
337 SourceLocation StaticAssertLoc = ConsumeToken();
338
339 if (Tok.isNot(tok::l_paren)) {
340 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000341 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000342 }
343
344 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000345
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000346 OwningExprResult AssertExpr(ParseConstantExpression());
347 if (AssertExpr.isInvalid()) {
348 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000349 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000350 }
351
Anders Carlssonad5f9602009-03-13 23:29:20 +0000352 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000353 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000354
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000355 if (Tok.isNot(tok::string_literal)) {
356 Diag(Tok, diag::err_expected_string_literal);
357 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000358 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000359 }
360
361 OwningExprResult AssertMessage(ParseStringLiteralExpression());
362 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000363 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000364
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000365 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000366
Chris Lattner97144fc2009-04-02 04:16:50 +0000367 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000368 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
369
Anders Carlssonad5f9602009-03-13 23:29:20 +0000370 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000371 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000372}
373
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000374/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
375///
376/// 'decltype' ( expression )
377///
378void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
379 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
380
381 SourceLocation StartLoc = ConsumeToken();
382 SourceLocation LParenLoc = Tok.getLocation();
383
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000384 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
385 "decltype")) {
386 SkipUntil(tok::r_paren);
387 return;
388 }
389
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000390 // Parse the expression
391
392 // C++0x [dcl.type.simple]p4:
393 // The operand of the decltype specifier is an unevaluated operand.
394 EnterExpressionEvaluationContext Unevaluated(Actions,
395 Action::Unevaluated);
396 OwningExprResult Result = ParseExpression();
397 if (Result.isInvalid()) {
398 SkipUntil(tok::r_paren);
399 return;
400 }
401
402 // Match the ')'
403 SourceLocation RParenLoc;
404 if (Tok.is(tok::r_paren))
405 RParenLoc = ConsumeParen();
406 else
407 MatchRHSPunctuation(tok::r_paren, LParenLoc);
408
409 if (RParenLoc.isInvalid())
410 return;
411
412 const char *PrevSpec = 0;
413 // Check for duplicate type specifiers (e.g. "int decltype(a)").
414 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
415 Result.release()))
416 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
417}
418
Douglas Gregor42a552f2008-11-05 20:51:48 +0000419/// ParseClassName - Parse a C++ class-name, which names a class. Note
420/// that we only check that the result names a type; semantic analysis
421/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000422/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000423/// found.
424///
425/// class-name: [C++ 9.1]
426/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000427/// simple-template-id
Douglas Gregor42a552f2008-11-05 20:51:48 +0000428///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000429Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000430 const CXXScopeSpec *SS,
431 bool DestrExpected) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000432 // Check whether we have a template-id that names a type.
433 if (Tok.is(tok::annot_template_id)) {
434 TemplateIdAnnotation *TemplateId
435 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000436 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000437 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000438
439 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
440 TypeTy *Type = Tok.getAnnotationValue();
441 EndLocation = Tok.getAnnotationEndLoc();
442 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000443
444 if (Type)
445 return Type;
446 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000447 }
448
449 // Fall through to produce an error below.
450 }
451
Douglas Gregor42a552f2008-11-05 20:51:48 +0000452 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000453 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000454 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000455 }
456
457 // We have an identifier; check whether it is actually a type.
Douglas Gregorb696ea32009-02-04 17:00:24 +0000458 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
459 Tok.getLocation(), CurScope, SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000460 if (!Type) {
Fariborz Jahaniand33c8682009-07-20 17:43:15 +0000461 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
462 : diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000463 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000464 }
465
466 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000467 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000468 return Type;
469}
470
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000471/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
472/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
473/// until we reach the start of a definition or see a token that
474/// cannot start a definition.
475///
476/// class-specifier: [C++ class]
477/// class-head '{' member-specification[opt] '}'
478/// class-head '{' member-specification[opt] '}' attributes[opt]
479/// class-head:
480/// class-key identifier[opt] base-clause[opt]
481/// class-key nested-name-specifier identifier base-clause[opt]
482/// class-key nested-name-specifier[opt] simple-template-id
483/// base-clause[opt]
484/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
485/// [GNU] class-key attributes[opt] nested-name-specifier
486/// identifier base-clause[opt]
487/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
488/// simple-template-id base-clause[opt]
489/// class-key:
490/// 'class'
491/// 'struct'
492/// 'union'
493///
494/// elaborated-type-specifier: [C++ dcl.type.elab]
495/// class-key ::[opt] nested-name-specifier[opt] identifier
496/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
497/// simple-template-id
498///
499/// Note that the C++ class-specifier and elaborated-type-specifier,
500/// together, subsume the C99 struct-or-union-specifier:
501///
502/// struct-or-union-specifier: [C99 6.7.2.1]
503/// struct-or-union identifier[opt] '{' struct-contents '}'
504/// struct-or-union identifier
505/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
506/// '}' attributes[opt]
507/// [GNU] struct-or-union attributes[opt] identifier
508/// struct-or-union:
509/// 'struct'
510/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000511void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
512 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000513 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000514 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000515 DeclSpec::TST TagType;
516 if (TagTokKind == tok::kw_struct)
517 TagType = DeclSpec::TST_struct;
518 else if (TagTokKind == tok::kw_class)
519 TagType = DeclSpec::TST_class;
520 else {
521 assert(TagTokKind == tok::kw_union && "Not a class specifier");
522 TagType = DeclSpec::TST_union;
523 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000524
525 AttributeList *Attr = 0;
526 // If attributes exist after tag, parse them.
527 if (Tok.is(tok::kw___attribute))
528 Attr = ParseAttributes();
529
Steve Narofff59e17e2008-12-24 20:59:21 +0000530 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000531 if (Tok.is(tok::kw___declspec))
532 Attr = ParseMicrosoftDeclSpec(Attr);
Steve Narofff59e17e2008-12-24 20:59:21 +0000533
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000534 // Parse the (optional) nested-name-specifier.
535 CXXScopeSpec SS;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000536 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS))
537 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000538 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000539
540 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000541 IdentifierInfo *Name = 0;
542 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000543 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000544 if (Tok.is(tok::identifier)) {
545 Name = Tok.getIdentifierInfo();
546 NameLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000547 } else if (Tok.is(tok::annot_template_id)) {
548 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
549 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000550
Douglas Gregorc45c2322009-03-31 00:43:58 +0000551 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000552 // The template-name in the simple-template-id refers to
553 // something other than a class template. Give an appropriate
554 // error message and skip to the ';'.
555 SourceRange Range(NameLoc);
556 if (SS.isNotEmpty())
557 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000558
Douglas Gregor39a8de12009-02-25 19:37:18 +0000559 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
560 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +0000561
Douglas Gregor39a8de12009-02-25 19:37:18 +0000562 DS.SetTypeSpecError();
563 SkipUntil(tok::semi, false, true);
564 TemplateId->Destroy();
565 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000566 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000567 }
568
569 // There are three options here. If we have 'struct foo;', then
570 // this is a forward declaration. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000571 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000572 // something like 'struct foo xyz', a reference.
John McCall0f434ec2009-07-31 02:45:11 +0000573 Action::TagUseKind TUK;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000574 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
John McCall0f434ec2009-07-31 02:45:11 +0000575 TUK = Action::TUK_Definition;
Anders Carlsson5dc2af12009-05-11 22:25:03 +0000576 else if (Tok.is(tok::semi) && !DS.isFriendSpecified())
John McCall0f434ec2009-07-31 02:45:11 +0000577 TUK = Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000578 else
John McCall0f434ec2009-07-31 02:45:11 +0000579 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000580
John McCall0f434ec2009-07-31 02:45:11 +0000581 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000582 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000583 Diag(StartLoc, diag::err_anon_type_definition)
584 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000585
586 // Skip the rest of this declarator, up until the comma or semicolon.
587 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000588
589 if (TemplateId)
590 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000591 return;
592 }
593
Douglas Gregorddc29e12009-02-06 22:42:48 +0000594 // Create the tag portion of the class or class template.
Douglas Gregor212e81c2009-03-25 00:13:59 +0000595 Action::DeclResult TagOrTempResult;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000596 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
597
John McCall0f434ec2009-07-31 02:45:11 +0000598 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000599 // to turn that template-id into a type.
600
Douglas Gregor402abb52009-05-28 23:31:59 +0000601 bool Owned = false;
John McCall0f434ec2009-07-31 02:45:11 +0000602 if (TemplateId && TUK != Action::TUK_Reference) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000603 // Explicit specialization, class template partial specialization,
604 // or explicit instantiation.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000605 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
606 TemplateId->getTemplateArgs(),
607 TemplateId->getTemplateArgIsType(),
608 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000609 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000610 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000611 // This is an explicit instantiation of a class template.
612 TagOrTempResult
613 = Actions.ActOnExplicitInstantiation(CurScope,
614 TemplateInfo.TemplateLoc,
615 TagType,
616 StartLoc,
617 SS,
618 TemplateTy::make(TemplateId->Template),
619 TemplateId->TemplateNameLoc,
620 TemplateId->LAngleLoc,
621 TemplateArgsPtr,
622 TemplateId->getTemplateArgLocations(),
623 TemplateId->RAngleLoc,
624 Attr);
625 } else {
626 // This is an explicit specialization or a class template
627 // partial specialization.
628 TemplateParameterLists FakedParamLists;
629
630 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
631 // This looks like an explicit instantiation, because we have
632 // something like
633 //
634 // template class Foo<X>
635 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000636 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000637 // meant to be an explicit specialization, but the user forgot
638 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000639 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000640
641 SourceLocation LAngleLoc
642 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
643 Diag(TemplateId->TemplateNameLoc,
644 diag::err_explicit_instantiation_with_definition)
645 << SourceRange(TemplateInfo.TemplateLoc)
646 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
647
648 // Create a fake template parameter list that contains only
649 // "template<>", so that we treat this construct as a class
650 // template specialization.
651 FakedParamLists.push_back(
652 Actions.ActOnTemplateParameterList(0, SourceLocation(),
653 TemplateInfo.TemplateLoc,
654 LAngleLoc,
655 0, 0,
656 LAngleLoc));
657 TemplateParams = &FakedParamLists;
658 }
659
660 // Build the class template specialization.
661 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000662 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000663 StartLoc, SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000664 TemplateTy::make(TemplateId->Template),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000665 TemplateId->TemplateNameLoc,
666 TemplateId->LAngleLoc,
667 TemplateArgsPtr,
668 TemplateId->getTemplateArgLocations(),
669 TemplateId->RAngleLoc,
670 Attr,
Douglas Gregorcc636682009-02-17 23:15:12 +0000671 Action::MultiTemplateParamsArg(Actions,
672 TemplateParams? &(*TemplateParams)[0] : 0,
673 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000674 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000675 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000676 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000677 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000678 // Explicit instantiation of a member of a class template
679 // specialization, e.g.,
680 //
681 // template struct Outer<int>::Inner;
682 //
683 TagOrTempResult
684 = Actions.ActOnExplicitInstantiation(CurScope,
685 TemplateInfo.TemplateLoc,
686 TagType, StartLoc, SS, Name,
687 NameLoc, Attr);
688 } else {
689 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000690 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000691 // FIXME: Diagnose this particular error.
692 }
693
694 // Declaration or definition of a class type
John McCall0f434ec2009-07-31 02:45:11 +0000695 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000696 Name, NameLoc, Attr, AS,
697 Action::MultiTemplateParamsArg(Actions,
698 TemplateParams? &(*TemplateParams)[0] : 0,
699 TemplateParams? TemplateParams->size() : 0),
700 Owned);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000701 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000702
703 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000704 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000705 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000706
707 // If there is a body, parse it and inform the actions module.
708 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000709 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000710 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000711 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000712 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall0f434ec2009-07-31 02:45:11 +0000713 else if (TUK == Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000714 // FIXME: Complain that we have a base-specifier list but no
715 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000716 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000717 }
718
719 const char *PrevSpec = 0;
Anders Carlsson66e99772009-05-11 22:27:47 +0000720 if (TagOrTempResult.isInvalid()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000721 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000722 return;
723 }
724
Anders Carlsson66e99772009-05-11 22:27:47 +0000725 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec,
Douglas Gregor402abb52009-05-28 23:31:59 +0000726 TagOrTempResult.get().getAs<void>(), Owned))
Chris Lattner1ab3b962008-11-18 07:48:38 +0000727 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Anders Carlssond4f551b2009-05-11 22:42:30 +0000728
729 if (DS.isFriendSpecified())
730 Actions.ActOnFriendDecl(CurScope, DS.getFriendSpecLoc(),
731 TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000732}
733
734/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
735///
736/// base-clause : [C++ class.derived]
737/// ':' base-specifier-list
738/// base-specifier-list:
739/// base-specifier '...'[opt]
740/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000741void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000742 assert(Tok.is(tok::colon) && "Not a base clause");
743 ConsumeToken();
744
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000745 // Build up an array of parsed base specifiers.
746 llvm::SmallVector<BaseTy *, 8> BaseInfo;
747
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000748 while (true) {
749 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000750 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000751 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000752 // Skip the rest of this base specifier, up until the comma or
753 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000754 SkipUntil(tok::comma, tok::l_brace, true, true);
755 } else {
756 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000757 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000758 }
759
760 // If the next token is a comma, consume it and keep reading
761 // base-specifiers.
762 if (Tok.isNot(tok::comma)) break;
763
764 // Consume the comma.
765 ConsumeToken();
766 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000767
768 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000769 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000770}
771
772/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
773/// one entry in the base class list of a class specifier, for example:
774/// class foo : public bar, virtual private baz {
775/// 'public bar' and 'virtual private baz' are each base-specifiers.
776///
777/// base-specifier: [C++ class.derived]
778/// ::[opt] nested-name-specifier[opt] class-name
779/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
780/// class-name
781/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
782/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000783Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000784 bool IsVirtual = false;
785 SourceLocation StartLoc = Tok.getLocation();
786
787 // Parse the 'virtual' keyword.
788 if (Tok.is(tok::kw_virtual)) {
789 ConsumeToken();
790 IsVirtual = true;
791 }
792
793 // Parse an (optional) access specifier.
794 AccessSpecifier Access = getAccessSpecifierIfPresent();
795 if (Access)
796 ConsumeToken();
797
798 // Parse the 'virtual' keyword (again!), in case it came after the
799 // access specifier.
800 if (Tok.is(tok::kw_virtual)) {
801 SourceLocation VirtualLoc = ConsumeToken();
802 if (IsVirtual) {
803 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000804 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000805 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000806 }
807
808 IsVirtual = true;
809 }
810
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000811 // Parse optional '::' and optional nested-name-specifier.
812 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000813 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000814
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000815 // The location of the base class itself.
816 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000817
818 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000819 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000820 TypeResult BaseType = ParseClassName(EndLocation, &SS);
821 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000822 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000823
824 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000825 SourceRange Range(StartLoc, EndLocation);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000826
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000827 // Notify semantic analysis that we have parsed a complete
828 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000829 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000830 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000831}
832
833/// getAccessSpecifierIfPresent - Determine whether the next token is
834/// a C++ access-specifier.
835///
836/// access-specifier: [C++ class.derived]
837/// 'private'
838/// 'protected'
839/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000840AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000841{
842 switch (Tok.getKind()) {
843 default: return AS_none;
844 case tok::kw_private: return AS_private;
845 case tok::kw_protected: return AS_protected;
846 case tok::kw_public: return AS_public;
847 }
848}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000849
Eli Friedmand33133c2009-07-22 21:45:50 +0000850void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
851 DeclPtrTy ThisDecl) {
852 // We just declared a member function. If this member function
853 // has any default arguments, we'll need to parse them later.
854 LateParsedMethodDeclaration *LateMethod = 0;
855 DeclaratorChunk::FunctionTypeInfo &FTI
856 = DeclaratorInfo.getTypeObject(0).Fun;
857 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
858 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
859 if (!LateMethod) {
860 // Push this method onto the stack of late-parsed method
861 // declarations.
862 getCurrentClass().MethodDecls.push_back(
863 LateParsedMethodDeclaration(ThisDecl));
864 LateMethod = &getCurrentClass().MethodDecls.back();
865
866 // Add all of the parameters prior to this one (they don't
867 // have default arguments).
868 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
869 for (unsigned I = 0; I < ParamIdx; ++I)
870 LateMethod->DefaultArgs.push_back(
871 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
872 }
873
874 // Add this parameter to the list of parameters (it or may
875 // not have a default argument).
876 LateMethod->DefaultArgs.push_back(
877 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
878 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
879 }
880 }
881}
882
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000883/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
884///
885/// member-declaration:
886/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
887/// function-definition ';'[opt]
888/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
889/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000890/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000891/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000892/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000893///
894/// member-declarator-list:
895/// member-declarator
896/// member-declarator-list ',' member-declarator
897///
898/// member-declarator:
899/// declarator pure-specifier[opt]
900/// declarator constant-initializer[opt]
901/// identifier[opt] ':' constant-expression
902///
Sebastian Redle2b68332009-04-12 17:16:29 +0000903/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000904/// '= 0'
905///
906/// constant-initializer:
907/// '=' constant-expression
908///
Chris Lattner682bf922009-03-29 16:50:03 +0000909void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000910 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +0000911 if (Tok.is(tok::kw_static_assert)) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000912 SourceLocation DeclEnd;
913 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000914 return;
915 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000916
Chris Lattner682bf922009-03-29 16:50:03 +0000917 if (Tok.is(tok::kw_template)) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000918 SourceLocation DeclEnd;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000919 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
920 AS);
Chris Lattner682bf922009-03-29 16:50:03 +0000921 return;
922 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000923
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000924 // Handle: member-declaration ::= '__extension__' member-declaration
925 if (Tok.is(tok::kw___extension__)) {
926 // __extension__ silences extension warnings in the subexpression.
927 ExtensionRAIIObject O(Diags); // Use RAII to do this.
928 ConsumeToken();
929 return ParseCXXClassMemberDeclaration(AS);
930 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000931
932 if (Tok.is(tok::kw_using)) {
933 // Eat 'using'.
934 SourceLocation UsingLoc = ConsumeToken();
935
936 if (Tok.is(tok::kw_namespace)) {
937 Diag(UsingLoc, diag::err_using_namespace_in_class);
938 SkipUntil(tok::semi, true, true);
939 }
940 else {
941 SourceLocation DeclEnd;
942 // Otherwise, it must be using-declaration.
943 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd);
944 }
945 return;
946 }
947
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000948 SourceLocation DSStart = Tok.getLocation();
949 // decl-specifier-seq:
950 // Parse the common declaration-specifiers piece.
951 DeclSpec DS;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000952 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000953
954 if (Tok.is(tok::semi)) {
955 ConsumeToken();
956 // C++ 9.2p7: The member-declarator-list can be omitted only after a
957 // class-specifier or an enum-specifier or in a friend declaration.
958 // FIXME: Friend declarations.
959 switch (DS.getTypeSpecType()) {
Chris Lattner682bf922009-03-29 16:50:03 +0000960 case DeclSpec::TST_struct:
961 case DeclSpec::TST_union:
962 case DeclSpec::TST_class:
963 case DeclSpec::TST_enum:
964 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
965 return;
966 default:
967 Diag(DSStart, diag::err_no_declarators);
968 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000969 }
970 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000971
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000972 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000973
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000974 if (Tok.isNot(tok::colon)) {
975 // Parse the first declarator.
976 ParseDeclarator(DeclaratorInfo);
977 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +0000978 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000979 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000980 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000981 if (Tok.is(tok::semi))
982 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000983 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000984 }
985
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000986 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000987 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +0000988 || (DeclaratorInfo.isFunctionDeclarator() &&
989 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000990 if (!DeclaratorInfo.isFunctionDeclarator()) {
991 Diag(Tok, diag::err_func_def_no_params);
992 ConsumeBrace();
993 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000994 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000995 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000996
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000997 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
998 Diag(Tok, diag::err_function_declared_typedef);
999 // This recovery skips the entire function body. It would be nice
1000 // to simply call ParseCXXInlineMethodDef() below, however Sema
1001 // assumes the declarator represents a function, not a typedef.
1002 ConsumeBrace();
1003 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001004 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001005 }
1006
Chris Lattner682bf922009-03-29 16:50:03 +00001007 ParseCXXInlineMethodDef(AS, DeclaratorInfo);
1008 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001009 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001010 }
1011
1012 // member-declarator-list:
1013 // member-declarator
1014 // member-declarator-list ',' member-declarator
1015
Chris Lattner682bf922009-03-29 16:50:03 +00001016 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001017 OwningExprResult BitfieldSize(Actions);
1018 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001019 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001020
1021 while (1) {
1022
1023 // member-declarator:
1024 // declarator pure-specifier[opt]
1025 // declarator constant-initializer[opt]
1026 // identifier[opt] ':' constant-expression
1027
1028 if (Tok.is(tok::colon)) {
1029 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001030 BitfieldSize = ParseConstantExpression();
1031 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001032 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001033 }
1034
1035 // pure-specifier:
1036 // '= 0'
1037 //
1038 // constant-initializer:
1039 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001040 //
1041 // defaulted/deleted function-definition:
1042 // '=' 'default' [TODO]
1043 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001044
1045 if (Tok.is(tok::equal)) {
1046 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001047 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1048 ConsumeToken();
1049 Deleted = true;
1050 } else {
1051 Init = ParseInitializer();
1052 if (Init.isInvalid())
1053 SkipUntil(tok::comma, true, true);
1054 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001055 }
1056
1057 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001058 if (Tok.is(tok::kw___attribute)) {
1059 SourceLocation Loc;
1060 AttributeList *AttrList = ParseAttributes(&Loc);
1061 DeclaratorInfo.AddAttributes(AttrList, Loc);
1062 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001063
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001064 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001065 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001066 // See Sema::ActOnCXXMemberDeclarator for details.
Chris Lattner682bf922009-03-29 16:50:03 +00001067 DeclPtrTy ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1068 DeclaratorInfo,
1069 BitfieldSize.release(),
Sebastian Redle2b68332009-04-12 17:16:29 +00001070 Init.release(),
1071 Deleted);
Chris Lattner682bf922009-03-29 16:50:03 +00001072 if (ThisDecl)
1073 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001074
Douglas Gregor72b505b2008-12-16 21:30:33 +00001075 if (DeclaratorInfo.isFunctionDeclarator() &&
1076 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
1077 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001078 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001079 }
1080
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001081 // If we don't have a comma, it is either the end of the list (a ';')
1082 // or an error, bail out.
1083 if (Tok.isNot(tok::comma))
1084 break;
1085
1086 // Consume the comma.
1087 ConsumeToken();
1088
1089 // Parse the next declarator.
1090 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001091 BitfieldSize = 0;
1092 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001093 Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001094
1095 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001096 if (Tok.is(tok::kw___attribute)) {
1097 SourceLocation Loc;
1098 AttributeList *AttrList = ParseAttributes(&Loc);
1099 DeclaratorInfo.AddAttributes(AttrList, Loc);
1100 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001101
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001102 if (Tok.isNot(tok::colon))
1103 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001104 }
1105
1106 if (Tok.is(tok::semi)) {
1107 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +00001108 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +00001109 DeclsInGroup.size());
1110 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001111 }
1112
1113 Diag(Tok, diag::err_expected_semi_decl_list);
1114 // Skip to end of block or statement
1115 SkipUntil(tok::r_brace, true, true);
1116 if (Tok.is(tok::semi))
1117 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001118 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001119}
1120
1121/// ParseCXXMemberSpecification - Parse the class definition.
1122///
1123/// member-specification:
1124/// member-declaration member-specification[opt]
1125/// access-specifier ':' member-specification[opt]
1126///
1127void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001128 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001129 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001130 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001131 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001132
Chris Lattner49f28ca2009-03-05 08:00:35 +00001133 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1134 PP.getSourceManager(),
1135 "parsing struct/union/class body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001136
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001137 SourceLocation LBraceLoc = ConsumeBrace();
1138
Douglas Gregor6569d682009-05-27 23:11:45 +00001139 // Determine whether this is a top-level (non-nested) class.
1140 bool TopLevelClass = ClassStack.empty() ||
1141 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001142
1143 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001144 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001145
Douglas Gregor6569d682009-05-27 23:11:45 +00001146 // Note that we are parsing a new (potentially-nested) class definition.
1147 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1148
Douglas Gregorddc29e12009-02-06 22:42:48 +00001149 if (TagDecl)
1150 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1151 else {
1152 SkipUntil(tok::r_brace, false, false);
1153 return;
1154 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001155
1156 // C++ 11p3: Members of a class defined with the keyword class are private
1157 // by default. Members of a class defined with the keywords struct or union
1158 // are public by default.
1159 AccessSpecifier CurAS;
1160 if (TagType == DeclSpec::TST_class)
1161 CurAS = AS_private;
1162 else
1163 CurAS = AS_public;
1164
1165 // While we still have something to read, read the member-declarations.
1166 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1167 // Each iteration of this loop reads one member-declaration.
1168
1169 // Check for extraneous top-level semicolon.
1170 if (Tok.is(tok::semi)) {
1171 Diag(Tok, diag::ext_extra_struct_semi);
1172 ConsumeToken();
1173 continue;
1174 }
1175
1176 AccessSpecifier AS = getAccessSpecifierIfPresent();
1177 if (AS != AS_none) {
1178 // Current token is a C++ access specifier.
1179 CurAS = AS;
1180 ConsumeToken();
1181 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1182 continue;
1183 }
1184
1185 // Parse all the comma separated declarators.
1186 ParseCXXClassMemberDeclaration(CurAS);
1187 }
1188
1189 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1190
1191 AttributeList *AttrList = 0;
1192 // If attributes exist after class contents, parse them.
1193 if (Tok.is(tok::kw___attribute))
1194 AttrList = ParseAttributes(); // FIXME: where should I put them?
1195
1196 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1197 LBraceLoc, RBraceLoc);
1198
1199 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1200 // complete within function bodies, default arguments,
1201 // exception-specifications, and constructor ctor-initializers (including
1202 // such things in nested classes).
1203 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001204 // FIXME: Only function bodies and constructor ctor-initializers are
1205 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001206 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001207 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001208 // are complete and we can parse the delayed portions of method
1209 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001210 ParseLexedMethodDeclarations(getCurrentClass());
1211 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001212 }
1213
1214 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001215 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001216 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001217
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001218 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001219}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001220
1221/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1222/// which explicitly initializes the members or base classes of a
1223/// class (C++ [class.base.init]). For example, the three initializers
1224/// after the ':' in the Derived constructor below:
1225///
1226/// @code
1227/// class Base { };
1228/// class Derived : Base {
1229/// int x;
1230/// float f;
1231/// public:
1232/// Derived(float f) : Base(), x(17), f(f) { }
1233/// };
1234/// @endcode
1235///
1236/// [C++] ctor-initializer:
1237/// ':' mem-initializer-list
1238///
1239/// [C++] mem-initializer-list:
1240/// mem-initializer
1241/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001242void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001243 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1244
1245 SourceLocation ColonLoc = ConsumeToken();
1246
1247 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
1248
1249 do {
1250 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001251 if (!MemInit.isInvalid())
1252 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001253
1254 if (Tok.is(tok::comma))
1255 ConsumeToken();
1256 else if (Tok.is(tok::l_brace))
1257 break;
1258 else {
1259 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001260 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001261 SkipUntil(tok::l_brace, true, true);
1262 break;
1263 }
1264 } while (true);
1265
1266 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001267 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001268}
1269
1270/// ParseMemInitializer - Parse a C++ member initializer, which is
1271/// part of a constructor initializer that explicitly initializes one
1272/// member or base class (C++ [class.base.init]). See
1273/// ParseConstructorInitializer for an example.
1274///
1275/// [C++] mem-initializer:
1276/// mem-initializer-id '(' expression-list[opt] ')'
1277///
1278/// [C++] mem-initializer-id:
1279/// '::'[opt] nested-name-specifier[opt] class-name
1280/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001281Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001282 // parse '::'[opt] nested-name-specifier[opt]
1283 CXXScopeSpec SS;
1284 ParseOptionalCXXScopeSpecifier(SS);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001285 TypeTy *TemplateTypeTy = 0;
1286 if (Tok.is(tok::annot_template_id)) {
1287 TemplateIdAnnotation *TemplateId
1288 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1289 if (TemplateId->Kind == TNK_Type_template) {
1290 AnnotateTemplateIdTokenAsType(&SS);
1291 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1292 TemplateTypeTy = Tok.getAnnotationValue();
1293 }
1294 // FIXME. May need to check for TNK_Dependent_template as well.
1295 }
1296 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001297 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001298 return true;
1299 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001300
Douglas Gregor7ad83902008-11-05 04:29:56 +00001301 // Get the identifier. This may be a member name or a class name,
1302 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001303 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001304 SourceLocation IdLoc = ConsumeToken();
1305
1306 // Parse the '('.
1307 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001308 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001309 return true;
1310 }
1311 SourceLocation LParenLoc = ConsumeParen();
1312
1313 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001314 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001315 CommaLocsTy CommaLocs;
1316 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1317 SkipUntil(tok::r_paren);
1318 return true;
1319 }
1320
1321 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1322
Fariborz Jahanian96174332009-07-01 19:21:19 +00001323 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1324 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001325 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001326 ArgExprs.size(), CommaLocs.data(),
1327 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001328}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001329
1330/// ParseExceptionSpecification - Parse a C++ exception-specification
1331/// (C++ [except.spec]).
1332///
Douglas Gregora4745612008-12-01 18:00:20 +00001333/// exception-specification:
1334/// 'throw' '(' type-id-list [opt] ')'
1335/// [MS] 'throw' '(' '...' ')'
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001336///
Douglas Gregora4745612008-12-01 18:00:20 +00001337/// type-id-list:
1338/// type-id
1339/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001340///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001341bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001342 llvm::SmallVector<TypeTy*, 2>
1343 &Exceptions,
1344 llvm::SmallVector<SourceRange, 2>
1345 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001346 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001347 assert(Tok.is(tok::kw_throw) && "expected throw");
1348
1349 SourceLocation ThrowLoc = ConsumeToken();
1350
1351 if (!Tok.is(tok::l_paren)) {
1352 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1353 }
1354 SourceLocation LParenLoc = ConsumeParen();
1355
Douglas Gregora4745612008-12-01 18:00:20 +00001356 // Parse throw(...), a Microsoft extension that means "this function
1357 // can throw anything".
1358 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001359 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001360 SourceLocation EllipsisLoc = ConsumeToken();
1361 if (!getLang().Microsoft)
1362 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001363 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001364 return false;
1365 }
1366
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001367 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001368 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001369 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001370 TypeResult Res(ParseTypeName(&Range));
1371 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001372 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001373 Ranges.push_back(Range);
1374 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001375 if (Tok.is(tok::comma))
1376 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001377 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001378 break;
1379 }
1380
Sebastian Redlab197ba2009-02-09 18:23:29 +00001381 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001382 return false;
1383}
Douglas Gregor6569d682009-05-27 23:11:45 +00001384
1385/// \brief We have just started parsing the definition of a new class,
1386/// so push that class onto our stack of classes that is currently
1387/// being parsed.
1388void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
1389 assert((TopLevelClass || !ClassStack.empty()) &&
1390 "Nested class without outer class");
1391 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1392}
1393
1394/// \brief Deallocate the given parsed class and all of its nested
1395/// classes.
1396void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1397 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1398 DeallocateParsedClasses(Class->NestedClasses[I]);
1399 delete Class;
1400}
1401
1402/// \brief Pop the top class of the stack of classes that are
1403/// currently being parsed.
1404///
1405/// This routine should be called when we have finished parsing the
1406/// definition of a class, but have not yet popped the Scope
1407/// associated with the class's definition.
1408///
1409/// \returns true if the class we've popped is a top-level class,
1410/// false otherwise.
1411void Parser::PopParsingClass() {
1412 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
1413
1414 ParsingClass *Victim = ClassStack.top();
1415 ClassStack.pop();
1416 if (Victim->TopLevelClass) {
1417 // Deallocate all of the nested classes of this class,
1418 // recursively: we don't need to keep any of this information.
1419 DeallocateParsedClasses(Victim);
1420 return;
1421 }
1422 assert(!ClassStack.empty() && "Missing top-level class?");
1423
1424 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1425 Victim->NestedClasses.empty()) {
1426 // The victim is a nested class, but we will not need to perform
1427 // any processing after the definition of this class since it has
1428 // no members whose handling was delayed. Therefore, we can just
1429 // remove this nested class.
1430 delete Victim;
1431 return;
1432 }
1433
1434 // This nested class has some members that will need to be processed
1435 // after the top-level class is completely defined. Therefore, add
1436 // it to the list of nested classes within its parent.
1437 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1438 ClassStack.top()->NestedClasses.push_back(Victim);
1439 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1440}