blob: fc3e6ae17f0ec2afeb1764d8f04d380b58dd8035 [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 '}'
Mike Stump25cf7602009-09-09 15:08:12 +000041///
Chris Lattnerf7b2e552007-08-25 06:57:03 +000042/// 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'.
Mike Stump25cf7602009-09-09 15:08:12 +000049
Chris Lattnerf7b2e552007-08-25 06:57:03 +000050 SourceLocation IdentLoc;
51 IdentifierInfo *Ident = 0;
Douglas Gregorb6d226f2009-06-17 19:49:00 +000052
53 Token attrTok;
Mike Stump25cf7602009-09-09 15:08:12 +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 }
Mike Stump25cf7602009-09-09 15:08:12 +000059
Chris Lattnerf7b2e552007-08-25 06:57:03 +000060 // 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 }
Mike Stump25cf7602009-09-09 15:08:12 +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 }
Mike Stump25cf7602009-09-09 15:08:12 +000075
Chris Lattner872b0442009-03-29 14:02:43 +000076 if (Tok.isNot(tok::l_brace)) {
Mike Stump25cf7602009-09-09 15:08:12 +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 }
Mike Stump25cf7602009-09-09 15:08:12 +000081
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");
Mike Stump25cf7602009-09-09 15:08:12 +000093
Chris Lattner872b0442009-03-29 14:02:43 +000094 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
95 ParseExternalDeclaration();
Mike Stump25cf7602009-09-09 15:08:12 +000096
Chris Lattner872b0442009-03-29 14:02:43 +000097 // 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,
Mike Stump25cf7602009-09-09 15:08:12 +0000111 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");
Mike Stump25cf7602009-09-09 15:08:12 +0000115
Anders Carlssonf94cca22009-03-28 04:07:16 +0000116 ConsumeToken(); // eat the '='.
Mike Stump25cf7602009-09-09 15:08:12 +0000117
Anders Carlssonf94cca22009-03-28 04:07:16 +0000118 CXXScopeSpec SS;
119 // Parse (optional) nested-name-specifier.
Douglas Gregor681d31d2009-09-02 22:59:36 +0000120 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Anders Carlssonf94cca22009-03-28 04:07:16 +0000121
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();
Mike Stump25cf7602009-09-09 15:08:12 +0000132
Anders Carlssonf94cca22009-03-28 04:07:16 +0000133 // 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);
Mike Stump25cf7602009-09-09 15:08:12 +0000137
138 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
Anders Carlsson26de7882009-03-28 22:53:22 +0000139 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);
Mike Stump25cf7602009-09-09 15:08:12 +0000160 DeclPtrTy LinkageSpec
161 = Actions.ActOnStartLinkageSpecification(CurScope,
Douglas Gregord8028382009-01-05 19:45:36 +0000162 /*FIXME: */SourceLocation(),
163 Loc, LangBufPtr, StrSize,
Mike Stump25cf7602009-09-09 15:08:12 +0000164 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregord8028382009-01-05 19:45:36 +0000165 : SourceLocation());
166
167 if (Tok.isNot(tok::l_brace)) {
168 ParseDeclarationOrFunctionDefinition();
Mike Stump25cf7602009-09-09 15:08:12 +0000169 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
Douglas Gregord8028382009-01-05 19:45:36 +0000170 SourceLocation());
Mike Stump25cf7602009-09-09 15:08:12 +0000171 }
Douglas Gregorad17e372008-12-16 22:23:02 +0000172
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.
Douglas Gregor681d31d2009-09-02 22:59:36 +0000219 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
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 }
Mike Stump25cf7602009-09-09 15:08:12 +0000233
Chris Lattner7898bf62009-01-06 07:27:21 +0000234 // Parse identifier.
235 NamespcName = Tok.getIdentifierInfo();
236 IdentLoc = ConsumeToken();
Mike Stump25cf7602009-09-09 15:08:12 +0000237
Chris Lattner7898bf62009-01-06 07:27:21 +0000238 // Parse (optional) attributes (most likely GNU strong-using extension).
239 if (Tok.is(tok::kw___attribute))
240 AttrList = ParseAttributes();
Mike Stump25cf7602009-09-09 15:08:12 +0000241
Chris Lattner7898bf62009-01-06 07:27:21 +0000242 // 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.
Douglas Gregor681d31d2009-09-02 22:59:36 +0000276 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor683a1142009-06-20 00:51:54 +0000277
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:
Mike Stump25cf7602009-09-09 15:08:12 +0000287 // A using-declaration shall not name a template-id.
Anders Carlsson66b082a2009-08-28 03:35:18 +0000288 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 }
Mike Stump25cf7602009-09-09 15:08:12 +0000292
Anders Carlssone8c36f22009-06-27 00:27:47 +0000293 IdentifierInfo *TargetName = 0;
294 OverloadedOperatorKind Op = OO_None;
295 SourceLocation IdentLoc;
Mike Stump25cf7602009-09-09 15:08:12 +0000296
Anders Carlssone8c36f22009-06-27 00:27:47 +0000297 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 }
Mike Stump25cf7602009-09-09 15:08:12 +0000318
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();
Mike Stump25cf7602009-09-09 15:08:12 +0000322
Douglas Gregor683a1142009-06-20 00:51:54 +0000323 // 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();
Mike Stump25cf7602009-09-09 15:08:12 +0000341
Anders Carlssonab041982009-03-11 16:27:10 +0000342 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 }
Mike Stump25cf7602009-09-09 15:08:12 +0000346
Anders Carlssonab041982009-03-11 16:27:10 +0000347 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 }
Mike Stump25cf7602009-09-09 15:08:12 +0000354
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 }
Mike Stump25cf7602009-09-09 15:08:12 +0000363
Anders Carlssonab041982009-03-11 16:27:10 +0000364 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump25cf7602009-09-09 15:08:12 +0000365 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);
Mike Stump25cf7602009-09-09 15:08:12 +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
Mike Stump25cf7602009-09-09 15:08:12 +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();
Mike Stump25cf7602009-09-09 15:08:12 +0000386
387 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlssoneed418b2009-06-24 17:47:40 +0000388 "decltype")) {
389 SkipUntil(tok::r_paren);
390 return;
391 }
Mike Stump25cf7602009-09-09 15:08:12 +0000392
Anders Carlssoneed418b2009-06-24 17:47:40 +0000393 // Parse the expression
Mike Stump25cf7602009-09-09 15:08:12 +0000394
Anders Carlssoneed418b2009-06-24 17:47:40 +0000395 // 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 }
Mike Stump25cf7602009-09-09 15:08:12 +0000404
Anders Carlssoneed418b2009-06-24 17:47:40 +0000405 // Match the ')'
406 SourceLocation RParenLoc;
407 if (Tok.is(tok::r_paren))
408 RParenLoc = ConsumeParen();
409 else
410 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump25cf7602009-09-09 15:08:12 +0000411
Anders Carlssoneed418b2009-06-24 17:47:40 +0000412 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)").
Mike Stump25cf7602009-09-09 15:08:12 +0000418 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
Mike Stump25cf7602009-09-09 15:08:12 +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)) {
Mike Stump25cf7602009-09-09 15:08:12 +0000438 TemplateIdAnnotation *TemplateId
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000439 = 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.
Mike Stump25cf7602009-09-09 15:08:12 +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) {
Mike Stump25cf7602009-09-09 15:08:12 +0000466 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
Fariborz Jahanian1b8fd752009-07-20 17:43:15 +0000467 : 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]
Mike Stump25cf7602009-09-09 15:08:12 +0000490/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregorec93f442008-04-13 21:30:24 +0000491/// identifier base-clause[opt]
Mike Stump25cf7602009-09-09 15:08:12 +0000492/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregorec93f442008-04-13 21:30:24 +0000493/// simple-template-id base-clause[opt]
494/// class-key:
495/// 'class'
496/// 'struct'
497/// 'union'
498///
499/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump25cf7602009-09-09 15:08:12 +0000500/// class-key ::[opt] nested-name-specifier[opt] identifier
501/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
502/// simple-template-id
Douglas Gregorec93f442008-04-13 21:30:24 +0000503///
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);
Mike Stump25cf7602009-09-09 15:08:12 +0000538
Douglas Gregor7d23bf42009-09-04 05:53:02 +0000539 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
540 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
541 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump25cf7602009-09-09 15:08:12 +0000542 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregor7d23bf42009-09-04 05:53:02 +0000543 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
544 // properly.
545 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
546 Tok.setKind(tok::identifier);
547 }
548
549 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
550 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
551 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump25cf7602009-09-09 15:08:12 +0000552 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregor7d23bf42009-09-04 05:53:02 +0000553 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
554 // properly.
555 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
556 Tok.setKind(tok::identifier);
557 }
Mike Stump25cf7602009-09-09 15:08:12 +0000558
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000559 // Parse the (optional) nested-name-specifier.
560 CXXScopeSpec SS;
Mike Stump25cf7602009-09-09 15:08:12 +0000561 if (getLang().CPlusPlus &&
Douglas Gregor681d31d2009-09-02 22:59:36 +0000562 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
Douglas Gregor0c281a82009-02-25 19:37:18 +0000563 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000564 Diag(Tok, diag::err_expected_ident);
Douglas Gregora08b6c72009-02-17 23:15:12 +0000565
566 // Parse the (optional) class name or simple-template-id.
Douglas Gregorec93f442008-04-13 21:30:24 +0000567 IdentifierInfo *Name = 0;
568 SourceLocation NameLoc;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000569 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregorec93f442008-04-13 21:30:24 +0000570 if (Tok.is(tok::identifier)) {
571 Name = Tok.getIdentifierInfo();
572 NameLoc = ConsumeToken();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000573 } else if (Tok.is(tok::annot_template_id)) {
574 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
575 NameLoc = ConsumeToken();
Douglas Gregora08b6c72009-02-17 23:15:12 +0000576
Douglas Gregoraabb8502009-03-31 00:43:58 +0000577 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000578 // The template-name in the simple-template-id refers to
579 // something other than a class template. Give an appropriate
580 // error message and skip to the ';'.
581 SourceRange Range(NameLoc);
582 if (SS.isNotEmpty())
583 Range.setBegin(SS.getBeginLoc());
Douglas Gregora08b6c72009-02-17 23:15:12 +0000584
Douglas Gregor0c281a82009-02-25 19:37:18 +0000585 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
586 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump25cf7602009-09-09 15:08:12 +0000587
Douglas Gregor0c281a82009-02-25 19:37:18 +0000588 DS.SetTypeSpecError();
589 SkipUntil(tok::semi, false, true);
590 TemplateId->Destroy();
591 return;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000592 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000593 }
594
John McCall140607b2009-08-06 02:15:43 +0000595 // There are four options here. If we have 'struct foo;', then this
596 // is either a forward declaration or a friend declaration, which
597 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor0c281a82009-02-25 19:37:18 +0000598 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregorec93f442008-04-13 21:30:24 +0000599 // something like 'struct foo xyz', a reference.
John McCall069c23a2009-07-31 02:45:11 +0000600 Action::TagUseKind TUK;
Douglas Gregorec93f442008-04-13 21:30:24 +0000601 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
John McCall069c23a2009-07-31 02:45:11 +0000602 TUK = Action::TUK_Definition;
John McCall140607b2009-08-06 02:15:43 +0000603 else if (Tok.is(tok::semi))
604 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregorec93f442008-04-13 21:30:24 +0000605 else
John McCall069c23a2009-07-31 02:45:11 +0000606 TUK = Action::TUK_Reference;
Douglas Gregorec93f442008-04-13 21:30:24 +0000607
John McCall069c23a2009-07-31 02:45:11 +0000608 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000609 // We have a declaration or reference to an anonymous class.
Chris Lattnerf006a222008-11-18 07:48:38 +0000610 Diag(StartLoc, diag::err_anon_type_definition)
611 << DeclSpec::getSpecifierName(TagType);
Douglas Gregorec93f442008-04-13 21:30:24 +0000612
613 // Skip the rest of this declarator, up until the comma or semicolon.
614 SkipUntil(tok::comma, true);
Douglas Gregor0c281a82009-02-25 19:37:18 +0000615
616 if (TemplateId)
617 TemplateId->Destroy();
Douglas Gregorec93f442008-04-13 21:30:24 +0000618 return;
619 }
620
Douglas Gregord406b032009-02-06 22:42:48 +0000621 // Create the tag portion of the class or class template.
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000622 Action::DeclResult TagOrTempResult;
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000623 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
624
John McCall069c23a2009-07-31 02:45:11 +0000625 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000626 // to turn that template-id into a type.
627
Douglas Gregor71f06032009-05-28 23:31:59 +0000628 bool Owned = false;
John McCallfd025182009-09-04 01:14:41 +0000629 if (TemplateId) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000630 // Explicit specialization, class template partial specialization,
631 // or explicit instantiation.
Mike Stump25cf7602009-09-09 15:08:12 +0000632 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor0c281a82009-02-25 19:37:18 +0000633 TemplateId->getTemplateArgs(),
634 TemplateId->getTemplateArgIsType(),
635 TemplateId->NumArgs);
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000636 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall069c23a2009-07-31 02:45:11 +0000637 TUK == Action::TUK_Declaration) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000638 // This is an explicit instantiation of a class template.
639 TagOrTempResult
Mike Stump25cf7602009-09-09 15:08:12 +0000640 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor7a374722009-09-04 06:33:52 +0000641 TemplateInfo.ExternLoc,
Mike Stump25cf7602009-09-09 15:08:12 +0000642 TemplateInfo.TemplateLoc,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000643 TagType,
Mike Stump25cf7602009-09-09 15:08:12 +0000644 StartLoc,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000645 SS,
Mike Stump25cf7602009-09-09 15:08:12 +0000646 TemplateTy::make(TemplateId->Template),
647 TemplateId->TemplateNameLoc,
648 TemplateId->LAngleLoc,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000649 TemplateArgsPtr,
650 TemplateId->getTemplateArgLocations(),
Mike Stump25cf7602009-09-09 15:08:12 +0000651 TemplateId->RAngleLoc,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000652 Attr);
John McCallfd025182009-09-04 01:14:41 +0000653 } else if (TUK == Action::TUK_Reference || TUK == Action::TUK_Friend) {
John McCalld321d492009-09-08 17:47:29 +0000654 Action::TypeResult Type
655 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
656 TemplateId->TemplateNameLoc,
657 TemplateId->LAngleLoc,
658 TemplateArgsPtr,
659 TemplateId->getTemplateArgLocations(),
660 TemplateId->RAngleLoc);
661
662 Type = Actions.ActOnTagTemplateIdType(Type, TUK, TagType, StartLoc);
John McCallfd025182009-09-04 01:14:41 +0000663
664 TemplateId->Destroy();
665
John McCalld321d492009-09-08 17:47:29 +0000666 if (Type.isInvalid()) {
John McCallfd025182009-09-04 01:14:41 +0000667 DS.SetTypeSpecError();
668 return;
669 }
John McCalld321d492009-09-08 17:47:29 +0000670
John McCallfd025182009-09-04 01:14:41 +0000671 const char *PrevSpec = 0;
672 unsigned DiagID;
673 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, PrevSpec,
John McCalld321d492009-09-08 17:47:29 +0000674 DiagID, Type.get()))
John McCallfd025182009-09-04 01:14:41 +0000675 Diag(StartLoc, DiagID) << PrevSpec;
676
677 return;
Mike Stump25cf7602009-09-09 15:08:12 +0000678
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000679 } else {
680 // This is an explicit specialization or a class template
681 // partial specialization.
682 TemplateParameterLists FakedParamLists;
683
684 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
685 // This looks like an explicit instantiation, because we have
686 // something like
687 //
688 // template class Foo<X>
689 //
Douglas Gregor96b6df92009-05-14 00:28:11 +0000690 // but it actually has a definition. Most likely, this was
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000691 // meant to be an explicit specialization, but the user forgot
692 // the '<>' after 'template'.
John McCall069c23a2009-07-31 02:45:11 +0000693 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000694
Mike Stump25cf7602009-09-09 15:08:12 +0000695 SourceLocation LAngleLoc
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000696 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump25cf7602009-09-09 15:08:12 +0000697 Diag(TemplateId->TemplateNameLoc,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000698 diag::err_explicit_instantiation_with_definition)
699 << SourceRange(TemplateInfo.TemplateLoc)
700 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
701
702 // Create a fake template parameter list that contains only
703 // "template<>", so that we treat this construct as a class
704 // template specialization.
705 FakedParamLists.push_back(
Mike Stump25cf7602009-09-09 15:08:12 +0000706 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000707 TemplateInfo.TemplateLoc,
Mike Stump25cf7602009-09-09 15:08:12 +0000708 LAngleLoc,
709 0, 0,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000710 LAngleLoc));
711 TemplateParams = &FakedParamLists;
712 }
713
714 // Build the class template specialization.
715 TagOrTempResult
John McCall069c23a2009-07-31 02:45:11 +0000716 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor0c281a82009-02-25 19:37:18 +0000717 StartLoc, SS,
Mike Stump25cf7602009-09-09 15:08:12 +0000718 TemplateTy::make(TemplateId->Template),
719 TemplateId->TemplateNameLoc,
720 TemplateId->LAngleLoc,
Douglas Gregor0c281a82009-02-25 19:37:18 +0000721 TemplateArgsPtr,
722 TemplateId->getTemplateArgLocations(),
Mike Stump25cf7602009-09-09 15:08:12 +0000723 TemplateId->RAngleLoc,
Douglas Gregor0c281a82009-02-25 19:37:18 +0000724 Attr,
Mike Stump25cf7602009-09-09 15:08:12 +0000725 Action::MultiTemplateParamsArg(Actions,
Douglas Gregora08b6c72009-02-17 23:15:12 +0000726 TemplateParams? &(*TemplateParams)[0] : 0,
727 TemplateParams? TemplateParams->size() : 0));
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000728 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000729 TemplateId->Destroy();
Douglas Gregor96b6df92009-05-14 00:28:11 +0000730 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall069c23a2009-07-31 02:45:11 +0000731 TUK == Action::TUK_Declaration) {
Douglas Gregor96b6df92009-05-14 00:28:11 +0000732 // Explicit instantiation of a member of a class template
733 // specialization, e.g.,
734 //
735 // template struct Outer<int>::Inner;
736 //
737 TagOrTempResult
Mike Stump25cf7602009-09-09 15:08:12 +0000738 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor7a374722009-09-04 06:33:52 +0000739 TemplateInfo.ExternLoc,
Mike Stump25cf7602009-09-09 15:08:12 +0000740 TemplateInfo.TemplateLoc,
741 TagType, StartLoc, SS, Name,
Douglas Gregor96b6df92009-05-14 00:28:11 +0000742 NameLoc, Attr);
743 } else {
744 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall069c23a2009-07-31 02:45:11 +0000745 TUK == Action::TUK_Definition) {
Douglas Gregor96b6df92009-05-14 00:28:11 +0000746 // FIXME: Diagnose this particular error.
747 }
748
749 // Declaration or definition of a class type
Mike Stump25cf7602009-09-09 15:08:12 +0000750 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Douglas Gregor84a20812009-07-22 23:48:44 +0000751 Name, NameLoc, Attr, AS,
Mike Stump25cf7602009-09-09 15:08:12 +0000752 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor84a20812009-07-22 23:48:44 +0000753 TemplateParams? &(*TemplateParams)[0] : 0,
754 TemplateParams? TemplateParams->size() : 0),
755 Owned);
Douglas Gregor96b6df92009-05-14 00:28:11 +0000756 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000757
758 // Parse the optional base clause (C++ only).
Chris Lattner31ccf0a2009-02-16 22:07:16 +0000759 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000760 ParseBaseClause(TagOrTempResult.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000761
762 // If there is a body, parse it and inform the actions module.
763 if (Tok.is(tok::l_brace))
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000764 if (getLang().CPlusPlus)
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000765 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000766 else
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000767 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall069c23a2009-07-31 02:45:11 +0000768 else if (TUK == Action::TUK_Definition) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000769 // FIXME: Complain that we have a base-specifier list but no
770 // definition.
Chris Lattnerf006a222008-11-18 07:48:38 +0000771 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregorec93f442008-04-13 21:30:24 +0000772 }
773
Anders Carlssonef3fa4f2009-05-11 22:27:47 +0000774 if (TagOrTempResult.isInvalid()) {
Douglas Gregord406b032009-02-06 22:42:48 +0000775 DS.SetTypeSpecError();
Anders Carlssonef3fa4f2009-05-11 22:27:47 +0000776 return;
777 }
Mike Stump25cf7602009-09-09 15:08:12 +0000778
John McCall9f6e0972009-08-03 20:12:06 +0000779 const char *PrevSpec = 0;
780 unsigned DiagID;
781 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
Douglas Gregor71f06032009-05-28 23:31:59 +0000782 TagOrTempResult.get().getAs<void>(), Owned))
John McCall9f6e0972009-08-03 20:12:06 +0000783 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregorec93f442008-04-13 21:30:24 +0000784}
785
Mike Stump25cf7602009-09-09 15:08:12 +0000786/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregorec93f442008-04-13 21:30:24 +0000787///
788/// base-clause : [C++ class.derived]
789/// ':' base-specifier-list
790/// base-specifier-list:
791/// base-specifier '...'[opt]
792/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattner5261d0c2009-03-28 19:18:32 +0000793void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000794 assert(Tok.is(tok::colon) && "Not a base clause");
795 ConsumeToken();
796
Douglas Gregorabed2172008-10-22 17:49:05 +0000797 // Build up an array of parsed base specifiers.
798 llvm::SmallVector<BaseTy *, 8> BaseInfo;
799
Douglas Gregorec93f442008-04-13 21:30:24 +0000800 while (true) {
801 // Parse a base-specifier.
Douglas Gregorabed2172008-10-22 17:49:05 +0000802 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000803 if (Result.isInvalid()) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000804 // Skip the rest of this base specifier, up until the comma or
805 // opening brace.
Douglas Gregorabed2172008-10-22 17:49:05 +0000806 SkipUntil(tok::comma, tok::l_brace, true, true);
807 } else {
808 // Add this to our array of base specifiers.
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000809 BaseInfo.push_back(Result.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000810 }
811
812 // If the next token is a comma, consume it and keep reading
813 // base-specifiers.
814 if (Tok.isNot(tok::comma)) break;
Mike Stump25cf7602009-09-09 15:08:12 +0000815
Douglas Gregorec93f442008-04-13 21:30:24 +0000816 // Consume the comma.
817 ConsumeToken();
818 }
Douglas Gregorabed2172008-10-22 17:49:05 +0000819
820 // Attach the base specifiers
Jay Foad9e6bef42009-05-21 09:52:38 +0000821 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregorec93f442008-04-13 21:30:24 +0000822}
823
824/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
825/// one entry in the base class list of a class specifier, for example:
826/// class foo : public bar, virtual private baz {
827/// 'public bar' and 'virtual private baz' are each base-specifiers.
828///
829/// base-specifier: [C++ class.derived]
830/// ::[opt] nested-name-specifier[opt] class-name
831/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
832/// class-name
833/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
834/// class-name
Chris Lattner5261d0c2009-03-28 19:18:32 +0000835Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000836 bool IsVirtual = false;
837 SourceLocation StartLoc = Tok.getLocation();
838
839 // Parse the 'virtual' keyword.
840 if (Tok.is(tok::kw_virtual)) {
841 ConsumeToken();
842 IsVirtual = true;
843 }
844
845 // Parse an (optional) access specifier.
846 AccessSpecifier Access = getAccessSpecifierIfPresent();
847 if (Access)
848 ConsumeToken();
Mike Stump25cf7602009-09-09 15:08:12 +0000849
Douglas Gregorec93f442008-04-13 21:30:24 +0000850 // Parse the 'virtual' keyword (again!), in case it came after the
851 // access specifier.
852 if (Tok.is(tok::kw_virtual)) {
853 SourceLocation VirtualLoc = ConsumeToken();
854 if (IsVirtual) {
855 // Complain about duplicate 'virtual'
Chris Lattnerf006a222008-11-18 07:48:38 +0000856 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregord7cb0372009-04-01 21:51:26 +0000857 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregorec93f442008-04-13 21:30:24 +0000858 }
859
860 IsVirtual = true;
861 }
862
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000863 // Parse optional '::' and optional nested-name-specifier.
864 CXXScopeSpec SS;
Douglas Gregor681d31d2009-09-02 22:59:36 +0000865 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Douglas Gregorec93f442008-04-13 21:30:24 +0000866
Douglas Gregorec93f442008-04-13 21:30:24 +0000867 // The location of the base class itself.
868 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000869
870 // Parse the class-name.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000871 SourceLocation EndLocation;
Douglas Gregord7cb0372009-04-01 21:51:26 +0000872 TypeResult BaseType = ParseClassName(EndLocation, &SS);
873 if (BaseType.isInvalid())
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000874 return true;
Mike Stump25cf7602009-09-09 15:08:12 +0000875
876 // Find the complete source range for the base-specifier.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000877 SourceRange Range(StartLoc, EndLocation);
Mike Stump25cf7602009-09-09 15:08:12 +0000878
Douglas Gregorec93f442008-04-13 21:30:24 +0000879 // Notify semantic analysis that we have parsed a complete
880 // base-specifier.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000881 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregord7cb0372009-04-01 21:51:26 +0000882 BaseType.get(), BaseLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000883}
884
885/// getAccessSpecifierIfPresent - Determine whether the next token is
886/// a C++ access-specifier.
887///
888/// access-specifier: [C++ class.derived]
889/// 'private'
890/// 'protected'
891/// 'public'
Mike Stump25cf7602009-09-09 15:08:12 +0000892AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregorec93f442008-04-13 21:30:24 +0000893 switch (Tok.getKind()) {
894 default: return AS_none;
895 case tok::kw_private: return AS_private;
896 case tok::kw_protected: return AS_protected;
897 case tok::kw_public: return AS_public;
898 }
899}
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000900
Eli Friedman40035e22009-07-22 21:45:50 +0000901void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
902 DeclPtrTy ThisDecl) {
903 // We just declared a member function. If this member function
904 // has any default arguments, we'll need to parse them later.
905 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump25cf7602009-09-09 15:08:12 +0000906 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedman40035e22009-07-22 21:45:50 +0000907 = DeclaratorInfo.getTypeObject(0).Fun;
908 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
909 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
910 if (!LateMethod) {
911 // Push this method onto the stack of late-parsed method
912 // declarations.
913 getCurrentClass().MethodDecls.push_back(
914 LateParsedMethodDeclaration(ThisDecl));
915 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregor6ee35052009-08-22 00:34:47 +0000916 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedman40035e22009-07-22 21:45:50 +0000917
918 // Add all of the parameters prior to this one (they don't
919 // have default arguments).
920 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
921 for (unsigned I = 0; I < ParamIdx; ++I)
922 LateMethod->DefaultArgs.push_back(
923 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
924 }
925
926 // Add this parameter to the list of parameters (it or may
927 // not have a default argument).
928 LateMethod->DefaultArgs.push_back(
929 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
930 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
931 }
932 }
933}
934
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000935/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
936///
937/// member-declaration:
938/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
939/// function-definition ';'[opt]
940/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
941/// using-declaration [TODO]
Anders Carlssonab041982009-03-11 16:27:10 +0000942/// [C++0x] static_assert-declaration
Anders Carlssoned20fb92009-03-26 00:52:18 +0000943/// template-declaration
Chris Lattnerf3375de2008-12-18 01:12:00 +0000944/// [GNU] '__extension__' member-declaration
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000945///
946/// member-declarator-list:
947/// member-declarator
948/// member-declarator-list ',' member-declarator
949///
950/// member-declarator:
951/// declarator pure-specifier[opt]
952/// declarator constant-initializer[opt]
953/// identifier[opt] ':' constant-expression
954///
Sebastian Redla55834a2009-04-12 17:16:29 +0000955/// pure-specifier:
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000956/// '= 0'
957///
958/// constant-initializer:
959/// '=' constant-expression
960///
Douglas Gregor398a8012009-08-20 22:52:58 +0000961void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
962 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlssonab041982009-03-11 16:27:10 +0000963 // static_assert-declaration
Chris Lattnera17991f2009-03-29 16:50:03 +0000964 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor398a8012009-08-20 22:52:58 +0000965 // FIXME: Check for templates
Chris Lattner9802a0a2009-04-02 04:16:50 +0000966 SourceLocation DeclEnd;
967 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000968 return;
969 }
Mike Stump25cf7602009-09-09 15:08:12 +0000970
Chris Lattnera17991f2009-03-29 16:50:03 +0000971 if (Tok.is(tok::kw_template)) {
Mike Stump25cf7602009-09-09 15:08:12 +0000972 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor398a8012009-08-20 22:52:58 +0000973 "Nested template improperly parsed?");
Chris Lattner9802a0a2009-04-02 04:16:50 +0000974 SourceLocation DeclEnd;
Mike Stump25cf7602009-09-09 15:08:12 +0000975 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000976 AS);
Chris Lattnera17991f2009-03-29 16:50:03 +0000977 return;
978 }
Anders Carlssoned20fb92009-03-26 00:52:18 +0000979
Chris Lattnerf3375de2008-12-18 01:12:00 +0000980 // Handle: member-declaration ::= '__extension__' member-declaration
981 if (Tok.is(tok::kw___extension__)) {
982 // __extension__ silences extension warnings in the subexpression.
983 ExtensionRAIIObject O(Diags); // Use RAII to do this.
984 ConsumeToken();
Douglas Gregor398a8012009-08-20 22:52:58 +0000985 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerf3375de2008-12-18 01:12:00 +0000986 }
Douglas Gregor683a1142009-06-20 00:51:54 +0000987
988 if (Tok.is(tok::kw_using)) {
Douglas Gregor398a8012009-08-20 22:52:58 +0000989 // FIXME: Check for template aliases
Mike Stump25cf7602009-09-09 15:08:12 +0000990
Douglas Gregor683a1142009-06-20 00:51:54 +0000991 // Eat 'using'.
992 SourceLocation UsingLoc = ConsumeToken();
993
994 if (Tok.is(tok::kw_namespace)) {
995 Diag(UsingLoc, diag::err_using_namespace_in_class);
996 SkipUntil(tok::semi, true, true);
997 }
998 else {
999 SourceLocation DeclEnd;
1000 // Otherwise, it must be using-declaration.
Anders Carlssone16b4fe2009-08-29 19:54:19 +00001001 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor683a1142009-06-20 00:51:54 +00001002 }
1003 return;
1004 }
1005
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001006 SourceLocation DSStart = Tok.getLocation();
1007 // decl-specifier-seq:
1008 // Parse the common declaration-specifiers piece.
1009 DeclSpec DS;
Douglas Gregor398a8012009-08-20 22:52:58 +00001010 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001011
1012 if (Tok.is(tok::semi)) {
1013 ConsumeToken();
John McCall140607b2009-08-06 02:15:43 +00001014
Douglas Gregor398a8012009-08-20 22:52:58 +00001015 // FIXME: Friend templates?
John McCall140607b2009-08-06 02:15:43 +00001016 if (DS.isFriendSpecified())
John McCall36493082009-08-11 06:59:38 +00001017 Actions.ActOnFriendDecl(CurScope, &DS, /*IsDefinition*/ false);
John McCall140607b2009-08-06 02:15:43 +00001018 else
Chris Lattnera17991f2009-03-29 16:50:03 +00001019 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall140607b2009-08-06 02:15:43 +00001020
1021 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001022 }
Argiris Kirtzidis38f16712008-07-01 10:37:29 +00001023
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001024 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001025
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001026 if (Tok.isNot(tok::colon)) {
1027 // Parse the first declarator.
1028 ParseDeclarator(DeclaratorInfo);
1029 // Error parsing the declarator?
Douglas Gregor6704b312008-11-17 22:58:34 +00001030 if (!DeclaratorInfo.hasName()) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001031 // If so, skip until the semi-colon or a }.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001032 SkipUntil(tok::r_brace, true);
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001033 if (Tok.is(tok::semi))
1034 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +00001035 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001036 }
1037
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001038 // function-definition:
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001039 if (Tok.is(tok::l_brace)
Sebastian Redlbc9ef252009-04-26 20:35:05 +00001040 || (DeclaratorInfo.isFunctionDeclarator() &&
1041 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001042 if (!DeclaratorInfo.isFunctionDeclarator()) {
1043 Diag(Tok, diag::err_func_def_no_params);
1044 ConsumeBrace();
1045 SkipUntil(tok::r_brace, true);
Chris Lattnera17991f2009-03-29 16:50:03 +00001046 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001047 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001048
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001049 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1050 Diag(Tok, diag::err_function_declared_typedef);
1051 // This recovery skips the entire function body. It would be nice
1052 // to simply call ParseCXXInlineMethodDef() below, however Sema
1053 // assumes the declarator represents a function, not a typedef.
1054 ConsumeBrace();
1055 SkipUntil(tok::r_brace, true);
Chris Lattnera17991f2009-03-29 16:50:03 +00001056 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001057 }
1058
Douglas Gregor398a8012009-08-20 22:52:58 +00001059 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattnera17991f2009-03-29 16:50:03 +00001060 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001061 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001062 }
1063
1064 // member-declarator-list:
1065 // member-declarator
1066 // member-declarator-list ',' member-declarator
1067
Chris Lattnera17991f2009-03-29 16:50:03 +00001068 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl62261042008-12-09 20:22:58 +00001069 OwningExprResult BitfieldSize(Actions);
1070 OwningExprResult Init(Actions);
Sebastian Redla55834a2009-04-12 17:16:29 +00001071 bool Deleted = false;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001072
1073 while (1) {
1074
1075 // member-declarator:
1076 // declarator pure-specifier[opt]
1077 // declarator constant-initializer[opt]
1078 // identifier[opt] ':' constant-expression
1079
1080 if (Tok.is(tok::colon)) {
1081 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001082 BitfieldSize = ParseConstantExpression();
1083 if (BitfieldSize.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001084 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001085 }
Mike Stump25cf7602009-09-09 15:08:12 +00001086
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001087 // pure-specifier:
1088 // '= 0'
1089 //
1090 // constant-initializer:
1091 // '=' constant-expression
Sebastian Redla55834a2009-04-12 17:16:29 +00001092 //
1093 // defaulted/deleted function-definition:
1094 // '=' 'default' [TODO]
1095 // '=' 'delete'
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001096
1097 if (Tok.is(tok::equal)) {
1098 ConsumeToken();
Sebastian Redla55834a2009-04-12 17:16:29 +00001099 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1100 ConsumeToken();
1101 Deleted = true;
1102 } else {
1103 Init = ParseInitializer();
1104 if (Init.isInvalid())
1105 SkipUntil(tok::comma, true, true);
1106 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001107 }
1108
1109 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001110 if (Tok.is(tok::kw___attribute)) {
1111 SourceLocation Loc;
1112 AttributeList *AttrList = ParseAttributes(&Loc);
1113 DeclaratorInfo.AddAttributes(AttrList, Loc);
1114 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001115
Argiris Kirtzidis38f16712008-07-01 10:37:29 +00001116 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattnera17991f2009-03-29 16:50:03 +00001117 // this call will *not* return the created decl; It will return null.
Argiris Kirtzidis38f16712008-07-01 10:37:29 +00001118 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall140607b2009-08-06 02:15:43 +00001119
1120 DeclPtrTy ThisDecl;
1121 if (DS.isFriendSpecified()) {
Douglas Gregor398a8012009-08-20 22:52:58 +00001122 // TODO: handle initializers, bitfields, 'delete', friend templates
John McCall36493082009-08-11 06:59:38 +00001123 ThisDecl = Actions.ActOnFriendDecl(CurScope, &DeclaratorInfo,
1124 /*IsDefinition*/ false);
Douglas Gregor398a8012009-08-20 22:52:58 +00001125 } else {
1126 Action::MultiTemplateParamsArg TemplateParams(Actions,
1127 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1128 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
John McCall140607b2009-08-06 02:15:43 +00001129 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1130 DeclaratorInfo,
Douglas Gregor398a8012009-08-20 22:52:58 +00001131 move(TemplateParams),
John McCall140607b2009-08-06 02:15:43 +00001132 BitfieldSize.release(),
1133 Init.release(),
1134 Deleted);
Douglas Gregor398a8012009-08-20 22:52:58 +00001135 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001136 if (ThisDecl)
1137 DeclsInGroup.push_back(ThisDecl);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001138
Douglas Gregor605de8d2008-12-16 21:30:33 +00001139 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump25cf7602009-09-09 15:08:12 +00001140 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor605de8d2008-12-16 21:30:33 +00001141 != DeclSpec::SCS_typedef) {
Eli Friedman40035e22009-07-22 21:45:50 +00001142 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor605de8d2008-12-16 21:30:33 +00001143 }
1144
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001145 // If we don't have a comma, it is either the end of the list (a ';')
1146 // or an error, bail out.
1147 if (Tok.isNot(tok::comma))
1148 break;
Mike Stump25cf7602009-09-09 15:08:12 +00001149
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001150 // Consume the comma.
1151 ConsumeToken();
Mike Stump25cf7602009-09-09 15:08:12 +00001152
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001153 // Parse the next declarator.
1154 DeclaratorInfo.clear();
Sebastian Redl62261042008-12-09 20:22:58 +00001155 BitfieldSize = 0;
1156 Init = 0;
Sebastian Redla55834a2009-04-12 17:16:29 +00001157 Deleted = false;
Mike Stump25cf7602009-09-09 15:08:12 +00001158
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001159 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001160 if (Tok.is(tok::kw___attribute)) {
1161 SourceLocation Loc;
1162 AttributeList *AttrList = ParseAttributes(&Loc);
1163 DeclaratorInfo.AddAttributes(AttrList, Loc);
1164 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001165
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001166 if (Tok.isNot(tok::colon))
1167 ParseDeclarator(DeclaratorInfo);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001168 }
1169
1170 if (Tok.is(tok::semi)) {
1171 ConsumeToken();
Eli Friedman4d57af22009-05-29 01:49:24 +00001172 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattnera17991f2009-03-29 16:50:03 +00001173 DeclsInGroup.size());
1174 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001175 }
1176
1177 Diag(Tok, diag::err_expected_semi_decl_list);
1178 // Skip to end of block or statement
1179 SkipUntil(tok::r_brace, true, true);
1180 if (Tok.is(tok::semi))
1181 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +00001182 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001183}
1184
1185/// ParseCXXMemberSpecification - Parse the class definition.
1186///
1187/// member-specification:
1188/// member-declaration member-specification[opt]
1189/// access-specifier ':' member-specification[opt]
1190///
1191void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001192 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Guptafa451432008-10-31 09:52:39 +00001193 assert((TagType == DeclSpec::TST_struct ||
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001194 TagType == DeclSpec::TST_union ||
Sanjiv Guptafa451432008-10-31 09:52:39 +00001195 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001196
Chris Lattnerc309ade2009-03-05 08:00:35 +00001197 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1198 PP.getSourceManager(),
1199 "parsing struct/union/class body");
Mike Stump25cf7602009-09-09 15:08:12 +00001200
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001201 SourceLocation LBraceLoc = ConsumeBrace();
1202
Douglas Gregora376cbd2009-05-27 23:11:45 +00001203 // Determine whether this is a top-level (non-nested) class.
Mike Stump25cf7602009-09-09 15:08:12 +00001204 bool TopLevelClass = ClassStack.empty() ||
Douglas Gregora376cbd2009-05-27 23:11:45 +00001205 CurScope->isInCXXInlineMethodScope();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001206
1207 // Enter a scope for the class.
Douglas Gregorcab994d2009-01-09 22:42:13 +00001208 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001209
Douglas Gregora376cbd2009-05-27 23:11:45 +00001210 // Note that we are parsing a new (potentially-nested) class definition.
1211 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1212
Douglas Gregord406b032009-02-06 22:42:48 +00001213 if (TagDecl)
1214 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1215 else {
1216 SkipUntil(tok::r_brace, false, false);
1217 return;
1218 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001219
1220 // C++ 11p3: Members of a class defined with the keyword class are private
1221 // by default. Members of a class defined with the keywords struct or union
1222 // are public by default.
1223 AccessSpecifier CurAS;
1224 if (TagType == DeclSpec::TST_class)
1225 CurAS = AS_private;
1226 else
1227 CurAS = AS_public;
1228
1229 // While we still have something to read, read the member-declarations.
1230 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1231 // Each iteration of this loop reads one member-declaration.
Mike Stump25cf7602009-09-09 15:08:12 +00001232
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001233 // Check for extraneous top-level semicolon.
1234 if (Tok.is(tok::semi)) {
1235 Diag(Tok, diag::ext_extra_struct_semi);
1236 ConsumeToken();
1237 continue;
1238 }
1239
1240 AccessSpecifier AS = getAccessSpecifierIfPresent();
1241 if (AS != AS_none) {
1242 // Current token is a C++ access specifier.
1243 CurAS = AS;
1244 ConsumeToken();
1245 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1246 continue;
1247 }
1248
Douglas Gregor398a8012009-08-20 22:52:58 +00001249 // FIXME: Make sure we don't have a template here.
Mike Stump25cf7602009-09-09 15:08:12 +00001250
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001251 // Parse all the comma separated declarators.
1252 ParseCXXClassMemberDeclaration(CurAS);
1253 }
Mike Stump25cf7602009-09-09 15:08:12 +00001254
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001255 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump25cf7602009-09-09 15:08:12 +00001256
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001257 AttributeList *AttrList = 0;
1258 // If attributes exist after class contents, parse them.
1259 if (Tok.is(tok::kw___attribute))
1260 AttrList = ParseAttributes(); // FIXME: where should I put them?
1261
1262 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1263 LBraceLoc, RBraceLoc);
1264
1265 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1266 // complete within function bodies, default arguments,
1267 // exception-specifications, and constructor ctor-initializers (including
1268 // such things in nested classes).
1269 //
Douglas Gregor605de8d2008-12-16 21:30:33 +00001270 // FIXME: Only function bodies and constructor ctor-initializers are
1271 // parsed correctly, fix the rest.
Douglas Gregora376cbd2009-05-27 23:11:45 +00001272 if (TopLevelClass) {
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001273 // We are not inside a nested class. This class and its nested classes
Douglas Gregor605de8d2008-12-16 21:30:33 +00001274 // are complete and we can parse the delayed portions of method
1275 // declarations and the lexed inline method definitions.
Douglas Gregora376cbd2009-05-27 23:11:45 +00001276 ParseLexedMethodDeclarations(getCurrentClass());
1277 ParseLexedMethodDefs(getCurrentClass());
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001278 }
1279
1280 // Leave the class scope.
Douglas Gregora376cbd2009-05-27 23:11:45 +00001281 ParsingDef.Pop();
Douglas Gregor95d40792008-12-10 06:34:36 +00001282 ClassScope.Exit();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001283
Argiris Kirtzidiseb925642009-07-14 03:17:52 +00001284 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001285}
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001286
1287/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1288/// which explicitly initializes the members or base classes of a
1289/// class (C++ [class.base.init]). For example, the three initializers
1290/// after the ':' in the Derived constructor below:
1291///
1292/// @code
1293/// class Base { };
1294/// class Derived : Base {
1295/// int x;
1296/// float f;
1297/// public:
1298/// Derived(float f) : Base(), x(17), f(f) { }
1299/// };
1300/// @endcode
1301///
Mike Stump25cf7602009-09-09 15:08:12 +00001302/// [C++] ctor-initializer:
1303/// ':' mem-initializer-list
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001304///
Mike Stump25cf7602009-09-09 15:08:12 +00001305/// [C++] mem-initializer-list:
1306/// mem-initializer
1307/// mem-initializer , mem-initializer-list
Chris Lattner5261d0c2009-03-28 19:18:32 +00001308void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001309 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1310
1311 SourceLocation ColonLoc = ConsumeToken();
Mike Stump25cf7602009-09-09 15:08:12 +00001312
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001313 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Mike Stump25cf7602009-09-09 15:08:12 +00001314
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001315 do {
1316 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +00001317 if (!MemInit.isInvalid())
1318 MemInitializers.push_back(MemInit.get());
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001319
1320 if (Tok.is(tok::comma))
1321 ConsumeToken();
1322 else if (Tok.is(tok::l_brace))
1323 break;
1324 else {
1325 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redlbc9ef252009-04-26 20:35:05 +00001326 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001327 SkipUntil(tok::l_brace, true, true);
1328 break;
1329 }
1330 } while (true);
1331
Mike Stump25cf7602009-09-09 15:08:12 +00001332 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +00001333 MemInitializers.data(), MemInitializers.size());
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001334}
1335
1336/// ParseMemInitializer - Parse a C++ member initializer, which is
1337/// part of a constructor initializer that explicitly initializes one
1338/// member or base class (C++ [class.base.init]). See
1339/// ParseConstructorInitializer for an example.
1340///
1341/// [C++] mem-initializer:
1342/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump25cf7602009-09-09 15:08:12 +00001343///
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001344/// [C++] mem-initializer-id:
1345/// '::'[opt] nested-name-specifier[opt] class-name
1346/// identifier
Chris Lattner5261d0c2009-03-28 19:18:32 +00001347Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianc37d3062009-06-30 23:26:25 +00001348 // parse '::'[opt] nested-name-specifier[opt]
1349 CXXScopeSpec SS;
Douglas Gregor681d31d2009-09-02 22:59:36 +00001350 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian2b2b7362009-07-01 19:21:19 +00001351 TypeTy *TemplateTypeTy = 0;
1352 if (Tok.is(tok::annot_template_id)) {
1353 TemplateIdAnnotation *TemplateId
1354 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1355 if (TemplateId->Kind == TNK_Type_template) {
1356 AnnotateTemplateIdTokenAsType(&SS);
1357 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1358 TemplateTypeTy = Tok.getAnnotationValue();
1359 }
1360 // FIXME. May need to check for TNK_Dependent_template as well.
1361 }
1362 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001363 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001364 return true;
1365 }
Mike Stump25cf7602009-09-09 15:08:12 +00001366
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001367 // Get the identifier. This may be a member name or a class name,
1368 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian2b2b7362009-07-01 19:21:19 +00001369 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001370 SourceLocation IdLoc = ConsumeToken();
1371
1372 // Parse the '('.
1373 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001374 Diag(Tok, diag::err_expected_lparen);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001375 return true;
1376 }
1377 SourceLocation LParenLoc = ConsumeParen();
1378
1379 // Parse the optional expression-list.
Sebastian Redl6008ac32008-11-25 22:21:31 +00001380 ExprVector ArgExprs(Actions);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001381 CommaLocsTy CommaLocs;
1382 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1383 SkipUntil(tok::r_paren);
1384 return true;
1385 }
1386
1387 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1388
Fariborz Jahanian2b2b7362009-07-01 19:21:19 +00001389 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1390 TemplateTypeTy, IdLoc,
Sebastian Redl6008ac32008-11-25 22:21:31 +00001391 LParenLoc, ArgExprs.take(),
Jay Foad9e6bef42009-05-21 09:52:38 +00001392 ArgExprs.size(), CommaLocs.data(),
1393 RParenLoc);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001394}
Douglas Gregor90a2c972008-11-25 03:22:00 +00001395
1396/// ParseExceptionSpecification - Parse a C++ exception-specification
1397/// (C++ [except.spec]).
1398///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001399/// exception-specification:
1400/// 'throw' '(' type-id-list [opt] ')'
1401/// [MS] 'throw' '(' '...' ')'
Mike Stump25cf7602009-09-09 15:08:12 +00001402///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001403/// type-id-list:
1404/// type-id
1405/// type-id-list ',' type-id
Douglas Gregor90a2c972008-11-25 03:22:00 +00001406///
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001407bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlaaacda92009-05-29 18:02:33 +00001408 llvm::SmallVector<TypeTy*, 2>
1409 &Exceptions,
1410 llvm::SmallVector<SourceRange, 2>
1411 &Ranges,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001412 bool &hasAnyExceptionSpec) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00001413 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump25cf7602009-09-09 15:08:12 +00001414
Douglas Gregor90a2c972008-11-25 03:22:00 +00001415 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump25cf7602009-09-09 15:08:12 +00001416
Douglas Gregor90a2c972008-11-25 03:22:00 +00001417 if (!Tok.is(tok::l_paren)) {
1418 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1419 }
1420 SourceLocation LParenLoc = ConsumeParen();
1421
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001422 // Parse throw(...), a Microsoft extension that means "this function
1423 // can throw anything".
1424 if (Tok.is(tok::ellipsis)) {
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001425 hasAnyExceptionSpec = true;
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001426 SourceLocation EllipsisLoc = ConsumeToken();
1427 if (!getLang().Microsoft)
1428 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redl0c986032009-02-09 18:23:29 +00001429 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001430 return false;
1431 }
1432
Douglas Gregor90a2c972008-11-25 03:22:00 +00001433 // Parse the sequence of type-ids.
Sebastian Redlaaacda92009-05-29 18:02:33 +00001434 SourceRange Range;
Douglas Gregor90a2c972008-11-25 03:22:00 +00001435 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlaaacda92009-05-29 18:02:33 +00001436 TypeResult Res(ParseTypeName(&Range));
1437 if (!Res.isInvalid()) {
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001438 Exceptions.push_back(Res.get());
Sebastian Redlaaacda92009-05-29 18:02:33 +00001439 Ranges.push_back(Range);
1440 }
Douglas Gregor90a2c972008-11-25 03:22:00 +00001441 if (Tok.is(tok::comma))
1442 ConsumeToken();
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001443 else
Douglas Gregor90a2c972008-11-25 03:22:00 +00001444 break;
1445 }
1446
Sebastian Redl0c986032009-02-09 18:23:29 +00001447 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001448 return false;
1449}
Douglas Gregora376cbd2009-05-27 23:11:45 +00001450
1451/// \brief We have just started parsing the definition of a new class,
1452/// so push that class onto our stack of classes that is currently
1453/// being parsed.
1454void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
Mike Stump25cf7602009-09-09 15:08:12 +00001455 assert((TopLevelClass || !ClassStack.empty()) &&
Douglas Gregora376cbd2009-05-27 23:11:45 +00001456 "Nested class without outer class");
1457 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1458}
1459
1460/// \brief Deallocate the given parsed class and all of its nested
1461/// classes.
1462void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1463 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1464 DeallocateParsedClasses(Class->NestedClasses[I]);
1465 delete Class;
1466}
1467
1468/// \brief Pop the top class of the stack of classes that are
1469/// currently being parsed.
1470///
1471/// This routine should be called when we have finished parsing the
1472/// definition of a class, but have not yet popped the Scope
1473/// associated with the class's definition.
1474///
1475/// \returns true if the class we've popped is a top-level class,
1476/// false otherwise.
1477void Parser::PopParsingClass() {
1478 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump25cf7602009-09-09 15:08:12 +00001479
Douglas Gregora376cbd2009-05-27 23:11:45 +00001480 ParsingClass *Victim = ClassStack.top();
1481 ClassStack.pop();
1482 if (Victim->TopLevelClass) {
1483 // Deallocate all of the nested classes of this class,
1484 // recursively: we don't need to keep any of this information.
1485 DeallocateParsedClasses(Victim);
1486 return;
Mike Stump25cf7602009-09-09 15:08:12 +00001487 }
Douglas Gregora376cbd2009-05-27 23:11:45 +00001488 assert(!ClassStack.empty() && "Missing top-level class?");
1489
1490 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1491 Victim->NestedClasses.empty()) {
1492 // The victim is a nested class, but we will not need to perform
1493 // any processing after the definition of this class since it has
1494 // no members whose handling was delayed. Therefore, we can just
1495 // remove this nested class.
1496 delete Victim;
1497 return;
1498 }
1499
1500 // This nested class has some members that will need to be processed
1501 // after the top-level class is completely defined. Therefore, add
1502 // it to the list of nested classes within its parent.
1503 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1504 ClassStack.top()->NestedClasses.push_back(Victim);
1505 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1506}