blob: da99ab9aa2df1498d1ff6bb9af9aaa3956277e65 [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///
Chris Lattner9802a0a2009-04-02 04:16:50 +000045Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
46 SourceLocation &DeclEnd) {
Chris Lattner34a01ad2007-10-09 17:33:22 +000047 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnerf7b2e552007-08-25 06:57:03 +000048 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
49
50 SourceLocation IdentLoc;
51 IdentifierInfo *Ident = 0;
52
Chris Lattner34a01ad2007-10-09 17:33:22 +000053 if (Tok.is(tok::identifier)) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +000054 Ident = Tok.getIdentifierInfo();
55 IdentLoc = ConsumeToken(); // eat the identifier.
56 }
57
58 // Read label attributes, if present.
Chris Lattner5261d0c2009-03-28 19:18:32 +000059 Action::AttrTy *AttrList = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +000060 if (Tok.is(tok::kw___attribute))
Chris Lattnerf7b2e552007-08-25 06:57:03 +000061 // FIXME: save these somewhere.
62 AttrList = ParseAttributes();
63
Anders Carlssonf94cca22009-03-28 04:07:16 +000064 if (Tok.is(tok::equal))
Chris Lattnerf7b2e552007-08-25 06:57:03 +000065 // FIXME: Verify no attributes were present.
Chris Lattner9802a0a2009-04-02 04:16:50 +000066 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Anders Carlssonf94cca22009-03-28 04:07:16 +000067
Chris Lattner872b0442009-03-29 14:02:43 +000068 if (Tok.isNot(tok::l_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +000069 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner872b0442009-03-29 14:02:43 +000070 diag::err_expected_ident_lbrace);
71 return DeclPtrTy();
Chris Lattnerf7b2e552007-08-25 06:57:03 +000072 }
73
Chris Lattner872b0442009-03-29 14:02:43 +000074 SourceLocation LBrace = ConsumeBrace();
75
76 // Enter a scope for the namespace.
77 ParseScope NamespaceScope(this, Scope::DeclScope);
78
79 DeclPtrTy NamespcDecl =
80 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
81
82 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
83 PP.getSourceManager(),
84 "parsing namespace");
85
86 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
87 ParseExternalDeclaration();
88
89 // Leave the namespace scope.
90 NamespaceScope.Exit();
91
Chris Lattner9802a0a2009-04-02 04:16:50 +000092 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
93 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner872b0442009-03-29 14:02:43 +000094
Chris Lattner9802a0a2009-04-02 04:16:50 +000095 DeclEnd = RBraceLoc;
Chris Lattner872b0442009-03-29 14:02:43 +000096 return NamespcDecl;
Chris Lattnerf7b2e552007-08-25 06:57:03 +000097}
Chris Lattner806a5f52008-01-12 07:05:38 +000098
Anders Carlssonf94cca22009-03-28 04:07:16 +000099/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
100/// alias definition.
101///
Anders Carlsson26de7882009-03-28 22:53:22 +0000102Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
103 SourceLocation AliasLoc,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000104 IdentifierInfo *Alias,
105 SourceLocation &DeclEnd) {
Anders Carlssonf94cca22009-03-28 04:07:16 +0000106 assert(Tok.is(tok::equal) && "Not equal token");
107
108 ConsumeToken(); // eat the '='.
109
110 CXXScopeSpec SS;
111 // Parse (optional) nested-name-specifier.
112 ParseOptionalCXXScopeSpecifier(SS);
113
114 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
115 Diag(Tok, diag::err_expected_namespace_name);
116 // Skip to end of the definition and eat the ';'.
117 SkipUntil(tok::semi);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000118 return DeclPtrTy();
Anders Carlssonf94cca22009-03-28 04:07:16 +0000119 }
120
121 // Parse identifier.
Anders Carlsson26de7882009-03-28 22:53:22 +0000122 IdentifierInfo *Ident = Tok.getIdentifierInfo();
123 SourceLocation IdentLoc = ConsumeToken();
Anders Carlssonf94cca22009-03-28 04:07:16 +0000124
125 // Eat the ';'.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000126 DeclEnd = Tok.getLocation();
Anders Carlssonf94cca22009-03-28 04:07:16 +0000127 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
128 "namespace name", tok::semi);
129
Anders Carlsson26de7882009-03-28 22:53:22 +0000130 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
131 SS, IdentLoc, Ident);
Anders Carlssonf94cca22009-03-28 04:07:16 +0000132}
133
Chris Lattner806a5f52008-01-12 07:05:38 +0000134/// ParseLinkage - We know that the current token is a string_literal
135/// and just before that, that extern was seen.
136///
137/// linkage-specification: [C++ 7.5p2: dcl.link]
138/// 'extern' string-literal '{' declaration-seq[opt] '}'
139/// 'extern' string-literal declaration
140///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000141Parser::DeclPtrTy Parser::ParseLinkage(unsigned Context) {
Douglas Gregor61818c52008-11-21 16:10:08 +0000142 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Chris Lattner806a5f52008-01-12 07:05:38 +0000143 llvm::SmallVector<char, 8> LangBuffer;
144 // LangBuffer is guaranteed to be big enough.
145 LangBuffer.resize(Tok.getLength());
146 const char *LangBufPtr = &LangBuffer[0];
147 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
148
149 SourceLocation Loc = ConsumeStringToken();
Chris Lattner806a5f52008-01-12 07:05:38 +0000150
Douglas Gregord8028382009-01-05 19:45:36 +0000151 ParseScope LinkageScope(this, Scope::DeclScope);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000152 DeclPtrTy LinkageSpec
Douglas Gregord8028382009-01-05 19:45:36 +0000153 = Actions.ActOnStartLinkageSpecification(CurScope,
154 /*FIXME: */SourceLocation(),
155 Loc, LangBufPtr, StrSize,
156 Tok.is(tok::l_brace)? Tok.getLocation()
157 : SourceLocation());
158
159 if (Tok.isNot(tok::l_brace)) {
160 ParseDeclarationOrFunctionDefinition();
161 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
162 SourceLocation());
Douglas Gregorad17e372008-12-16 22:23:02 +0000163 }
164
165 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorad17e372008-12-16 22:23:02 +0000166 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Douglas Gregord8028382009-01-05 19:45:36 +0000167 ParseExternalDeclaration();
Chris Lattner806a5f52008-01-12 07:05:38 +0000168 }
169
Douglas Gregorad17e372008-12-16 22:23:02 +0000170 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregord8028382009-01-05 19:45:36 +0000171 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattner806a5f52008-01-12 07:05:38 +0000172}
Douglas Gregorec93f442008-04-13 21:30:24 +0000173
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000174/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
175/// using-directive. Assumes that current token is 'using'.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000176Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
177 SourceLocation &DeclEnd) {
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000178 assert(Tok.is(tok::kw_using) && "Not using token");
179
180 // Eat 'using'.
181 SourceLocation UsingLoc = ConsumeToken();
182
Chris Lattner08ab4162009-01-06 06:55:51 +0000183 if (Tok.is(tok::kw_namespace))
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000184 // Next token after 'using' is 'namespace' so it must be using-directive
Chris Lattner9802a0a2009-04-02 04:16:50 +0000185 return ParseUsingDirective(Context, UsingLoc, DeclEnd);
Chris Lattner08ab4162009-01-06 06:55:51 +0000186
187 // Otherwise, it must be using-declaration.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000188 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000189}
190
191/// ParseUsingDirective - Parse C++ using-directive, assumes
192/// that current token is 'namespace' and 'using' was already parsed.
193///
194/// using-directive: [C++ 7.3.p4: namespace.udir]
195/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
196/// namespace-name ;
197/// [GNU] using-directive:
198/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
199/// namespace-name attributes[opt] ;
200///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000201Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000202 SourceLocation UsingLoc,
203 SourceLocation &DeclEnd) {
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000204 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
205
206 // Eat 'namespace'.
207 SourceLocation NamespcLoc = ConsumeToken();
208
209 CXXScopeSpec SS;
210 // Parse (optional) nested-name-specifier.
Chris Lattnerd706dc82009-01-06 06:59:53 +0000211 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000212
213 AttributeList *AttrList = 0;
214 IdentifierInfo *NamespcName = 0;
215 SourceLocation IdentLoc = SourceLocation();
216
217 // Parse namespace-name.
Chris Lattner7898bf62009-01-06 07:27:21 +0000218 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000219 Diag(Tok, diag::err_expected_namespace_name);
220 // If there was invalid namespace name, skip to end of decl, and eat ';'.
221 SkipUntil(tok::semi);
222 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattner5261d0c2009-03-28 19:18:32 +0000223 return DeclPtrTy();
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000224 }
Chris Lattner7898bf62009-01-06 07:27:21 +0000225
226 // Parse identifier.
227 NamespcName = Tok.getIdentifierInfo();
228 IdentLoc = ConsumeToken();
229
230 // Parse (optional) attributes (most likely GNU strong-using extension).
231 if (Tok.is(tok::kw___attribute))
232 AttrList = ParseAttributes();
233
234 // Eat ';'.
Chris Lattner9802a0a2009-04-02 04:16:50 +0000235 DeclEnd = Tok.getLocation();
Chris Lattner7898bf62009-01-06 07:27:21 +0000236 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
237 AttrList ? "attributes list" : "namespace name", tok::semi);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000238
239 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Chris Lattner7898bf62009-01-06 07:27:21 +0000240 IdentLoc, NamespcName, AttrList);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000241}
242
243/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
244/// 'using' was already seen.
245///
246/// using-declaration: [C++ 7.3.p3: namespace.udecl]
247/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
248/// unqualified-id [TODO]
249/// 'using' :: unqualified-id [TODO]
250///
Chris Lattner5261d0c2009-03-28 19:18:32 +0000251Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner9802a0a2009-04-02 04:16:50 +0000252 SourceLocation UsingLoc,
253 SourceLocation &DeclEnd) {
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000254 assert(false && "Not implemented");
255 // FIXME: Implement parsing.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000256 return DeclPtrTy();
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000257}
258
Anders Carlssonab041982009-03-11 16:27:10 +0000259/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
260///
261/// static_assert-declaration:
262/// static_assert ( constant-expression , string-literal ) ;
263///
Chris Lattner9802a0a2009-04-02 04:16:50 +0000264Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlssonab041982009-03-11 16:27:10 +0000265 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
266 SourceLocation StaticAssertLoc = ConsumeToken();
267
268 if (Tok.isNot(tok::l_paren)) {
269 Diag(Tok, diag::err_expected_lparen);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000270 return DeclPtrTy();
Anders Carlssonab041982009-03-11 16:27:10 +0000271 }
272
273 SourceLocation LParenLoc = ConsumeParen();
274
275 OwningExprResult AssertExpr(ParseConstantExpression());
276 if (AssertExpr.isInvalid()) {
277 SkipUntil(tok::semi);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000278 return DeclPtrTy();
Anders Carlssonab041982009-03-11 16:27:10 +0000279 }
280
Anders Carlssona24e8d52009-03-13 23:29:20 +0000281 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000282 return DeclPtrTy();
Anders Carlssona24e8d52009-03-13 23:29:20 +0000283
Anders Carlssonab041982009-03-11 16:27:10 +0000284 if (Tok.isNot(tok::string_literal)) {
285 Diag(Tok, diag::err_expected_string_literal);
286 SkipUntil(tok::semi);
Chris Lattner5261d0c2009-03-28 19:18:32 +0000287 return DeclPtrTy();
Anders Carlssonab041982009-03-11 16:27:10 +0000288 }
289
290 OwningExprResult AssertMessage(ParseStringLiteralExpression());
291 if (AssertMessage.isInvalid())
Chris Lattner5261d0c2009-03-28 19:18:32 +0000292 return DeclPtrTy();
Anders Carlssonab041982009-03-11 16:27:10 +0000293
Anders Carlssonc45057a2009-03-15 18:44:04 +0000294 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonab041982009-03-11 16:27:10 +0000295
Chris Lattner9802a0a2009-04-02 04:16:50 +0000296 DeclEnd = Tok.getLocation();
Anders Carlssonab041982009-03-11 16:27:10 +0000297 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
298
Anders Carlssona24e8d52009-03-13 23:29:20 +0000299 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlssonc45057a2009-03-15 18:44:04 +0000300 move(AssertMessage));
Anders Carlssonab041982009-03-11 16:27:10 +0000301}
302
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000303/// ParseClassName - Parse a C++ class-name, which names a class. Note
304/// that we only check that the result names a type; semantic analysis
305/// will need to verify that the type names a class. The result is
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000306/// either a type or NULL, depending on whether a type name was
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000307/// found.
308///
309/// class-name: [C++ 9.1]
310/// identifier
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000311/// simple-template-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000312///
Douglas Gregord7cb0372009-04-01 21:51:26 +0000313Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
314 const CXXScopeSpec *SS) {
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000315 // Check whether we have a template-id that names a type.
316 if (Tok.is(tok::annot_template_id)) {
317 TemplateIdAnnotation *TemplateId
318 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregoraabb8502009-03-31 00:43:58 +0000319 if (TemplateId->Kind == TNK_Type_template) {
Douglas Gregord7cb0372009-04-01 21:51:26 +0000320 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000321
322 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
323 TypeTy *Type = Tok.getAnnotationValue();
324 EndLocation = Tok.getAnnotationEndLoc();
325 ConsumeToken();
Douglas Gregord7cb0372009-04-01 21:51:26 +0000326
327 if (Type)
328 return Type;
329 return true;
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000330 }
331
332 // Fall through to produce an error below.
333 }
334
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000335 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000336 Diag(Tok, diag::err_expected_class_name);
Douglas Gregord7cb0372009-04-01 21:51:26 +0000337 return true;
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000338 }
339
340 // We have an identifier; check whether it is actually a type.
Douglas Gregor1075a162009-02-04 17:00:24 +0000341 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(),
342 Tok.getLocation(), CurScope, SS);
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000343 if (!Type) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000344 Diag(Tok, diag::err_expected_class_name);
Douglas Gregord7cb0372009-04-01 21:51:26 +0000345 return true;
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000346 }
347
348 // Consume the identifier.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000349 EndLocation = ConsumeToken();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000350 return Type;
351}
352
Douglas Gregorec93f442008-04-13 21:30:24 +0000353/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
354/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
355/// until we reach the start of a definition or see a token that
356/// cannot start a definition.
357///
358/// class-specifier: [C++ class]
359/// class-head '{' member-specification[opt] '}'
360/// class-head '{' member-specification[opt] '}' attributes[opt]
361/// class-head:
362/// class-key identifier[opt] base-clause[opt]
363/// class-key nested-name-specifier identifier base-clause[opt]
364/// class-key nested-name-specifier[opt] simple-template-id
365/// base-clause[opt]
366/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
367/// [GNU] class-key attributes[opt] nested-name-specifier
368/// identifier base-clause[opt]
369/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
370/// simple-template-id base-clause[opt]
371/// class-key:
372/// 'class'
373/// 'struct'
374/// 'union'
375///
376/// elaborated-type-specifier: [C++ dcl.type.elab]
377/// class-key ::[opt] nested-name-specifier[opt] identifier
378/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
379/// simple-template-id
380///
381/// Note that the C++ class-specifier and elaborated-type-specifier,
382/// together, subsume the C99 struct-or-union-specifier:
383///
384/// struct-or-union-specifier: [C99 6.7.2.1]
385/// struct-or-union identifier[opt] '{' struct-contents '}'
386/// struct-or-union identifier
387/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
388/// '}' attributes[opt]
389/// [GNU] struct-or-union attributes[opt] identifier
390/// struct-or-union:
391/// 'struct'
392/// 'union'
Chris Lattner197b4342009-04-12 21:49:30 +0000393void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
394 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000395 TemplateParameterLists *TemplateParams,
396 AccessSpecifier AS) {
Chris Lattner197b4342009-04-12 21:49:30 +0000397 DeclSpec::TST TagType;
398 if (TagTokKind == tok::kw_struct)
399 TagType = DeclSpec::TST_struct;
400 else if (TagTokKind == tok::kw_class)
401 TagType = DeclSpec::TST_class;
402 else {
403 assert(TagTokKind == tok::kw_union && "Not a class specifier");
404 TagType = DeclSpec::TST_union;
405 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000406
407 AttributeList *Attr = 0;
408 // If attributes exist after tag, parse them.
409 if (Tok.is(tok::kw___attribute))
410 Attr = ParseAttributes();
411
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000412 // If declspecs exist after tag, parse them.
413 if (Tok.is(tok::kw___declspec) && PP.getLangOptions().Microsoft)
414 FuzzyParseMicrosoftDeclSpec();
415
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000416 // Parse the (optional) nested-name-specifier.
417 CXXScopeSpec SS;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000418 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS))
419 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000420 Diag(Tok, diag::err_expected_ident);
Douglas Gregora08b6c72009-02-17 23:15:12 +0000421
422 // Parse the (optional) class name or simple-template-id.
Douglas Gregorec93f442008-04-13 21:30:24 +0000423 IdentifierInfo *Name = 0;
424 SourceLocation NameLoc;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000425 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregorec93f442008-04-13 21:30:24 +0000426 if (Tok.is(tok::identifier)) {
427 Name = Tok.getIdentifierInfo();
428 NameLoc = ConsumeToken();
Douglas Gregor0c281a82009-02-25 19:37:18 +0000429 } else if (Tok.is(tok::annot_template_id)) {
430 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
431 NameLoc = ConsumeToken();
Douglas Gregora08b6c72009-02-17 23:15:12 +0000432
Douglas Gregoraabb8502009-03-31 00:43:58 +0000433 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor0c281a82009-02-25 19:37:18 +0000434 // The template-name in the simple-template-id refers to
435 // something other than a class template. Give an appropriate
436 // error message and skip to the ';'.
437 SourceRange Range(NameLoc);
438 if (SS.isNotEmpty())
439 Range.setBegin(SS.getBeginLoc());
Douglas Gregora08b6c72009-02-17 23:15:12 +0000440
Douglas Gregor0c281a82009-02-25 19:37:18 +0000441 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
442 << Name << static_cast<int>(TemplateId->Kind) << Range;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000443
Douglas Gregor0c281a82009-02-25 19:37:18 +0000444 DS.SetTypeSpecError();
445 SkipUntil(tok::semi, false, true);
446 TemplateId->Destroy();
447 return;
Douglas Gregora08b6c72009-02-17 23:15:12 +0000448 }
Douglas Gregorec93f442008-04-13 21:30:24 +0000449 }
450
451 // There are three options here. If we have 'struct foo;', then
452 // this is a forward declaration. If we have 'struct foo {...' or
Douglas Gregor0c281a82009-02-25 19:37:18 +0000453 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregorec93f442008-04-13 21:30:24 +0000454 // something like 'struct foo xyz', a reference.
455 Action::TagKind TK;
456 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
457 TK = Action::TK_Definition;
Anders Carlsson919a8d42009-05-11 22:25:03 +0000458 else if (Tok.is(tok::semi) && !DS.isFriendSpecified())
Douglas Gregorec93f442008-04-13 21:30:24 +0000459 TK = Action::TK_Declaration;
460 else
461 TK = Action::TK_Reference;
462
Douglas Gregor0c281a82009-02-25 19:37:18 +0000463 if (!Name && !TemplateId && TK != Action::TK_Definition) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000464 // We have a declaration or reference to an anonymous class.
Chris Lattnerf006a222008-11-18 07:48:38 +0000465 Diag(StartLoc, diag::err_anon_type_definition)
466 << DeclSpec::getSpecifierName(TagType);
Douglas Gregorec93f442008-04-13 21:30:24 +0000467
468 // Skip the rest of this declarator, up until the comma or semicolon.
469 SkipUntil(tok::comma, true);
Douglas Gregor0c281a82009-02-25 19:37:18 +0000470
471 if (TemplateId)
472 TemplateId->Destroy();
Douglas Gregorec93f442008-04-13 21:30:24 +0000473 return;
474 }
475
Douglas Gregord406b032009-02-06 22:42:48 +0000476 // Create the tag portion of the class or class template.
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000477 Action::DeclResult TagOrTempResult;
Douglas Gregor0c281a82009-02-25 19:37:18 +0000478 if (TemplateId && TK != Action::TK_Reference) {
Douglas Gregora08b6c72009-02-17 23:15:12 +0000479 // Explicit specialization or class template partial
480 // specialization. Let semantic analysis decide.
Douglas Gregor0c281a82009-02-25 19:37:18 +0000481 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
482 TemplateId->getTemplateArgs(),
483 TemplateId->getTemplateArgIsType(),
484 TemplateId->NumArgs);
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000485 TagOrTempResult
Douglas Gregora08b6c72009-02-17 23:15:12 +0000486 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TK,
Douglas Gregor0c281a82009-02-25 19:37:18 +0000487 StartLoc, SS,
Douglas Gregordd13e842009-03-30 22:58:21 +0000488 TemplateTy::make(TemplateId->Template),
Douglas Gregor0c281a82009-02-25 19:37:18 +0000489 TemplateId->TemplateNameLoc,
490 TemplateId->LAngleLoc,
491 TemplateArgsPtr,
492 TemplateId->getTemplateArgLocations(),
493 TemplateId->RAngleLoc,
494 Attr,
Douglas Gregora08b6c72009-02-17 23:15:12 +0000495 Action::MultiTemplateParamsArg(Actions,
496 TemplateParams? &(*TemplateParams)[0] : 0,
497 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor0c281a82009-02-25 19:37:18 +0000498 TemplateId->Destroy();
499 } else if (TemplateParams && TK != Action::TK_Reference)
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000500 TagOrTempResult = Actions.ActOnClassTemplate(CurScope, TagType, TK,
501 StartLoc, SS, Name, NameLoc,
502 Attr,
Douglas Gregord406b032009-02-06 22:42:48 +0000503 Action::MultiTemplateParamsArg(Actions,
504 &(*TemplateParams)[0],
Anders Carlssoned20fb92009-03-26 00:52:18 +0000505 TemplateParams->size()),
506 AS);
Douglas Gregord406b032009-02-06 22:42:48 +0000507 else
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000508 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, Name,
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000509 NameLoc, Attr, AS);
Douglas Gregorec93f442008-04-13 21:30:24 +0000510
511 // Parse the optional base clause (C++ only).
Chris Lattner31ccf0a2009-02-16 22:07:16 +0000512 if (getLang().CPlusPlus && Tok.is(tok::colon))
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000513 ParseBaseClause(TagOrTempResult.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000514
515 // If there is a body, parse it and inform the actions module.
516 if (Tok.is(tok::l_brace))
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000517 if (getLang().CPlusPlus)
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000518 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000519 else
Douglas Gregorc5d6fa72009-03-25 00:13:59 +0000520 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000521 else if (TK == Action::TK_Definition) {
522 // FIXME: Complain that we have a base-specifier list but no
523 // definition.
Chris Lattnerf006a222008-11-18 07:48:38 +0000524 Diag(Tok, diag::err_expected_lbrace);
Douglas Gregorec93f442008-04-13 21:30:24 +0000525 }
526
527 const char *PrevSpec = 0;
Anders Carlssonef3fa4f2009-05-11 22:27:47 +0000528 if (TagOrTempResult.isInvalid()) {
Douglas Gregord406b032009-02-06 22:42:48 +0000529 DS.SetTypeSpecError();
Anders Carlssonef3fa4f2009-05-11 22:27:47 +0000530 return;
531 }
532
533 if (DS.isFriendSpecified() &&
534 !Actions.ActOnFriendDecl(CurScope, DS.getFriendSpecLoc(),
535 TagOrTempResult.get()))
536 return;
537
538 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec,
539 TagOrTempResult.get().getAs<void>()))
Chris Lattnerf006a222008-11-18 07:48:38 +0000540 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Douglas Gregorec93f442008-04-13 21:30:24 +0000541}
542
543/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
544///
545/// base-clause : [C++ class.derived]
546/// ':' base-specifier-list
547/// base-specifier-list:
548/// base-specifier '...'[opt]
549/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattner5261d0c2009-03-28 19:18:32 +0000550void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000551 assert(Tok.is(tok::colon) && "Not a base clause");
552 ConsumeToken();
553
Douglas Gregorabed2172008-10-22 17:49:05 +0000554 // Build up an array of parsed base specifiers.
555 llvm::SmallVector<BaseTy *, 8> BaseInfo;
556
Douglas Gregorec93f442008-04-13 21:30:24 +0000557 while (true) {
558 // Parse a base-specifier.
Douglas Gregorabed2172008-10-22 17:49:05 +0000559 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000560 if (Result.isInvalid()) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000561 // Skip the rest of this base specifier, up until the comma or
562 // opening brace.
Douglas Gregorabed2172008-10-22 17:49:05 +0000563 SkipUntil(tok::comma, tok::l_brace, true, true);
564 } else {
565 // Add this to our array of base specifiers.
Douglas Gregor10a18fc2009-01-26 22:44:13 +0000566 BaseInfo.push_back(Result.get());
Douglas Gregorec93f442008-04-13 21:30:24 +0000567 }
568
569 // If the next token is a comma, consume it and keep reading
570 // base-specifiers.
571 if (Tok.isNot(tok::comma)) break;
572
573 // Consume the comma.
574 ConsumeToken();
575 }
Douglas Gregorabed2172008-10-22 17:49:05 +0000576
577 // Attach the base specifiers
578 Actions.ActOnBaseSpecifiers(ClassDecl, &BaseInfo[0], BaseInfo.size());
Douglas Gregorec93f442008-04-13 21:30:24 +0000579}
580
581/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
582/// one entry in the base class list of a class specifier, for example:
583/// class foo : public bar, virtual private baz {
584/// 'public bar' and 'virtual private baz' are each base-specifiers.
585///
586/// base-specifier: [C++ class.derived]
587/// ::[opt] nested-name-specifier[opt] class-name
588/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
589/// class-name
590/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
591/// class-name
Chris Lattner5261d0c2009-03-28 19:18:32 +0000592Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregorec93f442008-04-13 21:30:24 +0000593 bool IsVirtual = false;
594 SourceLocation StartLoc = Tok.getLocation();
595
596 // Parse the 'virtual' keyword.
597 if (Tok.is(tok::kw_virtual)) {
598 ConsumeToken();
599 IsVirtual = true;
600 }
601
602 // Parse an (optional) access specifier.
603 AccessSpecifier Access = getAccessSpecifierIfPresent();
604 if (Access)
605 ConsumeToken();
606
607 // Parse the 'virtual' keyword (again!), in case it came after the
608 // access specifier.
609 if (Tok.is(tok::kw_virtual)) {
610 SourceLocation VirtualLoc = ConsumeToken();
611 if (IsVirtual) {
612 // Complain about duplicate 'virtual'
Chris Lattnerf006a222008-11-18 07:48:38 +0000613 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregord7cb0372009-04-01 21:51:26 +0000614 << CodeModificationHint::CreateRemoval(SourceRange(VirtualLoc));
Douglas Gregorec93f442008-04-13 21:30:24 +0000615 }
616
617 IsVirtual = true;
618 }
619
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000620 // Parse optional '::' and optional nested-name-specifier.
621 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +0000622 ParseOptionalCXXScopeSpecifier(SS);
Douglas Gregorec93f442008-04-13 21:30:24 +0000623
Douglas Gregorec93f442008-04-13 21:30:24 +0000624 // The location of the base class itself.
625 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000626
627 // Parse the class-name.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000628 SourceLocation EndLocation;
Douglas Gregord7cb0372009-04-01 21:51:26 +0000629 TypeResult BaseType = ParseClassName(EndLocation, &SS);
630 if (BaseType.isInvalid())
Douglas Gregor8210a8e2008-11-05 20:51:48 +0000631 return true;
Douglas Gregorec93f442008-04-13 21:30:24 +0000632
633 // Find the complete source range for the base-specifier.
Douglas Gregor7bbed2a2009-02-25 23:52:28 +0000634 SourceRange Range(StartLoc, EndLocation);
Douglas Gregorec93f442008-04-13 21:30:24 +0000635
Douglas Gregorec93f442008-04-13 21:30:24 +0000636 // Notify semantic analysis that we have parsed a complete
637 // base-specifier.
Sebastian Redl6008ac32008-11-25 22:21:31 +0000638 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregord7cb0372009-04-01 21:51:26 +0000639 BaseType.get(), BaseLoc);
Douglas Gregorec93f442008-04-13 21:30:24 +0000640}
641
642/// getAccessSpecifierIfPresent - Determine whether the next token is
643/// a C++ access-specifier.
644///
645/// access-specifier: [C++ class.derived]
646/// 'private'
647/// 'protected'
648/// 'public'
Douglas Gregor696be932008-04-14 00:13:42 +0000649AccessSpecifier Parser::getAccessSpecifierIfPresent() const
Douglas Gregorec93f442008-04-13 21:30:24 +0000650{
651 switch (Tok.getKind()) {
652 default: return AS_none;
653 case tok::kw_private: return AS_private;
654 case tok::kw_protected: return AS_protected;
655 case tok::kw_public: return AS_public;
656 }
657}
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000658
659/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
660///
661/// member-declaration:
662/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
663/// function-definition ';'[opt]
664/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
665/// using-declaration [TODO]
Anders Carlssonab041982009-03-11 16:27:10 +0000666/// [C++0x] static_assert-declaration
Anders Carlssoned20fb92009-03-26 00:52:18 +0000667/// template-declaration
Chris Lattnerf3375de2008-12-18 01:12:00 +0000668/// [GNU] '__extension__' member-declaration
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000669///
670/// member-declarator-list:
671/// member-declarator
672/// member-declarator-list ',' member-declarator
673///
674/// member-declarator:
675/// declarator pure-specifier[opt]
676/// declarator constant-initializer[opt]
677/// identifier[opt] ':' constant-expression
678///
Sebastian Redla55834a2009-04-12 17:16:29 +0000679/// pure-specifier:
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000680/// '= 0'
681///
682/// constant-initializer:
683/// '=' constant-expression
684///
Chris Lattnera17991f2009-03-29 16:50:03 +0000685void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS) {
Anders Carlssonab041982009-03-11 16:27:10 +0000686 // static_assert-declaration
Chris Lattnera17991f2009-03-29 16:50:03 +0000687 if (Tok.is(tok::kw_static_assert)) {
Chris Lattner9802a0a2009-04-02 04:16:50 +0000688 SourceLocation DeclEnd;
689 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattnera17991f2009-03-29 16:50:03 +0000690 return;
691 }
Anders Carlssonab041982009-03-11 16:27:10 +0000692
Chris Lattnera17991f2009-03-29 16:50:03 +0000693 if (Tok.is(tok::kw_template)) {
Chris Lattner9802a0a2009-04-02 04:16:50 +0000694 SourceLocation DeclEnd;
695 ParseTemplateDeclarationOrSpecialization(Declarator::MemberContext, DeclEnd,
696 AS);
Chris Lattnera17991f2009-03-29 16:50:03 +0000697 return;
698 }
Anders Carlssoned20fb92009-03-26 00:52:18 +0000699
Chris Lattnerf3375de2008-12-18 01:12:00 +0000700 // Handle: member-declaration ::= '__extension__' member-declaration
701 if (Tok.is(tok::kw___extension__)) {
702 // __extension__ silences extension warnings in the subexpression.
703 ExtensionRAIIObject O(Diags); // Use RAII to do this.
704 ConsumeToken();
705 return ParseCXXClassMemberDeclaration(AS);
706 }
707
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000708 SourceLocation DSStart = Tok.getLocation();
709 // decl-specifier-seq:
710 // Parse the common declaration-specifiers piece.
711 DeclSpec DS;
Douglas Gregor0c793bb2009-03-25 22:00:53 +0000712 ParseDeclarationSpecifiers(DS, 0, AS);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000713
714 if (Tok.is(tok::semi)) {
715 ConsumeToken();
716 // C++ 9.2p7: The member-declarator-list can be omitted only after a
717 // class-specifier or an enum-specifier or in a friend declaration.
718 // FIXME: Friend declarations.
719 switch (DS.getTypeSpecType()) {
Chris Lattnera17991f2009-03-29 16:50:03 +0000720 case DeclSpec::TST_struct:
721 case DeclSpec::TST_union:
722 case DeclSpec::TST_class:
723 case DeclSpec::TST_enum:
724 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
725 return;
726 default:
727 Diag(DSStart, diag::err_no_declarators);
728 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000729 }
730 }
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000731
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000732 Declarator DeclaratorInfo(DS, Declarator::MemberContext);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000733
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000734 if (Tok.isNot(tok::colon)) {
735 // Parse the first declarator.
736 ParseDeclarator(DeclaratorInfo);
737 // Error parsing the declarator?
Douglas Gregor6704b312008-11-17 22:58:34 +0000738 if (!DeclaratorInfo.hasName()) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000739 // If so, skip until the semi-colon or a }.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000740 SkipUntil(tok::r_brace, true);
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000741 if (Tok.is(tok::semi))
742 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000743 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000744 }
745
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000746 // function-definition:
Douglas Gregora65e8dd2008-11-05 04:29:56 +0000747 if (Tok.is(tok::l_brace)
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000748 || (DeclaratorInfo.isFunctionDeclarator() &&
749 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000750 if (!DeclaratorInfo.isFunctionDeclarator()) {
751 Diag(Tok, diag::err_func_def_no_params);
752 ConsumeBrace();
753 SkipUntil(tok::r_brace, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000754 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000755 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000756
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000757 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
758 Diag(Tok, diag::err_function_declared_typedef);
759 // This recovery skips the entire function body. It would be nice
760 // to simply call ParseCXXInlineMethodDef() below, however Sema
761 // assumes the declarator represents a function, not a typedef.
762 ConsumeBrace();
763 SkipUntil(tok::r_brace, true);
Chris Lattnera17991f2009-03-29 16:50:03 +0000764 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000765 }
766
Chris Lattnera17991f2009-03-29 16:50:03 +0000767 ParseCXXInlineMethodDef(AS, DeclaratorInfo);
768 return;
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000769 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000770 }
771
772 // member-declarator-list:
773 // member-declarator
774 // member-declarator-list ',' member-declarator
775
Chris Lattnera17991f2009-03-29 16:50:03 +0000776 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl62261042008-12-09 20:22:58 +0000777 OwningExprResult BitfieldSize(Actions);
778 OwningExprResult Init(Actions);
Sebastian Redla55834a2009-04-12 17:16:29 +0000779 bool Deleted = false;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000780
781 while (1) {
782
783 // member-declarator:
784 // declarator pure-specifier[opt]
785 // declarator constant-initializer[opt]
786 // identifier[opt] ':' constant-expression
787
788 if (Tok.is(tok::colon)) {
789 ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000790 BitfieldSize = ParseConstantExpression();
791 if (BitfieldSize.isInvalid())
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000792 SkipUntil(tok::comma, true, true);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000793 }
794
795 // pure-specifier:
796 // '= 0'
797 //
798 // constant-initializer:
799 // '=' constant-expression
Sebastian Redla55834a2009-04-12 17:16:29 +0000800 //
801 // defaulted/deleted function-definition:
802 // '=' 'default' [TODO]
803 // '=' 'delete'
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000804
805 if (Tok.is(tok::equal)) {
806 ConsumeToken();
Sebastian Redla55834a2009-04-12 17:16:29 +0000807 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
808 ConsumeToken();
809 Deleted = true;
810 } else {
811 Init = ParseInitializer();
812 if (Init.isInvalid())
813 SkipUntil(tok::comma, true, true);
814 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000815 }
816
817 // If attributes exist after the declarator, parse them.
Sebastian Redl0c986032009-02-09 18:23:29 +0000818 if (Tok.is(tok::kw___attribute)) {
819 SourceLocation Loc;
820 AttributeList *AttrList = ParseAttributes(&Loc);
821 DeclaratorInfo.AddAttributes(AttrList, Loc);
822 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000823
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000824 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattnera17991f2009-03-29 16:50:03 +0000825 // this call will *not* return the created decl; It will return null.
Argiris Kirtzidis38f16712008-07-01 10:37:29 +0000826 // See Sema::ActOnCXXMemberDeclarator for details.
Chris Lattnera17991f2009-03-29 16:50:03 +0000827 DeclPtrTy ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
828 DeclaratorInfo,
829 BitfieldSize.release(),
Sebastian Redla55834a2009-04-12 17:16:29 +0000830 Init.release(),
831 Deleted);
Chris Lattnera17991f2009-03-29 16:50:03 +0000832 if (ThisDecl)
833 DeclsInGroup.push_back(ThisDecl);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000834
Douglas Gregor605de8d2008-12-16 21:30:33 +0000835 if (DeclaratorInfo.isFunctionDeclarator() &&
836 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
837 != DeclSpec::SCS_typedef) {
838 // We just declared a member function. If this member function
839 // has any default arguments, we'll need to parse them later.
840 LateParsedMethodDeclaration *LateMethod = 0;
841 DeclaratorChunk::FunctionTypeInfo &FTI
842 = DeclaratorInfo.getTypeObject(0).Fun;
843 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
844 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
845 if (!LateMethod) {
846 // Push this method onto the stack of late-parsed method
847 // declarations.
848 getCurTopClassStack().MethodDecls.push_back(
Chris Lattnera17991f2009-03-29 16:50:03 +0000849 LateParsedMethodDeclaration(ThisDecl));
Douglas Gregor605de8d2008-12-16 21:30:33 +0000850 LateMethod = &getCurTopClassStack().MethodDecls.back();
851
852 // Add all of the parameters prior to this one (they don't
853 // have default arguments).
854 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
855 for (unsigned I = 0; I < ParamIdx; ++I)
856 LateMethod->DefaultArgs.push_back(
857 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param));
858 }
859
860 // Add this parameter to the list of parameters (it or may
861 // not have a default argument).
862 LateMethod->DefaultArgs.push_back(
863 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
864 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
865 }
866 }
867 }
868
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000869 // If we don't have a comma, it is either the end of the list (a ';')
870 // or an error, bail out.
871 if (Tok.isNot(tok::comma))
872 break;
873
874 // Consume the comma.
875 ConsumeToken();
876
877 // Parse the next declarator.
878 DeclaratorInfo.clear();
Sebastian Redl62261042008-12-09 20:22:58 +0000879 BitfieldSize = 0;
880 Init = 0;
Sebastian Redla55834a2009-04-12 17:16:29 +0000881 Deleted = false;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000882
883 // Attributes are only allowed on the second declarator.
Sebastian Redl0c986032009-02-09 18:23:29 +0000884 if (Tok.is(tok::kw___attribute)) {
885 SourceLocation Loc;
886 AttributeList *AttrList = ParseAttributes(&Loc);
887 DeclaratorInfo.AddAttributes(AttrList, Loc);
888 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000889
Argiris Kirtzidisf8009b42008-06-28 08:10:48 +0000890 if (Tok.isNot(tok::colon))
891 ParseDeclarator(DeclaratorInfo);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000892 }
893
894 if (Tok.is(tok::semi)) {
895 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000896 Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
897 DeclsInGroup.size());
898 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000899 }
900
901 Diag(Tok, diag::err_expected_semi_decl_list);
902 // Skip to end of block or statement
903 SkipUntil(tok::r_brace, true, true);
904 if (Tok.is(tok::semi))
905 ConsumeToken();
Chris Lattnera17991f2009-03-29 16:50:03 +0000906 return;
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000907}
908
909/// ParseCXXMemberSpecification - Parse the class definition.
910///
911/// member-specification:
912/// member-declaration member-specification[opt]
913/// access-specifier ':' member-specification[opt]
914///
915void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattner5261d0c2009-03-28 19:18:32 +0000916 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Guptafa451432008-10-31 09:52:39 +0000917 assert((TagType == DeclSpec::TST_struct ||
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000918 TagType == DeclSpec::TST_union ||
Sanjiv Guptafa451432008-10-31 09:52:39 +0000919 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000920
Chris Lattnerc309ade2009-03-05 08:00:35 +0000921 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
922 PP.getSourceManager(),
923 "parsing struct/union/class body");
Chris Lattner7efd75e2009-03-05 02:25:03 +0000924
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000925 SourceLocation LBraceLoc = ConsumeBrace();
926
Douglas Gregorcab994d2009-01-09 22:42:13 +0000927 if (!CurScope->isClassScope() && // Not about to define a nested class.
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000928 CurScope->isInCXXInlineMethodScope()) {
929 // We will define a local class of an inline method.
930 // Push a new LexedMethodsForTopClass for its inline methods.
931 PushTopClassStack();
932 }
933
934 // Enter a scope for the class.
Douglas Gregorcab994d2009-01-09 22:42:13 +0000935 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000936
Douglas Gregord406b032009-02-06 22:42:48 +0000937 if (TagDecl)
938 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
939 else {
940 SkipUntil(tok::r_brace, false, false);
941 return;
942 }
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000943
944 // C++ 11p3: Members of a class defined with the keyword class are private
945 // by default. Members of a class defined with the keywords struct or union
946 // are public by default.
947 AccessSpecifier CurAS;
948 if (TagType == DeclSpec::TST_class)
949 CurAS = AS_private;
950 else
951 CurAS = AS_public;
952
953 // While we still have something to read, read the member-declarations.
954 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
955 // Each iteration of this loop reads one member-declaration.
956
957 // Check for extraneous top-level semicolon.
958 if (Tok.is(tok::semi)) {
959 Diag(Tok, diag::ext_extra_struct_semi);
960 ConsumeToken();
961 continue;
962 }
963
964 AccessSpecifier AS = getAccessSpecifierIfPresent();
965 if (AS != AS_none) {
966 // Current token is a C++ access specifier.
967 CurAS = AS;
968 ConsumeToken();
969 ExpectAndConsume(tok::colon, diag::err_expected_colon);
970 continue;
971 }
972
973 // Parse all the comma separated declarators.
974 ParseCXXClassMemberDeclaration(CurAS);
975 }
976
977 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
978
979 AttributeList *AttrList = 0;
980 // If attributes exist after class contents, parse them.
981 if (Tok.is(tok::kw___attribute))
982 AttrList = ParseAttributes(); // FIXME: where should I put them?
983
984 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
985 LBraceLoc, RBraceLoc);
986
987 // C++ 9.2p2: Within the class member-specification, the class is regarded as
988 // complete within function bodies, default arguments,
989 // exception-specifications, and constructor ctor-initializers (including
990 // such things in nested classes).
991 //
Douglas Gregor605de8d2008-12-16 21:30:33 +0000992 // FIXME: Only function bodies and constructor ctor-initializers are
993 // parsed correctly, fix the rest.
Douglas Gregorcab994d2009-01-09 22:42:13 +0000994 if (!CurScope->getParent()->isClassScope()) {
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000995 // We are not inside a nested class. This class and its nested classes
Douglas Gregor605de8d2008-12-16 21:30:33 +0000996 // are complete and we can parse the delayed portions of method
997 // declarations and the lexed inline method definitions.
998 ParseLexedMethodDeclarations();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +0000999 ParseLexedMethodDefs();
1000
1001 // For a local class of inline method, pop the LexedMethodsForTopClass that
1002 // was previously pushed.
1003
Sanjiv Guptafa451432008-10-31 09:52:39 +00001004 assert((CurScope->isInCXXInlineMethodScope() ||
1005 TopClassStacks.size() == 1) &&
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001006 "MethodLexers not getting popped properly!");
1007 if (CurScope->isInCXXInlineMethodScope())
1008 PopTopClassStack();
1009 }
1010
1011 // Leave the class scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00001012 ClassScope.Exit();
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001013
Douglas Gregordb568cf2009-01-08 20:45:30 +00001014 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Argiris Kirtzidis9d784332008-06-24 22:12:16 +00001015}
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001016
1017/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1018/// which explicitly initializes the members or base classes of a
1019/// class (C++ [class.base.init]). For example, the three initializers
1020/// after the ':' in the Derived constructor below:
1021///
1022/// @code
1023/// class Base { };
1024/// class Derived : Base {
1025/// int x;
1026/// float f;
1027/// public:
1028/// Derived(float f) : Base(), x(17), f(f) { }
1029/// };
1030/// @endcode
1031///
1032/// [C++] ctor-initializer:
1033/// ':' mem-initializer-list
1034///
1035/// [C++] mem-initializer-list:
1036/// mem-initializer
1037/// mem-initializer , mem-initializer-list
Chris Lattner5261d0c2009-03-28 19:18:32 +00001038void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001039 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1040
1041 SourceLocation ColonLoc = ConsumeToken();
1042
1043 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
1044
1045 do {
1046 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor10a18fc2009-01-26 22:44:13 +00001047 if (!MemInit.isInvalid())
1048 MemInitializers.push_back(MemInit.get());
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001049
1050 if (Tok.is(tok::comma))
1051 ConsumeToken();
1052 else if (Tok.is(tok::l_brace))
1053 break;
1054 else {
1055 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redlbc9ef252009-04-26 20:35:05 +00001056 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001057 SkipUntil(tok::l_brace, true, true);
1058 break;
1059 }
1060 } while (true);
1061
1062 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
1063 &MemInitializers[0], MemInitializers.size());
1064}
1065
1066/// ParseMemInitializer - Parse a C++ member initializer, which is
1067/// part of a constructor initializer that explicitly initializes one
1068/// member or base class (C++ [class.base.init]). See
1069/// ParseConstructorInitializer for an example.
1070///
1071/// [C++] mem-initializer:
1072/// mem-initializer-id '(' expression-list[opt] ')'
1073///
1074/// [C++] mem-initializer-id:
1075/// '::'[opt] nested-name-specifier[opt] class-name
1076/// identifier
Chris Lattner5261d0c2009-03-28 19:18:32 +00001077Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001078 // FIXME: parse '::'[opt] nested-name-specifier[opt]
1079
1080 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001081 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001082 return true;
1083 }
1084
1085 // Get the identifier. This may be a member name or a class name,
1086 // but we'll let the semantic analysis determine which it is.
1087 IdentifierInfo *II = Tok.getIdentifierInfo();
1088 SourceLocation IdLoc = ConsumeToken();
1089
1090 // Parse the '('.
1091 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001092 Diag(Tok, diag::err_expected_lparen);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001093 return true;
1094 }
1095 SourceLocation LParenLoc = ConsumeParen();
1096
1097 // Parse the optional expression-list.
Sebastian Redl6008ac32008-11-25 22:21:31 +00001098 ExprVector ArgExprs(Actions);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001099 CommaLocsTy CommaLocs;
1100 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1101 SkipUntil(tok::r_paren);
1102 return true;
1103 }
1104
1105 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1106
Sebastian Redl6008ac32008-11-25 22:21:31 +00001107 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, II, IdLoc,
1108 LParenLoc, ArgExprs.take(),
1109 ArgExprs.size(), &CommaLocs[0], RParenLoc);
Douglas Gregora65e8dd2008-11-05 04:29:56 +00001110}
Douglas Gregor90a2c972008-11-25 03:22:00 +00001111
1112/// ParseExceptionSpecification - Parse a C++ exception-specification
1113/// (C++ [except.spec]).
1114///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001115/// exception-specification:
1116/// 'throw' '(' type-id-list [opt] ')'
1117/// [MS] 'throw' '(' '...' ')'
Douglas Gregor90a2c972008-11-25 03:22:00 +00001118///
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001119/// type-id-list:
1120/// type-id
1121/// type-id-list ',' type-id
Douglas Gregor90a2c972008-11-25 03:22:00 +00001122///
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001123bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
1124 std::vector<TypeTy*> &Exceptions,
1125 bool &hasAnyExceptionSpec) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00001126 assert(Tok.is(tok::kw_throw) && "expected throw");
1127
1128 SourceLocation ThrowLoc = ConsumeToken();
1129
1130 if (!Tok.is(tok::l_paren)) {
1131 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1132 }
1133 SourceLocation LParenLoc = ConsumeParen();
1134
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001135 // Parse throw(...), a Microsoft extension that means "this function
1136 // can throw anything".
1137 if (Tok.is(tok::ellipsis)) {
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001138 hasAnyExceptionSpec = true;
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001139 SourceLocation EllipsisLoc = ConsumeToken();
1140 if (!getLang().Microsoft)
1141 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redl0c986032009-02-09 18:23:29 +00001142 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor9ed9ac82008-12-01 18:00:20 +00001143 return false;
1144 }
1145
Douglas Gregor90a2c972008-11-25 03:22:00 +00001146 // Parse the sequence of type-ids.
1147 while (Tok.isNot(tok::r_paren)) {
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001148 TypeResult Res(ParseTypeName());
1149 if (!Res.isInvalid())
1150 Exceptions.push_back(Res.get());
Douglas Gregor90a2c972008-11-25 03:22:00 +00001151 if (Tok.is(tok::comma))
1152 ConsumeToken();
Sebastian Redl35f3a5b2009-04-29 17:30:04 +00001153 else
Douglas Gregor90a2c972008-11-25 03:22:00 +00001154 break;
1155 }
1156
Sebastian Redl0c986032009-02-09 18:23:29 +00001157 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001158 return false;
1159}