blob: 930d58dd26247c694e7475a49dc428160596568c [file] [log] [blame]
Chris Lattnerf7b2e552007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf7b2e552007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlssone8c36f22009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor696be932008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregorec93f442008-04-13 21:30:24 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattnerf7b2e552007-08-25 06:57:03 +000018#include "clang/Parse/Scope.h"
Chris Lattnerf3375de2008-12-18 01:12:00 +000019#include "ExtensionRAIIObject.h"
Chris Lattnerf7b2e552007-08-25 06:57:03 +000020using namespace clang;
21
22/// ParseNamespace - We know that the current token is a namespace keyword. This
23/// may either be a top level namespace or a block-level namespace alias.
24///
25/// namespace-definition: [C++ 7.3: basic.namespace]
26/// named-namespace-definition
27/// unnamed-namespace-definition
28///
29/// unnamed-namespace-definition:
30/// 'namespace' attributes[opt] '{' namespace-body '}'
31///
32/// named-namespace-definition:
33/// original-namespace-definition
34/// extension-namespace-definition
35///
36/// original-namespace-definition:
37/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
38///
39/// extension-namespace-definition:
40/// 'namespace' original-namespace-name '{' namespace-body '}'
41///
42/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
43/// 'namespace' identifier '=' qualified-namespace-specifier ';'
44///
Chris Lattner9802a0a2009-04-02 04:16:50 +000045Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
46 SourceLocation &DeclEnd) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000047 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnerf7b2e552007-08-25 06:57:03 +000048 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
49
50 SourceLocation IdentLoc;
51 IdentifierInfo *Ident = 0;
Douglas Gregorb6d226f2009-06-17 19:49:00 +000052
53 Token attrTok;
Chris Lattnerf7b2e552007-08-25 06:57:03 +000054
Chris Lattner34a01ad2007-10-09 17:33:22 +000055 if (Tok.is(tok::identifier)) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +000056 Ident = Tok.getIdentifierInfo();
57 IdentLoc = ConsumeToken(); // eat the identifier.
58 }
59
60 // Read label attributes, if present.
Chris Lattner5261d0c2009-03-28 19:18:32 +000061 Action::AttrTy *AttrList = 0;
Douglas Gregorb6d226f2009-06-17 19:49:00 +000062 if (Tok.is(tok::kw___attribute)) {
63 attrTok = Tok;
64
Chris Lattnerf7b2e552007-08-25 06:57:03 +000065 // FIXME: save these somewhere.
66 AttrList = ParseAttributes();
Douglas Gregorb6d226f2009-06-17 19:49:00 +000067 }
Chris Lattnerf7b2e552007-08-25 06:57:03 +000068
Douglas Gregorb6d226f2009-06-17 19:49:00 +000069 if (Tok.is(tok::equal)) {
70 if (AttrList)
71 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
72
Chris Lattner9802a0a2009-04-02 04:16:50 +000073 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregorb6d226f2009-06-17 19:49:00 +000074 }
Anders Carlssonf94cca22009-03-28 04:07:16 +000075
Chris Lattner872b0442009-03-29 14:02:43 +000076 if (Tok.isNot(tok::l_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +000077 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner872b0442009-03-29 14:02:43 +000078 diag::err_expected_ident_lbrace);
79 return DeclPtrTy();
Chris Lattnerf7b2e552007-08-25 06:57:03 +000080 }
81
Chris Lattner872b0442009-03-29 14:02:43 +000082 SourceLocation LBrace = ConsumeBrace();
83
84 // Enter a scope for the namespace.
85 ParseScope NamespaceScope(this, Scope::DeclScope);
86
87 DeclPtrTy NamespcDecl =
88 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
89
90 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
91 PP.getSourceManager(),
92 "parsing namespace");
93
94 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
95 ParseExternalDeclaration();
96
97 // Leave the namespace scope.
98 NamespaceScope.Exit();
99
Chris Lattner9802a0a2009-04-02 04:16:50 +0000100 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
101 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner872b0442009-03-29 14:02:43 +0000102
Chris Lattner9802a0a2009-04-02 04:16:50 +0000103 DeclEnd = RBraceLoc;
Chris Lattner872b0442009-03-29 14:02:43 +0000104 return NamespcDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000105}
Chris Lattner806a5f52008-01-12 07:05:38 +0000106
Anders Carlssonf94cca22009-03-28 04:07:16 +0000107/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
108/// alias definition.
109///
Anders Carlsson26de7882009-03-28 22:53:22 +0000110Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
111 SourceLocation AliasLoc,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000112 IdentifierInfo *Alias,
113 SourceLocation &DeclEnd) {
Anders Carlssonf94cca22009-03-28 04:07:16 +0000114 assert(Tok.is(tok::equal) && "Not equal token");
115
116 ConsumeToken(); // eat the '='.
117
118 CXXScopeSpec SS;
119 // Parse (optional) nested-name-specifier.
120 ParseOptionalCXXScopeSpecifier(SS);
121
122 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
123 Diag(Tok, diag::err_expected_namespace_name);
124 // Skip to end of the definition and eat the ';'.
125 SkipUntil(tok::semi);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000126 return DeclPtrTy();
Anders Carlssonf94cca22009-03-28 04:07:16 +0000127 }
128
129 // Parse identifier.
Anders Carlsson26de7882009-03-28 22:53:22 +0000130 IdentifierInfo *Ident = Tok.getIdentifierInfo();
131 SourceLocation IdentLoc = ConsumeToken();
Anders Carlssonf94cca22009-03-28 04:07:16 +0000132
133 // Eat the ';'.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000134 DeclEnd = Tok.getLocation();
Chris Lattnercb9057e2009-06-14 00:07:48 +0000135 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
136 "", tok::semi);
Anders Carlssonf94cca22009-03-28 04:07:16 +0000137
Anders Carlsson26de7882009-03-28 22:53:22 +0000138 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
139 SS, IdentLoc, Ident);
Anders Carlssonf94cca22009-03-28 04:07:16 +0000140}
141
Chris Lattner806a5f52008-01-12 07:05:38 +0000142/// ParseLinkage - We know that the current token is a string_literal
143/// and just before that, that extern was seen.
144///
145/// linkage-specification: [C++ 7.5p2: dcl.link]
146/// 'extern' string-literal '{' declaration-seq[opt] '}'
147/// 'extern' string-literal declaration
148///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000149Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) {
Douglas Gregor61818c52008-11-21 16:10:08 +0000150 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattner806a5f52008-01-12 07:05:38 +0000151 llvm::SmallVector<char, 8> LangBuffer;
152 // LangBuffer is guaranteed to be big enough.
153 LangBuffer.resize(Tok.getLength());
154 const char *LangBufPtr = &LangBuffer[0];
155 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
156
157 SourceLocation Loc = ConsumeStringToken();
Chris Lattner806a5f52008-01-12 07:05:38 +0000158
Douglas Gregord8028382009-01-05 19:45:36 +0000159 ParseScope LinkageScope(this, Scope::DeclScope);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000160 DeclPtrTy LinkageSpec
Douglas Gregord8028382009-01-05 19:45:36 +0000161 = Actions.ActOnStartLinkageSpecification(CurScope,
162 /*FIXME: */SourceLocation(),
163 Loc, LangBufPtr, StrSize,
164 Tok.is(tok::l_brace)? Tok.getLocation()
165 : SourceLocation());
166
167 if (Tok.isNot(tok::l_brace)) {
168 ParseDeclarationOrFunctionDefinition();
169 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
170 SourceLocation());
Douglas Gregorad17e372008-12-16 22:23:02 +0000171 }
172
173 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorad17e372008-12-16 22:23:02 +0000174 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregord8028382009-01-05 19:45:36 +0000175 ParseExternalDeclaration();
Chris Lattner806a5f52008-01-12 07:05:38 +0000176 }
177
Douglas Gregorad17e372008-12-16 22:23:02 +0000178 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregord8028382009-01-05 19:45:36 +0000179 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattner806a5f52008-01-12 07:05:38 +0000180}
Douglas Gregorec93f442008-04-13 21:30:24 +0000181
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000182/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
183/// using-directive. Assumes that current token is 'using'.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000184Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
185 SourceLocation &DeclEnd) {
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000186 assert(Tok.is(tok::kw_using) && "Not using token");
187
188 // Eat 'using'.
189 SourceLocation UsingLoc = ConsumeToken();
190
Chris Lattner08ab4162009-01-06 06:55:51 +0000191 if (Tok.is(tok::kw_namespace))
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000192 // Next token after 'using' is 'namespace' so it must be using-directive
Chris Lattner9802a0a2009-04-02 04:16:50 +0000193 return ParseUsingDirective(Context, UsingLoc, DeclEnd);
Chris Lattner08ab4162009-01-06 06:55:51 +0000194
195 // Otherwise, it must be using-declaration.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000196 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000197}
198
199/// ParseUsingDirective - Parse C++ using-directive, assumes
200/// that current token is 'namespace' and 'using' was already parsed.
201///
202/// using-directive: [C++ 7.3.p4: namespace.udir]
203/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
204/// namespace-name ;
205/// [GNU] using-directive:
206/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
207/// namespace-name attributes[opt] ;
208///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000209Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000210 SourceLocation UsingLoc,
211 SourceLocation &DeclEnd) {
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000212 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
213
214 // Eat 'namespace'.
215 SourceLocation NamespcLoc = ConsumeToken();
216
217 CXXScopeSpec SS;
218 // Parse (optional) nested-name-specifier.
Chris Lattnerd706dc82009-01-06 06:59:53 +0000219 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000220
221 AttributeList *AttrList = 0;
222 IdentifierInfo *NamespcName = 0;
223 SourceLocation IdentLoc = SourceLocation();
224
225 // Parse namespace-name.
Chris Lattner7898bf62009-01-06 07:27:21 +0000226 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000227 Diag(Tok, diag::err_expected_namespace_name);
228 // If there was invalid namespace name, skip to end of decl, and eat ';'.
229 SkipUntil(tok::semi);
230 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattner5261d0c2009-03-28 19:18:32 +0000231 return DeclPtrTy();
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000232 }
Chris Lattner7898bf62009-01-06 07:27:21 +0000233
234 // Parse identifier.
235 NamespcName = Tok.getIdentifierInfo();
236 IdentLoc = ConsumeToken();
237
238 // Parse (optional) attributes (most likely GNU strong-using extension).
239 if (Tok.is(tok::kw___attribute))
240 AttrList = ParseAttributes();
241
242 // Eat ';'.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000243 DeclEnd = Tok.getLocation();
Chris Lattnercb9057e2009-06-14 00:07:48 +0000244 ExpectAndConsume(tok::semi,
245 AttrList ? diag::err_expected_semi_after_attribute_list :
246 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000247
248 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner7898bf62009-01-06 07:27:21 +0000249 IdentLoc, NamespcName, AttrList);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000250}
251
252/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
253/// 'using' was already seen.
254///
255/// using-declaration: [C++ 7.3.p3: namespace.udecl]
256/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor683a1142009-06-20 00:51:54 +0000257/// unqualified-id
258/// 'using' :: unqualified-id
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000259///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000260Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000261 SourceLocation UsingLoc,
262 SourceLocation &DeclEnd) {
Douglas Gregor683a1142009-06-20 00:51:54 +0000263 CXXScopeSpec SS;
264 bool IsTypeName;
265
266 // Ignore optional 'typename'.
267 if (Tok.is(tok::kw_typename)) {
268 ConsumeToken();
269 IsTypeName = true;
270 }
271 else
272 IsTypeName = false;
273
274 // Parse nested-name-specifier.
275 ParseOptionalCXXScopeSpecifier(SS);
276
277 AttributeList *AttrList = 0;
Douglas Gregor683a1142009-06-20 00:51:54 +0000278
279 // Check nested-name specifier.
280 if (SS.isInvalid()) {
281 SkipUntil(tok::semi);
282 return DeclPtrTy();
283 }
284 if (Tok.is(tok::annot_template_id)) {
285 Diag(Tok, diag::err_unexpected_template_spec_in_using);
286 SkipUntil(tok::semi);
287 return DeclPtrTy();
288 }
Anders Carlssone8c36f22009-06-27 00:27:47 +0000289
290 IdentifierInfo *TargetName = 0;
291 OverloadedOperatorKind Op = OO_None;
292 SourceLocation IdentLoc;
293
294 if (Tok.is(tok::kw_operator)) {
295 IdentLoc = Tok.getLocation();
296
297 Op = TryParseOperatorFunctionId();
298 if (!Op) {
299 // If there was an invalid operator, skip to end of decl, and eat ';'.
300 SkipUntil(tok::semi);
301 return DeclPtrTy();
302 }
303 } else if (Tok.is(tok::identifier)) {
304 // Parse identifier.
305 TargetName = Tok.getIdentifierInfo();
306 IdentLoc = ConsumeToken();
307 } else {
308 // FIXME: Use a better diagnostic here.
Douglas Gregor683a1142009-06-20 00:51:54 +0000309 Diag(Tok, diag::err_expected_ident_in_using);
Anders Carlssone8c36f22009-06-27 00:27:47 +0000310
Douglas Gregor683a1142009-06-20 00:51:54 +0000311 // If there was invalid identifier, skip to end of decl, and eat ';'.
312 SkipUntil(tok::semi);
313 return DeclPtrTy();
314 }
315
Douglas Gregor683a1142009-06-20 00:51:54 +0000316 // Parse (optional) attributes (most likely GNU strong-using extension).
317 if (Tok.is(tok::kw___attribute))
318 AttrList = ParseAttributes();
319
320 // Eat ';'.
321 DeclEnd = Tok.getLocation();
322 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
323 AttrList ? "attributes list" : "namespace name", tok::semi);
324
325 return Actions.ActOnUsingDeclaration(CurScope, UsingLoc, SS,
Anders Carlssone8c36f22009-06-27 00:27:47 +0000326 IdentLoc, TargetName, Op,
327 AttrList, IsTypeName);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000328}
329
Anders Carlssonab041982009-03-11 16:27:10 +0000330/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
331///
332/// static_assert-declaration:
333/// static_assert ( constant-expression , string-literal ) ;
334///
Chris Lattner9802a0a2009-04-02 04:16:50 +0000335Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlssonab041982009-03-11 16:27:10 +0000336 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
337 SourceLocation StaticAssertLoc = ConsumeToken();
338
339 if (Tok.isNot(tok::l_paren)) {
340 Diag(Tok, diag::err_expected_lparen);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000341 return DeclPtrTy();
Anders Carlssonab041982009-03-11 16:27:10 +0000342 }
343
344 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor98189262009-06-19 23:52:42 +0000345
Anders Carlssonab041982009-03-11 16:27:10 +0000346 OwningExprResult AssertExpr(ParseConstantExpression());
347 if (AssertExpr.isInvalid()) {
348 SkipUntil(tok::semi);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000349 return DeclPtrTy();
Anders Carlssonab041982009-03-11 16:27:10 +0000350 }
351
Anders Carlssona24e8d52009-03-13 23:29:20 +0000352 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000353 return DeclPtrTy();
Anders Carlssona24e8d52009-03-13 23:29:20 +0000354
Anders Carlssonab041982009-03-11 16:27:10 +0000355 if (Tok.isNot(tok::string_literal)) {
356 Diag(Tok, diag::err_expected_string_literal);
357 SkipUntil(tok::semi);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000358 return DeclPtrTy();
Anders Carlssonab041982009-03-11 16:27:10 +0000359 }
360
361 OwningExprResult AssertMessage(ParseStringLiteralExpression());
362 if (AssertMessage.isInvalid())
Chris Lattner5261d0c2009-03-28 19:18:32 +0000363 return DeclPtrTy();
Anders Carlssonab041982009-03-11 16:27:10 +0000364
Anders Carlssonc45057a2009-03-15 18:44:04 +0000365 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonab041982009-03-11 16:27:10 +0000366
Chris Lattner9802a0a2009-04-02 04:16:50 +0000367 DeclEnd = Tok.getLocation();
Anders Carlssonab041982009-03-11 16:27:10 +0000368 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
369
Anders Carlssona24e8d52009-03-13 23:29:20 +0000370 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlssonc45057a2009-03-15 18:44:04 +0000371 move(AssertMessage));
Anders Carlssonab041982009-03-11 16:27:10 +0000372}
373
Anders Carlssoneed418b2009-06-24 17:47:40 +0000374/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
375///
376/// 'decltype' ( expression )
377///
378void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
379 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
380
381 SourceLocation StartLoc = ConsumeToken();
382 SourceLocation LParenLoc = Tok.getLocation();
383
Anders Carlssoneed418b2009-06-24 17:47:40 +0000384 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
385 "decltype")) {
386 SkipUntil(tok::r_paren);
387 return;
388 }
389
Anders Carlssoneed418b2009-06-24 17:47:40 +0000390 // Parse the expression
391
392 // C++0x [dcl.type.simple]p4:
393 // The operand of the decltype specifier is an unevaluated operand.
394 EnterExpressionEvaluationContext Unevaluated(Actions,
395 Action::Unevaluated);
396 OwningExprResult Result = ParseExpression();
397 if (Result.isInvalid()) {
398 SkipUntil(tok::r_paren);
399 return;
400 }
401
402 // Match the ')'
403 SourceLocation RParenLoc;
404 if (Tok.is(tok::r_paren))
405 RParenLoc = ConsumeParen();
406 else
407 MatchRHSPunctuation(tok::r_paren, LParenLoc);
408
409 if (RParenLoc.isInvalid())
410 return;
411
412 const char *PrevSpec = 0;
413 // Check for duplicate type specifiers (e.g. "int decltype(a)").
414 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
415 Result.release()))
416 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
417}
418
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000419/// ParseClassName - Parse a C++ class-name, which names a class. Note
420/// that we only check that the result names a type; semantic analysis
421/// will need to verify that the type names a class. The result is
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000422/// either a type or NULL, depending on whether a type name was
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000423/// found.
424///
425/// class-name: [C++ 9.1]
426/// identifier
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000427/// simple-template-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000428///
Douglas Gregord7cb0372009-04-01 21:51:26 +0000429Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahanian1b8fd752009-07-20 17:43:15 +0000430 const CXXScopeSpec *SS,
431 bool DestrExpected) {
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000432 // Check whether we have a template-id that names a type.
433 if (Tok.is(tok::annot_template_id)) {
434 TemplateIdAnnotation *TemplateId
435 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000436 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000437 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000438
439 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
440 TypeTy *Type = Tok.getAnnotationValue();
441 EndLocation = Tok.getAnnotationEndLoc();
442 ConsumeToken();
Douglas Gregord7cb0372009-04-01 21:51:26 +0000443
444 if (Type)
445 return Type;
446 return true;
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000447 }
448
449 // Fall through to produce an error below.
450 }
451
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000452 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000453 Diag(Tok, diag::err_expected_class_name);
Douglas Gregord7cb0372009-04-01 21:51:26 +0000454 return true;
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000455 }
456
457 // We have an identifier; check whether it is actually a type.
Douglas Gregor1075a162009-02-04 17:00:24 +0000458 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
459 Tok.getLocation(), CurScope, SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000460 if (!Type) {
Fariborz Jahanian1b8fd752009-07-20 17:43:15 +0000461 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
462 : diag::err_expected_class_name);
Douglas Gregord7cb0372009-04-01 21:51:26 +0000463 return true;
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000464 }
465
466 // Consume the identifier.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000467 EndLocation = ConsumeToken();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000468 return Type;
469}
470
Douglas Gregorec93f442008-04-13 21:30:24 +0000471/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
472/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
473/// until we reach the start of a definition or see a token that
474/// cannot start a definition.
475///
476/// class-specifier: [C++ class]
477/// class-head '{' member-specification[opt] '}'
478/// class-head '{' member-specification[opt] '}' attributes[opt]
479/// class-head:
480/// class-key identifier[opt] base-clause[opt]
481/// class-key nested-name-specifier identifier base-clause[opt]
482/// class-key nested-name-specifier[opt] simple-template-id
483/// base-clause[opt]
484/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
485/// [GNU] class-key attributes[opt] nested-name-specifier
486/// identifier base-clause[opt]
487/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
488/// simple-template-id base-clause[opt]
489/// class-key:
490/// 'class'
491/// 'struct'
492/// 'union'
493///
494/// elaborated-type-specifier: [C++ dcl.type.elab]
495/// class-key ::[opt] nested-name-specifier[opt] identifier
496/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
497/// simple-template-id
498///
499/// Note that the C++ class-specifier and elaborated-type-specifier,
500/// together, subsume the C99 struct-or-union-specifier:
501///
502/// struct-or-union-specifier: [C99 6.7.2.1]
503/// struct-or-union identifier[opt] '{' struct-contents '}'
504/// struct-or-union identifier
505/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
506/// '}' attributes[opt]
507/// [GNU] struct-or-union attributes[opt] identifier
508/// struct-or-union:
509/// 'struct'
510/// 'union'
Chris Lattner197b4342009-04-12 21:49:30 +0000511void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
512 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000513 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000514 AccessSpecifier AS) {
Chris Lattner197b4342009-04-12 21:49:30 +0000515 DeclSpec::TST TagType;
516 if (TagTokKind == tok::kw_struct)
517 TagType = DeclSpec::TST_struct;
518 else if (TagTokKind == tok::kw_class)
519 TagType = DeclSpec::TST_class;
520 else {
521 assert(TagTokKind == tok::kw_union && "Not a class specifier");
522 TagType = DeclSpec::TST_union;
523 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000524
525 AttributeList *Attr = 0;
526 // If attributes exist after tag, parse them.
527 if (Tok.is(tok::kw___attribute))
528 Attr = ParseAttributes();
529
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000530 // If declspecs exist after tag, parse them.
Eli Friedman891d82f2009-06-08 23:27:34 +0000531 if (Tok.is(tok::kw___declspec))
532 Attr = ParseMicrosoftDeclSpec(Attr);
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000533
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000534 // Parse the (optional) nested-name-specifier.
535 CXXScopeSpec SS;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000536 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS))
537 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000538 Diag(Tok, diag::err_expected_ident);
Douglas Gregora08b6c72009-02-17 23:15:12 +0000539
540 // Parse the (optional) class name or simple-template-id.
Douglas Gregorec93f442008-04-13 21:30:24 +0000541 IdentifierInfo *Name = 0;
542 SourceLocation NameLoc;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000543 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregorec93f442008-04-13 21:30:24 +0000544 if (Tok.is(tok::identifier)) {
545 Name = Tok.getIdentifierInfo();
546 NameLoc = ConsumeToken();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000547 } else if (Tok.is(tok::annot_template_id)) {
548 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
549 NameLoc = ConsumeToken();
Douglas Gregora08b6c72009-02-17 23:15:12 +0000550
Douglas Gregoraabb8502009-03-31 00:43:58 +0000551 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000552 // The template-name in the simple-template-id refers to
553 // something other than a class template. Give an appropriate
554 // error message and skip to the ';'.
555 SourceRange Range(NameLoc);
556 if (SS.isNotEmpty())
557 Range.setBegin(SS.getBeginLoc());
Douglas Gregora08b6c72009-02-17 23:15:12 +0000558
Douglas Gregor0c281a82009-02-25 19:37:18 +0000559 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
560 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000561
Douglas Gregor0c281a82009-02-25 19:37:18 +0000562 DS.SetTypeSpecError();
563 SkipUntil(tok::semi, false, true);
564 TemplateId->Destroy();
565 return;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000566 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000567 }
568
569 // There are three options here. If we have 'struct foo;', then
570 // this is a forward declaration. If we have 'struct foo {...' or
Douglas Gregor0c281a82009-02-25 19:37:18 +0000571 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregorec93f442008-04-13 21:30:24 +0000572 // something like 'struct foo xyz', a reference.
573 Action::TagKind TK;
574 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
575 TK = Action::TK_Definition;
Anders Carlsson919a8d42009-05-11 22:25:03 +0000576 else if (Tok.is(tok::semi) && !DS.isFriendSpecified())
Douglas Gregorec93f442008-04-13 21:30:24 +0000577 TK = Action::TK_Declaration;
578 else
579 TK = Action::TK_Reference;
580
Douglas Gregor0c281a82009-02-25 19:37:18 +0000581 if (!Name && !TemplateId && TK != Action::TK_Definition) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000582 // We have a declaration or reference to an anonymous class.
Chris Lattnerf006a222008-11-18 07:48:38 +0000583 Diag(StartLoc, diag::err_anon_type_definition)
584 << DeclSpec::getSpecifierName(TagType);
Douglas Gregorec93f442008-04-13 21:30:24 +0000585
586 // Skip the rest of this declarator, up until the comma or semicolon.
587 SkipUntil(tok::comma, true);
Douglas Gregor0c281a82009-02-25 19:37:18 +0000588
589 if (TemplateId)
590 TemplateId->Destroy();
Douglas Gregorec93f442008-04-13 21:30:24 +0000591 return;
592 }
593
Douglas Gregord406b032009-02-06 22:42:48 +0000594 // Create the tag portion of the class or class template.
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000595 Action::DeclResult TagOrTempResult;
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000596 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
597
598 // FIXME: When TK == TK_Reference and we have a template-id, we need
599 // to turn that template-id into a type.
600
Douglas Gregor71f06032009-05-28 23:31:59 +0000601 bool Owned = false;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000602 if (TemplateId && TK != Action::TK_Reference) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000603 // Explicit specialization, class template partial specialization,
604 // or explicit instantiation.
Douglas Gregor0c281a82009-02-25 19:37:18 +0000605 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
606 TemplateId->getTemplateArgs(),
607 TemplateId->getTemplateArgIsType(),
608 TemplateId->NumArgs);
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000609 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
610 TK == Action::TK_Declaration) {
611 // This is an explicit instantiation of a class template.
612 TagOrTempResult
613 = Actions.ActOnExplicitInstantiation(CurScope,
614 TemplateInfo.TemplateLoc,
615 TagType,
616 StartLoc,
617 SS,
618 TemplateTy::make(TemplateId->Template),
619 TemplateId->TemplateNameLoc,
620 TemplateId->LAngleLoc,
621 TemplateArgsPtr,
622 TemplateId->getTemplateArgLocations(),
623 TemplateId->RAngleLoc,
624 Attr);
625 } else {
626 // This is an explicit specialization or a class template
627 // partial specialization.
628 TemplateParameterLists FakedParamLists;
629
630 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
631 // This looks like an explicit instantiation, because we have
632 // something like
633 //
634 // template class Foo<X>
635 //
Douglas Gregor96b6df92009-05-14 00:28:11 +0000636 // but it actually has a definition. Most likely, this was
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000637 // meant to be an explicit specialization, but the user forgot
638 // the '<>' after 'template'.
Douglas Gregor96b6df92009-05-14 00:28:11 +0000639 assert(TK == Action::TK_Definition && "Expected a definition here");
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000640
641 SourceLocation LAngleLoc
642 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
643 Diag(TemplateId->TemplateNameLoc,
644 diag::err_explicit_instantiation_with_definition)
645 << SourceRange(TemplateInfo.TemplateLoc)
646 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
647
648 // Create a fake template parameter list that contains only
649 // "template<>", so that we treat this construct as a class
650 // template specialization.
651 FakedParamLists.push_back(
652 Actions.ActOnTemplateParameterList(0, SourceLocation(),
653 TemplateInfo.TemplateLoc,
654 LAngleLoc,
655 0, 0,
656 LAngleLoc));
657 TemplateParams = &FakedParamLists;
658 }
659
660 // Build the class template specialization.
661 TagOrTempResult
662 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK,
Douglas Gregor0c281a82009-02-25 19:37:18 +0000663 StartLoc, SS,
Douglas Gregordd13e842009-03-30 22:58:21 +0000664 TemplateTy::make(TemplateId->Template),
Douglas Gregor0c281a82009-02-25 19:37:18 +0000665 TemplateId->TemplateNameLoc,
666 TemplateId->LAngleLoc,
667 TemplateArgsPtr,
668 TemplateId->getTemplateArgLocations(),
669 TemplateId->RAngleLoc,
670 Attr,
Douglas Gregora08b6c72009-02-17 23:15:12 +0000671 Action::MultiTemplateParamsArg(Actions,
672 TemplateParams? &(*TemplateParams)[0] : 0,
673 TemplateParams? TemplateParams->size() : 0));
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000674 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000675 TemplateId->Destroy();
Douglas Gregor96b6df92009-05-14 00:28:11 +0000676 } else if (TemplateParams && TK != Action::TK_Reference) {
677 // Class template declaration or definition.
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000678 TagOrTempResult = Actions.ActOnClassTemplate(CurScope, TagType, TK,
679 StartLoc, SS, Name, NameLoc,
680 Attr,
Douglas Gregord406b032009-02-06 22:42:48 +0000681 Action::MultiTemplateParamsArg(Actions,
682 &(*TemplateParams)[0],
Anders Carlssoned20fb92009-03-26 00:52:18 +0000683 TemplateParams->size()),
684 AS);
Douglas Gregor96b6df92009-05-14 00:28:11 +0000685 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
686 TK == Action::TK_Declaration) {
687 // Explicit instantiation of a member of a class template
688 // specialization, e.g.,
689 //
690 // template struct Outer<int>::Inner;
691 //
692 TagOrTempResult
693 = Actions.ActOnExplicitInstantiation(CurScope,
694 TemplateInfo.TemplateLoc,
695 TagType, StartLoc, SS, Name,
696 NameLoc, Attr);
697 } else {
698 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
699 TK == Action::TK_Definition) {
700 // FIXME: Diagnose this particular error.
701 }
702
703 // Declaration or definition of a class type
704 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS,
Douglas Gregor71f06032009-05-28 23:31:59 +0000705 Name, NameLoc, Attr, AS, Owned);
Douglas Gregor96b6df92009-05-14 00:28:11 +0000706 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000707
708 // Parse the optional base clause (C++ only).
Chris Lattner31ccf0a2009-02-16 22:07:16 +0000709 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000710 ParseBaseClause(TagOrTempResult.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000711
712 // If there is a body, parse it and inform the actions module.
713 if (Tok.is(tok::l_brace))
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000714 if (getLang().CPlusPlus)
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000715 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000716 else
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000717 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000718 else if (TK == Action::TK_Definition) {
719 // FIXME: Complain that we have a base-specifier list but no
720 // definition.
Chris Lattnerf006a222008-11-18 07:48:38 +0000721 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregorec93f442008-04-13 21:30:24 +0000722 }
723
724 const char *PrevSpec = 0;
Anders Carlssonef3fa4f2009-05-11 22:27:47 +0000725 if (TagOrTempResult.isInvalid()) {
Douglas Gregord406b032009-02-06 22:42:48 +0000726 DS.SetTypeSpecError();
Anders Carlssonef3fa4f2009-05-11 22:27:47 +0000727 return;
728 }
729
Anders Carlssonef3fa4f2009-05-11 22:27:47 +0000730 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec,
Douglas Gregor71f06032009-05-28 23:31:59 +0000731 TagOrTempResult.get().getAs<void>(), Owned))
Chris Lattnerf006a222008-11-18 07:48:38 +0000732 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Anders Carlssone5e645d2009-05-11 22:42:30 +0000733
734 if (DS.isFriendSpecified())
735 Actions.ActOnFriendDecl(CurScope, DS.getFriendSpecLoc(),
736 TagOrTempResult.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000737}
738
739/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
740///
741/// base-clause : [C++ class.derived]
742/// ':' base-specifier-list
743/// base-specifier-list:
744/// base-specifier '...'[opt]
745/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattner5261d0c2009-03-28 19:18:32 +0000746void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000747 assert(Tok.is(tok::colon) && "Not a base clause");
748 ConsumeToken();
749
Douglas Gregorabed2172008-10-22 17:49:05 +0000750 // Build up an array of parsed base specifiers.
751 llvm::SmallVector<BaseTy *, 8> BaseInfo;
752
Douglas Gregorec93f442008-04-13 21:30:24 +0000753 while (true) {
754 // Parse a base-specifier.
Douglas Gregorabed2172008-10-22 17:49:05 +0000755 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000756 if (Result.isInvalid()) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000757 // Skip the rest of this base specifier, up until the comma or
758 // opening brace.
Douglas Gregorabed2172008-10-22 17:49:05 +0000759 SkipUntil(tok::comma, tok::l_brace, true, true);
760 } else {
761 // Add this to our array of base specifiers.
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000762 BaseInfo.push_back(Result.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000763 }
764
765 // If the next token is a comma, consume it and keep reading
766 // base-specifiers.
767 if (Tok.isNot(tok::comma)) break;
768
769 // Consume the comma.
770 ConsumeToken();
771 }
Douglas Gregorabed2172008-10-22 17:49:05 +0000772
773 // Attach the base specifiers
Jay Foad9e6bef42009-05-21 09:52:38 +0000774 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregorec93f442008-04-13 21:30:24 +0000775}
776
777/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
778/// one entry in the base class list of a class specifier, for example:
779/// class foo : public bar, virtual private baz {
780/// 'public bar' and 'virtual private baz' are each base-specifiers.
781///
782/// base-specifier: [C++ class.derived]
783/// ::[opt] nested-name-specifier[opt] class-name
784/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
785/// class-name
786/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
787/// class-name
Chris Lattner5261d0c2009-03-28 19:18:32 +0000788Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000789 bool IsVirtual = false;
790 SourceLocation StartLoc = Tok.getLocation();
791
792 // Parse the 'virtual' keyword.
793 if (Tok.is(tok::kw_virtual)) {
794 ConsumeToken();
795 IsVirtual = true;
796 }
797
798 // Parse an (optional) access specifier.
799 AccessSpecifier Access = getAccessSpecifierIfPresent();
800 if (Access)
801 ConsumeToken();
802
803 // Parse the 'virtual' keyword (again!), in case it came after the
804 // access specifier.
805 if (Tok.is(tok::kw_virtual)) {
806 SourceLocation VirtualLoc = ConsumeToken();
807 if (IsVirtual) {
808 // Complain about duplicate 'virtual'
Chris Lattnerf006a222008-11-18 07:48:38 +0000809 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregord7cb0372009-04-01 21:51:26 +0000810 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregorec93f442008-04-13 21:30:24 +0000811 }
812
813 IsVirtual = true;
814 }
815
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000816 // Parse optional '::' and optional nested-name-specifier.
817 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +0000818 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregorec93f442008-04-13 21:30:24 +0000819
Douglas Gregorec93f442008-04-13 21:30:24 +0000820 // The location of the base class itself.
821 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000822
823 // Parse the class-name.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000824 SourceLocation EndLocation;
Douglas Gregord7cb0372009-04-01 21:51:26 +0000825 TypeResult BaseType = ParseClassName(EndLocation, &SS);
826 if (BaseType.isInvalid())
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000827 return true;
Douglas Gregorec93f442008-04-13 21:30:24 +0000828
829 // Find the complete source range for the base-specifier.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000830 SourceRange Range(StartLoc, EndLocation);
Douglas Gregorec93f442008-04-13 21:30:24 +0000831
Douglas Gregorec93f442008-04-13 21:30:24 +0000832 // Notify semantic analysis that we have parsed a complete
833 // base-specifier.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000834 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregord7cb0372009-04-01 21:51:26 +0000835 BaseType.get(), BaseLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000836}
837
838/// getAccessSpecifierIfPresent - Determine whether the next token is
839/// a C++ access-specifier.
840///
841/// access-specifier: [C++ class.derived]
842/// 'private'
843/// 'protected'
844/// 'public'
Douglas Gregor696be932008-04-14 00:13:42 +0000845AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregorec93f442008-04-13 21:30:24 +0000846{
847 switch (Tok.getKind()) {
848 default: return AS_none;
849 case tok::kw_private: return AS_private;
850 case tok::kw_protected: return AS_protected;
851 case tok::kw_public: return AS_public;
852 }
853}
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000854
Eli Friedman40035e22009-07-22 21:45:50 +0000855void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
856 DeclPtrTy ThisDecl) {
857 // We just declared a member function. If this member function
858 // has any default arguments, we'll need to parse them later.
859 LateParsedMethodDeclaration *LateMethod = 0;
860 DeclaratorChunk::FunctionTypeInfo &FTI
861 = DeclaratorInfo.getTypeObject(0).Fun;
862 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
863 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
864 if (!LateMethod) {
865 // Push this method onto the stack of late-parsed method
866 // declarations.
867 getCurrentClass().MethodDecls.push_back(
868 LateParsedMethodDeclaration(ThisDecl));
869 LateMethod = &getCurrentClass().MethodDecls.back();
870
871 // Add all of the parameters prior to this one (they don't
872 // have default arguments).
873 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
874 for (unsigned I = 0; I < ParamIdx; ++I)
875 LateMethod->DefaultArgs.push_back(
876 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
877 }
878
879 // Add this parameter to the list of parameters (it or may
880 // not have a default argument).
881 LateMethod->DefaultArgs.push_back(
882 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
883 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
884 }
885 }
886}
887
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000888/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
889///
890/// member-declaration:
891/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
892/// function-definition ';'[opt]
893/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
894/// using-declaration [TODO]
Anders Carlssonab041982009-03-11 16:27:10 +0000895/// [C++0x] static_assert-declaration
Anders Carlssoned20fb92009-03-26 00:52:18 +0000896/// template-declaration
Chris Lattnerf3375de2008-12-18 01:12:00 +0000897/// [GNU] '__extension__' member-declaration
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000898///
899/// member-declarator-list:
900/// member-declarator
901/// member-declarator-list ',' member-declarator
902///
903/// member-declarator:
904/// declarator pure-specifier[opt]
905/// declarator constant-initializer[opt]
906/// identifier[opt] ':' constant-expression
907///
Sebastian Redla55834a2009-04-12 17:16:29 +0000908/// pure-specifier:
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000909/// '= 0'
910///
911/// constant-initializer:
912/// '=' constant-expression
913///
Chris Lattnera17991f2009-03-29 16:50:03 +0000914void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Anders Carlssonab041982009-03-11 16:27:10 +0000915 // static_assert-declaration
Chris Lattnera17991f2009-03-29 16:50:03 +0000916 if (Tok.is(tok::kw_static_assert)) {
Chris Lattner9802a0a2009-04-02 04:16:50 +0000917 SourceLocation DeclEnd;
918 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000919 return;
920 }
Anders Carlssonab041982009-03-11 16:27:10 +0000921
Chris Lattnera17991f2009-03-29 16:50:03 +0000922 if (Tok.is(tok::kw_template)) {
Chris Lattner9802a0a2009-04-02 04:16:50 +0000923 SourceLocation DeclEnd;
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000924 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
925 AS);
Chris Lattnera17991f2009-03-29 16:50:03 +0000926 return;
927 }
Anders Carlssoned20fb92009-03-26 00:52:18 +0000928
Chris Lattnerf3375de2008-12-18 01:12:00 +0000929 // Handle: member-declaration ::= '__extension__' member-declaration
930 if (Tok.is(tok::kw___extension__)) {
931 // __extension__ silences extension warnings in the subexpression.
932 ExtensionRAIIObject O(Diags); // Use RAII to do this.
933 ConsumeToken();
934 return ParseCXXClassMemberDeclaration(AS);
935 }
Douglas Gregor683a1142009-06-20 00:51:54 +0000936
937 if (Tok.is(tok::kw_using)) {
938 // Eat 'using'.
939 SourceLocation UsingLoc = ConsumeToken();
940
941 if (Tok.is(tok::kw_namespace)) {
942 Diag(UsingLoc, diag::err_using_namespace_in_class);
943 SkipUntil(tok::semi, true, true);
944 }
945 else {
946 SourceLocation DeclEnd;
947 // Otherwise, it must be using-declaration.
948 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd);
949 }
950 return;
951 }
952
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000953 SourceLocation DSStart = Tok.getLocation();
954 // decl-specifier-seq:
955 // Parse the common declaration-specifiers piece.
956 DeclSpec DS;
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000957 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000958
959 if (Tok.is(tok::semi)) {
960 ConsumeToken();
961 // C++ 9.2p7: The member-declarator-list can be omitted only after a
962 // class-specifier or an enum-specifier or in a friend declaration.
963 // FIXME: Friend declarations.
964 switch (DS.getTypeSpecType()) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000965 case DeclSpec::TST_struct:
966 case DeclSpec::TST_union:
967 case DeclSpec::TST_class:
968 case DeclSpec::TST_enum:
969 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
970 return;
971 default:
972 Diag(DSStart, diag::err_no_declarators);
973 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000974 }
975 }
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000976
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000977 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000978
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000979 if (Tok.isNot(tok::colon)) {
980 // Parse the first declarator.
981 ParseDeclarator(DeclaratorInfo);
982 // Error parsing the declarator?
Douglas Gregor6704b312008-11-17 22:58:34 +0000983 if (!DeclaratorInfo.hasName()) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000984 // If so, skip until the semi-colon or a }.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000985 SkipUntil(tok::r_brace, true);
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000986 if (Tok.is(tok::semi))
987 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000988 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000989 }
990
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000991 // function-definition:
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000992 if (Tok.is(tok::l_brace)
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000993 || (DeclaratorInfo.isFunctionDeclarator() &&
994 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000995 if (!DeclaratorInfo.isFunctionDeclarator()) {
996 Diag(Tok, diag::err_func_def_no_params);
997 ConsumeBrace();
998 SkipUntil(tok::r_brace, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000999 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001000 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001001
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001002 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1003 Diag(Tok, diag::err_function_declared_typedef);
1004 // This recovery skips the entire function body. It would be nice
1005 // to simply call ParseCXXInlineMethodDef() below, however Sema
1006 // assumes the declarator represents a function, not a typedef.
1007 ConsumeBrace();
1008 SkipUntil(tok::r_brace, true);
Chris Lattnera17991f2009-03-29 16:50:03 +00001009 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001010 }
1011
Chris Lattnera17991f2009-03-29 16:50:03 +00001012 ParseCXXInlineMethodDef(AS, DeclaratorInfo);
1013 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001014 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001015 }
1016
1017 // member-declarator-list:
1018 // member-declarator
1019 // member-declarator-list ',' member-declarator
1020
Chris Lattnera17991f2009-03-29 16:50:03 +00001021 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl62261042008-12-09 20:22:58 +00001022 OwningExprResult BitfieldSize(Actions);
1023 OwningExprResult Init(Actions);
Sebastian Redla55834a2009-04-12 17:16:29 +00001024 bool Deleted = false;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001025
1026 while (1) {
1027
1028 // member-declarator:
1029 // declarator pure-specifier[opt]
1030 // declarator constant-initializer[opt]
1031 // identifier[opt] ':' constant-expression
1032
1033 if (Tok.is(tok::colon)) {
1034 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001035 BitfieldSize = ParseConstantExpression();
1036 if (BitfieldSize.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001037 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001038 }
1039
1040 // pure-specifier:
1041 // '= 0'
1042 //
1043 // constant-initializer:
1044 // '=' constant-expression
Sebastian Redla55834a2009-04-12 17:16:29 +00001045 //
1046 // defaulted/deleted function-definition:
1047 // '=' 'default' [TODO]
1048 // '=' 'delete'
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001049
1050 if (Tok.is(tok::equal)) {
1051 ConsumeToken();
Sebastian Redla55834a2009-04-12 17:16:29 +00001052 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1053 ConsumeToken();
1054 Deleted = true;
1055 } else {
1056 Init = ParseInitializer();
1057 if (Init.isInvalid())
1058 SkipUntil(tok::comma, true, true);
1059 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001060 }
1061
1062 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001063 if (Tok.is(tok::kw___attribute)) {
1064 SourceLocation Loc;
1065 AttributeList *AttrList = ParseAttributes(&Loc);
1066 DeclaratorInfo.AddAttributes(AttrList, Loc);
1067 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001068
Argiris Kirtzidis38f16712008-07-01 10:37:29 +00001069 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattnera17991f2009-03-29 16:50:03 +00001070 // this call will *not* return the created decl; It will return null.
Argiris Kirtzidis38f16712008-07-01 10:37:29 +00001071 // See Sema::ActOnCXXMemberDeclarator for details.
Chris Lattnera17991f2009-03-29 16:50:03 +00001072 DeclPtrTy ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1073 DeclaratorInfo,
1074 BitfieldSize.release(),
Sebastian Redla55834a2009-04-12 17:16:29 +00001075 Init.release(),
1076 Deleted);
Chris Lattnera17991f2009-03-29 16:50:03 +00001077 if (ThisDecl)
1078 DeclsInGroup.push_back(ThisDecl);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001079
Douglas Gregor605de8d2008-12-16 21:30:33 +00001080 if (DeclaratorInfo.isFunctionDeclarator() &&
1081 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
1082 != DeclSpec::SCS_typedef) {
Eli Friedman40035e22009-07-22 21:45:50 +00001083 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor605de8d2008-12-16 21:30:33 +00001084 }
1085
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001086 // If we don't have a comma, it is either the end of the list (a ';')
1087 // or an error, bail out.
1088 if (Tok.isNot(tok::comma))
1089 break;
1090
1091 // Consume the comma.
1092 ConsumeToken();
1093
1094 // Parse the next declarator.
1095 DeclaratorInfo.clear();
Sebastian Redl62261042008-12-09 20:22:58 +00001096 BitfieldSize = 0;
1097 Init = 0;
Sebastian Redla55834a2009-04-12 17:16:29 +00001098 Deleted = false;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001099
1100 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001101 if (Tok.is(tok::kw___attribute)) {
1102 SourceLocation Loc;
1103 AttributeList *AttrList = ParseAttributes(&Loc);
1104 DeclaratorInfo.AddAttributes(AttrList, Loc);
1105 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001106
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001107 if (Tok.isNot(tok::colon))
1108 ParseDeclarator(DeclaratorInfo);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001109 }
1110
1111 if (Tok.is(tok::semi)) {
1112 ConsumeToken();
Eli Friedman4d57af22009-05-29 01:49:24 +00001113 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattnera17991f2009-03-29 16:50:03 +00001114 DeclsInGroup.size());
1115 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001116 }
1117
1118 Diag(Tok, diag::err_expected_semi_decl_list);
1119 // Skip to end of block or statement
1120 SkipUntil(tok::r_brace, true, true);
1121 if (Tok.is(tok::semi))
1122 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +00001123 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001124}
1125
1126/// ParseCXXMemberSpecification - Parse the class definition.
1127///
1128/// member-specification:
1129/// member-declaration member-specification[opt]
1130/// access-specifier ':' member-specification[opt]
1131///
1132void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001133 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Guptafa451432008-10-31 09:52:39 +00001134 assert((TagType == DeclSpec::TST_struct ||
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001135 TagType == DeclSpec::TST_union ||
Sanjiv Guptafa451432008-10-31 09:52:39 +00001136 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001137
Chris Lattnerc309ade2009-03-05 08:00:35 +00001138 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1139 PP.getSourceManager(),
1140 "parsing struct/union/class body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001141
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001142 SourceLocation LBraceLoc = ConsumeBrace();
1143
Douglas Gregora376cbd2009-05-27 23:11:45 +00001144 // Determine whether this is a top-level (non-nested) class.
1145 bool TopLevelClass = ClassStack.empty() ||
1146 CurScope->isInCXXInlineMethodScope();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001147
1148 // Enter a scope for the class.
Douglas Gregorcab994d2009-01-09 22:42:13 +00001149 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001150
Douglas Gregora376cbd2009-05-27 23:11:45 +00001151 // Note that we are parsing a new (potentially-nested) class definition.
1152 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1153
Douglas Gregord406b032009-02-06 22:42:48 +00001154 if (TagDecl)
1155 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1156 else {
1157 SkipUntil(tok::r_brace, false, false);
1158 return;
1159 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001160
1161 // C++ 11p3: Members of a class defined with the keyword class are private
1162 // by default. Members of a class defined with the keywords struct or union
1163 // are public by default.
1164 AccessSpecifier CurAS;
1165 if (TagType == DeclSpec::TST_class)
1166 CurAS = AS_private;
1167 else
1168 CurAS = AS_public;
1169
1170 // While we still have something to read, read the member-declarations.
1171 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1172 // Each iteration of this loop reads one member-declaration.
1173
1174 // Check for extraneous top-level semicolon.
1175 if (Tok.is(tok::semi)) {
1176 Diag(Tok, diag::ext_extra_struct_semi);
1177 ConsumeToken();
1178 continue;
1179 }
1180
1181 AccessSpecifier AS = getAccessSpecifierIfPresent();
1182 if (AS != AS_none) {
1183 // Current token is a C++ access specifier.
1184 CurAS = AS;
1185 ConsumeToken();
1186 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1187 continue;
1188 }
1189
1190 // Parse all the comma separated declarators.
1191 ParseCXXClassMemberDeclaration(CurAS);
1192 }
1193
1194 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1195
1196 AttributeList *AttrList = 0;
1197 // If attributes exist after class contents, parse them.
1198 if (Tok.is(tok::kw___attribute))
1199 AttrList = ParseAttributes(); // FIXME: where should I put them?
1200
1201 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1202 LBraceLoc, RBraceLoc);
1203
1204 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1205 // complete within function bodies, default arguments,
1206 // exception-specifications, and constructor ctor-initializers (including
1207 // such things in nested classes).
1208 //
Douglas Gregor605de8d2008-12-16 21:30:33 +00001209 // FIXME: Only function bodies and constructor ctor-initializers are
1210 // parsed correctly, fix the rest.
Douglas Gregora376cbd2009-05-27 23:11:45 +00001211 if (TopLevelClass) {
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001212 // We are not inside a nested class. This class and its nested classes
Douglas Gregor605de8d2008-12-16 21:30:33 +00001213 // are complete and we can parse the delayed portions of method
1214 // declarations and the lexed inline method definitions.
Douglas Gregora376cbd2009-05-27 23:11:45 +00001215 ParseLexedMethodDeclarations(getCurrentClass());
1216 ParseLexedMethodDefs(getCurrentClass());
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001217 }
1218
1219 // Leave the class scope.
Douglas Gregora376cbd2009-05-27 23:11:45 +00001220 ParsingDef.Pop();
Douglas Gregor95d40792008-12-10 06:34:36 +00001221 ClassScope.Exit();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001222
Argiris Kirtzidiseb925642009-07-14 03:17:52 +00001223 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001224}
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001225
1226/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1227/// which explicitly initializes the members or base classes of a
1228/// class (C++ [class.base.init]). For example, the three initializers
1229/// after the ':' in the Derived constructor below:
1230///
1231/// @code
1232/// class Base { };
1233/// class Derived : Base {
1234/// int x;
1235/// float f;
1236/// public:
1237/// Derived(float f) : Base(), x(17), f(f) { }
1238/// };
1239/// @endcode
1240///
1241/// [C++] ctor-initializer:
1242/// ':' mem-initializer-list
1243///
1244/// [C++] mem-initializer-list:
1245/// mem-initializer
1246/// mem-initializer , mem-initializer-list
Chris Lattner5261d0c2009-03-28 19:18:32 +00001247void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001248 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1249
1250 SourceLocation ColonLoc = ConsumeToken();
1251
1252 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
1253
1254 do {
1255 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +00001256 if (!MemInit.isInvalid())
1257 MemInitializers.push_back(MemInit.get());
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001258
1259 if (Tok.is(tok::comma))
1260 ConsumeToken();
1261 else if (Tok.is(tok::l_brace))
1262 break;
1263 else {
1264 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redlbc9ef252009-04-26 20:35:05 +00001265 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001266 SkipUntil(tok::l_brace, true, true);
1267 break;
1268 }
1269 } while (true);
1270
1271 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +00001272 MemInitializers.data(), MemInitializers.size());
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001273}
1274
1275/// ParseMemInitializer - Parse a C++ member initializer, which is
1276/// part of a constructor initializer that explicitly initializes one
1277/// member or base class (C++ [class.base.init]). See
1278/// ParseConstructorInitializer for an example.
1279///
1280/// [C++] mem-initializer:
1281/// mem-initializer-id '(' expression-list[opt] ')'
1282///
1283/// [C++] mem-initializer-id:
1284/// '::'[opt] nested-name-specifier[opt] class-name
1285/// identifier
Chris Lattner5261d0c2009-03-28 19:18:32 +00001286Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianc37d3062009-06-30 23:26:25 +00001287 // parse '::'[opt] nested-name-specifier[opt]
1288 CXXScopeSpec SS;
1289 ParseOptionalCXXScopeSpecifier(SS);
Fariborz Jahanian2b2b7362009-07-01 19:21:19 +00001290 TypeTy *TemplateTypeTy = 0;
1291 if (Tok.is(tok::annot_template_id)) {
1292 TemplateIdAnnotation *TemplateId
1293 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1294 if (TemplateId->Kind == TNK_Type_template) {
1295 AnnotateTemplateIdTokenAsType(&SS);
1296 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1297 TemplateTypeTy = Tok.getAnnotationValue();
1298 }
1299 // FIXME. May need to check for TNK_Dependent_template as well.
1300 }
1301 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001302 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001303 return true;
1304 }
Fariborz Jahanian2b2b7362009-07-01 19:21:19 +00001305
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001306 // Get the identifier. This may be a member name or a class name,
1307 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian2b2b7362009-07-01 19:21:19 +00001308 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001309 SourceLocation IdLoc = ConsumeToken();
1310
1311 // Parse the '('.
1312 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001313 Diag(Tok, diag::err_expected_lparen);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001314 return true;
1315 }
1316 SourceLocation LParenLoc = ConsumeParen();
1317
1318 // Parse the optional expression-list.
Sebastian Redl6008ac32008-11-25 22:21:31 +00001319 ExprVector ArgExprs(Actions);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001320 CommaLocsTy CommaLocs;
1321 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1322 SkipUntil(tok::r_paren);
1323 return true;
1324 }
1325
1326 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1327
Fariborz Jahanian2b2b7362009-07-01 19:21:19 +00001328 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1329 TemplateTypeTy, IdLoc,
Sebastian Redl6008ac32008-11-25 22:21:31 +00001330 LParenLoc, ArgExprs.take(),
Jay Foad9e6bef42009-05-21 09:52:38 +00001331 ArgExprs.size(), CommaLocs.data(),
1332 RParenLoc);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001333}
Douglas Gregor90a2c972008-11-25 03:22:00 +00001334
1335/// ParseExceptionSpecification - Parse a C++ exception-specification
1336/// (C++ [except.spec]).
1337///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001338/// exception-specification:
1339/// 'throw' '(' type-id-list [opt] ')'
1340/// [MS] 'throw' '(' '...' ')'
Douglas Gregor90a2c972008-11-25 03:22:00 +00001341///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001342/// type-id-list:
1343/// type-id
1344/// type-id-list ',' type-id
Douglas Gregor90a2c972008-11-25 03:22:00 +00001345///
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001346bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlaaacda92009-05-29 18:02:33 +00001347 llvm::SmallVector<TypeTy*, 2>
1348 &Exceptions,
1349 llvm::SmallVector<SourceRange, 2>
1350 &Ranges,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001351 bool &hasAnyExceptionSpec) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00001352 assert(Tok.is(tok::kw_throw) && "expected throw");
1353
1354 SourceLocation ThrowLoc = ConsumeToken();
1355
1356 if (!Tok.is(tok::l_paren)) {
1357 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1358 }
1359 SourceLocation LParenLoc = ConsumeParen();
1360
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001361 // Parse throw(...), a Microsoft extension that means "this function
1362 // can throw anything".
1363 if (Tok.is(tok::ellipsis)) {
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001364 hasAnyExceptionSpec = true;
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001365 SourceLocation EllipsisLoc = ConsumeToken();
1366 if (!getLang().Microsoft)
1367 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redl0c986032009-02-09 18:23:29 +00001368 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001369 return false;
1370 }
1371
Douglas Gregor90a2c972008-11-25 03:22:00 +00001372 // Parse the sequence of type-ids.
Sebastian Redlaaacda92009-05-29 18:02:33 +00001373 SourceRange Range;
Douglas Gregor90a2c972008-11-25 03:22:00 +00001374 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlaaacda92009-05-29 18:02:33 +00001375 TypeResult Res(ParseTypeName(&Range));
1376 if (!Res.isInvalid()) {
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001377 Exceptions.push_back(Res.get());
Sebastian Redlaaacda92009-05-29 18:02:33 +00001378 Ranges.push_back(Range);
1379 }
Douglas Gregor90a2c972008-11-25 03:22:00 +00001380 if (Tok.is(tok::comma))
1381 ConsumeToken();
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001382 else
Douglas Gregor90a2c972008-11-25 03:22:00 +00001383 break;
1384 }
1385
Sebastian Redl0c986032009-02-09 18:23:29 +00001386 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001387 return false;
1388}
Douglas Gregora376cbd2009-05-27 23:11:45 +00001389
1390/// \brief We have just started parsing the definition of a new class,
1391/// so push that class onto our stack of classes that is currently
1392/// being parsed.
1393void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
1394 assert((TopLevelClass || !ClassStack.empty()) &&
1395 "Nested class without outer class");
1396 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1397}
1398
1399/// \brief Deallocate the given parsed class and all of its nested
1400/// classes.
1401void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1402 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1403 DeallocateParsedClasses(Class->NestedClasses[I]);
1404 delete Class;
1405}
1406
1407/// \brief Pop the top class of the stack of classes that are
1408/// currently being parsed.
1409///
1410/// This routine should be called when we have finished parsing the
1411/// definition of a class, but have not yet popped the Scope
1412/// associated with the class's definition.
1413///
1414/// \returns true if the class we've popped is a top-level class,
1415/// false otherwise.
1416void Parser::PopParsingClass() {
1417 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
1418
1419 ParsingClass *Victim = ClassStack.top();
1420 ClassStack.pop();
1421 if (Victim->TopLevelClass) {
1422 // Deallocate all of the nested classes of this class,
1423 // recursively: we don't need to keep any of this information.
1424 DeallocateParsedClasses(Victim);
1425 return;
1426 }
1427 assert(!ClassStack.empty() && "Missing top-level class?");
1428
1429 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1430 Victim->NestedClasses.empty()) {
1431 // The victim is a nested class, but we will not need to perform
1432 // any processing after the definition of this class since it has
1433 // no members whose handling was delayed. Therefore, we can just
1434 // remove this nested class.
1435 delete Victim;
1436 return;
1437 }
1438
1439 // This nested class has some members that will need to be processed
1440 // after the top-level class is completely defined. Therefore, add
1441 // it to the list of nested classes within its parent.
1442 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1443 ClassStack.top()->NestedClasses.push_back(Victim);
1444 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1445}