blob: 188580de20a1a905dcd23a6ff184b21a8cdbde25 [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor1b7f8982008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregore37ac4f2008-04-13 21:30:24 +000016#include "clang/Parse/DeclSpec.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000017#include "clang/Parse/Scope.h"
Chris Lattnerbc8d5642008-12-18 01:12:00 +000018#include "ExtensionRAIIObject.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000019using namespace clang;
20
21/// ParseNamespace - We know that the current token is a namespace keyword. This
22/// may either be a top level namespace or a block-level namespace alias.
23///
24/// namespace-definition: [C++ 7.3: basic.namespace]
25/// named-namespace-definition
26/// unnamed-namespace-definition
27///
28/// unnamed-namespace-definition:
29/// 'namespace' attributes[opt] '{' namespace-body '}'
30///
31/// named-namespace-definition:
32/// original-namespace-definition
33/// extension-namespace-definition
34///
35/// original-namespace-definition:
36/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
37///
38/// extension-namespace-definition:
39/// 'namespace' original-namespace-name '{' namespace-body '}'
40///
41/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
42/// 'namespace' identifier '=' qualified-namespace-specifier ';'
43///
Chris Lattner97144fc2009-04-02 04:16:50 +000044Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
45 SourceLocation &DeclEnd) {
Chris Lattner04d66662007-10-09 17:33:22 +000046 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000047 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
48
49 SourceLocation IdentLoc;
50 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000051
52 Token attrTok;
Chris Lattner8f08cb72007-08-25 06:57:03 +000053
Chris Lattner04d66662007-10-09 17:33:22 +000054 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000055 Ident = Tok.getIdentifierInfo();
56 IdentLoc = ConsumeToken(); // eat the identifier.
57 }
58
59 // Read label attributes, if present.
Chris Lattnerb28317a2009-03-28 19:18:32 +000060 Action::AttrTy *AttrList = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000061 if (Tok.is(tok::kw___attribute)) {
62 attrTok = Tok;
63
Chris Lattner8f08cb72007-08-25 06:57:03 +000064 // FIXME: save these somewhere.
65 AttrList = ParseAttributes();
Douglas Gregor6a588dd2009-06-17 19:49:00 +000066 }
Chris Lattner8f08cb72007-08-25 06:57:03 +000067
Douglas Gregor6a588dd2009-06-17 19:49:00 +000068 if (Tok.is(tok::equal)) {
69 if (AttrList)
70 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
71
Chris Lattner97144fc2009-04-02 04:16:50 +000072 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000073 }
Anders Carlssonf67606a2009-03-28 04:07:16 +000074
Chris Lattner51448322009-03-29 14:02:43 +000075 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +000076 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000077 diag::err_expected_ident_lbrace);
78 return DeclPtrTy();
Chris Lattner8f08cb72007-08-25 06:57:03 +000079 }
80
Chris Lattner51448322009-03-29 14:02:43 +000081 SourceLocation LBrace = ConsumeBrace();
82
83 // Enter a scope for the namespace.
84 ParseScope NamespaceScope(this, Scope::DeclScope);
85
86 DeclPtrTy NamespcDecl =
87 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
88
89 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
90 PP.getSourceManager(),
91 "parsing namespace");
92
93 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
94 ParseExternalDeclaration();
95
96 // Leave the namespace scope.
97 NamespaceScope.Exit();
98
Chris Lattner97144fc2009-04-02 04:16:50 +000099 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
100 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000101
Chris Lattner97144fc2009-04-02 04:16:50 +0000102 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000103 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000104}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000105
Anders Carlssonf67606a2009-03-28 04:07:16 +0000106/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
107/// alias definition.
108///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000109Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
110 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000111 IdentifierInfo *Alias,
112 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000113 assert(Tok.is(tok::equal) && "Not equal token");
114
115 ConsumeToken(); // eat the '='.
116
117 CXXScopeSpec SS;
118 // Parse (optional) nested-name-specifier.
119 ParseOptionalCXXScopeSpecifier(SS);
120
121 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
122 Diag(Tok, diag::err_expected_namespace_name);
123 // Skip to end of the definition and eat the ';'.
124 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000125 return DeclPtrTy();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000126 }
127
128 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000129 IdentifierInfo *Ident = Tok.getIdentifierInfo();
130 SourceLocation IdentLoc = ConsumeToken();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000131
132 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000133 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000134 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
135 "", tok::semi);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000136
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000137 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
138 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000139}
140
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000141/// ParseLinkage - We know that the current token is a string_literal
142/// and just before that, that extern was seen.
143///
144/// linkage-specification: [C++ 7.5p2: dcl.link]
145/// 'extern' string-literal '{' declaration-seq[opt] '}'
146/// 'extern' string-literal declaration
147///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000148Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000149 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000150 llvm::SmallVector<char, 8> LangBuffer;
151 // LangBuffer is guaranteed to be big enough.
152 LangBuffer.resize(Tok.getLength());
153 const char *LangBufPtr = &LangBuffer[0];
154 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
155
156 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000157
Douglas Gregor074149e2009-01-05 19:45:36 +0000158 ParseScope LinkageScope(this, Scope::DeclScope);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000159 DeclPtrTy LinkageSpec
Douglas Gregor074149e2009-01-05 19:45:36 +0000160 = Actions.ActOnStartLinkageSpecification(CurScope,
161 /*FIXME: */SourceLocation(),
162 Loc, LangBufPtr, StrSize,
163 Tok.is(tok::l_brace)? Tok.getLocation()
164 : SourceLocation());
165
166 if (Tok.isNot(tok::l_brace)) {
167 ParseDeclarationOrFunctionDefinition();
168 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
169 SourceLocation());
Douglas Gregorf44515a2008-12-16 22:23:02 +0000170 }
171
172 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000173 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000174 ParseExternalDeclaration();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000175 }
176
Douglas Gregorf44515a2008-12-16 22:23:02 +0000177 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000178 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000179}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000180
Douglas Gregorf780abc2008-12-30 03:27:21 +0000181/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
182/// using-directive. Assumes that current token is 'using'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000183Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
184 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000185 assert(Tok.is(tok::kw_using) && "Not using token");
186
187 // Eat 'using'.
188 SourceLocation UsingLoc = ConsumeToken();
189
Chris Lattner2f274772009-01-06 06:55:51 +0000190 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000191 // Next token after 'using' is 'namespace' so it must be using-directive
Chris Lattner97144fc2009-04-02 04:16:50 +0000192 return ParseUsingDirective(Context, UsingLoc, DeclEnd);
Chris Lattner2f274772009-01-06 06:55:51 +0000193
194 // Otherwise, it must be using-declaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000195 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000196}
197
198/// ParseUsingDirective - Parse C++ using-directive, assumes
199/// that current token is 'namespace' and 'using' was already parsed.
200///
201/// using-directive: [C++ 7.3.p4: namespace.udir]
202/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
203/// namespace-name ;
204/// [GNU] using-directive:
205/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
206/// namespace-name attributes[opt] ;
207///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000208Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000209 SourceLocation UsingLoc,
210 SourceLocation &DeclEnd) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000211 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
212
213 // Eat 'namespace'.
214 SourceLocation NamespcLoc = ConsumeToken();
215
216 CXXScopeSpec SS;
217 // Parse (optional) nested-name-specifier.
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000218 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000219
220 AttributeList *AttrList = 0;
221 IdentifierInfo *NamespcName = 0;
222 SourceLocation IdentLoc = SourceLocation();
223
224 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000225 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000226 Diag(Tok, diag::err_expected_namespace_name);
227 // If there was invalid namespace name, skip to end of decl, and eat ';'.
228 SkipUntil(tok::semi);
229 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattnerb28317a2009-03-28 19:18:32 +0000230 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000231 }
Chris Lattner823c44e2009-01-06 07:27:21 +0000232
233 // Parse identifier.
234 NamespcName = Tok.getIdentifierInfo();
235 IdentLoc = ConsumeToken();
236
237 // Parse (optional) attributes (most likely GNU strong-using extension).
238 if (Tok.is(tok::kw___attribute))
239 AttrList = ParseAttributes();
240
241 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000242 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000243 ExpectAndConsume(tok::semi,
244 AttrList ? diag::err_expected_semi_after_attribute_list :
245 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000246
247 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner823c44e2009-01-06 07:27:21 +0000248 IdentLoc, NamespcName, AttrList);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000249}
250
251/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
252/// 'using' was already seen.
253///
254/// using-declaration: [C++ 7.3.p3: namespace.udecl]
255/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000256/// unqualified-id
257/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000258///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000259Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000260 SourceLocation UsingLoc,
261 SourceLocation &DeclEnd) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000262 CXXScopeSpec SS;
263 bool IsTypeName;
264
265 // Ignore optional 'typename'.
266 if (Tok.is(tok::kw_typename)) {
267 ConsumeToken();
268 IsTypeName = true;
269 }
270 else
271 IsTypeName = false;
272
273 // Parse nested-name-specifier.
274 ParseOptionalCXXScopeSpecifier(SS);
275
276 AttributeList *AttrList = 0;
277 IdentifierInfo *TargetName = 0;
278 SourceLocation IdentLoc = SourceLocation();
279
280 // Check nested-name specifier.
281 if (SS.isInvalid()) {
282 SkipUntil(tok::semi);
283 return DeclPtrTy();
284 }
285 if (Tok.is(tok::annot_template_id)) {
286 Diag(Tok, diag::err_unexpected_template_spec_in_using);
287 SkipUntil(tok::semi);
288 return DeclPtrTy();
289 }
290 if (Tok.isNot(tok::identifier)) {
291 Diag(Tok, diag::err_expected_ident_in_using);
292 // If there was invalid identifier, skip to end of decl, and eat ';'.
293 SkipUntil(tok::semi);
294 return DeclPtrTy();
295 }
296
297 // Parse identifier.
298 TargetName = Tok.getIdentifierInfo();
299 IdentLoc = ConsumeToken();
300
301 // Parse (optional) attributes (most likely GNU strong-using extension).
302 if (Tok.is(tok::kw___attribute))
303 AttrList = ParseAttributes();
304
305 // Eat ';'.
306 DeclEnd = Tok.getLocation();
307 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
308 AttrList ? "attributes list" : "namespace name", tok::semi);
309
310 return Actions.ActOnUsingDeclaration(CurScope, UsingLoc, SS,
311 IdentLoc, TargetName, AttrList, IsTypeName);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000312}
313
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000314/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
315///
316/// static_assert-declaration:
317/// static_assert ( constant-expression , string-literal ) ;
318///
Chris Lattner97144fc2009-04-02 04:16:50 +0000319Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000320 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
321 SourceLocation StaticAssertLoc = ConsumeToken();
322
323 if (Tok.isNot(tok::l_paren)) {
324 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000325 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000326 }
327
328 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000329
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000330 OwningExprResult AssertExpr(ParseConstantExpression());
331 if (AssertExpr.isInvalid()) {
332 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000333 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000334 }
335
Anders Carlssonad5f9602009-03-13 23:29:20 +0000336 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000337 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000338
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000339 if (Tok.isNot(tok::string_literal)) {
340 Diag(Tok, diag::err_expected_string_literal);
341 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000342 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000343 }
344
345 OwningExprResult AssertMessage(ParseStringLiteralExpression());
346 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000347 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000348
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000349 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000350
Chris Lattner97144fc2009-04-02 04:16:50 +0000351 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000352 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
353
Anders Carlssonad5f9602009-03-13 23:29:20 +0000354 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000355 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000356}
357
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000358/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
359///
360/// 'decltype' ( expression )
361///
362void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
363 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
364
365 SourceLocation StartLoc = ConsumeToken();
366 SourceLocation LParenLoc = Tok.getLocation();
367
368
369 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
370 "decltype")) {
371 SkipUntil(tok::r_paren);
372 return;
373 }
374
375
376 // Parse the expression
377
378 // C++0x [dcl.type.simple]p4:
379 // The operand of the decltype specifier is an unevaluated operand.
380 EnterExpressionEvaluationContext Unevaluated(Actions,
381 Action::Unevaluated);
382 OwningExprResult Result = ParseExpression();
383 if (Result.isInvalid()) {
384 SkipUntil(tok::r_paren);
385 return;
386 }
387
388 // Match the ')'
389 SourceLocation RParenLoc;
390 if (Tok.is(tok::r_paren))
391 RParenLoc = ConsumeParen();
392 else
393 MatchRHSPunctuation(tok::r_paren, LParenLoc);
394
395 if (RParenLoc.isInvalid())
396 return;
397
398 const char *PrevSpec = 0;
399 // Check for duplicate type specifiers (e.g. "int decltype(a)").
400 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
401 Result.release()))
402 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
403}
404
Douglas Gregor42a552f2008-11-05 20:51:48 +0000405/// ParseClassName - Parse a C++ class-name, which names a class. Note
406/// that we only check that the result names a type; semantic analysis
407/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000408/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000409/// found.
410///
411/// class-name: [C++ 9.1]
412/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000413/// simple-template-id
Douglas Gregor42a552f2008-11-05 20:51:48 +0000414///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000415Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
416 const CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000417 // Check whether we have a template-id that names a type.
418 if (Tok.is(tok::annot_template_id)) {
419 TemplateIdAnnotation *TemplateId
420 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000421 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000422 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000423
424 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
425 TypeTy *Type = Tok.getAnnotationValue();
426 EndLocation = Tok.getAnnotationEndLoc();
427 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000428
429 if (Type)
430 return Type;
431 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000432 }
433
434 // Fall through to produce an error below.
435 }
436
Douglas Gregor42a552f2008-11-05 20:51:48 +0000437 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000438 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000439 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000440 }
441
442 // We have an identifier; check whether it is actually a type.
Douglas Gregorb696ea32009-02-04 17:00:24 +0000443 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
444 Tok.getLocation(), CurScope, SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000445 if (!Type) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000446 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000447 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000448 }
449
450 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000451 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000452 return Type;
453}
454
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000455/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
456/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
457/// until we reach the start of a definition or see a token that
458/// cannot start a definition.
459///
460/// class-specifier: [C++ class]
461/// class-head '{' member-specification[opt] '}'
462/// class-head '{' member-specification[opt] '}' attributes[opt]
463/// class-head:
464/// class-key identifier[opt] base-clause[opt]
465/// class-key nested-name-specifier identifier base-clause[opt]
466/// class-key nested-name-specifier[opt] simple-template-id
467/// base-clause[opt]
468/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
469/// [GNU] class-key attributes[opt] nested-name-specifier
470/// identifier base-clause[opt]
471/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
472/// simple-template-id base-clause[opt]
473/// class-key:
474/// 'class'
475/// 'struct'
476/// 'union'
477///
478/// elaborated-type-specifier: [C++ dcl.type.elab]
479/// class-key ::[opt] nested-name-specifier[opt] identifier
480/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
481/// simple-template-id
482///
483/// Note that the C++ class-specifier and elaborated-type-specifier,
484/// together, subsume the C99 struct-or-union-specifier:
485///
486/// struct-or-union-specifier: [C99 6.7.2.1]
487/// struct-or-union identifier[opt] '{' struct-contents '}'
488/// struct-or-union identifier
489/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
490/// '}' attributes[opt]
491/// [GNU] struct-or-union attributes[opt] identifier
492/// struct-or-union:
493/// 'struct'
494/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000495void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
496 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000497 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000498 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000499 DeclSpec::TST TagType;
500 if (TagTokKind == tok::kw_struct)
501 TagType = DeclSpec::TST_struct;
502 else if (TagTokKind == tok::kw_class)
503 TagType = DeclSpec::TST_class;
504 else {
505 assert(TagTokKind == tok::kw_union && "Not a class specifier");
506 TagType = DeclSpec::TST_union;
507 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000508
509 AttributeList *Attr = 0;
510 // If attributes exist after tag, parse them.
511 if (Tok.is(tok::kw___attribute))
512 Attr = ParseAttributes();
513
Steve Narofff59e17e2008-12-24 20:59:21 +0000514 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000515 if (Tok.is(tok::kw___declspec))
516 Attr = ParseMicrosoftDeclSpec(Attr);
Steve Narofff59e17e2008-12-24 20:59:21 +0000517
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000518 // Parse the (optional) nested-name-specifier.
519 CXXScopeSpec SS;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000520 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS))
521 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000522 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000523
524 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000525 IdentifierInfo *Name = 0;
526 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000527 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000528 if (Tok.is(tok::identifier)) {
529 Name = Tok.getIdentifierInfo();
530 NameLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000531 } else if (Tok.is(tok::annot_template_id)) {
532 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
533 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000534
Douglas Gregorc45c2322009-03-31 00:43:58 +0000535 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000536 // The template-name in the simple-template-id refers to
537 // something other than a class template. Give an appropriate
538 // error message and skip to the ';'.
539 SourceRange Range(NameLoc);
540 if (SS.isNotEmpty())
541 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000542
Douglas Gregor39a8de12009-02-25 19:37:18 +0000543 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
544 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +0000545
Douglas Gregor39a8de12009-02-25 19:37:18 +0000546 DS.SetTypeSpecError();
547 SkipUntil(tok::semi, false, true);
548 TemplateId->Destroy();
549 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000550 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000551 }
552
553 // There are three options here. If we have 'struct foo;', then
554 // this is a forward declaration. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000555 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000556 // something like 'struct foo xyz', a reference.
557 Action::TagKind TK;
558 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
559 TK = Action::TK_Definition;
Anders Carlsson5dc2af12009-05-11 22:25:03 +0000560 else if (Tok.is(tok::semi) && !DS.isFriendSpecified())
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000561 TK = Action::TK_Declaration;
562 else
563 TK = Action::TK_Reference;
564
Douglas Gregor39a8de12009-02-25 19:37:18 +0000565 if (!Name && !TemplateId && TK != Action::TK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000566 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000567 Diag(StartLoc, diag::err_anon_type_definition)
568 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000569
570 // Skip the rest of this declarator, up until the comma or semicolon.
571 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000572
573 if (TemplateId)
574 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000575 return;
576 }
577
Douglas Gregorddc29e12009-02-06 22:42:48 +0000578 // Create the tag portion of the class or class template.
Douglas Gregor212e81c2009-03-25 00:13:59 +0000579 Action::DeclResult TagOrTempResult;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000580 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
581
582 // FIXME: When TK == TK_Reference and we have a template-id, we need
583 // to turn that template-id into a type.
584
Douglas Gregor402abb52009-05-28 23:31:59 +0000585 bool Owned = false;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000586 if (TemplateId && TK != Action::TK_Reference) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000587 // Explicit specialization, class template partial specialization,
588 // or explicit instantiation.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000589 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
590 TemplateId->getTemplateArgs(),
591 TemplateId->getTemplateArgIsType(),
592 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000593 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
594 TK == Action::TK_Declaration) {
595 // This is an explicit instantiation of a class template.
596 TagOrTempResult
597 = Actions.ActOnExplicitInstantiation(CurScope,
598 TemplateInfo.TemplateLoc,
599 TagType,
600 StartLoc,
601 SS,
602 TemplateTy::make(TemplateId->Template),
603 TemplateId->TemplateNameLoc,
604 TemplateId->LAngleLoc,
605 TemplateArgsPtr,
606 TemplateId->getTemplateArgLocations(),
607 TemplateId->RAngleLoc,
608 Attr);
609 } else {
610 // This is an explicit specialization or a class template
611 // partial specialization.
612 TemplateParameterLists FakedParamLists;
613
614 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
615 // This looks like an explicit instantiation, because we have
616 // something like
617 //
618 // template class Foo<X>
619 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000620 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000621 // meant to be an explicit specialization, but the user forgot
622 // the '<>' after 'template'.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000623 assert(TK == Action::TK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000624
625 SourceLocation LAngleLoc
626 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
627 Diag(TemplateId->TemplateNameLoc,
628 diag::err_explicit_instantiation_with_definition)
629 << SourceRange(TemplateInfo.TemplateLoc)
630 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
631
632 // Create a fake template parameter list that contains only
633 // "template<>", so that we treat this construct as a class
634 // template specialization.
635 FakedParamLists.push_back(
636 Actions.ActOnTemplateParameterList(0, SourceLocation(),
637 TemplateInfo.TemplateLoc,
638 LAngleLoc,
639 0, 0,
640 LAngleLoc));
641 TemplateParams = &FakedParamLists;
642 }
643
644 // Build the class template specialization.
645 TagOrTempResult
646 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000647 StartLoc, SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000648 TemplateTy::make(TemplateId->Template),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000649 TemplateId->TemplateNameLoc,
650 TemplateId->LAngleLoc,
651 TemplateArgsPtr,
652 TemplateId->getTemplateArgLocations(),
653 TemplateId->RAngleLoc,
654 Attr,
Douglas Gregorcc636682009-02-17 23:15:12 +0000655 Action::MultiTemplateParamsArg(Actions,
656 TemplateParams? &(*TemplateParams)[0] : 0,
657 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000658 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000659 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000660 } else if (TemplateParams && TK != Action::TK_Reference) {
661 // Class template declaration or definition.
Douglas Gregor212e81c2009-03-25 00:13:59 +0000662 TagOrTempResult = Actions.ActOnClassTemplate(CurScope, TagType, TK,
663 StartLoc, SS, Name, NameLoc,
664 Attr,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000665 Action::MultiTemplateParamsArg(Actions,
666 &(*TemplateParams)[0],
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000667 TemplateParams->size()),
668 AS);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000669 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
670 TK == Action::TK_Declaration) {
671 // Explicit instantiation of a member of a class template
672 // specialization, e.g.,
673 //
674 // template struct Outer<int>::Inner;
675 //
676 TagOrTempResult
677 = Actions.ActOnExplicitInstantiation(CurScope,
678 TemplateInfo.TemplateLoc,
679 TagType, StartLoc, SS, Name,
680 NameLoc, Attr);
681 } else {
682 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
683 TK == Action::TK_Definition) {
684 // FIXME: Diagnose this particular error.
685 }
686
687 // Declaration or definition of a class type
688 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS,
Douglas Gregor402abb52009-05-28 23:31:59 +0000689 Name, NameLoc, Attr, AS, Owned);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000690 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000691
692 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000693 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000694 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000695
696 // If there is a body, parse it and inform the actions module.
697 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000698 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000699 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000700 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000701 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000702 else if (TK == Action::TK_Definition) {
703 // FIXME: Complain that we have a base-specifier list but no
704 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000705 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000706 }
707
708 const char *PrevSpec = 0;
Anders Carlsson66e99772009-05-11 22:27:47 +0000709 if (TagOrTempResult.isInvalid()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000710 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000711 return;
712 }
713
Anders Carlsson66e99772009-05-11 22:27:47 +0000714 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec,
Douglas Gregor402abb52009-05-28 23:31:59 +0000715 TagOrTempResult.get().getAs<void>(), Owned))
Chris Lattner1ab3b962008-11-18 07:48:38 +0000716 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Anders Carlssond4f551b2009-05-11 22:42:30 +0000717
718 if (DS.isFriendSpecified())
719 Actions.ActOnFriendDecl(CurScope, DS.getFriendSpecLoc(),
720 TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000721}
722
723/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
724///
725/// base-clause : [C++ class.derived]
726/// ':' base-specifier-list
727/// base-specifier-list:
728/// base-specifier '...'[opt]
729/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000730void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000731 assert(Tok.is(tok::colon) && "Not a base clause");
732 ConsumeToken();
733
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000734 // Build up an array of parsed base specifiers.
735 llvm::SmallVector<BaseTy *, 8> BaseInfo;
736
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000737 while (true) {
738 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000739 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000740 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000741 // Skip the rest of this base specifier, up until the comma or
742 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000743 SkipUntil(tok::comma, tok::l_brace, true, true);
744 } else {
745 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000746 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000747 }
748
749 // If the next token is a comma, consume it and keep reading
750 // base-specifiers.
751 if (Tok.isNot(tok::comma)) break;
752
753 // Consume the comma.
754 ConsumeToken();
755 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000756
757 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000758 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000759}
760
761/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
762/// one entry in the base class list of a class specifier, for example:
763/// class foo : public bar, virtual private baz {
764/// 'public bar' and 'virtual private baz' are each base-specifiers.
765///
766/// base-specifier: [C++ class.derived]
767/// ::[opt] nested-name-specifier[opt] class-name
768/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
769/// class-name
770/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
771/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000772Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000773 bool IsVirtual = false;
774 SourceLocation StartLoc = Tok.getLocation();
775
776 // Parse the 'virtual' keyword.
777 if (Tok.is(tok::kw_virtual)) {
778 ConsumeToken();
779 IsVirtual = true;
780 }
781
782 // Parse an (optional) access specifier.
783 AccessSpecifier Access = getAccessSpecifierIfPresent();
784 if (Access)
785 ConsumeToken();
786
787 // Parse the 'virtual' keyword (again!), in case it came after the
788 // access specifier.
789 if (Tok.is(tok::kw_virtual)) {
790 SourceLocation VirtualLoc = ConsumeToken();
791 if (IsVirtual) {
792 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000793 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000794 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000795 }
796
797 IsVirtual = true;
798 }
799
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000800 // Parse optional '::' and optional nested-name-specifier.
801 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000802 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000803
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000804 // The location of the base class itself.
805 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000806
807 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000808 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000809 TypeResult BaseType = ParseClassName(EndLocation, &SS);
810 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000811 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000812
813 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000814 SourceRange Range(StartLoc, EndLocation);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000815
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000816 // Notify semantic analysis that we have parsed a complete
817 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000818 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000819 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000820}
821
822/// getAccessSpecifierIfPresent - Determine whether the next token is
823/// a C++ access-specifier.
824///
825/// access-specifier: [C++ class.derived]
826/// 'private'
827/// 'protected'
828/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000829AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000830{
831 switch (Tok.getKind()) {
832 default: return AS_none;
833 case tok::kw_private: return AS_private;
834 case tok::kw_protected: return AS_protected;
835 case tok::kw_public: return AS_public;
836 }
837}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000838
839/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
840///
841/// member-declaration:
842/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
843/// function-definition ';'[opt]
844/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
845/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000846/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000847/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000848/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000849///
850/// member-declarator-list:
851/// member-declarator
852/// member-declarator-list ',' member-declarator
853///
854/// member-declarator:
855/// declarator pure-specifier[opt]
856/// declarator constant-initializer[opt]
857/// identifier[opt] ':' constant-expression
858///
Sebastian Redle2b68332009-04-12 17:16:29 +0000859/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000860/// '= 0'
861///
862/// constant-initializer:
863/// '=' constant-expression
864///
Chris Lattner682bf922009-03-29 16:50:03 +0000865void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000866 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +0000867 if (Tok.is(tok::kw_static_assert)) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000868 SourceLocation DeclEnd;
869 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000870 return;
871 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000872
Chris Lattner682bf922009-03-29 16:50:03 +0000873 if (Tok.is(tok::kw_template)) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000874 SourceLocation DeclEnd;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000875 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
876 AS);
Chris Lattner682bf922009-03-29 16:50:03 +0000877 return;
878 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000879
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000880 // Handle: member-declaration ::= '__extension__' member-declaration
881 if (Tok.is(tok::kw___extension__)) {
882 // __extension__ silences extension warnings in the subexpression.
883 ExtensionRAIIObject O(Diags); // Use RAII to do this.
884 ConsumeToken();
885 return ParseCXXClassMemberDeclaration(AS);
886 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000887
888 if (Tok.is(tok::kw_using)) {
889 // Eat 'using'.
890 SourceLocation UsingLoc = ConsumeToken();
891
892 if (Tok.is(tok::kw_namespace)) {
893 Diag(UsingLoc, diag::err_using_namespace_in_class);
894 SkipUntil(tok::semi, true, true);
895 }
896 else {
897 SourceLocation DeclEnd;
898 // Otherwise, it must be using-declaration.
899 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd);
900 }
901 return;
902 }
903
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000904 SourceLocation DSStart = Tok.getLocation();
905 // decl-specifier-seq:
906 // Parse the common declaration-specifiers piece.
907 DeclSpec DS;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000908 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000909
910 if (Tok.is(tok::semi)) {
911 ConsumeToken();
912 // C++ 9.2p7: The member-declarator-list can be omitted only after a
913 // class-specifier or an enum-specifier or in a friend declaration.
914 // FIXME: Friend declarations.
915 switch (DS.getTypeSpecType()) {
Chris Lattner682bf922009-03-29 16:50:03 +0000916 case DeclSpec::TST_struct:
917 case DeclSpec::TST_union:
918 case DeclSpec::TST_class:
919 case DeclSpec::TST_enum:
920 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
921 return;
922 default:
923 Diag(DSStart, diag::err_no_declarators);
924 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000925 }
926 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000927
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000928 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000929
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000930 if (Tok.isNot(tok::colon)) {
931 // Parse the first declarator.
932 ParseDeclarator(DeclaratorInfo);
933 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +0000934 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000935 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000936 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000937 if (Tok.is(tok::semi))
938 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000939 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000940 }
941
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000942 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000943 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +0000944 || (DeclaratorInfo.isFunctionDeclarator() &&
945 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000946 if (!DeclaratorInfo.isFunctionDeclarator()) {
947 Diag(Tok, diag::err_func_def_no_params);
948 ConsumeBrace();
949 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000950 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000951 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000952
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000953 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
954 Diag(Tok, diag::err_function_declared_typedef);
955 // This recovery skips the entire function body. It would be nice
956 // to simply call ParseCXXInlineMethodDef() below, however Sema
957 // assumes the declarator represents a function, not a typedef.
958 ConsumeBrace();
959 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000960 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000961 }
962
Chris Lattner682bf922009-03-29 16:50:03 +0000963 ParseCXXInlineMethodDef(AS, DeclaratorInfo);
964 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000965 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000966 }
967
968 // member-declarator-list:
969 // member-declarator
970 // member-declarator-list ',' member-declarator
971
Chris Lattner682bf922009-03-29 16:50:03 +0000972 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000973 OwningExprResult BitfieldSize(Actions);
974 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +0000975 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000976
977 while (1) {
978
979 // member-declarator:
980 // declarator pure-specifier[opt]
981 // declarator constant-initializer[opt]
982 // identifier[opt] ':' constant-expression
983
984 if (Tok.is(tok::colon)) {
985 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000986 BitfieldSize = ParseConstantExpression();
987 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000988 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000989 }
990
991 // pure-specifier:
992 // '= 0'
993 //
994 // constant-initializer:
995 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +0000996 //
997 // defaulted/deleted function-definition:
998 // '=' 'default' [TODO]
999 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001000
1001 if (Tok.is(tok::equal)) {
1002 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001003 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1004 ConsumeToken();
1005 Deleted = true;
1006 } else {
1007 Init = ParseInitializer();
1008 if (Init.isInvalid())
1009 SkipUntil(tok::comma, true, true);
1010 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001011 }
1012
1013 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001014 if (Tok.is(tok::kw___attribute)) {
1015 SourceLocation Loc;
1016 AttributeList *AttrList = ParseAttributes(&Loc);
1017 DeclaratorInfo.AddAttributes(AttrList, Loc);
1018 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001019
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001020 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001021 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001022 // See Sema::ActOnCXXMemberDeclarator for details.
Chris Lattner682bf922009-03-29 16:50:03 +00001023 DeclPtrTy ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1024 DeclaratorInfo,
1025 BitfieldSize.release(),
Sebastian Redle2b68332009-04-12 17:16:29 +00001026 Init.release(),
1027 Deleted);
Chris Lattner682bf922009-03-29 16:50:03 +00001028 if (ThisDecl)
1029 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001030
Douglas Gregor72b505b2008-12-16 21:30:33 +00001031 if (DeclaratorInfo.isFunctionDeclarator() &&
1032 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
1033 != DeclSpec::SCS_typedef) {
1034 // We just declared a member function. If this member function
1035 // has any default arguments, we'll need to parse them later.
1036 LateParsedMethodDeclaration *LateMethod = 0;
1037 DeclaratorChunk::FunctionTypeInfo &FTI
1038 = DeclaratorInfo.getTypeObject(0).Fun;
1039 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1040 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1041 if (!LateMethod) {
1042 // Push this method onto the stack of late-parsed method
1043 // declarations.
Douglas Gregor6569d682009-05-27 23:11:45 +00001044 getCurrentClass().MethodDecls.push_back(
Chris Lattner682bf922009-03-29 16:50:03 +00001045 LateParsedMethodDeclaration(ThisDecl));
Douglas Gregor6569d682009-05-27 23:11:45 +00001046 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregor72b505b2008-12-16 21:30:33 +00001047
1048 // Add all of the parameters prior to this one (they don't
1049 // have default arguments).
1050 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1051 for (unsigned I = 0; I < ParamIdx; ++I)
1052 LateMethod->DefaultArgs.push_back(
1053 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
1054 }
1055
1056 // Add this parameter to the list of parameters (it or may
1057 // not have a default argument).
1058 LateMethod->DefaultArgs.push_back(
1059 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1060 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1061 }
1062 }
1063 }
1064
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001065 // If we don't have a comma, it is either the end of the list (a ';')
1066 // or an error, bail out.
1067 if (Tok.isNot(tok::comma))
1068 break;
1069
1070 // Consume the comma.
1071 ConsumeToken();
1072
1073 // Parse the next declarator.
1074 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001075 BitfieldSize = 0;
1076 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001077 Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001078
1079 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001080 if (Tok.is(tok::kw___attribute)) {
1081 SourceLocation Loc;
1082 AttributeList *AttrList = ParseAttributes(&Loc);
1083 DeclaratorInfo.AddAttributes(AttrList, Loc);
1084 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001085
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001086 if (Tok.isNot(tok::colon))
1087 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001088 }
1089
1090 if (Tok.is(tok::semi)) {
1091 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +00001092 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +00001093 DeclsInGroup.size());
1094 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001095 }
1096
1097 Diag(Tok, diag::err_expected_semi_decl_list);
1098 // Skip to end of block or statement
1099 SkipUntil(tok::r_brace, true, true);
1100 if (Tok.is(tok::semi))
1101 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001102 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001103}
1104
1105/// ParseCXXMemberSpecification - Parse the class definition.
1106///
1107/// member-specification:
1108/// member-declaration member-specification[opt]
1109/// access-specifier ':' member-specification[opt]
1110///
1111void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001112 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001113 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001114 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001115 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001116
Chris Lattner49f28ca2009-03-05 08:00:35 +00001117 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1118 PP.getSourceManager(),
1119 "parsing struct/union/class body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001120
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001121 SourceLocation LBraceLoc = ConsumeBrace();
1122
Douglas Gregor6569d682009-05-27 23:11:45 +00001123 // Determine whether this is a top-level (non-nested) class.
1124 bool TopLevelClass = ClassStack.empty() ||
1125 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001126
1127 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001128 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001129
Douglas Gregor6569d682009-05-27 23:11:45 +00001130 // Note that we are parsing a new (potentially-nested) class definition.
1131 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1132
Douglas Gregorddc29e12009-02-06 22:42:48 +00001133 if (TagDecl)
1134 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1135 else {
1136 SkipUntil(tok::r_brace, false, false);
1137 return;
1138 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001139
1140 // C++ 11p3: Members of a class defined with the keyword class are private
1141 // by default. Members of a class defined with the keywords struct or union
1142 // are public by default.
1143 AccessSpecifier CurAS;
1144 if (TagType == DeclSpec::TST_class)
1145 CurAS = AS_private;
1146 else
1147 CurAS = AS_public;
1148
1149 // While we still have something to read, read the member-declarations.
1150 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1151 // Each iteration of this loop reads one member-declaration.
1152
1153 // Check for extraneous top-level semicolon.
1154 if (Tok.is(tok::semi)) {
1155 Diag(Tok, diag::ext_extra_struct_semi);
1156 ConsumeToken();
1157 continue;
1158 }
1159
1160 AccessSpecifier AS = getAccessSpecifierIfPresent();
1161 if (AS != AS_none) {
1162 // Current token is a C++ access specifier.
1163 CurAS = AS;
1164 ConsumeToken();
1165 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1166 continue;
1167 }
1168
1169 // Parse all the comma separated declarators.
1170 ParseCXXClassMemberDeclaration(CurAS);
1171 }
1172
1173 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1174
1175 AttributeList *AttrList = 0;
1176 // If attributes exist after class contents, parse them.
1177 if (Tok.is(tok::kw___attribute))
1178 AttrList = ParseAttributes(); // FIXME: where should I put them?
1179
1180 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1181 LBraceLoc, RBraceLoc);
1182
1183 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1184 // complete within function bodies, default arguments,
1185 // exception-specifications, and constructor ctor-initializers (including
1186 // such things in nested classes).
1187 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001188 // FIXME: Only function bodies and constructor ctor-initializers are
1189 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001190 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001191 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001192 // are complete and we can parse the delayed portions of method
1193 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001194 ParseLexedMethodDeclarations(getCurrentClass());
1195 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001196 }
1197
1198 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001199 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001200 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001201
Douglas Gregor72de6672009-01-08 20:45:30 +00001202 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001203}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001204
1205/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1206/// which explicitly initializes the members or base classes of a
1207/// class (C++ [class.base.init]). For example, the three initializers
1208/// after the ':' in the Derived constructor below:
1209///
1210/// @code
1211/// class Base { };
1212/// class Derived : Base {
1213/// int x;
1214/// float f;
1215/// public:
1216/// Derived(float f) : Base(), x(17), f(f) { }
1217/// };
1218/// @endcode
1219///
1220/// [C++] ctor-initializer:
1221/// ':' mem-initializer-list
1222///
1223/// [C++] mem-initializer-list:
1224/// mem-initializer
1225/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001226void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001227 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1228
1229 SourceLocation ColonLoc = ConsumeToken();
1230
1231 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
1232
1233 do {
1234 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001235 if (!MemInit.isInvalid())
1236 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001237
1238 if (Tok.is(tok::comma))
1239 ConsumeToken();
1240 else if (Tok.is(tok::l_brace))
1241 break;
1242 else {
1243 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001244 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001245 SkipUntil(tok::l_brace, true, true);
1246 break;
1247 }
1248 } while (true);
1249
1250 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001251 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001252}
1253
1254/// ParseMemInitializer - Parse a C++ member initializer, which is
1255/// part of a constructor initializer that explicitly initializes one
1256/// member or base class (C++ [class.base.init]). See
1257/// ParseConstructorInitializer for an example.
1258///
1259/// [C++] mem-initializer:
1260/// mem-initializer-id '(' expression-list[opt] ')'
1261///
1262/// [C++] mem-initializer-id:
1263/// '::'[opt] nested-name-specifier[opt] class-name
1264/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001265Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001266 // FIXME: parse '::'[opt] nested-name-specifier[opt]
1267
1268 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001269 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001270 return true;
1271 }
1272
1273 // Get the identifier. This may be a member name or a class name,
1274 // but we'll let the semantic analysis determine which it is.
1275 IdentifierInfo *II = Tok.getIdentifierInfo();
1276 SourceLocation IdLoc = ConsumeToken();
1277
1278 // Parse the '('.
1279 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001280 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001281 return true;
1282 }
1283 SourceLocation LParenLoc = ConsumeParen();
1284
1285 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001286 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001287 CommaLocsTy CommaLocs;
1288 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1289 SkipUntil(tok::r_paren);
1290 return true;
1291 }
1292
1293 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1294
Sebastian Redla55e52c2008-11-25 22:21:31 +00001295 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
1296 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001297 ArgExprs.size(), CommaLocs.data(),
1298 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001299}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001300
1301/// ParseExceptionSpecification - Parse a C++ exception-specification
1302/// (C++ [except.spec]).
1303///
Douglas Gregora4745612008-12-01 18:00:20 +00001304/// exception-specification:
1305/// 'throw' '(' type-id-list [opt] ')'
1306/// [MS] 'throw' '(' '...' ')'
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001307///
Douglas Gregora4745612008-12-01 18:00:20 +00001308/// type-id-list:
1309/// type-id
1310/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001311///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001312bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001313 llvm::SmallVector<TypeTy*, 2>
1314 &Exceptions,
1315 llvm::SmallVector<SourceRange, 2>
1316 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001317 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001318 assert(Tok.is(tok::kw_throw) && "expected throw");
1319
1320 SourceLocation ThrowLoc = ConsumeToken();
1321
1322 if (!Tok.is(tok::l_paren)) {
1323 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1324 }
1325 SourceLocation LParenLoc = ConsumeParen();
1326
Douglas Gregora4745612008-12-01 18:00:20 +00001327 // Parse throw(...), a Microsoft extension that means "this function
1328 // can throw anything".
1329 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001330 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001331 SourceLocation EllipsisLoc = ConsumeToken();
1332 if (!getLang().Microsoft)
1333 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001334 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001335 return false;
1336 }
1337
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001338 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001339 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001340 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001341 TypeResult Res(ParseTypeName(&Range));
1342 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001343 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001344 Ranges.push_back(Range);
1345 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001346 if (Tok.is(tok::comma))
1347 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001348 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001349 break;
1350 }
1351
Sebastian Redlab197ba2009-02-09 18:23:29 +00001352 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001353 return false;
1354}
Douglas Gregor6569d682009-05-27 23:11:45 +00001355
1356/// \brief We have just started parsing the definition of a new class,
1357/// so push that class onto our stack of classes that is currently
1358/// being parsed.
1359void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
1360 assert((TopLevelClass || !ClassStack.empty()) &&
1361 "Nested class without outer class");
1362 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1363}
1364
1365/// \brief Deallocate the given parsed class and all of its nested
1366/// classes.
1367void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1368 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1369 DeallocateParsedClasses(Class->NestedClasses[I]);
1370 delete Class;
1371}
1372
1373/// \brief Pop the top class of the stack of classes that are
1374/// currently being parsed.
1375///
1376/// This routine should be called when we have finished parsing the
1377/// definition of a class, but have not yet popped the Scope
1378/// associated with the class's definition.
1379///
1380/// \returns true if the class we've popped is a top-level class,
1381/// false otherwise.
1382void Parser::PopParsingClass() {
1383 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
1384
1385 ParsingClass *Victim = ClassStack.top();
1386 ClassStack.pop();
1387 if (Victim->TopLevelClass) {
1388 // Deallocate all of the nested classes of this class,
1389 // recursively: we don't need to keep any of this information.
1390 DeallocateParsedClasses(Victim);
1391 return;
1392 }
1393 assert(!ClassStack.empty() && "Missing top-level class?");
1394
1395 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1396 Victim->NestedClasses.empty()) {
1397 // The victim is a nested class, but we will not need to perform
1398 // any processing after the definition of this class since it has
1399 // no members whose handling was delayed. Therefore, we can just
1400 // remove this nested class.
1401 delete Victim;
1402 return;
1403 }
1404
1405 // This nested class has some members that will need to be processed
1406 // after the top-level class is completely defined. Therefore, add
1407 // it to the list of nested classes within its parent.
1408 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1409 ClassStack.top()->NestedClasses.push_back(Victim);
1410 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1411}