blob: 752d552f11d1c760979590ecc0e238cf456003ca [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///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000141Parser::TypeTy *Parser::ParseClassName(const CXXScopeSpec *SS) {
Douglas Gregor42a552f2008-11-05 20:51:48 +0000142 // 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.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000150 TypeTy *Type = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope, SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000151 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
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000219 // Parse the (optional) nested-name-specifier.
220 CXXScopeSpec SS;
221 if (isTokenCXXScopeSpecifier()) {
222 ParseCXXScopeSpecifier(SS);
223 if (Tok.isNot(tok::identifier))
224 Diag(Tok, diag::err_expected_ident);
225 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000226
227 // Parse the (optional) class name.
228 // FIXME: Alternatively, parse a simple-template-id.
229 IdentifierInfo *Name = 0;
230 SourceLocation NameLoc;
231 if (Tok.is(tok::identifier)) {
232 Name = Tok.getIdentifierInfo();
233 NameLoc = ConsumeToken();
234 }
235
236 // There are three options here. If we have 'struct foo;', then
237 // this is a forward declaration. If we have 'struct foo {...' or
238 // 'struct fo :...' then this is a definition. Otherwise we have
239 // something like 'struct foo xyz', a reference.
240 Action::TagKind TK;
241 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
242 TK = Action::TK_Definition;
243 else if (Tok.is(tok::semi))
244 TK = Action::TK_Declaration;
245 else
246 TK = Action::TK_Reference;
247
248 if (!Name && TK != Action::TK_Definition) {
249 // We have a declaration or reference to an anonymous class.
250 Diag(StartLoc, diag::err_anon_type_definition,
251 DeclSpec::getSpecifierName(TagType));
252
253 // Skip the rest of this declarator, up until the comma or semicolon.
254 SkipUntil(tok::comma, true);
255 return;
256 }
257
258 // Parse the tag portion of this.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000259 DeclTy *TagDecl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name,
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000260 NameLoc, Attr);
261
262 // Parse the optional base clause (C++ only).
263 if (getLang().CPlusPlus && Tok.is(tok::colon)) {
264 ParseBaseClause(TagDecl);
265 }
266
267 // If there is a body, parse it and inform the actions module.
268 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000269 if (getLang().CPlusPlus)
270 ParseCXXMemberSpecification(StartLoc, TagType, TagDecl);
271 else
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000272 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000273 else if (TK == Action::TK_Definition) {
274 // FIXME: Complain that we have a base-specifier list but no
275 // definition.
276 Diag(Tok.getLocation(), diag::err_expected_lbrace);
277 }
278
279 const char *PrevSpec = 0;
280 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
281 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
282}
283
284/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
285///
286/// base-clause : [C++ class.derived]
287/// ':' base-specifier-list
288/// base-specifier-list:
289/// base-specifier '...'[opt]
290/// base-specifier-list ',' base-specifier '...'[opt]
291void Parser::ParseBaseClause(DeclTy *ClassDecl)
292{
293 assert(Tok.is(tok::colon) && "Not a base clause");
294 ConsumeToken();
295
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000296 // Build up an array of parsed base specifiers.
297 llvm::SmallVector<BaseTy *, 8> BaseInfo;
298
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000299 while (true) {
300 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000301 BaseResult Result = ParseBaseSpecifier(ClassDecl);
302 if (Result.isInvalid) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000303 // Skip the rest of this base specifier, up until the comma or
304 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000305 SkipUntil(tok::comma, tok::l_brace, true, true);
306 } else {
307 // Add this to our array of base specifiers.
308 BaseInfo.push_back(Result.Val);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000309 }
310
311 // If the next token is a comma, consume it and keep reading
312 // base-specifiers.
313 if (Tok.isNot(tok::comma)) break;
314
315 // Consume the comma.
316 ConsumeToken();
317 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000318
319 // Attach the base specifiers
320 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000321}
322
323/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
324/// one entry in the base class list of a class specifier, for example:
325/// class foo : public bar, virtual private baz {
326/// 'public bar' and 'virtual private baz' are each base-specifiers.
327///
328/// base-specifier: [C++ class.derived]
329/// ::[opt] nested-name-specifier[opt] class-name
330/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
331/// class-name
332/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
333/// class-name
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000334Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000335{
336 bool IsVirtual = false;
337 SourceLocation StartLoc = Tok.getLocation();
338
339 // Parse the 'virtual' keyword.
340 if (Tok.is(tok::kw_virtual)) {
341 ConsumeToken();
342 IsVirtual = true;
343 }
344
345 // Parse an (optional) access specifier.
346 AccessSpecifier Access = getAccessSpecifierIfPresent();
347 if (Access)
348 ConsumeToken();
349
350 // Parse the 'virtual' keyword (again!), in case it came after the
351 // access specifier.
352 if (Tok.is(tok::kw_virtual)) {
353 SourceLocation VirtualLoc = ConsumeToken();
354 if (IsVirtual) {
355 // Complain about duplicate 'virtual'
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000356 Diag(VirtualLoc, diag::err_dup_virtual,
357 SourceRange(VirtualLoc, VirtualLoc));
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000358 }
359
360 IsVirtual = true;
361 }
362
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000363 // Parse optional '::' and optional nested-name-specifier.
364 CXXScopeSpec SS;
365 if (isTokenCXXScopeSpecifier())
366 ParseCXXScopeSpecifier(SS);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000367
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000368 // The location of the base class itself.
369 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000370
371 // Parse the class-name.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000372 TypeTy *BaseType = ParseClassName(&SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000373 if (!BaseType)
374 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000375
376 // Find the complete source range for the base-specifier.
377 SourceRange Range(StartLoc, BaseLoc);
378
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000379 // Notify semantic analysis that we have parsed a complete
380 // base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000381 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, BaseType,
382 BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000383}
384
385/// getAccessSpecifierIfPresent - Determine whether the next token is
386/// a C++ access-specifier.
387///
388/// access-specifier: [C++ class.derived]
389/// 'private'
390/// 'protected'
391/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000392AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000393{
394 switch (Tok.getKind()) {
395 default: return AS_none;
396 case tok::kw_private: return AS_private;
397 case tok::kw_protected: return AS_protected;
398 case tok::kw_public: return AS_public;
399 }
400}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000401
402/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
403///
404/// member-declaration:
405/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
406/// function-definition ';'[opt]
407/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
408/// using-declaration [TODO]
409/// [C++0x] static_assert-declaration [TODO]
410/// template-declaration [TODO]
411///
412/// member-declarator-list:
413/// member-declarator
414/// member-declarator-list ',' member-declarator
415///
416/// member-declarator:
417/// declarator pure-specifier[opt]
418/// declarator constant-initializer[opt]
419/// identifier[opt] ':' constant-expression
420///
421/// pure-specifier: [TODO]
422/// '= 0'
423///
424/// constant-initializer:
425/// '=' constant-expression
426///
427Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
428 SourceLocation DSStart = Tok.getLocation();
429 // decl-specifier-seq:
430 // Parse the common declaration-specifiers piece.
431 DeclSpec DS;
432 ParseDeclarationSpecifiers(DS);
433
434 if (Tok.is(tok::semi)) {
435 ConsumeToken();
436 // C++ 9.2p7: The member-declarator-list can be omitted only after a
437 // class-specifier or an enum-specifier or in a friend declaration.
438 // FIXME: Friend declarations.
439 switch (DS.getTypeSpecType()) {
440 case DeclSpec::TST_struct:
441 case DeclSpec::TST_union:
442 case DeclSpec::TST_class:
443 case DeclSpec::TST_enum:
444 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
445 default:
446 Diag(DSStart, diag::err_no_declarators);
447 return 0;
448 }
449 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000450
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000451 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000452
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000453 if (Tok.isNot(tok::colon)) {
454 // Parse the first declarator.
455 ParseDeclarator(DeclaratorInfo);
456 // Error parsing the declarator?
457 if (DeclaratorInfo.getIdentifier() == 0) {
458 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000459 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000460 if (Tok.is(tok::semi))
461 ConsumeToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000462 return 0;
463 }
464
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000465 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000466 if (Tok.is(tok::l_brace)
467 || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000468 if (!DeclaratorInfo.isFunctionDeclarator()) {
469 Diag(Tok, diag::err_func_def_no_params);
470 ConsumeBrace();
471 SkipUntil(tok::r_brace, true);
472 return 0;
473 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000474
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000475 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
476 Diag(Tok, diag::err_function_declared_typedef);
477 // This recovery skips the entire function body. It would be nice
478 // to simply call ParseCXXInlineMethodDef() below, however Sema
479 // assumes the declarator represents a function, not a typedef.
480 ConsumeBrace();
481 SkipUntil(tok::r_brace, true);
482 return 0;
483 }
484
485 return ParseCXXInlineMethodDef(AS, DeclaratorInfo);
486 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000487 }
488
489 // member-declarator-list:
490 // member-declarator
491 // member-declarator-list ',' member-declarator
492
493 DeclTy *LastDeclInGroup = 0;
494 ExprTy *BitfieldSize = 0;
495 ExprTy *Init = 0;
496
497 while (1) {
498
499 // member-declarator:
500 // declarator pure-specifier[opt]
501 // declarator constant-initializer[opt]
502 // identifier[opt] ':' constant-expression
503
504 if (Tok.is(tok::colon)) {
505 ConsumeToken();
506 ExprResult Res = ParseConstantExpression();
507 if (Res.isInvalid)
508 SkipUntil(tok::comma, true, true);
509 else
510 BitfieldSize = Res.Val;
511 }
512
513 // pure-specifier:
514 // '= 0'
515 //
516 // constant-initializer:
517 // '=' constant-expression
518
519 if (Tok.is(tok::equal)) {
520 ConsumeToken();
521 ExprResult Res = ParseInitializer();
522 if (Res.isInvalid)
523 SkipUntil(tok::comma, true, true);
524 else
525 Init = Res.Val;
526 }
527
528 // If attributes exist after the declarator, parse them.
529 if (Tok.is(tok::kw___attribute))
530 DeclaratorInfo.AddAttributes(ParseAttributes());
531
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000532 // NOTE: If Sema is the Action module and declarator is an instance field,
533 // this call will *not* return the created decl; LastDeclInGroup will be
534 // returned instead.
535 // See Sema::ActOnCXXMemberDeclarator for details.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000536 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
537 DeclaratorInfo,
538 BitfieldSize, Init,
539 LastDeclInGroup);
540
541 // If we don't have a comma, it is either the end of the list (a ';')
542 // or an error, bail out.
543 if (Tok.isNot(tok::comma))
544 break;
545
546 // Consume the comma.
547 ConsumeToken();
548
549 // Parse the next declarator.
550 DeclaratorInfo.clear();
551 BitfieldSize = Init = 0;
552
553 // Attributes are only allowed on the second declarator.
554 if (Tok.is(tok::kw___attribute))
555 DeclaratorInfo.AddAttributes(ParseAttributes());
556
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000557 if (Tok.isNot(tok::colon))
558 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000559 }
560
561 if (Tok.is(tok::semi)) {
562 ConsumeToken();
563 // Reverse the chain list.
564 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
565 }
566
567 Diag(Tok, diag::err_expected_semi_decl_list);
568 // Skip to end of block or statement
569 SkipUntil(tok::r_brace, true, true);
570 if (Tok.is(tok::semi))
571 ConsumeToken();
572 return 0;
573}
574
575/// ParseCXXMemberSpecification - Parse the class definition.
576///
577/// member-specification:
578/// member-declaration member-specification[opt]
579/// access-specifier ':' member-specification[opt]
580///
581void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
582 unsigned TagType, DeclTy *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000583 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000584 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000585 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000586
587 SourceLocation LBraceLoc = ConsumeBrace();
588
589 if (!CurScope->isCXXClassScope() && // Not about to define a nested class.
590 CurScope->isInCXXInlineMethodScope()) {
591 // We will define a local class of an inline method.
592 // Push a new LexedMethodsForTopClass for its inline methods.
593 PushTopClassStack();
594 }
595
596 // Enter a scope for the class.
597 EnterScope(Scope::CXXClassScope|Scope::DeclScope);
598
599 Actions.ActOnStartCXXClassDef(CurScope, TagDecl, LBraceLoc);
600
601 // C++ 11p3: Members of a class defined with the keyword class are private
602 // by default. Members of a class defined with the keywords struct or union
603 // are public by default.
604 AccessSpecifier CurAS;
605 if (TagType == DeclSpec::TST_class)
606 CurAS = AS_private;
607 else
608 CurAS = AS_public;
609
610 // While we still have something to read, read the member-declarations.
611 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
612 // Each iteration of this loop reads one member-declaration.
613
614 // Check for extraneous top-level semicolon.
615 if (Tok.is(tok::semi)) {
616 Diag(Tok, diag::ext_extra_struct_semi);
617 ConsumeToken();
618 continue;
619 }
620
621 AccessSpecifier AS = getAccessSpecifierIfPresent();
622 if (AS != AS_none) {
623 // Current token is a C++ access specifier.
624 CurAS = AS;
625 ConsumeToken();
626 ExpectAndConsume(tok::colon, diag::err_expected_colon);
627 continue;
628 }
629
630 // Parse all the comma separated declarators.
631 ParseCXXClassMemberDeclaration(CurAS);
632 }
633
634 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
635
636 AttributeList *AttrList = 0;
637 // If attributes exist after class contents, parse them.
638 if (Tok.is(tok::kw___attribute))
639 AttrList = ParseAttributes(); // FIXME: where should I put them?
640
641 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
642 LBraceLoc, RBraceLoc);
643
644 // C++ 9.2p2: Within the class member-specification, the class is regarded as
645 // complete within function bodies, default arguments,
646 // exception-specifications, and constructor ctor-initializers (including
647 // such things in nested classes).
648 //
649 // FIXME: Only function bodies are parsed correctly, fix the rest.
650 if (!CurScope->getParent()->isCXXClassScope()) {
651 // We are not inside a nested class. This class and its nested classes
652 // are complete and we can parse the lexed inline method definitions.
653 ParseLexedMethodDefs();
654
655 // For a local class of inline method, pop the LexedMethodsForTopClass that
656 // was previously pushed.
657
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000658 assert((CurScope->isInCXXInlineMethodScope() ||
659 TopClassStacks.size() == 1) &&
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000660 "MethodLexers not getting popped properly!");
661 if (CurScope->isInCXXInlineMethodScope())
662 PopTopClassStack();
663 }
664
665 // Leave the class scope.
666 ExitScope();
667
Argyrios Kyrtzidis5b7f0c82008-08-09 00:39:29 +0000668 Actions.ActOnFinishCXXClassDef(TagDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000669}
Douglas Gregor7ad83902008-11-05 04:29:56 +0000670
671/// ParseConstructorInitializer - Parse a C++ constructor initializer,
672/// which explicitly initializes the members or base classes of a
673/// class (C++ [class.base.init]). For example, the three initializers
674/// after the ':' in the Derived constructor below:
675///
676/// @code
677/// class Base { };
678/// class Derived : Base {
679/// int x;
680/// float f;
681/// public:
682/// Derived(float f) : Base(), x(17), f(f) { }
683/// };
684/// @endcode
685///
686/// [C++] ctor-initializer:
687/// ':' mem-initializer-list
688///
689/// [C++] mem-initializer-list:
690/// mem-initializer
691/// mem-initializer , mem-initializer-list
692void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) {
693 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
694
695 SourceLocation ColonLoc = ConsumeToken();
696
697 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
698
699 do {
700 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
701 if (!MemInit.isInvalid)
702 MemInitializers.push_back(MemInit.Val);
703
704 if (Tok.is(tok::comma))
705 ConsumeToken();
706 else if (Tok.is(tok::l_brace))
707 break;
708 else {
709 // Skip over garbage, until we get to '{'. Don't eat the '{'.
710 SkipUntil(tok::l_brace, true, true);
711 break;
712 }
713 } while (true);
714
715 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
716 &MemInitializers[0], MemInitializers.size());
717}
718
719/// ParseMemInitializer - Parse a C++ member initializer, which is
720/// part of a constructor initializer that explicitly initializes one
721/// member or base class (C++ [class.base.init]). See
722/// ParseConstructorInitializer for an example.
723///
724/// [C++] mem-initializer:
725/// mem-initializer-id '(' expression-list[opt] ')'
726///
727/// [C++] mem-initializer-id:
728/// '::'[opt] nested-name-specifier[opt] class-name
729/// identifier
730Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) {
731 // FIXME: parse '::'[opt] nested-name-specifier[opt]
732
733 if (Tok.isNot(tok::identifier)) {
734 Diag(Tok.getLocation(), diag::err_expected_member_or_base_name);
735 return true;
736 }
737
738 // Get the identifier. This may be a member name or a class name,
739 // but we'll let the semantic analysis determine which it is.
740 IdentifierInfo *II = Tok.getIdentifierInfo();
741 SourceLocation IdLoc = ConsumeToken();
742
743 // Parse the '('.
744 if (Tok.isNot(tok::l_paren)) {
745 Diag(Tok.getLocation(), diag::err_expected_lparen);
746 return true;
747 }
748 SourceLocation LParenLoc = ConsumeParen();
749
750 // Parse the optional expression-list.
751 ExprListTy ArgExprs;
752 CommaLocsTy CommaLocs;
753 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
754 SkipUntil(tok::r_paren);
755 return true;
756 }
757
758 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
759
760 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
761 LParenLoc, &ArgExprs[0], ArgExprs.size(),
762 &CommaLocs[0], RParenLoc);
763}