blob: 560edc131b05c2db36d56e7dede5a8c273eb18ac [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 {
Chris Lattner1ab3b962008-11-18 07:48:38 +000086 Diag(Tok, Ident ? diag::err_expected_lbrace :
87 diag::err_expected_ident_lbrace);
Chris Lattner8f08cb72007-08-25 06:57:03 +000088 }
89
90 return 0;
91}
Chris Lattnerc6fdc342008-01-12 07:05:38 +000092
93/// ParseLinkage - We know that the current token is a string_literal
94/// and just before that, that extern was seen.
95///
96/// linkage-specification: [C++ 7.5p2: dcl.link]
97/// 'extern' string-literal '{' declaration-seq[opt] '}'
98/// 'extern' string-literal declaration
99///
100Parser::DeclTy *Parser::ParseLinkage(unsigned Context) {
101 assert(Tok.is(tok::string_literal) && "Not a stringliteral!");
102 llvm::SmallVector<char, 8> LangBuffer;
103 // LangBuffer is guaranteed to be big enough.
104 LangBuffer.resize(Tok.getLength());
105 const char *LangBufPtr = &LangBuffer[0];
106 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
107
108 SourceLocation Loc = ConsumeStringToken();
109 DeclTy *D = 0;
110 SourceLocation LBrace, RBrace;
111
112 if (Tok.isNot(tok::l_brace)) {
113 D = ParseDeclaration(Context);
114 } else {
115 LBrace = ConsumeBrace();
116 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
117 // FIXME capture the decls.
118 D = ParseExternalDeclaration();
119 }
120
121 RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
122 }
123
124 if (!D)
125 return 0;
126
127 return Actions.ActOnLinkageSpec(Loc, LBrace, RBrace, LangBufPtr, StrSize, D);
128}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000129
Douglas Gregor42a552f2008-11-05 20:51:48 +0000130/// ParseClassName - Parse a C++ class-name, which names a class. Note
131/// that we only check that the result names a type; semantic analysis
132/// will need to verify that the type names a class. The result is
133/// either a type or NULL, dependending on whether a type name was
134/// found.
135///
136/// class-name: [C++ 9.1]
137/// identifier
138/// template-id [TODO]
139///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000140Parser::TypeTy *Parser::ParseClassName(const CXXScopeSpec *SS) {
Douglas Gregor42a552f2008-11-05 20:51:48 +0000141 // Parse the class-name.
142 // FIXME: Alternatively, parse a simple-template-id.
143 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000144 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000145 return 0;
146 }
147
148 // We have an identifier; check whether it is actually a type.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000149 TypeTy *Type = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope, SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000150 if (!Type) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000151 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000152 return 0;
153 }
154
155 // Consume the identifier.
156 ConsumeToken();
157
158 return Type;
159}
160
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000161/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
162/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
163/// until we reach the start of a definition or see a token that
164/// cannot start a definition.
165///
166/// class-specifier: [C++ class]
167/// class-head '{' member-specification[opt] '}'
168/// class-head '{' member-specification[opt] '}' attributes[opt]
169/// class-head:
170/// class-key identifier[opt] base-clause[opt]
171/// class-key nested-name-specifier identifier base-clause[opt]
172/// class-key nested-name-specifier[opt] simple-template-id
173/// base-clause[opt]
174/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
175/// [GNU] class-key attributes[opt] nested-name-specifier
176/// identifier base-clause[opt]
177/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
178/// simple-template-id base-clause[opt]
179/// class-key:
180/// 'class'
181/// 'struct'
182/// 'union'
183///
184/// elaborated-type-specifier: [C++ dcl.type.elab]
185/// class-key ::[opt] nested-name-specifier[opt] identifier
186/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
187/// simple-template-id
188///
189/// Note that the C++ class-specifier and elaborated-type-specifier,
190/// together, subsume the C99 struct-or-union-specifier:
191///
192/// struct-or-union-specifier: [C99 6.7.2.1]
193/// struct-or-union identifier[opt] '{' struct-contents '}'
194/// struct-or-union identifier
195/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
196/// '}' attributes[opt]
197/// [GNU] struct-or-union attributes[opt] identifier
198/// struct-or-union:
199/// 'struct'
200/// 'union'
201void Parser::ParseClassSpecifier(DeclSpec &DS) {
202 assert((Tok.is(tok::kw_class) ||
203 Tok.is(tok::kw_struct) ||
204 Tok.is(tok::kw_union)) &&
205 "Not a class specifier");
206 DeclSpec::TST TagType =
207 Tok.is(tok::kw_class) ? DeclSpec::TST_class :
208 Tok.is(tok::kw_struct) ? DeclSpec::TST_struct :
209 DeclSpec::TST_union;
210
211 SourceLocation StartLoc = ConsumeToken();
212
213 AttributeList *Attr = 0;
214 // If attributes exist after tag, parse them.
215 if (Tok.is(tok::kw___attribute))
216 Attr = ParseAttributes();
217
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000218 // Parse the (optional) nested-name-specifier.
219 CXXScopeSpec SS;
220 if (isTokenCXXScopeSpecifier()) {
221 ParseCXXScopeSpecifier(SS);
222 if (Tok.isNot(tok::identifier))
223 Diag(Tok, diag::err_expected_ident);
224 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000225
226 // Parse the (optional) class name.
227 // FIXME: Alternatively, parse a simple-template-id.
228 IdentifierInfo *Name = 0;
229 SourceLocation NameLoc;
230 if (Tok.is(tok::identifier)) {
231 Name = Tok.getIdentifierInfo();
232 NameLoc = ConsumeToken();
233 }
234
235 // There are three options here. If we have 'struct foo;', then
236 // this is a forward declaration. If we have 'struct foo {...' or
237 // 'struct fo :...' then this is a definition. Otherwise we have
238 // something like 'struct foo xyz', a reference.
239 Action::TagKind TK;
240 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
241 TK = Action::TK_Definition;
242 else if (Tok.is(tok::semi))
243 TK = Action::TK_Declaration;
244 else
245 TK = Action::TK_Reference;
246
247 if (!Name && TK != Action::TK_Definition) {
248 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000249 Diag(StartLoc, diag::err_anon_type_definition)
250 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000251
252 // Skip the rest of this declarator, up until the comma or semicolon.
253 SkipUntil(tok::comma, true);
254 return;
255 }
256
257 // Parse the tag portion of this.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000258 DeclTy *TagDecl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name,
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000259 NameLoc, Attr);
260
261 // Parse the optional base clause (C++ only).
262 if (getLang().CPlusPlus && Tok.is(tok::colon)) {
263 ParseBaseClause(TagDecl);
264 }
265
266 // If there is a body, parse it and inform the actions module.
267 if (Tok.is(tok::l_brace))
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000268 if (getLang().CPlusPlus)
269 ParseCXXMemberSpecification(StartLoc, TagType, TagDecl);
270 else
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000271 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000272 else if (TK == Action::TK_Definition) {
273 // FIXME: Complain that we have a base-specifier list but no
274 // definition.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000275 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000276 }
277
278 const char *PrevSpec = 0;
279 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
Chris Lattner1ab3b962008-11-18 07:48:38 +0000280 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000281}
282
283/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
284///
285/// base-clause : [C++ class.derived]
286/// ':' base-specifier-list
287/// base-specifier-list:
288/// base-specifier '...'[opt]
289/// base-specifier-list ',' base-specifier '...'[opt]
290void Parser::ParseBaseClause(DeclTy *ClassDecl)
291{
292 assert(Tok.is(tok::colon) && "Not a base clause");
293 ConsumeToken();
294
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000295 // Build up an array of parsed base specifiers.
296 llvm::SmallVector<BaseTy *, 8> BaseInfo;
297
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000298 while (true) {
299 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000300 BaseResult Result = ParseBaseSpecifier(ClassDecl);
301 if (Result.isInvalid) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000302 // Skip the rest of this base specifier, up until the comma or
303 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000304 SkipUntil(tok::comma, tok::l_brace, true, true);
305 } else {
306 // Add this to our array of base specifiers.
307 BaseInfo.push_back(Result.Val);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000308 }
309
310 // If the next token is a comma, consume it and keep reading
311 // base-specifiers.
312 if (Tok.isNot(tok::comma)) break;
313
314 // Consume the comma.
315 ConsumeToken();
316 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000317
318 // Attach the base specifiers
319 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000320}
321
322/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
323/// one entry in the base class list of a class specifier, for example:
324/// class foo : public bar, virtual private baz {
325/// 'public bar' and 'virtual private baz' are each base-specifiers.
326///
327/// base-specifier: [C++ class.derived]
328/// ::[opt] nested-name-specifier[opt] class-name
329/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
330/// class-name
331/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
332/// class-name
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000333Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000334{
335 bool IsVirtual = false;
336 SourceLocation StartLoc = Tok.getLocation();
337
338 // Parse the 'virtual' keyword.
339 if (Tok.is(tok::kw_virtual)) {
340 ConsumeToken();
341 IsVirtual = true;
342 }
343
344 // Parse an (optional) access specifier.
345 AccessSpecifier Access = getAccessSpecifierIfPresent();
346 if (Access)
347 ConsumeToken();
348
349 // Parse the 'virtual' keyword (again!), in case it came after the
350 // access specifier.
351 if (Tok.is(tok::kw_virtual)) {
352 SourceLocation VirtualLoc = ConsumeToken();
353 if (IsVirtual) {
354 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +0000355 Diag(VirtualLoc, diag::err_dup_virtual)
356 << SourceRange(VirtualLoc, VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000357 }
358
359 IsVirtual = true;
360 }
361
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000362 // Parse optional '::' and optional nested-name-specifier.
363 CXXScopeSpec SS;
364 if (isTokenCXXScopeSpecifier())
365 ParseCXXScopeSpecifier(SS);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000366
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000367 // The location of the base class itself.
368 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +0000369
370 // Parse the class-name.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000371 TypeTy *BaseType = ParseClassName(&SS);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000372 if (!BaseType)
373 return true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000374
375 // Find the complete source range for the base-specifier.
376 SourceRange Range(StartLoc, BaseLoc);
377
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000378 // Notify semantic analysis that we have parsed a complete
379 // base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +0000380 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, BaseType,
381 BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000382}
383
384/// getAccessSpecifierIfPresent - Determine whether the next token is
385/// a C++ access-specifier.
386///
387/// access-specifier: [C++ class.derived]
388/// 'private'
389/// 'protected'
390/// 'public'
Douglas Gregor1b7f8982008-04-14 00:13:42 +0000391AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000392{
393 switch (Tok.getKind()) {
394 default: return AS_none;
395 case tok::kw_private: return AS_private;
396 case tok::kw_protected: return AS_protected;
397 case tok::kw_public: return AS_public;
398 }
399}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000400
401/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
402///
403/// member-declaration:
404/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
405/// function-definition ';'[opt]
406/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
407/// using-declaration [TODO]
408/// [C++0x] static_assert-declaration [TODO]
409/// template-declaration [TODO]
410///
411/// member-declarator-list:
412/// member-declarator
413/// member-declarator-list ',' member-declarator
414///
415/// member-declarator:
416/// declarator pure-specifier[opt]
417/// declarator constant-initializer[opt]
418/// identifier[opt] ':' constant-expression
419///
420/// pure-specifier: [TODO]
421/// '= 0'
422///
423/// constant-initializer:
424/// '=' constant-expression
425///
426Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
427 SourceLocation DSStart = Tok.getLocation();
428 // decl-specifier-seq:
429 // Parse the common declaration-specifiers piece.
430 DeclSpec DS;
431 ParseDeclarationSpecifiers(DS);
432
433 if (Tok.is(tok::semi)) {
434 ConsumeToken();
435 // C++ 9.2p7: The member-declarator-list can be omitted only after a
436 // class-specifier or an enum-specifier or in a friend declaration.
437 // FIXME: Friend declarations.
438 switch (DS.getTypeSpecType()) {
439 case DeclSpec::TST_struct:
440 case DeclSpec::TST_union:
441 case DeclSpec::TST_class:
442 case DeclSpec::TST_enum:
443 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
444 default:
445 Diag(DSStart, diag::err_no_declarators);
446 return 0;
447 }
448 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000449
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000450 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000451
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000452 if (Tok.isNot(tok::colon)) {
453 // Parse the first declarator.
454 ParseDeclarator(DeclaratorInfo);
455 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +0000456 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000457 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000458 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000459 if (Tok.is(tok::semi))
460 ConsumeToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000461 return 0;
462 }
463
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000464 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +0000465 if (Tok.is(tok::l_brace)
466 || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000467 if (!DeclaratorInfo.isFunctionDeclarator()) {
468 Diag(Tok, diag::err_func_def_no_params);
469 ConsumeBrace();
470 SkipUntil(tok::r_brace, true);
471 return 0;
472 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000473
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000474 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
475 Diag(Tok, diag::err_function_declared_typedef);
476 // This recovery skips the entire function body. It would be nice
477 // to simply call ParseCXXInlineMethodDef() below, however Sema
478 // assumes the declarator represents a function, not a typedef.
479 ConsumeBrace();
480 SkipUntil(tok::r_brace, true);
481 return 0;
482 }
483
484 return ParseCXXInlineMethodDef(AS, DeclaratorInfo);
485 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000486 }
487
488 // member-declarator-list:
489 // member-declarator
490 // member-declarator-list ',' member-declarator
491
492 DeclTy *LastDeclInGroup = 0;
493 ExprTy *BitfieldSize = 0;
494 ExprTy *Init = 0;
495
496 while (1) {
497
498 // member-declarator:
499 // declarator pure-specifier[opt]
500 // declarator constant-initializer[opt]
501 // identifier[opt] ':' constant-expression
502
503 if (Tok.is(tok::colon)) {
504 ConsumeToken();
505 ExprResult Res = ParseConstantExpression();
506 if (Res.isInvalid)
507 SkipUntil(tok::comma, true, true);
508 else
509 BitfieldSize = Res.Val;
510 }
511
512 // pure-specifier:
513 // '= 0'
514 //
515 // constant-initializer:
516 // '=' constant-expression
517
518 if (Tok.is(tok::equal)) {
519 ConsumeToken();
520 ExprResult Res = ParseInitializer();
521 if (Res.isInvalid)
522 SkipUntil(tok::comma, true, true);
523 else
524 Init = Res.Val;
525 }
526
527 // If attributes exist after the declarator, parse them.
528 if (Tok.is(tok::kw___attribute))
529 DeclaratorInfo.AddAttributes(ParseAttributes());
530
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000531 // NOTE: If Sema is the Action module and declarator is an instance field,
532 // this call will *not* return the created decl; LastDeclInGroup will be
533 // returned instead.
534 // See Sema::ActOnCXXMemberDeclarator for details.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000535 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
536 DeclaratorInfo,
537 BitfieldSize, Init,
538 LastDeclInGroup);
539
540 // If we don't have a comma, it is either the end of the list (a ';')
541 // or an error, bail out.
542 if (Tok.isNot(tok::comma))
543 break;
544
545 // Consume the comma.
546 ConsumeToken();
547
548 // Parse the next declarator.
549 DeclaratorInfo.clear();
550 BitfieldSize = Init = 0;
551
552 // Attributes are only allowed on the second declarator.
553 if (Tok.is(tok::kw___attribute))
554 DeclaratorInfo.AddAttributes(ParseAttributes());
555
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +0000556 if (Tok.isNot(tok::colon))
557 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000558 }
559
560 if (Tok.is(tok::semi)) {
561 ConsumeToken();
562 // Reverse the chain list.
563 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
564 }
565
566 Diag(Tok, diag::err_expected_semi_decl_list);
567 // Skip to end of block or statement
568 SkipUntil(tok::r_brace, true, true);
569 if (Tok.is(tok::semi))
570 ConsumeToken();
571 return 0;
572}
573
574/// ParseCXXMemberSpecification - Parse the class definition.
575///
576/// member-specification:
577/// member-declaration member-specification[opt]
578/// access-specifier ':' member-specification[opt]
579///
580void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
581 unsigned TagType, DeclTy *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000582 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000583 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000584 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000585
586 SourceLocation LBraceLoc = ConsumeBrace();
587
588 if (!CurScope->isCXXClassScope() && // Not about to define a nested class.
589 CurScope->isInCXXInlineMethodScope()) {
590 // We will define a local class of an inline method.
591 // Push a new LexedMethodsForTopClass for its inline methods.
592 PushTopClassStack();
593 }
594
595 // Enter a scope for the class.
596 EnterScope(Scope::CXXClassScope|Scope::DeclScope);
597
598 Actions.ActOnStartCXXClassDef(CurScope, TagDecl, LBraceLoc);
599
600 // C++ 11p3: Members of a class defined with the keyword class are private
601 // by default. Members of a class defined with the keywords struct or union
602 // are public by default.
603 AccessSpecifier CurAS;
604 if (TagType == DeclSpec::TST_class)
605 CurAS = AS_private;
606 else
607 CurAS = AS_public;
608
609 // While we still have something to read, read the member-declarations.
610 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
611 // Each iteration of this loop reads one member-declaration.
612
613 // Check for extraneous top-level semicolon.
614 if (Tok.is(tok::semi)) {
615 Diag(Tok, diag::ext_extra_struct_semi);
616 ConsumeToken();
617 continue;
618 }
619
620 AccessSpecifier AS = getAccessSpecifierIfPresent();
621 if (AS != AS_none) {
622 // Current token is a C++ access specifier.
623 CurAS = AS;
624 ConsumeToken();
625 ExpectAndConsume(tok::colon, diag::err_expected_colon);
626 continue;
627 }
628
629 // Parse all the comma separated declarators.
630 ParseCXXClassMemberDeclaration(CurAS);
631 }
632
633 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
634
635 AttributeList *AttrList = 0;
636 // If attributes exist after class contents, parse them.
637 if (Tok.is(tok::kw___attribute))
638 AttrList = ParseAttributes(); // FIXME: where should I put them?
639
640 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
641 LBraceLoc, RBraceLoc);
642
643 // C++ 9.2p2: Within the class member-specification, the class is regarded as
644 // complete within function bodies, default arguments,
645 // exception-specifications, and constructor ctor-initializers (including
646 // such things in nested classes).
647 //
648 // FIXME: Only function bodies are parsed correctly, fix the rest.
649 if (!CurScope->getParent()->isCXXClassScope()) {
650 // We are not inside a nested class. This class and its nested classes
651 // are complete and we can parse the lexed inline method definitions.
652 ParseLexedMethodDefs();
653
654 // For a local class of inline method, pop the LexedMethodsForTopClass that
655 // was previously pushed.
656
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000657 assert((CurScope->isInCXXInlineMethodScope() ||
658 TopClassStacks.size() == 1) &&
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000659 "MethodLexers not getting popped properly!");
660 if (CurScope->isInCXXInlineMethodScope())
661 PopTopClassStack();
662 }
663
664 // Leave the class scope.
665 ExitScope();
666
Argyrios Kyrtzidis5b7f0c82008-08-09 00:39:29 +0000667 Actions.ActOnFinishCXXClassDef(TagDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000668}
Douglas Gregor7ad83902008-11-05 04:29:56 +0000669
670/// ParseConstructorInitializer - Parse a C++ constructor initializer,
671/// which explicitly initializes the members or base classes of a
672/// class (C++ [class.base.init]). For example, the three initializers
673/// after the ':' in the Derived constructor below:
674///
675/// @code
676/// class Base { };
677/// class Derived : Base {
678/// int x;
679/// float f;
680/// public:
681/// Derived(float f) : Base(), x(17), f(f) { }
682/// };
683/// @endcode
684///
685/// [C++] ctor-initializer:
686/// ':' mem-initializer-list
687///
688/// [C++] mem-initializer-list:
689/// mem-initializer
690/// mem-initializer , mem-initializer-list
691void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) {
692 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
693
694 SourceLocation ColonLoc = ConsumeToken();
695
696 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
697
698 do {
699 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
700 if (!MemInit.isInvalid)
701 MemInitializers.push_back(MemInit.Val);
702
703 if (Tok.is(tok::comma))
704 ConsumeToken();
705 else if (Tok.is(tok::l_brace))
706 break;
707 else {
708 // Skip over garbage, until we get to '{'. Don't eat the '{'.
709 SkipUntil(tok::l_brace, true, true);
710 break;
711 }
712 } while (true);
713
714 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
715 &MemInitializers[0], MemInitializers.size());
716}
717
718/// ParseMemInitializer - Parse a C++ member initializer, which is
719/// part of a constructor initializer that explicitly initializes one
720/// member or base class (C++ [class.base.init]). See
721/// ParseConstructorInitializer for an example.
722///
723/// [C++] mem-initializer:
724/// mem-initializer-id '(' expression-list[opt] ')'
725///
726/// [C++] mem-initializer-id:
727/// '::'[opt] nested-name-specifier[opt] class-name
728/// identifier
729Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) {
730 // FIXME: parse '::'[opt] nested-name-specifier[opt]
731
732 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000733 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000734 return true;
735 }
736
737 // Get the identifier. This may be a member name or a class name,
738 // but we'll let the semantic analysis determine which it is.
739 IdentifierInfo *II = Tok.getIdentifierInfo();
740 SourceLocation IdLoc = ConsumeToken();
741
742 // Parse the '('.
743 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000744 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000745 return true;
746 }
747 SourceLocation LParenLoc = ConsumeParen();
748
749 // Parse the optional expression-list.
750 ExprListTy ArgExprs;
751 CommaLocsTy CommaLocs;
752 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
753 SkipUntil(tok::r_paren);
754 return true;
755 }
756
757 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
758
759 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
760 LParenLoc, &ArgExprs[0], ArgExprs.size(),
761 &CommaLocs[0], RParenLoc);
762}