blob: ec2a37a852408aa669281988f255f5d15e19011d [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 Lattnerf7b2e552007-08-25 06:57:03 +000019using namespace clang;
20
21/// ParseNamespace - We know that the current token is a namespace keyword. This
22/// may either be a top level namespace or a block-level namespace alias.
23///
24/// namespace-definition: [C++ 7.3: basic.namespace]
25/// named-namespace-definition
26/// unnamed-namespace-definition
27///
28/// unnamed-namespace-definition:
29/// 'namespace' attributes[opt] '{' namespace-body '}'
30///
31/// named-namespace-definition:
32/// original-namespace-definition
33/// extension-namespace-definition
34///
35/// original-namespace-definition:
36/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
37///
38/// extension-namespace-definition:
39/// 'namespace' original-namespace-name '{' namespace-body '}'
40///
41/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
42/// 'namespace' identifier '=' qualified-namespace-specifier ';'
43///
44Parser::DeclTy *Parser::ParseNamespace(unsigned Context) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000045 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnerf7b2e552007-08-25 06:57:03 +000046 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
47
48 SourceLocation IdentLoc;
49 IdentifierInfo *Ident = 0;
50
Chris Lattner34a01ad2007-10-09 17:33:22 +000051 if (Tok.is(tok::identifier)) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +000052 Ident = Tok.getIdentifierInfo();
53 IdentLoc = ConsumeToken(); // eat the identifier.
54 }
55
56 // Read label attributes, if present.
57 DeclTy *AttrList = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +000058 if (Tok.is(tok::kw___attribute))
Chris Lattnerf7b2e552007-08-25 06:57:03 +000059 // FIXME: save these somewhere.
60 AttrList = ParseAttributes();
61
Chris Lattner34a01ad2007-10-09 17:33:22 +000062 if (Tok.is(tok::equal)) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +000063 // FIXME: Verify no attributes were present.
64 // FIXME: parse this.
Chris Lattner34a01ad2007-10-09 17:33:22 +000065 } else if (Tok.is(tok::l_brace)) {
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000066
Chris Lattnerf7b2e552007-08-25 06:57:03 +000067 SourceLocation LBrace = ConsumeBrace();
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000068
69 // Enter a scope for the namespace.
Douglas Gregor95d40792008-12-10 06:34:36 +000070 ParseScope NamespaceScope(this, Scope::DeclScope);
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000071
72 DeclTy *NamespcDecl =
73 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
74
75 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
Chris Lattner9c135722007-08-25 18:15:16 +000076 ParseExternalDeclaration();
Chris Lattnerf7b2e552007-08-25 06:57:03 +000077
Argiris Kirtzidis5f21e592008-05-01 21:44:34 +000078 // Leave the namespace scope.
Douglas Gregor95d40792008-12-10 06:34:36 +000079 NamespaceScope.Exit();
Argiris Kirtzidis5f21e592008-05-01 21:44:34 +000080
Chris Lattnerf7b2e552007-08-25 06:57:03 +000081 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000082 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBrace);
83
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +000084 return NamespcDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +000085
Chris Lattnerf7b2e552007-08-25 06:57:03 +000086 } else {
Chris Lattnerf006a222008-11-18 07:48:38 +000087 Diag(Tok, Ident ? diag::err_expected_lbrace :
88 diag::err_expected_ident_lbrace);
Chris Lattnerf7b2e552007-08-25 06:57:03 +000089 }
90
91 return 0;
92}
Chris Lattner806a5f52008-01-12 07:05:38 +000093
94/// ParseLinkage - We know that the current token is a string_literal
95/// and just before that, that extern was seen.
96///
97/// linkage-specification: [C++ 7.5p2: dcl.link]
98/// 'extern' string-literal '{' declaration-seq[opt] '}'
99/// 'extern' string-literal declaration
100///
101Parser::DeclTy *Parser::ParseLinkage(unsigned Context) {
Douglas Gregor61818c52008-11-21 16:10:08 +0000102 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattner806a5f52008-01-12 07:05:38 +0000103 llvm::SmallVector<char, 8> LangBuffer;
104 // LangBuffer is guaranteed to be big enough.
105 LangBuffer.resize(Tok.getLength());
106 const char *LangBufPtr = &LangBuffer[0];
107 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
108
109 SourceLocation Loc = ConsumeStringToken();
110 DeclTy *D = 0;
Chris Lattner806a5f52008-01-12 07:05:38 +0000111
112 if (Tok.isNot(tok::l_brace)) {
Douglas Gregor61818c52008-11-21 16:10:08 +0000113 D = ParseDeclarationOrFunctionDefinition();
Douglas Gregorad17e372008-12-16 22:23:02 +0000114 if (D)
115 return Actions.ActOnLinkageSpec(Loc, LangBufPtr, StrSize, D);
Chris Lattner806a5f52008-01-12 07:05:38 +0000116
Douglas Gregorad17e372008-12-16 22:23:02 +0000117 return 0;
118 }
119
120 SourceLocation LBrace = ConsumeBrace();
121 llvm::SmallVector<DeclTy *, 8> InnerDecls;
122 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
123 D = ParseExternalDeclaration();
124 if (D)
125 InnerDecls.push_back(D);
Chris Lattner806a5f52008-01-12 07:05:38 +0000126 }
127
Douglas Gregorad17e372008-12-16 22:23:02 +0000128 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
129 return Actions.ActOnLinkageSpec(Loc, LBrace, RBrace, LangBufPtr, StrSize,
130 &InnerDecls.front(), InnerDecls.size());
Chris Lattner806a5f52008-01-12 07:05:38 +0000131}
Douglas Gregorec93f442008-04-13 21:30:24 +0000132
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000133/// ParseClassName - Parse a C++ class-name, which names a class. Note
134/// that we only check that the result names a type; semantic analysis
135/// will need to verify that the type names a class. The result is
136/// either a type or NULL, dependending on whether a type name was
137/// found.
138///
139/// class-name: [C++ 9.1]
140/// identifier
141/// template-id [TODO]
142///
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000143Parser::TypeTy *Parser::ParseClassName(const CXXScopeSpec *SS) {
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000144 // Parse the class-name.
145 // FIXME: Alternatively, parse a simple-template-id.
146 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000147 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000148 return 0;
149 }
150
151 // We have an identifier; check whether it is actually a type.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000152 TypeTy *Type = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope, SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000153 if (!Type) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000154 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000155 return 0;
156 }
157
158 // Consume the identifier.
159 ConsumeToken();
160
161 return Type;
162}
163
Douglas Gregorec93f442008-04-13 21:30:24 +0000164/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
165/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
166/// until we reach the start of a definition or see a token that
167/// cannot start a definition.
168///
169/// class-specifier: [C++ class]
170/// class-head '{' member-specification[opt] '}'
171/// class-head '{' member-specification[opt] '}' attributes[opt]
172/// class-head:
173/// class-key identifier[opt] base-clause[opt]
174/// class-key nested-name-specifier identifier base-clause[opt]
175/// class-key nested-name-specifier[opt] simple-template-id
176/// base-clause[opt]
177/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
178/// [GNU] class-key attributes[opt] nested-name-specifier
179/// identifier base-clause[opt]
180/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
181/// simple-template-id base-clause[opt]
182/// class-key:
183/// 'class'
184/// 'struct'
185/// 'union'
186///
187/// elaborated-type-specifier: [C++ dcl.type.elab]
188/// class-key ::[opt] nested-name-specifier[opt] identifier
189/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
190/// simple-template-id
191///
192/// Note that the C++ class-specifier and elaborated-type-specifier,
193/// together, subsume the C99 struct-or-union-specifier:
194///
195/// struct-or-union-specifier: [C99 6.7.2.1]
196/// struct-or-union identifier[opt] '{' struct-contents '}'
197/// struct-or-union identifier
198/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
199/// '}' attributes[opt]
200/// [GNU] struct-or-union attributes[opt] identifier
201/// struct-or-union:
202/// 'struct'
203/// 'union'
204void Parser::ParseClassSpecifier(DeclSpec &DS) {
205 assert((Tok.is(tok::kw_class) ||
206 Tok.is(tok::kw_struct) ||
207 Tok.is(tok::kw_union)) &&
208 "Not a class specifier");
209 DeclSpec::TST TagType =
210 Tok.is(tok::kw_class) ? DeclSpec::TST_class :
211 Tok.is(tok::kw_struct) ? DeclSpec::TST_struct :
212 DeclSpec::TST_union;
213
214 SourceLocation StartLoc = ConsumeToken();
215
216 AttributeList *Attr = 0;
217 // If attributes exist after tag, parse them.
218 if (Tok.is(tok::kw___attribute))
219 Attr = ParseAttributes();
220
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000221 // Parse the (optional) nested-name-specifier.
222 CXXScopeSpec SS;
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +0000223 if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000224 if (Tok.isNot(tok::identifier))
225 Diag(Tok, diag::err_expected_ident);
226 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000227
228 // Parse the (optional) class name.
229 // FIXME: Alternatively, parse a simple-template-id.
230 IdentifierInfo *Name = 0;
231 SourceLocation NameLoc;
232 if (Tok.is(tok::identifier)) {
233 Name = Tok.getIdentifierInfo();
234 NameLoc = ConsumeToken();
235 }
236
237 // There are three options here. If we have 'struct foo;', then
238 // this is a forward declaration. If we have 'struct foo {...' or
239 // 'struct fo :...' then this is a definition. Otherwise we have
240 // something like 'struct foo xyz', a reference.
241 Action::TagKind TK;
242 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
243 TK = Action::TK_Definition;
244 else if (Tok.is(tok::semi))
245 TK = Action::TK_Declaration;
246 else
247 TK = Action::TK_Reference;
248
249 if (!Name && TK != Action::TK_Definition) {
250 // We have a declaration or reference to an anonymous class.
Chris Lattnerf006a222008-11-18 07:48:38 +0000251 Diag(StartLoc, diag::err_anon_type_definition)
252 << DeclSpec::getSpecifierName(TagType);
Douglas Gregorec93f442008-04-13 21:30:24 +0000253
254 // Skip the rest of this declarator, up until the comma or semicolon.
255 SkipUntil(tok::comma, true);
256 return;
257 }
258
259 // Parse the tag portion of this.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000260 DeclTy *TagDecl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name,
Douglas Gregorec93f442008-04-13 21:30:24 +0000261 NameLoc, Attr);
262
263 // Parse the optional base clause (C++ only).
264 if (getLang().CPlusPlus && Tok.is(tok::colon)) {
265 ParseBaseClause(TagDecl);
266 }
267
268 // If there is a body, parse it and inform the actions module.
269 if (Tok.is(tok::l_brace))
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000270 if (getLang().CPlusPlus)
271 ParseCXXMemberSpecification(StartLoc, TagType, TagDecl);
272 else
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000273 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Douglas Gregorec93f442008-04-13 21:30:24 +0000274 else if (TK == Action::TK_Definition) {
275 // FIXME: Complain that we have a base-specifier list but no
276 // definition.
Chris Lattnerf006a222008-11-18 07:48:38 +0000277 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregorec93f442008-04-13 21:30:24 +0000278 }
279
280 const char *PrevSpec = 0;
281 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +0000282 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Douglas Gregorec93f442008-04-13 21:30:24 +0000283}
284
285/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
286///
287/// base-clause : [C++ class.derived]
288/// ':' base-specifier-list
289/// base-specifier-list:
290/// base-specifier '...'[opt]
291/// base-specifier-list ',' base-specifier '...'[opt]
292void Parser::ParseBaseClause(DeclTy *ClassDecl)
293{
294 assert(Tok.is(tok::colon) && "Not a base clause");
295 ConsumeToken();
296
Douglas Gregorabed2172008-10-22 17:49:05 +0000297 // Build up an array of parsed base specifiers.
298 llvm::SmallVector<BaseTy *, 8> BaseInfo;
299
Douglas Gregorec93f442008-04-13 21:30:24 +0000300 while (true) {
301 // Parse a base-specifier.
Douglas Gregorabed2172008-10-22 17:49:05 +0000302 BaseResult Result = ParseBaseSpecifier(ClassDecl);
303 if (Result.isInvalid) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000304 // Skip the rest of this base specifier, up until the comma or
305 // opening brace.
Douglas Gregorabed2172008-10-22 17:49:05 +0000306 SkipUntil(tok::comma, tok::l_brace, true, true);
307 } else {
308 // Add this to our array of base specifiers.
309 BaseInfo.push_back(Result.Val);
Douglas Gregorec93f442008-04-13 21:30:24 +0000310 }
311
312 // If the next token is a comma, consume it and keep reading
313 // base-specifiers.
314 if (Tok.isNot(tok::comma)) break;
315
316 // Consume the comma.
317 ConsumeToken();
318 }
Douglas Gregorabed2172008-10-22 17:49:05 +0000319
320 // Attach the base specifiers
321 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregorec93f442008-04-13 21:30:24 +0000322}
323
324/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
325/// one entry in the base class list of a class specifier, for example:
326/// class foo : public bar, virtual private baz {
327/// 'public bar' and 'virtual private baz' are each base-specifiers.
328///
329/// base-specifier: [C++ class.derived]
330/// ::[opt] nested-name-specifier[opt] class-name
331/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
332/// class-name
333/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
334/// class-name
Douglas Gregorabed2172008-10-22 17:49:05 +0000335Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
Douglas Gregorec93f442008-04-13 21:30:24 +0000336{
337 bool IsVirtual = false;
338 SourceLocation StartLoc = Tok.getLocation();
339
340 // Parse the 'virtual' keyword.
341 if (Tok.is(tok::kw_virtual)) {
342 ConsumeToken();
343 IsVirtual = true;
344 }
345
346 // Parse an (optional) access specifier.
347 AccessSpecifier Access = getAccessSpecifierIfPresent();
348 if (Access)
349 ConsumeToken();
350
351 // Parse the 'virtual' keyword (again!), in case it came after the
352 // access specifier.
353 if (Tok.is(tok::kw_virtual)) {
354 SourceLocation VirtualLoc = ConsumeToken();
355 if (IsVirtual) {
356 // Complain about duplicate 'virtual'
Chris Lattnerf006a222008-11-18 07:48:38 +0000357 Diag(VirtualLoc, diag::err_dup_virtual)
358 << SourceRange(VirtualLoc, VirtualLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000359 }
360
361 IsVirtual = true;
362 }
363
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000364 // Parse optional '::' and optional nested-name-specifier.
365 CXXScopeSpec SS;
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +0000366 MaybeParseCXXScopeSpecifier(SS);
Douglas Gregorec93f442008-04-13 21:30:24 +0000367
Douglas Gregorec93f442008-04-13 21:30:24 +0000368 // The location of the base class itself.
369 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000370
371 // Parse the class-name.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000372 TypeTy *BaseType = ParseClassName(&SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000373 if (!BaseType)
374 return true;
Douglas Gregorec93f442008-04-13 21:30:24 +0000375
376 // Find the complete source range for the base-specifier.
377 SourceRange Range(StartLoc, BaseLoc);
378
Douglas Gregorec93f442008-04-13 21:30:24 +0000379 // Notify semantic analysis that we have parsed a complete
380 // base-specifier.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000381 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
382 BaseType, BaseLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000383}
384
385/// getAccessSpecifierIfPresent - Determine whether the next token is
386/// a C++ access-specifier.
387///
388/// access-specifier: [C++ class.derived]
389/// 'private'
390/// 'protected'
391/// 'public'
Douglas Gregor696be932008-04-14 00:13:42 +0000392AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregorec93f442008-04-13 21:30:24 +0000393{
394 switch (Tok.getKind()) {
395 default: return AS_none;
396 case tok::kw_private: return AS_private;
397 case tok::kw_protected: return AS_protected;
398 case tok::kw_public: return AS_public;
399 }
400}
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000401
402/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
403///
404/// member-declaration:
405/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
406/// function-definition ';'[opt]
407/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
408/// using-declaration [TODO]
409/// [C++0x] static_assert-declaration [TODO]
410/// template-declaration [TODO]
411///
412/// member-declarator-list:
413/// member-declarator
414/// member-declarator-list ',' member-declarator
415///
416/// member-declarator:
417/// declarator pure-specifier[opt]
418/// declarator constant-initializer[opt]
419/// identifier[opt] ':' constant-expression
420///
421/// pure-specifier: [TODO]
422/// '= 0'
423///
424/// constant-initializer:
425/// '=' constant-expression
426///
427Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
428 SourceLocation DSStart = Tok.getLocation();
429 // decl-specifier-seq:
430 // Parse the common declaration-specifiers piece.
431 DeclSpec DS;
432 ParseDeclarationSpecifiers(DS);
433
434 if (Tok.is(tok::semi)) {
435 ConsumeToken();
436 // C++ 9.2p7: The member-declarator-list can be omitted only after a
437 // class-specifier or an enum-specifier or in a friend declaration.
438 // FIXME: Friend declarations.
439 switch (DS.getTypeSpecType()) {
440 case DeclSpec::TST_struct:
441 case DeclSpec::TST_union:
442 case DeclSpec::TST_class:
443 case DeclSpec::TST_enum:
444 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
445 default:
446 Diag(DSStart, diag::err_no_declarators);
447 return 0;
448 }
449 }
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000450
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000451 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000452
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000453 if (Tok.isNot(tok::colon)) {
454 // Parse the first declarator.
455 ParseDeclarator(DeclaratorInfo);
456 // Error parsing the declarator?
Douglas Gregor6704b312008-11-17 22:58:34 +0000457 if (!DeclaratorInfo.hasName()) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000458 // If so, skip until the semi-colon or a }.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000459 SkipUntil(tok::r_brace, true);
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000460 if (Tok.is(tok::semi))
461 ConsumeToken();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000462 return 0;
463 }
464
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000465 // function-definition:
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000466 if (Tok.is(tok::l_brace)
467 || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000468 if (!DeclaratorInfo.isFunctionDeclarator()) {
469 Diag(Tok, diag::err_func_def_no_params);
470 ConsumeBrace();
471 SkipUntil(tok::r_brace, true);
472 return 0;
473 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000474
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000475 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
476 Diag(Tok, diag::err_function_declared_typedef);
477 // This recovery skips the entire function body. It would be nice
478 // to simply call ParseCXXInlineMethodDef() below, however Sema
479 // assumes the declarator represents a function, not a typedef.
480 ConsumeBrace();
481 SkipUntil(tok::r_brace, true);
482 return 0;
483 }
484
485 return ParseCXXInlineMethodDef(AS, DeclaratorInfo);
486 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000487 }
488
489 // member-declarator-list:
490 // member-declarator
491 // member-declarator-list ',' member-declarator
492
493 DeclTy *LastDeclInGroup = 0;
Sebastian Redl62261042008-12-09 20:22:58 +0000494 OwningExprResult BitfieldSize(Actions);
495 OwningExprResult Init(Actions);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000496
497 while (1) {
498
499 // member-declarator:
500 // declarator pure-specifier[opt]
501 // declarator constant-initializer[opt]
502 // identifier[opt] ':' constant-expression
503
504 if (Tok.is(tok::colon)) {
505 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000506 BitfieldSize = ParseConstantExpression();
507 if (BitfieldSize.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000508 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000509 }
510
511 // pure-specifier:
512 // '= 0'
513 //
514 // constant-initializer:
515 // '=' constant-expression
516
517 if (Tok.is(tok::equal)) {
518 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000519 Init = ParseInitializer();
520 if (Init.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000521 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000522 }
523
524 // If attributes exist after the declarator, parse them.
525 if (Tok.is(tok::kw___attribute))
526 DeclaratorInfo.AddAttributes(ParseAttributes());
527
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000528 // NOTE: If Sema is the Action module and declarator is an instance field,
529 // this call will *not* return the created decl; LastDeclInGroup will be
530 // returned instead.
531 // See Sema::ActOnCXXMemberDeclarator for details.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000532 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
533 DeclaratorInfo,
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000534 BitfieldSize.release(),
535 Init.release(),
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000536 LastDeclInGroup);
537
Douglas Gregor605de8d2008-12-16 21:30:33 +0000538 if (DeclaratorInfo.isFunctionDeclarator() &&
539 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
540 != DeclSpec::SCS_typedef) {
541 // We just declared a member function. If this member function
542 // has any default arguments, we'll need to parse them later.
543 LateParsedMethodDeclaration *LateMethod = 0;
544 DeclaratorChunk::FunctionTypeInfo &FTI
545 = DeclaratorInfo.getTypeObject(0).Fun;
546 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
547 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
548 if (!LateMethod) {
549 // Push this method onto the stack of late-parsed method
550 // declarations.
551 getCurTopClassStack().MethodDecls.push_back(
552 LateParsedMethodDeclaration(LastDeclInGroup));
553 LateMethod = &getCurTopClassStack().MethodDecls.back();
554
555 // Add all of the parameters prior to this one (they don't
556 // have default arguments).
557 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
558 for (unsigned I = 0; I < ParamIdx; ++I)
559 LateMethod->DefaultArgs.push_back(
560 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
561 }
562
563 // Add this parameter to the list of parameters (it or may
564 // not have a default argument).
565 LateMethod->DefaultArgs.push_back(
566 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
567 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
568 }
569 }
570 }
571
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000572 // If we don't have a comma, it is either the end of the list (a ';')
573 // or an error, bail out.
574 if (Tok.isNot(tok::comma))
575 break;
576
577 // Consume the comma.
578 ConsumeToken();
579
580 // Parse the next declarator.
581 DeclaratorInfo.clear();
Sebastian Redl62261042008-12-09 20:22:58 +0000582 BitfieldSize = 0;
583 Init = 0;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000584
585 // Attributes are only allowed on the second declarator.
586 if (Tok.is(tok::kw___attribute))
587 DeclaratorInfo.AddAttributes(ParseAttributes());
588
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000589 if (Tok.isNot(tok::colon))
590 ParseDeclarator(DeclaratorInfo);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000591 }
592
593 if (Tok.is(tok::semi)) {
594 ConsumeToken();
595 // Reverse the chain list.
596 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
597 }
598
599 Diag(Tok, diag::err_expected_semi_decl_list);
600 // Skip to end of block or statement
601 SkipUntil(tok::r_brace, true, true);
602 if (Tok.is(tok::semi))
603 ConsumeToken();
604 return 0;
605}
606
607/// ParseCXXMemberSpecification - Parse the class definition.
608///
609/// member-specification:
610/// member-declaration member-specification[opt]
611/// access-specifier ':' member-specification[opt]
612///
613void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
614 unsigned TagType, DeclTy *TagDecl) {
Sanjiv Guptafa451432008-10-31 09:52:39 +0000615 assert((TagType == DeclSpec::TST_struct ||
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000616 TagType == DeclSpec::TST_union ||
Sanjiv Guptafa451432008-10-31 09:52:39 +0000617 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000618
619 SourceLocation LBraceLoc = ConsumeBrace();
620
621 if (!CurScope->isCXXClassScope() && // Not about to define a nested class.
622 CurScope->isInCXXInlineMethodScope()) {
623 // We will define a local class of an inline method.
624 // Push a new LexedMethodsForTopClass for its inline methods.
625 PushTopClassStack();
626 }
627
628 // Enter a scope for the class.
Douglas Gregor95d40792008-12-10 06:34:36 +0000629 ParseScope ClassScope(this, Scope::CXXClassScope|Scope::DeclScope);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000630
631 Actions.ActOnStartCXXClassDef(CurScope, TagDecl, LBraceLoc);
632
633 // C++ 11p3: Members of a class defined with the keyword class are private
634 // by default. Members of a class defined with the keywords struct or union
635 // are public by default.
636 AccessSpecifier CurAS;
637 if (TagType == DeclSpec::TST_class)
638 CurAS = AS_private;
639 else
640 CurAS = AS_public;
641
642 // While we still have something to read, read the member-declarations.
643 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
644 // Each iteration of this loop reads one member-declaration.
645
646 // Check for extraneous top-level semicolon.
647 if (Tok.is(tok::semi)) {
648 Diag(Tok, diag::ext_extra_struct_semi);
649 ConsumeToken();
650 continue;
651 }
652
653 AccessSpecifier AS = getAccessSpecifierIfPresent();
654 if (AS != AS_none) {
655 // Current token is a C++ access specifier.
656 CurAS = AS;
657 ConsumeToken();
658 ExpectAndConsume(tok::colon, diag::err_expected_colon);
659 continue;
660 }
661
662 // Parse all the comma separated declarators.
663 ParseCXXClassMemberDeclaration(CurAS);
664 }
665
666 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
667
668 AttributeList *AttrList = 0;
669 // If attributes exist after class contents, parse them.
670 if (Tok.is(tok::kw___attribute))
671 AttrList = ParseAttributes(); // FIXME: where should I put them?
672
673 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
674 LBraceLoc, RBraceLoc);
675
676 // C++ 9.2p2: Within the class member-specification, the class is regarded as
677 // complete within function bodies, default arguments,
678 // exception-specifications, and constructor ctor-initializers (including
679 // such things in nested classes).
680 //
Douglas Gregor605de8d2008-12-16 21:30:33 +0000681 // FIXME: Only function bodies and constructor ctor-initializers are
682 // parsed correctly, fix the rest.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000683 if (!CurScope->getParent()->isCXXClassScope()) {
684 // We are not inside a nested class. This class and its nested classes
Douglas Gregor605de8d2008-12-16 21:30:33 +0000685 // are complete and we can parse the delayed portions of method
686 // declarations and the lexed inline method definitions.
687 ParseLexedMethodDeclarations();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000688 ParseLexedMethodDefs();
689
690 // For a local class of inline method, pop the LexedMethodsForTopClass that
691 // was previously pushed.
692
Sanjiv Guptafa451432008-10-31 09:52:39 +0000693 assert((CurScope->isInCXXInlineMethodScope() ||
694 TopClassStacks.size() == 1) &&
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000695 "MethodLexers not getting popped properly!");
696 if (CurScope->isInCXXInlineMethodScope())
697 PopTopClassStack();
698 }
699
700 // Leave the class scope.
Douglas Gregor95d40792008-12-10 06:34:36 +0000701 ClassScope.Exit();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000702
Argiris Kirtzidis448b4e42008-08-09 00:39:29 +0000703 Actions.ActOnFinishCXXClassDef(TagDecl);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000704}
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000705
706/// ParseConstructorInitializer - Parse a C++ constructor initializer,
707/// which explicitly initializes the members or base classes of a
708/// class (C++ [class.base.init]). For example, the three initializers
709/// after the ':' in the Derived constructor below:
710///
711/// @code
712/// class Base { };
713/// class Derived : Base {
714/// int x;
715/// float f;
716/// public:
717/// Derived(float f) : Base(), x(17), f(f) { }
718/// };
719/// @endcode
720///
721/// [C++] ctor-initializer:
722/// ':' mem-initializer-list
723///
724/// [C++] mem-initializer-list:
725/// mem-initializer
726/// mem-initializer , mem-initializer-list
727void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) {
728 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
729
730 SourceLocation ColonLoc = ConsumeToken();
731
732 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
733
734 do {
735 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
736 if (!MemInit.isInvalid)
737 MemInitializers.push_back(MemInit.Val);
738
739 if (Tok.is(tok::comma))
740 ConsumeToken();
741 else if (Tok.is(tok::l_brace))
742 break;
743 else {
744 // Skip over garbage, until we get to '{'. Don't eat the '{'.
745 SkipUntil(tok::l_brace, true, true);
746 break;
747 }
748 } while (true);
749
750 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
751 &MemInitializers[0], MemInitializers.size());
752}
753
754/// ParseMemInitializer - Parse a C++ member initializer, which is
755/// part of a constructor initializer that explicitly initializes one
756/// member or base class (C++ [class.base.init]). See
757/// ParseConstructorInitializer for an example.
758///
759/// [C++] mem-initializer:
760/// mem-initializer-id '(' expression-list[opt] ')'
761///
762/// [C++] mem-initializer-id:
763/// '::'[opt] nested-name-specifier[opt] class-name
764/// identifier
765Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) {
766 // FIXME: parse '::'[opt] nested-name-specifier[opt]
767
768 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000769 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000770 return true;
771 }
772
773 // Get the identifier. This may be a member name or a class name,
774 // but we'll let the semantic analysis determine which it is.
775 IdentifierInfo *II = Tok.getIdentifierInfo();
776 SourceLocation IdLoc = ConsumeToken();
777
778 // Parse the '('.
779 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000780 Diag(Tok, diag::err_expected_lparen);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000781 return true;
782 }
783 SourceLocation LParenLoc = ConsumeParen();
784
785 // Parse the optional expression-list.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000786 ExprVector ArgExprs(Actions);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000787 CommaLocsTy CommaLocs;
788 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
789 SkipUntil(tok::r_paren);
790 return true;
791 }
792
793 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
794
Sebastian Redl6008ac32008-11-25 22:21:31 +0000795 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
796 LParenLoc, ArgExprs.take(),
797 ArgExprs.size(), &CommaLocs[0], RParenLoc);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000798}
Douglas Gregor90a2c972008-11-25 03:22:00 +0000799
800/// ParseExceptionSpecification - Parse a C++ exception-specification
801/// (C++ [except.spec]).
802///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +0000803/// exception-specification:
804/// 'throw' '(' type-id-list [opt] ')'
805/// [MS] 'throw' '(' '...' ')'
Douglas Gregor90a2c972008-11-25 03:22:00 +0000806///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +0000807/// type-id-list:
808/// type-id
809/// type-id-list ',' type-id
Douglas Gregor90a2c972008-11-25 03:22:00 +0000810///
811bool Parser::ParseExceptionSpecification() {
812 assert(Tok.is(tok::kw_throw) && "expected throw");
813
814 SourceLocation ThrowLoc = ConsumeToken();
815
816 if (!Tok.is(tok::l_paren)) {
817 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
818 }
819 SourceLocation LParenLoc = ConsumeParen();
820
Douglas Gregor9ed9ac82008-12-01 18:00:20 +0000821 // Parse throw(...), a Microsoft extension that means "this function
822 // can throw anything".
823 if (Tok.is(tok::ellipsis)) {
824 SourceLocation EllipsisLoc = ConsumeToken();
825 if (!getLang().Microsoft)
826 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
827 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
828 return false;
829 }
830
Douglas Gregor90a2c972008-11-25 03:22:00 +0000831 // Parse the sequence of type-ids.
832 while (Tok.isNot(tok::r_paren)) {
833 ParseTypeName();
834 if (Tok.is(tok::comma))
835 ConsumeToken();
836 else
837 break;
838 }
839
840 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
841 return false;
842}