blob: 1b0b811c88f36a3358729699b6fe4a1a1064a632 [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"
Douglas Gregorec93f442008-04-13 21:30:24 +000015#include "clang/Basic/Diagnostic.h"
16#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
76 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
Chris Lattner9c135722007-08-25 18:15:16 +000077 ParseExternalDeclaration();
Chris Lattnerf7b2e552007-08-25 06:57:03 +000078
Argiris Kirtzidis5f21e592008-05-01 21:44:34 +000079 // Leave the namespace scope.
Douglas Gregor95d40792008-12-10 06:34:36 +000080 NamespaceScope.Exit();
Argiris Kirtzidis5f21e592008-05-01 21:44:34 +000081
Chris Lattnerf7b2e552007-08-25 06:57:03 +000082 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000083 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBrace);
84
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000085 return NamespcDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +000086
Chris Lattnerf7b2e552007-08-25 06:57:03 +000087 } else {
Chris Lattnerf006a222008-11-18 07:48:38 +000088 Diag(Tok, Ident ? diag::err_expected_lbrace :
89 diag::err_expected_ident_lbrace);
Chris Lattnerf7b2e552007-08-25 06:57:03 +000090 }
91
92 return 0;
93}
Chris Lattner806a5f52008-01-12 07:05:38 +000094
95/// ParseLinkage - We know that the current token is a string_literal
96/// and just before that, that extern was seen.
97///
98/// linkage-specification: [C++ 7.5p2: dcl.link]
99/// 'extern' string-literal '{' declaration-seq[opt] '}'
100/// 'extern' string-literal declaration
101///
102Parser::DeclTy *Parser::ParseLinkage(unsigned Context) {
Douglas Gregor61818c52008-11-21 16:10:08 +0000103 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattner806a5f52008-01-12 07:05:38 +0000104 llvm::SmallVector<char, 8> LangBuffer;
105 // LangBuffer is guaranteed to be big enough.
106 LangBuffer.resize(Tok.getLength());
107 const char *LangBufPtr = &LangBuffer[0];
108 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
109
110 SourceLocation Loc = ConsumeStringToken();
111 DeclTy *D = 0;
Chris Lattner806a5f52008-01-12 07:05:38 +0000112
113 if (Tok.isNot(tok::l_brace)) {
Douglas Gregor61818c52008-11-21 16:10:08 +0000114 D = ParseDeclarationOrFunctionDefinition();
Douglas Gregorad17e372008-12-16 22:23:02 +0000115 if (D)
116 return Actions.ActOnLinkageSpec(Loc, LangBufPtr, StrSize, D);
Chris Lattner806a5f52008-01-12 07:05:38 +0000117
Douglas Gregorad17e372008-12-16 22:23:02 +0000118 return 0;
119 }
120
121 SourceLocation LBrace = ConsumeBrace();
122 llvm::SmallVector<DeclTy *, 8> InnerDecls;
123 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
124 D = ParseExternalDeclaration();
125 if (D)
126 InnerDecls.push_back(D);
Chris Lattner806a5f52008-01-12 07:05:38 +0000127 }
128
Douglas Gregorad17e372008-12-16 22:23:02 +0000129 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
130 return Actions.ActOnLinkageSpec(Loc, LBrace, RBrace, LangBufPtr, StrSize,
131 &InnerDecls.front(), InnerDecls.size());
Chris Lattner806a5f52008-01-12 07:05:38 +0000132}
Douglas Gregorec93f442008-04-13 21:30:24 +0000133
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000134/// ParseClassName - Parse a C++ class-name, which names a class. Note
135/// that we only check that the result names a type; semantic analysis
136/// will need to verify that the type names a class. The result is
137/// either a type or NULL, dependending on whether a type name was
138/// found.
139///
140/// class-name: [C++ 9.1]
141/// identifier
142/// template-id [TODO]
143///
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000144Parser::TypeTy *Parser::ParseClassName(const CXXScopeSpec *SS) {
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000145 // Parse the class-name.
146 // FIXME: Alternatively, parse a simple-template-id.
147 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000148 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000149 return 0;
150 }
151
152 // We have an identifier; check whether it is actually a type.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000153 TypeTy *Type = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope, SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000154 if (!Type) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000155 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000156 return 0;
157 }
158
159 // Consume the identifier.
160 ConsumeToken();
161
162 return Type;
163}
164
Douglas Gregorec93f442008-04-13 21:30:24 +0000165/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
166/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
167/// until we reach the start of a definition or see a token that
168/// cannot start a definition.
169///
170/// class-specifier: [C++ class]
171/// class-head '{' member-specification[opt] '}'
172/// class-head '{' member-specification[opt] '}' attributes[opt]
173/// class-head:
174/// class-key identifier[opt] base-clause[opt]
175/// class-key nested-name-specifier identifier base-clause[opt]
176/// class-key nested-name-specifier[opt] simple-template-id
177/// base-clause[opt]
178/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
179/// [GNU] class-key attributes[opt] nested-name-specifier
180/// identifier base-clause[opt]
181/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
182/// simple-template-id base-clause[opt]
183/// class-key:
184/// 'class'
185/// 'struct'
186/// 'union'
187///
188/// elaborated-type-specifier: [C++ dcl.type.elab]
189/// class-key ::[opt] nested-name-specifier[opt] identifier
190/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
191/// simple-template-id
192///
193/// Note that the C++ class-specifier and elaborated-type-specifier,
194/// together, subsume the C99 struct-or-union-specifier:
195///
196/// struct-or-union-specifier: [C99 6.7.2.1]
197/// struct-or-union identifier[opt] '{' struct-contents '}'
198/// struct-or-union identifier
199/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
200/// '}' attributes[opt]
201/// [GNU] struct-or-union attributes[opt] identifier
202/// struct-or-union:
203/// 'struct'
204/// 'union'
Douglas Gregor52473432008-12-24 02:52:09 +0000205void Parser::ParseClassSpecifier(DeclSpec &DS,
206 TemplateParameterLists *TemplateParams) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000207 assert((Tok.is(tok::kw_class) ||
208 Tok.is(tok::kw_struct) ||
209 Tok.is(tok::kw_union)) &&
210 "Not a class specifier");
211 DeclSpec::TST TagType =
212 Tok.is(tok::kw_class) ? DeclSpec::TST_class :
213 Tok.is(tok::kw_struct) ? DeclSpec::TST_struct :
214 DeclSpec::TST_union;
215
216 SourceLocation StartLoc = ConsumeToken();
217
218 AttributeList *Attr = 0;
219 // If attributes exist after tag, parse them.
220 if (Tok.is(tok::kw___attribute))
221 Attr = ParseAttributes();
222
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000223 // Parse the (optional) nested-name-specifier.
224 CXXScopeSpec SS;
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +0000225 if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000226 if (Tok.isNot(tok::identifier))
227 Diag(Tok, diag::err_expected_ident);
228 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000229
230 // Parse the (optional) class name.
231 // FIXME: Alternatively, parse a simple-template-id.
232 IdentifierInfo *Name = 0;
233 SourceLocation NameLoc;
234 if (Tok.is(tok::identifier)) {
235 Name = Tok.getIdentifierInfo();
236 NameLoc = ConsumeToken();
237 }
238
239 // There are three options here. If we have 'struct foo;', then
240 // this is a forward declaration. If we have 'struct foo {...' or
241 // 'struct fo :...' then this is a definition. Otherwise we have
242 // something like 'struct foo xyz', a reference.
243 Action::TagKind TK;
244 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
245 TK = Action::TK_Definition;
246 else if (Tok.is(tok::semi))
247 TK = Action::TK_Declaration;
248 else
249 TK = Action::TK_Reference;
250
251 if (!Name && TK != Action::TK_Definition) {
252 // We have a declaration or reference to an anonymous class.
Chris Lattnerf006a222008-11-18 07:48:38 +0000253 Diag(StartLoc, diag::err_anon_type_definition)
254 << DeclSpec::getSpecifierName(TagType);
Douglas Gregorec93f442008-04-13 21:30:24 +0000255
256 // Skip the rest of this declarator, up until the comma or semicolon.
257 SkipUntil(tok::comma, true);
258 return;
259 }
260
261 // Parse the tag portion of this.
Douglas Gregor52473432008-12-24 02:52:09 +0000262 DeclTy *TagDecl
263 = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name,
264 NameLoc, Attr,
265 Action::MultiTemplateParamsArg(
266 Actions,
267 TemplateParams? &(*TemplateParams)[0] : 0,
268 TemplateParams? TemplateParams->size() : 0));
Douglas Gregorec93f442008-04-13 21:30:24 +0000269
270 // Parse the optional base clause (C++ only).
271 if (getLang().CPlusPlus && Tok.is(tok::colon)) {
272 ParseBaseClause(TagDecl);
273 }
274
275 // If there is a body, parse it and inform the actions module.
276 if (Tok.is(tok::l_brace))
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000277 if (getLang().CPlusPlus)
278 ParseCXXMemberSpecification(StartLoc, TagType, TagDecl);
279 else
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000280 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Douglas Gregorec93f442008-04-13 21:30:24 +0000281 else if (TK == Action::TK_Definition) {
282 // FIXME: Complain that we have a base-specifier list but no
283 // definition.
Chris Lattnerf006a222008-11-18 07:48:38 +0000284 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregorec93f442008-04-13 21:30:24 +0000285 }
286
287 const char *PrevSpec = 0;
288 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +0000289 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Douglas Gregorec93f442008-04-13 21:30:24 +0000290}
291
292/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
293///
294/// base-clause : [C++ class.derived]
295/// ':' base-specifier-list
296/// base-specifier-list:
297/// base-specifier '...'[opt]
298/// base-specifier-list ',' base-specifier '...'[opt]
299void Parser::ParseBaseClause(DeclTy *ClassDecl)
300{
301 assert(Tok.is(tok::colon) && "Not a base clause");
302 ConsumeToken();
303
Douglas Gregorabed2172008-10-22 17:49:05 +0000304 // Build up an array of parsed base specifiers.
305 llvm::SmallVector<BaseTy *, 8> BaseInfo;
306
Douglas Gregorec93f442008-04-13 21:30:24 +0000307 while (true) {
308 // Parse a base-specifier.
Douglas Gregorabed2172008-10-22 17:49:05 +0000309 BaseResult Result = ParseBaseSpecifier(ClassDecl);
310 if (Result.isInvalid) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000311 // Skip the rest of this base specifier, up until the comma or
312 // opening brace.
Douglas Gregorabed2172008-10-22 17:49:05 +0000313 SkipUntil(tok::comma, tok::l_brace, true, true);
314 } else {
315 // Add this to our array of base specifiers.
316 BaseInfo.push_back(Result.Val);
Douglas Gregorec93f442008-04-13 21:30:24 +0000317 }
318
319 // If the next token is a comma, consume it and keep reading
320 // base-specifiers.
321 if (Tok.isNot(tok::comma)) break;
322
323 // Consume the comma.
324 ConsumeToken();
325 }
Douglas Gregorabed2172008-10-22 17:49:05 +0000326
327 // Attach the base specifiers
328 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregorec93f442008-04-13 21:30:24 +0000329}
330
331/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
332/// one entry in the base class list of a class specifier, for example:
333/// class foo : public bar, virtual private baz {
334/// 'public bar' and 'virtual private baz' are each base-specifiers.
335///
336/// base-specifier: [C++ class.derived]
337/// ::[opt] nested-name-specifier[opt] class-name
338/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
339/// class-name
340/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
341/// class-name
Douglas Gregorabed2172008-10-22 17:49:05 +0000342Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
Douglas Gregorec93f442008-04-13 21:30:24 +0000343{
344 bool IsVirtual = false;
345 SourceLocation StartLoc = Tok.getLocation();
346
347 // Parse the 'virtual' keyword.
348 if (Tok.is(tok::kw_virtual)) {
349 ConsumeToken();
350 IsVirtual = true;
351 }
352
353 // Parse an (optional) access specifier.
354 AccessSpecifier Access = getAccessSpecifierIfPresent();
355 if (Access)
356 ConsumeToken();
357
358 // Parse the 'virtual' keyword (again!), in case it came after the
359 // access specifier.
360 if (Tok.is(tok::kw_virtual)) {
361 SourceLocation VirtualLoc = ConsumeToken();
362 if (IsVirtual) {
363 // Complain about duplicate 'virtual'
Chris Lattnerf006a222008-11-18 07:48:38 +0000364 Diag(VirtualLoc, diag::err_dup_virtual)
365 << SourceRange(VirtualLoc, VirtualLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000366 }
367
368 IsVirtual = true;
369 }
370
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000371 // Parse optional '::' and optional nested-name-specifier.
372 CXXScopeSpec SS;
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +0000373 MaybeParseCXXScopeSpecifier(SS);
Douglas Gregorec93f442008-04-13 21:30:24 +0000374
Douglas Gregorec93f442008-04-13 21:30:24 +0000375 // The location of the base class itself.
376 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000377
378 // Parse the class-name.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000379 TypeTy *BaseType = ParseClassName(&SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000380 if (!BaseType)
381 return true;
Douglas Gregorec93f442008-04-13 21:30:24 +0000382
383 // Find the complete source range for the base-specifier.
384 SourceRange Range(StartLoc, BaseLoc);
385
Douglas Gregorec93f442008-04-13 21:30:24 +0000386 // Notify semantic analysis that we have parsed a complete
387 // base-specifier.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000388 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
389 BaseType, BaseLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000390}
391
392/// getAccessSpecifierIfPresent - Determine whether the next token is
393/// a C++ access-specifier.
394///
395/// access-specifier: [C++ class.derived]
396/// 'private'
397/// 'protected'
398/// 'public'
Douglas Gregor696be932008-04-14 00:13:42 +0000399AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregorec93f442008-04-13 21:30:24 +0000400{
401 switch (Tok.getKind()) {
402 default: return AS_none;
403 case tok::kw_private: return AS_private;
404 case tok::kw_protected: return AS_protected;
405 case tok::kw_public: return AS_public;
406 }
407}
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000408
409/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
410///
411/// member-declaration:
412/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
413/// function-definition ';'[opt]
414/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
415/// using-declaration [TODO]
416/// [C++0x] static_assert-declaration [TODO]
417/// template-declaration [TODO]
Chris Lattnerf3375de2008-12-18 01:12:00 +0000418/// [GNU] '__extension__' member-declaration
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000419///
420/// member-declarator-list:
421/// member-declarator
422/// member-declarator-list ',' member-declarator
423///
424/// member-declarator:
425/// declarator pure-specifier[opt]
426/// declarator constant-initializer[opt]
427/// identifier[opt] ':' constant-expression
428///
429/// pure-specifier: [TODO]
430/// '= 0'
431///
432/// constant-initializer:
433/// '=' constant-expression
434///
435Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Chris Lattnerf3375de2008-12-18 01:12:00 +0000436 // Handle: member-declaration ::= '__extension__' member-declaration
437 if (Tok.is(tok::kw___extension__)) {
438 // __extension__ silences extension warnings in the subexpression.
439 ExtensionRAIIObject O(Diags); // Use RAII to do this.
440 ConsumeToken();
441 return ParseCXXClassMemberDeclaration(AS);
442 }
443
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000444 SourceLocation DSStart = Tok.getLocation();
445 // decl-specifier-seq:
446 // Parse the common declaration-specifiers piece.
447 DeclSpec DS;
448 ParseDeclarationSpecifiers(DS);
449
450 if (Tok.is(tok::semi)) {
451 ConsumeToken();
452 // C++ 9.2p7: The member-declarator-list can be omitted only after a
453 // class-specifier or an enum-specifier or in a friend declaration.
454 // FIXME: Friend declarations.
455 switch (DS.getTypeSpecType()) {
456 case DeclSpec::TST_struct:
457 case DeclSpec::TST_union:
458 case DeclSpec::TST_class:
459 case DeclSpec::TST_enum:
460 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
461 default:
462 Diag(DSStart, diag::err_no_declarators);
463 return 0;
464 }
465 }
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000466
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000467 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000468
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000469 if (Tok.isNot(tok::colon)) {
470 // Parse the first declarator.
471 ParseDeclarator(DeclaratorInfo);
472 // Error parsing the declarator?
Douglas Gregor6704b312008-11-17 22:58:34 +0000473 if (!DeclaratorInfo.hasName()) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000474 // If so, skip until the semi-colon or a }.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000475 SkipUntil(tok::r_brace, true);
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000476 if (Tok.is(tok::semi))
477 ConsumeToken();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000478 return 0;
479 }
480
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000481 // function-definition:
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000482 if (Tok.is(tok::l_brace)
483 || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000484 if (!DeclaratorInfo.isFunctionDeclarator()) {
485 Diag(Tok, diag::err_func_def_no_params);
486 ConsumeBrace();
487 SkipUntil(tok::r_brace, true);
488 return 0;
489 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000490
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000491 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
492 Diag(Tok, diag::err_function_declared_typedef);
493 // This recovery skips the entire function body. It would be nice
494 // to simply call ParseCXXInlineMethodDef() below, however Sema
495 // assumes the declarator represents a function, not a typedef.
496 ConsumeBrace();
497 SkipUntil(tok::r_brace, true);
498 return 0;
499 }
500
501 return ParseCXXInlineMethodDef(AS, DeclaratorInfo);
502 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000503 }
504
505 // member-declarator-list:
506 // member-declarator
507 // member-declarator-list ',' member-declarator
508
509 DeclTy *LastDeclInGroup = 0;
Sebastian Redl62261042008-12-09 20:22:58 +0000510 OwningExprResult BitfieldSize(Actions);
511 OwningExprResult Init(Actions);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000512
513 while (1) {
514
515 // member-declarator:
516 // declarator pure-specifier[opt]
517 // declarator constant-initializer[opt]
518 // identifier[opt] ':' constant-expression
519
520 if (Tok.is(tok::colon)) {
521 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000522 BitfieldSize = ParseConstantExpression();
523 if (BitfieldSize.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000524 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000525 }
526
527 // pure-specifier:
528 // '= 0'
529 //
530 // constant-initializer:
531 // '=' constant-expression
532
533 if (Tok.is(tok::equal)) {
534 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000535 Init = ParseInitializer();
536 if (Init.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000537 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000538 }
539
540 // If attributes exist after the declarator, parse them.
541 if (Tok.is(tok::kw___attribute))
542 DeclaratorInfo.AddAttributes(ParseAttributes());
543
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000544 // NOTE: If Sema is the Action module and declarator is an instance field,
545 // this call will *not* return the created decl; LastDeclInGroup will be
546 // returned instead.
547 // See Sema::ActOnCXXMemberDeclarator for details.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000548 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
549 DeclaratorInfo,
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000550 BitfieldSize.release(),
551 Init.release(),
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000552 LastDeclInGroup);
553
Douglas Gregor605de8d2008-12-16 21:30:33 +0000554 if (DeclaratorInfo.isFunctionDeclarator() &&
555 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
556 != DeclSpec::SCS_typedef) {
557 // We just declared a member function. If this member function
558 // has any default arguments, we'll need to parse them later.
559 LateParsedMethodDeclaration *LateMethod = 0;
560 DeclaratorChunk::FunctionTypeInfo &FTI
561 = DeclaratorInfo.getTypeObject(0).Fun;
562 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
563 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
564 if (!LateMethod) {
565 // Push this method onto the stack of late-parsed method
566 // declarations.
567 getCurTopClassStack().MethodDecls.push_back(
568 LateParsedMethodDeclaration(LastDeclInGroup));
569 LateMethod = &getCurTopClassStack().MethodDecls.back();
570
571 // Add all of the parameters prior to this one (they don't
572 // have default arguments).
573 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
574 for (unsigned I = 0; I < ParamIdx; ++I)
575 LateMethod->DefaultArgs.push_back(
576 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
577 }
578
579 // Add this parameter to the list of parameters (it or may
580 // not have a default argument).
581 LateMethod->DefaultArgs.push_back(
582 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
583 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
584 }
585 }
586 }
587
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000588 // If we don't have a comma, it is either the end of the list (a ';')
589 // or an error, bail out.
590 if (Tok.isNot(tok::comma))
591 break;
592
593 // Consume the comma.
594 ConsumeToken();
595
596 // Parse the next declarator.
597 DeclaratorInfo.clear();
Sebastian Redl62261042008-12-09 20:22:58 +0000598 BitfieldSize = 0;
599 Init = 0;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000600
601 // Attributes are only allowed on the second declarator.
602 if (Tok.is(tok::kw___attribute))
603 DeclaratorInfo.AddAttributes(ParseAttributes());
604
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000605 if (Tok.isNot(tok::colon))
606 ParseDeclarator(DeclaratorInfo);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000607 }
608
609 if (Tok.is(tok::semi)) {
610 ConsumeToken();
611 // Reverse the chain list.
612 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
613 }
614
615 Diag(Tok, diag::err_expected_semi_decl_list);
616 // Skip to end of block or statement
617 SkipUntil(tok::r_brace, true, true);
618 if (Tok.is(tok::semi))
619 ConsumeToken();
620 return 0;
621}
622
623/// ParseCXXMemberSpecification - Parse the class definition.
624///
625/// member-specification:
626/// member-declaration member-specification[opt]
627/// access-specifier ':' member-specification[opt]
628///
629void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
630 unsigned TagType, DeclTy *TagDecl) {
Sanjiv Guptafa451432008-10-31 09:52:39 +0000631 assert((TagType == DeclSpec::TST_struct ||
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000632 TagType == DeclSpec::TST_union ||
Sanjiv Guptafa451432008-10-31 09:52:39 +0000633 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000634
635 SourceLocation LBraceLoc = ConsumeBrace();
636
637 if (!CurScope->isCXXClassScope() && // Not about to define a nested class.
638 CurScope->isInCXXInlineMethodScope()) {
639 // We will define a local class of an inline method.
640 // Push a new LexedMethodsForTopClass for its inline methods.
641 PushTopClassStack();
642 }
643
644 // Enter a scope for the class.
Douglas Gregor95d40792008-12-10 06:34:36 +0000645 ParseScope ClassScope(this, Scope::CXXClassScope|Scope::DeclScope);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000646
647 Actions.ActOnStartCXXClassDef(CurScope, TagDecl, LBraceLoc);
648
649 // C++ 11p3: Members of a class defined with the keyword class are private
650 // by default. Members of a class defined with the keywords struct or union
651 // are public by default.
652 AccessSpecifier CurAS;
653 if (TagType == DeclSpec::TST_class)
654 CurAS = AS_private;
655 else
656 CurAS = AS_public;
657
658 // While we still have something to read, read the member-declarations.
659 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
660 // Each iteration of this loop reads one member-declaration.
661
662 // Check for extraneous top-level semicolon.
663 if (Tok.is(tok::semi)) {
664 Diag(Tok, diag::ext_extra_struct_semi);
665 ConsumeToken();
666 continue;
667 }
668
669 AccessSpecifier AS = getAccessSpecifierIfPresent();
670 if (AS != AS_none) {
671 // Current token is a C++ access specifier.
672 CurAS = AS;
673 ConsumeToken();
674 ExpectAndConsume(tok::colon, diag::err_expected_colon);
675 continue;
676 }
677
678 // Parse all the comma separated declarators.
679 ParseCXXClassMemberDeclaration(CurAS);
680 }
681
682 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
683
684 AttributeList *AttrList = 0;
685 // If attributes exist after class contents, parse them.
686 if (Tok.is(tok::kw___attribute))
687 AttrList = ParseAttributes(); // FIXME: where should I put them?
688
689 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
690 LBraceLoc, RBraceLoc);
691
692 // C++ 9.2p2: Within the class member-specification, the class is regarded as
693 // complete within function bodies, default arguments,
694 // exception-specifications, and constructor ctor-initializers (including
695 // such things in nested classes).
696 //
Douglas Gregor605de8d2008-12-16 21:30:33 +0000697 // FIXME: Only function bodies and constructor ctor-initializers are
698 // parsed correctly, fix the rest.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000699 if (!CurScope->getParent()->isCXXClassScope()) {
700 // We are not inside a nested class. This class and its nested classes
Douglas Gregor605de8d2008-12-16 21:30:33 +0000701 // are complete and we can parse the delayed portions of method
702 // declarations and the lexed inline method definitions.
703 ParseLexedMethodDeclarations();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000704 ParseLexedMethodDefs();
705
706 // For a local class of inline method, pop the LexedMethodsForTopClass that
707 // was previously pushed.
708
Sanjiv Guptafa451432008-10-31 09:52:39 +0000709 assert((CurScope->isInCXXInlineMethodScope() ||
710 TopClassStacks.size() == 1) &&
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000711 "MethodLexers not getting popped properly!");
712 if (CurScope->isInCXXInlineMethodScope())
713 PopTopClassStack();
714 }
715
716 // Leave the class scope.
Douglas Gregor95d40792008-12-10 06:34:36 +0000717 ClassScope.Exit();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000718
Argiris Kirtzidis448b4e42008-08-09 00:39:29 +0000719 Actions.ActOnFinishCXXClassDef(TagDecl);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000720}
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000721
722/// ParseConstructorInitializer - Parse a C++ constructor initializer,
723/// which explicitly initializes the members or base classes of a
724/// class (C++ [class.base.init]). For example, the three initializers
725/// after the ':' in the Derived constructor below:
726///
727/// @code
728/// class Base { };
729/// class Derived : Base {
730/// int x;
731/// float f;
732/// public:
733/// Derived(float f) : Base(), x(17), f(f) { }
734/// };
735/// @endcode
736///
737/// [C++] ctor-initializer:
738/// ':' mem-initializer-list
739///
740/// [C++] mem-initializer-list:
741/// mem-initializer
742/// mem-initializer , mem-initializer-list
743void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) {
744 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
745
746 SourceLocation ColonLoc = ConsumeToken();
747
748 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
749
750 do {
751 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
752 if (!MemInit.isInvalid)
753 MemInitializers.push_back(MemInit.Val);
754
755 if (Tok.is(tok::comma))
756 ConsumeToken();
757 else if (Tok.is(tok::l_brace))
758 break;
759 else {
760 // Skip over garbage, until we get to '{'. Don't eat the '{'.
761 SkipUntil(tok::l_brace, true, true);
762 break;
763 }
764 } while (true);
765
766 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
767 &MemInitializers[0], MemInitializers.size());
768}
769
770/// ParseMemInitializer - Parse a C++ member initializer, which is
771/// part of a constructor initializer that explicitly initializes one
772/// member or base class (C++ [class.base.init]). See
773/// ParseConstructorInitializer for an example.
774///
775/// [C++] mem-initializer:
776/// mem-initializer-id '(' expression-list[opt] ')'
777///
778/// [C++] mem-initializer-id:
779/// '::'[opt] nested-name-specifier[opt] class-name
780/// identifier
781Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) {
782 // FIXME: parse '::'[opt] nested-name-specifier[opt]
783
784 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000785 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000786 return true;
787 }
788
789 // Get the identifier. This may be a member name or a class name,
790 // but we'll let the semantic analysis determine which it is.
791 IdentifierInfo *II = Tok.getIdentifierInfo();
792 SourceLocation IdLoc = ConsumeToken();
793
794 // Parse the '('.
795 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000796 Diag(Tok, diag::err_expected_lparen);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000797 return true;
798 }
799 SourceLocation LParenLoc = ConsumeParen();
800
801 // Parse the optional expression-list.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000802 ExprVector ArgExprs(Actions);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000803 CommaLocsTy CommaLocs;
804 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
805 SkipUntil(tok::r_paren);
806 return true;
807 }
808
809 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
810
Sebastian Redl6008ac32008-11-25 22:21:31 +0000811 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
812 LParenLoc, ArgExprs.take(),
813 ArgExprs.size(), &CommaLocs[0], RParenLoc);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000814}
Douglas Gregor90a2c972008-11-25 03:22:00 +0000815
816/// ParseExceptionSpecification - Parse a C++ exception-specification
817/// (C++ [except.spec]).
818///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +0000819/// exception-specification:
820/// 'throw' '(' type-id-list [opt] ')'
821/// [MS] 'throw' '(' '...' ')'
Douglas Gregor90a2c972008-11-25 03:22:00 +0000822///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +0000823/// type-id-list:
824/// type-id
825/// type-id-list ',' type-id
Douglas Gregor90a2c972008-11-25 03:22:00 +0000826///
827bool Parser::ParseExceptionSpecification() {
828 assert(Tok.is(tok::kw_throw) && "expected throw");
829
830 SourceLocation ThrowLoc = ConsumeToken();
831
832 if (!Tok.is(tok::l_paren)) {
833 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
834 }
835 SourceLocation LParenLoc = ConsumeParen();
836
Douglas Gregor9ed9ac82008-12-01 18:00:20 +0000837 // Parse throw(...), a Microsoft extension that means "this function
838 // can throw anything".
839 if (Tok.is(tok::ellipsis)) {
840 SourceLocation EllipsisLoc = ConsumeToken();
841 if (!getLang().Microsoft)
842 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
843 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
844 return false;
845 }
846
Douglas Gregor90a2c972008-11-25 03:22:00 +0000847 // Parse the sequence of type-ids.
848 while (Tok.isNot(tok::r_paren)) {
849 ParseTypeName();
850 if (Tok.is(tok::comma))
851 ConsumeToken();
852 else
853 break;
854 }
855
856 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
857 return false;
858}