blob: bce9ee0d8266ed501e934cdfcbbe2299af500a3c [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;
John McCall9f6e0972009-08-03 20:12:06 +0000413 unsigned DiagID;
Anders Carlssoneed418b2009-06-24 17:47:40 +0000414 // Check for duplicate type specifiers (e.g. "int decltype(a)").
415 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCall9f6e0972009-08-03 20:12:06 +0000416 DiagID, Result.release()))
417 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlssoneed418b2009-06-24 17:47:40 +0000418}
419
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000420/// ParseClassName - Parse a C++ class-name, which names a class. Note
421/// that we only check that the result names a type; semantic analysis
422/// will need to verify that the type names a class. The result is
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000423/// either a type or NULL, depending on whether a type name was
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000424/// found.
425///
426/// class-name: [C++ 9.1]
427/// identifier
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000428/// simple-template-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000429///
Douglas Gregord7cb0372009-04-01 21:51:26 +0000430Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Fariborz Jahanian1b8fd752009-07-20 17:43:15 +0000431 const CXXScopeSpec *SS,
432 bool DestrExpected) {
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000433 // Check whether we have a template-id that names a type.
434 if (Tok.is(tok::annot_template_id)) {
435 TemplateIdAnnotation *TemplateId
436 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000437 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000438 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000439
440 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
441 TypeTy *Type = Tok.getAnnotationValue();
442 EndLocation = Tok.getAnnotationEndLoc();
443 ConsumeToken();
Douglas Gregord7cb0372009-04-01 21:51:26 +0000444
445 if (Type)
446 return Type;
447 return true;
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000448 }
449
450 // Fall through to produce an error below.
451 }
452
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000453 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000454 Diag(Tok, diag::err_expected_class_name);
Douglas Gregord7cb0372009-04-01 21:51:26 +0000455 return true;
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000456 }
457
458 // We have an identifier; check whether it is actually a type.
Douglas Gregor1075a162009-02-04 17:00:24 +0000459 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
460 Tok.getLocation(), CurScope, SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000461 if (!Type) {
Fariborz Jahanian1b8fd752009-07-20 17:43:15 +0000462 Diag(Tok, DestrExpected ? diag::err_destructor_class_name
463 : diag::err_expected_class_name);
Douglas Gregord7cb0372009-04-01 21:51:26 +0000464 return true;
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000465 }
466
467 // Consume the identifier.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000468 EndLocation = ConsumeToken();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000469 return Type;
470}
471
Douglas Gregorec93f442008-04-13 21:30:24 +0000472/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
473/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
474/// until we reach the start of a definition or see a token that
475/// cannot start a definition.
476///
477/// class-specifier: [C++ class]
478/// class-head '{' member-specification[opt] '}'
479/// class-head '{' member-specification[opt] '}' attributes[opt]
480/// class-head:
481/// class-key identifier[opt] base-clause[opt]
482/// class-key nested-name-specifier identifier base-clause[opt]
483/// class-key nested-name-specifier[opt] simple-template-id
484/// base-clause[opt]
485/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
486/// [GNU] class-key attributes[opt] nested-name-specifier
487/// identifier base-clause[opt]
488/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
489/// simple-template-id base-clause[opt]
490/// class-key:
491/// 'class'
492/// 'struct'
493/// 'union'
494///
495/// elaborated-type-specifier: [C++ dcl.type.elab]
496/// class-key ::[opt] nested-name-specifier[opt] identifier
497/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
498/// simple-template-id
499///
500/// Note that the C++ class-specifier and elaborated-type-specifier,
501/// together, subsume the C99 struct-or-union-specifier:
502///
503/// struct-or-union-specifier: [C99 6.7.2.1]
504/// struct-or-union identifier[opt] '{' struct-contents '}'
505/// struct-or-union identifier
506/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
507/// '}' attributes[opt]
508/// [GNU] struct-or-union attributes[opt] identifier
509/// struct-or-union:
510/// 'struct'
511/// 'union'
Chris Lattner197b4342009-04-12 21:49:30 +0000512void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
513 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000514 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000515 AccessSpecifier AS) {
Chris Lattner197b4342009-04-12 21:49:30 +0000516 DeclSpec::TST TagType;
517 if (TagTokKind == tok::kw_struct)
518 TagType = DeclSpec::TST_struct;
519 else if (TagTokKind == tok::kw_class)
520 TagType = DeclSpec::TST_class;
521 else {
522 assert(TagTokKind == tok::kw_union && "Not a class specifier");
523 TagType = DeclSpec::TST_union;
524 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000525
526 AttributeList *Attr = 0;
527 // If attributes exist after tag, parse them.
528 if (Tok.is(tok::kw___attribute))
529 Attr = ParseAttributes();
530
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000531 // If declspecs exist after tag, parse them.
Eli Friedman891d82f2009-06-08 23:27:34 +0000532 if (Tok.is(tok::kw___declspec))
533 Attr = ParseMicrosoftDeclSpec(Attr);
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000534
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000535 // Parse the (optional) nested-name-specifier.
536 CXXScopeSpec SS;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000537 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS))
538 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000539 Diag(Tok, diag::err_expected_ident);
Douglas Gregora08b6c72009-02-17 23:15:12 +0000540
541 // Parse the (optional) class name or simple-template-id.
Douglas Gregorec93f442008-04-13 21:30:24 +0000542 IdentifierInfo *Name = 0;
543 SourceLocation NameLoc;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000544 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregorec93f442008-04-13 21:30:24 +0000545 if (Tok.is(tok::identifier)) {
546 Name = Tok.getIdentifierInfo();
547 NameLoc = ConsumeToken();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000548 } else if (Tok.is(tok::annot_template_id)) {
549 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
550 NameLoc = ConsumeToken();
Douglas Gregora08b6c72009-02-17 23:15:12 +0000551
Douglas Gregoraabb8502009-03-31 00:43:58 +0000552 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000553 // The template-name in the simple-template-id refers to
554 // something other than a class template. Give an appropriate
555 // error message and skip to the ';'.
556 SourceRange Range(NameLoc);
557 if (SS.isNotEmpty())
558 Range.setBegin(SS.getBeginLoc());
Douglas Gregora08b6c72009-02-17 23:15:12 +0000559
Douglas Gregor0c281a82009-02-25 19:37:18 +0000560 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
561 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000562
Douglas Gregor0c281a82009-02-25 19:37:18 +0000563 DS.SetTypeSpecError();
564 SkipUntil(tok::semi, false, true);
565 TemplateId->Destroy();
566 return;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000567 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000568 }
569
John McCall140607b2009-08-06 02:15:43 +0000570 // There are four options here. If we have 'struct foo;', then this
571 // is either a forward declaration or a friend declaration, which
572 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor0c281a82009-02-25 19:37:18 +0000573 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregorec93f442008-04-13 21:30:24 +0000574 // something like 'struct foo xyz', a reference.
John McCall069c23a2009-07-31 02:45:11 +0000575 Action::TagUseKind TUK;
Douglas Gregorec93f442008-04-13 21:30:24 +0000576 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
John McCall069c23a2009-07-31 02:45:11 +0000577 TUK = Action::TUK_Definition;
John McCall140607b2009-08-06 02:15:43 +0000578 else if (Tok.is(tok::semi))
579 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregorec93f442008-04-13 21:30:24 +0000580 else
John McCall069c23a2009-07-31 02:45:11 +0000581 TUK = Action::TUK_Reference;
Douglas Gregorec93f442008-04-13 21:30:24 +0000582
John McCall069c23a2009-07-31 02:45:11 +0000583 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000584 // We have a declaration or reference to an anonymous class.
Chris Lattnerf006a222008-11-18 07:48:38 +0000585 Diag(StartLoc, diag::err_anon_type_definition)
586 << DeclSpec::getSpecifierName(TagType);
Douglas Gregorec93f442008-04-13 21:30:24 +0000587
588 // Skip the rest of this declarator, up until the comma or semicolon.
589 SkipUntil(tok::comma, true);
Douglas Gregor0c281a82009-02-25 19:37:18 +0000590
591 if (TemplateId)
592 TemplateId->Destroy();
Douglas Gregorec93f442008-04-13 21:30:24 +0000593 return;
594 }
595
Douglas Gregord406b032009-02-06 22:42:48 +0000596 // Create the tag portion of the class or class template.
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000597 Action::DeclResult TagOrTempResult;
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000598 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
599
John McCall069c23a2009-07-31 02:45:11 +0000600 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000601 // to turn that template-id into a type.
602
Douglas Gregor71f06032009-05-28 23:31:59 +0000603 bool Owned = false;
John McCall140607b2009-08-06 02:15:43 +0000604 if (TemplateId && TUK != Action::TUK_Reference && TUK != Action::TUK_Friend) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000605 // Explicit specialization, class template partial specialization,
606 // or explicit instantiation.
Douglas Gregor0c281a82009-02-25 19:37:18 +0000607 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
608 TemplateId->getTemplateArgs(),
609 TemplateId->getTemplateArgIsType(),
610 TemplateId->NumArgs);
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000611 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall069c23a2009-07-31 02:45:11 +0000612 TUK == Action::TUK_Declaration) {
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000613 // This is an explicit instantiation of a class template.
614 TagOrTempResult
615 = Actions.ActOnExplicitInstantiation(CurScope,
616 TemplateInfo.TemplateLoc,
617 TagType,
618 StartLoc,
619 SS,
620 TemplateTy::make(TemplateId->Template),
621 TemplateId->TemplateNameLoc,
622 TemplateId->LAngleLoc,
623 TemplateArgsPtr,
624 TemplateId->getTemplateArgLocations(),
625 TemplateId->RAngleLoc,
626 Attr);
627 } else {
628 // This is an explicit specialization or a class template
629 // partial specialization.
630 TemplateParameterLists FakedParamLists;
631
632 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
633 // This looks like an explicit instantiation, because we have
634 // something like
635 //
636 // template class Foo<X>
637 //
Douglas Gregor96b6df92009-05-14 00:28:11 +0000638 // but it actually has a definition. Most likely, this was
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000639 // meant to be an explicit specialization, but the user forgot
640 // the '<>' after 'template'.
John McCall069c23a2009-07-31 02:45:11 +0000641 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000642
643 SourceLocation LAngleLoc
644 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
645 Diag(TemplateId->TemplateNameLoc,
646 diag::err_explicit_instantiation_with_definition)
647 << SourceRange(TemplateInfo.TemplateLoc)
648 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
649
650 // Create a fake template parameter list that contains only
651 // "template<>", so that we treat this construct as a class
652 // template specialization.
653 FakedParamLists.push_back(
654 Actions.ActOnTemplateParameterList(0, SourceLocation(),
655 TemplateInfo.TemplateLoc,
656 LAngleLoc,
657 0, 0,
658 LAngleLoc));
659 TemplateParams = &FakedParamLists;
660 }
661
662 // Build the class template specialization.
663 TagOrTempResult
John McCall069c23a2009-07-31 02:45:11 +0000664 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor0c281a82009-02-25 19:37:18 +0000665 StartLoc, SS,
Douglas Gregordd13e842009-03-30 22:58:21 +0000666 TemplateTy::make(TemplateId->Template),
Douglas Gregor0c281a82009-02-25 19:37:18 +0000667 TemplateId->TemplateNameLoc,
668 TemplateId->LAngleLoc,
669 TemplateArgsPtr,
670 TemplateId->getTemplateArgLocations(),
671 TemplateId->RAngleLoc,
672 Attr,
Douglas Gregora08b6c72009-02-17 23:15:12 +0000673 Action::MultiTemplateParamsArg(Actions,
674 TemplateParams? &(*TemplateParams)[0] : 0,
675 TemplateParams? TemplateParams->size() : 0));
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000676 }
Douglas Gregor0c281a82009-02-25 19:37:18 +0000677 TemplateId->Destroy();
Douglas Gregor96b6df92009-05-14 00:28:11 +0000678 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall069c23a2009-07-31 02:45:11 +0000679 TUK == Action::TUK_Declaration) {
Douglas Gregor96b6df92009-05-14 00:28:11 +0000680 // Explicit instantiation of a member of a class template
681 // specialization, e.g.,
682 //
683 // template struct Outer<int>::Inner;
684 //
685 TagOrTempResult
686 = Actions.ActOnExplicitInstantiation(CurScope,
687 TemplateInfo.TemplateLoc,
688 TagType, StartLoc, SS, Name,
689 NameLoc, Attr);
690 } else {
691 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall069c23a2009-07-31 02:45:11 +0000692 TUK == Action::TUK_Definition) {
Douglas Gregor96b6df92009-05-14 00:28:11 +0000693 // FIXME: Diagnose this particular error.
694 }
695
696 // Declaration or definition of a class type
John McCall069c23a2009-07-31 02:45:11 +0000697 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Douglas Gregor84a20812009-07-22 23:48:44 +0000698 Name, NameLoc, Attr, AS,
699 Action::MultiTemplateParamsArg(Actions,
700 TemplateParams? &(*TemplateParams)[0] : 0,
701 TemplateParams? TemplateParams->size() : 0),
702 Owned);
Douglas Gregor96b6df92009-05-14 00:28:11 +0000703 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000704
705 // Parse the optional base clause (C++ only).
Chris Lattner31ccf0a2009-02-16 22:07:16 +0000706 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000707 ParseBaseClause(TagOrTempResult.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000708
709 // If there is a body, parse it and inform the actions module.
710 if (Tok.is(tok::l_brace))
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000711 if (getLang().CPlusPlus)
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000712 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000713 else
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000714 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
John McCall069c23a2009-07-31 02:45:11 +0000715 else if (TUK == Action::TUK_Definition) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000716 // FIXME: Complain that we have a base-specifier list but no
717 // definition.
Chris Lattnerf006a222008-11-18 07:48:38 +0000718 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregorec93f442008-04-13 21:30:24 +0000719 }
720
Anders Carlssonef3fa4f2009-05-11 22:27:47 +0000721 if (TagOrTempResult.isInvalid()) {
Douglas Gregord406b032009-02-06 22:42:48 +0000722 DS.SetTypeSpecError();
Anders Carlssonef3fa4f2009-05-11 22:27:47 +0000723 return;
724 }
725
John McCall9f6e0972009-08-03 20:12:06 +0000726 const char *PrevSpec = 0;
727 unsigned DiagID;
728 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID,
Douglas Gregor71f06032009-05-28 23:31:59 +0000729 TagOrTempResult.get().getAs<void>(), Owned))
John McCall9f6e0972009-08-03 20:12:06 +0000730 Diag(StartLoc, DiagID) << PrevSpec;
Douglas Gregorec93f442008-04-13 21:30:24 +0000731}
732
733/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
734///
735/// base-clause : [C++ class.derived]
736/// ':' base-specifier-list
737/// base-specifier-list:
738/// base-specifier '...'[opt]
739/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattner5261d0c2009-03-28 19:18:32 +0000740void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000741 assert(Tok.is(tok::colon) && "Not a base clause");
742 ConsumeToken();
743
Douglas Gregorabed2172008-10-22 17:49:05 +0000744 // Build up an array of parsed base specifiers.
745 llvm::SmallVector<BaseTy *, 8> BaseInfo;
746
Douglas Gregorec93f442008-04-13 21:30:24 +0000747 while (true) {
748 // Parse a base-specifier.
Douglas Gregorabed2172008-10-22 17:49:05 +0000749 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000750 if (Result.isInvalid()) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000751 // Skip the rest of this base specifier, up until the comma or
752 // opening brace.
Douglas Gregorabed2172008-10-22 17:49:05 +0000753 SkipUntil(tok::comma, tok::l_brace, true, true);
754 } else {
755 // Add this to our array of base specifiers.
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000756 BaseInfo.push_back(Result.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000757 }
758
759 // If the next token is a comma, consume it and keep reading
760 // base-specifiers.
761 if (Tok.isNot(tok::comma)) break;
762
763 // Consume the comma.
764 ConsumeToken();
765 }
Douglas Gregorabed2172008-10-22 17:49:05 +0000766
767 // Attach the base specifiers
Jay Foad9e6bef42009-05-21 09:52:38 +0000768 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregorec93f442008-04-13 21:30:24 +0000769}
770
771/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
772/// one entry in the base class list of a class specifier, for example:
773/// class foo : public bar, virtual private baz {
774/// 'public bar' and 'virtual private baz' are each base-specifiers.
775///
776/// base-specifier: [C++ class.derived]
777/// ::[opt] nested-name-specifier[opt] class-name
778/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
779/// class-name
780/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
781/// class-name
Chris Lattner5261d0c2009-03-28 19:18:32 +0000782Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000783 bool IsVirtual = false;
784 SourceLocation StartLoc = Tok.getLocation();
785
786 // Parse the 'virtual' keyword.
787 if (Tok.is(tok::kw_virtual)) {
788 ConsumeToken();
789 IsVirtual = true;
790 }
791
792 // Parse an (optional) access specifier.
793 AccessSpecifier Access = getAccessSpecifierIfPresent();
794 if (Access)
795 ConsumeToken();
796
797 // Parse the 'virtual' keyword (again!), in case it came after the
798 // access specifier.
799 if (Tok.is(tok::kw_virtual)) {
800 SourceLocation VirtualLoc = ConsumeToken();
801 if (IsVirtual) {
802 // Complain about duplicate 'virtual'
Chris Lattnerf006a222008-11-18 07:48:38 +0000803 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregord7cb0372009-04-01 21:51:26 +0000804 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregorec93f442008-04-13 21:30:24 +0000805 }
806
807 IsVirtual = true;
808 }
809
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000810 // Parse optional '::' and optional nested-name-specifier.
811 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +0000812 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregorec93f442008-04-13 21:30:24 +0000813
Douglas Gregorec93f442008-04-13 21:30:24 +0000814 // The location of the base class itself.
815 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000816
817 // Parse the class-name.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000818 SourceLocation EndLocation;
Douglas Gregord7cb0372009-04-01 21:51:26 +0000819 TypeResult BaseType = ParseClassName(EndLocation, &SS);
820 if (BaseType.isInvalid())
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000821 return true;
Douglas Gregorec93f442008-04-13 21:30:24 +0000822
823 // Find the complete source range for the base-specifier.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000824 SourceRange Range(StartLoc, EndLocation);
Douglas Gregorec93f442008-04-13 21:30:24 +0000825
Douglas Gregorec93f442008-04-13 21:30:24 +0000826 // Notify semantic analysis that we have parsed a complete
827 // base-specifier.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000828 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregord7cb0372009-04-01 21:51:26 +0000829 BaseType.get(), BaseLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000830}
831
832/// getAccessSpecifierIfPresent - Determine whether the next token is
833/// a C++ access-specifier.
834///
835/// access-specifier: [C++ class.derived]
836/// 'private'
837/// 'protected'
838/// 'public'
Douglas Gregor696be932008-04-14 00:13:42 +0000839AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregorec93f442008-04-13 21:30:24 +0000840{
841 switch (Tok.getKind()) {
842 default: return AS_none;
843 case tok::kw_private: return AS_private;
844 case tok::kw_protected: return AS_protected;
845 case tok::kw_public: return AS_public;
846 }
847}
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000848
Eli Friedman40035e22009-07-22 21:45:50 +0000849void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
850 DeclPtrTy ThisDecl) {
851 // We just declared a member function. If this member function
852 // has any default arguments, we'll need to parse them later.
853 LateParsedMethodDeclaration *LateMethod = 0;
854 DeclaratorChunk::FunctionTypeInfo &FTI
855 = DeclaratorInfo.getTypeObject(0).Fun;
856 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
857 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
858 if (!LateMethod) {
859 // Push this method onto the stack of late-parsed method
860 // declarations.
861 getCurrentClass().MethodDecls.push_back(
862 LateParsedMethodDeclaration(ThisDecl));
863 LateMethod = &getCurrentClass().MethodDecls.back();
864
865 // Add all of the parameters prior to this one (they don't
866 // have default arguments).
867 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
868 for (unsigned I = 0; I < ParamIdx; ++I)
869 LateMethod->DefaultArgs.push_back(
870 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
871 }
872
873 // Add this parameter to the list of parameters (it or may
874 // not have a default argument).
875 LateMethod->DefaultArgs.push_back(
876 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
877 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
878 }
879 }
880}
881
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000882/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
883///
884/// member-declaration:
885/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
886/// function-definition ';'[opt]
887/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
888/// using-declaration [TODO]
Anders Carlssonab041982009-03-11 16:27:10 +0000889/// [C++0x] static_assert-declaration
Anders Carlssoned20fb92009-03-26 00:52:18 +0000890/// template-declaration
Chris Lattnerf3375de2008-12-18 01:12:00 +0000891/// [GNU] '__extension__' member-declaration
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000892///
893/// member-declarator-list:
894/// member-declarator
895/// member-declarator-list ',' member-declarator
896///
897/// member-declarator:
898/// declarator pure-specifier[opt]
899/// declarator constant-initializer[opt]
900/// identifier[opt] ':' constant-expression
901///
Sebastian Redla55834a2009-04-12 17:16:29 +0000902/// pure-specifier:
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000903/// '= 0'
904///
905/// constant-initializer:
906/// '=' constant-expression
907///
Douglas Gregor398a8012009-08-20 22:52:58 +0000908void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
909 const ParsedTemplateInfo &TemplateInfo) {
Anders Carlssonab041982009-03-11 16:27:10 +0000910 // static_assert-declaration
Chris Lattnera17991f2009-03-29 16:50:03 +0000911 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor398a8012009-08-20 22:52:58 +0000912 // FIXME: Check for templates
Chris Lattner9802a0a2009-04-02 04:16:50 +0000913 SourceLocation DeclEnd;
914 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000915 return;
916 }
Anders Carlssonab041982009-03-11 16:27:10 +0000917
Chris Lattnera17991f2009-03-29 16:50:03 +0000918 if (Tok.is(tok::kw_template)) {
Douglas Gregor398a8012009-08-20 22:52:58 +0000919 assert(!TemplateInfo.TemplateParams &&
920 "Nested template improperly parsed?");
Chris Lattner9802a0a2009-04-02 04:16:50 +0000921 SourceLocation DeclEnd;
Douglas Gregora9db0fa2009-05-12 23:25:50 +0000922 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
923 AS);
Chris Lattnera17991f2009-03-29 16:50:03 +0000924 return;
925 }
Anders Carlssoned20fb92009-03-26 00:52:18 +0000926
Chris Lattnerf3375de2008-12-18 01:12:00 +0000927 // Handle: member-declaration ::= '__extension__' member-declaration
928 if (Tok.is(tok::kw___extension__)) {
929 // __extension__ silences extension warnings in the subexpression.
930 ExtensionRAIIObject O(Diags); // Use RAII to do this.
931 ConsumeToken();
Douglas Gregor398a8012009-08-20 22:52:58 +0000932 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerf3375de2008-12-18 01:12:00 +0000933 }
Douglas Gregor683a1142009-06-20 00:51:54 +0000934
935 if (Tok.is(tok::kw_using)) {
Douglas Gregor398a8012009-08-20 22:52:58 +0000936 // FIXME: Check for template aliases
937
Douglas Gregor683a1142009-06-20 00:51:54 +0000938 // 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 Gregor398a8012009-08-20 22:52:58 +0000957 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000958
959 if (Tok.is(tok::semi)) {
960 ConsumeToken();
John McCall140607b2009-08-06 02:15:43 +0000961
Douglas Gregor398a8012009-08-20 22:52:58 +0000962 // FIXME: Friend templates?
John McCall140607b2009-08-06 02:15:43 +0000963 if (DS.isFriendSpecified())
John McCall36493082009-08-11 06:59:38 +0000964 Actions.ActOnFriendDecl(CurScope, &DS, /*IsDefinition*/ false);
John McCall140607b2009-08-06 02:15:43 +0000965 else
Chris Lattnera17991f2009-03-29 16:50:03 +0000966 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall140607b2009-08-06 02:15:43 +0000967
968 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000969 }
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000970
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000971 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000972
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000973 if (Tok.isNot(tok::colon)) {
974 // Parse the first declarator.
975 ParseDeclarator(DeclaratorInfo);
976 // Error parsing the declarator?
Douglas Gregor6704b312008-11-17 22:58:34 +0000977 if (!DeclaratorInfo.hasName()) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000978 // If so, skip until the semi-colon or a }.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000979 SkipUntil(tok::r_brace, true);
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000980 if (Tok.is(tok::semi))
981 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000982 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000983 }
984
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000985 // function-definition:
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000986 if (Tok.is(tok::l_brace)
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000987 || (DeclaratorInfo.isFunctionDeclarator() &&
988 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000989 if (!DeclaratorInfo.isFunctionDeclarator()) {
990 Diag(Tok, diag::err_func_def_no_params);
991 ConsumeBrace();
992 SkipUntil(tok::r_brace, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000993 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000994 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000995
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000996 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
997 Diag(Tok, diag::err_function_declared_typedef);
998 // This recovery skips the entire function body. It would be nice
999 // to simply call ParseCXXInlineMethodDef() below, however Sema
1000 // assumes the declarator represents a function, not a typedef.
1001 ConsumeBrace();
1002 SkipUntil(tok::r_brace, true);
Chris Lattnera17991f2009-03-29 16:50:03 +00001003 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001004 }
1005
Douglas Gregor398a8012009-08-20 22:52:58 +00001006 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattnera17991f2009-03-29 16:50:03 +00001007 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001008 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001009 }
1010
1011 // member-declarator-list:
1012 // member-declarator
1013 // member-declarator-list ',' member-declarator
1014
Chris Lattnera17991f2009-03-29 16:50:03 +00001015 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl62261042008-12-09 20:22:58 +00001016 OwningExprResult BitfieldSize(Actions);
1017 OwningExprResult Init(Actions);
Sebastian Redla55834a2009-04-12 17:16:29 +00001018 bool Deleted = false;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001019
1020 while (1) {
1021
1022 // member-declarator:
1023 // declarator pure-specifier[opt]
1024 // declarator constant-initializer[opt]
1025 // identifier[opt] ':' constant-expression
1026
1027 if (Tok.is(tok::colon)) {
1028 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001029 BitfieldSize = ParseConstantExpression();
1030 if (BitfieldSize.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001031 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001032 }
1033
1034 // pure-specifier:
1035 // '= 0'
1036 //
1037 // constant-initializer:
1038 // '=' constant-expression
Sebastian Redla55834a2009-04-12 17:16:29 +00001039 //
1040 // defaulted/deleted function-definition:
1041 // '=' 'default' [TODO]
1042 // '=' 'delete'
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001043
1044 if (Tok.is(tok::equal)) {
1045 ConsumeToken();
Sebastian Redla55834a2009-04-12 17:16:29 +00001046 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1047 ConsumeToken();
1048 Deleted = true;
1049 } else {
1050 Init = ParseInitializer();
1051 if (Init.isInvalid())
1052 SkipUntil(tok::comma, true, true);
1053 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001054 }
1055
1056 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +00001057 if (Tok.is(tok::kw___attribute)) {
1058 SourceLocation Loc;
1059 AttributeList *AttrList = ParseAttributes(&Loc);
1060 DeclaratorInfo.AddAttributes(AttrList, Loc);
1061 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001062
Argiris Kirtzidis38f16712008-07-01 10:37:29 +00001063 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattnera17991f2009-03-29 16:50:03 +00001064 // this call will *not* return the created decl; It will return null.
Argiris Kirtzidis38f16712008-07-01 10:37:29 +00001065 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall140607b2009-08-06 02:15:43 +00001066
1067 DeclPtrTy ThisDecl;
1068 if (DS.isFriendSpecified()) {
Douglas Gregor398a8012009-08-20 22:52:58 +00001069 // TODO: handle initializers, bitfields, 'delete', friend templates
John McCall36493082009-08-11 06:59:38 +00001070 ThisDecl = Actions.ActOnFriendDecl(CurScope, &DeclaratorInfo,
1071 /*IsDefinition*/ false);
Douglas Gregor398a8012009-08-20 22:52:58 +00001072 } else {
1073 Action::MultiTemplateParamsArg TemplateParams(Actions,
1074 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1075 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
John McCall140607b2009-08-06 02:15:43 +00001076 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1077 DeclaratorInfo,
Douglas Gregor398a8012009-08-20 22:52:58 +00001078 move(TemplateParams),
John McCall140607b2009-08-06 02:15:43 +00001079 BitfieldSize.release(),
1080 Init.release(),
1081 Deleted);
Douglas Gregor398a8012009-08-20 22:52:58 +00001082 }
Chris Lattnera17991f2009-03-29 16:50:03 +00001083 if (ThisDecl)
1084 DeclsInGroup.push_back(ThisDecl);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001085
Douglas Gregor605de8d2008-12-16 21:30:33 +00001086 if (DeclaratorInfo.isFunctionDeclarator() &&
1087 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
1088 != DeclSpec::SCS_typedef) {
Eli Friedman40035e22009-07-22 21:45:50 +00001089 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor605de8d2008-12-16 21:30:33 +00001090 }
1091
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001092 // If we don't have a comma, it is either the end of the list (a ';')
1093 // or an error, bail out.
1094 if (Tok.isNot(tok::comma))
1095 break;
1096
1097 // Consume the comma.
1098 ConsumeToken();
1099
1100 // Parse the next declarator.
1101 DeclaratorInfo.clear();
Sebastian Redl62261042008-12-09 20:22:58 +00001102 BitfieldSize = 0;
1103 Init = 0;
Sebastian Redla55834a2009-04-12 17:16:29 +00001104 Deleted = false;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001105
1106 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +00001107 if (Tok.is(tok::kw___attribute)) {
1108 SourceLocation Loc;
1109 AttributeList *AttrList = ParseAttributes(&Loc);
1110 DeclaratorInfo.AddAttributes(AttrList, Loc);
1111 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001112
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +00001113 if (Tok.isNot(tok::colon))
1114 ParseDeclarator(DeclaratorInfo);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001115 }
1116
1117 if (Tok.is(tok::semi)) {
1118 ConsumeToken();
Eli Friedman4d57af22009-05-29 01:49:24 +00001119 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattnera17991f2009-03-29 16:50:03 +00001120 DeclsInGroup.size());
1121 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001122 }
1123
1124 Diag(Tok, diag::err_expected_semi_decl_list);
1125 // Skip to end of block or statement
1126 SkipUntil(tok::r_brace, true, true);
1127 if (Tok.is(tok::semi))
1128 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +00001129 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001130}
1131
1132/// ParseCXXMemberSpecification - Parse the class definition.
1133///
1134/// member-specification:
1135/// member-declaration member-specification[opt]
1136/// access-specifier ':' member-specification[opt]
1137///
1138void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +00001139 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Guptafa451432008-10-31 09:52:39 +00001140 assert((TagType == DeclSpec::TST_struct ||
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001141 TagType == DeclSpec::TST_union ||
Sanjiv Guptafa451432008-10-31 09:52:39 +00001142 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001143
Chris Lattnerc309ade2009-03-05 08:00:35 +00001144 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1145 PP.getSourceManager(),
1146 "parsing struct/union/class body");
Chris Lattner7efd75e2009-03-05 02:25:03 +00001147
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001148 SourceLocation LBraceLoc = ConsumeBrace();
1149
Douglas Gregora376cbd2009-05-27 23:11:45 +00001150 // Determine whether this is a top-level (non-nested) class.
1151 bool TopLevelClass = ClassStack.empty() ||
1152 CurScope->isInCXXInlineMethodScope();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001153
1154 // Enter a scope for the class.
Douglas Gregorcab994d2009-01-09 22:42:13 +00001155 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001156
Douglas Gregora376cbd2009-05-27 23:11:45 +00001157 // Note that we are parsing a new (potentially-nested) class definition.
1158 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1159
Douglas Gregord406b032009-02-06 22:42:48 +00001160 if (TagDecl)
1161 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1162 else {
1163 SkipUntil(tok::r_brace, false, false);
1164 return;
1165 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001166
1167 // C++ 11p3: Members of a class defined with the keyword class are private
1168 // by default. Members of a class defined with the keywords struct or union
1169 // are public by default.
1170 AccessSpecifier CurAS;
1171 if (TagType == DeclSpec::TST_class)
1172 CurAS = AS_private;
1173 else
1174 CurAS = AS_public;
1175
1176 // While we still have something to read, read the member-declarations.
1177 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1178 // Each iteration of this loop reads one member-declaration.
1179
1180 // Check for extraneous top-level semicolon.
1181 if (Tok.is(tok::semi)) {
1182 Diag(Tok, diag::ext_extra_struct_semi);
1183 ConsumeToken();
1184 continue;
1185 }
1186
1187 AccessSpecifier AS = getAccessSpecifierIfPresent();
1188 if (AS != AS_none) {
1189 // Current token is a C++ access specifier.
1190 CurAS = AS;
1191 ConsumeToken();
1192 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1193 continue;
1194 }
1195
Douglas Gregor398a8012009-08-20 22:52:58 +00001196 // FIXME: Make sure we don't have a template here.
1197
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001198 // Parse all the comma separated declarators.
1199 ParseCXXClassMemberDeclaration(CurAS);
1200 }
1201
1202 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1203
1204 AttributeList *AttrList = 0;
1205 // If attributes exist after class contents, parse them.
1206 if (Tok.is(tok::kw___attribute))
1207 AttrList = ParseAttributes(); // FIXME: where should I put them?
1208
1209 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1210 LBraceLoc, RBraceLoc);
1211
1212 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1213 // complete within function bodies, default arguments,
1214 // exception-specifications, and constructor ctor-initializers (including
1215 // such things in nested classes).
1216 //
Douglas Gregor605de8d2008-12-16 21:30:33 +00001217 // FIXME: Only function bodies and constructor ctor-initializers are
1218 // parsed correctly, fix the rest.
Douglas Gregora376cbd2009-05-27 23:11:45 +00001219 if (TopLevelClass) {
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001220 // We are not inside a nested class. This class and its nested classes
Douglas Gregor605de8d2008-12-16 21:30:33 +00001221 // are complete and we can parse the delayed portions of method
1222 // declarations and the lexed inline method definitions.
Douglas Gregora376cbd2009-05-27 23:11:45 +00001223 ParseLexedMethodDeclarations(getCurrentClass());
1224 ParseLexedMethodDefs(getCurrentClass());
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001225 }
1226
1227 // Leave the class scope.
Douglas Gregora376cbd2009-05-27 23:11:45 +00001228 ParsingDef.Pop();
Douglas Gregor95d40792008-12-10 06:34:36 +00001229 ClassScope.Exit();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001230
Argiris Kirtzidiseb925642009-07-14 03:17:52 +00001231 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001232}
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001233
1234/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1235/// which explicitly initializes the members or base classes of a
1236/// class (C++ [class.base.init]). For example, the three initializers
1237/// after the ':' in the Derived constructor below:
1238///
1239/// @code
1240/// class Base { };
1241/// class Derived : Base {
1242/// int x;
1243/// float f;
1244/// public:
1245/// Derived(float f) : Base(), x(17), f(f) { }
1246/// };
1247/// @endcode
1248///
1249/// [C++] ctor-initializer:
1250/// ':' mem-initializer-list
1251///
1252/// [C++] mem-initializer-list:
1253/// mem-initializer
1254/// mem-initializer , mem-initializer-list
Chris Lattner5261d0c2009-03-28 19:18:32 +00001255void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001256 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1257
1258 SourceLocation ColonLoc = ConsumeToken();
1259
1260 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
1261
1262 do {
1263 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +00001264 if (!MemInit.isInvalid())
1265 MemInitializers.push_back(MemInit.get());
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001266
1267 if (Tok.is(tok::comma))
1268 ConsumeToken();
1269 else if (Tok.is(tok::l_brace))
1270 break;
1271 else {
1272 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redlbc9ef252009-04-26 20:35:05 +00001273 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001274 SkipUntil(tok::l_brace, true, true);
1275 break;
1276 }
1277 } while (true);
1278
1279 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foad9e6bef42009-05-21 09:52:38 +00001280 MemInitializers.data(), MemInitializers.size());
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001281}
1282
1283/// ParseMemInitializer - Parse a C++ member initializer, which is
1284/// part of a constructor initializer that explicitly initializes one
1285/// member or base class (C++ [class.base.init]). See
1286/// ParseConstructorInitializer for an example.
1287///
1288/// [C++] mem-initializer:
1289/// mem-initializer-id '(' expression-list[opt] ')'
1290///
1291/// [C++] mem-initializer-id:
1292/// '::'[opt] nested-name-specifier[opt] class-name
1293/// identifier
Chris Lattner5261d0c2009-03-28 19:18:32 +00001294Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianc37d3062009-06-30 23:26:25 +00001295 // parse '::'[opt] nested-name-specifier[opt]
1296 CXXScopeSpec SS;
1297 ParseOptionalCXXScopeSpecifier(SS);
Fariborz Jahanian2b2b7362009-07-01 19:21:19 +00001298 TypeTy *TemplateTypeTy = 0;
1299 if (Tok.is(tok::annot_template_id)) {
1300 TemplateIdAnnotation *TemplateId
1301 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1302 if (TemplateId->Kind == TNK_Type_template) {
1303 AnnotateTemplateIdTokenAsType(&SS);
1304 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1305 TemplateTypeTy = Tok.getAnnotationValue();
1306 }
1307 // FIXME. May need to check for TNK_Dependent_template as well.
1308 }
1309 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001310 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001311 return true;
1312 }
Fariborz Jahanian2b2b7362009-07-01 19:21:19 +00001313
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001314 // Get the identifier. This may be a member name or a class name,
1315 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian2b2b7362009-07-01 19:21:19 +00001316 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001317 SourceLocation IdLoc = ConsumeToken();
1318
1319 // Parse the '('.
1320 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001321 Diag(Tok, diag::err_expected_lparen);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001322 return true;
1323 }
1324 SourceLocation LParenLoc = ConsumeParen();
1325
1326 // Parse the optional expression-list.
Sebastian Redl6008ac32008-11-25 22:21:31 +00001327 ExprVector ArgExprs(Actions);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001328 CommaLocsTy CommaLocs;
1329 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1330 SkipUntil(tok::r_paren);
1331 return true;
1332 }
1333
1334 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1335
Fariborz Jahanian2b2b7362009-07-01 19:21:19 +00001336 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1337 TemplateTypeTy, IdLoc,
Sebastian Redl6008ac32008-11-25 22:21:31 +00001338 LParenLoc, ArgExprs.take(),
Jay Foad9e6bef42009-05-21 09:52:38 +00001339 ArgExprs.size(), CommaLocs.data(),
1340 RParenLoc);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001341}
Douglas Gregor90a2c972008-11-25 03:22:00 +00001342
1343/// ParseExceptionSpecification - Parse a C++ exception-specification
1344/// (C++ [except.spec]).
1345///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001346/// exception-specification:
1347/// 'throw' '(' type-id-list [opt] ')'
1348/// [MS] 'throw' '(' '...' ')'
Douglas Gregor90a2c972008-11-25 03:22:00 +00001349///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001350/// type-id-list:
1351/// type-id
1352/// type-id-list ',' type-id
Douglas Gregor90a2c972008-11-25 03:22:00 +00001353///
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001354bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlaaacda92009-05-29 18:02:33 +00001355 llvm::SmallVector<TypeTy*, 2>
1356 &Exceptions,
1357 llvm::SmallVector<SourceRange, 2>
1358 &Ranges,
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001359 bool &hasAnyExceptionSpec) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00001360 assert(Tok.is(tok::kw_throw) && "expected throw");
1361
1362 SourceLocation ThrowLoc = ConsumeToken();
1363
1364 if (!Tok.is(tok::l_paren)) {
1365 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1366 }
1367 SourceLocation LParenLoc = ConsumeParen();
1368
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001369 // Parse throw(...), a Microsoft extension that means "this function
1370 // can throw anything".
1371 if (Tok.is(tok::ellipsis)) {
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001372 hasAnyExceptionSpec = true;
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001373 SourceLocation EllipsisLoc = ConsumeToken();
1374 if (!getLang().Microsoft)
1375 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redl0c986032009-02-09 18:23:29 +00001376 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001377 return false;
1378 }
1379
Douglas Gregor90a2c972008-11-25 03:22:00 +00001380 // Parse the sequence of type-ids.
Sebastian Redlaaacda92009-05-29 18:02:33 +00001381 SourceRange Range;
Douglas Gregor90a2c972008-11-25 03:22:00 +00001382 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlaaacda92009-05-29 18:02:33 +00001383 TypeResult Res(ParseTypeName(&Range));
1384 if (!Res.isInvalid()) {
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001385 Exceptions.push_back(Res.get());
Sebastian Redlaaacda92009-05-29 18:02:33 +00001386 Ranges.push_back(Range);
1387 }
Douglas Gregor90a2c972008-11-25 03:22:00 +00001388 if (Tok.is(tok::comma))
1389 ConsumeToken();
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001390 else
Douglas Gregor90a2c972008-11-25 03:22:00 +00001391 break;
1392 }
1393
Sebastian Redl0c986032009-02-09 18:23:29 +00001394 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001395 return false;
1396}
Douglas Gregora376cbd2009-05-27 23:11:45 +00001397
1398/// \brief We have just started parsing the definition of a new class,
1399/// so push that class onto our stack of classes that is currently
1400/// being parsed.
1401void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
1402 assert((TopLevelClass || !ClassStack.empty()) &&
1403 "Nested class without outer class");
1404 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1405}
1406
1407/// \brief Deallocate the given parsed class and all of its nested
1408/// classes.
1409void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1410 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1411 DeallocateParsedClasses(Class->NestedClasses[I]);
1412 delete Class;
1413}
1414
1415/// \brief Pop the top class of the stack of classes that are
1416/// currently being parsed.
1417///
1418/// This routine should be called when we have finished parsing the
1419/// definition of a class, but have not yet popped the Scope
1420/// associated with the class's definition.
1421///
1422/// \returns true if the class we've popped is a top-level class,
1423/// false otherwise.
1424void Parser::PopParsingClass() {
1425 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
1426
1427 ParsingClass *Victim = ClassStack.top();
1428 ClassStack.pop();
1429 if (Victim->TopLevelClass) {
1430 // Deallocate all of the nested classes of this class,
1431 // recursively: we don't need to keep any of this information.
1432 DeallocateParsedClasses(Victim);
1433 return;
1434 }
1435 assert(!ClassStack.empty() && "Missing top-level class?");
1436
1437 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1438 Victim->NestedClasses.empty()) {
1439 // The victim is a nested class, but we will not need to perform
1440 // any processing after the definition of this class since it has
1441 // no members whose handling was delayed. Therefore, we can just
1442 // remove this nested class.
1443 delete Victim;
1444 return;
1445 }
1446
1447 // This nested class has some members that will need to be processed
1448 // after the top-level class is completely defined. Therefore, add
1449 // it to the list of nested classes within its parent.
1450 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1451 ClassStack.top()->NestedClasses.push_back(Victim);
1452 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1453}