blob: fed03a8c3423fad6987ca140e8e183f39a2eff2c [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
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000368 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
369 "decltype")) {
370 SkipUntil(tok::r_paren);
371 return;
372 }
373
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000374 // Parse the expression
375
376 // C++0x [dcl.type.simple]p4:
377 // The operand of the decltype specifier is an unevaluated operand.
378 EnterExpressionEvaluationContext Unevaluated(Actions,
379 Action::Unevaluated);
380 OwningExprResult Result = ParseExpression();
381 if (Result.isInvalid()) {
382 SkipUntil(tok::r_paren);
383 return;
384 }
385
386 // Match the ')'
387 SourceLocation RParenLoc;
388 if (Tok.is(tok::r_paren))
389 RParenLoc = ConsumeParen();
390 else
391 MatchRHSPunctuation(tok::r_paren, LParenLoc);
392
393 if (RParenLoc.isInvalid())
394 return;
395
396 const char *PrevSpec = 0;
397 // Check for duplicate type specifiers (e.g. "int decltype(a)").
398 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
399 Result.release()))
400 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
401}
402
Douglas Gregor42a552f2008-11-05 20:51:48 +0000403/// ParseClassName - Parse a C++ class-name, which names a class. Note
404/// that we only check that the result names a type; semantic analysis
405/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000406/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000407/// found.
408///
409/// class-name: [C++ 9.1]
410/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000411/// simple-template-id
Douglas Gregor42a552f2008-11-05 20:51:48 +0000412///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000413Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
414 const CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000415 // Check whether we have a template-id that names a type.
416 if (Tok.is(tok::annot_template_id)) {
417 TemplateIdAnnotation *TemplateId
418 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000419 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000420 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000421
422 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
423 TypeTy *Type = Tok.getAnnotationValue();
424 EndLocation = Tok.getAnnotationEndLoc();
425 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000426
427 if (Type)
428 return Type;
429 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000430 }
431
432 // Fall through to produce an error below.
433 }
434
Douglas Gregor42a552f2008-11-05 20:51:48 +0000435 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000436 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000437 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000438 }
439
440 // We have an identifier; check whether it is actually a type.
Douglas Gregorb696ea32009-02-04 17:00:24 +0000441 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
442 Tok.getLocation(), CurScope, SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000443 if (!Type) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000444 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000445 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000446 }
447
448 // Consume the identifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000449 EndLocation = ConsumeToken();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000450 return Type;
451}
452
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000453/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
454/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
455/// until we reach the start of a definition or see a token that
456/// cannot start a definition.
457///
458/// class-specifier: [C++ class]
459/// class-head '{' member-specification[opt] '}'
460/// class-head '{' member-specification[opt] '}' attributes[opt]
461/// class-head:
462/// class-key identifier[opt] base-clause[opt]
463/// class-key nested-name-specifier identifier base-clause[opt]
464/// class-key nested-name-specifier[opt] simple-template-id
465/// base-clause[opt]
466/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
467/// [GNU] class-key attributes[opt] nested-name-specifier
468/// identifier base-clause[opt]
469/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
470/// simple-template-id base-clause[opt]
471/// class-key:
472/// 'class'
473/// 'struct'
474/// 'union'
475///
476/// elaborated-type-specifier: [C++ dcl.type.elab]
477/// class-key ::[opt] nested-name-specifier[opt] identifier
478/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
479/// simple-template-id
480///
481/// Note that the C++ class-specifier and elaborated-type-specifier,
482/// together, subsume the C99 struct-or-union-specifier:
483///
484/// struct-or-union-specifier: [C99 6.7.2.1]
485/// struct-or-union identifier[opt] '{' struct-contents '}'
486/// struct-or-union identifier
487/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
488/// '}' attributes[opt]
489/// [GNU] struct-or-union attributes[opt] identifier
490/// struct-or-union:
491/// 'struct'
492/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000493void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
494 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000495 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000496 AccessSpecifier AS) {
Chris Lattner4c97d762009-04-12 21:49:30 +0000497 DeclSpec::TST TagType;
498 if (TagTokKind == tok::kw_struct)
499 TagType = DeclSpec::TST_struct;
500 else if (TagTokKind == tok::kw_class)
501 TagType = DeclSpec::TST_class;
502 else {
503 assert(TagTokKind == tok::kw_union && "Not a class specifier");
504 TagType = DeclSpec::TST_union;
505 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000506
507 AttributeList *Attr = 0;
508 // If attributes exist after tag, parse them.
509 if (Tok.is(tok::kw___attribute))
510 Attr = ParseAttributes();
511
Steve Narofff59e17e2008-12-24 20:59:21 +0000512 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000513 if (Tok.is(tok::kw___declspec))
514 Attr = ParseMicrosoftDeclSpec(Attr);
Steve Narofff59e17e2008-12-24 20:59:21 +0000515
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000516 // Parse the (optional) nested-name-specifier.
517 CXXScopeSpec SS;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000518 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS))
519 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000520 Diag(Tok, diag::err_expected_ident);
Douglas Gregorcc636682009-02-17 23:15:12 +0000521
522 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000523 IdentifierInfo *Name = 0;
524 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000525 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000526 if (Tok.is(tok::identifier)) {
527 Name = Tok.getIdentifierInfo();
528 NameLoc = ConsumeToken();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000529 } else if (Tok.is(tok::annot_template_id)) {
530 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
531 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000532
Douglas Gregorc45c2322009-03-31 00:43:58 +0000533 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000534 // The template-name in the simple-template-id refers to
535 // something other than a class template. Give an appropriate
536 // error message and skip to the ';'.
537 SourceRange Range(NameLoc);
538 if (SS.isNotEmpty())
539 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000540
Douglas Gregor39a8de12009-02-25 19:37:18 +0000541 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
542 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +0000543
Douglas Gregor39a8de12009-02-25 19:37:18 +0000544 DS.SetTypeSpecError();
545 SkipUntil(tok::semi, false, true);
546 TemplateId->Destroy();
547 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000548 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000549 }
550
551 // There are three options here. If we have 'struct foo;', then
552 // this is a forward declaration. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000553 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000554 // something like 'struct foo xyz', a reference.
555 Action::TagKind TK;
556 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
557 TK = Action::TK_Definition;
Anders Carlsson5dc2af12009-05-11 22:25:03 +0000558 else if (Tok.is(tok::semi) && !DS.isFriendSpecified())
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000559 TK = Action::TK_Declaration;
560 else
561 TK = Action::TK_Reference;
562
Douglas Gregor39a8de12009-02-25 19:37:18 +0000563 if (!Name && !TemplateId && TK != Action::TK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000564 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000565 Diag(StartLoc, diag::err_anon_type_definition)
566 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000567
568 // Skip the rest of this declarator, up until the comma or semicolon.
569 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000570
571 if (TemplateId)
572 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000573 return;
574 }
575
Douglas Gregorddc29e12009-02-06 22:42:48 +0000576 // Create the tag portion of the class or class template.
Douglas Gregor212e81c2009-03-25 00:13:59 +0000577 Action::DeclResult TagOrTempResult;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000578 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
579
580 // FIXME: When TK == TK_Reference and we have a template-id, we need
581 // to turn that template-id into a type.
582
Douglas Gregor402abb52009-05-28 23:31:59 +0000583 bool Owned = false;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000584 if (TemplateId && TK != Action::TK_Reference) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000585 // Explicit specialization, class template partial specialization,
586 // or explicit instantiation.
Douglas Gregor39a8de12009-02-25 19:37:18 +0000587 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
588 TemplateId->getTemplateArgs(),
589 TemplateId->getTemplateArgIsType(),
590 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000591 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
592 TK == Action::TK_Declaration) {
593 // This is an explicit instantiation of a class template.
594 TagOrTempResult
595 = Actions.ActOnExplicitInstantiation(CurScope,
596 TemplateInfo.TemplateLoc,
597 TagType,
598 StartLoc,
599 SS,
600 TemplateTy::make(TemplateId->Template),
601 TemplateId->TemplateNameLoc,
602 TemplateId->LAngleLoc,
603 TemplateArgsPtr,
604 TemplateId->getTemplateArgLocations(),
605 TemplateId->RAngleLoc,
606 Attr);
607 } else {
608 // This is an explicit specialization or a class template
609 // partial specialization.
610 TemplateParameterLists FakedParamLists;
611
612 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
613 // This looks like an explicit instantiation, because we have
614 // something like
615 //
616 // template class Foo<X>
617 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000618 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000619 // meant to be an explicit specialization, but the user forgot
620 // the '<>' after 'template'.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000621 assert(TK == Action::TK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000622
623 SourceLocation LAngleLoc
624 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
625 Diag(TemplateId->TemplateNameLoc,
626 diag::err_explicit_instantiation_with_definition)
627 << SourceRange(TemplateInfo.TemplateLoc)
628 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>");
629
630 // Create a fake template parameter list that contains only
631 // "template<>", so that we treat this construct as a class
632 // template specialization.
633 FakedParamLists.push_back(
634 Actions.ActOnTemplateParameterList(0, SourceLocation(),
635 TemplateInfo.TemplateLoc,
636 LAngleLoc,
637 0, 0,
638 LAngleLoc));
639 TemplateParams = &FakedParamLists;
640 }
641
642 // Build the class template specialization.
643 TagOrTempResult
644 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000645 StartLoc, SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +0000646 TemplateTy::make(TemplateId->Template),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000647 TemplateId->TemplateNameLoc,
648 TemplateId->LAngleLoc,
649 TemplateArgsPtr,
650 TemplateId->getTemplateArgLocations(),
651 TemplateId->RAngleLoc,
652 Attr,
Douglas Gregorcc636682009-02-17 23:15:12 +0000653 Action::MultiTemplateParamsArg(Actions,
654 TemplateParams? &(*TemplateParams)[0] : 0,
655 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000656 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000657 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000658 } else if (TemplateParams && TK != Action::TK_Reference) {
659 // Class template declaration or definition.
Douglas Gregor212e81c2009-03-25 00:13:59 +0000660 TagOrTempResult = Actions.ActOnClassTemplate(CurScope, TagType, TK,
661 StartLoc, SS, Name, NameLoc,
662 Attr,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000663 Action::MultiTemplateParamsArg(Actions,
664 &(*TemplateParams)[0],
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000665 TemplateParams->size()),
666 AS);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000667 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
668 TK == Action::TK_Declaration) {
669 // Explicit instantiation of a member of a class template
670 // specialization, e.g.,
671 //
672 // template struct Outer<int>::Inner;
673 //
674 TagOrTempResult
675 = Actions.ActOnExplicitInstantiation(CurScope,
676 TemplateInfo.TemplateLoc,
677 TagType, StartLoc, SS, Name,
678 NameLoc, Attr);
679 } else {
680 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
681 TK == Action::TK_Definition) {
682 // FIXME: Diagnose this particular error.
683 }
684
685 // Declaration or definition of a class type
686 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS,
Douglas Gregor402abb52009-05-28 23:31:59 +0000687 Name, NameLoc, Attr, AS, Owned);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000688 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000689
690 // Parse the optional base clause (C++ only).
Chris Lattner22bd9052009-02-16 22:07:16 +0000691 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000692 ParseBaseClause(TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000693
694 // If there is a body, parse it and inform the actions module.
695 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000696 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000697 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000698 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000699 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000700 else if (TK == Action::TK_Definition) {
701 // FIXME: Complain that we have a base-specifier list but no
702 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000703 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000704 }
705
706 const char *PrevSpec = 0;
Anders Carlsson66e99772009-05-11 22:27:47 +0000707 if (TagOrTempResult.isInvalid()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000708 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000709 return;
710 }
711
Anders Carlsson66e99772009-05-11 22:27:47 +0000712 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec,
Douglas Gregor402abb52009-05-28 23:31:59 +0000713 TagOrTempResult.get().getAs<void>(), Owned))
Chris Lattner1ab3b962008-11-18 07:48:38 +0000714 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Anders Carlssond4f551b2009-05-11 22:42:30 +0000715
716 if (DS.isFriendSpecified())
717 Actions.ActOnFriendDecl(CurScope, DS.getFriendSpecLoc(),
718 TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000719}
720
721/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
722///
723/// base-clause : [C++ class.derived]
724/// ':' base-specifier-list
725/// base-specifier-list:
726/// base-specifier '...'[opt]
727/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +0000728void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000729 assert(Tok.is(tok::colon) && "Not a base clause");
730 ConsumeToken();
731
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000732 // Build up an array of parsed base specifiers.
733 llvm::SmallVector<BaseTy *, 8> BaseInfo;
734
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000735 while (true) {
736 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000737 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000738 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000739 // Skip the rest of this base specifier, up until the comma or
740 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000741 SkipUntil(tok::comma, tok::l_brace, true, true);
742 } else {
743 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +0000744 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000745 }
746
747 // If the next token is a comma, consume it and keep reading
748 // base-specifiers.
749 if (Tok.isNot(tok::comma)) break;
750
751 // Consume the comma.
752 ConsumeToken();
753 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000754
755 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +0000756 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000757}
758
759/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
760/// one entry in the base class list of a class specifier, for example:
761/// class foo : public bar, virtual private baz {
762/// 'public bar' and 'virtual private baz' are each base-specifiers.
763///
764/// base-specifier: [C++ class.derived]
765/// ::[opt] nested-name-specifier[opt] class-name
766/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
767/// class-name
768/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
769/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +0000770Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000771 bool IsVirtual = false;
772 SourceLocation StartLoc = Tok.getLocation();
773
774 // Parse the 'virtual' keyword.
775 if (Tok.is(tok::kw_virtual)) {
776 ConsumeToken();
777 IsVirtual = true;
778 }
779
780 // Parse an (optional) access specifier.
781 AccessSpecifier Access = getAccessSpecifierIfPresent();
782 if (Access)
783 ConsumeToken();
784
785 // Parse the 'virtual' keyword (again!), in case it came after the
786 // access specifier.
787 if (Tok.is(tok::kw_virtual)) {
788 SourceLocation VirtualLoc = ConsumeToken();
789 if (IsVirtual) {
790 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000791 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor31a19b62009-04-01 21:51:26 +0000792 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000793 }
794
795 IsVirtual = true;
796 }
797
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000798 // Parse optional '::' and optional nested-name-specifier.
799 CXXScopeSpec SS;
Chris Lattner7a0ab5f2009-01-06 06:59:53 +0000800 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000801
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000802 // The location of the base class itself.
803 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000804
805 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000806 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +0000807 TypeResult BaseType = ParseClassName(EndLocation, &SS);
808 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +0000809 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000810
811 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +0000812 SourceRange Range(StartLoc, EndLocation);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000813
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000814 // Notify semantic analysis that we have parsed a complete
815 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +0000816 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +0000817 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000818}
819
820/// getAccessSpecifierIfPresent - Determine whether the next token is
821/// a C++ access-specifier.
822///
823/// access-specifier: [C++ class.derived]
824/// 'private'
825/// 'protected'
826/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000827AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000828{
829 switch (Tok.getKind()) {
830 default: return AS_none;
831 case tok::kw_private: return AS_private;
832 case tok::kw_protected: return AS_protected;
833 case tok::kw_public: return AS_public;
834 }
835}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000836
837/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
838///
839/// member-declaration:
840/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
841/// function-definition ';'[opt]
842/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
843/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000844/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000845/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000846/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000847///
848/// member-declarator-list:
849/// member-declarator
850/// member-declarator-list ',' member-declarator
851///
852/// member-declarator:
853/// declarator pure-specifier[opt]
854/// declarator constant-initializer[opt]
855/// identifier[opt] ':' constant-expression
856///
Sebastian Redle2b68332009-04-12 17:16:29 +0000857/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000858/// '= 0'
859///
860/// constant-initializer:
861/// '=' constant-expression
862///
Chris Lattner682bf922009-03-29 16:50:03 +0000863void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000864 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +0000865 if (Tok.is(tok::kw_static_assert)) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000866 SourceLocation DeclEnd;
867 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000868 return;
869 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000870
Chris Lattner682bf922009-03-29 16:50:03 +0000871 if (Tok.is(tok::kw_template)) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000872 SourceLocation DeclEnd;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000873 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
874 AS);
Chris Lattner682bf922009-03-29 16:50:03 +0000875 return;
876 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000877
Chris Lattnerbc8d5642008-12-18 01:12:00 +0000878 // Handle: member-declaration ::= '__extension__' member-declaration
879 if (Tok.is(tok::kw___extension__)) {
880 // __extension__ silences extension warnings in the subexpression.
881 ExtensionRAIIObject O(Diags); // Use RAII to do this.
882 ConsumeToken();
883 return ParseCXXClassMemberDeclaration(AS);
884 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000885
886 if (Tok.is(tok::kw_using)) {
887 // Eat 'using'.
888 SourceLocation UsingLoc = ConsumeToken();
889
890 if (Tok.is(tok::kw_namespace)) {
891 Diag(UsingLoc, diag::err_using_namespace_in_class);
892 SkipUntil(tok::semi, true, true);
893 }
894 else {
895 SourceLocation DeclEnd;
896 // Otherwise, it must be using-declaration.
897 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd);
898 }
899 return;
900 }
901
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000902 SourceLocation DSStart = Tok.getLocation();
903 // decl-specifier-seq:
904 // Parse the common declaration-specifiers piece.
905 DeclSpec DS;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000906 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000907
908 if (Tok.is(tok::semi)) {
909 ConsumeToken();
910 // C++ 9.2p7: The member-declarator-list can be omitted only after a
911 // class-specifier or an enum-specifier or in a friend declaration.
912 // FIXME: Friend declarations.
913 switch (DS.getTypeSpecType()) {
Chris Lattner682bf922009-03-29 16:50:03 +0000914 case DeclSpec::TST_struct:
915 case DeclSpec::TST_union:
916 case DeclSpec::TST_class:
917 case DeclSpec::TST_enum:
918 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
919 return;
920 default:
921 Diag(DSStart, diag::err_no_declarators);
922 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000923 }
924 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000925
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000926 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000927
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000928 if (Tok.isNot(tok::colon)) {
929 // Parse the first declarator.
930 ParseDeclarator(DeclaratorInfo);
931 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +0000932 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000933 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000934 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000935 if (Tok.is(tok::semi))
936 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000937 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000938 }
939
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000940 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000941 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +0000942 || (DeclaratorInfo.isFunctionDeclarator() &&
943 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000944 if (!DeclaratorInfo.isFunctionDeclarator()) {
945 Diag(Tok, diag::err_func_def_no_params);
946 ConsumeBrace();
947 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000948 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000949 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000950
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000951 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
952 Diag(Tok, diag::err_function_declared_typedef);
953 // This recovery skips the entire function body. It would be nice
954 // to simply call ParseCXXInlineMethodDef() below, however Sema
955 // assumes the declarator represents a function, not a typedef.
956 ConsumeBrace();
957 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +0000958 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000959 }
960
Chris Lattner682bf922009-03-29 16:50:03 +0000961 ParseCXXInlineMethodDef(AS, DeclaratorInfo);
962 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000963 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000964 }
965
966 // member-declarator-list:
967 // member-declarator
968 // member-declarator-list ',' member-declarator
969
Chris Lattner682bf922009-03-29 16:50:03 +0000970 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +0000971 OwningExprResult BitfieldSize(Actions);
972 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +0000973 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000974
975 while (1) {
976
977 // member-declarator:
978 // declarator pure-specifier[opt]
979 // declarator constant-initializer[opt]
980 // identifier[opt] ':' constant-expression
981
982 if (Tok.is(tok::colon)) {
983 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000984 BitfieldSize = ParseConstantExpression();
985 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000986 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000987 }
988
989 // pure-specifier:
990 // '= 0'
991 //
992 // constant-initializer:
993 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +0000994 //
995 // defaulted/deleted function-definition:
996 // '=' 'default' [TODO]
997 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000998
999 if (Tok.is(tok::equal)) {
1000 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001001 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1002 ConsumeToken();
1003 Deleted = true;
1004 } else {
1005 Init = ParseInitializer();
1006 if (Init.isInvalid())
1007 SkipUntil(tok::comma, true, true);
1008 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001009 }
1010
1011 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001012 if (Tok.is(tok::kw___attribute)) {
1013 SourceLocation Loc;
1014 AttributeList *AttrList = ParseAttributes(&Loc);
1015 DeclaratorInfo.AddAttributes(AttrList, Loc);
1016 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001017
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001018 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001019 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001020 // See Sema::ActOnCXXMemberDeclarator for details.
Chris Lattner682bf922009-03-29 16:50:03 +00001021 DeclPtrTy ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1022 DeclaratorInfo,
1023 BitfieldSize.release(),
Sebastian Redle2b68332009-04-12 17:16:29 +00001024 Init.release(),
1025 Deleted);
Chris Lattner682bf922009-03-29 16:50:03 +00001026 if (ThisDecl)
1027 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001028
Douglas Gregor72b505b2008-12-16 21:30:33 +00001029 if (DeclaratorInfo.isFunctionDeclarator() &&
1030 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
1031 != DeclSpec::SCS_typedef) {
1032 // We just declared a member function. If this member function
1033 // has any default arguments, we'll need to parse them later.
1034 LateParsedMethodDeclaration *LateMethod = 0;
1035 DeclaratorChunk::FunctionTypeInfo &FTI
1036 = DeclaratorInfo.getTypeObject(0).Fun;
1037 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1038 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1039 if (!LateMethod) {
1040 // Push this method onto the stack of late-parsed method
1041 // declarations.
Douglas Gregor6569d682009-05-27 23:11:45 +00001042 getCurrentClass().MethodDecls.push_back(
Chris Lattner682bf922009-03-29 16:50:03 +00001043 LateParsedMethodDeclaration(ThisDecl));
Douglas Gregor6569d682009-05-27 23:11:45 +00001044 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregor72b505b2008-12-16 21:30:33 +00001045
1046 // Add all of the parameters prior to this one (they don't
1047 // have default arguments).
1048 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1049 for (unsigned I = 0; I < ParamIdx; ++I)
1050 LateMethod->DefaultArgs.push_back(
1051 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
1052 }
1053
1054 // Add this parameter to the list of parameters (it or may
1055 // not have a default argument).
1056 LateMethod->DefaultArgs.push_back(
1057 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1058 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1059 }
1060 }
1061 }
1062
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001063 // If we don't have a comma, it is either the end of the list (a ';')
1064 // or an error, bail out.
1065 if (Tok.isNot(tok::comma))
1066 break;
1067
1068 // Consume the comma.
1069 ConsumeToken();
1070
1071 // Parse the next declarator.
1072 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001073 BitfieldSize = 0;
1074 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001075 Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001076
1077 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001078 if (Tok.is(tok::kw___attribute)) {
1079 SourceLocation Loc;
1080 AttributeList *AttrList = ParseAttributes(&Loc);
1081 DeclaratorInfo.AddAttributes(AttrList, Loc);
1082 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001083
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001084 if (Tok.isNot(tok::colon))
1085 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001086 }
1087
1088 if (Tok.is(tok::semi)) {
1089 ConsumeToken();
Eli Friedmanc1dc6532009-05-29 01:49:24 +00001090 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
Chris Lattner682bf922009-03-29 16:50:03 +00001091 DeclsInGroup.size());
1092 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001093 }
1094
1095 Diag(Tok, diag::err_expected_semi_decl_list);
1096 // Skip to end of block or statement
1097 SkipUntil(tok::r_brace, true, true);
1098 if (Tok.is(tok::semi))
1099 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001100 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001101}
1102
1103/// ParseCXXMemberSpecification - Parse the class definition.
1104///
1105/// member-specification:
1106/// member-declaration member-specification[opt]
1107/// access-specifier ':' member-specification[opt]
1108///
1109void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001110 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001111 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001112 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001113 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001114
Chris Lattner49f28ca2009-03-05 08:00:35 +00001115 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1116 PP.getSourceManager(),
1117 "parsing struct/union/class body");
Chris Lattner27b7f102009-03-05 02:25:03 +00001118
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001119 SourceLocation LBraceLoc = ConsumeBrace();
1120
Douglas Gregor6569d682009-05-27 23:11:45 +00001121 // Determine whether this is a top-level (non-nested) class.
1122 bool TopLevelClass = ClassStack.empty() ||
1123 CurScope->isInCXXInlineMethodScope();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001124
1125 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001126 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001127
Douglas Gregor6569d682009-05-27 23:11:45 +00001128 // Note that we are parsing a new (potentially-nested) class definition.
1129 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass);
1130
Douglas Gregorddc29e12009-02-06 22:42:48 +00001131 if (TagDecl)
1132 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1133 else {
1134 SkipUntil(tok::r_brace, false, false);
1135 return;
1136 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001137
1138 // C++ 11p3: Members of a class defined with the keyword class are private
1139 // by default. Members of a class defined with the keywords struct or union
1140 // are public by default.
1141 AccessSpecifier CurAS;
1142 if (TagType == DeclSpec::TST_class)
1143 CurAS = AS_private;
1144 else
1145 CurAS = AS_public;
1146
1147 // While we still have something to read, read the member-declarations.
1148 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1149 // Each iteration of this loop reads one member-declaration.
1150
1151 // Check for extraneous top-level semicolon.
1152 if (Tok.is(tok::semi)) {
1153 Diag(Tok, diag::ext_extra_struct_semi);
1154 ConsumeToken();
1155 continue;
1156 }
1157
1158 AccessSpecifier AS = getAccessSpecifierIfPresent();
1159 if (AS != AS_none) {
1160 // Current token is a C++ access specifier.
1161 CurAS = AS;
1162 ConsumeToken();
1163 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1164 continue;
1165 }
1166
1167 // Parse all the comma separated declarators.
1168 ParseCXXClassMemberDeclaration(CurAS);
1169 }
1170
1171 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1172
1173 AttributeList *AttrList = 0;
1174 // If attributes exist after class contents, parse them.
1175 if (Tok.is(tok::kw___attribute))
1176 AttrList = ParseAttributes(); // FIXME: where should I put them?
1177
1178 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
1179 LBraceLoc, RBraceLoc);
1180
1181 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1182 // complete within function bodies, default arguments,
1183 // exception-specifications, and constructor ctor-initializers (including
1184 // such things in nested classes).
1185 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001186 // FIXME: Only function bodies and constructor ctor-initializers are
1187 // parsed correctly, fix the rest.
Douglas Gregor6569d682009-05-27 23:11:45 +00001188 if (TopLevelClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001189 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001190 // are complete and we can parse the delayed portions of method
1191 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001192 ParseLexedMethodDeclarations(getCurrentClass());
1193 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001194 }
1195
1196 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001197 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001198 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001199
Douglas Gregor72de6672009-01-08 20:45:30 +00001200 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001201}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001202
1203/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1204/// which explicitly initializes the members or base classes of a
1205/// class (C++ [class.base.init]). For example, the three initializers
1206/// after the ':' in the Derived constructor below:
1207///
1208/// @code
1209/// class Base { };
1210/// class Derived : Base {
1211/// int x;
1212/// float f;
1213/// public:
1214/// Derived(float f) : Base(), x(17), f(f) { }
1215/// };
1216/// @endcode
1217///
1218/// [C++] ctor-initializer:
1219/// ':' mem-initializer-list
1220///
1221/// [C++] mem-initializer-list:
1222/// mem-initializer
1223/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001224void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001225 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1226
1227 SourceLocation ColonLoc = ConsumeToken();
1228
1229 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
1230
1231 do {
1232 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001233 if (!MemInit.isInvalid())
1234 MemInitializers.push_back(MemInit.get());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001235
1236 if (Tok.is(tok::comma))
1237 ConsumeToken();
1238 else if (Tok.is(tok::l_brace))
1239 break;
1240 else {
1241 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001242 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001243 SkipUntil(tok::l_brace, true, true);
1244 break;
1245 }
1246 } while (true);
1247
1248 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001249 MemInitializers.data(), MemInitializers.size());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001250}
1251
1252/// ParseMemInitializer - Parse a C++ member initializer, which is
1253/// part of a constructor initializer that explicitly initializes one
1254/// member or base class (C++ [class.base.init]). See
1255/// ParseConstructorInitializer for an example.
1256///
1257/// [C++] mem-initializer:
1258/// mem-initializer-id '(' expression-list[opt] ')'
1259///
1260/// [C++] mem-initializer-id:
1261/// '::'[opt] nested-name-specifier[opt] class-name
1262/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001263Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001264 // FIXME: parse '::'[opt] nested-name-specifier[opt]
1265
1266 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001267 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001268 return true;
1269 }
1270
1271 // Get the identifier. This may be a member name or a class name,
1272 // but we'll let the semantic analysis determine which it is.
1273 IdentifierInfo *II = Tok.getIdentifierInfo();
1274 SourceLocation IdLoc = ConsumeToken();
1275
1276 // Parse the '('.
1277 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001278 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001279 return true;
1280 }
1281 SourceLocation LParenLoc = ConsumeParen();
1282
1283 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001284 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001285 CommaLocsTy CommaLocs;
1286 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1287 SkipUntil(tok::r_paren);
1288 return true;
1289 }
1290
1291 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1292
Sebastian Redla55e52c2008-11-25 22:21:31 +00001293 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
1294 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001295 ArgExprs.size(), CommaLocs.data(),
1296 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001297}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001298
1299/// ParseExceptionSpecification - Parse a C++ exception-specification
1300/// (C++ [except.spec]).
1301///
Douglas Gregora4745612008-12-01 18:00:20 +00001302/// exception-specification:
1303/// 'throw' '(' type-id-list [opt] ')'
1304/// [MS] 'throw' '(' '...' ')'
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001305///
Douglas Gregora4745612008-12-01 18:00:20 +00001306/// type-id-list:
1307/// type-id
1308/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001309///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001310bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001311 llvm::SmallVector<TypeTy*, 2>
1312 &Exceptions,
1313 llvm::SmallVector<SourceRange, 2>
1314 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001315 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001316 assert(Tok.is(tok::kw_throw) && "expected throw");
1317
1318 SourceLocation ThrowLoc = ConsumeToken();
1319
1320 if (!Tok.is(tok::l_paren)) {
1321 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1322 }
1323 SourceLocation LParenLoc = ConsumeParen();
1324
Douglas Gregora4745612008-12-01 18:00:20 +00001325 // Parse throw(...), a Microsoft extension that means "this function
1326 // can throw anything".
1327 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001328 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001329 SourceLocation EllipsisLoc = ConsumeToken();
1330 if (!getLang().Microsoft)
1331 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001332 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001333 return false;
1334 }
1335
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001336 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001337 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001338 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001339 TypeResult Res(ParseTypeName(&Range));
1340 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001341 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001342 Ranges.push_back(Range);
1343 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001344 if (Tok.is(tok::comma))
1345 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001346 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001347 break;
1348 }
1349
Sebastian Redlab197ba2009-02-09 18:23:29 +00001350 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001351 return false;
1352}
Douglas Gregor6569d682009-05-27 23:11:45 +00001353
1354/// \brief We have just started parsing the definition of a new class,
1355/// so push that class onto our stack of classes that is currently
1356/// being parsed.
1357void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) {
1358 assert((TopLevelClass || !ClassStack.empty()) &&
1359 "Nested class without outer class");
1360 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass));
1361}
1362
1363/// \brief Deallocate the given parsed class and all of its nested
1364/// classes.
1365void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1366 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1367 DeallocateParsedClasses(Class->NestedClasses[I]);
1368 delete Class;
1369}
1370
1371/// \brief Pop the top class of the stack of classes that are
1372/// currently being parsed.
1373///
1374/// This routine should be called when we have finished parsing the
1375/// definition of a class, but have not yet popped the Scope
1376/// associated with the class's definition.
1377///
1378/// \returns true if the class we've popped is a top-level class,
1379/// false otherwise.
1380void Parser::PopParsingClass() {
1381 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
1382
1383 ParsingClass *Victim = ClassStack.top();
1384 ClassStack.pop();
1385 if (Victim->TopLevelClass) {
1386 // Deallocate all of the nested classes of this class,
1387 // recursively: we don't need to keep any of this information.
1388 DeallocateParsedClasses(Victim);
1389 return;
1390 }
1391 assert(!ClassStack.empty() && "Missing top-level class?");
1392
1393 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1394 Victim->NestedClasses.empty()) {
1395 // The victim is a nested class, but we will not need to perform
1396 // any processing after the definition of this class since it has
1397 // no members whose handling was delayed. Therefore, we can just
1398 // remove this nested class.
1399 delete Victim;
1400 return;
1401 }
1402
1403 // This nested class has some members that will need to be processed
1404 // after the top-level class is completely defined. Therefore, add
1405 // it to the list of nested classes within its parent.
1406 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1407 ClassStack.top()->NestedClasses.push_back(Victim);
1408 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1409}