blob: f534f42ccb82572ee10bd12d6f9680bc8eb134a8 [file] [log] [blame]
Chris Lattnerf7b2e552007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf7b2e552007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor696be932008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregorec93f442008-04-13 21:30:24 +000016#include "clang/Parse/DeclSpec.h"
Chris Lattnerf7b2e552007-08-25 06:57:03 +000017#include "clang/Parse/Scope.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000018#include "AstGuard.h"
Chris Lattnerf3375de2008-12-18 01:12:00 +000019#include "ExtensionRAIIObject.h"
Chris Lattnerf7b2e552007-08-25 06:57:03 +000020using namespace clang;
21
22/// ParseNamespace - We know that the current token is a namespace keyword. This
23/// may either be a top level namespace or a block-level namespace alias.
24///
25/// namespace-definition: [C++ 7.3: basic.namespace]
26/// named-namespace-definition
27/// unnamed-namespace-definition
28///
29/// unnamed-namespace-definition:
30/// 'namespace' attributes[opt] '{' namespace-body '}'
31///
32/// named-namespace-definition:
33/// original-namespace-definition
34/// extension-namespace-definition
35///
36/// original-namespace-definition:
37/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
38///
39/// extension-namespace-definition:
40/// 'namespace' original-namespace-name '{' namespace-body '}'
41///
42/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
43/// 'namespace' identifier '=' qualified-namespace-specifier ';'
44///
45Parser::DeclTy *Parser::ParseNamespace(unsigned Context) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000046 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnerf7b2e552007-08-25 06:57:03 +000047 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
48
49 SourceLocation IdentLoc;
50 IdentifierInfo *Ident = 0;
51
Chris Lattner34a01ad2007-10-09 17:33:22 +000052 if (Tok.is(tok::identifier)) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +000053 Ident = Tok.getIdentifierInfo();
54 IdentLoc = ConsumeToken(); // eat the identifier.
55 }
56
57 // Read label attributes, if present.
58 DeclTy *AttrList = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +000059 if (Tok.is(tok::kw___attribute))
Chris Lattnerf7b2e552007-08-25 06:57:03 +000060 // FIXME: save these somewhere.
61 AttrList = ParseAttributes();
62
Chris Lattner34a01ad2007-10-09 17:33:22 +000063 if (Tok.is(tok::equal)) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +000064 // FIXME: Verify no attributes were present.
65 // FIXME: parse this.
Chris Lattner34a01ad2007-10-09 17:33:22 +000066 } else if (Tok.is(tok::l_brace)) {
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000067
Chris Lattnerf7b2e552007-08-25 06:57:03 +000068 SourceLocation LBrace = ConsumeBrace();
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000069
70 // Enter a scope for the namespace.
Douglas Gregor95d40792008-12-10 06:34:36 +000071 ParseScope NamespaceScope(this, Scope::DeclScope);
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000072
73 DeclTy *NamespcDecl =
74 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
75
Chris Lattnerc309ade2009-03-05 08:00:35 +000076 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
77 PP.getSourceManager(),
78 "parsing namespace");
Chris Lattner696da202009-03-05 02:09:07 +000079
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000080 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
Chris Lattner9c135722007-08-25 18:15:16 +000081 ParseExternalDeclaration();
Chris Lattnerf7b2e552007-08-25 06:57:03 +000082
Argiris Kirtzidis5f21e592008-05-01 21:44:34 +000083 // Leave the namespace scope.
Douglas Gregor95d40792008-12-10 06:34:36 +000084 NamespaceScope.Exit();
Argiris Kirtzidis5f21e592008-05-01 21:44:34 +000085
Chris Lattnerf7b2e552007-08-25 06:57:03 +000086 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000087 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBrace);
88
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000089 return NamespcDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +000090
Chris Lattnerf7b2e552007-08-25 06:57:03 +000091 } else {
Chris Lattnerf006a222008-11-18 07:48:38 +000092 Diag(Tok, Ident ? diag::err_expected_lbrace :
93 diag::err_expected_ident_lbrace);
Chris Lattnerf7b2e552007-08-25 06:57:03 +000094 }
95
96 return 0;
97}
Chris Lattner806a5f52008-01-12 07:05:38 +000098
99/// ParseLinkage - We know that the current token is a string_literal
100/// and just before that, that extern was seen.
101///
102/// linkage-specification: [C++ 7.5p2: dcl.link]
103/// 'extern' string-literal '{' declaration-seq[opt] '}'
104/// 'extern' string-literal declaration
105///
106Parser::DeclTy *Parser::ParseLinkage(unsigned Context) {
Douglas Gregor61818c52008-11-21 16:10:08 +0000107 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattner806a5f52008-01-12 07:05:38 +0000108 llvm::SmallVector<char, 8> LangBuffer;
109 // LangBuffer is guaranteed to be big enough.
110 LangBuffer.resize(Tok.getLength());
111 const char *LangBufPtr = &LangBuffer[0];
112 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
113
114 SourceLocation Loc = ConsumeStringToken();
Chris Lattner806a5f52008-01-12 07:05:38 +0000115
Douglas Gregord8028382009-01-05 19:45:36 +0000116 ParseScope LinkageScope(this, Scope::DeclScope);
117 DeclTy *LinkageSpec
118 = Actions.ActOnStartLinkageSpecification(CurScope,
119 /*FIXME: */SourceLocation(),
120 Loc, LangBufPtr, StrSize,
121 Tok.is(tok::l_brace)? Tok.getLocation()
122 : SourceLocation());
123
124 if (Tok.isNot(tok::l_brace)) {
125 ParseDeclarationOrFunctionDefinition();
126 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
127 SourceLocation());
Douglas Gregorad17e372008-12-16 22:23:02 +0000128 }
129
130 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorad17e372008-12-16 22:23:02 +0000131 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregord8028382009-01-05 19:45:36 +0000132 ParseExternalDeclaration();
Chris Lattner806a5f52008-01-12 07:05:38 +0000133 }
134
Douglas Gregorad17e372008-12-16 22:23:02 +0000135 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregord8028382009-01-05 19:45:36 +0000136 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattner806a5f52008-01-12 07:05:38 +0000137}
Douglas Gregorec93f442008-04-13 21:30:24 +0000138
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000139/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
140/// using-directive. Assumes that current token is 'using'.
Chris Lattner08ab4162009-01-06 06:55:51 +0000141Parser::DeclTy *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context) {
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000142 assert(Tok.is(tok::kw_using) && "Not using token");
143
144 // Eat 'using'.
145 SourceLocation UsingLoc = ConsumeToken();
146
Chris Lattner08ab4162009-01-06 06:55:51 +0000147 if (Tok.is(tok::kw_namespace))
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000148 // Next token after 'using' is 'namespace' so it must be using-directive
149 return ParseUsingDirective(Context, UsingLoc);
Chris Lattner08ab4162009-01-06 06:55:51 +0000150
151 // Otherwise, it must be using-declaration.
152 return ParseUsingDeclaration(Context, UsingLoc);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000153}
154
155/// ParseUsingDirective - Parse C++ using-directive, assumes
156/// that current token is 'namespace' and 'using' was already parsed.
157///
158/// using-directive: [C++ 7.3.p4: namespace.udir]
159/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
160/// namespace-name ;
161/// [GNU] using-directive:
162/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
163/// namespace-name attributes[opt] ;
164///
165Parser::DeclTy *Parser::ParseUsingDirective(unsigned Context,
166 SourceLocation UsingLoc) {
167 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
168
169 // Eat 'namespace'.
170 SourceLocation NamespcLoc = ConsumeToken();
171
172 CXXScopeSpec SS;
173 // Parse (optional) nested-name-specifier.
Chris Lattnerd706dc82009-01-06 06:59:53 +0000174 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000175
176 AttributeList *AttrList = 0;
177 IdentifierInfo *NamespcName = 0;
178 SourceLocation IdentLoc = SourceLocation();
179
180 // Parse namespace-name.
Chris Lattner7898bf62009-01-06 07:27:21 +0000181 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000182 Diag(Tok, diag::err_expected_namespace_name);
183 // If there was invalid namespace name, skip to end of decl, and eat ';'.
184 SkipUntil(tok::semi);
185 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
186 return 0;
187 }
Chris Lattner7898bf62009-01-06 07:27:21 +0000188
189 // Parse identifier.
190 NamespcName = Tok.getIdentifierInfo();
191 IdentLoc = ConsumeToken();
192
193 // Parse (optional) attributes (most likely GNU strong-using extension).
194 if (Tok.is(tok::kw___attribute))
195 AttrList = ParseAttributes();
196
197 // Eat ';'.
198 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
199 AttrList ? "attributes list" : "namespace name", tok::semi);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000200
201 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner7898bf62009-01-06 07:27:21 +0000202 IdentLoc, NamespcName, AttrList);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000203}
204
205/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
206/// 'using' was already seen.
207///
208/// using-declaration: [C++ 7.3.p3: namespace.udecl]
209/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
210/// unqualified-id [TODO]
211/// 'using' :: unqualified-id [TODO]
212///
213Parser::DeclTy *Parser::ParseUsingDeclaration(unsigned Context,
214 SourceLocation UsingLoc) {
215 assert(false && "Not implemented");
216 // FIXME: Implement parsing.
217 return 0;
218}
219
Anders Carlssonab041982009-03-11 16:27:10 +0000220/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
221///
222/// static_assert-declaration:
223/// static_assert ( constant-expression , string-literal ) ;
224///
225Parser::DeclTy *Parser::ParseStaticAssertDeclaration() {
226 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
227 SourceLocation StaticAssertLoc = ConsumeToken();
228
229 if (Tok.isNot(tok::l_paren)) {
230 Diag(Tok, diag::err_expected_lparen);
231 return 0;
232 }
233
234 SourceLocation LParenLoc = ConsumeParen();
235
236 OwningExprResult AssertExpr(ParseConstantExpression());
237 if (AssertExpr.isInvalid()) {
238 SkipUntil(tok::semi);
239 return 0;
240 }
241
Anders Carlssona24e8d52009-03-13 23:29:20 +0000242 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Anders Carlssonab041982009-03-11 16:27:10 +0000243 return 0;
Anders Carlssona24e8d52009-03-13 23:29:20 +0000244
Anders Carlssonab041982009-03-11 16:27:10 +0000245 if (Tok.isNot(tok::string_literal)) {
246 Diag(Tok, diag::err_expected_string_literal);
247 SkipUntil(tok::semi);
248 return 0;
249 }
250
251 OwningExprResult AssertMessage(ParseStringLiteralExpression());
252 if (AssertMessage.isInvalid())
253 return 0;
254
255 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
256
257 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
258
Anders Carlssona24e8d52009-03-13 23:29:20 +0000259 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
260 move(AssertMessage),
Anders Carlssonab041982009-03-11 16:27:10 +0000261 RParenLoc);
262}
263
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000264/// ParseClassName - Parse a C++ class-name, which names a class. Note
265/// that we only check that the result names a type; semantic analysis
266/// will need to verify that the type names a class. The result is
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000267/// either a type or NULL, depending on whether a type name was
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000268/// found.
269///
270/// class-name: [C++ 9.1]
271/// identifier
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000272/// simple-template-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000273///
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000274Parser::TypeTy *Parser::ParseClassName(SourceLocation &EndLocation,
275 const CXXScopeSpec *SS) {
276 // Check whether we have a template-id that names a type.
277 if (Tok.is(tok::annot_template_id)) {
278 TemplateIdAnnotation *TemplateId
279 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
280 if (TemplateId->Kind == TNK_Class_template) {
281 if (AnnotateTemplateIdTokenAsType(SS))
282 return 0;
283
284 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
285 TypeTy *Type = Tok.getAnnotationValue();
286 EndLocation = Tok.getAnnotationEndLoc();
287 ConsumeToken();
288 return Type;
289 }
290
291 // Fall through to produce an error below.
292 }
293
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000294 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000295 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000296 return 0;
297 }
298
299 // We have an identifier; check whether it is actually a type.
Douglas Gregor1075a162009-02-04 17:00:24 +0000300 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
301 Tok.getLocation(), CurScope, SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000302 if (!Type) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000303 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000304 return 0;
305 }
306
307 // Consume the identifier.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000308 EndLocation = ConsumeToken();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000309 return Type;
310}
311
Douglas Gregorec93f442008-04-13 21:30:24 +0000312/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
313/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
314/// until we reach the start of a definition or see a token that
315/// cannot start a definition.
316///
317/// class-specifier: [C++ class]
318/// class-head '{' member-specification[opt] '}'
319/// class-head '{' member-specification[opt] '}' attributes[opt]
320/// class-head:
321/// class-key identifier[opt] base-clause[opt]
322/// class-key nested-name-specifier identifier base-clause[opt]
323/// class-key nested-name-specifier[opt] simple-template-id
324/// base-clause[opt]
325/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
326/// [GNU] class-key attributes[opt] nested-name-specifier
327/// identifier base-clause[opt]
328/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
329/// simple-template-id base-clause[opt]
330/// class-key:
331/// 'class'
332/// 'struct'
333/// 'union'
334///
335/// elaborated-type-specifier: [C++ dcl.type.elab]
336/// class-key ::[opt] nested-name-specifier[opt] identifier
337/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
338/// simple-template-id
339///
340/// Note that the C++ class-specifier and elaborated-type-specifier,
341/// together, subsume the C99 struct-or-union-specifier:
342///
343/// struct-or-union-specifier: [C99 6.7.2.1]
344/// struct-or-union identifier[opt] '{' struct-contents '}'
345/// struct-or-union identifier
346/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
347/// '}' attributes[opt]
348/// [GNU] struct-or-union attributes[opt] identifier
349/// struct-or-union:
350/// 'struct'
351/// 'union'
Douglas Gregor52473432008-12-24 02:52:09 +0000352void Parser::ParseClassSpecifier(DeclSpec &DS,
353 TemplateParameterLists *TemplateParams) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000354 assert((Tok.is(tok::kw_class) ||
355 Tok.is(tok::kw_struct) ||
356 Tok.is(tok::kw_union)) &&
357 "Not a class specifier");
358 DeclSpec::TST TagType =
359 Tok.is(tok::kw_class) ? DeclSpec::TST_class :
360 Tok.is(tok::kw_struct) ? DeclSpec::TST_struct :
361 DeclSpec::TST_union;
362
363 SourceLocation StartLoc = ConsumeToken();
364
365 AttributeList *Attr = 0;
366 // If attributes exist after tag, parse them.
367 if (Tok.is(tok::kw___attribute))
368 Attr = ParseAttributes();
369
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000370 // If declspecs exist after tag, parse them.
371 if (Tok.is(tok::kw___declspec) && PP.getLangOptions().Microsoft)
372 FuzzyParseMicrosoftDeclSpec();
373
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000374 // Parse the (optional) nested-name-specifier.
375 CXXScopeSpec SS;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000376 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS))
377 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000378 Diag(Tok, diag::err_expected_ident);
Douglas Gregora08b6c72009-02-17 23:15:12 +0000379
380 // Parse the (optional) class name or simple-template-id.
Douglas Gregorec93f442008-04-13 21:30:24 +0000381 IdentifierInfo *Name = 0;
382 SourceLocation NameLoc;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000383 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregorec93f442008-04-13 21:30:24 +0000384 if (Tok.is(tok::identifier)) {
385 Name = Tok.getIdentifierInfo();
386 NameLoc = ConsumeToken();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000387 } else if (Tok.is(tok::annot_template_id)) {
388 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
389 NameLoc = ConsumeToken();
Douglas Gregora08b6c72009-02-17 23:15:12 +0000390
Douglas Gregor0c281a82009-02-25 19:37:18 +0000391 if (TemplateId->Kind != TNK_Class_template) {
392 // The template-name in the simple-template-id refers to
393 // something other than a class template. Give an appropriate
394 // error message and skip to the ';'.
395 SourceRange Range(NameLoc);
396 if (SS.isNotEmpty())
397 Range.setBegin(SS.getBeginLoc());
Douglas Gregora08b6c72009-02-17 23:15:12 +0000398
Douglas Gregor0c281a82009-02-25 19:37:18 +0000399 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
400 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000401
Douglas Gregor0c281a82009-02-25 19:37:18 +0000402 DS.SetTypeSpecError();
403 SkipUntil(tok::semi, false, true);
404 TemplateId->Destroy();
405 return;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000406 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000407 }
408
409 // There are three options here. If we have 'struct foo;', then
410 // this is a forward declaration. If we have 'struct foo {...' or
Douglas Gregor0c281a82009-02-25 19:37:18 +0000411 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregorec93f442008-04-13 21:30:24 +0000412 // something like 'struct foo xyz', a reference.
413 Action::TagKind TK;
414 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
415 TK = Action::TK_Definition;
416 else if (Tok.is(tok::semi))
417 TK = Action::TK_Declaration;
418 else
419 TK = Action::TK_Reference;
420
Douglas Gregor0c281a82009-02-25 19:37:18 +0000421 if (!Name && !TemplateId && TK != Action::TK_Definition) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000422 // We have a declaration or reference to an anonymous class.
Chris Lattnerf006a222008-11-18 07:48:38 +0000423 Diag(StartLoc, diag::err_anon_type_definition)
424 << DeclSpec::getSpecifierName(TagType);
Douglas Gregorec93f442008-04-13 21:30:24 +0000425
426 // Skip the rest of this declarator, up until the comma or semicolon.
427 SkipUntil(tok::comma, true);
Douglas Gregor0c281a82009-02-25 19:37:18 +0000428
429 if (TemplateId)
430 TemplateId->Destroy();
Douglas Gregorec93f442008-04-13 21:30:24 +0000431 return;
432 }
433
Douglas Gregord406b032009-02-06 22:42:48 +0000434 // Create the tag portion of the class or class template.
435 DeclTy *TagOrTempDecl;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000436 if (TemplateId && TK != Action::TK_Reference) {
Douglas Gregora08b6c72009-02-17 23:15:12 +0000437 // Explicit specialization or class template partial
438 // specialization. Let semantic analysis decide.
Douglas Gregor0c281a82009-02-25 19:37:18 +0000439 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
440 TemplateId->getTemplateArgs(),
441 TemplateId->getTemplateArgIsType(),
442 TemplateId->NumArgs);
Douglas Gregora08b6c72009-02-17 23:15:12 +0000443 TagOrTempDecl
444 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK,
Douglas Gregor0c281a82009-02-25 19:37:18 +0000445 StartLoc, SS,
446 TemplateId->Template,
447 TemplateId->TemplateNameLoc,
448 TemplateId->LAngleLoc,
449 TemplateArgsPtr,
450 TemplateId->getTemplateArgLocations(),
451 TemplateId->RAngleLoc,
452 Attr,
Douglas Gregora08b6c72009-02-17 23:15:12 +0000453 Action::MultiTemplateParamsArg(Actions,
454 TemplateParams? &(*TemplateParams)[0] : 0,
455 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor0c281a82009-02-25 19:37:18 +0000456 TemplateId->Destroy();
457 } else if (TemplateParams && TK != Action::TK_Reference)
Douglas Gregord406b032009-02-06 22:42:48 +0000458 TagOrTempDecl = Actions.ActOnClassTemplate(CurScope, TagType, TK, StartLoc,
459 SS, Name, NameLoc, Attr,
460 Action::MultiTemplateParamsArg(Actions,
461 &(*TemplateParams)[0],
462 TemplateParams->size()));
463 else
464 TagOrTempDecl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name,
465 NameLoc, Attr);
Douglas Gregorec93f442008-04-13 21:30:24 +0000466
467 // Parse the optional base clause (C++ only).
Chris Lattner31ccf0a2009-02-16 22:07:16 +0000468 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregor279272e2009-02-04 19:02:06 +0000469 ParseBaseClause(TagOrTempDecl);
Douglas Gregorec93f442008-04-13 21:30:24 +0000470
471 // If there is a body, parse it and inform the actions module.
472 if (Tok.is(tok::l_brace))
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000473 if (getLang().CPlusPlus)
Douglas Gregor279272e2009-02-04 19:02:06 +0000474 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempDecl);
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000475 else
Douglas Gregor279272e2009-02-04 19:02:06 +0000476 ParseStructUnionBody(StartLoc, TagType, TagOrTempDecl);
Douglas Gregorec93f442008-04-13 21:30:24 +0000477 else if (TK == Action::TK_Definition) {
478 // FIXME: Complain that we have a base-specifier list but no
479 // definition.
Chris Lattnerf006a222008-11-18 07:48:38 +0000480 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregorec93f442008-04-13 21:30:24 +0000481 }
482
483 const char *PrevSpec = 0;
Douglas Gregord406b032009-02-06 22:42:48 +0000484 if (!TagOrTempDecl)
485 DS.SetTypeSpecError();
486 else if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagOrTempDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +0000487 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Douglas Gregorec93f442008-04-13 21:30:24 +0000488}
489
490/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
491///
492/// base-clause : [C++ class.derived]
493/// ':' base-specifier-list
494/// base-specifier-list:
495/// base-specifier '...'[opt]
496/// base-specifier-list ',' base-specifier '...'[opt]
497void Parser::ParseBaseClause(DeclTy *ClassDecl)
498{
499 assert(Tok.is(tok::colon) && "Not a base clause");
500 ConsumeToken();
501
Douglas Gregorabed2172008-10-22 17:49:05 +0000502 // Build up an array of parsed base specifiers.
503 llvm::SmallVector<BaseTy *, 8> BaseInfo;
504
Douglas Gregorec93f442008-04-13 21:30:24 +0000505 while (true) {
506 // Parse a base-specifier.
Douglas Gregorabed2172008-10-22 17:49:05 +0000507 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000508 if (Result.isInvalid()) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000509 // Skip the rest of this base specifier, up until the comma or
510 // opening brace.
Douglas Gregorabed2172008-10-22 17:49:05 +0000511 SkipUntil(tok::comma, tok::l_brace, true, true);
512 } else {
513 // Add this to our array of base specifiers.
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000514 BaseInfo.push_back(Result.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000515 }
516
517 // If the next token is a comma, consume it and keep reading
518 // base-specifiers.
519 if (Tok.isNot(tok::comma)) break;
520
521 // Consume the comma.
522 ConsumeToken();
523 }
Douglas Gregorabed2172008-10-22 17:49:05 +0000524
525 // Attach the base specifiers
526 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregorec93f442008-04-13 21:30:24 +0000527}
528
529/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
530/// one entry in the base class list of a class specifier, for example:
531/// class foo : public bar, virtual private baz {
532/// 'public bar' and 'virtual private baz' are each base-specifiers.
533///
534/// base-specifier: [C++ class.derived]
535/// ::[opt] nested-name-specifier[opt] class-name
536/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
537/// class-name
538/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
539/// class-name
Douglas Gregorabed2172008-10-22 17:49:05 +0000540Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
Douglas Gregorec93f442008-04-13 21:30:24 +0000541{
542 bool IsVirtual = false;
543 SourceLocation StartLoc = Tok.getLocation();
544
545 // Parse the 'virtual' keyword.
546 if (Tok.is(tok::kw_virtual)) {
547 ConsumeToken();
548 IsVirtual = true;
549 }
550
551 // Parse an (optional) access specifier.
552 AccessSpecifier Access = getAccessSpecifierIfPresent();
553 if (Access)
554 ConsumeToken();
555
556 // Parse the 'virtual' keyword (again!), in case it came after the
557 // access specifier.
558 if (Tok.is(tok::kw_virtual)) {
559 SourceLocation VirtualLoc = ConsumeToken();
560 if (IsVirtual) {
561 // Complain about duplicate 'virtual'
Chris Lattnerf006a222008-11-18 07:48:38 +0000562 Diag(VirtualLoc, diag::err_dup_virtual)
563 << SourceRange(VirtualLoc, VirtualLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000564 }
565
566 IsVirtual = true;
567 }
568
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000569 // Parse optional '::' and optional nested-name-specifier.
570 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +0000571 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregorec93f442008-04-13 21:30:24 +0000572
Douglas Gregorec93f442008-04-13 21:30:24 +0000573 // The location of the base class itself.
574 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000575
576 // Parse the class-name.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000577 SourceLocation EndLocation;
578 TypeTy *BaseType = ParseClassName(EndLocation, &SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000579 if (!BaseType)
580 return true;
Douglas Gregorec93f442008-04-13 21:30:24 +0000581
582 // Find the complete source range for the base-specifier.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000583 SourceRange Range(StartLoc, EndLocation);
Douglas Gregorec93f442008-04-13 21:30:24 +0000584
Douglas Gregorec93f442008-04-13 21:30:24 +0000585 // Notify semantic analysis that we have parsed a complete
586 // base-specifier.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000587 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
588 BaseType, BaseLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000589}
590
591/// getAccessSpecifierIfPresent - Determine whether the next token is
592/// a C++ access-specifier.
593///
594/// access-specifier: [C++ class.derived]
595/// 'private'
596/// 'protected'
597/// 'public'
Douglas Gregor696be932008-04-14 00:13:42 +0000598AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregorec93f442008-04-13 21:30:24 +0000599{
600 switch (Tok.getKind()) {
601 default: return AS_none;
602 case tok::kw_private: return AS_private;
603 case tok::kw_protected: return AS_protected;
604 case tok::kw_public: return AS_public;
605 }
606}
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000607
608/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
609///
610/// member-declaration:
611/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
612/// function-definition ';'[opt]
613/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
614/// using-declaration [TODO]
Anders Carlssonab041982009-03-11 16:27:10 +0000615/// [C++0x] static_assert-declaration
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000616/// template-declaration [TODO]
Chris Lattnerf3375de2008-12-18 01:12:00 +0000617/// [GNU] '__extension__' member-declaration
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000618///
619/// member-declarator-list:
620/// member-declarator
621/// member-declarator-list ',' member-declarator
622///
623/// member-declarator:
624/// declarator pure-specifier[opt]
625/// declarator constant-initializer[opt]
626/// identifier[opt] ':' constant-expression
627///
628/// pure-specifier: [TODO]
629/// '= 0'
630///
631/// constant-initializer:
632/// '=' constant-expression
633///
634Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Anders Carlssonab041982009-03-11 16:27:10 +0000635 // static_assert-declaration
636 if (Tok.is(tok::kw_static_assert))
637 return ParseStaticAssertDeclaration();
638
Chris Lattnerf3375de2008-12-18 01:12:00 +0000639 // Handle: member-declaration ::= '__extension__' member-declaration
640 if (Tok.is(tok::kw___extension__)) {
641 // __extension__ silences extension warnings in the subexpression.
642 ExtensionRAIIObject O(Diags); // Use RAII to do this.
643 ConsumeToken();
644 return ParseCXXClassMemberDeclaration(AS);
645 }
646
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000647 SourceLocation DSStart = Tok.getLocation();
648 // decl-specifier-seq:
649 // Parse the common declaration-specifiers piece.
650 DeclSpec DS;
651 ParseDeclarationSpecifiers(DS);
652
653 if (Tok.is(tok::semi)) {
654 ConsumeToken();
655 // C++ 9.2p7: The member-declarator-list can be omitted only after a
656 // class-specifier or an enum-specifier or in a friend declaration.
657 // FIXME: Friend declarations.
658 switch (DS.getTypeSpecType()) {
659 case DeclSpec::TST_struct:
660 case DeclSpec::TST_union:
661 case DeclSpec::TST_class:
662 case DeclSpec::TST_enum:
663 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
664 default:
665 Diag(DSStart, diag::err_no_declarators);
666 return 0;
667 }
668 }
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000669
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000670 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000671
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000672 if (Tok.isNot(tok::colon)) {
673 // Parse the first declarator.
674 ParseDeclarator(DeclaratorInfo);
675 // Error parsing the declarator?
Douglas Gregor6704b312008-11-17 22:58:34 +0000676 if (!DeclaratorInfo.hasName()) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000677 // If so, skip until the semi-colon or a }.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000678 SkipUntil(tok::r_brace, true);
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000679 if (Tok.is(tok::semi))
680 ConsumeToken();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000681 return 0;
682 }
683
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000684 // function-definition:
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000685 if (Tok.is(tok::l_brace)
686 || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000687 if (!DeclaratorInfo.isFunctionDeclarator()) {
688 Diag(Tok, diag::err_func_def_no_params);
689 ConsumeBrace();
690 SkipUntil(tok::r_brace, true);
691 return 0;
692 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000693
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000694 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
695 Diag(Tok, diag::err_function_declared_typedef);
696 // This recovery skips the entire function body. It would be nice
697 // to simply call ParseCXXInlineMethodDef() below, however Sema
698 // assumes the declarator represents a function, not a typedef.
699 ConsumeBrace();
700 SkipUntil(tok::r_brace, true);
701 return 0;
702 }
703
704 return ParseCXXInlineMethodDef(AS, DeclaratorInfo);
705 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000706 }
707
708 // member-declarator-list:
709 // member-declarator
710 // member-declarator-list ',' member-declarator
711
712 DeclTy *LastDeclInGroup = 0;
Sebastian Redl62261042008-12-09 20:22:58 +0000713 OwningExprResult BitfieldSize(Actions);
714 OwningExprResult Init(Actions);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000715
716 while (1) {
717
718 // member-declarator:
719 // declarator pure-specifier[opt]
720 // declarator constant-initializer[opt]
721 // identifier[opt] ':' constant-expression
722
723 if (Tok.is(tok::colon)) {
724 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000725 BitfieldSize = ParseConstantExpression();
726 if (BitfieldSize.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000727 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000728 }
729
730 // pure-specifier:
731 // '= 0'
732 //
733 // constant-initializer:
734 // '=' constant-expression
735
736 if (Tok.is(tok::equal)) {
737 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000738 Init = ParseInitializer();
739 if (Init.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000740 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000741 }
742
743 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +0000744 if (Tok.is(tok::kw___attribute)) {
745 SourceLocation Loc;
746 AttributeList *AttrList = ParseAttributes(&Loc);
747 DeclaratorInfo.AddAttributes(AttrList, Loc);
748 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000749
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000750 // NOTE: If Sema is the Action module and declarator is an instance field,
751 // this call will *not* return the created decl; LastDeclInGroup will be
752 // returned instead.
753 // See Sema::ActOnCXXMemberDeclarator for details.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000754 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
755 DeclaratorInfo,
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000756 BitfieldSize.release(),
757 Init.release(),
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000758 LastDeclInGroup);
759
Douglas Gregor605de8d2008-12-16 21:30:33 +0000760 if (DeclaratorInfo.isFunctionDeclarator() &&
761 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
762 != DeclSpec::SCS_typedef) {
763 // We just declared a member function. If this member function
764 // has any default arguments, we'll need to parse them later.
765 LateParsedMethodDeclaration *LateMethod = 0;
766 DeclaratorChunk::FunctionTypeInfo &FTI
767 = DeclaratorInfo.getTypeObject(0).Fun;
768 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
769 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
770 if (!LateMethod) {
771 // Push this method onto the stack of late-parsed method
772 // declarations.
773 getCurTopClassStack().MethodDecls.push_back(
774 LateParsedMethodDeclaration(LastDeclInGroup));
775 LateMethod = &getCurTopClassStack().MethodDecls.back();
776
777 // Add all of the parameters prior to this one (they don't
778 // have default arguments).
779 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
780 for (unsigned I = 0; I < ParamIdx; ++I)
781 LateMethod->DefaultArgs.push_back(
782 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
783 }
784
785 // Add this parameter to the list of parameters (it or may
786 // not have a default argument).
787 LateMethod->DefaultArgs.push_back(
788 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
789 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
790 }
791 }
792 }
793
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000794 // If we don't have a comma, it is either the end of the list (a ';')
795 // or an error, bail out.
796 if (Tok.isNot(tok::comma))
797 break;
798
799 // Consume the comma.
800 ConsumeToken();
801
802 // Parse the next declarator.
803 DeclaratorInfo.clear();
Sebastian Redl62261042008-12-09 20:22:58 +0000804 BitfieldSize = 0;
805 Init = 0;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000806
807 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +0000808 if (Tok.is(tok::kw___attribute)) {
809 SourceLocation Loc;
810 AttributeList *AttrList = ParseAttributes(&Loc);
811 DeclaratorInfo.AddAttributes(AttrList, Loc);
812 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000813
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000814 if (Tok.isNot(tok::colon))
815 ParseDeclarator(DeclaratorInfo);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000816 }
817
818 if (Tok.is(tok::semi)) {
819 ConsumeToken();
820 // Reverse the chain list.
821 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
822 }
823
824 Diag(Tok, diag::err_expected_semi_decl_list);
825 // Skip to end of block or statement
826 SkipUntil(tok::r_brace, true, true);
827 if (Tok.is(tok::semi))
828 ConsumeToken();
829 return 0;
830}
831
832/// ParseCXXMemberSpecification - Parse the class definition.
833///
834/// member-specification:
835/// member-declaration member-specification[opt]
836/// access-specifier ':' member-specification[opt]
837///
838void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
839 unsigned TagType, DeclTy *TagDecl) {
Sanjiv Guptafa451432008-10-31 09:52:39 +0000840 assert((TagType == DeclSpec::TST_struct ||
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000841 TagType == DeclSpec::TST_union ||
Sanjiv Guptafa451432008-10-31 09:52:39 +0000842 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000843
Chris Lattnerc309ade2009-03-05 08:00:35 +0000844 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
845 PP.getSourceManager(),
846 "parsing struct/union/class body");
Chris Lattner7efd75e2009-03-05 02:25:03 +0000847
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000848 SourceLocation LBraceLoc = ConsumeBrace();
849
Douglas Gregorcab994d2009-01-09 22:42:13 +0000850 if (!CurScope->isClassScope() && // Not about to define a nested class.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000851 CurScope->isInCXXInlineMethodScope()) {
852 // We will define a local class of an inline method.
853 // Push a new LexedMethodsForTopClass for its inline methods.
854 PushTopClassStack();
855 }
856
857 // Enter a scope for the class.
Douglas Gregorcab994d2009-01-09 22:42:13 +0000858 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000859
Douglas Gregord406b032009-02-06 22:42:48 +0000860 if (TagDecl)
861 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
862 else {
863 SkipUntil(tok::r_brace, false, false);
864 return;
865 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000866
867 // C++ 11p3: Members of a class defined with the keyword class are private
868 // by default. Members of a class defined with the keywords struct or union
869 // are public by default.
870 AccessSpecifier CurAS;
871 if (TagType == DeclSpec::TST_class)
872 CurAS = AS_private;
873 else
874 CurAS = AS_public;
875
876 // While we still have something to read, read the member-declarations.
877 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
878 // Each iteration of this loop reads one member-declaration.
879
880 // Check for extraneous top-level semicolon.
881 if (Tok.is(tok::semi)) {
882 Diag(Tok, diag::ext_extra_struct_semi);
883 ConsumeToken();
884 continue;
885 }
886
887 AccessSpecifier AS = getAccessSpecifierIfPresent();
888 if (AS != AS_none) {
889 // Current token is a C++ access specifier.
890 CurAS = AS;
891 ConsumeToken();
892 ExpectAndConsume(tok::colon, diag::err_expected_colon);
893 continue;
894 }
895
896 // Parse all the comma separated declarators.
897 ParseCXXClassMemberDeclaration(CurAS);
898 }
899
900 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
901
902 AttributeList *AttrList = 0;
903 // If attributes exist after class contents, parse them.
904 if (Tok.is(tok::kw___attribute))
905 AttrList = ParseAttributes(); // FIXME: where should I put them?
906
907 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
908 LBraceLoc, RBraceLoc);
909
910 // C++ 9.2p2: Within the class member-specification, the class is regarded as
911 // complete within function bodies, default arguments,
912 // exception-specifications, and constructor ctor-initializers (including
913 // such things in nested classes).
914 //
Douglas Gregor605de8d2008-12-16 21:30:33 +0000915 // FIXME: Only function bodies and constructor ctor-initializers are
916 // parsed correctly, fix the rest.
Douglas Gregorcab994d2009-01-09 22:42:13 +0000917 if (!CurScope->getParent()->isClassScope()) {
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000918 // We are not inside a nested class. This class and its nested classes
Douglas Gregor605de8d2008-12-16 21:30:33 +0000919 // are complete and we can parse the delayed portions of method
920 // declarations and the lexed inline method definitions.
921 ParseLexedMethodDeclarations();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000922 ParseLexedMethodDefs();
923
924 // For a local class of inline method, pop the LexedMethodsForTopClass that
925 // was previously pushed.
926
Sanjiv Guptafa451432008-10-31 09:52:39 +0000927 assert((CurScope->isInCXXInlineMethodScope() ||
928 TopClassStacks.size() == 1) &&
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000929 "MethodLexers not getting popped properly!");
930 if (CurScope->isInCXXInlineMethodScope())
931 PopTopClassStack();
932 }
933
934 // Leave the class scope.
Douglas Gregor95d40792008-12-10 06:34:36 +0000935 ClassScope.Exit();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000936
Douglas Gregordb568cf2009-01-08 20:45:30 +0000937 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000938}
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000939
940/// ParseConstructorInitializer - Parse a C++ constructor initializer,
941/// which explicitly initializes the members or base classes of a
942/// class (C++ [class.base.init]). For example, the three initializers
943/// after the ':' in the Derived constructor below:
944///
945/// @code
946/// class Base { };
947/// class Derived : Base {
948/// int x;
949/// float f;
950/// public:
951/// Derived(float f) : Base(), x(17), f(f) { }
952/// };
953/// @endcode
954///
955/// [C++] ctor-initializer:
956/// ':' mem-initializer-list
957///
958/// [C++] mem-initializer-list:
959/// mem-initializer
960/// mem-initializer , mem-initializer-list
961void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) {
962 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
963
964 SourceLocation ColonLoc = ConsumeToken();
965
966 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
967
968 do {
969 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000970 if (!MemInit.isInvalid())
971 MemInitializers.push_back(MemInit.get());
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000972
973 if (Tok.is(tok::comma))
974 ConsumeToken();
975 else if (Tok.is(tok::l_brace))
976 break;
977 else {
978 // Skip over garbage, until we get to '{'. Don't eat the '{'.
979 SkipUntil(tok::l_brace, true, true);
980 break;
981 }
982 } while (true);
983
984 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
985 &MemInitializers[0], MemInitializers.size());
986}
987
988/// ParseMemInitializer - Parse a C++ member initializer, which is
989/// part of a constructor initializer that explicitly initializes one
990/// member or base class (C++ [class.base.init]). See
991/// ParseConstructorInitializer for an example.
992///
993/// [C++] mem-initializer:
994/// mem-initializer-id '(' expression-list[opt] ')'
995///
996/// [C++] mem-initializer-id:
997/// '::'[opt] nested-name-specifier[opt] class-name
998/// identifier
999Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) {
1000 // FIXME: parse '::'[opt] nested-name-specifier[opt]
1001
1002 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001003 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001004 return true;
1005 }
1006
1007 // Get the identifier. This may be a member name or a class name,
1008 // but we'll let the semantic analysis determine which it is.
1009 IdentifierInfo *II = Tok.getIdentifierInfo();
1010 SourceLocation IdLoc = ConsumeToken();
1011
1012 // Parse the '('.
1013 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001014 Diag(Tok, diag::err_expected_lparen);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001015 return true;
1016 }
1017 SourceLocation LParenLoc = ConsumeParen();
1018
1019 // Parse the optional expression-list.
Sebastian Redl6008ac32008-11-25 22:21:31 +00001020 ExprVector ArgExprs(Actions);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001021 CommaLocsTy CommaLocs;
1022 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1023 SkipUntil(tok::r_paren);
1024 return true;
1025 }
1026
1027 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1028
Sebastian Redl6008ac32008-11-25 22:21:31 +00001029 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
1030 LParenLoc, ArgExprs.take(),
1031 ArgExprs.size(), &CommaLocs[0], RParenLoc);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001032}
Douglas Gregor90a2c972008-11-25 03:22:00 +00001033
1034/// ParseExceptionSpecification - Parse a C++ exception-specification
1035/// (C++ [except.spec]).
1036///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001037/// exception-specification:
1038/// 'throw' '(' type-id-list [opt] ')'
1039/// [MS] 'throw' '(' '...' ')'
Douglas Gregor90a2c972008-11-25 03:22:00 +00001040///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001041/// type-id-list:
1042/// type-id
1043/// type-id-list ',' type-id
Douglas Gregor90a2c972008-11-25 03:22:00 +00001044///
Sebastian Redl0c986032009-02-09 18:23:29 +00001045bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00001046 assert(Tok.is(tok::kw_throw) && "expected throw");
1047
1048 SourceLocation ThrowLoc = ConsumeToken();
1049
1050 if (!Tok.is(tok::l_paren)) {
1051 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1052 }
1053 SourceLocation LParenLoc = ConsumeParen();
1054
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001055 // Parse throw(...), a Microsoft extension that means "this function
1056 // can throw anything".
1057 if (Tok.is(tok::ellipsis)) {
1058 SourceLocation EllipsisLoc = ConsumeToken();
1059 if (!getLang().Microsoft)
1060 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redl0c986032009-02-09 18:23:29 +00001061 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001062 return false;
1063 }
1064
Douglas Gregor90a2c972008-11-25 03:22:00 +00001065 // Parse the sequence of type-ids.
1066 while (Tok.isNot(tok::r_paren)) {
1067 ParseTypeName();
1068 if (Tok.is(tok::comma))
1069 ConsumeToken();
1070 else
1071 break;
1072 }
1073
Sebastian Redl0c986032009-02-09 18:23:29 +00001074 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001075 return false;
1076}