blob: 32367630214cddd32289e5e41b337dc381643186 [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 Gregor5ff0ee52008-12-30 03:27:21 +0000134/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
135/// using-directive. Assumes that current token is 'using'.
136Parser::DeclTy *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context)
137{
138 assert(Tok.is(tok::kw_using) && "Not using token");
139
140 // Eat 'using'.
141 SourceLocation UsingLoc = ConsumeToken();
142
143 if (Tok.is(tok::kw_namespace)) {
144 // Next token after 'using' is 'namespace' so it must be using-directive
145 return ParseUsingDirective(Context, UsingLoc);
146 } else {
147 // Otherwise, it must be using-declaration.
148 return ParseUsingDeclaration(Context, UsingLoc); //FIXME: It is just stub.
149 }
150}
151
152/// ParseUsingDirective - Parse C++ using-directive, assumes
153/// that current token is 'namespace' and 'using' was already parsed.
154///
155/// using-directive: [C++ 7.3.p4: namespace.udir]
156/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
157/// namespace-name ;
158/// [GNU] using-directive:
159/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
160/// namespace-name attributes[opt] ;
161///
162Parser::DeclTy *Parser::ParseUsingDirective(unsigned Context,
163 SourceLocation UsingLoc) {
164 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
165
166 // Eat 'namespace'.
167 SourceLocation NamespcLoc = ConsumeToken();
168
169 CXXScopeSpec SS;
170 // Parse (optional) nested-name-specifier.
171 MaybeParseCXXScopeSpecifier(SS);
172
173 AttributeList *AttrList = 0;
174 IdentifierInfo *NamespcName = 0;
175 SourceLocation IdentLoc = SourceLocation();
176
177 // Parse namespace-name.
178 if (!SS.isInvalid() && Tok.is(tok::identifier)) {
179 // Parse identifier.
180 NamespcName = Tok.getIdentifierInfo();
181 IdentLoc = ConsumeToken();
182 // Parse (optional) attributes (most likely GNU strong-using extension)
183 if (Tok.is(tok::kw___attribute)) {
184 AttrList = ParseAttributes();
185 }
186 // Eat ';'.
187 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
188 AttrList? "attributes list" : "namespace name")) {
189 SkipUntil(tok::semi);
190 return 0;
191 }
192 } else {
193 Diag(Tok, diag::err_expected_namespace_name);
194 // If there was invalid namespace name, skip to end of decl, and eat ';'.
195 SkipUntil(tok::semi);
196 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
197 return 0;
198 }
199
200 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
201 IdentLoc ,NamespcName, AttrList);
202}
203
204/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
205/// 'using' was already seen.
206///
207/// using-declaration: [C++ 7.3.p3: namespace.udecl]
208/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
209/// unqualified-id [TODO]
210/// 'using' :: unqualified-id [TODO]
211///
212Parser::DeclTy *Parser::ParseUsingDeclaration(unsigned Context,
213 SourceLocation UsingLoc) {
214 assert(false && "Not implemented");
215 // FIXME: Implement parsing.
216 return 0;
217}
218
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000219/// ParseClassName - Parse a C++ class-name, which names a class. Note
220/// that we only check that the result names a type; semantic analysis
221/// will need to verify that the type names a class. The result is
222/// either a type or NULL, dependending on whether a type name was
223/// found.
224///
225/// class-name: [C++ 9.1]
226/// identifier
227/// template-id [TODO]
228///
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000229Parser::TypeTy *Parser::ParseClassName(const CXXScopeSpec *SS) {
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000230 // Parse the class-name.
231 // FIXME: Alternatively, parse a simple-template-id.
232 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000233 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000234 return 0;
235 }
236
237 // We have an identifier; check whether it is actually a type.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000238 TypeTy *Type = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope, SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000239 if (!Type) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000240 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000241 return 0;
242 }
243
244 // Consume the identifier.
245 ConsumeToken();
246
247 return Type;
248}
249
Douglas Gregorec93f442008-04-13 21:30:24 +0000250/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
251/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
252/// until we reach the start of a definition or see a token that
253/// cannot start a definition.
254///
255/// class-specifier: [C++ class]
256/// class-head '{' member-specification[opt] '}'
257/// class-head '{' member-specification[opt] '}' attributes[opt]
258/// class-head:
259/// class-key identifier[opt] base-clause[opt]
260/// class-key nested-name-specifier identifier base-clause[opt]
261/// class-key nested-name-specifier[opt] simple-template-id
262/// base-clause[opt]
263/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
264/// [GNU] class-key attributes[opt] nested-name-specifier
265/// identifier base-clause[opt]
266/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
267/// simple-template-id base-clause[opt]
268/// class-key:
269/// 'class'
270/// 'struct'
271/// 'union'
272///
273/// elaborated-type-specifier: [C++ dcl.type.elab]
274/// class-key ::[opt] nested-name-specifier[opt] identifier
275/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
276/// simple-template-id
277///
278/// Note that the C++ class-specifier and elaborated-type-specifier,
279/// together, subsume the C99 struct-or-union-specifier:
280///
281/// struct-or-union-specifier: [C99 6.7.2.1]
282/// struct-or-union identifier[opt] '{' struct-contents '}'
283/// struct-or-union identifier
284/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
285/// '}' attributes[opt]
286/// [GNU] struct-or-union attributes[opt] identifier
287/// struct-or-union:
288/// 'struct'
289/// 'union'
Douglas Gregor52473432008-12-24 02:52:09 +0000290void Parser::ParseClassSpecifier(DeclSpec &DS,
291 TemplateParameterLists *TemplateParams) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000292 assert((Tok.is(tok::kw_class) ||
293 Tok.is(tok::kw_struct) ||
294 Tok.is(tok::kw_union)) &&
295 "Not a class specifier");
296 DeclSpec::TST TagType =
297 Tok.is(tok::kw_class) ? DeclSpec::TST_class :
298 Tok.is(tok::kw_struct) ? DeclSpec::TST_struct :
299 DeclSpec::TST_union;
300
301 SourceLocation StartLoc = ConsumeToken();
302
303 AttributeList *Attr = 0;
304 // If attributes exist after tag, parse them.
305 if (Tok.is(tok::kw___attribute))
306 Attr = ParseAttributes();
307
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000308 // If declspecs exist after tag, parse them.
309 if (Tok.is(tok::kw___declspec) && PP.getLangOptions().Microsoft)
310 FuzzyParseMicrosoftDeclSpec();
311
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000312 // Parse the (optional) nested-name-specifier.
313 CXXScopeSpec SS;
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +0000314 if (getLang().CPlusPlus && MaybeParseCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000315 if (Tok.isNot(tok::identifier))
316 Diag(Tok, diag::err_expected_ident);
317 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000318
319 // Parse the (optional) class name.
320 // FIXME: Alternatively, parse a simple-template-id.
321 IdentifierInfo *Name = 0;
322 SourceLocation NameLoc;
323 if (Tok.is(tok::identifier)) {
324 Name = Tok.getIdentifierInfo();
325 NameLoc = ConsumeToken();
326 }
327
328 // There are three options here. If we have 'struct foo;', then
329 // this is a forward declaration. If we have 'struct foo {...' or
330 // 'struct fo :...' then this is a definition. Otherwise we have
331 // something like 'struct foo xyz', a reference.
332 Action::TagKind TK;
333 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
334 TK = Action::TK_Definition;
335 else if (Tok.is(tok::semi))
336 TK = Action::TK_Declaration;
337 else
338 TK = Action::TK_Reference;
339
340 if (!Name && TK != Action::TK_Definition) {
341 // We have a declaration or reference to an anonymous class.
Chris Lattnerf006a222008-11-18 07:48:38 +0000342 Diag(StartLoc, diag::err_anon_type_definition)
343 << DeclSpec::getSpecifierName(TagType);
Douglas Gregorec93f442008-04-13 21:30:24 +0000344
345 // Skip the rest of this declarator, up until the comma or semicolon.
346 SkipUntil(tok::comma, true);
347 return;
348 }
349
350 // Parse the tag portion of this.
Douglas Gregor52473432008-12-24 02:52:09 +0000351 DeclTy *TagDecl
352 = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name,
353 NameLoc, Attr,
354 Action::MultiTemplateParamsArg(
355 Actions,
356 TemplateParams? &(*TemplateParams)[0] : 0,
357 TemplateParams? TemplateParams->size() : 0));
Douglas Gregorec93f442008-04-13 21:30:24 +0000358
359 // Parse the optional base clause (C++ only).
360 if (getLang().CPlusPlus && Tok.is(tok::colon)) {
361 ParseBaseClause(TagDecl);
362 }
363
364 // If there is a body, parse it and inform the actions module.
365 if (Tok.is(tok::l_brace))
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000366 if (getLang().CPlusPlus)
367 ParseCXXMemberSpecification(StartLoc, TagType, TagDecl);
368 else
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000369 ParseStructUnionBody(StartLoc, TagType, TagDecl);
Douglas Gregorec93f442008-04-13 21:30:24 +0000370 else if (TK == Action::TK_Definition) {
371 // FIXME: Complain that we have a base-specifier list but no
372 // definition.
Chris Lattnerf006a222008-11-18 07:48:38 +0000373 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregorec93f442008-04-13 21:30:24 +0000374 }
375
376 const char *PrevSpec = 0;
377 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +0000378 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Douglas Gregorec93f442008-04-13 21:30:24 +0000379}
380
381/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
382///
383/// base-clause : [C++ class.derived]
384/// ':' base-specifier-list
385/// base-specifier-list:
386/// base-specifier '...'[opt]
387/// base-specifier-list ',' base-specifier '...'[opt]
388void Parser::ParseBaseClause(DeclTy *ClassDecl)
389{
390 assert(Tok.is(tok::colon) && "Not a base clause");
391 ConsumeToken();
392
Douglas Gregorabed2172008-10-22 17:49:05 +0000393 // Build up an array of parsed base specifiers.
394 llvm::SmallVector<BaseTy *, 8> BaseInfo;
395
Douglas Gregorec93f442008-04-13 21:30:24 +0000396 while (true) {
397 // Parse a base-specifier.
Douglas Gregorabed2172008-10-22 17:49:05 +0000398 BaseResult Result = ParseBaseSpecifier(ClassDecl);
399 if (Result.isInvalid) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000400 // Skip the rest of this base specifier, up until the comma or
401 // opening brace.
Douglas Gregorabed2172008-10-22 17:49:05 +0000402 SkipUntil(tok::comma, tok::l_brace, true, true);
403 } else {
404 // Add this to our array of base specifiers.
405 BaseInfo.push_back(Result.Val);
Douglas Gregorec93f442008-04-13 21:30:24 +0000406 }
407
408 // If the next token is a comma, consume it and keep reading
409 // base-specifiers.
410 if (Tok.isNot(tok::comma)) break;
411
412 // Consume the comma.
413 ConsumeToken();
414 }
Douglas Gregorabed2172008-10-22 17:49:05 +0000415
416 // Attach the base specifiers
417 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregorec93f442008-04-13 21:30:24 +0000418}
419
420/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
421/// one entry in the base class list of a class specifier, for example:
422/// class foo : public bar, virtual private baz {
423/// 'public bar' and 'virtual private baz' are each base-specifiers.
424///
425/// base-specifier: [C++ class.derived]
426/// ::[opt] nested-name-specifier[opt] class-name
427/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
428/// class-name
429/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
430/// class-name
Douglas Gregorabed2172008-10-22 17:49:05 +0000431Parser::BaseResult Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
Douglas Gregorec93f442008-04-13 21:30:24 +0000432{
433 bool IsVirtual = false;
434 SourceLocation StartLoc = Tok.getLocation();
435
436 // Parse the 'virtual' keyword.
437 if (Tok.is(tok::kw_virtual)) {
438 ConsumeToken();
439 IsVirtual = true;
440 }
441
442 // Parse an (optional) access specifier.
443 AccessSpecifier Access = getAccessSpecifierIfPresent();
444 if (Access)
445 ConsumeToken();
446
447 // Parse the 'virtual' keyword (again!), in case it came after the
448 // access specifier.
449 if (Tok.is(tok::kw_virtual)) {
450 SourceLocation VirtualLoc = ConsumeToken();
451 if (IsVirtual) {
452 // Complain about duplicate 'virtual'
Chris Lattnerf006a222008-11-18 07:48:38 +0000453 Diag(VirtualLoc, diag::err_dup_virtual)
454 << SourceRange(VirtualLoc, VirtualLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000455 }
456
457 IsVirtual = true;
458 }
459
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000460 // Parse optional '::' and optional nested-name-specifier.
461 CXXScopeSpec SS;
Argiris Kirtzidis91c80dc2008-11-26 21:41:52 +0000462 MaybeParseCXXScopeSpecifier(SS);
Douglas Gregorec93f442008-04-13 21:30:24 +0000463
Douglas Gregorec93f442008-04-13 21:30:24 +0000464 // The location of the base class itself.
465 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000466
467 // Parse the class-name.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000468 TypeTy *BaseType = ParseClassName(&SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000469 if (!BaseType)
470 return true;
Douglas Gregorec93f442008-04-13 21:30:24 +0000471
472 // Find the complete source range for the base-specifier.
473 SourceRange Range(StartLoc, BaseLoc);
474
Douglas Gregorec93f442008-04-13 21:30:24 +0000475 // Notify semantic analysis that we have parsed a complete
476 // base-specifier.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000477 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
478 BaseType, BaseLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000479}
480
481/// getAccessSpecifierIfPresent - Determine whether the next token is
482/// a C++ access-specifier.
483///
484/// access-specifier: [C++ class.derived]
485/// 'private'
486/// 'protected'
487/// 'public'
Douglas Gregor696be932008-04-14 00:13:42 +0000488AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregorec93f442008-04-13 21:30:24 +0000489{
490 switch (Tok.getKind()) {
491 default: return AS_none;
492 case tok::kw_private: return AS_private;
493 case tok::kw_protected: return AS_protected;
494 case tok::kw_public: return AS_public;
495 }
496}
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000497
498/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
499///
500/// member-declaration:
501/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
502/// function-definition ';'[opt]
503/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
504/// using-declaration [TODO]
505/// [C++0x] static_assert-declaration [TODO]
506/// template-declaration [TODO]
Chris Lattnerf3375de2008-12-18 01:12:00 +0000507/// [GNU] '__extension__' member-declaration
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000508///
509/// member-declarator-list:
510/// member-declarator
511/// member-declarator-list ',' member-declarator
512///
513/// member-declarator:
514/// declarator pure-specifier[opt]
515/// declarator constant-initializer[opt]
516/// identifier[opt] ':' constant-expression
517///
518/// pure-specifier: [TODO]
519/// '= 0'
520///
521/// constant-initializer:
522/// '=' constant-expression
523///
524Parser::DeclTy *Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Chris Lattnerf3375de2008-12-18 01:12:00 +0000525 // Handle: member-declaration ::= '__extension__' member-declaration
526 if (Tok.is(tok::kw___extension__)) {
527 // __extension__ silences extension warnings in the subexpression.
528 ExtensionRAIIObject O(Diags); // Use RAII to do this.
529 ConsumeToken();
530 return ParseCXXClassMemberDeclaration(AS);
531 }
532
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000533 SourceLocation DSStart = Tok.getLocation();
534 // decl-specifier-seq:
535 // Parse the common declaration-specifiers piece.
536 DeclSpec DS;
537 ParseDeclarationSpecifiers(DS);
538
539 if (Tok.is(tok::semi)) {
540 ConsumeToken();
541 // C++ 9.2p7: The member-declarator-list can be omitted only after a
542 // class-specifier or an enum-specifier or in a friend declaration.
543 // FIXME: Friend declarations.
544 switch (DS.getTypeSpecType()) {
545 case DeclSpec::TST_struct:
546 case DeclSpec::TST_union:
547 case DeclSpec::TST_class:
548 case DeclSpec::TST_enum:
549 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
550 default:
551 Diag(DSStart, diag::err_no_declarators);
552 return 0;
553 }
554 }
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000555
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000556 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000557
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000558 if (Tok.isNot(tok::colon)) {
559 // Parse the first declarator.
560 ParseDeclarator(DeclaratorInfo);
561 // Error parsing the declarator?
Douglas Gregor6704b312008-11-17 22:58:34 +0000562 if (!DeclaratorInfo.hasName()) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000563 // If so, skip until the semi-colon or a }.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000564 SkipUntil(tok::r_brace, true);
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000565 if (Tok.is(tok::semi))
566 ConsumeToken();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000567 return 0;
568 }
569
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000570 // function-definition:
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000571 if (Tok.is(tok::l_brace)
572 || (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::colon))) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000573 if (!DeclaratorInfo.isFunctionDeclarator()) {
574 Diag(Tok, diag::err_func_def_no_params);
575 ConsumeBrace();
576 SkipUntil(tok::r_brace, true);
577 return 0;
578 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000579
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000580 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
581 Diag(Tok, diag::err_function_declared_typedef);
582 // This recovery skips the entire function body. It would be nice
583 // to simply call ParseCXXInlineMethodDef() below, however Sema
584 // assumes the declarator represents a function, not a typedef.
585 ConsumeBrace();
586 SkipUntil(tok::r_brace, true);
587 return 0;
588 }
589
590 return ParseCXXInlineMethodDef(AS, DeclaratorInfo);
591 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000592 }
593
594 // member-declarator-list:
595 // member-declarator
596 // member-declarator-list ',' member-declarator
597
598 DeclTy *LastDeclInGroup = 0;
Sebastian Redl62261042008-12-09 20:22:58 +0000599 OwningExprResult BitfieldSize(Actions);
600 OwningExprResult Init(Actions);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000601
602 while (1) {
603
604 // member-declarator:
605 // declarator pure-specifier[opt]
606 // declarator constant-initializer[opt]
607 // identifier[opt] ':' constant-expression
608
609 if (Tok.is(tok::colon)) {
610 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000611 BitfieldSize = ParseConstantExpression();
612 if (BitfieldSize.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000613 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000614 }
615
616 // pure-specifier:
617 // '= 0'
618 //
619 // constant-initializer:
620 // '=' constant-expression
621
622 if (Tok.is(tok::equal)) {
623 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000624 Init = ParseInitializer();
625 if (Init.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000626 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000627 }
628
629 // If attributes exist after the declarator, parse them.
630 if (Tok.is(tok::kw___attribute))
631 DeclaratorInfo.AddAttributes(ParseAttributes());
632
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000633 // NOTE: If Sema is the Action module and declarator is an instance field,
634 // this call will *not* return the created decl; LastDeclInGroup will be
635 // returned instead.
636 // See Sema::ActOnCXXMemberDeclarator for details.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000637 LastDeclInGroup = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
638 DeclaratorInfo,
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000639 BitfieldSize.release(),
640 Init.release(),
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000641 LastDeclInGroup);
642
Douglas Gregor605de8d2008-12-16 21:30:33 +0000643 if (DeclaratorInfo.isFunctionDeclarator() &&
644 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
645 != DeclSpec::SCS_typedef) {
646 // We just declared a member function. If this member function
647 // has any default arguments, we'll need to parse them later.
648 LateParsedMethodDeclaration *LateMethod = 0;
649 DeclaratorChunk::FunctionTypeInfo &FTI
650 = DeclaratorInfo.getTypeObject(0).Fun;
651 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
652 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
653 if (!LateMethod) {
654 // Push this method onto the stack of late-parsed method
655 // declarations.
656 getCurTopClassStack().MethodDecls.push_back(
657 LateParsedMethodDeclaration(LastDeclInGroup));
658 LateMethod = &getCurTopClassStack().MethodDecls.back();
659
660 // Add all of the parameters prior to this one (they don't
661 // have default arguments).
662 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
663 for (unsigned I = 0; I < ParamIdx; ++I)
664 LateMethod->DefaultArgs.push_back(
665 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
666 }
667
668 // Add this parameter to the list of parameters (it or may
669 // not have a default argument).
670 LateMethod->DefaultArgs.push_back(
671 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
672 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
673 }
674 }
675 }
676
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000677 // If we don't have a comma, it is either the end of the list (a ';')
678 // or an error, bail out.
679 if (Tok.isNot(tok::comma))
680 break;
681
682 // Consume the comma.
683 ConsumeToken();
684
685 // Parse the next declarator.
686 DeclaratorInfo.clear();
Sebastian Redl62261042008-12-09 20:22:58 +0000687 BitfieldSize = 0;
688 Init = 0;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000689
690 // Attributes are only allowed on the second declarator.
691 if (Tok.is(tok::kw___attribute))
692 DeclaratorInfo.AddAttributes(ParseAttributes());
693
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000694 if (Tok.isNot(tok::colon))
695 ParseDeclarator(DeclaratorInfo);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000696 }
697
698 if (Tok.is(tok::semi)) {
699 ConsumeToken();
700 // Reverse the chain list.
701 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
702 }
703
704 Diag(Tok, diag::err_expected_semi_decl_list);
705 // Skip to end of block or statement
706 SkipUntil(tok::r_brace, true, true);
707 if (Tok.is(tok::semi))
708 ConsumeToken();
709 return 0;
710}
711
712/// ParseCXXMemberSpecification - Parse the class definition.
713///
714/// member-specification:
715/// member-declaration member-specification[opt]
716/// access-specifier ':' member-specification[opt]
717///
718void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
719 unsigned TagType, DeclTy *TagDecl) {
Sanjiv Guptafa451432008-10-31 09:52:39 +0000720 assert((TagType == DeclSpec::TST_struct ||
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000721 TagType == DeclSpec::TST_union ||
Sanjiv Guptafa451432008-10-31 09:52:39 +0000722 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000723
724 SourceLocation LBraceLoc = ConsumeBrace();
725
726 if (!CurScope->isCXXClassScope() && // Not about to define a nested class.
727 CurScope->isInCXXInlineMethodScope()) {
728 // We will define a local class of an inline method.
729 // Push a new LexedMethodsForTopClass for its inline methods.
730 PushTopClassStack();
731 }
732
733 // Enter a scope for the class.
Douglas Gregor95d40792008-12-10 06:34:36 +0000734 ParseScope ClassScope(this, Scope::CXXClassScope|Scope::DeclScope);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000735
736 Actions.ActOnStartCXXClassDef(CurScope, TagDecl, LBraceLoc);
737
738 // C++ 11p3: Members of a class defined with the keyword class are private
739 // by default. Members of a class defined with the keywords struct or union
740 // are public by default.
741 AccessSpecifier CurAS;
742 if (TagType == DeclSpec::TST_class)
743 CurAS = AS_private;
744 else
745 CurAS = AS_public;
746
747 // While we still have something to read, read the member-declarations.
748 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
749 // Each iteration of this loop reads one member-declaration.
750
751 // Check for extraneous top-level semicolon.
752 if (Tok.is(tok::semi)) {
753 Diag(Tok, diag::ext_extra_struct_semi);
754 ConsumeToken();
755 continue;
756 }
757
758 AccessSpecifier AS = getAccessSpecifierIfPresent();
759 if (AS != AS_none) {
760 // Current token is a C++ access specifier.
761 CurAS = AS;
762 ConsumeToken();
763 ExpectAndConsume(tok::colon, diag::err_expected_colon);
764 continue;
765 }
766
767 // Parse all the comma separated declarators.
768 ParseCXXClassMemberDeclaration(CurAS);
769 }
770
771 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
772
773 AttributeList *AttrList = 0;
774 // If attributes exist after class contents, parse them.
775 if (Tok.is(tok::kw___attribute))
776 AttrList = ParseAttributes(); // FIXME: where should I put them?
777
778 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
779 LBraceLoc, RBraceLoc);
780
781 // C++ 9.2p2: Within the class member-specification, the class is regarded as
782 // complete within function bodies, default arguments,
783 // exception-specifications, and constructor ctor-initializers (including
784 // such things in nested classes).
785 //
Douglas Gregor605de8d2008-12-16 21:30:33 +0000786 // FIXME: Only function bodies and constructor ctor-initializers are
787 // parsed correctly, fix the rest.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000788 if (!CurScope->getParent()->isCXXClassScope()) {
789 // We are not inside a nested class. This class and its nested classes
Douglas Gregor605de8d2008-12-16 21:30:33 +0000790 // are complete and we can parse the delayed portions of method
791 // declarations and the lexed inline method definitions.
792 ParseLexedMethodDeclarations();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000793 ParseLexedMethodDefs();
794
795 // For a local class of inline method, pop the LexedMethodsForTopClass that
796 // was previously pushed.
797
Sanjiv Guptafa451432008-10-31 09:52:39 +0000798 assert((CurScope->isInCXXInlineMethodScope() ||
799 TopClassStacks.size() == 1) &&
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000800 "MethodLexers not getting popped properly!");
801 if (CurScope->isInCXXInlineMethodScope())
802 PopTopClassStack();
803 }
804
805 // Leave the class scope.
Douglas Gregor95d40792008-12-10 06:34:36 +0000806 ClassScope.Exit();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000807
Argiris Kirtzidis448b4e42008-08-09 00:39:29 +0000808 Actions.ActOnFinishCXXClassDef(TagDecl);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000809}
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000810
811/// ParseConstructorInitializer - Parse a C++ constructor initializer,
812/// which explicitly initializes the members or base classes of a
813/// class (C++ [class.base.init]). For example, the three initializers
814/// after the ':' in the Derived constructor below:
815///
816/// @code
817/// class Base { };
818/// class Derived : Base {
819/// int x;
820/// float f;
821/// public:
822/// Derived(float f) : Base(), x(17), f(f) { }
823/// };
824/// @endcode
825///
826/// [C++] ctor-initializer:
827/// ':' mem-initializer-list
828///
829/// [C++] mem-initializer-list:
830/// mem-initializer
831/// mem-initializer , mem-initializer-list
832void Parser::ParseConstructorInitializer(DeclTy *ConstructorDecl) {
833 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
834
835 SourceLocation ColonLoc = ConsumeToken();
836
837 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
838
839 do {
840 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
841 if (!MemInit.isInvalid)
842 MemInitializers.push_back(MemInit.Val);
843
844 if (Tok.is(tok::comma))
845 ConsumeToken();
846 else if (Tok.is(tok::l_brace))
847 break;
848 else {
849 // Skip over garbage, until we get to '{'. Don't eat the '{'.
850 SkipUntil(tok::l_brace, true, true);
851 break;
852 }
853 } while (true);
854
855 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
856 &MemInitializers[0], MemInitializers.size());
857}
858
859/// ParseMemInitializer - Parse a C++ member initializer, which is
860/// part of a constructor initializer that explicitly initializes one
861/// member or base class (C++ [class.base.init]). See
862/// ParseConstructorInitializer for an example.
863///
864/// [C++] mem-initializer:
865/// mem-initializer-id '(' expression-list[opt] ')'
866///
867/// [C++] mem-initializer-id:
868/// '::'[opt] nested-name-specifier[opt] class-name
869/// identifier
870Parser::MemInitResult Parser::ParseMemInitializer(DeclTy *ConstructorDecl) {
871 // FIXME: parse '::'[opt] nested-name-specifier[opt]
872
873 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000874 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000875 return true;
876 }
877
878 // Get the identifier. This may be a member name or a class name,
879 // but we'll let the semantic analysis determine which it is.
880 IdentifierInfo *II = Tok.getIdentifierInfo();
881 SourceLocation IdLoc = ConsumeToken();
882
883 // Parse the '('.
884 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000885 Diag(Tok, diag::err_expected_lparen);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000886 return true;
887 }
888 SourceLocation LParenLoc = ConsumeParen();
889
890 // Parse the optional expression-list.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000891 ExprVector ArgExprs(Actions);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000892 CommaLocsTy CommaLocs;
893 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
894 SkipUntil(tok::r_paren);
895 return true;
896 }
897
898 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
899
Sebastian Redl6008ac32008-11-25 22:21:31 +0000900 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
901 LParenLoc, ArgExprs.take(),
902 ArgExprs.size(), &CommaLocs[0], RParenLoc);
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000903}
Douglas Gregor90a2c972008-11-25 03:22:00 +0000904
905/// ParseExceptionSpecification - Parse a C++ exception-specification
906/// (C++ [except.spec]).
907///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +0000908/// exception-specification:
909/// 'throw' '(' type-id-list [opt] ')'
910/// [MS] 'throw' '(' '...' ')'
Douglas Gregor90a2c972008-11-25 03:22:00 +0000911///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +0000912/// type-id-list:
913/// type-id
914/// type-id-list ',' type-id
Douglas Gregor90a2c972008-11-25 03:22:00 +0000915///
916bool Parser::ParseExceptionSpecification() {
917 assert(Tok.is(tok::kw_throw) && "expected throw");
918
919 SourceLocation ThrowLoc = ConsumeToken();
920
921 if (!Tok.is(tok::l_paren)) {
922 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
923 }
924 SourceLocation LParenLoc = ConsumeParen();
925
Douglas Gregor9ed9ac82008-12-01 18:00:20 +0000926 // Parse throw(...), a Microsoft extension that means "this function
927 // can throw anything".
928 if (Tok.is(tok::ellipsis)) {
929 SourceLocation EllipsisLoc = ConsumeToken();
930 if (!getLang().Microsoft)
931 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
932 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
933 return false;
934 }
935
Douglas Gregor90a2c972008-11-25 03:22:00 +0000936 // Parse the sequence of type-ids.
937 while (Tok.isNot(tok::r_paren)) {
938 ParseTypeName();
939 if (Tok.is(tok::comma))
940 ConsumeToken();
941 else
942 break;
943 }
944
945 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
946 return false;
947}