blob: 1d3de90060b7ca1e58a37fd60132e5b95d4c7c24 [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
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000223 // If declspecs exist after tag, parse them.
224 if (Tok.is(tok::kw___declspec) && PP.getLangOptions().Microsoft)
225 FuzzyParseMicrosoftDeclSpec();
226
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000227 // Parse the (optional) nested-name-specifier.
228 CXXScopeSpec SS;
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +0000229 if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000230 if (Tok.isNot(tok::identifier))
231 Diag(Tok, diag::err_expected_ident);
232 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000233
234 // Parse the (optional) class name.
235 // FIXME: Alternatively, parse a simple-template-id.
236 IdentifierInfo *Name = 0;
237 SourceLocation NameLoc;
238 if (Tok.is(tok::identifier)) {
239 Name = Tok.getIdentifierInfo();
240 NameLoc = ConsumeToken();
241 }
242
243 // There are three options here. If we have 'struct foo;', then
244 // this is a forward declaration. If we have 'struct foo {...' or
245 // 'struct fo :...' then this is a definition. Otherwise we have
246 // something like 'struct foo xyz', a reference.
247 Action::TagKind TK;
248 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
249 TK = Action::TK_Definition;
250 else if (Tok.is(tok::semi))
251 TK = Action::TK_Declaration;
252 else
253 TK = Action::TK_Reference;
254
255 if (!Name && TK != Action::TK_Definition) {
256 // We have a declaration or reference to an anonymous class.
Chris Lattnerf006a222008-11-18 07:48:38 +0000257 Diag(StartLoc, diag::err_anon_type_definition)
258 << DeclSpec::getSpecifierName(TagType);
Douglas Gregorec93f442008-04-13 21:30:24 +0000259
260 // Skip the rest of this declarator, up until the comma or semicolon.
261 SkipUntil(tok::comma, true);
262 return;
263 }
264
265 // Parse the tag portion of this.
Douglas Gregor52473432008-12-24 02:52:09 +0000266 DeclTy *TagDecl
267 = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name,
268 NameLoc, Attr,
269 Action::MultiTemplateParamsArg(
270 Actions,
271 TemplateParams? &(*TemplateParams)[0] : 0,
272 TemplateParams? TemplateParams->size() : 0));
Douglas Gregorec93f442008-04-13 21:30:24 +0000273
274 // Parse the optional base clause (C++ only).
275 if (getLang().CPlusPlus && Tok.is(tok::colon)) {
276 ParseBaseClause(TagDecl);
277 }
278
279 // If there is a body, parse it and inform the actions module.
280 if (Tok.is(tok::l_brace))
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000281 if (getLang().CPlusPlus)
282 ParseCXXMemberSpecification(StartLoc, TagType, TagDecl);
283 else
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000284 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Douglas Gregorec93f442008-04-13 21:30:24 +0000285 else if (TK == Action::TK_Definition) {
286 // FIXME: Complain that we have a base-specifier list but no
287 // definition.
Chris Lattnerf006a222008-11-18 07:48:38 +0000288 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregorec93f442008-04-13 21:30:24 +0000289 }
290
291 const char *PrevSpec = 0;
292 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +0000293 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Douglas Gregorec93f442008-04-13 21:30:24 +0000294}
295
296/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
297///
298/// base-clause : [C++ class.derived]
299/// ':' base-specifier-list
300/// base-specifier-list:
301/// base-specifier '...'[opt]
302/// base-specifier-list ',' base-specifier '...'[opt]
303void Parser::ParseBaseClause(DeclTy *ClassDecl)
304{
305 assert(Tok.is(tok::colon) && "Not a base clause");
306 ConsumeToken();
307
Douglas Gregorabed2172008-10-22 17:49:05 +0000308 // Build up an array of parsed base specifiers.
309 llvm::SmallVector<BaseTy *, 8> BaseInfo;
310
Douglas Gregorec93f442008-04-13 21:30:24 +0000311 while (true) {
312 // Parse a base-specifier.
Douglas Gregorabed2172008-10-22 17:49:05 +0000313 BaseResult Result = ParseBaseSpecifier(ClassDecl);
314 if (Result.isInvalid) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000315 // Skip the rest of this base specifier, up until the comma or
316 // opening brace.
Douglas Gregorabed2172008-10-22 17:49:05 +0000317 SkipUntil(tok::comma, tok::l_brace, true, true);
318 } else {
319 // Add this to our array of base specifiers.
320 BaseInfo.push_back(Result.Val);
Douglas Gregorec93f442008-04-13 21:30:24 +0000321 }
322
323 // If the next token is a comma, consume it and keep reading
324 // base-specifiers.
325 if (Tok.isNot(tok::comma)) break;
326
327 // Consume the comma.
328 ConsumeToken();
329 }
Douglas Gregorabed2172008-10-22 17:49:05 +0000330
331 // Attach the base specifiers
332 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregorec93f442008-04-13 21:30:24 +0000333}
334
335/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
336/// one entry in the base class list of a class specifier, for example:
337/// class foo : public bar, virtual private baz {
338/// 'public bar' and 'virtual private baz' are each base-specifiers.
339///
340/// base-specifier: [C++ class.derived]
341/// ::[opt] nested-name-specifier[opt] class-name
342/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
343/// class-name
344/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
345/// class-name
Douglas Gregorabed2172008-10-22 17:49:05 +0000346Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
Douglas Gregorec93f442008-04-13 21:30:24 +0000347{
348 bool IsVirtual = false;
349 SourceLocation StartLoc = Tok.getLocation();
350
351 // Parse the 'virtual' keyword.
352 if (Tok.is(tok::kw_virtual)) {
353 ConsumeToken();
354 IsVirtual = true;
355 }
356
357 // Parse an (optional) access specifier.
358 AccessSpecifier Access = getAccessSpecifierIfPresent();
359 if (Access)
360 ConsumeToken();
361
362 // Parse the 'virtual' keyword (again!), in case it came after the
363 // access specifier.
364 if (Tok.is(tok::kw_virtual)) {
365 SourceLocation VirtualLoc = ConsumeToken();
366 if (IsVirtual) {
367 // Complain about duplicate 'virtual'
Chris Lattnerf006a222008-11-18 07:48:38 +0000368 Diag(VirtualLoc, diag::err_dup_virtual)
369 << SourceRange(VirtualLoc, VirtualLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000370 }
371
372 IsVirtual = true;
373 }
374
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000375 // Parse optional '::' and optional nested-name-specifier.
376 CXXScopeSpec SS;
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +0000377 MaybeParseCXXScopeSpecifier(SS);
Douglas Gregorec93f442008-04-13 21:30:24 +0000378
Douglas Gregorec93f442008-04-13 21:30:24 +0000379 // The location of the base class itself.
380 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000381
382 // Parse the class-name.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000383 TypeTy *BaseType = ParseClassName(&SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000384 if (!BaseType)
385 return true;
Douglas Gregorec93f442008-04-13 21:30:24 +0000386
387 // Find the complete source range for the base-specifier.
388 SourceRange Range(StartLoc, BaseLoc);
389
Douglas Gregorec93f442008-04-13 21:30:24 +0000390 // Notify semantic analysis that we have parsed a complete
391 // base-specifier.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000392 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
393 BaseType, BaseLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000394}
395
396/// getAccessSpecifierIfPresent - Determine whether the next token is
397/// a C++ access-specifier.
398///
399/// access-specifier: [C++ class.derived]
400/// 'private'
401/// 'protected'
402/// 'public'
Douglas Gregor696be932008-04-14 00:13:42 +0000403AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregorec93f442008-04-13 21:30:24 +0000404{
405 switch (Tok.getKind()) {
406 default: return AS_none;
407 case tok::kw_private: return AS_private;
408 case tok::kw_protected: return AS_protected;
409 case tok::kw_public: return AS_public;
410 }
411}
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000412
413/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
414///
415/// member-declaration:
416/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
417/// function-definition ';'[opt]
418/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
419/// using-declaration [TODO]
420/// [C++0x] static_assert-declaration [TODO]
421/// template-declaration [TODO]
Chris Lattnerf3375de2008-12-18 01:12:00 +0000422/// [GNU] '__extension__' member-declaration
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000423///
424/// member-declarator-list:
425/// member-declarator
426/// member-declarator-list ',' member-declarator
427///
428/// member-declarator:
429/// declarator pure-specifier[opt]
430/// declarator constant-initializer[opt]
431/// identifier[opt] ':' constant-expression
432///
433/// pure-specifier: [TODO]
434/// '= 0'
435///
436/// constant-initializer:
437/// '=' constant-expression
438///
439Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Chris Lattnerf3375de2008-12-18 01:12:00 +0000440 // Handle: member-declaration ::= '__extension__' member-declaration
441 if (Tok.is(tok::kw___extension__)) {
442 // __extension__ silences extension warnings in the subexpression.
443 ExtensionRAIIObject O(Diags); // Use RAII to do this.
444 ConsumeToken();
445 return ParseCXXClassMemberDeclaration(AS);
446 }
447
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000448 SourceLocation DSStart = Tok.getLocation();
449 // decl-specifier-seq:
450 // Parse the common declaration-specifiers piece.
451 DeclSpec DS;
452 ParseDeclarationSpecifiers(DS);
453
454 if (Tok.is(tok::semi)) {
455 ConsumeToken();
456 // C++ 9.2p7: The member-declarator-list can be omitted only after a
457 // class-specifier or an enum-specifier or in a friend declaration.
458 // FIXME: Friend declarations.
459 switch (DS.getTypeSpecType()) {
460 case DeclSpec::TST_struct:
461 case DeclSpec::TST_union:
462 case DeclSpec::TST_class:
463 case DeclSpec::TST_enum:
464 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
465 default:
466 Diag(DSStart, diag::err_no_declarators);
467 return 0;
468 }
469 }
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000470
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000471 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000472
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000473 if (Tok.isNot(tok::colon)) {
474 // Parse the first declarator.
475 ParseDeclarator(DeclaratorInfo);
476 // Error parsing the declarator?
Douglas Gregor6704b312008-11-17 22:58:34 +0000477 if (!DeclaratorInfo.hasName()) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000478 // If so, skip until the semi-colon or a }.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000479 SkipUntil(tok::r_brace, true);
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000480 if (Tok.is(tok::semi))
481 ConsumeToken();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000482 return 0;
483 }
484
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000485 // function-definition:
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000486 if (Tok.is(tok::l_brace)
487 || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000488 if (!DeclaratorInfo.isFunctionDeclarator()) {
489 Diag(Tok, diag::err_func_def_no_params);
490 ConsumeBrace();
491 SkipUntil(tok::r_brace, true);
492 return 0;
493 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000494
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000495 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
496 Diag(Tok, diag::err_function_declared_typedef);
497 // This recovery skips the entire function body. It would be nice
498 // to simply call ParseCXXInlineMethodDef() below, however Sema
499 // assumes the declarator represents a function, not a typedef.
500 ConsumeBrace();
501 SkipUntil(tok::r_brace, true);
502 return 0;
503 }
504
505 return ParseCXXInlineMethodDef(AS, DeclaratorInfo);
506 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000507 }
508
509 // member-declarator-list:
510 // member-declarator
511 // member-declarator-list ',' member-declarator
512
513 DeclTy *LastDeclInGroup = 0;
Sebastian Redl62261042008-12-09 20:22:58 +0000514 OwningExprResult BitfieldSize(Actions);
515 OwningExprResult Init(Actions);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000516
517 while (1) {
518
519 // member-declarator:
520 // declarator pure-specifier[opt]
521 // declarator constant-initializer[opt]
522 // identifier[opt] ':' constant-expression
523
524 if (Tok.is(tok::colon)) {
525 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000526 BitfieldSize = ParseConstantExpression();
527 if (BitfieldSize.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000528 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000529 }
530
531 // pure-specifier:
532 // '= 0'
533 //
534 // constant-initializer:
535 // '=' constant-expression
536
537 if (Tok.is(tok::equal)) {
538 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000539 Init = ParseInitializer();
540 if (Init.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000541 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000542 }
543
544 // If attributes exist after the declarator, parse them.
545 if (Tok.is(tok::kw___attribute))
546 DeclaratorInfo.AddAttributes(ParseAttributes());
547
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000548 // NOTE: If Sema is the Action module and declarator is an instance field,
549 // this call will *not* return the created decl; LastDeclInGroup will be
550 // returned instead.
551 // See Sema::ActOnCXXMemberDeclarator for details.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000552 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
553 DeclaratorInfo,
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000554 BitfieldSize.release(),
555 Init.release(),
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000556 LastDeclInGroup);
557
Douglas Gregor605de8d2008-12-16 21:30:33 +0000558 if (DeclaratorInfo.isFunctionDeclarator() &&
559 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
560 != DeclSpec::SCS_typedef) {
561 // We just declared a member function. If this member function
562 // has any default arguments, we'll need to parse them later.
563 LateParsedMethodDeclaration *LateMethod = 0;
564 DeclaratorChunk::FunctionTypeInfo &FTI
565 = DeclaratorInfo.getTypeObject(0).Fun;
566 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
567 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
568 if (!LateMethod) {
569 // Push this method onto the stack of late-parsed method
570 // declarations.
571 getCurTopClassStack().MethodDecls.push_back(
572 LateParsedMethodDeclaration(LastDeclInGroup));
573 LateMethod = &getCurTopClassStack().MethodDecls.back();
574
575 // Add all of the parameters prior to this one (they don't
576 // have default arguments).
577 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
578 for (unsigned I = 0; I < ParamIdx; ++I)
579 LateMethod->DefaultArgs.push_back(
580 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
581 }
582
583 // Add this parameter to the list of parameters (it or may
584 // not have a default argument).
585 LateMethod->DefaultArgs.push_back(
586 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
587 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
588 }
589 }
590 }
591
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000592 // If we don't have a comma, it is either the end of the list (a ';')
593 // or an error, bail out.
594 if (Tok.isNot(tok::comma))
595 break;
596
597 // Consume the comma.
598 ConsumeToken();
599
600 // Parse the next declarator.
601 DeclaratorInfo.clear();
Sebastian Redl62261042008-12-09 20:22:58 +0000602 BitfieldSize = 0;
603 Init = 0;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000604
605 // Attributes are only allowed on the second declarator.
606 if (Tok.is(tok::kw___attribute))
607 DeclaratorInfo.AddAttributes(ParseAttributes());
608
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000609 if (Tok.isNot(tok::colon))
610 ParseDeclarator(DeclaratorInfo);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000611 }
612
613 if (Tok.is(tok::semi)) {
614 ConsumeToken();
615 // Reverse the chain list.
616 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
617 }
618
619 Diag(Tok, diag::err_expected_semi_decl_list);
620 // Skip to end of block or statement
621 SkipUntil(tok::r_brace, true, true);
622 if (Tok.is(tok::semi))
623 ConsumeToken();
624 return 0;
625}
626
627/// ParseCXXMemberSpecification - Parse the class definition.
628///
629/// member-specification:
630/// member-declaration member-specification[opt]
631/// access-specifier ':' member-specification[opt]
632///
633void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
634 unsigned TagType, DeclTy *TagDecl) {
Sanjiv Guptafa451432008-10-31 09:52:39 +0000635 assert((TagType == DeclSpec::TST_struct ||
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000636 TagType == DeclSpec::TST_union ||
Sanjiv Guptafa451432008-10-31 09:52:39 +0000637 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000638
639 SourceLocation LBraceLoc = ConsumeBrace();
640
641 if (!CurScope->isCXXClassScope() && // Not about to define a nested class.
642 CurScope->isInCXXInlineMethodScope()) {
643 // We will define a local class of an inline method.
644 // Push a new LexedMethodsForTopClass for its inline methods.
645 PushTopClassStack();
646 }
647
648 // Enter a scope for the class.
Douglas Gregor95d40792008-12-10 06:34:36 +0000649 ParseScope ClassScope(this, Scope::CXXClassScope|Scope::DeclScope);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000650
651 Actions.ActOnStartCXXClassDef(CurScope, TagDecl, LBraceLoc);
652
653 // C++ 11p3: Members of a class defined with the keyword class are private
654 // by default. Members of a class defined with the keywords struct or union
655 // are public by default.
656 AccessSpecifier CurAS;
657 if (TagType == DeclSpec::TST_class)
658 CurAS = AS_private;
659 else
660 CurAS = AS_public;
661
662 // While we still have something to read, read the member-declarations.
663 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
664 // Each iteration of this loop reads one member-declaration.
665
666 // Check for extraneous top-level semicolon.
667 if (Tok.is(tok::semi)) {
668 Diag(Tok, diag::ext_extra_struct_semi);
669 ConsumeToken();
670 continue;
671 }
672
673 AccessSpecifier AS = getAccessSpecifierIfPresent();
674 if (AS != AS_none) {
675 // Current token is a C++ access specifier.
676 CurAS = AS;
677 ConsumeToken();
678 ExpectAndConsume(tok::colon, diag::err_expected_colon);
679 continue;
680 }
681
682 // Parse all the comma separated declarators.
683 ParseCXXClassMemberDeclaration(CurAS);
684 }
685
686 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
687
688 AttributeList *AttrList = 0;
689 // If attributes exist after class contents, parse them.
690 if (Tok.is(tok::kw___attribute))
691 AttrList = ParseAttributes(); // FIXME: where should I put them?
692
693 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
694 LBraceLoc, RBraceLoc);
695
696 // C++ 9.2p2: Within the class member-specification, the class is regarded as
697 // complete within function bodies, default arguments,
698 // exception-specifications, and constructor ctor-initializers (including
699 // such things in nested classes).
700 //
Douglas Gregor605de8d2008-12-16 21:30:33 +0000701 // FIXME: Only function bodies and constructor ctor-initializers are
702 // parsed correctly, fix the rest.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000703 if (!CurScope->getParent()->isCXXClassScope()) {
704 // We are not inside a nested class. This class and its nested classes
Douglas Gregor605de8d2008-12-16 21:30:33 +0000705 // are complete and we can parse the delayed portions of method
706 // declarations and the lexed inline method definitions.
707 ParseLexedMethodDeclarations();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000708 ParseLexedMethodDefs();
709
710 // For a local class of inline method, pop the LexedMethodsForTopClass that
711 // was previously pushed.
712
Sanjiv Guptafa451432008-10-31 09:52:39 +0000713 assert((CurScope->isInCXXInlineMethodScope() ||
714 TopClassStacks.size() == 1) &&
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000715 "MethodLexers not getting popped properly!");
716 if (CurScope->isInCXXInlineMethodScope())
717 PopTopClassStack();
718 }
719
720 // Leave the class scope.
Douglas Gregor95d40792008-12-10 06:34:36 +0000721 ClassScope.Exit();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000722
Argiris Kirtzidis448b4e42008-08-09 00:39:29 +0000723 Actions.ActOnFinishCXXClassDef(TagDecl);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000724}
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000725
726/// ParseConstructorInitializer - Parse a C++ constructor initializer,
727/// which explicitly initializes the members or base classes of a
728/// class (C++ [class.base.init]). For example, the three initializers
729/// after the ':' in the Derived constructor below:
730///
731/// @code
732/// class Base { };
733/// class Derived : Base {
734/// int x;
735/// float f;
736/// public:
737/// Derived(float f) : Base(), x(17), f(f) { }
738/// };
739/// @endcode
740///
741/// [C++] ctor-initializer:
742/// ':' mem-initializer-list
743///
744/// [C++] mem-initializer-list:
745/// mem-initializer
746/// mem-initializer , mem-initializer-list
747void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) {
748 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
749
750 SourceLocation ColonLoc = ConsumeToken();
751
752 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
753
754 do {
755 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
756 if (!MemInit.isInvalid)
757 MemInitializers.push_back(MemInit.Val);
758
759 if (Tok.is(tok::comma))
760 ConsumeToken();
761 else if (Tok.is(tok::l_brace))
762 break;
763 else {
764 // Skip over garbage, until we get to '{'. Don't eat the '{'.
765 SkipUntil(tok::l_brace, true, true);
766 break;
767 }
768 } while (true);
769
770 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
771 &MemInitializers[0], MemInitializers.size());
772}
773
774/// ParseMemInitializer - Parse a C++ member initializer, which is
775/// part of a constructor initializer that explicitly initializes one
776/// member or base class (C++ [class.base.init]). See
777/// ParseConstructorInitializer for an example.
778///
779/// [C++] mem-initializer:
780/// mem-initializer-id '(' expression-list[opt] ')'
781///
782/// [C++] mem-initializer-id:
783/// '::'[opt] nested-name-specifier[opt] class-name
784/// identifier
785Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) {
786 // FIXME: parse '::'[opt] nested-name-specifier[opt]
787
788 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000789 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000790 return true;
791 }
792
793 // Get the identifier. This may be a member name or a class name,
794 // but we'll let the semantic analysis determine which it is.
795 IdentifierInfo *II = Tok.getIdentifierInfo();
796 SourceLocation IdLoc = ConsumeToken();
797
798 // Parse the '('.
799 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000800 Diag(Tok, diag::err_expected_lparen);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000801 return true;
802 }
803 SourceLocation LParenLoc = ConsumeParen();
804
805 // Parse the optional expression-list.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000806 ExprVector ArgExprs(Actions);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000807 CommaLocsTy CommaLocs;
808 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
809 SkipUntil(tok::r_paren);
810 return true;
811 }
812
813 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
814
Sebastian Redl6008ac32008-11-25 22:21:31 +0000815 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
816 LParenLoc, ArgExprs.take(),
817 ArgExprs.size(), &CommaLocs[0], RParenLoc);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000818}
Douglas Gregor90a2c972008-11-25 03:22:00 +0000819
820/// ParseExceptionSpecification - Parse a C++ exception-specification
821/// (C++ [except.spec]).
822///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +0000823/// exception-specification:
824/// 'throw' '(' type-id-list [opt] ')'
825/// [MS] 'throw' '(' '...' ')'
Douglas Gregor90a2c972008-11-25 03:22:00 +0000826///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +0000827/// type-id-list:
828/// type-id
829/// type-id-list ',' type-id
Douglas Gregor90a2c972008-11-25 03:22:00 +0000830///
831bool Parser::ParseExceptionSpecification() {
832 assert(Tok.is(tok::kw_throw) && "expected throw");
833
834 SourceLocation ThrowLoc = ConsumeToken();
835
836 if (!Tok.is(tok::l_paren)) {
837 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
838 }
839 SourceLocation LParenLoc = ConsumeParen();
840
Douglas Gregor9ed9ac82008-12-01 18:00:20 +0000841 // Parse throw(...), a Microsoft extension that means "this function
842 // can throw anything".
843 if (Tok.is(tok::ellipsis)) {
844 SourceLocation EllipsisLoc = ConsumeToken();
845 if (!getLang().Microsoft)
846 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
847 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
848 return false;
849 }
850
Douglas Gregor90a2c972008-11-25 03:22:00 +0000851 // Parse the sequence of type-ids.
852 while (Tok.isNot(tok::r_paren)) {
853 ParseTypeName();
854 if (Tok.is(tok::comma))
855 ConsumeToken();
856 else
857 break;
858 }
859
860 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
861 return false;
862}