blob: 59c086f8131dc9c8879ab802e472458cc6e9a558 [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"
Chris Lattner545f39e2009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregorec93f442008-04-13 21:30:24 +000016#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();
Chris Lattner806a5f52008-01-12 07:05:38 +0000111
Douglas Gregord8028382009-01-05 19:45:36 +0000112 ParseScope LinkageScope(this, Scope::DeclScope);
113 DeclTy *LinkageSpec
114 = Actions.ActOnStartLinkageSpecification(CurScope,
115 /*FIXME: */SourceLocation(),
116 Loc, LangBufPtr, StrSize,
117 Tok.is(tok::l_brace)? Tok.getLocation()
118 : SourceLocation());
119
120 if (Tok.isNot(tok::l_brace)) {
121 ParseDeclarationOrFunctionDefinition();
122 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
123 SourceLocation());
Douglas Gregorad17e372008-12-16 22:23:02 +0000124 }
125
126 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorad17e372008-12-16 22:23:02 +0000127 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregord8028382009-01-05 19:45:36 +0000128 ParseExternalDeclaration();
Chris Lattner806a5f52008-01-12 07:05:38 +0000129 }
130
Douglas Gregorad17e372008-12-16 22:23:02 +0000131 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregord8028382009-01-05 19:45:36 +0000132 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattner806a5f52008-01-12 07:05:38 +0000133}
Douglas Gregorec93f442008-04-13 21:30:24 +0000134
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000135/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
136/// using-directive. Assumes that current token is 'using'.
Chris Lattner08ab4162009-01-06 06:55:51 +0000137Parser::DeclTy *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context) {
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000138 assert(Tok.is(tok::kw_using) && "Not using token");
139
140 // Eat 'using'.
141 SourceLocation UsingLoc = ConsumeToken();
142
Chris Lattner08ab4162009-01-06 06:55:51 +0000143 if (Tok.is(tok::kw_namespace))
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000144 // Next token after 'using' is 'namespace' so it must be using-directive
145 return ParseUsingDirective(Context, UsingLoc);
Chris Lattner08ab4162009-01-06 06:55:51 +0000146
147 // Otherwise, it must be using-declaration.
148 return ParseUsingDeclaration(Context, UsingLoc);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000149}
150
151/// ParseUsingDirective - Parse C++ using-directive, assumes
152/// that current token is 'namespace' and 'using' was already parsed.
153///
154/// using-directive: [C++ 7.3.p4: namespace.udir]
155/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
156/// namespace-name ;
157/// [GNU] using-directive:
158/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
159/// namespace-name attributes[opt] ;
160///
161Parser::DeclTy *Parser::ParseUsingDirective(unsigned Context,
162 SourceLocation UsingLoc) {
163 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
164
165 // Eat 'namespace'.
166 SourceLocation NamespcLoc = ConsumeToken();
167
168 CXXScopeSpec SS;
169 // Parse (optional) nested-name-specifier.
Chris Lattnerd706dc82009-01-06 06:59:53 +0000170 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000171
172 AttributeList *AttrList = 0;
173 IdentifierInfo *NamespcName = 0;
174 SourceLocation IdentLoc = SourceLocation();
175
176 // Parse namespace-name.
Chris Lattner7898bf62009-01-06 07:27:21 +0000177 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000178 Diag(Tok, diag::err_expected_namespace_name);
179 // If there was invalid namespace name, skip to end of decl, and eat ';'.
180 SkipUntil(tok::semi);
181 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
182 return 0;
183 }
Chris Lattner7898bf62009-01-06 07:27:21 +0000184
185 // Parse identifier.
186 NamespcName = Tok.getIdentifierInfo();
187 IdentLoc = ConsumeToken();
188
189 // Parse (optional) attributes (most likely GNU strong-using extension).
190 if (Tok.is(tok::kw___attribute))
191 AttrList = ParseAttributes();
192
193 // Eat ';'.
194 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
195 AttrList ? "attributes list" : "namespace name", tok::semi);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000196
197 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner7898bf62009-01-06 07:27:21 +0000198 IdentLoc, NamespcName, AttrList);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000199}
200
201/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
202/// 'using' was already seen.
203///
204/// using-declaration: [C++ 7.3.p3: namespace.udecl]
205/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
206/// unqualified-id [TODO]
207/// 'using' :: unqualified-id [TODO]
208///
209Parser::DeclTy *Parser::ParseUsingDeclaration(unsigned Context,
210 SourceLocation UsingLoc) {
211 assert(false && "Not implemented");
212 // FIXME: Implement parsing.
213 return 0;
214}
215
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000216/// ParseClassName - Parse a C++ class-name, which names a class. Note
217/// that we only check that the result names a type; semantic analysis
218/// will need to verify that the type names a class. The result is
219/// either a type or NULL, dependending on whether a type name was
220/// found.
221///
222/// class-name: [C++ 9.1]
223/// identifier
224/// template-id [TODO]
225///
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000226Parser::TypeTy *Parser::ParseClassName(const CXXScopeSpec *SS) {
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000227 // Parse the class-name.
228 // FIXME: Alternatively, parse a simple-template-id.
229 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000230 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000231 return 0;
232 }
233
234 // We have an identifier; check whether it is actually a type.
Douglas Gregor1075a162009-02-04 17:00:24 +0000235 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
236 Tok.getLocation(), CurScope, SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000237 if (!Type) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000238 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000239 return 0;
240 }
241
242 // Consume the identifier.
243 ConsumeToken();
244
245 return Type;
246}
247
Douglas Gregorec93f442008-04-13 21:30:24 +0000248/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
249/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
250/// until we reach the start of a definition or see a token that
251/// cannot start a definition.
252///
253/// class-specifier: [C++ class]
254/// class-head '{' member-specification[opt] '}'
255/// class-head '{' member-specification[opt] '}' attributes[opt]
256/// class-head:
257/// class-key identifier[opt] base-clause[opt]
258/// class-key nested-name-specifier identifier base-clause[opt]
259/// class-key nested-name-specifier[opt] simple-template-id
260/// base-clause[opt]
261/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
262/// [GNU] class-key attributes[opt] nested-name-specifier
263/// identifier base-clause[opt]
264/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
265/// simple-template-id base-clause[opt]
266/// class-key:
267/// 'class'
268/// 'struct'
269/// 'union'
270///
271/// elaborated-type-specifier: [C++ dcl.type.elab]
272/// class-key ::[opt] nested-name-specifier[opt] identifier
273/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
274/// simple-template-id
275///
276/// Note that the C++ class-specifier and elaborated-type-specifier,
277/// together, subsume the C99 struct-or-union-specifier:
278///
279/// struct-or-union-specifier: [C99 6.7.2.1]
280/// struct-or-union identifier[opt] '{' struct-contents '}'
281/// struct-or-union identifier
282/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
283/// '}' attributes[opt]
284/// [GNU] struct-or-union attributes[opt] identifier
285/// struct-or-union:
286/// 'struct'
287/// 'union'
Douglas Gregor52473432008-12-24 02:52:09 +0000288void Parser::ParseClassSpecifier(DeclSpec &DS,
289 TemplateParameterLists *TemplateParams) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000290 assert((Tok.is(tok::kw_class) ||
291 Tok.is(tok::kw_struct) ||
292 Tok.is(tok::kw_union)) &&
293 "Not a class specifier");
294 DeclSpec::TST TagType =
295 Tok.is(tok::kw_class) ? DeclSpec::TST_class :
296 Tok.is(tok::kw_struct) ? DeclSpec::TST_struct :
297 DeclSpec::TST_union;
298
299 SourceLocation StartLoc = ConsumeToken();
300
301 AttributeList *Attr = 0;
302 // If attributes exist after tag, parse them.
303 if (Tok.is(tok::kw___attribute))
304 Attr = ParseAttributes();
305
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000306 // If declspecs exist after tag, parse them.
307 if (Tok.is(tok::kw___declspec) && PP.getLangOptions().Microsoft)
308 FuzzyParseMicrosoftDeclSpec();
309
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000310 // Parse the (optional) nested-name-specifier.
311 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +0000312 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000313 if (Tok.isNot(tok::identifier))
314 Diag(Tok, diag::err_expected_ident);
315 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000316
317 // Parse the (optional) class name.
318 // FIXME: Alternatively, parse a simple-template-id.
319 IdentifierInfo *Name = 0;
320 SourceLocation NameLoc;
321 if (Tok.is(tok::identifier)) {
322 Name = Tok.getIdentifierInfo();
323 NameLoc = ConsumeToken();
324 }
325
326 // There are three options here. If we have 'struct foo;', then
327 // this is a forward declaration. If we have 'struct foo {...' or
328 // 'struct fo :...' then this is a definition. Otherwise we have
329 // something like 'struct foo xyz', a reference.
330 Action::TagKind TK;
331 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
332 TK = Action::TK_Definition;
333 else if (Tok.is(tok::semi))
334 TK = Action::TK_Declaration;
335 else
336 TK = Action::TK_Reference;
337
338 if (!Name && TK != Action::TK_Definition) {
339 // We have a declaration or reference to an anonymous class.
Chris Lattnerf006a222008-11-18 07:48:38 +0000340 Diag(StartLoc, diag::err_anon_type_definition)
341 << DeclSpec::getSpecifierName(TagType);
Douglas Gregorec93f442008-04-13 21:30:24 +0000342
343 // Skip the rest of this declarator, up until the comma or semicolon.
344 SkipUntil(tok::comma, true);
345 return;
346 }
347
Douglas Gregord406b032009-02-06 22:42:48 +0000348 // Create the tag portion of the class or class template.
349 DeclTy *TagOrTempDecl;
350 if (TemplateParams && TK != Action::TK_Reference)
351 TagOrTempDecl = Actions.ActOnClassTemplate(CurScope, TagType, TK, StartLoc,
352 SS, Name, NameLoc, Attr,
353 Action::MultiTemplateParamsArg(Actions,
354 &(*TemplateParams)[0],
355 TemplateParams->size()));
356 else
357 TagOrTempDecl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name,
358 NameLoc, Attr);
Douglas Gregorec93f442008-04-13 21:30:24 +0000359
360 // Parse the optional base clause (C++ only).
361 if (getLang().CPlusPlus && Tok.is(tok::colon)) {
Douglas Gregor279272e2009-02-04 19:02:06 +0000362 ParseBaseClause(TagOrTempDecl);
Douglas Gregorec93f442008-04-13 21:30:24 +0000363 }
364
365 // If there is a body, parse it and inform the actions module.
366 if (Tok.is(tok::l_brace))
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000367 if (getLang().CPlusPlus)
Douglas Gregor279272e2009-02-04 19:02:06 +0000368 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempDecl);
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000369 else
Douglas Gregor279272e2009-02-04 19:02:06 +0000370 ParseStructUnionBody(StartLoc, TagType, TagOrTempDecl);
Douglas Gregorec93f442008-04-13 21:30:24 +0000371 else if (TK == Action::TK_Definition) {
372 // FIXME: Complain that we have a base-specifier list but no
373 // definition.
Chris Lattnerf006a222008-11-18 07:48:38 +0000374 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregorec93f442008-04-13 21:30:24 +0000375 }
376
377 const char *PrevSpec = 0;
Douglas Gregord406b032009-02-06 22:42:48 +0000378 if (!TagOrTempDecl)
379 DS.SetTypeSpecError();
380 else if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagOrTempDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +0000381 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Douglas Gregorec93f442008-04-13 21:30:24 +0000382}
383
384/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
385///
386/// base-clause : [C++ class.derived]
387/// ':' base-specifier-list
388/// base-specifier-list:
389/// base-specifier '...'[opt]
390/// base-specifier-list ',' base-specifier '...'[opt]
391void Parser::ParseBaseClause(DeclTy *ClassDecl)
392{
393 assert(Tok.is(tok::colon) && "Not a base clause");
394 ConsumeToken();
395
Douglas Gregorabed2172008-10-22 17:49:05 +0000396 // Build up an array of parsed base specifiers.
397 llvm::SmallVector<BaseTy *, 8> BaseInfo;
398
Douglas Gregorec93f442008-04-13 21:30:24 +0000399 while (true) {
400 // Parse a base-specifier.
Douglas Gregorabed2172008-10-22 17:49:05 +0000401 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000402 if (Result.isInvalid()) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000403 // Skip the rest of this base specifier, up until the comma or
404 // opening brace.
Douglas Gregorabed2172008-10-22 17:49:05 +0000405 SkipUntil(tok::comma, tok::l_brace, true, true);
406 } else {
407 // Add this to our array of base specifiers.
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000408 BaseInfo.push_back(Result.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000409 }
410
411 // If the next token is a comma, consume it and keep reading
412 // base-specifiers.
413 if (Tok.isNot(tok::comma)) break;
414
415 // Consume the comma.
416 ConsumeToken();
417 }
Douglas Gregorabed2172008-10-22 17:49:05 +0000418
419 // Attach the base specifiers
420 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregorec93f442008-04-13 21:30:24 +0000421}
422
423/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
424/// one entry in the base class list of a class specifier, for example:
425/// class foo : public bar, virtual private baz {
426/// 'public bar' and 'virtual private baz' are each base-specifiers.
427///
428/// base-specifier: [C++ class.derived]
429/// ::[opt] nested-name-specifier[opt] class-name
430/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
431/// class-name
432/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
433/// class-name
Douglas Gregorabed2172008-10-22 17:49:05 +0000434Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
Douglas Gregorec93f442008-04-13 21:30:24 +0000435{
436 bool IsVirtual = false;
437 SourceLocation StartLoc = Tok.getLocation();
438
439 // Parse the 'virtual' keyword.
440 if (Tok.is(tok::kw_virtual)) {
441 ConsumeToken();
442 IsVirtual = true;
443 }
444
445 // Parse an (optional) access specifier.
446 AccessSpecifier Access = getAccessSpecifierIfPresent();
447 if (Access)
448 ConsumeToken();
449
450 // Parse the 'virtual' keyword (again!), in case it came after the
451 // access specifier.
452 if (Tok.is(tok::kw_virtual)) {
453 SourceLocation VirtualLoc = ConsumeToken();
454 if (IsVirtual) {
455 // Complain about duplicate 'virtual'
Chris Lattnerf006a222008-11-18 07:48:38 +0000456 Diag(VirtualLoc, diag::err_dup_virtual)
457 << SourceRange(VirtualLoc, VirtualLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000458 }
459
460 IsVirtual = true;
461 }
462
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000463 // Parse optional '::' and optional nested-name-specifier.
464 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +0000465 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregorec93f442008-04-13 21:30:24 +0000466
Douglas Gregorec93f442008-04-13 21:30:24 +0000467 // The location of the base class itself.
468 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000469
470 // Parse the class-name.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000471 TypeTy *BaseType = ParseClassName(&SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000472 if (!BaseType)
473 return true;
Douglas Gregorec93f442008-04-13 21:30:24 +0000474
475 // Find the complete source range for the base-specifier.
476 SourceRange Range(StartLoc, BaseLoc);
477
Douglas Gregorec93f442008-04-13 21:30:24 +0000478 // Notify semantic analysis that we have parsed a complete
479 // base-specifier.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000480 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
481 BaseType, BaseLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000482}
483
484/// getAccessSpecifierIfPresent - Determine whether the next token is
485/// a C++ access-specifier.
486///
487/// access-specifier: [C++ class.derived]
488/// 'private'
489/// 'protected'
490/// 'public'
Douglas Gregor696be932008-04-14 00:13:42 +0000491AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregorec93f442008-04-13 21:30:24 +0000492{
493 switch (Tok.getKind()) {
494 default: return AS_none;
495 case tok::kw_private: return AS_private;
496 case tok::kw_protected: return AS_protected;
497 case tok::kw_public: return AS_public;
498 }
499}
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000500
501/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
502///
503/// member-declaration:
504/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
505/// function-definition ';'[opt]
506/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
507/// using-declaration [TODO]
508/// [C++0x] static_assert-declaration [TODO]
509/// template-declaration [TODO]
Chris Lattnerf3375de2008-12-18 01:12:00 +0000510/// [GNU] '__extension__' member-declaration
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000511///
512/// member-declarator-list:
513/// member-declarator
514/// member-declarator-list ',' member-declarator
515///
516/// member-declarator:
517/// declarator pure-specifier[opt]
518/// declarator constant-initializer[opt]
519/// identifier[opt] ':' constant-expression
520///
521/// pure-specifier: [TODO]
522/// '= 0'
523///
524/// constant-initializer:
525/// '=' constant-expression
526///
527Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Chris Lattnerf3375de2008-12-18 01:12:00 +0000528 // Handle: member-declaration ::= '__extension__' member-declaration
529 if (Tok.is(tok::kw___extension__)) {
530 // __extension__ silences extension warnings in the subexpression.
531 ExtensionRAIIObject O(Diags); // Use RAII to do this.
532 ConsumeToken();
533 return ParseCXXClassMemberDeclaration(AS);
534 }
535
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000536 SourceLocation DSStart = Tok.getLocation();
537 // decl-specifier-seq:
538 // Parse the common declaration-specifiers piece.
539 DeclSpec DS;
540 ParseDeclarationSpecifiers(DS);
541
542 if (Tok.is(tok::semi)) {
543 ConsumeToken();
544 // C++ 9.2p7: The member-declarator-list can be omitted only after a
545 // class-specifier or an enum-specifier or in a friend declaration.
546 // FIXME: Friend declarations.
547 switch (DS.getTypeSpecType()) {
548 case DeclSpec::TST_struct:
549 case DeclSpec::TST_union:
550 case DeclSpec::TST_class:
551 case DeclSpec::TST_enum:
552 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
553 default:
554 Diag(DSStart, diag::err_no_declarators);
555 return 0;
556 }
557 }
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000558
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000559 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000560
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000561 if (Tok.isNot(tok::colon)) {
562 // Parse the first declarator.
563 ParseDeclarator(DeclaratorInfo);
564 // Error parsing the declarator?
Douglas Gregor6704b312008-11-17 22:58:34 +0000565 if (!DeclaratorInfo.hasName()) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000566 // If so, skip until the semi-colon or a }.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000567 SkipUntil(tok::r_brace, true);
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000568 if (Tok.is(tok::semi))
569 ConsumeToken();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000570 return 0;
571 }
572
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000573 // function-definition:
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000574 if (Tok.is(tok::l_brace)
575 || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000576 if (!DeclaratorInfo.isFunctionDeclarator()) {
577 Diag(Tok, diag::err_func_def_no_params);
578 ConsumeBrace();
579 SkipUntil(tok::r_brace, true);
580 return 0;
581 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000582
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000583 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
584 Diag(Tok, diag::err_function_declared_typedef);
585 // This recovery skips the entire function body. It would be nice
586 // to simply call ParseCXXInlineMethodDef() below, however Sema
587 // assumes the declarator represents a function, not a typedef.
588 ConsumeBrace();
589 SkipUntil(tok::r_brace, true);
590 return 0;
591 }
592
593 return ParseCXXInlineMethodDef(AS, DeclaratorInfo);
594 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000595 }
596
597 // member-declarator-list:
598 // member-declarator
599 // member-declarator-list ',' member-declarator
600
601 DeclTy *LastDeclInGroup = 0;
Sebastian Redl62261042008-12-09 20:22:58 +0000602 OwningExprResult BitfieldSize(Actions);
603 OwningExprResult Init(Actions);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000604
605 while (1) {
606
607 // member-declarator:
608 // declarator pure-specifier[opt]
609 // declarator constant-initializer[opt]
610 // identifier[opt] ':' constant-expression
611
612 if (Tok.is(tok::colon)) {
613 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000614 BitfieldSize = ParseConstantExpression();
615 if (BitfieldSize.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000616 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000617 }
618
619 // pure-specifier:
620 // '= 0'
621 //
622 // constant-initializer:
623 // '=' constant-expression
624
625 if (Tok.is(tok::equal)) {
626 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000627 Init = ParseInitializer();
628 if (Init.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000629 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000630 }
631
632 // If attributes exist after the declarator, parse them.
633 if (Tok.is(tok::kw___attribute))
634 DeclaratorInfo.AddAttributes(ParseAttributes());
635
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000636 // NOTE: If Sema is the Action module and declarator is an instance field,
637 // this call will *not* return the created decl; LastDeclInGroup will be
638 // returned instead.
639 // See Sema::ActOnCXXMemberDeclarator for details.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000640 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
641 DeclaratorInfo,
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000642 BitfieldSize.release(),
643 Init.release(),
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000644 LastDeclInGroup);
645
Douglas Gregor605de8d2008-12-16 21:30:33 +0000646 if (DeclaratorInfo.isFunctionDeclarator() &&
647 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
648 != DeclSpec::SCS_typedef) {
649 // We just declared a member function. If this member function
650 // has any default arguments, we'll need to parse them later.
651 LateParsedMethodDeclaration *LateMethod = 0;
652 DeclaratorChunk::FunctionTypeInfo &FTI
653 = DeclaratorInfo.getTypeObject(0).Fun;
654 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
655 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
656 if (!LateMethod) {
657 // Push this method onto the stack of late-parsed method
658 // declarations.
659 getCurTopClassStack().MethodDecls.push_back(
660 LateParsedMethodDeclaration(LastDeclInGroup));
661 LateMethod = &getCurTopClassStack().MethodDecls.back();
662
663 // Add all of the parameters prior to this one (they don't
664 // have default arguments).
665 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
666 for (unsigned I = 0; I < ParamIdx; ++I)
667 LateMethod->DefaultArgs.push_back(
668 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
669 }
670
671 // Add this parameter to the list of parameters (it or may
672 // not have a default argument).
673 LateMethod->DefaultArgs.push_back(
674 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
675 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
676 }
677 }
678 }
679
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000680 // If we don't have a comma, it is either the end of the list (a ';')
681 // or an error, bail out.
682 if (Tok.isNot(tok::comma))
683 break;
684
685 // Consume the comma.
686 ConsumeToken();
687
688 // Parse the next declarator.
689 DeclaratorInfo.clear();
Sebastian Redl62261042008-12-09 20:22:58 +0000690 BitfieldSize = 0;
691 Init = 0;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000692
693 // Attributes are only allowed on the second declarator.
694 if (Tok.is(tok::kw___attribute))
695 DeclaratorInfo.AddAttributes(ParseAttributes());
696
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000697 if (Tok.isNot(tok::colon))
698 ParseDeclarator(DeclaratorInfo);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000699 }
700
701 if (Tok.is(tok::semi)) {
702 ConsumeToken();
703 // Reverse the chain list.
704 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
705 }
706
707 Diag(Tok, diag::err_expected_semi_decl_list);
708 // Skip to end of block or statement
709 SkipUntil(tok::r_brace, true, true);
710 if (Tok.is(tok::semi))
711 ConsumeToken();
712 return 0;
713}
714
715/// ParseCXXMemberSpecification - Parse the class definition.
716///
717/// member-specification:
718/// member-declaration member-specification[opt]
719/// access-specifier ':' member-specification[opt]
720///
721void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
722 unsigned TagType, DeclTy *TagDecl) {
Sanjiv Guptafa451432008-10-31 09:52:39 +0000723 assert((TagType == DeclSpec::TST_struct ||
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000724 TagType == DeclSpec::TST_union ||
Sanjiv Guptafa451432008-10-31 09:52:39 +0000725 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000726
727 SourceLocation LBraceLoc = ConsumeBrace();
728
Douglas Gregorcab994d2009-01-09 22:42:13 +0000729 if (!CurScope->isClassScope() && // Not about to define a nested class.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000730 CurScope->isInCXXInlineMethodScope()) {
731 // We will define a local class of an inline method.
732 // Push a new LexedMethodsForTopClass for its inline methods.
733 PushTopClassStack();
734 }
735
736 // Enter a scope for the class.
Douglas Gregorcab994d2009-01-09 22:42:13 +0000737 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000738
Douglas Gregord406b032009-02-06 22:42:48 +0000739 if (TagDecl)
740 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
741 else {
742 SkipUntil(tok::r_brace, false, false);
743 return;
744 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000745
746 // C++ 11p3: Members of a class defined with the keyword class are private
747 // by default. Members of a class defined with the keywords struct or union
748 // are public by default.
749 AccessSpecifier CurAS;
750 if (TagType == DeclSpec::TST_class)
751 CurAS = AS_private;
752 else
753 CurAS = AS_public;
754
755 // While we still have something to read, read the member-declarations.
756 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
757 // Each iteration of this loop reads one member-declaration.
758
759 // Check for extraneous top-level semicolon.
760 if (Tok.is(tok::semi)) {
761 Diag(Tok, diag::ext_extra_struct_semi);
762 ConsumeToken();
763 continue;
764 }
765
766 AccessSpecifier AS = getAccessSpecifierIfPresent();
767 if (AS != AS_none) {
768 // Current token is a C++ access specifier.
769 CurAS = AS;
770 ConsumeToken();
771 ExpectAndConsume(tok::colon, diag::err_expected_colon);
772 continue;
773 }
774
775 // Parse all the comma separated declarators.
776 ParseCXXClassMemberDeclaration(CurAS);
777 }
778
779 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
780
781 AttributeList *AttrList = 0;
782 // If attributes exist after class contents, parse them.
783 if (Tok.is(tok::kw___attribute))
784 AttrList = ParseAttributes(); // FIXME: where should I put them?
785
786 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
787 LBraceLoc, RBraceLoc);
788
789 // C++ 9.2p2: Within the class member-specification, the class is regarded as
790 // complete within function bodies, default arguments,
791 // exception-specifications, and constructor ctor-initializers (including
792 // such things in nested classes).
793 //
Douglas Gregor605de8d2008-12-16 21:30:33 +0000794 // FIXME: Only function bodies and constructor ctor-initializers are
795 // parsed correctly, fix the rest.
Douglas Gregorcab994d2009-01-09 22:42:13 +0000796 if (!CurScope->getParent()->isClassScope()) {
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000797 // We are not inside a nested class. This class and its nested classes
Douglas Gregor605de8d2008-12-16 21:30:33 +0000798 // are complete and we can parse the delayed portions of method
799 // declarations and the lexed inline method definitions.
800 ParseLexedMethodDeclarations();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000801 ParseLexedMethodDefs();
802
803 // For a local class of inline method, pop the LexedMethodsForTopClass that
804 // was previously pushed.
805
Sanjiv Guptafa451432008-10-31 09:52:39 +0000806 assert((CurScope->isInCXXInlineMethodScope() ||
807 TopClassStacks.size() == 1) &&
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000808 "MethodLexers not getting popped properly!");
809 if (CurScope->isInCXXInlineMethodScope())
810 PopTopClassStack();
811 }
812
813 // Leave the class scope.
Douglas Gregor95d40792008-12-10 06:34:36 +0000814 ClassScope.Exit();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000815
Douglas Gregordb568cf2009-01-08 20:45:30 +0000816 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000817}
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000818
819/// ParseConstructorInitializer - Parse a C++ constructor initializer,
820/// which explicitly initializes the members or base classes of a
821/// class (C++ [class.base.init]). For example, the three initializers
822/// after the ':' in the Derived constructor below:
823///
824/// @code
825/// class Base { };
826/// class Derived : Base {
827/// int x;
828/// float f;
829/// public:
830/// Derived(float f) : Base(), x(17), f(f) { }
831/// };
832/// @endcode
833///
834/// [C++] ctor-initializer:
835/// ':' mem-initializer-list
836///
837/// [C++] mem-initializer-list:
838/// mem-initializer
839/// mem-initializer , mem-initializer-list
840void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) {
841 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
842
843 SourceLocation ColonLoc = ConsumeToken();
844
845 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
846
847 do {
848 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000849 if (!MemInit.isInvalid())
850 MemInitializers.push_back(MemInit.get());
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000851
852 if (Tok.is(tok::comma))
853 ConsumeToken();
854 else if (Tok.is(tok::l_brace))
855 break;
856 else {
857 // Skip over garbage, until we get to '{'. Don't eat the '{'.
858 SkipUntil(tok::l_brace, true, true);
859 break;
860 }
861 } while (true);
862
863 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
864 &MemInitializers[0], MemInitializers.size());
865}
866
867/// ParseMemInitializer - Parse a C++ member initializer, which is
868/// part of a constructor initializer that explicitly initializes one
869/// member or base class (C++ [class.base.init]). See
870/// ParseConstructorInitializer for an example.
871///
872/// [C++] mem-initializer:
873/// mem-initializer-id '(' expression-list[opt] ')'
874///
875/// [C++] mem-initializer-id:
876/// '::'[opt] nested-name-specifier[opt] class-name
877/// identifier
878Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) {
879 // FIXME: parse '::'[opt] nested-name-specifier[opt]
880
881 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000882 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000883 return true;
884 }
885
886 // Get the identifier. This may be a member name or a class name,
887 // but we'll let the semantic analysis determine which it is.
888 IdentifierInfo *II = Tok.getIdentifierInfo();
889 SourceLocation IdLoc = ConsumeToken();
890
891 // Parse the '('.
892 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000893 Diag(Tok, diag::err_expected_lparen);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000894 return true;
895 }
896 SourceLocation LParenLoc = ConsumeParen();
897
898 // Parse the optional expression-list.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000899 ExprVector ArgExprs(Actions);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000900 CommaLocsTy CommaLocs;
901 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
902 SkipUntil(tok::r_paren);
903 return true;
904 }
905
906 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
907
Sebastian Redl6008ac32008-11-25 22:21:31 +0000908 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
909 LParenLoc, ArgExprs.take(),
910 ArgExprs.size(), &CommaLocs[0], RParenLoc);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000911}
Douglas Gregor90a2c972008-11-25 03:22:00 +0000912
913/// ParseExceptionSpecification - Parse a C++ exception-specification
914/// (C++ [except.spec]).
915///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +0000916/// exception-specification:
917/// 'throw' '(' type-id-list [opt] ')'
918/// [MS] 'throw' '(' '...' ')'
Douglas Gregor90a2c972008-11-25 03:22:00 +0000919///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +0000920/// type-id-list:
921/// type-id
922/// type-id-list ',' type-id
Douglas Gregor90a2c972008-11-25 03:22:00 +0000923///
924bool Parser::ParseExceptionSpecification() {
925 assert(Tok.is(tok::kw_throw) && "expected throw");
926
927 SourceLocation ThrowLoc = ConsumeToken();
928
929 if (!Tok.is(tok::l_paren)) {
930 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
931 }
932 SourceLocation LParenLoc = ConsumeParen();
933
Douglas Gregor9ed9ac82008-12-01 18:00:20 +0000934 // Parse throw(...), a Microsoft extension that means "this function
935 // can throw anything".
936 if (Tok.is(tok::ellipsis)) {
937 SourceLocation EllipsisLoc = ConsumeToken();
938 if (!getLang().Microsoft)
939 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
940 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
941 return false;
942 }
943
Douglas Gregor90a2c972008-11-25 03:22:00 +0000944 // Parse the sequence of type-ids.
945 while (Tok.isNot(tok::r_paren)) {
946 ParseTypeName();
947 if (Tok.is(tok::comma))
948 ConsumeToken();
949 else
950 break;
951 }
952
953 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
954 return false;
955}