blob: d2d8c1eeaedc2564dbc03deb88e896829aa54ba9 [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
Anders Carlssonc45057a2009-03-15 18:44:04 +0000255 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonab041982009-03-11 16:27:10 +0000256
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),
Anders Carlssonc45057a2009-03-15 18:44:04 +0000260 move(AssertMessage));
Anders Carlssonab041982009-03-11 16:27:10 +0000261}
262
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000263/// ParseClassName - Parse a C++ class-name, which names a class. Note
264/// that we only check that the result names a type; semantic analysis
265/// will need to verify that the type names a class. The result is
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000266/// either a type or NULL, depending on whether a type name was
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000267/// found.
268///
269/// class-name: [C++ 9.1]
270/// identifier
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000271/// simple-template-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000272///
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000273Parser::TypeTy *Parser::ParseClassName(SourceLocation &EndLocation,
274 const CXXScopeSpec *SS) {
275 // Check whether we have a template-id that names a type.
276 if (Tok.is(tok::annot_template_id)) {
277 TemplateIdAnnotation *TemplateId
278 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
279 if (TemplateId->Kind == TNK_Class_template) {
280 if (AnnotateTemplateIdTokenAsType(SS))
281 return 0;
282
283 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
284 TypeTy *Type = Tok.getAnnotationValue();
285 EndLocation = Tok.getAnnotationEndLoc();
286 ConsumeToken();
287 return Type;
288 }
289
290 // Fall through to produce an error below.
291 }
292
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000293 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000294 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000295 return 0;
296 }
297
298 // We have an identifier; check whether it is actually a type.
Douglas Gregor1075a162009-02-04 17:00:24 +0000299 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
300 Tok.getLocation(), CurScope, SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000301 if (!Type) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000302 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000303 return 0;
304 }
305
306 // Consume the identifier.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000307 EndLocation = ConsumeToken();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000308 return Type;
309}
310
Douglas Gregorec93f442008-04-13 21:30:24 +0000311/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
312/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
313/// until we reach the start of a definition or see a token that
314/// cannot start a definition.
315///
316/// class-specifier: [C++ class]
317/// class-head '{' member-specification[opt] '}'
318/// class-head '{' member-specification[opt] '}' attributes[opt]
319/// class-head:
320/// class-key identifier[opt] base-clause[opt]
321/// class-key nested-name-specifier identifier base-clause[opt]
322/// class-key nested-name-specifier[opt] simple-template-id
323/// base-clause[opt]
324/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
325/// [GNU] class-key attributes[opt] nested-name-specifier
326/// identifier base-clause[opt]
327/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
328/// simple-template-id base-clause[opt]
329/// class-key:
330/// 'class'
331/// 'struct'
332/// 'union'
333///
334/// elaborated-type-specifier: [C++ dcl.type.elab]
335/// class-key ::[opt] nested-name-specifier[opt] identifier
336/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
337/// simple-template-id
338///
339/// Note that the C++ class-specifier and elaborated-type-specifier,
340/// together, subsume the C99 struct-or-union-specifier:
341///
342/// struct-or-union-specifier: [C99 6.7.2.1]
343/// struct-or-union identifier[opt] '{' struct-contents '}'
344/// struct-or-union identifier
345/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
346/// '}' attributes[opt]
347/// [GNU] struct-or-union attributes[opt] identifier
348/// struct-or-union:
349/// 'struct'
350/// 'union'
Douglas Gregor52473432008-12-24 02:52:09 +0000351void Parser::ParseClassSpecifier(DeclSpec &DS,
352 TemplateParameterLists *TemplateParams) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000353 assert((Tok.is(tok::kw_class) ||
354 Tok.is(tok::kw_struct) ||
355 Tok.is(tok::kw_union)) &&
356 "Not a class specifier");
357 DeclSpec::TST TagType =
358 Tok.is(tok::kw_class) ? DeclSpec::TST_class :
359 Tok.is(tok::kw_struct) ? DeclSpec::TST_struct :
360 DeclSpec::TST_union;
361
362 SourceLocation StartLoc = ConsumeToken();
363
364 AttributeList *Attr = 0;
365 // If attributes exist after tag, parse them.
366 if (Tok.is(tok::kw___attribute))
367 Attr = ParseAttributes();
368
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000369 // If declspecs exist after tag, parse them.
370 if (Tok.is(tok::kw___declspec) && PP.getLangOptions().Microsoft)
371 FuzzyParseMicrosoftDeclSpec();
372
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000373 // Parse the (optional) nested-name-specifier.
374 CXXScopeSpec SS;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000375 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS))
376 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000377 Diag(Tok, diag::err_expected_ident);
Douglas Gregora08b6c72009-02-17 23:15:12 +0000378
379 // Parse the (optional) class name or simple-template-id.
Douglas Gregorec93f442008-04-13 21:30:24 +0000380 IdentifierInfo *Name = 0;
381 SourceLocation NameLoc;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000382 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregorec93f442008-04-13 21:30:24 +0000383 if (Tok.is(tok::identifier)) {
384 Name = Tok.getIdentifierInfo();
385 NameLoc = ConsumeToken();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000386 } else if (Tok.is(tok::annot_template_id)) {
387 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
388 NameLoc = ConsumeToken();
Douglas Gregora08b6c72009-02-17 23:15:12 +0000389
Douglas Gregor0c281a82009-02-25 19:37:18 +0000390 if (TemplateId->Kind != TNK_Class_template) {
391 // The template-name in the simple-template-id refers to
392 // something other than a class template. Give an appropriate
393 // error message and skip to the ';'.
394 SourceRange Range(NameLoc);
395 if (SS.isNotEmpty())
396 Range.setBegin(SS.getBeginLoc());
Douglas Gregora08b6c72009-02-17 23:15:12 +0000397
Douglas Gregor0c281a82009-02-25 19:37:18 +0000398 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
399 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000400
Douglas Gregor0c281a82009-02-25 19:37:18 +0000401 DS.SetTypeSpecError();
402 SkipUntil(tok::semi, false, true);
403 TemplateId->Destroy();
404 return;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000405 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000406 }
407
408 // There are three options here. If we have 'struct foo;', then
409 // this is a forward declaration. If we have 'struct foo {...' or
Douglas Gregor0c281a82009-02-25 19:37:18 +0000410 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregorec93f442008-04-13 21:30:24 +0000411 // something like 'struct foo xyz', a reference.
412 Action::TagKind TK;
413 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
414 TK = Action::TK_Definition;
415 else if (Tok.is(tok::semi))
416 TK = Action::TK_Declaration;
417 else
418 TK = Action::TK_Reference;
419
Douglas Gregor0c281a82009-02-25 19:37:18 +0000420 if (!Name && !TemplateId && TK != Action::TK_Definition) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000421 // We have a declaration or reference to an anonymous class.
Chris Lattnerf006a222008-11-18 07:48:38 +0000422 Diag(StartLoc, diag::err_anon_type_definition)
423 << DeclSpec::getSpecifierName(TagType);
Douglas Gregorec93f442008-04-13 21:30:24 +0000424
425 // Skip the rest of this declarator, up until the comma or semicolon.
426 SkipUntil(tok::comma, true);
Douglas Gregor0c281a82009-02-25 19:37:18 +0000427
428 if (TemplateId)
429 TemplateId->Destroy();
Douglas Gregorec93f442008-04-13 21:30:24 +0000430 return;
431 }
432
Douglas Gregord406b032009-02-06 22:42:48 +0000433 // Create the tag portion of the class or class template.
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000434 Action::DeclResult TagOrTempResult;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000435 if (TemplateId && TK != Action::TK_Reference) {
Douglas Gregora08b6c72009-02-17 23:15:12 +0000436 // Explicit specialization or class template partial
437 // specialization. Let semantic analysis decide.
Douglas Gregor0c281a82009-02-25 19:37:18 +0000438 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
439 TemplateId->getTemplateArgs(),
440 TemplateId->getTemplateArgIsType(),
441 TemplateId->NumArgs);
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000442 TagOrTempResult
Douglas Gregora08b6c72009-02-17 23:15:12 +0000443 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK,
Douglas Gregor0c281a82009-02-25 19:37:18 +0000444 StartLoc, SS,
445 TemplateId->Template,
446 TemplateId->TemplateNameLoc,
447 TemplateId->LAngleLoc,
448 TemplateArgsPtr,
449 TemplateId->getTemplateArgLocations(),
450 TemplateId->RAngleLoc,
451 Attr,
Douglas Gregora08b6c72009-02-17 23:15:12 +0000452 Action::MultiTemplateParamsArg(Actions,
453 TemplateParams? &(*TemplateParams)[0] : 0,
454 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor0c281a82009-02-25 19:37:18 +0000455 TemplateId->Destroy();
456 } else if (TemplateParams && TK != Action::TK_Reference)
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000457 TagOrTempResult = Actions.ActOnClassTemplate(CurScope, TagType, TK,
458 StartLoc, SS, Name, NameLoc,
459 Attr,
Douglas Gregord406b032009-02-06 22:42:48 +0000460 Action::MultiTemplateParamsArg(Actions,
461 &(*TemplateParams)[0],
462 TemplateParams->size()));
463 else
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000464 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name,
Douglas Gregord406b032009-02-06 22:42:48 +0000465 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 Gregorc5d6fa72009-03-25 00:13:59 +0000469 ParseBaseClause(TagOrTempResult.get());
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 Gregorc5d6fa72009-03-25 00:13:59 +0000474 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000475 else
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000476 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
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 Gregorc5d6fa72009-03-25 00:13:59 +0000484 if (TagOrTempResult.isInvalid())
Douglas Gregord406b032009-02-06 22:42:48 +0000485 DS.SetTypeSpecError();
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000486 else if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec,
487 TagOrTempResult.get()))
Chris Lattnerf006a222008-11-18 07:48:38 +0000488 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Douglas Gregorec93f442008-04-13 21:30:24 +0000489}
490
491/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
492///
493/// base-clause : [C++ class.derived]
494/// ':' base-specifier-list
495/// base-specifier-list:
496/// base-specifier '...'[opt]
497/// base-specifier-list ',' base-specifier '...'[opt]
498void Parser::ParseBaseClause(DeclTy *ClassDecl)
499{
500 assert(Tok.is(tok::colon) && "Not a base clause");
501 ConsumeToken();
502
Douglas Gregorabed2172008-10-22 17:49:05 +0000503 // Build up an array of parsed base specifiers.
504 llvm::SmallVector<BaseTy *, 8> BaseInfo;
505
Douglas Gregorec93f442008-04-13 21:30:24 +0000506 while (true) {
507 // Parse a base-specifier.
Douglas Gregorabed2172008-10-22 17:49:05 +0000508 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000509 if (Result.isInvalid()) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000510 // Skip the rest of this base specifier, up until the comma or
511 // opening brace.
Douglas Gregorabed2172008-10-22 17:49:05 +0000512 SkipUntil(tok::comma, tok::l_brace, true, true);
513 } else {
514 // Add this to our array of base specifiers.
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000515 BaseInfo.push_back(Result.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000516 }
517
518 // If the next token is a comma, consume it and keep reading
519 // base-specifiers.
520 if (Tok.isNot(tok::comma)) break;
521
522 // Consume the comma.
523 ConsumeToken();
524 }
Douglas Gregorabed2172008-10-22 17:49:05 +0000525
526 // Attach the base specifiers
527 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregorec93f442008-04-13 21:30:24 +0000528}
529
530/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
531/// one entry in the base class list of a class specifier, for example:
532/// class foo : public bar, virtual private baz {
533/// 'public bar' and 'virtual private baz' are each base-specifiers.
534///
535/// base-specifier: [C++ class.derived]
536/// ::[opt] nested-name-specifier[opt] class-name
537/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
538/// class-name
539/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
540/// class-name
Douglas Gregorabed2172008-10-22 17:49:05 +0000541Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
Douglas Gregorec93f442008-04-13 21:30:24 +0000542{
543 bool IsVirtual = false;
544 SourceLocation StartLoc = Tok.getLocation();
545
546 // Parse the 'virtual' keyword.
547 if (Tok.is(tok::kw_virtual)) {
548 ConsumeToken();
549 IsVirtual = true;
550 }
551
552 // Parse an (optional) access specifier.
553 AccessSpecifier Access = getAccessSpecifierIfPresent();
554 if (Access)
555 ConsumeToken();
556
557 // Parse the 'virtual' keyword (again!), in case it came after the
558 // access specifier.
559 if (Tok.is(tok::kw_virtual)) {
560 SourceLocation VirtualLoc = ConsumeToken();
561 if (IsVirtual) {
562 // Complain about duplicate 'virtual'
Chris Lattnerf006a222008-11-18 07:48:38 +0000563 Diag(VirtualLoc, diag::err_dup_virtual)
564 << SourceRange(VirtualLoc, VirtualLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000565 }
566
567 IsVirtual = true;
568 }
569
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000570 // Parse optional '::' and optional nested-name-specifier.
571 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +0000572 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregorec93f442008-04-13 21:30:24 +0000573
Douglas Gregorec93f442008-04-13 21:30:24 +0000574 // The location of the base class itself.
575 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000576
577 // Parse the class-name.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000578 SourceLocation EndLocation;
579 TypeTy *BaseType = ParseClassName(EndLocation, &SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000580 if (!BaseType)
581 return true;
Douglas Gregorec93f442008-04-13 21:30:24 +0000582
583 // Find the complete source range for the base-specifier.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000584 SourceRange Range(StartLoc, EndLocation);
Douglas Gregorec93f442008-04-13 21:30:24 +0000585
Douglas Gregorec93f442008-04-13 21:30:24 +0000586 // Notify semantic analysis that we have parsed a complete
587 // base-specifier.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000588 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
589 BaseType, BaseLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000590}
591
592/// getAccessSpecifierIfPresent - Determine whether the next token is
593/// a C++ access-specifier.
594///
595/// access-specifier: [C++ class.derived]
596/// 'private'
597/// 'protected'
598/// 'public'
Douglas Gregor696be932008-04-14 00:13:42 +0000599AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregorec93f442008-04-13 21:30:24 +0000600{
601 switch (Tok.getKind()) {
602 default: return AS_none;
603 case tok::kw_private: return AS_private;
604 case tok::kw_protected: return AS_protected;
605 case tok::kw_public: return AS_public;
606 }
607}
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000608
609/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
610///
611/// member-declaration:
612/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
613/// function-definition ';'[opt]
614/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
615/// using-declaration [TODO]
Anders Carlssonab041982009-03-11 16:27:10 +0000616/// [C++0x] static_assert-declaration
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000617/// template-declaration [TODO]
Chris Lattnerf3375de2008-12-18 01:12:00 +0000618/// [GNU] '__extension__' member-declaration
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000619///
620/// member-declarator-list:
621/// member-declarator
622/// member-declarator-list ',' member-declarator
623///
624/// member-declarator:
625/// declarator pure-specifier[opt]
626/// declarator constant-initializer[opt]
627/// identifier[opt] ':' constant-expression
628///
629/// pure-specifier: [TODO]
630/// '= 0'
631///
632/// constant-initializer:
633/// '=' constant-expression
634///
635Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Anders Carlssonab041982009-03-11 16:27:10 +0000636 // static_assert-declaration
637 if (Tok.is(tok::kw_static_assert))
638 return ParseStaticAssertDeclaration();
639
Chris Lattnerf3375de2008-12-18 01:12:00 +0000640 // Handle: member-declaration ::= '__extension__' member-declaration
641 if (Tok.is(tok::kw___extension__)) {
642 // __extension__ silences extension warnings in the subexpression.
643 ExtensionRAIIObject O(Diags); // Use RAII to do this.
644 ConsumeToken();
645 return ParseCXXClassMemberDeclaration(AS);
646 }
647
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000648 SourceLocation DSStart = Tok.getLocation();
649 // decl-specifier-seq:
650 // Parse the common declaration-specifiers piece.
651 DeclSpec DS;
652 ParseDeclarationSpecifiers(DS);
653
654 if (Tok.is(tok::semi)) {
655 ConsumeToken();
656 // C++ 9.2p7: The member-declarator-list can be omitted only after a
657 // class-specifier or an enum-specifier or in a friend declaration.
658 // FIXME: Friend declarations.
659 switch (DS.getTypeSpecType()) {
660 case DeclSpec::TST_struct:
661 case DeclSpec::TST_union:
662 case DeclSpec::TST_class:
663 case DeclSpec::TST_enum:
664 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
665 default:
666 Diag(DSStart, diag::err_no_declarators);
667 return 0;
668 }
669 }
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000670
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000671 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000672
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000673 if (Tok.isNot(tok::colon)) {
674 // Parse the first declarator.
675 ParseDeclarator(DeclaratorInfo);
676 // Error parsing the declarator?
Douglas Gregor6704b312008-11-17 22:58:34 +0000677 if (!DeclaratorInfo.hasName()) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000678 // If so, skip until the semi-colon or a }.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000679 SkipUntil(tok::r_brace, true);
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000680 if (Tok.is(tok::semi))
681 ConsumeToken();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000682 return 0;
683 }
684
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000685 // function-definition:
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000686 if (Tok.is(tok::l_brace)
687 || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000688 if (!DeclaratorInfo.isFunctionDeclarator()) {
689 Diag(Tok, diag::err_func_def_no_params);
690 ConsumeBrace();
691 SkipUntil(tok::r_brace, true);
692 return 0;
693 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000694
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000695 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
696 Diag(Tok, diag::err_function_declared_typedef);
697 // This recovery skips the entire function body. It would be nice
698 // to simply call ParseCXXInlineMethodDef() below, however Sema
699 // assumes the declarator represents a function, not a typedef.
700 ConsumeBrace();
701 SkipUntil(tok::r_brace, true);
702 return 0;
703 }
704
705 return ParseCXXInlineMethodDef(AS, DeclaratorInfo);
706 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000707 }
708
709 // member-declarator-list:
710 // member-declarator
711 // member-declarator-list ',' member-declarator
712
713 DeclTy *LastDeclInGroup = 0;
Sebastian Redl62261042008-12-09 20:22:58 +0000714 OwningExprResult BitfieldSize(Actions);
715 OwningExprResult Init(Actions);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000716
717 while (1) {
718
719 // member-declarator:
720 // declarator pure-specifier[opt]
721 // declarator constant-initializer[opt]
722 // identifier[opt] ':' constant-expression
723
724 if (Tok.is(tok::colon)) {
725 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000726 BitfieldSize = ParseConstantExpression();
727 if (BitfieldSize.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000728 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000729 }
730
731 // pure-specifier:
732 // '= 0'
733 //
734 // constant-initializer:
735 // '=' constant-expression
736
737 if (Tok.is(tok::equal)) {
738 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000739 Init = ParseInitializer();
740 if (Init.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000741 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000742 }
743
744 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +0000745 if (Tok.is(tok::kw___attribute)) {
746 SourceLocation Loc;
747 AttributeList *AttrList = ParseAttributes(&Loc);
748 DeclaratorInfo.AddAttributes(AttrList, Loc);
749 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000750
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000751 // NOTE: If Sema is the Action module and declarator is an instance field,
752 // this call will *not* return the created decl; LastDeclInGroup will be
753 // returned instead.
754 // See Sema::ActOnCXXMemberDeclarator for details.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000755 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
756 DeclaratorInfo,
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000757 BitfieldSize.release(),
758 Init.release(),
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000759 LastDeclInGroup);
760
Douglas Gregor605de8d2008-12-16 21:30:33 +0000761 if (DeclaratorInfo.isFunctionDeclarator() &&
762 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
763 != DeclSpec::SCS_typedef) {
764 // We just declared a member function. If this member function
765 // has any default arguments, we'll need to parse them later.
766 LateParsedMethodDeclaration *LateMethod = 0;
767 DeclaratorChunk::FunctionTypeInfo &FTI
768 = DeclaratorInfo.getTypeObject(0).Fun;
769 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
770 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
771 if (!LateMethod) {
772 // Push this method onto the stack of late-parsed method
773 // declarations.
774 getCurTopClassStack().MethodDecls.push_back(
775 LateParsedMethodDeclaration(LastDeclInGroup));
776 LateMethod = &getCurTopClassStack().MethodDecls.back();
777
778 // Add all of the parameters prior to this one (they don't
779 // have default arguments).
780 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
781 for (unsigned I = 0; I < ParamIdx; ++I)
782 LateMethod->DefaultArgs.push_back(
783 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
784 }
785
786 // Add this parameter to the list of parameters (it or may
787 // not have a default argument).
788 LateMethod->DefaultArgs.push_back(
789 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
790 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
791 }
792 }
793 }
794
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000795 // If we don't have a comma, it is either the end of the list (a ';')
796 // or an error, bail out.
797 if (Tok.isNot(tok::comma))
798 break;
799
800 // Consume the comma.
801 ConsumeToken();
802
803 // Parse the next declarator.
804 DeclaratorInfo.clear();
Sebastian Redl62261042008-12-09 20:22:58 +0000805 BitfieldSize = 0;
806 Init = 0;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000807
808 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +0000809 if (Tok.is(tok::kw___attribute)) {
810 SourceLocation Loc;
811 AttributeList *AttrList = ParseAttributes(&Loc);
812 DeclaratorInfo.AddAttributes(AttrList, Loc);
813 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000814
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000815 if (Tok.isNot(tok::colon))
816 ParseDeclarator(DeclaratorInfo);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000817 }
818
819 if (Tok.is(tok::semi)) {
820 ConsumeToken();
821 // Reverse the chain list.
822 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
823 }
824
825 Diag(Tok, diag::err_expected_semi_decl_list);
826 // Skip to end of block or statement
827 SkipUntil(tok::r_brace, true, true);
828 if (Tok.is(tok::semi))
829 ConsumeToken();
830 return 0;
831}
832
833/// ParseCXXMemberSpecification - Parse the class definition.
834///
835/// member-specification:
836/// member-declaration member-specification[opt]
837/// access-specifier ':' member-specification[opt]
838///
839void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
840 unsigned TagType, DeclTy *TagDecl) {
Sanjiv Guptafa451432008-10-31 09:52:39 +0000841 assert((TagType == DeclSpec::TST_struct ||
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000842 TagType == DeclSpec::TST_union ||
Sanjiv Guptafa451432008-10-31 09:52:39 +0000843 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000844
Chris Lattnerc309ade2009-03-05 08:00:35 +0000845 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
846 PP.getSourceManager(),
847 "parsing struct/union/class body");
Chris Lattner7efd75e2009-03-05 02:25:03 +0000848
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000849 SourceLocation LBraceLoc = ConsumeBrace();
850
Douglas Gregorcab994d2009-01-09 22:42:13 +0000851 if (!CurScope->isClassScope() && // Not about to define a nested class.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000852 CurScope->isInCXXInlineMethodScope()) {
853 // We will define a local class of an inline method.
854 // Push a new LexedMethodsForTopClass for its inline methods.
855 PushTopClassStack();
856 }
857
858 // Enter a scope for the class.
Douglas Gregorcab994d2009-01-09 22:42:13 +0000859 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000860
Douglas Gregord406b032009-02-06 22:42:48 +0000861 if (TagDecl)
862 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
863 else {
864 SkipUntil(tok::r_brace, false, false);
865 return;
866 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000867
868 // C++ 11p3: Members of a class defined with the keyword class are private
869 // by default. Members of a class defined with the keywords struct or union
870 // are public by default.
871 AccessSpecifier CurAS;
872 if (TagType == DeclSpec::TST_class)
873 CurAS = AS_private;
874 else
875 CurAS = AS_public;
876
877 // While we still have something to read, read the member-declarations.
878 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
879 // Each iteration of this loop reads one member-declaration.
880
881 // Check for extraneous top-level semicolon.
882 if (Tok.is(tok::semi)) {
883 Diag(Tok, diag::ext_extra_struct_semi);
884 ConsumeToken();
885 continue;
886 }
887
888 AccessSpecifier AS = getAccessSpecifierIfPresent();
889 if (AS != AS_none) {
890 // Current token is a C++ access specifier.
891 CurAS = AS;
892 ConsumeToken();
893 ExpectAndConsume(tok::colon, diag::err_expected_colon);
894 continue;
895 }
896
897 // Parse all the comma separated declarators.
898 ParseCXXClassMemberDeclaration(CurAS);
899 }
900
901 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
902
903 AttributeList *AttrList = 0;
904 // If attributes exist after class contents, parse them.
905 if (Tok.is(tok::kw___attribute))
906 AttrList = ParseAttributes(); // FIXME: where should I put them?
907
908 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
909 LBraceLoc, RBraceLoc);
910
911 // C++ 9.2p2: Within the class member-specification, the class is regarded as
912 // complete within function bodies, default arguments,
913 // exception-specifications, and constructor ctor-initializers (including
914 // such things in nested classes).
915 //
Douglas Gregor605de8d2008-12-16 21:30:33 +0000916 // FIXME: Only function bodies and constructor ctor-initializers are
917 // parsed correctly, fix the rest.
Douglas Gregorcab994d2009-01-09 22:42:13 +0000918 if (!CurScope->getParent()->isClassScope()) {
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000919 // We are not inside a nested class. This class and its nested classes
Douglas Gregor605de8d2008-12-16 21:30:33 +0000920 // are complete and we can parse the delayed portions of method
921 // declarations and the lexed inline method definitions.
922 ParseLexedMethodDeclarations();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000923 ParseLexedMethodDefs();
924
925 // For a local class of inline method, pop the LexedMethodsForTopClass that
926 // was previously pushed.
927
Sanjiv Guptafa451432008-10-31 09:52:39 +0000928 assert((CurScope->isInCXXInlineMethodScope() ||
929 TopClassStacks.size() == 1) &&
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000930 "MethodLexers not getting popped properly!");
931 if (CurScope->isInCXXInlineMethodScope())
932 PopTopClassStack();
933 }
934
935 // Leave the class scope.
Douglas Gregor95d40792008-12-10 06:34:36 +0000936 ClassScope.Exit();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000937
Douglas Gregordb568cf2009-01-08 20:45:30 +0000938 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000939}
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000940
941/// ParseConstructorInitializer - Parse a C++ constructor initializer,
942/// which explicitly initializes the members or base classes of a
943/// class (C++ [class.base.init]). For example, the three initializers
944/// after the ':' in the Derived constructor below:
945///
946/// @code
947/// class Base { };
948/// class Derived : Base {
949/// int x;
950/// float f;
951/// public:
952/// Derived(float f) : Base(), x(17), f(f) { }
953/// };
954/// @endcode
955///
956/// [C++] ctor-initializer:
957/// ':' mem-initializer-list
958///
959/// [C++] mem-initializer-list:
960/// mem-initializer
961/// mem-initializer , mem-initializer-list
962void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) {
963 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
964
965 SourceLocation ColonLoc = ConsumeToken();
966
967 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
968
969 do {
970 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000971 if (!MemInit.isInvalid())
972 MemInitializers.push_back(MemInit.get());
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000973
974 if (Tok.is(tok::comma))
975 ConsumeToken();
976 else if (Tok.is(tok::l_brace))
977 break;
978 else {
979 // Skip over garbage, until we get to '{'. Don't eat the '{'.
980 SkipUntil(tok::l_brace, true, true);
981 break;
982 }
983 } while (true);
984
985 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
986 &MemInitializers[0], MemInitializers.size());
987}
988
989/// ParseMemInitializer - Parse a C++ member initializer, which is
990/// part of a constructor initializer that explicitly initializes one
991/// member or base class (C++ [class.base.init]). See
992/// ParseConstructorInitializer for an example.
993///
994/// [C++] mem-initializer:
995/// mem-initializer-id '(' expression-list[opt] ')'
996///
997/// [C++] mem-initializer-id:
998/// '::'[opt] nested-name-specifier[opt] class-name
999/// identifier
1000Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) {
1001 // FIXME: parse '::'[opt] nested-name-specifier[opt]
1002
1003 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001004 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001005 return true;
1006 }
1007
1008 // Get the identifier. This may be a member name or a class name,
1009 // but we'll let the semantic analysis determine which it is.
1010 IdentifierInfo *II = Tok.getIdentifierInfo();
1011 SourceLocation IdLoc = ConsumeToken();
1012
1013 // Parse the '('.
1014 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001015 Diag(Tok, diag::err_expected_lparen);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001016 return true;
1017 }
1018 SourceLocation LParenLoc = ConsumeParen();
1019
1020 // Parse the optional expression-list.
Sebastian Redl6008ac32008-11-25 22:21:31 +00001021 ExprVector ArgExprs(Actions);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001022 CommaLocsTy CommaLocs;
1023 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1024 SkipUntil(tok::r_paren);
1025 return true;
1026 }
1027
1028 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1029
Sebastian Redl6008ac32008-11-25 22:21:31 +00001030 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
1031 LParenLoc, ArgExprs.take(),
1032 ArgExprs.size(), &CommaLocs[0], RParenLoc);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001033}
Douglas Gregor90a2c972008-11-25 03:22:00 +00001034
1035/// ParseExceptionSpecification - Parse a C++ exception-specification
1036/// (C++ [except.spec]).
1037///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001038/// exception-specification:
1039/// 'throw' '(' type-id-list [opt] ')'
1040/// [MS] 'throw' '(' '...' ')'
Douglas Gregor90a2c972008-11-25 03:22:00 +00001041///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001042/// type-id-list:
1043/// type-id
1044/// type-id-list ',' type-id
Douglas Gregor90a2c972008-11-25 03:22:00 +00001045///
Sebastian Redl0c986032009-02-09 18:23:29 +00001046bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00001047 assert(Tok.is(tok::kw_throw) && "expected throw");
1048
1049 SourceLocation ThrowLoc = ConsumeToken();
1050
1051 if (!Tok.is(tok::l_paren)) {
1052 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1053 }
1054 SourceLocation LParenLoc = ConsumeParen();
1055
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001056 // Parse throw(...), a Microsoft extension that means "this function
1057 // can throw anything".
1058 if (Tok.is(tok::ellipsis)) {
1059 SourceLocation EllipsisLoc = ConsumeToken();
1060 if (!getLang().Microsoft)
1061 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redl0c986032009-02-09 18:23:29 +00001062 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001063 return false;
1064 }
1065
Douglas Gregor90a2c972008-11-25 03:22:00 +00001066 // Parse the sequence of type-ids.
1067 while (Tok.isNot(tok::r_paren)) {
1068 ParseTypeName();
1069 if (Tok.is(tok::comma))
1070 ConsumeToken();
1071 else
1072 break;
1073 }
1074
Sebastian Redl0c986032009-02-09 18:23:29 +00001075 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001076 return false;
1077}