blob: a7d3b52bd2393309cebe4def4a42239f3b7ab805 [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
Douglas Gregor696be932008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregorec93f442008-04-13 21:30:24 +000016#include "clang/Parse/DeclSpec.h"
Chris Lattnerf7b2e552007-08-25 06:57:03 +000017#include "clang/Parse/Scope.h"
Chris Lattnerf3375de2008-12-18 01:12:00 +000018#include "ExtensionRAIIObject.h"
Chris Lattnerf7b2e552007-08-25 06:57:03 +000019using namespace clang;
20
21/// ParseNamespace - We know that the current token is a namespace keyword. This
22/// may either be a top level namespace or a block-level namespace alias.
23///
24/// namespace-definition: [C++ 7.3: basic.namespace]
25/// named-namespace-definition
26/// unnamed-namespace-definition
27///
28/// unnamed-namespace-definition:
29/// 'namespace' attributes[opt] '{' namespace-body '}'
30///
31/// named-namespace-definition:
32/// original-namespace-definition
33/// extension-namespace-definition
34///
35/// original-namespace-definition:
36/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
37///
38/// extension-namespace-definition:
39/// 'namespace' original-namespace-name '{' namespace-body '}'
40///
41/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
42/// 'namespace' identifier '=' qualified-namespace-specifier ';'
43///
Chris Lattner9802a0a2009-04-02 04:16:50 +000044Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
45 SourceLocation &DeclEnd) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000046 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnerf7b2e552007-08-25 06:57:03 +000047 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
48
49 SourceLocation IdentLoc;
50 IdentifierInfo *Ident = 0;
51
Chris Lattner34a01ad2007-10-09 17:33:22 +000052 if (Tok.is(tok::identifier)) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +000053 Ident = Tok.getIdentifierInfo();
54 IdentLoc = ConsumeToken(); // eat the identifier.
55 }
56
57 // Read label attributes, if present.
Chris Lattner5261d0c2009-03-28 19:18:32 +000058 Action::AttrTy *AttrList = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +000059 if (Tok.is(tok::kw___attribute))
Chris Lattnerf7b2e552007-08-25 06:57:03 +000060 // FIXME: save these somewhere.
61 AttrList = ParseAttributes();
62
Anders Carlssonf94cca22009-03-28 04:07:16 +000063 if (Tok.is(tok::equal))
Chris Lattnerf7b2e552007-08-25 06:57:03 +000064 // FIXME: Verify no attributes were present.
Chris Lattner9802a0a2009-04-02 04:16:50 +000065 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Anders Carlssonf94cca22009-03-28 04:07:16 +000066
Chris Lattner872b0442009-03-29 14:02:43 +000067 if (Tok.isNot(tok::l_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +000068 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner872b0442009-03-29 14:02:43 +000069 diag::err_expected_ident_lbrace);
70 return DeclPtrTy();
Chris Lattnerf7b2e552007-08-25 06:57:03 +000071 }
72
Chris Lattner872b0442009-03-29 14:02:43 +000073 SourceLocation LBrace = ConsumeBrace();
74
75 // Enter a scope for the namespace.
76 ParseScope NamespaceScope(this, Scope::DeclScope);
77
78 DeclPtrTy NamespcDecl =
79 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
80
81 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
82 PP.getSourceManager(),
83 "parsing namespace");
84
85 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
86 ParseExternalDeclaration();
87
88 // Leave the namespace scope.
89 NamespaceScope.Exit();
90
Chris Lattner9802a0a2009-04-02 04:16:50 +000091 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
92 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner872b0442009-03-29 14:02:43 +000093
Chris Lattner9802a0a2009-04-02 04:16:50 +000094 DeclEnd = RBraceLoc;
Chris Lattner872b0442009-03-29 14:02:43 +000095 return NamespcDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +000096}
Chris Lattner806a5f52008-01-12 07:05:38 +000097
Anders Carlssonf94cca22009-03-28 04:07:16 +000098/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
99/// alias definition.
100///
Anders Carlsson26de7882009-03-28 22:53:22 +0000101Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
102 SourceLocation AliasLoc,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000103 IdentifierInfo *Alias,
104 SourceLocation &DeclEnd) {
Anders Carlssonf94cca22009-03-28 04:07:16 +0000105 assert(Tok.is(tok::equal) && "Not equal token");
106
107 ConsumeToken(); // eat the '='.
108
109 CXXScopeSpec SS;
110 // Parse (optional) nested-name-specifier.
111 ParseOptionalCXXScopeSpecifier(SS);
112
113 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
114 Diag(Tok, diag::err_expected_namespace_name);
115 // Skip to end of the definition and eat the ';'.
116 SkipUntil(tok::semi);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000117 return DeclPtrTy();
Anders Carlssonf94cca22009-03-28 04:07:16 +0000118 }
119
120 // Parse identifier.
Anders Carlsson26de7882009-03-28 22:53:22 +0000121 IdentifierInfo *Ident = Tok.getIdentifierInfo();
122 SourceLocation IdentLoc = ConsumeToken();
Anders Carlssonf94cca22009-03-28 04:07:16 +0000123
124 // Eat the ';'.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000125 DeclEnd = Tok.getLocation();
Anders Carlssonf94cca22009-03-28 04:07:16 +0000126 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
127 "namespace name", tok::semi);
128
Anders Carlsson26de7882009-03-28 22:53:22 +0000129 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
130 SS, IdentLoc, Ident);
Anders Carlssonf94cca22009-03-28 04:07:16 +0000131}
132
Chris Lattner806a5f52008-01-12 07:05:38 +0000133/// ParseLinkage - We know that the current token is a string_literal
134/// and just before that, that extern was seen.
135///
136/// linkage-specification: [C++ 7.5p2: dcl.link]
137/// 'extern' string-literal '{' declaration-seq[opt] '}'
138/// 'extern' string-literal declaration
139///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000140Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) {
Douglas Gregor61818c52008-11-21 16:10:08 +0000141 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattner806a5f52008-01-12 07:05:38 +0000142 llvm::SmallVector<char, 8> LangBuffer;
143 // LangBuffer is guaranteed to be big enough.
144 LangBuffer.resize(Tok.getLength());
145 const char *LangBufPtr = &LangBuffer[0];
146 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
147
148 SourceLocation Loc = ConsumeStringToken();
Chris Lattner806a5f52008-01-12 07:05:38 +0000149
Douglas Gregord8028382009-01-05 19:45:36 +0000150 ParseScope LinkageScope(this, Scope::DeclScope);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000151 DeclPtrTy LinkageSpec
Douglas Gregord8028382009-01-05 19:45:36 +0000152 = Actions.ActOnStartLinkageSpecification(CurScope,
153 /*FIXME: */SourceLocation(),
154 Loc, LangBufPtr, StrSize,
155 Tok.is(tok::l_brace)? Tok.getLocation()
156 : SourceLocation());
157
158 if (Tok.isNot(tok::l_brace)) {
159 ParseDeclarationOrFunctionDefinition();
160 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
161 SourceLocation());
Douglas Gregorad17e372008-12-16 22:23:02 +0000162 }
163
164 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorad17e372008-12-16 22:23:02 +0000165 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregord8028382009-01-05 19:45:36 +0000166 ParseExternalDeclaration();
Chris Lattner806a5f52008-01-12 07:05:38 +0000167 }
168
Douglas Gregorad17e372008-12-16 22:23:02 +0000169 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregord8028382009-01-05 19:45:36 +0000170 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattner806a5f52008-01-12 07:05:38 +0000171}
Douglas Gregorec93f442008-04-13 21:30:24 +0000172
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000173/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
174/// using-directive. Assumes that current token is 'using'.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000175Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
176 SourceLocation &DeclEnd) {
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000177 assert(Tok.is(tok::kw_using) && "Not using token");
178
179 // Eat 'using'.
180 SourceLocation UsingLoc = ConsumeToken();
181
Chris Lattner08ab4162009-01-06 06:55:51 +0000182 if (Tok.is(tok::kw_namespace))
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000183 // Next token after 'using' is 'namespace' so it must be using-directive
Chris Lattner9802a0a2009-04-02 04:16:50 +0000184 return ParseUsingDirective(Context, UsingLoc, DeclEnd);
Chris Lattner08ab4162009-01-06 06:55:51 +0000185
186 // Otherwise, it must be using-declaration.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000187 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000188}
189
190/// ParseUsingDirective - Parse C++ using-directive, assumes
191/// that current token is 'namespace' and 'using' was already parsed.
192///
193/// using-directive: [C++ 7.3.p4: namespace.udir]
194/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
195/// namespace-name ;
196/// [GNU] using-directive:
197/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
198/// namespace-name attributes[opt] ;
199///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000200Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000201 SourceLocation UsingLoc,
202 SourceLocation &DeclEnd) {
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000203 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
204
205 // Eat 'namespace'.
206 SourceLocation NamespcLoc = ConsumeToken();
207
208 CXXScopeSpec SS;
209 // Parse (optional) nested-name-specifier.
Chris Lattnerd706dc82009-01-06 06:59:53 +0000210 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000211
212 AttributeList *AttrList = 0;
213 IdentifierInfo *NamespcName = 0;
214 SourceLocation IdentLoc = SourceLocation();
215
216 // Parse namespace-name.
Chris Lattner7898bf62009-01-06 07:27:21 +0000217 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000218 Diag(Tok, diag::err_expected_namespace_name);
219 // If there was invalid namespace name, skip to end of decl, and eat ';'.
220 SkipUntil(tok::semi);
221 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattner5261d0c2009-03-28 19:18:32 +0000222 return DeclPtrTy();
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000223 }
Chris Lattner7898bf62009-01-06 07:27:21 +0000224
225 // Parse identifier.
226 NamespcName = Tok.getIdentifierInfo();
227 IdentLoc = ConsumeToken();
228
229 // Parse (optional) attributes (most likely GNU strong-using extension).
230 if (Tok.is(tok::kw___attribute))
231 AttrList = ParseAttributes();
232
233 // Eat ';'.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000234 DeclEnd = Tok.getLocation();
Chris Lattner7898bf62009-01-06 07:27:21 +0000235 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
236 AttrList ? "attributes list" : "namespace name", tok::semi);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000237
238 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner7898bf62009-01-06 07:27:21 +0000239 IdentLoc, NamespcName, AttrList);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000240}
241
242/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
243/// 'using' was already seen.
244///
245/// using-declaration: [C++ 7.3.p3: namespace.udecl]
246/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
247/// unqualified-id [TODO]
248/// 'using' :: unqualified-id [TODO]
249///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000250Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000251 SourceLocation UsingLoc,
252 SourceLocation &DeclEnd) {
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000253 assert(false && "Not implemented");
254 // FIXME: Implement parsing.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000255 return DeclPtrTy();
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000256}
257
Anders Carlssonab041982009-03-11 16:27:10 +0000258/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
259///
260/// static_assert-declaration:
261/// static_assert ( constant-expression , string-literal ) ;
262///
Chris Lattner9802a0a2009-04-02 04:16:50 +0000263Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlssonab041982009-03-11 16:27:10 +0000264 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
265 SourceLocation StaticAssertLoc = ConsumeToken();
266
267 if (Tok.isNot(tok::l_paren)) {
268 Diag(Tok, diag::err_expected_lparen);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000269 return DeclPtrTy();
Anders Carlssonab041982009-03-11 16:27:10 +0000270 }
271
272 SourceLocation LParenLoc = ConsumeParen();
273
274 OwningExprResult AssertExpr(ParseConstantExpression());
275 if (AssertExpr.isInvalid()) {
276 SkipUntil(tok::semi);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000277 return DeclPtrTy();
Anders Carlssonab041982009-03-11 16:27:10 +0000278 }
279
Anders Carlssona24e8d52009-03-13 23:29:20 +0000280 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000281 return DeclPtrTy();
Anders Carlssona24e8d52009-03-13 23:29:20 +0000282
Anders Carlssonab041982009-03-11 16:27:10 +0000283 if (Tok.isNot(tok::string_literal)) {
284 Diag(Tok, diag::err_expected_string_literal);
285 SkipUntil(tok::semi);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000286 return DeclPtrTy();
Anders Carlssonab041982009-03-11 16:27:10 +0000287 }
288
289 OwningExprResult AssertMessage(ParseStringLiteralExpression());
290 if (AssertMessage.isInvalid())
Chris Lattner5261d0c2009-03-28 19:18:32 +0000291 return DeclPtrTy();
Anders Carlssonab041982009-03-11 16:27:10 +0000292
Anders Carlssonc45057a2009-03-15 18:44:04 +0000293 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonab041982009-03-11 16:27:10 +0000294
Chris Lattner9802a0a2009-04-02 04:16:50 +0000295 DeclEnd = Tok.getLocation();
Anders Carlssonab041982009-03-11 16:27:10 +0000296 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
297
Anders Carlssona24e8d52009-03-13 23:29:20 +0000298 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlssonc45057a2009-03-15 18:44:04 +0000299 move(AssertMessage));
Anders Carlssonab041982009-03-11 16:27:10 +0000300}
301
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000302/// ParseClassName - Parse a C++ class-name, which names a class. Note
303/// that we only check that the result names a type; semantic analysis
304/// will need to verify that the type names a class. The result is
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000305/// either a type or NULL, depending on whether a type name was
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000306/// found.
307///
308/// class-name: [C++ 9.1]
309/// identifier
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000310/// simple-template-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000311///
Douglas Gregord7cb0372009-04-01 21:51:26 +0000312Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
313 const CXXScopeSpec *SS) {
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000314 // Check whether we have a template-id that names a type.
315 if (Tok.is(tok::annot_template_id)) {
316 TemplateIdAnnotation *TemplateId
317 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000318 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000319 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000320
321 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
322 TypeTy *Type = Tok.getAnnotationValue();
323 EndLocation = Tok.getAnnotationEndLoc();
324 ConsumeToken();
Douglas Gregord7cb0372009-04-01 21:51:26 +0000325
326 if (Type)
327 return Type;
328 return true;
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000329 }
330
331 // Fall through to produce an error below.
332 }
333
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000334 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000335 Diag(Tok, diag::err_expected_class_name);
Douglas Gregord7cb0372009-04-01 21:51:26 +0000336 return true;
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000337 }
338
339 // We have an identifier; check whether it is actually a type.
Douglas Gregor1075a162009-02-04 17:00:24 +0000340 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
341 Tok.getLocation(), CurScope, SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000342 if (!Type) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000343 Diag(Tok, diag::err_expected_class_name);
Douglas Gregord7cb0372009-04-01 21:51:26 +0000344 return true;
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000345 }
346
347 // Consume the identifier.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000348 EndLocation = ConsumeToken();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000349 return Type;
350}
351
Douglas Gregorec93f442008-04-13 21:30:24 +0000352/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
353/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
354/// until we reach the start of a definition or see a token that
355/// cannot start a definition.
356///
357/// class-specifier: [C++ class]
358/// class-head '{' member-specification[opt] '}'
359/// class-head '{' member-specification[opt] '}' attributes[opt]
360/// class-head:
361/// class-key identifier[opt] base-clause[opt]
362/// class-key nested-name-specifier identifier base-clause[opt]
363/// class-key nested-name-specifier[opt] simple-template-id
364/// base-clause[opt]
365/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
366/// [GNU] class-key attributes[opt] nested-name-specifier
367/// identifier base-clause[opt]
368/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
369/// simple-template-id base-clause[opt]
370/// class-key:
371/// 'class'
372/// 'struct'
373/// 'union'
374///
375/// elaborated-type-specifier: [C++ dcl.type.elab]
376/// class-key ::[opt] nested-name-specifier[opt] identifier
377/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
378/// simple-template-id
379///
380/// Note that the C++ class-specifier and elaborated-type-specifier,
381/// together, subsume the C99 struct-or-union-specifier:
382///
383/// struct-or-union-specifier: [C99 6.7.2.1]
384/// struct-or-union identifier[opt] '{' struct-contents '}'
385/// struct-or-union identifier
386/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
387/// '}' attributes[opt]
388/// [GNU] struct-or-union attributes[opt] identifier
389/// struct-or-union:
390/// 'struct'
391/// 'union'
Chris Lattner197b4342009-04-12 21:49:30 +0000392void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
393 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000394 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000395 AccessSpecifier AS) {
Chris Lattner197b4342009-04-12 21:49:30 +0000396 DeclSpec::TST TagType;
397 if (TagTokKind == tok::kw_struct)
398 TagType = DeclSpec::TST_struct;
399 else if (TagTokKind == tok::kw_class)
400 TagType = DeclSpec::TST_class;
401 else {
402 assert(TagTokKind == tok::kw_union && "Not a class specifier");
403 TagType = DeclSpec::TST_union;
404 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000405
406 AttributeList *Attr = 0;
407 // If attributes exist after tag, parse them.
408 if (Tok.is(tok::kw___attribute))
409 Attr = ParseAttributes();
410
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000411 // If declspecs exist after tag, parse them.
412 if (Tok.is(tok::kw___declspec) && PP.getLangOptions().Microsoft)
413 FuzzyParseMicrosoftDeclSpec();
414
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000415 // Parse the (optional) nested-name-specifier.
416 CXXScopeSpec SS;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000417 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS))
418 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000419 Diag(Tok, diag::err_expected_ident);
Douglas Gregora08b6c72009-02-17 23:15:12 +0000420
421 // Parse the (optional) class name or simple-template-id.
Douglas Gregorec93f442008-04-13 21:30:24 +0000422 IdentifierInfo *Name = 0;
423 SourceLocation NameLoc;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000424 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregorec93f442008-04-13 21:30:24 +0000425 if (Tok.is(tok::identifier)) {
426 Name = Tok.getIdentifierInfo();
427 NameLoc = ConsumeToken();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000428 } else if (Tok.is(tok::annot_template_id)) {
429 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
430 NameLoc = ConsumeToken();
Douglas Gregora08b6c72009-02-17 23:15:12 +0000431
Douglas Gregoraabb8502009-03-31 00:43:58 +0000432 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000433 // The template-name in the simple-template-id refers to
434 // something other than a class template. Give an appropriate
435 // error message and skip to the ';'.
436 SourceRange Range(NameLoc);
437 if (SS.isNotEmpty())
438 Range.setBegin(SS.getBeginLoc());
Douglas Gregora08b6c72009-02-17 23:15:12 +0000439
Douglas Gregor0c281a82009-02-25 19:37:18 +0000440 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
441 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000442
Douglas Gregor0c281a82009-02-25 19:37:18 +0000443 DS.SetTypeSpecError();
444 SkipUntil(tok::semi, false, true);
445 TemplateId->Destroy();
446 return;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000447 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000448 }
449
450 // There are three options here. If we have 'struct foo;', then
451 // this is a forward declaration. If we have 'struct foo {...' or
Douglas Gregor0c281a82009-02-25 19:37:18 +0000452 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregorec93f442008-04-13 21:30:24 +0000453 // something like 'struct foo xyz', a reference.
454 Action::TagKind TK;
455 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
456 TK = Action::TK_Definition;
Anders Carlsson919a8d42009-05-11 22:25:03 +0000457 else if (Tok.is(tok::semi) && !DS.isFriendSpecified())
Douglas Gregorec93f442008-04-13 21:30:24 +0000458 TK = Action::TK_Declaration;
459 else
460 TK = Action::TK_Reference;
461
Douglas Gregor0c281a82009-02-25 19:37:18 +0000462 if (!Name && !TemplateId && TK != Action::TK_Definition) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000463 // We have a declaration or reference to an anonymous class.
Chris Lattnerf006a222008-11-18 07:48:38 +0000464 Diag(StartLoc, diag::err_anon_type_definition)
465 << DeclSpec::getSpecifierName(TagType);
Douglas Gregorec93f442008-04-13 21:30:24 +0000466
467 // Skip the rest of this declarator, up until the comma or semicolon.
468 SkipUntil(tok::comma, true);
Douglas Gregor0c281a82009-02-25 19:37:18 +0000469
470 if (TemplateId)
471 TemplateId->Destroy();
Douglas Gregorec93f442008-04-13 21:30:24 +0000472 return;
473 }
474
Douglas Gregord406b032009-02-06 22:42:48 +0000475 // Create the tag portion of the class or class template.
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000476 Action::DeclResult TagOrTempResult;
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000477 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
478
479 // FIXME: When TK == TK_Reference and we have a template-id, we need
480 // to turn that template-id into a type.
481
Douglas Gregor0c281a82009-02-25 19:37:18 +0000482 if (TemplateId && TK != Action::TK_Reference) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000483 // Explicit specialization, class template partial specialization,
484 // or explicit instantiation.
Douglas Gregor0c281a82009-02-25 19:37:18 +0000485 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
486 TemplateId->getTemplateArgs(),
487 TemplateId->getTemplateArgIsType(),
488 TemplateId->NumArgs);
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000489 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
490 TK == Action::TK_Declaration) {
491 // This is an explicit instantiation of a class template.
492 TagOrTempResult
493 = Actions.ActOnExplicitInstantiation(CurScope,
494 TemplateInfo.TemplateLoc,
495 TagType,
496 StartLoc,
497 SS,
498 TemplateTy::make(TemplateId->Template),
499 TemplateId->TemplateNameLoc,
500 TemplateId->LAngleLoc,
501 TemplateArgsPtr,
502 TemplateId->getTemplateArgLocations(),
503 TemplateId->RAngleLoc,
504 Attr);
505 } else {
506 // This is an explicit specialization or a class template
507 // partial specialization.
508 TemplateParameterLists FakedParamLists;
509
510 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
511 // This looks like an explicit instantiation, because we have
512 // something like
513 //
514 // template class Foo<X>
515 //
Douglas Gregor96b6df92009-05-14 00:28:11 +0000516 // but it actually has a definition. Most likely, this was
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000517 // meant to be an explicit specialization, but the user forgot
518 // the '<>' after 'template'.
Douglas Gregor96b6df92009-05-14 00:28:11 +0000519 assert(TK == Action::TK_Definition && "Expected a definition here");
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000520
521 SourceLocation LAngleLoc
522 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
523 Diag(TemplateId->TemplateNameLoc,
524 diag::err_explicit_instantiation_with_definition)
525 << SourceRange(TemplateInfo.TemplateLoc)
526 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
527
528 // Create a fake template parameter list that contains only
529 // "template<>", so that we treat this construct as a class
530 // template specialization.
531 FakedParamLists.push_back(
532 Actions.ActOnTemplateParameterList(0, SourceLocation(),
533 TemplateInfo.TemplateLoc,
534 LAngleLoc,
535 0, 0,
536 LAngleLoc));
537 TemplateParams = &FakedParamLists;
538 }
539
540 // Build the class template specialization.
541 TagOrTempResult
542 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK,
Douglas Gregor0c281a82009-02-25 19:37:18 +0000543 StartLoc, SS,
Douglas Gregordd13e842009-03-30 22:58:21 +0000544 TemplateTy::make(TemplateId->Template),
Douglas Gregor0c281a82009-02-25 19:37:18 +0000545 TemplateId->TemplateNameLoc,
546 TemplateId->LAngleLoc,
547 TemplateArgsPtr,
548 TemplateId->getTemplateArgLocations(),
549 TemplateId->RAngleLoc,
550 Attr,
Douglas Gregora08b6c72009-02-17 23:15:12 +0000551 Action::MultiTemplateParamsArg(Actions,
552 TemplateParams? &(*TemplateParams)[0] : 0,
553 TemplateParams? TemplateParams->size() : 0));
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000554 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000555 TemplateId->Destroy();
Douglas Gregor96b6df92009-05-14 00:28:11 +0000556 } else if (TemplateParams && TK != Action::TK_Reference) {
557 // Class template declaration or definition.
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000558 TagOrTempResult = Actions.ActOnClassTemplate(CurScope, TagType, TK,
559 StartLoc, SS, Name, NameLoc,
560 Attr,
Douglas Gregord406b032009-02-06 22:42:48 +0000561 Action::MultiTemplateParamsArg(Actions,
562 &(*TemplateParams)[0],
Anders Carlssoned20fb92009-03-26 00:52:18 +0000563 TemplateParams->size()),
564 AS);
Douglas Gregor96b6df92009-05-14 00:28:11 +0000565 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
566 TK == Action::TK_Declaration) {
567 // Explicit instantiation of a member of a class template
568 // specialization, e.g.,
569 //
570 // template struct Outer<int>::Inner;
571 //
572 TagOrTempResult
573 = Actions.ActOnExplicitInstantiation(CurScope,
574 TemplateInfo.TemplateLoc,
575 TagType, StartLoc, SS, Name,
576 NameLoc, Attr);
577 } else {
578 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
579 TK == Action::TK_Definition) {
580 // FIXME: Diagnose this particular error.
581 }
582
583 // Declaration or definition of a class type
584 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS,
585 Name, NameLoc, Attr, AS);
586 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000587
588 // Parse the optional base clause (C++ only).
Chris Lattner31ccf0a2009-02-16 22:07:16 +0000589 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000590 ParseBaseClause(TagOrTempResult.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000591
592 // If there is a body, parse it and inform the actions module.
593 if (Tok.is(tok::l_brace))
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000594 if (getLang().CPlusPlus)
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000595 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000596 else
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000597 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000598 else if (TK == Action::TK_Definition) {
599 // FIXME: Complain that we have a base-specifier list but no
600 // definition.
Chris Lattnerf006a222008-11-18 07:48:38 +0000601 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregorec93f442008-04-13 21:30:24 +0000602 }
603
604 const char *PrevSpec = 0;
Anders Carlssonef3fa4f2009-05-11 22:27:47 +0000605 if (TagOrTempResult.isInvalid()) {
Douglas Gregord406b032009-02-06 22:42:48 +0000606 DS.SetTypeSpecError();
Anders Carlssonef3fa4f2009-05-11 22:27:47 +0000607 return;
608 }
609
Anders Carlssonef3fa4f2009-05-11 22:27:47 +0000610 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec,
611 TagOrTempResult.get().getAs<void>()))
Chris Lattnerf006a222008-11-18 07:48:38 +0000612 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Anders Carlssone5e645d2009-05-11 22:42:30 +0000613
614 if (DS.isFriendSpecified())
615 Actions.ActOnFriendDecl(CurScope, DS.getFriendSpecLoc(),
616 TagOrTempResult.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000617}
618
619/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
620///
621/// base-clause : [C++ class.derived]
622/// ':' base-specifier-list
623/// base-specifier-list:
624/// base-specifier '...'[opt]
625/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattner5261d0c2009-03-28 19:18:32 +0000626void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000627 assert(Tok.is(tok::colon) && "Not a base clause");
628 ConsumeToken();
629
Douglas Gregorabed2172008-10-22 17:49:05 +0000630 // Build up an array of parsed base specifiers.
631 llvm::SmallVector<BaseTy *, 8> BaseInfo;
632
Douglas Gregorec93f442008-04-13 21:30:24 +0000633 while (true) {
634 // Parse a base-specifier.
Douglas Gregorabed2172008-10-22 17:49:05 +0000635 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000636 if (Result.isInvalid()) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000637 // Skip the rest of this base specifier, up until the comma or
638 // opening brace.
Douglas Gregorabed2172008-10-22 17:49:05 +0000639 SkipUntil(tok::comma, tok::l_brace, true, true);
640 } else {
641 // Add this to our array of base specifiers.
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000642 BaseInfo.push_back(Result.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000643 }
644
645 // If the next token is a comma, consume it and keep reading
646 // base-specifiers.
647 if (Tok.isNot(tok::comma)) break;
648
649 // Consume the comma.
650 ConsumeToken();
651 }
Douglas Gregorabed2172008-10-22 17:49:05 +0000652
653 // Attach the base specifiers
Jay Foad9e6bef42009-05-21 09:52:38 +0000654 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregorec93f442008-04-13 21:30:24 +0000655}
656
657/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
658/// one entry in the base class list of a class specifier, for example:
659/// class foo : public bar, virtual private baz {
660/// 'public bar' and 'virtual private baz' are each base-specifiers.
661///
662/// base-specifier: [C++ class.derived]
663/// ::[opt] nested-name-specifier[opt] class-name
664/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
665/// class-name
666/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
667/// class-name
Chris Lattner5261d0c2009-03-28 19:18:32 +0000668Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000669 bool IsVirtual = false;
670 SourceLocation StartLoc = Tok.getLocation();
671
672 // Parse the 'virtual' keyword.
673 if (Tok.is(tok::kw_virtual)) {
674 ConsumeToken();
675 IsVirtual = true;
676 }
677
678 // Parse an (optional) access specifier.
679 AccessSpecifier Access = getAccessSpecifierIfPresent();
680 if (Access)
681 ConsumeToken();
682
683 // Parse the 'virtual' keyword (again!), in case it came after the
684 // access specifier.
685 if (Tok.is(tok::kw_virtual)) {
686 SourceLocation VirtualLoc = ConsumeToken();
687 if (IsVirtual) {
688 // Complain about duplicate 'virtual'
Chris Lattnerf006a222008-11-18 07:48:38 +0000689 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregord7cb0372009-04-01 21:51:26 +0000690 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregorec93f442008-04-13 21:30:24 +0000691 }
692
693 IsVirtual = true;
694 }
695
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000696 // Parse optional '::' and optional nested-name-specifier.
697 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +0000698 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregorec93f442008-04-13 21:30:24 +0000699
Douglas Gregorec93f442008-04-13 21:30:24 +0000700 // The location of the base class itself.
701 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000702
703 // Parse the class-name.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000704 SourceLocation EndLocation;
Douglas Gregord7cb0372009-04-01 21:51:26 +0000705 TypeResult BaseType = ParseClassName(EndLocation, &SS);
706 if (BaseType.isInvalid())
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000707 return true;
Douglas Gregorec93f442008-04-13 21:30:24 +0000708
709 // Find the complete source range for the base-specifier.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000710 SourceRange Range(StartLoc, EndLocation);
Douglas Gregorec93f442008-04-13 21:30:24 +0000711
Douglas Gregorec93f442008-04-13 21:30:24 +0000712 // Notify semantic analysis that we have parsed a complete
713 // base-specifier.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000714 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregord7cb0372009-04-01 21:51:26 +0000715 BaseType.get(), BaseLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000716}
717
718/// getAccessSpecifierIfPresent - Determine whether the next token is
719/// a C++ access-specifier.
720///
721/// access-specifier: [C++ class.derived]
722/// 'private'
723/// 'protected'
724/// 'public'
Douglas Gregor696be932008-04-14 00:13:42 +0000725AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregorec93f442008-04-13 21:30:24 +0000726{
727 switch (Tok.getKind()) {
728 default: return AS_none;
729 case tok::kw_private: return AS_private;
730 case tok::kw_protected: return AS_protected;
731 case tok::kw_public: return AS_public;
732 }
733}
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000734
735/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
736///
737/// member-declaration:
738/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
739/// function-definition ';'[opt]
740/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
741/// using-declaration [TODO]
Anders Carlssonab041982009-03-11 16:27:10 +0000742/// [C++0x] static_assert-declaration
Anders Carlssoned20fb92009-03-26 00:52:18 +0000743/// template-declaration
Chris Lattnerf3375de2008-12-18 01:12:00 +0000744/// [GNU] '__extension__' member-declaration
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000745///
746/// member-declarator-list:
747/// member-declarator
748/// member-declarator-list ',' member-declarator
749///
750/// member-declarator:
751/// declarator pure-specifier[opt]
752/// declarator constant-initializer[opt]
753/// identifier[opt] ':' constant-expression
754///
Sebastian Redla55834a2009-04-12 17:16:29 +0000755/// pure-specifier:
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000756/// '= 0'
757///
758/// constant-initializer:
759/// '=' constant-expression
760///
Chris Lattnera17991f2009-03-29 16:50:03 +0000761void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Anders Carlssonab041982009-03-11 16:27:10 +0000762 // static_assert-declaration
Chris Lattnera17991f2009-03-29 16:50:03 +0000763 if (Tok.is(tok::kw_static_assert)) {
Chris Lattner9802a0a2009-04-02 04:16:50 +0000764 SourceLocation DeclEnd;
765 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000766 return;
767 }
Anders Carlssonab041982009-03-11 16:27:10 +0000768
Chris Lattnera17991f2009-03-29 16:50:03 +0000769 if (Tok.is(tok::kw_template)) {
Chris Lattner9802a0a2009-04-02 04:16:50 +0000770 SourceLocation DeclEnd;
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000771 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
772 AS);
Chris Lattnera17991f2009-03-29 16:50:03 +0000773 return;
774 }
Anders Carlssoned20fb92009-03-26 00:52:18 +0000775
Chris Lattnerf3375de2008-12-18 01:12:00 +0000776 // Handle: member-declaration ::= '__extension__' member-declaration
777 if (Tok.is(tok::kw___extension__)) {
778 // __extension__ silences extension warnings in the subexpression.
779 ExtensionRAIIObject O(Diags); // Use RAII to do this.
780 ConsumeToken();
781 return ParseCXXClassMemberDeclaration(AS);
782 }
783
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000784 SourceLocation DSStart = Tok.getLocation();
785 // decl-specifier-seq:
786 // Parse the common declaration-specifiers piece.
787 DeclSpec DS;
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000788 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000789
790 if (Tok.is(tok::semi)) {
791 ConsumeToken();
792 // C++ 9.2p7: The member-declarator-list can be omitted only after a
793 // class-specifier or an enum-specifier or in a friend declaration.
794 // FIXME: Friend declarations.
795 switch (DS.getTypeSpecType()) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000796 case DeclSpec::TST_struct:
797 case DeclSpec::TST_union:
798 case DeclSpec::TST_class:
799 case DeclSpec::TST_enum:
800 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
801 return;
802 default:
803 Diag(DSStart, diag::err_no_declarators);
804 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000805 }
806 }
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000807
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000808 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000809
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000810 if (Tok.isNot(tok::colon)) {
811 // Parse the first declarator.
812 ParseDeclarator(DeclaratorInfo);
813 // Error parsing the declarator?
Douglas Gregor6704b312008-11-17 22:58:34 +0000814 if (!DeclaratorInfo.hasName()) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000815 // If so, skip until the semi-colon or a }.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000816 SkipUntil(tok::r_brace, true);
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000817 if (Tok.is(tok::semi))
818 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000819 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000820 }
821
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000822 // function-definition:
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000823 if (Tok.is(tok::l_brace)
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000824 || (DeclaratorInfo.isFunctionDeclarator() &&
825 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000826 if (!DeclaratorInfo.isFunctionDeclarator()) {
827 Diag(Tok, diag::err_func_def_no_params);
828 ConsumeBrace();
829 SkipUntil(tok::r_brace, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000830 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000831 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000832
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000833 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
834 Diag(Tok, diag::err_function_declared_typedef);
835 // This recovery skips the entire function body. It would be nice
836 // to simply call ParseCXXInlineMethodDef() below, however Sema
837 // assumes the declarator represents a function, not a typedef.
838 ConsumeBrace();
839 SkipUntil(tok::r_brace, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000840 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000841 }
842
Chris Lattnera17991f2009-03-29 16:50:03 +0000843 ParseCXXInlineMethodDef(AS, DeclaratorInfo);
844 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000845 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000846 }
847
848 // member-declarator-list:
849 // member-declarator
850 // member-declarator-list ',' member-declarator
851
Chris Lattnera17991f2009-03-29 16:50:03 +0000852 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl62261042008-12-09 20:22:58 +0000853 OwningExprResult BitfieldSize(Actions);
854 OwningExprResult Init(Actions);
Sebastian Redla55834a2009-04-12 17:16:29 +0000855 bool Deleted = false;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000856
857 while (1) {
858
859 // member-declarator:
860 // declarator pure-specifier[opt]
861 // declarator constant-initializer[opt]
862 // identifier[opt] ':' constant-expression
863
864 if (Tok.is(tok::colon)) {
865 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000866 BitfieldSize = ParseConstantExpression();
867 if (BitfieldSize.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000868 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000869 }
870
871 // pure-specifier:
872 // '= 0'
873 //
874 // constant-initializer:
875 // '=' constant-expression
Sebastian Redla55834a2009-04-12 17:16:29 +0000876 //
877 // defaulted/deleted function-definition:
878 // '=' 'default' [TODO]
879 // '=' 'delete'
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000880
881 if (Tok.is(tok::equal)) {
882 ConsumeToken();
Sebastian Redla55834a2009-04-12 17:16:29 +0000883 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
884 ConsumeToken();
885 Deleted = true;
886 } else {
887 Init = ParseInitializer();
888 if (Init.isInvalid())
889 SkipUntil(tok::comma, true, true);
890 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000891 }
892
893 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +0000894 if (Tok.is(tok::kw___attribute)) {
895 SourceLocation Loc;
896 AttributeList *AttrList = ParseAttributes(&Loc);
897 DeclaratorInfo.AddAttributes(AttrList, Loc);
898 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000899
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000900 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattnera17991f2009-03-29 16:50:03 +0000901 // this call will *not* return the created decl; It will return null.
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000902 // See Sema::ActOnCXXMemberDeclarator for details.
Chris Lattnera17991f2009-03-29 16:50:03 +0000903 DeclPtrTy ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
904 DeclaratorInfo,
905 BitfieldSize.release(),
Sebastian Redla55834a2009-04-12 17:16:29 +0000906 Init.release(),
907 Deleted);
Chris Lattnera17991f2009-03-29 16:50:03 +0000908 if (ThisDecl)
909 DeclsInGroup.push_back(ThisDecl);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000910
Douglas Gregor605de8d2008-12-16 21:30:33 +0000911 if (DeclaratorInfo.isFunctionDeclarator() &&
912 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
913 != DeclSpec::SCS_typedef) {
914 // We just declared a member function. If this member function
915 // has any default arguments, we'll need to parse them later.
916 LateParsedMethodDeclaration *LateMethod = 0;
917 DeclaratorChunk::FunctionTypeInfo &FTI
918 = DeclaratorInfo.getTypeObject(0).Fun;
919 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
920 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
921 if (!LateMethod) {
922 // Push this method onto the stack of late-parsed method
923 // declarations.
Douglas Gregora376cbd2009-05-27 23:11:45 +0000924 getCurrentClass().MethodDecls.push_back(
Chris Lattnera17991f2009-03-29 16:50:03 +0000925 LateParsedMethodDeclaration(ThisDecl));
Douglas Gregora376cbd2009-05-27 23:11:45 +0000926 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregor605de8d2008-12-16 21:30:33 +0000927
928 // Add all of the parameters prior to this one (they don't
929 // have default arguments).
930 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
931 for (unsigned I = 0; I < ParamIdx; ++I)
932 LateMethod->DefaultArgs.push_back(
933 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
934 }
935
936 // Add this parameter to the list of parameters (it or may
937 // not have a default argument).
938 LateMethod->DefaultArgs.push_back(
939 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
940 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
941 }
942 }
943 }
944
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000945 // If we don't have a comma, it is either the end of the list (a ';')
946 // or an error, bail out.
947 if (Tok.isNot(tok::comma))
948 break;
949
950 // Consume the comma.
951 ConsumeToken();
952
953 // Parse the next declarator.
954 DeclaratorInfo.clear();
Sebastian Redl62261042008-12-09 20:22:58 +0000955 BitfieldSize = 0;
956 Init = 0;
Sebastian Redla55834a2009-04-12 17:16:29 +0000957 Deleted = false;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000958
959 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +0000960 if (Tok.is(tok::kw___attribute)) {
961 SourceLocation Loc;
962 AttributeList *AttrList = ParseAttributes(&Loc);
963 DeclaratorInfo.AddAttributes(AttrList, Loc);
964 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000965
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000966 if (Tok.isNot(tok::colon))
967 ParseDeclarator(DeclaratorInfo);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000968 }
969
970 if (Tok.is(tok::semi)) {
971 ConsumeToken();
Jay Foad9e6bef42009-05-21 09:52:38 +0000972 Actions.FinalizeDeclaratorGroup(CurScope, DeclsInGroup.data(),
Chris Lattnera17991f2009-03-29 16:50:03 +0000973 DeclsInGroup.size());
974 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000975 }
976
977 Diag(Tok, diag::err_expected_semi_decl_list);
978 // Skip to end of block or statement
979 SkipUntil(tok::r_brace, true, true);
980 if (Tok.is(tok::semi))
981 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000982 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000983}
984
985/// ParseCXXMemberSpecification - Parse the class definition.
986///
987/// member-specification:
988/// member-declaration member-specification[opt]
989/// access-specifier ':' member-specification[opt]
990///
991void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000992 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Guptafa451432008-10-31 09:52:39 +0000993 assert((TagType == DeclSpec::TST_struct ||
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000994 TagType == DeclSpec::TST_union ||
Sanjiv Guptafa451432008-10-31 09:52:39 +0000995 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000996
Chris Lattnerc309ade2009-03-05 08:00:35 +0000997 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
998 PP.getSourceManager(),
999 "parsing struct/union/class body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001000
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001001 SourceLocation LBraceLoc = ConsumeBrace();
1002
Douglas Gregora376cbd2009-05-27 23:11:45 +00001003 // Determine whether this is a top-level (non-nested) class.
1004 bool TopLevelClass = ClassStack.empty() ||
1005 CurScope->isInCXXInlineMethodScope();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001006
1007 // Enter a scope for the class.
Douglas Gregorcab994d2009-01-09 22:42:13 +00001008 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001009
Douglas Gregora376cbd2009-05-27 23:11:45 +00001010 // Note that we are parsing a new (potentially-nested) class definition.
1011 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1012
Douglas Gregord406b032009-02-06 22:42:48 +00001013 if (TagDecl)
1014 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1015 else {
1016 SkipUntil(tok::r_brace, false, false);
1017 return;
1018 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001019
1020 // C++ 11p3: Members of a class defined with the keyword class are private
1021 // by default. Members of a class defined with the keywords struct or union
1022 // are public by default.
1023 AccessSpecifier CurAS;
1024 if (TagType == DeclSpec::TST_class)
1025 CurAS = AS_private;
1026 else
1027 CurAS = AS_public;
1028
1029 // While we still have something to read, read the member-declarations.
1030 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1031 // Each iteration of this loop reads one member-declaration.
1032
1033 // Check for extraneous top-level semicolon.
1034 if (Tok.is(tok::semi)) {
1035 Diag(Tok, diag::ext_extra_struct_semi);
1036 ConsumeToken();
1037 continue;
1038 }
1039
1040 AccessSpecifier AS = getAccessSpecifierIfPresent();
1041 if (AS != AS_none) {
1042 // Current token is a C++ access specifier.
1043 CurAS = AS;
1044 ConsumeToken();
1045 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1046 continue;
1047 }
1048
1049 // Parse all the comma separated declarators.
1050 ParseCXXClassMemberDeclaration(CurAS);
1051 }
1052
1053 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1054
1055 AttributeList *AttrList = 0;
1056 // If attributes exist after class contents, parse them.
1057 if (Tok.is(tok::kw___attribute))
1058 AttrList = ParseAttributes(); // FIXME: where should I put them?
1059
1060 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1061 LBraceLoc, RBraceLoc);
1062
1063 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1064 // complete within function bodies, default arguments,
1065 // exception-specifications, and constructor ctor-initializers (including
1066 // such things in nested classes).
1067 //
Douglas Gregor605de8d2008-12-16 21:30:33 +00001068 // FIXME: Only function bodies and constructor ctor-initializers are
1069 // parsed correctly, fix the rest.
Douglas Gregora376cbd2009-05-27 23:11:45 +00001070 if (TopLevelClass) {
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001071 // We are not inside a nested class. This class and its nested classes
Douglas Gregor605de8d2008-12-16 21:30:33 +00001072 // are complete and we can parse the delayed portions of method
1073 // declarations and the lexed inline method definitions.
Douglas Gregora376cbd2009-05-27 23:11:45 +00001074 ParseLexedMethodDeclarations(getCurrentClass());
1075 ParseLexedMethodDefs(getCurrentClass());
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001076 }
1077
1078 // Leave the class scope.
Douglas Gregora376cbd2009-05-27 23:11:45 +00001079 ParsingDef.Pop();
Douglas Gregor95d40792008-12-10 06:34:36 +00001080 ClassScope.Exit();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001081
Douglas Gregordb568cf2009-01-08 20:45:30 +00001082 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001083}
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001084
1085/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1086/// which explicitly initializes the members or base classes of a
1087/// class (C++ [class.base.init]). For example, the three initializers
1088/// after the ':' in the Derived constructor below:
1089///
1090/// @code
1091/// class Base { };
1092/// class Derived : Base {
1093/// int x;
1094/// float f;
1095/// public:
1096/// Derived(float f) : Base(), x(17), f(f) { }
1097/// };
1098/// @endcode
1099///
1100/// [C++] ctor-initializer:
1101/// ':' mem-initializer-list
1102///
1103/// [C++] mem-initializer-list:
1104/// mem-initializer
1105/// mem-initializer , mem-initializer-list
Chris Lattner5261d0c2009-03-28 19:18:32 +00001106void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001107 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1108
1109 SourceLocation ColonLoc = ConsumeToken();
1110
1111 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
1112
1113 do {
1114 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +00001115 if (!MemInit.isInvalid())
1116 MemInitializers.push_back(MemInit.get());
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001117
1118 if (Tok.is(tok::comma))
1119 ConsumeToken();
1120 else if (Tok.is(tok::l_brace))
1121 break;
1122 else {
1123 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redlbc9ef252009-04-26 20:35:05 +00001124 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001125 SkipUntil(tok::l_brace, true, true);
1126 break;
1127 }
1128 } while (true);
1129
1130 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +00001131 MemInitializers.data(), MemInitializers.size());
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001132}
1133
1134/// ParseMemInitializer - Parse a C++ member initializer, which is
1135/// part of a constructor initializer that explicitly initializes one
1136/// member or base class (C++ [class.base.init]). See
1137/// ParseConstructorInitializer for an example.
1138///
1139/// [C++] mem-initializer:
1140/// mem-initializer-id '(' expression-list[opt] ')'
1141///
1142/// [C++] mem-initializer-id:
1143/// '::'[opt] nested-name-specifier[opt] class-name
1144/// identifier
Chris Lattner5261d0c2009-03-28 19:18:32 +00001145Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001146 // FIXME: parse '::'[opt] nested-name-specifier[opt]
1147
1148 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001149 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001150 return true;
1151 }
1152
1153 // Get the identifier. This may be a member name or a class name,
1154 // but we'll let the semantic analysis determine which it is.
1155 IdentifierInfo *II = Tok.getIdentifierInfo();
1156 SourceLocation IdLoc = ConsumeToken();
1157
1158 // Parse the '('.
1159 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001160 Diag(Tok, diag::err_expected_lparen);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001161 return true;
1162 }
1163 SourceLocation LParenLoc = ConsumeParen();
1164
1165 // Parse the optional expression-list.
Sebastian Redl6008ac32008-11-25 22:21:31 +00001166 ExprVector ArgExprs(Actions);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001167 CommaLocsTy CommaLocs;
1168 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1169 SkipUntil(tok::r_paren);
1170 return true;
1171 }
1172
1173 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1174
Sebastian Redl6008ac32008-11-25 22:21:31 +00001175 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
1176 LParenLoc, ArgExprs.take(),
Jay Foad9e6bef42009-05-21 09:52:38 +00001177 ArgExprs.size(), CommaLocs.data(),
1178 RParenLoc);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001179}
Douglas Gregor90a2c972008-11-25 03:22:00 +00001180
1181/// ParseExceptionSpecification - Parse a C++ exception-specification
1182/// (C++ [except.spec]).
1183///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001184/// exception-specification:
1185/// 'throw' '(' type-id-list [opt] ')'
1186/// [MS] 'throw' '(' '...' ')'
Douglas Gregor90a2c972008-11-25 03:22:00 +00001187///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001188/// type-id-list:
1189/// type-id
1190/// type-id-list ',' type-id
Douglas Gregor90a2c972008-11-25 03:22:00 +00001191///
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001192bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
1193 std::vector<TypeTy*> &Exceptions,
1194 bool &hasAnyExceptionSpec) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00001195 assert(Tok.is(tok::kw_throw) && "expected throw");
1196
1197 SourceLocation ThrowLoc = ConsumeToken();
1198
1199 if (!Tok.is(tok::l_paren)) {
1200 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1201 }
1202 SourceLocation LParenLoc = ConsumeParen();
1203
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001204 // Parse throw(...), a Microsoft extension that means "this function
1205 // can throw anything".
1206 if (Tok.is(tok::ellipsis)) {
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001207 hasAnyExceptionSpec = true;
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001208 SourceLocation EllipsisLoc = ConsumeToken();
1209 if (!getLang().Microsoft)
1210 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redl0c986032009-02-09 18:23:29 +00001211 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001212 return false;
1213 }
1214
Douglas Gregor90a2c972008-11-25 03:22:00 +00001215 // Parse the sequence of type-ids.
1216 while (Tok.isNot(tok::r_paren)) {
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001217 TypeResult Res(ParseTypeName());
1218 if (!Res.isInvalid())
1219 Exceptions.push_back(Res.get());
Douglas Gregor90a2c972008-11-25 03:22:00 +00001220 if (Tok.is(tok::comma))
1221 ConsumeToken();
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001222 else
Douglas Gregor90a2c972008-11-25 03:22:00 +00001223 break;
1224 }
1225
Sebastian Redl0c986032009-02-09 18:23:29 +00001226 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001227 return false;
1228}
Douglas Gregora376cbd2009-05-27 23:11:45 +00001229
1230/// \brief We have just started parsing the definition of a new class,
1231/// so push that class onto our stack of classes that is currently
1232/// being parsed.
1233void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
1234 assert((TopLevelClass || !ClassStack.empty()) &&
1235 "Nested class without outer class");
1236 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1237}
1238
1239/// \brief Deallocate the given parsed class and all of its nested
1240/// classes.
1241void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1242 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1243 DeallocateParsedClasses(Class->NestedClasses[I]);
1244 delete Class;
1245}
1246
1247/// \brief Pop the top class of the stack of classes that are
1248/// currently being parsed.
1249///
1250/// This routine should be called when we have finished parsing the
1251/// definition of a class, but have not yet popped the Scope
1252/// associated with the class's definition.
1253///
1254/// \returns true if the class we've popped is a top-level class,
1255/// false otherwise.
1256void Parser::PopParsingClass() {
1257 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
1258
1259 ParsingClass *Victim = ClassStack.top();
1260 ClassStack.pop();
1261 if (Victim->TopLevelClass) {
1262 // Deallocate all of the nested classes of this class,
1263 // recursively: we don't need to keep any of this information.
1264 DeallocateParsedClasses(Victim);
1265 return;
1266 }
1267 assert(!ClassStack.empty() && "Missing top-level class?");
1268
1269 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1270 Victim->NestedClasses.empty()) {
1271 // The victim is a nested class, but we will not need to perform
1272 // any processing after the definition of this class since it has
1273 // no members whose handling was delayed. Therefore, we can just
1274 // remove this nested class.
1275 delete Victim;
1276 return;
1277 }
1278
1279 // This nested class has some members that will need to be processed
1280 // after the top-level class is completely defined. Therefore, add
1281 // it to the list of nested classes within its parent.
1282 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1283 ClassStack.top()->NestedClasses.push_back(Victim);
1284 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1285}