blob: 31926ce0f5cc02f6e7234a74f8c405eb70de1ba8 [file] [log] [blame]
Chris Lattnerf7b2e552007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattnerf7b2e552007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlssone8c36f22009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor696be932008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregorec93f442008-04-13 21:30:24 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattnerf7b2e552007-08-25 06:57:03 +000018#include "clang/Parse/Scope.h"
Chris Lattnerf3375de2008-12-18 01:12:00 +000019#include "ExtensionRAIIObject.h"
Chris Lattnerf7b2e552007-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 Lattner9802a0a2009-04-02 04:16:50 +000045Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
46 SourceLocation &DeclEnd) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000047 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnerf7b2e552007-08-25 06:57:03 +000048 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
49
50 SourceLocation IdentLoc;
51 IdentifierInfo *Ident = 0;
Douglas Gregorb6d226f2009-06-17 19:49:00 +000052
53 Token attrTok;
Chris Lattnerf7b2e552007-08-25 06:57:03 +000054
Chris Lattner34a01ad2007-10-09 17:33:22 +000055 if (Tok.is(tok::identifier)) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +000056 Ident = Tok.getIdentifierInfo();
57 IdentLoc = ConsumeToken(); // eat the identifier.
58 }
59
60 // Read label attributes, if present.
Chris Lattner5261d0c2009-03-28 19:18:32 +000061 Action::AttrTy *AttrList = 0;
Douglas Gregorb6d226f2009-06-17 19:49:00 +000062 if (Tok.is(tok::kw___attribute)) {
63 attrTok = Tok;
64
Chris Lattnerf7b2e552007-08-25 06:57:03 +000065 // FIXME: save these somewhere.
66 AttrList = ParseAttributes();
Douglas Gregorb6d226f2009-06-17 19:49:00 +000067 }
Chris Lattnerf7b2e552007-08-25 06:57:03 +000068
Douglas Gregorb6d226f2009-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 Lattner9802a0a2009-04-02 04:16:50 +000073 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregorb6d226f2009-06-17 19:49:00 +000074 }
Anders Carlssonf94cca22009-03-28 04:07:16 +000075
Chris Lattner872b0442009-03-29 14:02:43 +000076 if (Tok.isNot(tok::l_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +000077 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner872b0442009-03-29 14:02:43 +000078 diag::err_expected_ident_lbrace);
79 return DeclPtrTy();
Chris Lattnerf7b2e552007-08-25 06:57:03 +000080 }
81
Chris Lattner872b0442009-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 Lattner9802a0a2009-04-02 04:16:50 +0000100 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
101 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner872b0442009-03-29 14:02:43 +0000102
Chris Lattner9802a0a2009-04-02 04:16:50 +0000103 DeclEnd = RBraceLoc;
Chris Lattner872b0442009-03-29 14:02:43 +0000104 return NamespcDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000105}
Chris Lattner806a5f52008-01-12 07:05:38 +0000106
Anders Carlssonf94cca22009-03-28 04:07:16 +0000107/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
108/// alias definition.
109///
Anders Carlsson26de7882009-03-28 22:53:22 +0000110Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
111 SourceLocation AliasLoc,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000112 IdentifierInfo *Alias,
113 SourceLocation &DeclEnd) {
Anders Carlssonf94cca22009-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 Lattner5261d0c2009-03-28 19:18:32 +0000126 return DeclPtrTy();
Anders Carlssonf94cca22009-03-28 04:07:16 +0000127 }
128
129 // Parse identifier.
Anders Carlsson26de7882009-03-28 22:53:22 +0000130 IdentifierInfo *Ident = Tok.getIdentifierInfo();
131 SourceLocation IdentLoc = ConsumeToken();
Anders Carlssonf94cca22009-03-28 04:07:16 +0000132
133 // Eat the ';'.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000134 DeclEnd = Tok.getLocation();
Chris Lattnercb9057e2009-06-14 00:07:48 +0000135 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
136 "", tok::semi);
Anders Carlssonf94cca22009-03-28 04:07:16 +0000137
Anders Carlsson26de7882009-03-28 22:53:22 +0000138 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
139 SS, IdentLoc, Ident);
Anders Carlssonf94cca22009-03-28 04:07:16 +0000140}
141
Chris Lattner806a5f52008-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 Lattner5261d0c2009-03-28 19:18:32 +0000149Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) {
Douglas Gregor61818c52008-11-21 16:10:08 +0000150 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattner806a5f52008-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 Lattner806a5f52008-01-12 07:05:38 +0000158
Douglas Gregord8028382009-01-05 19:45:36 +0000159 ParseScope LinkageScope(this, Scope::DeclScope);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000160 DeclPtrTy LinkageSpec
Douglas Gregord8028382009-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 Gregorad17e372008-12-16 22:23:02 +0000171 }
172
173 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorad17e372008-12-16 22:23:02 +0000174 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregord8028382009-01-05 19:45:36 +0000175 ParseExternalDeclaration();
Chris Lattner806a5f52008-01-12 07:05:38 +0000176 }
177
Douglas Gregorad17e372008-12-16 22:23:02 +0000178 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregord8028382009-01-05 19:45:36 +0000179 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattner806a5f52008-01-12 07:05:38 +0000180}
Douglas Gregorec93f442008-04-13 21:30:24 +0000181
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000182/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
183/// using-directive. Assumes that current token is 'using'.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000184Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
185 SourceLocation &DeclEnd) {
Douglas Gregor5ff0ee52008-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 Lattner08ab4162009-01-06 06:55:51 +0000191 if (Tok.is(tok::kw_namespace))
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000192 // Next token after 'using' is 'namespace' so it must be using-directive
Chris Lattner9802a0a2009-04-02 04:16:50 +0000193 return ParseUsingDirective(Context, UsingLoc, DeclEnd);
Chris Lattner08ab4162009-01-06 06:55:51 +0000194
195 // Otherwise, it must be using-declaration.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000196 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregor5ff0ee52008-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 Lattner5261d0c2009-03-28 19:18:32 +0000209Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000210 SourceLocation UsingLoc,
211 SourceLocation &DeclEnd) {
Douglas Gregor5ff0ee52008-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 Lattnerd706dc82009-01-06 06:59:53 +0000219 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000220
221 AttributeList *AttrList = 0;
222 IdentifierInfo *NamespcName = 0;
223 SourceLocation IdentLoc = SourceLocation();
224
225 // Parse namespace-name.
Chris Lattner7898bf62009-01-06 07:27:21 +0000226 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregor5ff0ee52008-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 Lattner5261d0c2009-03-28 19:18:32 +0000231 return DeclPtrTy();
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000232 }
Chris Lattner7898bf62009-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 Lattner9802a0a2009-04-02 04:16:50 +0000243 DeclEnd = Tok.getLocation();
Chris Lattnercb9057e2009-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 Gregor5ff0ee52008-12-30 03:27:21 +0000247
248 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner7898bf62009-01-06 07:27:21 +0000249 IdentLoc, NamespcName, AttrList);
Douglas Gregor5ff0ee52008-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 Gregor683a1142009-06-20 00:51:54 +0000257/// unqualified-id
258/// 'using' :: unqualified-id
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000259///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000260Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000261 SourceLocation UsingLoc,
Anders Carlssone16b4fe2009-08-29 19:54:19 +0000262 SourceLocation &DeclEnd,
263 AccessSpecifier AS) {
Douglas Gregor683a1142009-06-20 00:51:54 +0000264 CXXScopeSpec SS;
265 bool IsTypeName;
266
267 // Ignore optional 'typename'.
268 if (Tok.is(tok::kw_typename)) {
269 ConsumeToken();
270 IsTypeName = true;
271 }
272 else
273 IsTypeName = false;
274
275 // Parse nested-name-specifier.
276 ParseOptionalCXXScopeSpecifier(SS);
277
278 AttributeList *AttrList = 0;
Douglas Gregor683a1142009-06-20 00:51:54 +0000279
280 // Check nested-name specifier.
281 if (SS.isInvalid()) {
282 SkipUntil(tok::semi);
283 return DeclPtrTy();
284 }
285 if (Tok.is(tok::annot_template_id)) {
Anders Carlsson66b082a2009-08-28 03:35:18 +0000286 // C++0x N2914 [namespace.udecl]p5:
287 // A using-declaration shall not name a template-id.
288 Diag(Tok, diag::err_using_decl_can_not_refer_to_template_spec);
Douglas Gregor683a1142009-06-20 00:51:54 +0000289 SkipUntil(tok::semi);
290 return DeclPtrTy();
291 }
Anders Carlssone8c36f22009-06-27 00:27:47 +0000292
293 IdentifierInfo *TargetName = 0;
294 OverloadedOperatorKind Op = OO_None;
295 SourceLocation IdentLoc;
296
297 if (Tok.is(tok::kw_operator)) {
298 IdentLoc = Tok.getLocation();
299
300 Op = TryParseOperatorFunctionId();
301 if (!Op) {
302 // If there was an invalid operator, skip to end of decl, and eat ';'.
303 SkipUntil(tok::semi);
304 return DeclPtrTy();
305 }
306 } else if (Tok.is(tok::identifier)) {
307 // Parse identifier.
308 TargetName = Tok.getIdentifierInfo();
309 IdentLoc = ConsumeToken();
310 } else {
311 // FIXME: Use a better diagnostic here.
Douglas Gregor683a1142009-06-20 00:51:54 +0000312 Diag(Tok, diag::err_expected_ident_in_using);
Anders Carlssone8c36f22009-06-27 00:27:47 +0000313
Douglas Gregor683a1142009-06-20 00:51:54 +0000314 // If there was invalid identifier, skip to end of decl, and eat ';'.
315 SkipUntil(tok::semi);
316 return DeclPtrTy();
317 }
318
Douglas Gregor683a1142009-06-20 00:51:54 +0000319 // Parse (optional) attributes (most likely GNU strong-using extension).
320 if (Tok.is(tok::kw___attribute))
321 AttrList = ParseAttributes();
322
323 // Eat ';'.
324 DeclEnd = Tok.getLocation();
325 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
326 AttrList ? "attributes list" : "namespace name", tok::semi);
327
Anders Carlssone16b4fe2009-08-29 19:54:19 +0000328 return Actions.ActOnUsingDeclaration(CurScope, AS, UsingLoc, SS,
Anders Carlssone8c36f22009-06-27 00:27:47 +0000329 IdentLoc, TargetName, Op,
330 AttrList, IsTypeName);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000331}
332
Anders Carlssonab041982009-03-11 16:27:10 +0000333/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
334///
335/// static_assert-declaration:
336/// static_assert ( constant-expression , string-literal ) ;
337///
Chris Lattner9802a0a2009-04-02 04:16:50 +0000338Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlssonab041982009-03-11 16:27:10 +0000339 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
340 SourceLocation StaticAssertLoc = ConsumeToken();
341
342 if (Tok.isNot(tok::l_paren)) {
343 Diag(Tok, diag::err_expected_lparen);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000344 return DeclPtrTy();
Anders Carlssonab041982009-03-11 16:27:10 +0000345 }
346
347 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor98189262009-06-19 23:52:42 +0000348
Anders Carlssonab041982009-03-11 16:27:10 +0000349 OwningExprResult AssertExpr(ParseConstantExpression());
350 if (AssertExpr.isInvalid()) {
351 SkipUntil(tok::semi);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000352 return DeclPtrTy();
Anders Carlssonab041982009-03-11 16:27:10 +0000353 }
354
Anders Carlssona24e8d52009-03-13 23:29:20 +0000355 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000356 return DeclPtrTy();
Anders Carlssona24e8d52009-03-13 23:29:20 +0000357
Anders Carlssonab041982009-03-11 16:27:10 +0000358 if (Tok.isNot(tok::string_literal)) {
359 Diag(Tok, diag::err_expected_string_literal);
360 SkipUntil(tok::semi);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000361 return DeclPtrTy();
Anders Carlssonab041982009-03-11 16:27:10 +0000362 }
363
364 OwningExprResult AssertMessage(ParseStringLiteralExpression());
365 if (AssertMessage.isInvalid())
Chris Lattner5261d0c2009-03-28 19:18:32 +0000366 return DeclPtrTy();
Anders Carlssonab041982009-03-11 16:27:10 +0000367
Anders Carlssonc45057a2009-03-15 18:44:04 +0000368 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonab041982009-03-11 16:27:10 +0000369
Chris Lattner9802a0a2009-04-02 04:16:50 +0000370 DeclEnd = Tok.getLocation();
Anders Carlssonab041982009-03-11 16:27:10 +0000371 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
372
Anders Carlssona24e8d52009-03-13 23:29:20 +0000373 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlssonc45057a2009-03-15 18:44:04 +0000374 move(AssertMessage));
Anders Carlssonab041982009-03-11 16:27:10 +0000375}
376
Anders Carlssoneed418b2009-06-24 17:47:40 +0000377/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
378///
379/// 'decltype' ( expression )
380///
381void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
382 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
383
384 SourceLocation StartLoc = ConsumeToken();
385 SourceLocation LParenLoc = Tok.getLocation();
386
Anders Carlssoneed418b2009-06-24 17:47:40 +0000387 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
388 "decltype")) {
389 SkipUntil(tok::r_paren);
390 return;
391 }
392
Anders Carlssoneed418b2009-06-24 17:47:40 +0000393 // Parse the expression
394
395 // C++0x [dcl.type.simple]p4:
396 // The operand of the decltype specifier is an unevaluated operand.
397 EnterExpressionEvaluationContext Unevaluated(Actions,
398 Action::Unevaluated);
399 OwningExprResult Result = ParseExpression();
400 if (Result.isInvalid()) {
401 SkipUntil(tok::r_paren);
402 return;
403 }
404
405 // Match the ')'
406 SourceLocation RParenLoc;
407 if (Tok.is(tok::r_paren))
408 RParenLoc = ConsumeParen();
409 else
410 MatchRHSPunctuation(tok::r_paren, LParenLoc);
411
412 if (RParenLoc.isInvalid())
413 return;
414
415 const char *PrevSpec = 0;
John McCall9f6e0972009-08-03 20:12:06 +0000416 unsigned DiagID;
Anders Carlssoneed418b2009-06-24 17:47:40 +0000417 // Check for duplicate type specifiers (e.g. "int decltype(a)").
418 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +0000419 DiagID, Result.release()))
420 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlssoneed418b2009-06-24 17:47:40 +0000421}
422
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000423/// ParseClassName - Parse a C++ class-name, which names a class. Note
424/// that we only check that the result names a type; semantic analysis
425/// will need to verify that the type names a class. The result is
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000426/// either a type or NULL, depending on whether a type name was
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000427/// found.
428///
429/// class-name: [C++ 9.1]
430/// identifier
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000431/// simple-template-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000432///
Douglas Gregord7cb0372009-04-01 21:51:26 +0000433Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahanian1b8fd752009-07-20 17:43:15 +0000434 const CXXScopeSpec *SS,
435 bool DestrExpected) {
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000436 // Check whether we have a template-id that names a type.
437 if (Tok.is(tok::annot_template_id)) {
438 TemplateIdAnnotation *TemplateId
439 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000440 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000441 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000442
443 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
444 TypeTy *Type = Tok.getAnnotationValue();
445 EndLocation = Tok.getAnnotationEndLoc();
446 ConsumeToken();
Douglas Gregord7cb0372009-04-01 21:51:26 +0000447
448 if (Type)
449 return Type;
450 return true;
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000451 }
452
453 // Fall through to produce an error below.
454 }
455
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000456 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000457 Diag(Tok, diag::err_expected_class_name);
Douglas Gregord7cb0372009-04-01 21:51:26 +0000458 return true;
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000459 }
460
461 // We have an identifier; check whether it is actually a type.
Douglas Gregor1075a162009-02-04 17:00:24 +0000462 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor64037c82009-08-26 18:27:52 +0000463 Tok.getLocation(), CurScope, SS,
464 true);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000465 if (!Type) {
Fariborz Jahanian1b8fd752009-07-20 17:43:15 +0000466 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
467 : diag::err_expected_class_name);
Douglas Gregord7cb0372009-04-01 21:51:26 +0000468 return true;
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000469 }
470
471 // Consume the identifier.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000472 EndLocation = ConsumeToken();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000473 return Type;
474}
475
Douglas Gregorec93f442008-04-13 21:30:24 +0000476/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
477/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
478/// until we reach the start of a definition or see a token that
479/// cannot start a definition.
480///
481/// class-specifier: [C++ class]
482/// class-head '{' member-specification[opt] '}'
483/// class-head '{' member-specification[opt] '}' attributes[opt]
484/// class-head:
485/// class-key identifier[opt] base-clause[opt]
486/// class-key nested-name-specifier identifier base-clause[opt]
487/// class-key nested-name-specifier[opt] simple-template-id
488/// base-clause[opt]
489/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
490/// [GNU] class-key attributes[opt] nested-name-specifier
491/// identifier base-clause[opt]
492/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
493/// simple-template-id base-clause[opt]
494/// class-key:
495/// 'class'
496/// 'struct'
497/// 'union'
498///
499/// elaborated-type-specifier: [C++ dcl.type.elab]
500/// class-key ::[opt] nested-name-specifier[opt] identifier
501/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
502/// simple-template-id
503///
504/// Note that the C++ class-specifier and elaborated-type-specifier,
505/// together, subsume the C99 struct-or-union-specifier:
506///
507/// struct-or-union-specifier: [C99 6.7.2.1]
508/// struct-or-union identifier[opt] '{' struct-contents '}'
509/// struct-or-union identifier
510/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
511/// '}' attributes[opt]
512/// [GNU] struct-or-union attributes[opt] identifier
513/// struct-or-union:
514/// 'struct'
515/// 'union'
Chris Lattner197b4342009-04-12 21:49:30 +0000516void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
517 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000518 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000519 AccessSpecifier AS) {
Chris Lattner197b4342009-04-12 21:49:30 +0000520 DeclSpec::TST TagType;
521 if (TagTokKind == tok::kw_struct)
522 TagType = DeclSpec::TST_struct;
523 else if (TagTokKind == tok::kw_class)
524 TagType = DeclSpec::TST_class;
525 else {
526 assert(TagTokKind == tok::kw_union && "Not a class specifier");
527 TagType = DeclSpec::TST_union;
528 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000529
530 AttributeList *Attr = 0;
531 // If attributes exist after tag, parse them.
532 if (Tok.is(tok::kw___attribute))
533 Attr = ParseAttributes();
534
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000535 // If declspecs exist after tag, parse them.
Eli Friedman891d82f2009-06-08 23:27:34 +0000536 if (Tok.is(tok::kw___declspec))
537 Attr = ParseMicrosoftDeclSpec(Attr);
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000538
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000539 // Parse the (optional) nested-name-specifier.
540 CXXScopeSpec SS;
Douglas Gregorc03d3302009-08-25 22:51:20 +0000541 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, true))
Douglas Gregor0c281a82009-02-25 19:37:18 +0000542 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000543 Diag(Tok, diag::err_expected_ident);
Douglas Gregora08b6c72009-02-17 23:15:12 +0000544
545 // Parse the (optional) class name or simple-template-id.
Douglas Gregorec93f442008-04-13 21:30:24 +0000546 IdentifierInfo *Name = 0;
547 SourceLocation NameLoc;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000548 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregorec93f442008-04-13 21:30:24 +0000549 if (Tok.is(tok::identifier)) {
550 Name = Tok.getIdentifierInfo();
551 NameLoc = ConsumeToken();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000552 } else if (Tok.is(tok::annot_template_id)) {
553 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
554 NameLoc = ConsumeToken();
Douglas Gregora08b6c72009-02-17 23:15:12 +0000555
Douglas Gregoraabb8502009-03-31 00:43:58 +0000556 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000557 // The template-name in the simple-template-id refers to
558 // something other than a class template. Give an appropriate
559 // error message and skip to the ';'.
560 SourceRange Range(NameLoc);
561 if (SS.isNotEmpty())
562 Range.setBegin(SS.getBeginLoc());
Douglas Gregora08b6c72009-02-17 23:15:12 +0000563
Douglas Gregor0c281a82009-02-25 19:37:18 +0000564 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
565 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000566
Douglas Gregor0c281a82009-02-25 19:37:18 +0000567 DS.SetTypeSpecError();
568 SkipUntil(tok::semi, false, true);
569 TemplateId->Destroy();
570 return;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000571 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000572 }
573
John McCall140607b2009-08-06 02:15:43 +0000574 // There are four options here. If we have 'struct foo;', then this
575 // is either a forward declaration or a friend declaration, which
576 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor0c281a82009-02-25 19:37:18 +0000577 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregorec93f442008-04-13 21:30:24 +0000578 // something like 'struct foo xyz', a reference.
John McCall069c23a2009-07-31 02:45:11 +0000579 Action::TagUseKind TUK;
Douglas Gregorec93f442008-04-13 21:30:24 +0000580 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
John McCall069c23a2009-07-31 02:45:11 +0000581 TUK = Action::TUK_Definition;
John McCall140607b2009-08-06 02:15:43 +0000582 else if (Tok.is(tok::semi))
583 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregorec93f442008-04-13 21:30:24 +0000584 else
John McCall069c23a2009-07-31 02:45:11 +0000585 TUK = Action::TUK_Reference;
Douglas Gregorec93f442008-04-13 21:30:24 +0000586
John McCall069c23a2009-07-31 02:45:11 +0000587 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000588 // We have a declaration or reference to an anonymous class.
Chris Lattnerf006a222008-11-18 07:48:38 +0000589 Diag(StartLoc, diag::err_anon_type_definition)
590 << DeclSpec::getSpecifierName(TagType);
Douglas Gregorec93f442008-04-13 21:30:24 +0000591
592 // Skip the rest of this declarator, up until the comma or semicolon.
593 SkipUntil(tok::comma, true);
Douglas Gregor0c281a82009-02-25 19:37:18 +0000594
595 if (TemplateId)
596 TemplateId->Destroy();
Douglas Gregorec93f442008-04-13 21:30:24 +0000597 return;
598 }
599
Douglas Gregord406b032009-02-06 22:42:48 +0000600 // Create the tag portion of the class or class template.
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000601 Action::DeclResult TagOrTempResult;
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000602 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
603
John McCall069c23a2009-07-31 02:45:11 +0000604 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000605 // to turn that template-id into a type.
606
Douglas Gregor71f06032009-05-28 23:31:59 +0000607 bool Owned = false;
John McCall140607b2009-08-06 02:15:43 +0000608 if (TemplateId && TUK != Action::TUK_Reference && TUK != Action::TUK_Friend) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000609 // Explicit specialization, class template partial specialization,
610 // or explicit instantiation.
Douglas Gregor0c281a82009-02-25 19:37:18 +0000611 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
612 TemplateId->getTemplateArgs(),
613 TemplateId->getTemplateArgIsType(),
614 TemplateId->NumArgs);
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000615 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall069c23a2009-07-31 02:45:11 +0000616 TUK == Action::TUK_Declaration) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000617 // This is an explicit instantiation of a class template.
618 TagOrTempResult
619 = Actions.ActOnExplicitInstantiation(CurScope,
620 TemplateInfo.TemplateLoc,
621 TagType,
622 StartLoc,
623 SS,
624 TemplateTy::make(TemplateId->Template),
625 TemplateId->TemplateNameLoc,
626 TemplateId->LAngleLoc,
627 TemplateArgsPtr,
628 TemplateId->getTemplateArgLocations(),
629 TemplateId->RAngleLoc,
630 Attr);
631 } else {
632 // This is an explicit specialization or a class template
633 // partial specialization.
634 TemplateParameterLists FakedParamLists;
635
636 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
637 // This looks like an explicit instantiation, because we have
638 // something like
639 //
640 // template class Foo<X>
641 //
Douglas Gregor96b6df92009-05-14 00:28:11 +0000642 // but it actually has a definition. Most likely, this was
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000643 // meant to be an explicit specialization, but the user forgot
644 // the '<>' after 'template'.
John McCall069c23a2009-07-31 02:45:11 +0000645 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000646
647 SourceLocation LAngleLoc
648 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
649 Diag(TemplateId->TemplateNameLoc,
650 diag::err_explicit_instantiation_with_definition)
651 << SourceRange(TemplateInfo.TemplateLoc)
652 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
653
654 // Create a fake template parameter list that contains only
655 // "template<>", so that we treat this construct as a class
656 // template specialization.
657 FakedParamLists.push_back(
658 Actions.ActOnTemplateParameterList(0, SourceLocation(),
659 TemplateInfo.TemplateLoc,
660 LAngleLoc,
661 0, 0,
662 LAngleLoc));
663 TemplateParams = &FakedParamLists;
664 }
665
666 // Build the class template specialization.
667 TagOrTempResult
John McCall069c23a2009-07-31 02:45:11 +0000668 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor0c281a82009-02-25 19:37:18 +0000669 StartLoc, SS,
Douglas Gregordd13e842009-03-30 22:58:21 +0000670 TemplateTy::make(TemplateId->Template),
Douglas Gregor0c281a82009-02-25 19:37:18 +0000671 TemplateId->TemplateNameLoc,
672 TemplateId->LAngleLoc,
673 TemplateArgsPtr,
674 TemplateId->getTemplateArgLocations(),
675 TemplateId->RAngleLoc,
676 Attr,
Douglas Gregora08b6c72009-02-17 23:15:12 +0000677 Action::MultiTemplateParamsArg(Actions,
678 TemplateParams? &(*TemplateParams)[0] : 0,
679 TemplateParams? TemplateParams->size() : 0));
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000680 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000681 TemplateId->Destroy();
Douglas Gregor96b6df92009-05-14 00:28:11 +0000682 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall069c23a2009-07-31 02:45:11 +0000683 TUK == Action::TUK_Declaration) {
Douglas Gregor96b6df92009-05-14 00:28:11 +0000684 // Explicit instantiation of a member of a class template
685 // specialization, e.g.,
686 //
687 // template struct Outer<int>::Inner;
688 //
689 TagOrTempResult
690 = Actions.ActOnExplicitInstantiation(CurScope,
691 TemplateInfo.TemplateLoc,
692 TagType, StartLoc, SS, Name,
693 NameLoc, Attr);
694 } else {
695 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall069c23a2009-07-31 02:45:11 +0000696 TUK == Action::TUK_Definition) {
Douglas Gregor96b6df92009-05-14 00:28:11 +0000697 // FIXME: Diagnose this particular error.
698 }
699
700 // Declaration or definition of a class type
John McCall069c23a2009-07-31 02:45:11 +0000701 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Douglas Gregor84a20812009-07-22 23:48:44 +0000702 Name, NameLoc, Attr, AS,
703 Action::MultiTemplateParamsArg(Actions,
704 TemplateParams? &(*TemplateParams)[0] : 0,
705 TemplateParams? TemplateParams->size() : 0),
706 Owned);
Douglas Gregor96b6df92009-05-14 00:28:11 +0000707 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000708
709 // Parse the optional base clause (C++ only).
Chris Lattner31ccf0a2009-02-16 22:07:16 +0000710 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000711 ParseBaseClause(TagOrTempResult.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000712
713 // If there is a body, parse it and inform the actions module.
714 if (Tok.is(tok::l_brace))
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000715 if (getLang().CPlusPlus)
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000716 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000717 else
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000718 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall069c23a2009-07-31 02:45:11 +0000719 else if (TUK == Action::TUK_Definition) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000720 // FIXME: Complain that we have a base-specifier list but no
721 // definition.
Chris Lattnerf006a222008-11-18 07:48:38 +0000722 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregorec93f442008-04-13 21:30:24 +0000723 }
724
Anders Carlssonef3fa4f2009-05-11 22:27:47 +0000725 if (TagOrTempResult.isInvalid()) {
Douglas Gregord406b032009-02-06 22:42:48 +0000726 DS.SetTypeSpecError();
Anders Carlssonef3fa4f2009-05-11 22:27:47 +0000727 return;
728 }
729
John McCall9f6e0972009-08-03 20:12:06 +0000730 const char *PrevSpec = 0;
731 unsigned DiagID;
732 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
Douglas Gregor71f06032009-05-28 23:31:59 +0000733 TagOrTempResult.get().getAs<void>(), Owned))
John McCall9f6e0972009-08-03 20:12:06 +0000734 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregorec93f442008-04-13 21:30:24 +0000735}
736
737/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
738///
739/// base-clause : [C++ class.derived]
740/// ':' base-specifier-list
741/// base-specifier-list:
742/// base-specifier '...'[opt]
743/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattner5261d0c2009-03-28 19:18:32 +0000744void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000745 assert(Tok.is(tok::colon) && "Not a base clause");
746 ConsumeToken();
747
Douglas Gregorabed2172008-10-22 17:49:05 +0000748 // Build up an array of parsed base specifiers.
749 llvm::SmallVector<BaseTy *, 8> BaseInfo;
750
Douglas Gregorec93f442008-04-13 21:30:24 +0000751 while (true) {
752 // Parse a base-specifier.
Douglas Gregorabed2172008-10-22 17:49:05 +0000753 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000754 if (Result.isInvalid()) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000755 // Skip the rest of this base specifier, up until the comma or
756 // opening brace.
Douglas Gregorabed2172008-10-22 17:49:05 +0000757 SkipUntil(tok::comma, tok::l_brace, true, true);
758 } else {
759 // Add this to our array of base specifiers.
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000760 BaseInfo.push_back(Result.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000761 }
762
763 // If the next token is a comma, consume it and keep reading
764 // base-specifiers.
765 if (Tok.isNot(tok::comma)) break;
766
767 // Consume the comma.
768 ConsumeToken();
769 }
Douglas Gregorabed2172008-10-22 17:49:05 +0000770
771 // Attach the base specifiers
Jay Foad9e6bef42009-05-21 09:52:38 +0000772 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregorec93f442008-04-13 21:30:24 +0000773}
774
775/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
776/// one entry in the base class list of a class specifier, for example:
777/// class foo : public bar, virtual private baz {
778/// 'public bar' and 'virtual private baz' are each base-specifiers.
779///
780/// base-specifier: [C++ class.derived]
781/// ::[opt] nested-name-specifier[opt] class-name
782/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
783/// class-name
784/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
785/// class-name
Chris Lattner5261d0c2009-03-28 19:18:32 +0000786Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000787 bool IsVirtual = false;
788 SourceLocation StartLoc = Tok.getLocation();
789
790 // Parse the 'virtual' keyword.
791 if (Tok.is(tok::kw_virtual)) {
792 ConsumeToken();
793 IsVirtual = true;
794 }
795
796 // Parse an (optional) access specifier.
797 AccessSpecifier Access = getAccessSpecifierIfPresent();
798 if (Access)
799 ConsumeToken();
800
801 // Parse the 'virtual' keyword (again!), in case it came after the
802 // access specifier.
803 if (Tok.is(tok::kw_virtual)) {
804 SourceLocation VirtualLoc = ConsumeToken();
805 if (IsVirtual) {
806 // Complain about duplicate 'virtual'
Chris Lattnerf006a222008-11-18 07:48:38 +0000807 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregord7cb0372009-04-01 21:51:26 +0000808 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregorec93f442008-04-13 21:30:24 +0000809 }
810
811 IsVirtual = true;
812 }
813
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000814 // Parse optional '::' and optional nested-name-specifier.
815 CXXScopeSpec SS;
Douglas Gregorc03d3302009-08-25 22:51:20 +0000816 ParseOptionalCXXScopeSpecifier(SS, true);
Douglas Gregorec93f442008-04-13 21:30:24 +0000817
Douglas Gregorec93f442008-04-13 21:30:24 +0000818 // The location of the base class itself.
819 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000820
821 // Parse the class-name.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000822 SourceLocation EndLocation;
Douglas Gregord7cb0372009-04-01 21:51:26 +0000823 TypeResult BaseType = ParseClassName(EndLocation, &SS);
824 if (BaseType.isInvalid())
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000825 return true;
Douglas Gregorec93f442008-04-13 21:30:24 +0000826
827 // Find the complete source range for the base-specifier.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000828 SourceRange Range(StartLoc, EndLocation);
Douglas Gregorec93f442008-04-13 21:30:24 +0000829
Douglas Gregorec93f442008-04-13 21:30:24 +0000830 // Notify semantic analysis that we have parsed a complete
831 // base-specifier.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000832 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregord7cb0372009-04-01 21:51:26 +0000833 BaseType.get(), BaseLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000834}
835
836/// getAccessSpecifierIfPresent - Determine whether the next token is
837/// a C++ access-specifier.
838///
839/// access-specifier: [C++ class.derived]
840/// 'private'
841/// 'protected'
842/// 'public'
Douglas Gregor696be932008-04-14 00:13:42 +0000843AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregorec93f442008-04-13 21:30:24 +0000844{
845 switch (Tok.getKind()) {
846 default: return AS_none;
847 case tok::kw_private: return AS_private;
848 case tok::kw_protected: return AS_protected;
849 case tok::kw_public: return AS_public;
850 }
851}
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000852
Eli Friedman40035e22009-07-22 21:45:50 +0000853void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
854 DeclPtrTy ThisDecl) {
855 // We just declared a member function. If this member function
856 // has any default arguments, we'll need to parse them later.
857 LateParsedMethodDeclaration *LateMethod = 0;
858 DeclaratorChunk::FunctionTypeInfo &FTI
859 = DeclaratorInfo.getTypeObject(0).Fun;
860 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
861 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
862 if (!LateMethod) {
863 // Push this method onto the stack of late-parsed method
864 // declarations.
865 getCurrentClass().MethodDecls.push_back(
866 LateParsedMethodDeclaration(ThisDecl));
867 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregor6ee35052009-08-22 00:34:47 +0000868 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedman40035e22009-07-22 21:45:50 +0000869
870 // Add all of the parameters prior to this one (they don't
871 // have default arguments).
872 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
873 for (unsigned I = 0; I < ParamIdx; ++I)
874 LateMethod->DefaultArgs.push_back(
875 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
876 }
877
878 // Add this parameter to the list of parameters (it or may
879 // not have a default argument).
880 LateMethod->DefaultArgs.push_back(
881 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
882 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
883 }
884 }
885}
886
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000887/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
888///
889/// member-declaration:
890/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
891/// function-definition ';'[opt]
892/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
893/// using-declaration [TODO]
Anders Carlssonab041982009-03-11 16:27:10 +0000894/// [C++0x] static_assert-declaration
Anders Carlssoned20fb92009-03-26 00:52:18 +0000895/// template-declaration
Chris Lattnerf3375de2008-12-18 01:12:00 +0000896/// [GNU] '__extension__' member-declaration
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000897///
898/// member-declarator-list:
899/// member-declarator
900/// member-declarator-list ',' member-declarator
901///
902/// member-declarator:
903/// declarator pure-specifier[opt]
904/// declarator constant-initializer[opt]
905/// identifier[opt] ':' constant-expression
906///
Sebastian Redla55834a2009-04-12 17:16:29 +0000907/// pure-specifier:
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000908/// '= 0'
909///
910/// constant-initializer:
911/// '=' constant-expression
912///
Douglas Gregor398a8012009-08-20 22:52:58 +0000913void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
914 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlssonab041982009-03-11 16:27:10 +0000915 // static_assert-declaration
Chris Lattnera17991f2009-03-29 16:50:03 +0000916 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor398a8012009-08-20 22:52:58 +0000917 // FIXME: Check for templates
Chris Lattner9802a0a2009-04-02 04:16:50 +0000918 SourceLocation DeclEnd;
919 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000920 return;
921 }
Anders Carlssonab041982009-03-11 16:27:10 +0000922
Chris Lattnera17991f2009-03-29 16:50:03 +0000923 if (Tok.is(tok::kw_template)) {
Douglas Gregor398a8012009-08-20 22:52:58 +0000924 assert(!TemplateInfo.TemplateParams &&
925 "Nested template improperly parsed?");
Chris Lattner9802a0a2009-04-02 04:16:50 +0000926 SourceLocation DeclEnd;
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000927 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
928 AS);
Chris Lattnera17991f2009-03-29 16:50:03 +0000929 return;
930 }
Anders Carlssoned20fb92009-03-26 00:52:18 +0000931
Chris Lattnerf3375de2008-12-18 01:12:00 +0000932 // Handle: member-declaration ::= '__extension__' member-declaration
933 if (Tok.is(tok::kw___extension__)) {
934 // __extension__ silences extension warnings in the subexpression.
935 ExtensionRAIIObject O(Diags); // Use RAII to do this.
936 ConsumeToken();
Douglas Gregor398a8012009-08-20 22:52:58 +0000937 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerf3375de2008-12-18 01:12:00 +0000938 }
Douglas Gregor683a1142009-06-20 00:51:54 +0000939
940 if (Tok.is(tok::kw_using)) {
Douglas Gregor398a8012009-08-20 22:52:58 +0000941 // FIXME: Check for template aliases
942
Douglas Gregor683a1142009-06-20 00:51:54 +0000943 // Eat 'using'.
944 SourceLocation UsingLoc = ConsumeToken();
945
946 if (Tok.is(tok::kw_namespace)) {
947 Diag(UsingLoc, diag::err_using_namespace_in_class);
948 SkipUntil(tok::semi, true, true);
949 }
950 else {
951 SourceLocation DeclEnd;
952 // Otherwise, it must be using-declaration.
Anders Carlssone16b4fe2009-08-29 19:54:19 +0000953 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor683a1142009-06-20 00:51:54 +0000954 }
955 return;
956 }
957
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000958 SourceLocation DSStart = Tok.getLocation();
959 // decl-specifier-seq:
960 // Parse the common declaration-specifiers piece.
961 DeclSpec DS;
Douglas Gregor398a8012009-08-20 22:52:58 +0000962 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000963
964 if (Tok.is(tok::semi)) {
965 ConsumeToken();
John McCall140607b2009-08-06 02:15:43 +0000966
Douglas Gregor398a8012009-08-20 22:52:58 +0000967 // FIXME: Friend templates?
John McCall140607b2009-08-06 02:15:43 +0000968 if (DS.isFriendSpecified())
John McCall36493082009-08-11 06:59:38 +0000969 Actions.ActOnFriendDecl(CurScope, &DS, /*IsDefinition*/ false);
John McCall140607b2009-08-06 02:15:43 +0000970 else
Chris Lattnera17991f2009-03-29 16:50:03 +0000971 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall140607b2009-08-06 02:15:43 +0000972
973 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000974 }
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000975
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000976 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000977
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000978 if (Tok.isNot(tok::colon)) {
979 // Parse the first declarator.
980 ParseDeclarator(DeclaratorInfo);
981 // Error parsing the declarator?
Douglas Gregor6704b312008-11-17 22:58:34 +0000982 if (!DeclaratorInfo.hasName()) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000983 // If so, skip until the semi-colon or a }.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000984 SkipUntil(tok::r_brace, true);
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000985 if (Tok.is(tok::semi))
986 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000987 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000988 }
989
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000990 // function-definition:
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000991 if (Tok.is(tok::l_brace)
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000992 || (DeclaratorInfo.isFunctionDeclarator() &&
993 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000994 if (!DeclaratorInfo.isFunctionDeclarator()) {
995 Diag(Tok, diag::err_func_def_no_params);
996 ConsumeBrace();
997 SkipUntil(tok::r_brace, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000998 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000999 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001000
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001001 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1002 Diag(Tok, diag::err_function_declared_typedef);
1003 // This recovery skips the entire function body. It would be nice
1004 // to simply call ParseCXXInlineMethodDef() below, however Sema
1005 // assumes the declarator represents a function, not a typedef.
1006 ConsumeBrace();
1007 SkipUntil(tok::r_brace, true);
Chris Lattnera17991f2009-03-29 16:50:03 +00001008 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001009 }
1010
Douglas Gregor398a8012009-08-20 22:52:58 +00001011 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattnera17991f2009-03-29 16:50:03 +00001012 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001013 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001014 }
1015
1016 // member-declarator-list:
1017 // member-declarator
1018 // member-declarator-list ',' member-declarator
1019
Chris Lattnera17991f2009-03-29 16:50:03 +00001020 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl62261042008-12-09 20:22:58 +00001021 OwningExprResult BitfieldSize(Actions);
1022 OwningExprResult Init(Actions);
Sebastian Redla55834a2009-04-12 17:16:29 +00001023 bool Deleted = false;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001024
1025 while (1) {
1026
1027 // member-declarator:
1028 // declarator pure-specifier[opt]
1029 // declarator constant-initializer[opt]
1030 // identifier[opt] ':' constant-expression
1031
1032 if (Tok.is(tok::colon)) {
1033 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001034 BitfieldSize = ParseConstantExpression();
1035 if (BitfieldSize.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001036 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001037 }
1038
1039 // pure-specifier:
1040 // '= 0'
1041 //
1042 // constant-initializer:
1043 // '=' constant-expression
Sebastian Redla55834a2009-04-12 17:16:29 +00001044 //
1045 // defaulted/deleted function-definition:
1046 // '=' 'default' [TODO]
1047 // '=' 'delete'
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001048
1049 if (Tok.is(tok::equal)) {
1050 ConsumeToken();
Sebastian Redla55834a2009-04-12 17:16:29 +00001051 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1052 ConsumeToken();
1053 Deleted = true;
1054 } else {
1055 Init = ParseInitializer();
1056 if (Init.isInvalid())
1057 SkipUntil(tok::comma, true, true);
1058 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001059 }
1060
1061 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001062 if (Tok.is(tok::kw___attribute)) {
1063 SourceLocation Loc;
1064 AttributeList *AttrList = ParseAttributes(&Loc);
1065 DeclaratorInfo.AddAttributes(AttrList, Loc);
1066 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001067
Argiris Kirtzidis38f16712008-07-01 10:37:29 +00001068 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattnera17991f2009-03-29 16:50:03 +00001069 // this call will *not* return the created decl; It will return null.
Argiris Kirtzidis38f16712008-07-01 10:37:29 +00001070 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall140607b2009-08-06 02:15:43 +00001071
1072 DeclPtrTy ThisDecl;
1073 if (DS.isFriendSpecified()) {
Douglas Gregor398a8012009-08-20 22:52:58 +00001074 // TODO: handle initializers, bitfields, 'delete', friend templates
John McCall36493082009-08-11 06:59:38 +00001075 ThisDecl = Actions.ActOnFriendDecl(CurScope, &DeclaratorInfo,
1076 /*IsDefinition*/ false);
Douglas Gregor398a8012009-08-20 22:52:58 +00001077 } else {
1078 Action::MultiTemplateParamsArg TemplateParams(Actions,
1079 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1080 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
John McCall140607b2009-08-06 02:15:43 +00001081 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1082 DeclaratorInfo,
Douglas Gregor398a8012009-08-20 22:52:58 +00001083 move(TemplateParams),
John McCall140607b2009-08-06 02:15:43 +00001084 BitfieldSize.release(),
1085 Init.release(),
1086 Deleted);
Douglas Gregor398a8012009-08-20 22:52:58 +00001087 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001088 if (ThisDecl)
1089 DeclsInGroup.push_back(ThisDecl);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001090
Douglas Gregor605de8d2008-12-16 21:30:33 +00001091 if (DeclaratorInfo.isFunctionDeclarator() &&
1092 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
1093 != DeclSpec::SCS_typedef) {
Eli Friedman40035e22009-07-22 21:45:50 +00001094 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor605de8d2008-12-16 21:30:33 +00001095 }
1096
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001097 // If we don't have a comma, it is either the end of the list (a ';')
1098 // or an error, bail out.
1099 if (Tok.isNot(tok::comma))
1100 break;
1101
1102 // Consume the comma.
1103 ConsumeToken();
1104
1105 // Parse the next declarator.
1106 DeclaratorInfo.clear();
Sebastian Redl62261042008-12-09 20:22:58 +00001107 BitfieldSize = 0;
1108 Init = 0;
Sebastian Redla55834a2009-04-12 17:16:29 +00001109 Deleted = false;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001110
1111 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001112 if (Tok.is(tok::kw___attribute)) {
1113 SourceLocation Loc;
1114 AttributeList *AttrList = ParseAttributes(&Loc);
1115 DeclaratorInfo.AddAttributes(AttrList, Loc);
1116 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001117
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001118 if (Tok.isNot(tok::colon))
1119 ParseDeclarator(DeclaratorInfo);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001120 }
1121
1122 if (Tok.is(tok::semi)) {
1123 ConsumeToken();
Eli Friedman4d57af22009-05-29 01:49:24 +00001124 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattnera17991f2009-03-29 16:50:03 +00001125 DeclsInGroup.size());
1126 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001127 }
1128
1129 Diag(Tok, diag::err_expected_semi_decl_list);
1130 // Skip to end of block or statement
1131 SkipUntil(tok::r_brace, true, true);
1132 if (Tok.is(tok::semi))
1133 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +00001134 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001135}
1136
1137/// ParseCXXMemberSpecification - Parse the class definition.
1138///
1139/// member-specification:
1140/// member-declaration member-specification[opt]
1141/// access-specifier ':' member-specification[opt]
1142///
1143void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001144 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Guptafa451432008-10-31 09:52:39 +00001145 assert((TagType == DeclSpec::TST_struct ||
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001146 TagType == DeclSpec::TST_union ||
Sanjiv Guptafa451432008-10-31 09:52:39 +00001147 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001148
Chris Lattnerc309ade2009-03-05 08:00:35 +00001149 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1150 PP.getSourceManager(),
1151 "parsing struct/union/class body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001152
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001153 SourceLocation LBraceLoc = ConsumeBrace();
1154
Douglas Gregora376cbd2009-05-27 23:11:45 +00001155 // Determine whether this is a top-level (non-nested) class.
1156 bool TopLevelClass = ClassStack.empty() ||
1157 CurScope->isInCXXInlineMethodScope();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001158
1159 // Enter a scope for the class.
Douglas Gregorcab994d2009-01-09 22:42:13 +00001160 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001161
Douglas Gregora376cbd2009-05-27 23:11:45 +00001162 // Note that we are parsing a new (potentially-nested) class definition.
1163 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1164
Douglas Gregord406b032009-02-06 22:42:48 +00001165 if (TagDecl)
1166 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1167 else {
1168 SkipUntil(tok::r_brace, false, false);
1169 return;
1170 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001171
1172 // C++ 11p3: Members of a class defined with the keyword class are private
1173 // by default. Members of a class defined with the keywords struct or union
1174 // are public by default.
1175 AccessSpecifier CurAS;
1176 if (TagType == DeclSpec::TST_class)
1177 CurAS = AS_private;
1178 else
1179 CurAS = AS_public;
1180
1181 // While we still have something to read, read the member-declarations.
1182 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1183 // Each iteration of this loop reads one member-declaration.
1184
1185 // Check for extraneous top-level semicolon.
1186 if (Tok.is(tok::semi)) {
1187 Diag(Tok, diag::ext_extra_struct_semi);
1188 ConsumeToken();
1189 continue;
1190 }
1191
1192 AccessSpecifier AS = getAccessSpecifierIfPresent();
1193 if (AS != AS_none) {
1194 // Current token is a C++ access specifier.
1195 CurAS = AS;
1196 ConsumeToken();
1197 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1198 continue;
1199 }
1200
Douglas Gregor398a8012009-08-20 22:52:58 +00001201 // FIXME: Make sure we don't have a template here.
1202
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001203 // Parse all the comma separated declarators.
1204 ParseCXXClassMemberDeclaration(CurAS);
1205 }
1206
1207 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1208
1209 AttributeList *AttrList = 0;
1210 // If attributes exist after class contents, parse them.
1211 if (Tok.is(tok::kw___attribute))
1212 AttrList = ParseAttributes(); // FIXME: where should I put them?
1213
1214 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1215 LBraceLoc, RBraceLoc);
1216
1217 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1218 // complete within function bodies, default arguments,
1219 // exception-specifications, and constructor ctor-initializers (including
1220 // such things in nested classes).
1221 //
Douglas Gregor605de8d2008-12-16 21:30:33 +00001222 // FIXME: Only function bodies and constructor ctor-initializers are
1223 // parsed correctly, fix the rest.
Douglas Gregora376cbd2009-05-27 23:11:45 +00001224 if (TopLevelClass) {
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001225 // We are not inside a nested class. This class and its nested classes
Douglas Gregor605de8d2008-12-16 21:30:33 +00001226 // are complete and we can parse the delayed portions of method
1227 // declarations and the lexed inline method definitions.
Douglas Gregora376cbd2009-05-27 23:11:45 +00001228 ParseLexedMethodDeclarations(getCurrentClass());
1229 ParseLexedMethodDefs(getCurrentClass());
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001230 }
1231
1232 // Leave the class scope.
Douglas Gregora376cbd2009-05-27 23:11:45 +00001233 ParsingDef.Pop();
Douglas Gregor95d40792008-12-10 06:34:36 +00001234 ClassScope.Exit();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001235
Argiris Kirtzidiseb925642009-07-14 03:17:52 +00001236 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001237}
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001238
1239/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1240/// which explicitly initializes the members or base classes of a
1241/// class (C++ [class.base.init]). For example, the three initializers
1242/// after the ':' in the Derived constructor below:
1243///
1244/// @code
1245/// class Base { };
1246/// class Derived : Base {
1247/// int x;
1248/// float f;
1249/// public:
1250/// Derived(float f) : Base(), x(17), f(f) { }
1251/// };
1252/// @endcode
1253///
1254/// [C++] ctor-initializer:
1255/// ':' mem-initializer-list
1256///
1257/// [C++] mem-initializer-list:
1258/// mem-initializer
1259/// mem-initializer , mem-initializer-list
Chris Lattner5261d0c2009-03-28 19:18:32 +00001260void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001261 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1262
1263 SourceLocation ColonLoc = ConsumeToken();
1264
1265 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
1266
1267 do {
1268 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +00001269 if (!MemInit.isInvalid())
1270 MemInitializers.push_back(MemInit.get());
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001271
1272 if (Tok.is(tok::comma))
1273 ConsumeToken();
1274 else if (Tok.is(tok::l_brace))
1275 break;
1276 else {
1277 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redlbc9ef252009-04-26 20:35:05 +00001278 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001279 SkipUntil(tok::l_brace, true, true);
1280 break;
1281 }
1282 } while (true);
1283
1284 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +00001285 MemInitializers.data(), MemInitializers.size());
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001286}
1287
1288/// ParseMemInitializer - Parse a C++ member initializer, which is
1289/// part of a constructor initializer that explicitly initializes one
1290/// member or base class (C++ [class.base.init]). See
1291/// ParseConstructorInitializer for an example.
1292///
1293/// [C++] mem-initializer:
1294/// mem-initializer-id '(' expression-list[opt] ')'
1295///
1296/// [C++] mem-initializer-id:
1297/// '::'[opt] nested-name-specifier[opt] class-name
1298/// identifier
Chris Lattner5261d0c2009-03-28 19:18:32 +00001299Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianc37d3062009-06-30 23:26:25 +00001300 // parse '::'[opt] nested-name-specifier[opt]
1301 CXXScopeSpec SS;
1302 ParseOptionalCXXScopeSpecifier(SS);
Fariborz Jahanian2b2b7362009-07-01 19:21:19 +00001303 TypeTy *TemplateTypeTy = 0;
1304 if (Tok.is(tok::annot_template_id)) {
1305 TemplateIdAnnotation *TemplateId
1306 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1307 if (TemplateId->Kind == TNK_Type_template) {
1308 AnnotateTemplateIdTokenAsType(&SS);
1309 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1310 TemplateTypeTy = Tok.getAnnotationValue();
1311 }
1312 // FIXME. May need to check for TNK_Dependent_template as well.
1313 }
1314 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001315 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001316 return true;
1317 }
Fariborz Jahanian2b2b7362009-07-01 19:21:19 +00001318
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001319 // Get the identifier. This may be a member name or a class name,
1320 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian2b2b7362009-07-01 19:21:19 +00001321 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001322 SourceLocation IdLoc = ConsumeToken();
1323
1324 // Parse the '('.
1325 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001326 Diag(Tok, diag::err_expected_lparen);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001327 return true;
1328 }
1329 SourceLocation LParenLoc = ConsumeParen();
1330
1331 // Parse the optional expression-list.
Sebastian Redl6008ac32008-11-25 22:21:31 +00001332 ExprVector ArgExprs(Actions);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001333 CommaLocsTy CommaLocs;
1334 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1335 SkipUntil(tok::r_paren);
1336 return true;
1337 }
1338
1339 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1340
Fariborz Jahanian2b2b7362009-07-01 19:21:19 +00001341 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1342 TemplateTypeTy, IdLoc,
Sebastian Redl6008ac32008-11-25 22:21:31 +00001343 LParenLoc, ArgExprs.take(),
Jay Foad9e6bef42009-05-21 09:52:38 +00001344 ArgExprs.size(), CommaLocs.data(),
1345 RParenLoc);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001346}
Douglas Gregor90a2c972008-11-25 03:22:00 +00001347
1348/// ParseExceptionSpecification - Parse a C++ exception-specification
1349/// (C++ [except.spec]).
1350///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001351/// exception-specification:
1352/// 'throw' '(' type-id-list [opt] ')'
1353/// [MS] 'throw' '(' '...' ')'
Douglas Gregor90a2c972008-11-25 03:22:00 +00001354///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001355/// type-id-list:
1356/// type-id
1357/// type-id-list ',' type-id
Douglas Gregor90a2c972008-11-25 03:22:00 +00001358///
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001359bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlaaacda92009-05-29 18:02:33 +00001360 llvm::SmallVector<TypeTy*, 2>
1361 &Exceptions,
1362 llvm::SmallVector<SourceRange, 2>
1363 &Ranges,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001364 bool &hasAnyExceptionSpec) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00001365 assert(Tok.is(tok::kw_throw) && "expected throw");
1366
1367 SourceLocation ThrowLoc = ConsumeToken();
1368
1369 if (!Tok.is(tok::l_paren)) {
1370 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1371 }
1372 SourceLocation LParenLoc = ConsumeParen();
1373
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001374 // Parse throw(...), a Microsoft extension that means "this function
1375 // can throw anything".
1376 if (Tok.is(tok::ellipsis)) {
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001377 hasAnyExceptionSpec = true;
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001378 SourceLocation EllipsisLoc = ConsumeToken();
1379 if (!getLang().Microsoft)
1380 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redl0c986032009-02-09 18:23:29 +00001381 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001382 return false;
1383 }
1384
Douglas Gregor90a2c972008-11-25 03:22:00 +00001385 // Parse the sequence of type-ids.
Sebastian Redlaaacda92009-05-29 18:02:33 +00001386 SourceRange Range;
Douglas Gregor90a2c972008-11-25 03:22:00 +00001387 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlaaacda92009-05-29 18:02:33 +00001388 TypeResult Res(ParseTypeName(&Range));
1389 if (!Res.isInvalid()) {
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001390 Exceptions.push_back(Res.get());
Sebastian Redlaaacda92009-05-29 18:02:33 +00001391 Ranges.push_back(Range);
1392 }
Douglas Gregor90a2c972008-11-25 03:22:00 +00001393 if (Tok.is(tok::comma))
1394 ConsumeToken();
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001395 else
Douglas Gregor90a2c972008-11-25 03:22:00 +00001396 break;
1397 }
1398
Sebastian Redl0c986032009-02-09 18:23:29 +00001399 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001400 return false;
1401}
Douglas Gregora376cbd2009-05-27 23:11:45 +00001402
1403/// \brief We have just started parsing the definition of a new class,
1404/// so push that class onto our stack of classes that is currently
1405/// being parsed.
1406void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
1407 assert((TopLevelClass || !ClassStack.empty()) &&
1408 "Nested class without outer class");
1409 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1410}
1411
1412/// \brief Deallocate the given parsed class and all of its nested
1413/// classes.
1414void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1415 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1416 DeallocateParsedClasses(Class->NestedClasses[I]);
1417 delete Class;
1418}
1419
1420/// \brief Pop the top class of the stack of classes that are
1421/// currently being parsed.
1422///
1423/// This routine should be called when we have finished parsing the
1424/// definition of a class, but have not yet popped the Scope
1425/// associated with the class's definition.
1426///
1427/// \returns true if the class we've popped is a top-level class,
1428/// false otherwise.
1429void Parser::PopParsingClass() {
1430 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
1431
1432 ParsingClass *Victim = ClassStack.top();
1433 ClassStack.pop();
1434 if (Victim->TopLevelClass) {
1435 // Deallocate all of the nested classes of this class,
1436 // recursively: we don't need to keep any of this information.
1437 DeallocateParsedClasses(Victim);
1438 return;
1439 }
1440 assert(!ClassStack.empty() && "Missing top-level class?");
1441
1442 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1443 Victim->NestedClasses.empty()) {
1444 // The victim is a nested class, but we will not need to perform
1445 // any processing after the definition of this class since it has
1446 // no members whose handling was delayed. Therefore, we can just
1447 // remove this nested class.
1448 delete Victim;
1449 return;
1450 }
1451
1452 // This nested class has some members that will need to be processed
1453 // after the top-level class is completely defined. Therefore, add
1454 // it to the list of nested classes within its parent.
1455 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1456 ClassStack.top()->NestedClasses.push_back(Victim);
1457 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1458}