blob: f90469acacbe7eaae9265de2b687bd86c26d814b [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor1b7f8982008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Douglas Gregore37ac4f2008-04-13 21:30:24 +000015#include "clang/Basic/Diagnostic.h"
16#include "clang/Parse/DeclSpec.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000017#include "clang/Parse/Scope.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000018using namespace clang;
19
20/// ParseNamespace - We know that the current token is a namespace keyword. This
21/// may either be a top level namespace or a block-level namespace alias.
22///
23/// namespace-definition: [C++ 7.3: basic.namespace]
24/// named-namespace-definition
25/// unnamed-namespace-definition
26///
27/// unnamed-namespace-definition:
28/// 'namespace' attributes[opt] '{' namespace-body '}'
29///
30/// named-namespace-definition:
31/// original-namespace-definition
32/// extension-namespace-definition
33///
34/// original-namespace-definition:
35/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
36///
37/// extension-namespace-definition:
38/// 'namespace' original-namespace-name '{' namespace-body '}'
39///
40/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
41/// 'namespace' identifier '=' qualified-namespace-specifier ';'
42///
43Parser::DeclTy *Parser::ParseNamespace(unsigned Context) {
Chris Lattner04d66662007-10-09 17:33:22 +000044 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000045 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
46
47 SourceLocation IdentLoc;
48 IdentifierInfo *Ident = 0;
49
Chris Lattner04d66662007-10-09 17:33:22 +000050 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000051 Ident = Tok.getIdentifierInfo();
52 IdentLoc = ConsumeToken(); // eat the identifier.
53 }
54
55 // Read label attributes, if present.
56 DeclTy *AttrList = 0;
Chris Lattner04d66662007-10-09 17:33:22 +000057 if (Tok.is(tok::kw___attribute))
Chris Lattner8f08cb72007-08-25 06:57:03 +000058 // FIXME: save these somewhere.
59 AttrList = ParseAttributes();
60
Chris Lattner04d66662007-10-09 17:33:22 +000061 if (Tok.is(tok::equal)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000062 // FIXME: Verify no attributes were present.
63 // FIXME: parse this.
Chris Lattner04d66662007-10-09 17:33:22 +000064 } else if (Tok.is(tok::l_brace)) {
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000065
Chris Lattner8f08cb72007-08-25 06:57:03 +000066 SourceLocation LBrace = ConsumeBrace();
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000067
68 // Enter a scope for the namespace.
69 EnterScope(Scope::DeclScope);
70
71 DeclTy *NamespcDecl =
72 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
73
74 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
Chris Lattnerbae35112007-08-25 18:15:16 +000075 ParseExternalDeclaration();
Chris Lattner8f08cb72007-08-25 06:57:03 +000076
Argyrios Kyrtzidis8ba5d792008-05-01 21:44:34 +000077 // Leave the namespace scope.
78 ExitScope();
79
Chris Lattner8f08cb72007-08-25 06:57:03 +000080 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000081 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBrace);
82
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +000083 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +000084
Chris Lattner8f08cb72007-08-25 06:57:03 +000085 } else {
86 unsigned D = Ident ? diag::err_expected_lbrace :
87 diag::err_expected_ident_lbrace;
88 Diag(Tok.getLocation(), D);
89 }
90
91 return 0;
92}
Chris Lattnerc6fdc342008-01-12 07:05:38 +000093
94/// ParseLinkage - We know that the current token is a string_literal
95/// and just before that, that extern was seen.
96///
97/// linkage-specification: [C++ 7.5p2: dcl.link]
98/// 'extern' string-literal '{' declaration-seq[opt] '}'
99/// 'extern' string-literal declaration
100///
101Parser::DeclTy *Parser::ParseLinkage(unsigned Context) {
102 assert(Tok.is(tok::string_literal) && "Not a stringliteral!");
103 llvm::SmallVector<char, 8> LangBuffer;
104 // LangBuffer is guaranteed to be big enough.
105 LangBuffer.resize(Tok.getLength());
106 const char *LangBufPtr = &LangBuffer[0];
107 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
108
109 SourceLocation Loc = ConsumeStringToken();
110 DeclTy *D = 0;
111 SourceLocation LBrace, RBrace;
112
113 if (Tok.isNot(tok::l_brace)) {
114 D = ParseDeclaration(Context);
115 } else {
116 LBrace = ConsumeBrace();
117 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
118 // FIXME capture the decls.
119 D = ParseExternalDeclaration();
120 }
121
122 RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
123 }
124
125 if (!D)
126 return 0;
127
128 return Actions.ActOnLinkageSpec(Loc, LBrace, RBrace, LangBufPtr, StrSize, D);
129}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000130
Douglas Gregor42a552f2008-11-05 20:51:48 +0000131/// ParseClassName - Parse a C++ class-name, which names a class. Note
132/// that we only check that the result names a type; semantic analysis
133/// will need to verify that the type names a class. The result is
134/// either a type or NULL, dependending on whether a type name was
135/// found.
136///
137/// class-name: [C++ 9.1]
138/// identifier
139/// template-id [TODO]
140///
141Parser::TypeTy *Parser::ParseClassName() {
142 // Parse the class-name.
143 // FIXME: Alternatively, parse a simple-template-id.
144 if (Tok.isNot(tok::identifier)) {
145 Diag(Tok.getLocation(), diag::err_expected_class_name);
146 return 0;
147 }
148
149 // We have an identifier; check whether it is actually a type.
150 TypeTy *Type = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
151 if (!Type) {
152 Diag(Tok.getLocation(), diag::err_expected_class_name);
153 return 0;
154 }
155
156 // Consume the identifier.
157 ConsumeToken();
158
159 return Type;
160}
161
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000162/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
163/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
164/// until we reach the start of a definition or see a token that
165/// cannot start a definition.
166///
167/// class-specifier: [C++ class]
168/// class-head '{' member-specification[opt] '}'
169/// class-head '{' member-specification[opt] '}' attributes[opt]
170/// class-head:
171/// class-key identifier[opt] base-clause[opt]
172/// class-key nested-name-specifier identifier base-clause[opt]
173/// class-key nested-name-specifier[opt] simple-template-id
174/// base-clause[opt]
175/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
176/// [GNU] class-key attributes[opt] nested-name-specifier
177/// identifier base-clause[opt]
178/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
179/// simple-template-id base-clause[opt]
180/// class-key:
181/// 'class'
182/// 'struct'
183/// 'union'
184///
185/// elaborated-type-specifier: [C++ dcl.type.elab]
186/// class-key ::[opt] nested-name-specifier[opt] identifier
187/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
188/// simple-template-id
189///
190/// Note that the C++ class-specifier and elaborated-type-specifier,
191/// together, subsume the C99 struct-or-union-specifier:
192///
193/// struct-or-union-specifier: [C99 6.7.2.1]
194/// struct-or-union identifier[opt] '{' struct-contents '}'
195/// struct-or-union identifier
196/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
197/// '}' attributes[opt]
198/// [GNU] struct-or-union attributes[opt] identifier
199/// struct-or-union:
200/// 'struct'
201/// 'union'
202void Parser::ParseClassSpecifier(DeclSpec &DS) {
203 assert((Tok.is(tok::kw_class) ||
204 Tok.is(tok::kw_struct) ||
205 Tok.is(tok::kw_union)) &&
206 "Not a class specifier");
207 DeclSpec::TST TagType =
208 Tok.is(tok::kw_class) ? DeclSpec::TST_class :
209 Tok.is(tok::kw_struct) ? DeclSpec::TST_struct :
210 DeclSpec::TST_union;
211
212 SourceLocation StartLoc = ConsumeToken();
213
214 AttributeList *Attr = 0;
215 // If attributes exist after tag, parse them.
216 if (Tok.is(tok::kw___attribute))
217 Attr = ParseAttributes();
218
219 // FIXME: Parse the (optional) nested-name-specifier.
220
221 // Parse the (optional) class name.
222 // FIXME: Alternatively, parse a simple-template-id.
223 IdentifierInfo *Name = 0;
224 SourceLocation NameLoc;
225 if (Tok.is(tok::identifier)) {
226 Name = Tok.getIdentifierInfo();
227 NameLoc = ConsumeToken();
228 }
229
230 // There are three options here. If we have 'struct foo;', then
231 // this is a forward declaration. If we have 'struct foo {...' or
232 // 'struct fo :...' then this is a definition. Otherwise we have
233 // something like 'struct foo xyz', a reference.
234 Action::TagKind TK;
235 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
236 TK = Action::TK_Definition;
237 else if (Tok.is(tok::semi))
238 TK = Action::TK_Declaration;
239 else
240 TK = Action::TK_Reference;
241
242 if (!Name && TK != Action::TK_Definition) {
243 // We have a declaration or reference to an anonymous class.
244 Diag(StartLoc, diag::err_anon_type_definition,
245 DeclSpec::getSpecifierName(TagType));
246
247 // Skip the rest of this declarator, up until the comma or semicolon.
248 SkipUntil(tok::comma, true);
249 return;
250 }
251
252 // Parse the tag portion of this.
253 DeclTy *TagDecl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name,
254 NameLoc, Attr);
255
256 // Parse the optional base clause (C++ only).
257 if (getLang().CPlusPlus && Tok.is(tok::colon)) {
258 ParseBaseClause(TagDecl);
259 }
260
261 // If there is a body, parse it and inform the actions module.
262 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000263 if (getLang().CPlusPlus)
264 ParseCXXMemberSpecification(StartLoc, TagType, TagDecl);
265 else
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000266 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000267 else if (TK == Action::TK_Definition) {
268 // FIXME: Complain that we have a base-specifier list but no
269 // definition.
270 Diag(Tok.getLocation(), diag::err_expected_lbrace);
271 }
272
273 const char *PrevSpec = 0;
274 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
275 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
276}
277
278/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
279///
280/// base-clause : [C++ class.derived]
281/// ':' base-specifier-list
282/// base-specifier-list:
283/// base-specifier '...'[opt]
284/// base-specifier-list ',' base-specifier '...'[opt]
285void Parser::ParseBaseClause(DeclTy *ClassDecl)
286{
287 assert(Tok.is(tok::colon) && "Not a base clause");
288 ConsumeToken();
289
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000290 // Build up an array of parsed base specifiers.
291 llvm::SmallVector<BaseTy *, 8> BaseInfo;
292
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000293 while (true) {
294 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000295 BaseResult Result = ParseBaseSpecifier(ClassDecl);
296 if (Result.isInvalid) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000297 // Skip the rest of this base specifier, up until the comma or
298 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000299 SkipUntil(tok::comma, tok::l_brace, true, true);
300 } else {
301 // Add this to our array of base specifiers.
302 BaseInfo.push_back(Result.Val);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000303 }
304
305 // If the next token is a comma, consume it and keep reading
306 // base-specifiers.
307 if (Tok.isNot(tok::comma)) break;
308
309 // Consume the comma.
310 ConsumeToken();
311 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000312
313 // Attach the base specifiers
314 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000315}
316
317/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
318/// one entry in the base class list of a class specifier, for example:
319/// class foo : public bar, virtual private baz {
320/// 'public bar' and 'virtual private baz' are each base-specifiers.
321///
322/// base-specifier: [C++ class.derived]
323/// ::[opt] nested-name-specifier[opt] class-name
324/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
325/// class-name
326/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
327/// class-name
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000328Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000329{
330 bool IsVirtual = false;
331 SourceLocation StartLoc = Tok.getLocation();
332
333 // Parse the 'virtual' keyword.
334 if (Tok.is(tok::kw_virtual)) {
335 ConsumeToken();
336 IsVirtual = true;
337 }
338
339 // Parse an (optional) access specifier.
340 AccessSpecifier Access = getAccessSpecifierIfPresent();
341 if (Access)
342 ConsumeToken();
343
344 // Parse the 'virtual' keyword (again!), in case it came after the
345 // access specifier.
346 if (Tok.is(tok::kw_virtual)) {
347 SourceLocation VirtualLoc = ConsumeToken();
348 if (IsVirtual) {
349 // Complain about duplicate 'virtual'
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000350 Diag(VirtualLoc, diag::err_dup_virtual,
351 SourceRange(VirtualLoc, VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000352 }
353
354 IsVirtual = true;
355 }
356
357 // FIXME: Parse optional '::' and optional nested-name-specifier.
358
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000359 // The location of the base class itself.
360 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000361
362 // Parse the class-name.
363 TypeTy *BaseType = ParseClassName();
364 if (!BaseType)
365 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000366
367 // Find the complete source range for the base-specifier.
368 SourceRange Range(StartLoc, BaseLoc);
369
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000370 // Notify semantic analysis that we have parsed a complete
371 // base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000372 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, BaseType,
373 BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000374}
375
376/// getAccessSpecifierIfPresent - Determine whether the next token is
377/// a C++ access-specifier.
378///
379/// access-specifier: [C++ class.derived]
380/// 'private'
381/// 'protected'
382/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000383AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000384{
385 switch (Tok.getKind()) {
386 default: return AS_none;
387 case tok::kw_private: return AS_private;
388 case tok::kw_protected: return AS_protected;
389 case tok::kw_public: return AS_public;
390 }
391}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000392
393/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
394///
395/// member-declaration:
396/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
397/// function-definition ';'[opt]
398/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
399/// using-declaration [TODO]
400/// [C++0x] static_assert-declaration [TODO]
401/// template-declaration [TODO]
402///
403/// member-declarator-list:
404/// member-declarator
405/// member-declarator-list ',' member-declarator
406///
407/// member-declarator:
408/// declarator pure-specifier[opt]
409/// declarator constant-initializer[opt]
410/// identifier[opt] ':' constant-expression
411///
412/// pure-specifier: [TODO]
413/// '= 0'
414///
415/// constant-initializer:
416/// '=' constant-expression
417///
418Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
419 SourceLocation DSStart = Tok.getLocation();
420 // decl-specifier-seq:
421 // Parse the common declaration-specifiers piece.
422 DeclSpec DS;
423 ParseDeclarationSpecifiers(DS);
424
425 if (Tok.is(tok::semi)) {
426 ConsumeToken();
427 // C++ 9.2p7: The member-declarator-list can be omitted only after a
428 // class-specifier or an enum-specifier or in a friend declaration.
429 // FIXME: Friend declarations.
430 switch (DS.getTypeSpecType()) {
431 case DeclSpec::TST_struct:
432 case DeclSpec::TST_union:
433 case DeclSpec::TST_class:
434 case DeclSpec::TST_enum:
435 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
436 default:
437 Diag(DSStart, diag::err_no_declarators);
438 return 0;
439 }
440 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000441
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000442 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000443
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000444 if (Tok.isNot(tok::colon)) {
445 // Parse the first declarator.
446 ParseDeclarator(DeclaratorInfo);
447 // Error parsing the declarator?
448 if (DeclaratorInfo.getIdentifier() == 0) {
449 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000450 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000451 if (Tok.is(tok::semi))
452 ConsumeToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000453 return 0;
454 }
455
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000456 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000457 if (Tok.is(tok::l_brace)
458 || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000459 if (!DeclaratorInfo.isFunctionDeclarator()) {
460 Diag(Tok, diag::err_func_def_no_params);
461 ConsumeBrace();
462 SkipUntil(tok::r_brace, true);
463 return 0;
464 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000465
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000466 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
467 Diag(Tok, diag::err_function_declared_typedef);
468 // This recovery skips the entire function body. It would be nice
469 // to simply call ParseCXXInlineMethodDef() below, however Sema
470 // assumes the declarator represents a function, not a typedef.
471 ConsumeBrace();
472 SkipUntil(tok::r_brace, true);
473 return 0;
474 }
475
476 return ParseCXXInlineMethodDef(AS, DeclaratorInfo);
477 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000478 }
479
480 // member-declarator-list:
481 // member-declarator
482 // member-declarator-list ',' member-declarator
483
484 DeclTy *LastDeclInGroup = 0;
485 ExprTy *BitfieldSize = 0;
486 ExprTy *Init = 0;
487
488 while (1) {
489
490 // member-declarator:
491 // declarator pure-specifier[opt]
492 // declarator constant-initializer[opt]
493 // identifier[opt] ':' constant-expression
494
495 if (Tok.is(tok::colon)) {
496 ConsumeToken();
497 ExprResult Res = ParseConstantExpression();
498 if (Res.isInvalid)
499 SkipUntil(tok::comma, true, true);
500 else
501 BitfieldSize = Res.Val;
502 }
503
504 // pure-specifier:
505 // '= 0'
506 //
507 // constant-initializer:
508 // '=' constant-expression
509
510 if (Tok.is(tok::equal)) {
511 ConsumeToken();
512 ExprResult Res = ParseInitializer();
513 if (Res.isInvalid)
514 SkipUntil(tok::comma, true, true);
515 else
516 Init = Res.Val;
517 }
518
519 // If attributes exist after the declarator, parse them.
520 if (Tok.is(tok::kw___attribute))
521 DeclaratorInfo.AddAttributes(ParseAttributes());
522
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000523 // NOTE: If Sema is the Action module and declarator is an instance field,
524 // this call will *not* return the created decl; LastDeclInGroup will be
525 // returned instead.
526 // See Sema::ActOnCXXMemberDeclarator for details.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000527 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
528 DeclaratorInfo,
529 BitfieldSize, Init,
530 LastDeclInGroup);
531
532 // If we don't have a comma, it is either the end of the list (a ';')
533 // or an error, bail out.
534 if (Tok.isNot(tok::comma))
535 break;
536
537 // Consume the comma.
538 ConsumeToken();
539
540 // Parse the next declarator.
541 DeclaratorInfo.clear();
542 BitfieldSize = Init = 0;
543
544 // Attributes are only allowed on the second declarator.
545 if (Tok.is(tok::kw___attribute))
546 DeclaratorInfo.AddAttributes(ParseAttributes());
547
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000548 if (Tok.isNot(tok::colon))
549 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000550 }
551
552 if (Tok.is(tok::semi)) {
553 ConsumeToken();
554 // Reverse the chain list.
555 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
556 }
557
558 Diag(Tok, diag::err_expected_semi_decl_list);
559 // Skip to end of block or statement
560 SkipUntil(tok::r_brace, true, true);
561 if (Tok.is(tok::semi))
562 ConsumeToken();
563 return 0;
564}
565
566/// ParseCXXMemberSpecification - Parse the class definition.
567///
568/// member-specification:
569/// member-declaration member-specification[opt]
570/// access-specifier ':' member-specification[opt]
571///
572void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
573 unsigned TagType, DeclTy *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000574 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000575 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000576 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000577
578 SourceLocation LBraceLoc = ConsumeBrace();
579
580 if (!CurScope->isCXXClassScope() && // Not about to define a nested class.
581 CurScope->isInCXXInlineMethodScope()) {
582 // We will define a local class of an inline method.
583 // Push a new LexedMethodsForTopClass for its inline methods.
584 PushTopClassStack();
585 }
586
587 // Enter a scope for the class.
588 EnterScope(Scope::CXXClassScope|Scope::DeclScope);
589
590 Actions.ActOnStartCXXClassDef(CurScope, TagDecl, LBraceLoc);
591
592 // C++ 11p3: Members of a class defined with the keyword class are private
593 // by default. Members of a class defined with the keywords struct or union
594 // are public by default.
595 AccessSpecifier CurAS;
596 if (TagType == DeclSpec::TST_class)
597 CurAS = AS_private;
598 else
599 CurAS = AS_public;
600
601 // While we still have something to read, read the member-declarations.
602 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
603 // Each iteration of this loop reads one member-declaration.
604
605 // Check for extraneous top-level semicolon.
606 if (Tok.is(tok::semi)) {
607 Diag(Tok, diag::ext_extra_struct_semi);
608 ConsumeToken();
609 continue;
610 }
611
612 AccessSpecifier AS = getAccessSpecifierIfPresent();
613 if (AS != AS_none) {
614 // Current token is a C++ access specifier.
615 CurAS = AS;
616 ConsumeToken();
617 ExpectAndConsume(tok::colon, diag::err_expected_colon);
618 continue;
619 }
620
621 // Parse all the comma separated declarators.
622 ParseCXXClassMemberDeclaration(CurAS);
623 }
624
625 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
626
627 AttributeList *AttrList = 0;
628 // If attributes exist after class contents, parse them.
629 if (Tok.is(tok::kw___attribute))
630 AttrList = ParseAttributes(); // FIXME: where should I put them?
631
632 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
633 LBraceLoc, RBraceLoc);
634
635 // C++ 9.2p2: Within the class member-specification, the class is regarded as
636 // complete within function bodies, default arguments,
637 // exception-specifications, and constructor ctor-initializers (including
638 // such things in nested classes).
639 //
640 // FIXME: Only function bodies are parsed correctly, fix the rest.
641 if (!CurScope->getParent()->isCXXClassScope()) {
642 // We are not inside a nested class. This class and its nested classes
643 // are complete and we can parse the lexed inline method definitions.
644 ParseLexedMethodDefs();
645
646 // For a local class of inline method, pop the LexedMethodsForTopClass that
647 // was previously pushed.
648
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000649 assert((CurScope->isInCXXInlineMethodScope() ||
650 TopClassStacks.size() == 1) &&
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000651 "MethodLexers not getting popped properly!");
652 if (CurScope->isInCXXInlineMethodScope())
653 PopTopClassStack();
654 }
655
656 // Leave the class scope.
657 ExitScope();
658
Argyrios Kyrtzidis5b7f0c82008-08-09 00:39:29 +0000659 Actions.ActOnFinishCXXClassDef(TagDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000660}
Douglas Gregor7ad83902008-11-05 04:29:56 +0000661
662/// ParseConstructorInitializer - Parse a C++ constructor initializer,
663/// which explicitly initializes the members or base classes of a
664/// class (C++ [class.base.init]). For example, the three initializers
665/// after the ':' in the Derived constructor below:
666///
667/// @code
668/// class Base { };
669/// class Derived : Base {
670/// int x;
671/// float f;
672/// public:
673/// Derived(float f) : Base(), x(17), f(f) { }
674/// };
675/// @endcode
676///
677/// [C++] ctor-initializer:
678/// ':' mem-initializer-list
679///
680/// [C++] mem-initializer-list:
681/// mem-initializer
682/// mem-initializer , mem-initializer-list
683void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) {
684 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
685
686 SourceLocation ColonLoc = ConsumeToken();
687
688 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
689
690 do {
691 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
692 if (!MemInit.isInvalid)
693 MemInitializers.push_back(MemInit.Val);
694
695 if (Tok.is(tok::comma))
696 ConsumeToken();
697 else if (Tok.is(tok::l_brace))
698 break;
699 else {
700 // Skip over garbage, until we get to '{'. Don't eat the '{'.
701 SkipUntil(tok::l_brace, true, true);
702 break;
703 }
704 } while (true);
705
706 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
707 &MemInitializers[0], MemInitializers.size());
708}
709
710/// ParseMemInitializer - Parse a C++ member initializer, which is
711/// part of a constructor initializer that explicitly initializes one
712/// member or base class (C++ [class.base.init]). See
713/// ParseConstructorInitializer for an example.
714///
715/// [C++] mem-initializer:
716/// mem-initializer-id '(' expression-list[opt] ')'
717///
718/// [C++] mem-initializer-id:
719/// '::'[opt] nested-name-specifier[opt] class-name
720/// identifier
721Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) {
722 // FIXME: parse '::'[opt] nested-name-specifier[opt]
723
724 if (Tok.isNot(tok::identifier)) {
725 Diag(Tok.getLocation(), diag::err_expected_member_or_base_name);
726 return true;
727 }
728
729 // Get the identifier. This may be a member name or a class name,
730 // but we'll let the semantic analysis determine which it is.
731 IdentifierInfo *II = Tok.getIdentifierInfo();
732 SourceLocation IdLoc = ConsumeToken();
733
734 // Parse the '('.
735 if (Tok.isNot(tok::l_paren)) {
736 Diag(Tok.getLocation(), diag::err_expected_lparen);
737 return true;
738 }
739 SourceLocation LParenLoc = ConsumeParen();
740
741 // Parse the optional expression-list.
742 ExprListTy ArgExprs;
743 CommaLocsTy CommaLocs;
744 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
745 SkipUntil(tok::r_paren);
746 return true;
747 }
748
749 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
750
751 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
752 LParenLoc, &ArgExprs[0], ArgExprs.size(),
753 &CommaLocs[0], RParenLoc);
754}