blob: 015ac5b5dddddbbc6924e072b4d5bd178fd974ae [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson0c6139d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor1b7f8982008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
Douglas Gregore37ac4f2008-04-13 21:30:24 +000017#include "clang/Parse/DeclSpec.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000018#include "clang/Parse/Scope.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000019#include "clang/Parse/Template.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000021using namespace clang;
22
23/// ParseNamespace - We know that the current token is a namespace keyword. This
24/// may either be a top level namespace or a block-level namespace alias.
25///
26/// namespace-definition: [C++ 7.3: basic.namespace]
27/// named-namespace-definition
28/// unnamed-namespace-definition
29///
30/// unnamed-namespace-definition:
31/// 'namespace' attributes[opt] '{' namespace-body '}'
32///
33/// named-namespace-definition:
34/// original-namespace-definition
35/// extension-namespace-definition
36///
37/// original-namespace-definition:
38/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
39///
40/// extension-namespace-definition:
41/// 'namespace' original-namespace-name '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000042///
Chris Lattner8f08cb72007-08-25 06:57:03 +000043/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
44/// 'namespace' identifier '=' qualified-namespace-specifier ';'
45///
Chris Lattner97144fc2009-04-02 04:16:50 +000046Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context,
47 SourceLocation &DeclEnd) {
Chris Lattner04d66662007-10-09 17:33:22 +000048 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000049 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor49f40bd2009-09-18 19:03:04 +000051 if (Tok.is(tok::code_completion)) {
52 Actions.CodeCompleteNamespaceDecl(CurScope);
53 ConsumeToken();
54 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000055
Chris Lattner8f08cb72007-08-25 06:57:03 +000056 SourceLocation IdentLoc;
57 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000058
59 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000060
Chris Lattner04d66662007-10-09 17:33:22 +000061 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000062 Ident = Tok.getIdentifierInfo();
63 IdentLoc = ConsumeToken(); // eat the identifier.
64 }
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner8f08cb72007-08-25 06:57:03 +000066 // Read label attributes, if present.
Ted Kremenek1e377652010-02-11 02:19:13 +000067 llvm::OwningPtr<AttributeList> AttrList;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000068 if (Tok.is(tok::kw___attribute)) {
69 attrTok = Tok;
70
Chris Lattner8f08cb72007-08-25 06:57:03 +000071 // FIXME: save these somewhere.
Ted Kremenek1e377652010-02-11 02:19:13 +000072 AttrList.reset(ParseGNUAttributes());
Douglas Gregor6a588dd2009-06-17 19:49:00 +000073 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Douglas Gregor6a588dd2009-06-17 19:49:00 +000075 if (Tok.is(tok::equal)) {
76 if (AttrList)
77 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
78
Chris Lattner97144fc2009-04-02 04:16:50 +000079 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000080 }
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattner51448322009-03-29 14:02:43 +000082 if (Tok.isNot(tok::l_brace)) {
Mike Stump1eb44332009-09-09 15:08:12 +000083 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000084 diag::err_expected_ident_lbrace);
85 return DeclPtrTy();
Chris Lattner8f08cb72007-08-25 06:57:03 +000086 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattner51448322009-03-29 14:02:43 +000088 SourceLocation LBrace = ConsumeBrace();
89
90 // Enter a scope for the namespace.
91 ParseScope NamespaceScope(this, Scope::DeclScope);
92
93 DeclPtrTy NamespcDecl =
Ted Kremenek1e377652010-02-11 02:19:13 +000094 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace,
95 AttrList.get());
Chris Lattner51448322009-03-29 14:02:43 +000096
97 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions,
98 PP.getSourceManager(),
99 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Sean Huntbbd37c62009-11-21 08:43:09 +0000101 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
102 CXX0XAttributeList Attr;
103 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
104 Attr = ParseCXX0XAttributes();
105 ParseExternalDeclaration(Attr);
106 }
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Chris Lattner51448322009-03-29 14:02:43 +0000108 // Leave the namespace scope.
109 NamespaceScope.Exit();
110
Chris Lattner97144fc2009-04-02 04:16:50 +0000111 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
112 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000113
Chris Lattner97144fc2009-04-02 04:16:50 +0000114 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000115 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000116}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000117
Anders Carlssonf67606a2009-03-28 04:07:16 +0000118/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
119/// alias definition.
120///
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000121Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000122 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000123 IdentifierInfo *Alias,
124 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000125 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Anders Carlssonf67606a2009-03-28 04:07:16 +0000127 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000129 if (Tok.is(tok::code_completion)) {
130 Actions.CodeCompleteNamespaceAliasDecl(CurScope);
131 ConsumeToken();
132 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000133
Anders Carlssonf67606a2009-03-28 04:07:16 +0000134 CXXScopeSpec SS;
135 // Parse (optional) nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000136 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000137
138 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
139 Diag(Tok, diag::err_expected_namespace_name);
140 // Skip to end of the definition and eat the ';'.
141 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000142 return DeclPtrTy();
Anders Carlssonf67606a2009-03-28 04:07:16 +0000143 }
144
145 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000146 IdentifierInfo *Ident = Tok.getIdentifierInfo();
147 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Anders Carlssonf67606a2009-03-28 04:07:16 +0000149 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000150 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000151 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
152 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000153
154 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000155 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000156}
157
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000158/// ParseLinkage - We know that the current token is a string_literal
159/// and just before that, that extern was seen.
160///
161/// linkage-specification: [C++ 7.5p2: dcl.link]
162/// 'extern' string-literal '{' declaration-seq[opt] '}'
163/// 'extern' string-literal declaration
164///
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000165Parser::DeclPtrTy Parser::ParseLinkage(ParsingDeclSpec &DS,
166 unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000167 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000168 llvm::SmallString<8> LangBuffer;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000169 // LangBuffer is guaranteed to be big enough.
Douglas Gregor453091c2010-03-16 22:30:13 +0000170 bool Invalid = false;
171 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
172 if (Invalid)
173 return DeclPtrTy();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000174
175 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000176
Douglas Gregor074149e2009-01-05 19:45:36 +0000177 ParseScope LinkageScope(this, Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000178 DeclPtrTy LinkageSpec
179 = Actions.ActOnStartLinkageSpecification(CurScope,
Douglas Gregor074149e2009-01-05 19:45:36 +0000180 /*FIXME: */SourceLocation(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000181 Loc, Lang,
Mike Stump1eb44332009-09-09 15:08:12 +0000182 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000183 : SourceLocation());
184
Sean Huntbbd37c62009-11-21 08:43:09 +0000185 CXX0XAttributeList Attr;
186 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
187 Attr = ParseCXX0XAttributes();
188 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000189
Douglas Gregor074149e2009-01-05 19:45:36 +0000190 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian3acd9aa2009-12-09 21:39:38 +0000191 ParseDeclarationOrFunctionDefinition(DS, Attr.AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000192 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000193 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000194 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000195
Douglas Gregor63a01132010-02-07 08:38:28 +0000196 DS.abort();
197
Sean Huntbbd37c62009-11-21 08:43:09 +0000198 if (Attr.HasAttr)
199 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
200 << Attr.Range;
201
Douglas Gregorf44515a2008-12-16 22:23:02 +0000202 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000203 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000204 CXX0XAttributeList Attr;
205 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
206 Attr = ParseCXX0XAttributes();
207 ParseExternalDeclaration(Attr);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000208 }
209
Douglas Gregorf44515a2008-12-16 22:23:02 +0000210 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Douglas Gregor074149e2009-01-05 19:45:36 +0000211 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000212}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000213
Douglas Gregorf780abc2008-12-30 03:27:21 +0000214/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
215/// using-directive. Assumes that current token is 'using'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000216Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000217 SourceLocation &DeclEnd,
218 CXX0XAttributeList Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000219 assert(Tok.is(tok::kw_using) && "Not using token");
220
221 // Eat 'using'.
222 SourceLocation UsingLoc = ConsumeToken();
223
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000224 if (Tok.is(tok::code_completion)) {
225 Actions.CodeCompleteUsing(CurScope);
226 ConsumeToken();
227 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000228
Chris Lattner2f274772009-01-06 06:55:51 +0000229 if (Tok.is(tok::kw_namespace))
Douglas Gregorf780abc2008-12-30 03:27:21 +0000230 // Next token after 'using' is 'namespace' so it must be using-directive
Sean Huntbbd37c62009-11-21 08:43:09 +0000231 return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList);
232
233 if (Attr.HasAttr)
234 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
235 << Attr.Range;
Chris Lattner2f274772009-01-06 06:55:51 +0000236
237 // Otherwise, it must be using-declaration.
Sean Huntbbd37c62009-11-21 08:43:09 +0000238 // Ignore illegal attributes (the caller should already have issued an error.
Chris Lattner97144fc2009-04-02 04:16:50 +0000239 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000240}
241
242/// ParseUsingDirective - Parse C++ using-directive, assumes
243/// that current token is 'namespace' and 'using' was already parsed.
244///
245/// using-directive: [C++ 7.3.p4: namespace.udir]
246/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
247/// namespace-name ;
248/// [GNU] using-directive:
249/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
250/// namespace-name attributes[opt] ;
251///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000252Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000253 SourceLocation UsingLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000254 SourceLocation &DeclEnd,
255 AttributeList *Attr) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000256 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
257
258 // Eat 'namespace'.
259 SourceLocation NamespcLoc = ConsumeToken();
260
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000261 if (Tok.is(tok::code_completion)) {
262 Actions.CodeCompleteUsingDirective(CurScope);
263 ConsumeToken();
264 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000265
Douglas Gregorf780abc2008-12-30 03:27:21 +0000266 CXXScopeSpec SS;
267 // Parse (optional) nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000268 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000269
Douglas Gregorf780abc2008-12-30 03:27:21 +0000270 IdentifierInfo *NamespcName = 0;
271 SourceLocation IdentLoc = SourceLocation();
272
273 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000274 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000275 Diag(Tok, diag::err_expected_namespace_name);
276 // If there was invalid namespace name, skip to end of decl, and eat ';'.
277 SkipUntil(tok::semi);
278 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Chris Lattnerb28317a2009-03-28 19:18:32 +0000279 return DeclPtrTy();
Douglas Gregorf780abc2008-12-30 03:27:21 +0000280 }
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Chris Lattner823c44e2009-01-06 07:27:21 +0000282 // Parse identifier.
283 NamespcName = Tok.getIdentifierInfo();
284 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Chris Lattner823c44e2009-01-06 07:27:21 +0000286 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000287 bool GNUAttr = false;
288 if (Tok.is(tok::kw___attribute)) {
289 GNUAttr = true;
290 Attr = addAttributeLists(Attr, ParseGNUAttributes());
291 }
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Chris Lattner823c44e2009-01-06 07:27:21 +0000293 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000294 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000295 ExpectAndConsume(tok::semi,
Sean Huntbbd37c62009-11-21 08:43:09 +0000296 GNUAttr ? diag::err_expected_semi_after_attribute_list :
Chris Lattner6869d8e2009-06-14 00:07:48 +0000297 diag::err_expected_semi_after_namespace_name, "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000298
299 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000300 IdentLoc, NamespcName, Attr);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000301}
302
303/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
304/// 'using' was already seen.
305///
306/// using-declaration: [C++ 7.3.p3: namespace.udecl]
307/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000308/// unqualified-id
309/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000310///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000311Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context,
Chris Lattner97144fc2009-04-02 04:16:50 +0000312 SourceLocation UsingLoc,
Anders Carlsson595adc12009-08-29 19:54:19 +0000313 SourceLocation &DeclEnd,
314 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000315 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000316 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000317 bool IsTypeName;
318
319 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000320 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000321 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000322 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000323 ConsumeToken();
324 IsTypeName = true;
325 }
326 else
327 IsTypeName = false;
328
329 // Parse nested-name-specifier.
Chris Lattnerbe1ea442009-12-07 01:38:03 +0000330 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000331
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000332 // Check nested-name specifier.
333 if (SS.isInvalid()) {
334 SkipUntil(tok::semi);
335 return DeclPtrTy();
336 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000337
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000338 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000339 // destructor names and allow the action module to diagnose any semantic
340 // errors.
341 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000342 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000343 /*EnteringContext=*/false,
344 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000345 /*AllowConstructorName=*/true,
346 /*ObjectType=*/0,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000347 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000348 SkipUntil(tok::semi);
349 return DeclPtrTy();
350 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000351
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000352 // Parse (optional) attributes (most likely GNU strong-using extension).
Ted Kremenek1e377652010-02-11 02:19:13 +0000353 llvm::OwningPtr<AttributeList> AttrList;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000354 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +0000355 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000357 // Eat ';'.
358 DeclEnd = Tok.getLocation();
359 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000360 AttrList ? "attributes list" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000361 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000362
John McCall60fa3cf2009-12-11 02:10:03 +0000363 return Actions.ActOnUsingDeclaration(CurScope, AS, true, UsingLoc, SS, Name,
Ted Kremenek1e377652010-02-11 02:19:13 +0000364 AttrList.get(), IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000365}
366
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000367/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
368///
369/// static_assert-declaration:
370/// static_assert ( constant-expression , string-literal ) ;
371///
Chris Lattner97144fc2009-04-02 04:16:50 +0000372Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000373 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
374 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000376 if (Tok.isNot(tok::l_paren)) {
377 Diag(Tok, diag::err_expected_lparen);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000378 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000379 }
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000381 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000382
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000383 OwningExprResult AssertExpr(ParseConstantExpression());
384 if (AssertExpr.isInvalid()) {
385 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000386 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Anders Carlssonad5f9602009-03-13 23:29:20 +0000389 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000390 return DeclPtrTy();
Anders Carlssonad5f9602009-03-13 23:29:20 +0000391
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000392 if (Tok.isNot(tok::string_literal)) {
393 Diag(Tok, diag::err_expected_string_literal);
394 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000395 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000398 OwningExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000399 if (AssertMessage.isInvalid())
Chris Lattnerb28317a2009-03-28 19:18:32 +0000400 return DeclPtrTy();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000401
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000402 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattner97144fc2009-04-02 04:16:50 +0000404 DeclEnd = Tok.getLocation();
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000405 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert);
406
Mike Stump1eb44332009-09-09 15:08:12 +0000407 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr),
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000408 move(AssertMessage));
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000409}
410
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000411/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
412///
413/// 'decltype' ( expression )
414///
415void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
416 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
417
418 SourceLocation StartLoc = ConsumeToken();
419 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000420
421 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000422 "decltype")) {
423 SkipUntil(tok::r_paren);
424 return;
425 }
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000427 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000429 // C++0x [dcl.type.simple]p4:
430 // The operand of the decltype specifier is an unevaluated operand.
431 EnterExpressionEvaluationContext Unevaluated(Actions,
432 Action::Unevaluated);
433 OwningExprResult Result = ParseExpression();
434 if (Result.isInvalid()) {
435 SkipUntil(tok::r_paren);
436 return;
437 }
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000439 // Match the ')'
440 SourceLocation RParenLoc;
441 if (Tok.is(tok::r_paren))
442 RParenLoc = ConsumeParen();
443 else
444 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000446 if (RParenLoc.isInvalid())
447 return;
448
449 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000450 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000451 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000452 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000453 DiagID, Result.release()))
454 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000455}
456
Douglas Gregor42a552f2008-11-05 20:51:48 +0000457/// ParseClassName - Parse a C++ class-name, which names a class. Note
458/// that we only check that the result names a type; semantic analysis
459/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000460/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000461/// found.
462///
463/// class-name: [C++ 9.1]
464/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000465/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000466///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000467Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000468 CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000469 // Check whether we have a template-id that names a type.
470 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000471 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000472 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000473 if (TemplateId->Kind == TNK_Type_template ||
474 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000475 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000476
477 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
478 TypeTy *Type = Tok.getAnnotationValue();
479 EndLocation = Tok.getAnnotationEndLoc();
480 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000481
482 if (Type)
483 return Type;
484 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000485 }
486
487 // Fall through to produce an error below.
488 }
489
Douglas Gregor42a552f2008-11-05 20:51:48 +0000490 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000491 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000492 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000493 }
494
Douglas Gregor84d0a192010-01-12 21:28:44 +0000495 IdentifierInfo *Id = Tok.getIdentifierInfo();
496 SourceLocation IdLoc = ConsumeToken();
497
498 if (Tok.is(tok::less)) {
499 // It looks the user intended to write a template-id here, but the
500 // template-name was wrong. Try to fix that.
501 TemplateNameKind TNK = TNK_Type_template;
502 TemplateTy Template;
503 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, CurScope,
504 SS, Template, TNK)) {
505 Diag(IdLoc, diag::err_unknown_template_name)
506 << Id;
507 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000508
Douglas Gregor84d0a192010-01-12 21:28:44 +0000509 if (!Template)
510 return true;
511
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000512 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000513 UnqualifiedId TemplateName;
514 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000515
Douglas Gregor84d0a192010-01-12 21:28:44 +0000516 // Parse the full template-id, then turn it into a type.
517 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
518 SourceLocation(), true))
519 return true;
520 if (TNK == TNK_Dependent_template_name)
521 AnnotateTemplateIdTokenAsType(SS);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000522
Douglas Gregor84d0a192010-01-12 21:28:44 +0000523 // If we didn't end up with a typename token, there's nothing more we
524 // can do.
525 if (Tok.isNot(tok::annot_typename))
526 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000527
Douglas Gregor84d0a192010-01-12 21:28:44 +0000528 // Retrieve the type from the annotation token, consume that token, and
529 // return.
530 EndLocation = Tok.getAnnotationEndLoc();
531 TypeTy *Type = Tok.getAnnotationValue();
532 ConsumeToken();
533 return Type;
534 }
535
Douglas Gregor42a552f2008-11-05 20:51:48 +0000536 // We have an identifier; check whether it is actually a type.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000537 TypeTy *Type = Actions.getTypeName(*Id, IdLoc, CurScope, SS, true);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000538 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000539 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000540 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000541 }
542
543 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000544 EndLocation = IdLoc;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000545 return Type;
546}
547
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000548/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
549/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
550/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000551/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000552///
553/// class-specifier: [C++ class]
554/// class-head '{' member-specification[opt] '}'
555/// class-head '{' member-specification[opt] '}' attributes[opt]
556/// class-head:
557/// class-key identifier[opt] base-clause[opt]
558/// class-key nested-name-specifier identifier base-clause[opt]
559/// class-key nested-name-specifier[opt] simple-template-id
560/// base-clause[opt]
561/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000562/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000563/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000564/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000565/// simple-template-id base-clause[opt]
566/// class-key:
567/// 'class'
568/// 'struct'
569/// 'union'
570///
571/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000572/// class-key ::[opt] nested-name-specifier[opt] identifier
573/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
574/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000575///
576/// Note that the C++ class-specifier and elaborated-type-specifier,
577/// together, subsume the C99 struct-or-union-specifier:
578///
579/// struct-or-union-specifier: [C99 6.7.2.1]
580/// struct-or-union identifier[opt] '{' struct-contents '}'
581/// struct-or-union identifier
582/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
583/// '}' attributes[opt]
584/// [GNU] struct-or-union attributes[opt] identifier
585/// struct-or-union:
586/// 'struct'
587/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000588void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
589 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000590 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redld9bafa72010-02-03 21:21:43 +0000591 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000592 DeclSpec::TST TagType;
593 if (TagTokKind == tok::kw_struct)
594 TagType = DeclSpec::TST_struct;
595 else if (TagTokKind == tok::kw_class)
596 TagType = DeclSpec::TST_class;
597 else {
598 assert(TagTokKind == tok::kw_union && "Not a class specifier");
599 TagType = DeclSpec::TST_union;
600 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000601
Douglas Gregor374929f2009-09-18 15:37:17 +0000602 if (Tok.is(tok::code_completion)) {
603 // Code completion for a struct, class, or union name.
604 Actions.CodeCompleteTag(CurScope, TagType);
605 ConsumeToken();
606 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000607
Sean Huntbbd37c62009-11-21 08:43:09 +0000608 AttributeList *AttrList = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000609 // If attributes exist after tag, parse them.
610 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +0000611 AttrList = ParseGNUAttributes();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000612
Steve Narofff59e17e2008-12-24 20:59:21 +0000613 // If declspecs exist after tag, parse them.
Eli Friedman290eeb02009-06-08 23:27:34 +0000614 if (Tok.is(tok::kw___declspec))
Sean Huntbbd37c62009-11-21 08:43:09 +0000615 AttrList = ParseMicrosoftDeclSpec(AttrList);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000616
Sean Huntbbd37c62009-11-21 08:43:09 +0000617 // If C++0x attributes exist here, parse them.
618 // FIXME: Are we consistent with the ordering of parsing of different
619 // styles of attributes?
620 if (isCXX0XAttributeSpecifier())
621 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Douglas Gregorb117a602009-09-04 05:53:02 +0000623 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
624 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
625 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000626 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000627 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
628 // properly.
629 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
630 Tok.setKind(tok::identifier);
631 }
632
633 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
634 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
635 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000636 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000637 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
638 // properly.
639 Tok.getIdentifierInfo()->setTokenID(tok::identifier);
640 Tok.setKind(tok::identifier);
641 }
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000643 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000644 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000645 if (getLang().CPlusPlus) {
646 // "FOO : BAR" is not a potential typo for "FOO::BAR".
647 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000648
John McCall9ba61662010-02-26 08:45:28 +0000649 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
650 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +0000651 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
652 Diag(Tok, diag::err_expected_ident);
653 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000654
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000655 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
656
Douglas Gregorcc636682009-02-17 23:15:12 +0000657 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000658 IdentifierInfo *Name = 0;
659 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000660 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000661 if (Tok.is(tok::identifier)) {
662 Name = Tok.getIdentifierInfo();
663 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000664
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000665 if (Tok.is(tok::less)) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000666 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000667 // Eat the template argument list and try to continue parsing this as
668 // a class (or template thereof).
669 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000670 SourceLocation LAngleLoc, RAngleLoc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000671 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000672 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000673 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000674 // We couldn't parse the template argument list at all, so don't
675 // try to give any location information for the list.
676 LAngleLoc = RAngleLoc = SourceLocation();
677 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000678
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000679 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000680 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000681 << (TagType == DeclSpec::TST_class? 0
682 : TagType == DeclSpec::TST_struct? 1
683 : 2)
684 << Name
685 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000686
687 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000688 // we've removed its template argument list.
689 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
690 if (TemplateParams && TemplateParams->size() > 1) {
691 TemplateParams->pop_back();
692 } else {
693 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000694 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000695 = ParsedTemplateInfo::NonTemplate;
696 }
697 } else if (TemplateInfo.Kind
698 == ParsedTemplateInfo::ExplicitInstantiation) {
699 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000700 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000701 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000702 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000703 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000704 = SourceLocation();
705 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
706 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000707 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000708
709
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000710 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000711 } else if (Tok.is(tok::annot_template_id)) {
712 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
713 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000714
Douglas Gregorc45c2322009-03-31 00:43:58 +0000715 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000716 // The template-name in the simple-template-id refers to
717 // something other than a class template. Give an appropriate
718 // error message and skip to the ';'.
719 SourceRange Range(NameLoc);
720 if (SS.isNotEmpty())
721 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000722
Douglas Gregor39a8de12009-02-25 19:37:18 +0000723 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
724 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Douglas Gregor39a8de12009-02-25 19:37:18 +0000726 DS.SetTypeSpecError();
727 SkipUntil(tok::semi, false, true);
728 TemplateId->Destroy();
729 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000730 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000731 }
732
John McCall67d1a672009-08-06 02:15:43 +0000733 // There are four options here. If we have 'struct foo;', then this
734 // is either a forward declaration or a friend declaration, which
735 // have to be treated differently. If we have 'struct foo {...' or
Douglas Gregor39a8de12009-02-25 19:37:18 +0000736 // 'struct foo :...' then this is a definition. Otherwise we have
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000737 // something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000738 // However, in some contexts, things look like declarations but are just
739 // references, e.g.
740 // new struct s;
741 // or
742 // &T::operator struct s;
743 // For these, SuppressDeclarations is true.
John McCall0f434ec2009-07-31 02:45:11 +0000744 Action::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000745 if (SuppressDeclarations)
746 TUK = Action::TUK_Reference;
747 else if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))){
Douglas Gregord85bea22009-09-26 06:47:28 +0000748 if (DS.isFriendSpecified()) {
749 // C++ [class.friend]p2:
750 // A class shall not be defined in a friend declaration.
751 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
752 << SourceRange(DS.getFriendSpecLoc());
753
754 // Skip everything up to the semicolon, so that this looks like a proper
755 // friend class (or template thereof) declaration.
756 SkipUntil(tok::semi, true, true);
757 TUK = Action::TUK_Friend;
758 } else {
759 // Okay, this is a class definition.
760 TUK = Action::TUK_Definition;
761 }
762 } else if (Tok.is(tok::semi))
John McCall67d1a672009-08-06 02:15:43 +0000763 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000764 else
John McCall0f434ec2009-07-31 02:45:11 +0000765 TUK = Action::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000766
John McCall0f434ec2009-07-31 02:45:11 +0000767 if (!Name && !TemplateId && TUK != Action::TUK_Definition) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000768 // We have a declaration or reference to an anonymous class.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000769 Diag(StartLoc, diag::err_anon_type_definition)
770 << DeclSpec::getSpecifierName(TagType);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000771
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000772 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000773
774 if (TemplateId)
775 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000776 return;
777 }
778
Douglas Gregorddc29e12009-02-06 22:42:48 +0000779 // Create the tag portion of the class or class template.
John McCallc4e70192009-09-11 04:59:25 +0000780 Action::DeclResult TagOrTempResult = true; // invalid
781 Action::TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000782
Douglas Gregor402abb52009-05-28 23:31:59 +0000783 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000784 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000785 // Explicit specialization, class template partial specialization,
786 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000787 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000788 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000789 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000790 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000791 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000792 // This is an explicit instantiation of a class template.
793 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000794 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000795 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000796 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000797 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000798 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000799 SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000800 TemplateTy::make(TemplateId->Template),
801 TemplateId->TemplateNameLoc,
802 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000803 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000804 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000805 AttrList);
John McCall74256f52010-04-14 00:24:33 +0000806
807 // Friend template-ids are treated as references unless
808 // they have template headers, in which case they're ill-formed
809 // (FIXME: "template <class T> friend class A<T>::B<int>;").
810 // We diagnose this error in ActOnClassTemplateSpecialization.
811 } else if (TUK == Action::TUK_Reference ||
812 (TUK == Action::TUK_Friend &&
813 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
John McCallc4e70192009-09-11 04:59:25 +0000814 TypeResult
John McCall6b2becf2009-09-08 17:47:29 +0000815 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
816 TemplateId->TemplateNameLoc,
817 TemplateId->LAngleLoc,
818 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000819 TemplateId->RAngleLoc);
820
John McCallc4e70192009-09-11 04:59:25 +0000821 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
822 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000823 } else {
824 // This is an explicit specialization or a class template
825 // partial specialization.
826 TemplateParameterLists FakedParamLists;
827
828 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
829 // This looks like an explicit instantiation, because we have
830 // something like
831 //
832 // template class Foo<X>
833 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000834 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000835 // meant to be an explicit specialization, but the user forgot
836 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000837 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000838
Mike Stump1eb44332009-09-09 15:08:12 +0000839 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000840 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000841 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000842 diag::err_explicit_instantiation_with_definition)
843 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +0000844 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000845
846 // Create a fake template parameter list that contains only
847 // "template<>", so that we treat this construct as a class
848 // template specialization.
849 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000850 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000851 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000852 LAngleLoc,
853 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000854 LAngleLoc));
855 TemplateParams = &FakedParamLists;
856 }
857
858 // Build the class template specialization.
859 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000860 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000861 StartLoc, SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000862 TemplateTy::make(TemplateId->Template),
863 TemplateId->TemplateNameLoc,
864 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000865 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000866 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000867 AttrList,
Mike Stump1eb44332009-09-09 15:08:12 +0000868 Action::MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000869 TemplateParams? &(*TemplateParams)[0] : 0,
870 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000871 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000872 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000873 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000874 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000875 // Explicit instantiation of a member of a class template
876 // specialization, e.g.,
877 //
878 // template struct Outer<int>::Inner;
879 //
880 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000881 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000882 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000883 TemplateInfo.TemplateLoc,
884 TagType, StartLoc, SS, Name,
Sean Huntbbd37c62009-11-21 08:43:09 +0000885 NameLoc, AttrList);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000886 } else {
887 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000888 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000889 // FIXME: Diagnose this particular error.
890 }
891
John McCallc4e70192009-09-11 04:59:25 +0000892 bool IsDependent = false;
893
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000894 // Declaration or definition of a class type
Mike Stump1eb44332009-09-09 15:08:12 +0000895 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000896 Name, NameLoc, AttrList, AS,
Mike Stump1eb44332009-09-09 15:08:12 +0000897 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000898 TemplateParams? &(*TemplateParams)[0] : 0,
899 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000900 Owned, IsDependent);
901
902 // If ActOnTag said the type was dependent, try again with the
903 // less common call.
904 if (IsDependent)
905 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000906 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000907 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000908
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000909 // If there is a body, parse it and inform the actions module.
John McCallbd0dfa52009-12-19 21:48:58 +0000910 if (TUK == Action::TUK_Definition) {
911 assert(Tok.is(tok::l_brace) ||
912 (getLang().CPlusPlus && Tok.is(tok::colon)));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000913 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000914 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000915 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000916 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000917 }
918
John McCallc4e70192009-09-11 04:59:25 +0000919 void *Result;
920 if (!TypeResult.isInvalid()) {
921 TagType = DeclSpec::TST_typename;
922 Result = TypeResult.get();
923 Owned = false;
924 } else if (!TagOrTempResult.isInvalid()) {
925 Result = TagOrTempResult.get().getAs<void>();
926 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000927 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000928 return;
929 }
Mike Stump1eb44332009-09-09 15:08:12 +0000930
John McCallfec54012009-08-03 20:12:06 +0000931 const char *PrevSpec = 0;
932 unsigned DiagID;
John McCallc4e70192009-09-11 04:59:25 +0000933
Douglas Gregorb988f9c2010-01-25 16:33:23 +0000934 // FIXME: The DeclSpec should keep the locations of both the keyword and the
935 // name (if there is one).
936 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000937
Douglas Gregorb988f9c2010-01-25 16:33:23 +0000938 if (DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID,
John McCallc4e70192009-09-11 04:59:25 +0000939 Result, Owned))
John McCallfec54012009-08-03 20:12:06 +0000940 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000941
Chris Lattner4ed5d912010-02-02 01:23:29 +0000942 // At this point, we've successfully parsed a class-specifier in 'definition'
943 // form (e.g. "struct foo { int x; }". While we could just return here, we're
944 // going to look at what comes after it to improve error recovery. If an
945 // impossible token occurs next, we assume that the programmer forgot a ; at
946 // the end of the declaration and recover that way.
947 //
948 // This switch enumerates the valid "follow" set for definition.
949 if (TUK == Action::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000950 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000951 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000952 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000953 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +0000954 case tok::star: // struct foo {...} * P;
955 case tok::amp: // struct foo {...} & R = ...
956 case tok::identifier: // struct foo {...} V ;
957 case tok::r_paren: //(struct foo {...} ) {4}
958 case tok::annot_cxxscope: // struct foo {...} a:: b;
959 case tok::annot_typename: // struct foo {...} a ::b;
960 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +0000961 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +0000962 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000963 ExpectedSemi = false;
964 break;
965 // Type qualifiers
966 case tok::kw_const: // struct foo {...} const x;
967 case tok::kw_volatile: // struct foo {...} volatile x;
968 case tok::kw_restrict: // struct foo {...} restrict x;
969 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +0000970 // Storage-class specifiers
971 case tok::kw_static: // struct foo {...} static x;
972 case tok::kw_extern: // struct foo {...} extern x;
973 case tok::kw_typedef: // struct foo {...} typedef x;
974 case tok::kw_register: // struct foo {...} register x;
975 case tok::kw_auto: // struct foo {...} auto x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000976 // As shown above, type qualifiers and storage class specifiers absolutely
977 // can occur after class specifiers according to the grammar. However,
978 // almost noone actually writes code like this. If we see one of these,
979 // it is much more likely that someone missed a semi colon and the
980 // type/storage class specifier we're seeing is part of the *next*
981 // intended declaration, as in:
982 //
983 // struct foo { ... }
984 // typedef int X;
985 //
986 // We'd really like to emit a missing semicolon error instead of emitting
987 // an error on the 'int' saying that you can't have two type specifiers in
988 // the same declaration of X. Because of this, we look ahead past this
989 // token to see if it's a type specifier. If so, we know the code is
990 // otherwise invalid, so we can produce the expected semi error.
991 if (!isKnownToBeTypeSpecifier(NextToken()))
992 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000993 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000994
995 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +0000996 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000997 if (!getLang().CPlusPlus)
998 ExpectedSemi = false;
999 break;
1000 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001001
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001002 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001003 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1004 TagType == DeclSpec::TST_class ? "class"
1005 : TagType == DeclSpec::TST_struct? "struct" : "union");
1006 // Push this token back into the preprocessor and change our current token
1007 // to ';' so that the rest of the code recovers as though there were an
1008 // ';' after the definition.
1009 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001010 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001011 }
1012 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001013}
1014
Mike Stump1eb44332009-09-09 15:08:12 +00001015/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001016///
1017/// base-clause : [C++ class.derived]
1018/// ':' base-specifier-list
1019/// base-specifier-list:
1020/// base-specifier '...'[opt]
1021/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +00001022void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001023 assert(Tok.is(tok::colon) && "Not a base clause");
1024 ConsumeToken();
1025
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001026 // Build up an array of parsed base specifiers.
1027 llvm::SmallVector<BaseTy *, 8> BaseInfo;
1028
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001029 while (true) {
1030 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001031 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001032 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001033 // Skip the rest of this base specifier, up until the comma or
1034 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001035 SkipUntil(tok::comma, tok::l_brace, true, true);
1036 } else {
1037 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001038 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001039 }
1040
1041 // If the next token is a comma, consume it and keep reading
1042 // base-specifiers.
1043 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001045 // Consume the comma.
1046 ConsumeToken();
1047 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001048
1049 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001050 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001051}
1052
1053/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1054/// one entry in the base class list of a class specifier, for example:
1055/// class foo : public bar, virtual private baz {
1056/// 'public bar' and 'virtual private baz' are each base-specifiers.
1057///
1058/// base-specifier: [C++ class.derived]
1059/// ::[opt] nested-name-specifier[opt] class-name
1060/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1061/// class-name
1062/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1063/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +00001064Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001065 bool IsVirtual = false;
1066 SourceLocation StartLoc = Tok.getLocation();
1067
1068 // Parse the 'virtual' keyword.
1069 if (Tok.is(tok::kw_virtual)) {
1070 ConsumeToken();
1071 IsVirtual = true;
1072 }
1073
1074 // Parse an (optional) access specifier.
1075 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001076 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001077 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001079 // Parse the 'virtual' keyword (again!), in case it came after the
1080 // access specifier.
1081 if (Tok.is(tok::kw_virtual)) {
1082 SourceLocation VirtualLoc = ConsumeToken();
1083 if (IsVirtual) {
1084 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001085 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001086 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001087 }
1088
1089 IsVirtual = true;
1090 }
1091
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001092 // Parse optional '::' and optional nested-name-specifier.
1093 CXXScopeSpec SS;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001094 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0,
Douglas Gregord2b43bf2010-03-02 00:25:00 +00001095 /*EnteringContext=*/false);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001096
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001097 // The location of the base class itself.
1098 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001099
1100 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001101 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +00001102 TypeResult BaseType = ParseClassName(EndLocation, &SS);
1103 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001104 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001105
1106 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001107 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001108
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001109 // Notify semantic analysis that we have parsed a complete
1110 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001111 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +00001112 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001113}
1114
1115/// getAccessSpecifierIfPresent - Determine whether the next token is
1116/// a C++ access-specifier.
1117///
1118/// access-specifier: [C++ class.derived]
1119/// 'private'
1120/// 'protected'
1121/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001122AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001123 switch (Tok.getKind()) {
1124 default: return AS_none;
1125 case tok::kw_private: return AS_private;
1126 case tok::kw_protected: return AS_protected;
1127 case tok::kw_public: return AS_public;
1128 }
1129}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001130
Eli Friedmand33133c2009-07-22 21:45:50 +00001131void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
1132 DeclPtrTy ThisDecl) {
1133 // We just declared a member function. If this member function
1134 // has any default arguments, we'll need to parse them later.
1135 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001136 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +00001137 = DeclaratorInfo.getTypeObject(0).Fun;
1138 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1139 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1140 if (!LateMethod) {
1141 // Push this method onto the stack of late-parsed method
1142 // declarations.
1143 getCurrentClass().MethodDecls.push_back(
1144 LateParsedMethodDeclaration(ThisDecl));
1145 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +00001146 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001147
1148 // Add all of the parameters prior to this one (they don't
1149 // have default arguments).
1150 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1151 for (unsigned I = 0; I < ParamIdx; ++I)
1152 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001153 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001154 }
1155
1156 // Add this parameter to the list of parameters (it or may
1157 // not have a default argument).
1158 LateMethod->DefaultArgs.push_back(
1159 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1160 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1161 }
1162 }
1163}
1164
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001165/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1166///
1167/// member-declaration:
1168/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1169/// function-definition ';'[opt]
1170/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1171/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001172/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001173/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001174/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001175///
1176/// member-declarator-list:
1177/// member-declarator
1178/// member-declarator-list ',' member-declarator
1179///
1180/// member-declarator:
1181/// declarator pure-specifier[opt]
1182/// declarator constant-initializer[opt]
1183/// identifier[opt] ':' constant-expression
1184///
Sebastian Redle2b68332009-04-12 17:16:29 +00001185/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001186/// '= 0'
1187///
1188/// constant-initializer:
1189/// '=' constant-expression
1190///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001191void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
1192 const ParsedTemplateInfo &TemplateInfo) {
John McCall60fa3cf2009-12-11 02:10:03 +00001193 // Access declarations.
1194 if (!TemplateInfo.Kind &&
1195 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001196 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001197 Tok.is(tok::annot_cxxscope)) {
1198 bool isAccessDecl = false;
1199 if (NextToken().is(tok::identifier))
1200 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1201 else
1202 isAccessDecl = NextToken().is(tok::kw_operator);
1203
1204 if (isAccessDecl) {
1205 // Collect the scope specifier token we annotated earlier.
1206 CXXScopeSpec SS;
1207 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType*/ 0, false);
1208
1209 // Try to parse an unqualified-id.
1210 UnqualifiedId Name;
1211 if (ParseUnqualifiedId(SS, false, true, true, /*ObjectType*/ 0, Name)) {
1212 SkipUntil(tok::semi);
1213 return;
1214 }
1215
1216 // TODO: recover from mistakenly-qualified operator declarations.
1217 if (ExpectAndConsume(tok::semi,
1218 diag::err_expected_semi_after,
1219 "access declaration",
1220 tok::semi))
1221 return;
1222
1223 Actions.ActOnUsingDeclaration(CurScope, AS,
1224 false, SourceLocation(),
1225 SS, Name,
1226 /* AttrList */ 0,
1227 /* IsTypeName */ false,
1228 SourceLocation());
1229 return;
1230 }
1231 }
1232
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001233 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001234 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001235 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001236 SourceLocation DeclEnd;
1237 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001238 return;
1239 }
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Chris Lattner682bf922009-03-29 16:50:03 +00001241 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001242 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001243 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001244 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001245 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001246 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001247 return;
1248 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001249
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001250 // Handle: member-declaration ::= '__extension__' member-declaration
1251 if (Tok.is(tok::kw___extension__)) {
1252 // __extension__ silences extension warnings in the subexpression.
1253 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1254 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +00001255 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001256 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001257
Chris Lattner4ed5d912010-02-02 01:23:29 +00001258 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1259 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001260 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001261
Sean Huntbbd37c62009-11-21 08:43:09 +00001262 CXX0XAttributeList AttrList;
1263 // Optional C++0x attribute-specifier
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001264 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Sean Huntbbd37c62009-11-21 08:43:09 +00001265 AttrList = ParseCXX0XAttributes();
Sean Huntbbd37c62009-11-21 08:43:09 +00001266
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001267 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001268 // FIXME: Check for template aliases
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001269
Sean Huntbbd37c62009-11-21 08:43:09 +00001270 if (AttrList.HasAttr)
1271 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1272 << AttrList.Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001274 // Eat 'using'.
1275 SourceLocation UsingLoc = ConsumeToken();
1276
1277 if (Tok.is(tok::kw_namespace)) {
1278 Diag(UsingLoc, diag::err_using_namespace_in_class);
1279 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001280 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001281 SourceLocation DeclEnd;
1282 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001283 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001284 }
1285 return;
1286 }
1287
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001288 SourceLocation DSStart = Tok.getLocation();
1289 // decl-specifier-seq:
1290 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001291 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +00001292 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001293 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001294
John McCalldd4a3b02009-09-16 22:47:08 +00001295 Action::MultiTemplateParamsArg TemplateParams(Actions,
1296 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1297 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1298
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001299 if (Tok.is(tok::semi)) {
1300 ConsumeToken();
Douglas Gregord85bea22009-09-26 06:47:28 +00001301 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +00001302 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001303 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001304
John McCall54abf7d2009-11-04 02:18:39 +00001305 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001306
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001307 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001308 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1309 ColonProtectionRAIIObject X(*this);
1310
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001311 // Parse the first declarator.
1312 ParseDeclarator(DeclaratorInfo);
1313 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001314 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001315 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001316 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001317 if (Tok.is(tok::semi))
1318 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001319 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001320 }
1321
John Thompson1b2fc0f2009-11-25 22:58:06 +00001322 // If attributes exist after the declarator, but before an '{', parse them.
1323 if (Tok.is(tok::kw___attribute)) {
1324 SourceLocation Loc;
1325 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1326 DeclaratorInfo.AddAttributes(AttrList, Loc);
1327 }
1328
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001329 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001330 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001331 || (DeclaratorInfo.isFunctionDeclarator() &&
1332 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001333 if (!DeclaratorInfo.isFunctionDeclarator()) {
1334 Diag(Tok, diag::err_func_def_no_params);
1335 ConsumeBrace();
1336 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001337 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001338 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001339
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001340 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1341 Diag(Tok, diag::err_function_declared_typedef);
1342 // This recovery skips the entire function body. It would be nice
1343 // to simply call ParseCXXInlineMethodDef() below, however Sema
1344 // assumes the declarator represents a function, not a typedef.
1345 ConsumeBrace();
1346 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001347 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001348 }
1349
Douglas Gregor37b372b2009-08-20 22:52:58 +00001350 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001351 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001352 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001353 }
1354
1355 // member-declarator-list:
1356 // member-declarator
1357 // member-declarator-list ',' member-declarator
1358
Chris Lattner682bf922009-03-29 16:50:03 +00001359 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001360 OwningExprResult BitfieldSize(Actions);
1361 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001362 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001363
1364 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001365 // member-declarator:
1366 // declarator pure-specifier[opt]
1367 // declarator constant-initializer[opt]
1368 // identifier[opt] ':' constant-expression
1369
1370 if (Tok.is(tok::colon)) {
1371 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001372 BitfieldSize = ParseConstantExpression();
1373 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001374 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001375 }
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001377 // pure-specifier:
1378 // '= 0'
1379 //
1380 // constant-initializer:
1381 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001382 //
1383 // defaulted/deleted function-definition:
1384 // '=' 'default' [TODO]
1385 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001386
1387 if (Tok.is(tok::equal)) {
1388 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001389 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1390 ConsumeToken();
1391 Deleted = true;
1392 } else {
1393 Init = ParseInitializer();
1394 if (Init.isInvalid())
1395 SkipUntil(tok::comma, true, true);
1396 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001397 }
1398
1399 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001400 if (Tok.is(tok::kw___attribute)) {
1401 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001402 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001403 DeclaratorInfo.AddAttributes(AttrList, Loc);
1404 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001405
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001406 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001407 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001408 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001409
1410 DeclPtrTy ThisDecl;
1411 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001412 // TODO: handle initializers, bitfields, 'delete'
1413 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1414 /*IsDefinition*/ false,
1415 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001416 } else {
John McCall67d1a672009-08-06 02:15:43 +00001417 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1418 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001419 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001420 BitfieldSize.release(),
1421 Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001422 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001423 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001424 }
Chris Lattner682bf922009-03-29 16:50:03 +00001425 if (ThisDecl)
1426 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001427
Douglas Gregor72b505b2008-12-16 21:30:33 +00001428 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001429 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001430 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001431 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001432 }
1433
John McCall54abf7d2009-11-04 02:18:39 +00001434 DeclaratorInfo.complete(ThisDecl);
1435
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001436 // If we don't have a comma, it is either the end of the list (a ';')
1437 // or an error, bail out.
1438 if (Tok.isNot(tok::comma))
1439 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001441 // Consume the comma.
1442 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001444 // Parse the next declarator.
1445 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001446 BitfieldSize = 0;
1447 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001448 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001450 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001451 if (Tok.is(tok::kw___attribute)) {
1452 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001453 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001454 DeclaratorInfo.AddAttributes(AttrList, Loc);
1455 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001456
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001457 if (Tok.isNot(tok::colon))
1458 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001459 }
1460
Chris Lattnerae50d502010-02-02 00:43:15 +00001461 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1462 // Skip to end of block or statement.
1463 SkipUntil(tok::r_brace, true, true);
1464 // If we stopped at a ';', eat it.
1465 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001466 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001467 }
1468
Chris Lattnerae50d502010-02-02 00:43:15 +00001469 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
1470 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001471}
1472
1473/// ParseCXXMemberSpecification - Parse the class definition.
1474///
1475/// member-specification:
1476/// member-declaration member-specification[opt]
1477/// access-specifier ':' member-specification[opt]
1478///
1479void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001480 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001481 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001482 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001483 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001484
Chris Lattner49f28ca2009-03-05 08:00:35 +00001485 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1486 PP.getSourceManager(),
1487 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Douglas Gregor26997fd2010-01-16 20:52:59 +00001489 // Determine whether this is a non-nested class. Note that local
1490 // classes are *not* considered to be nested classes.
1491 bool NonNestedClass = true;
1492 if (!ClassStack.empty()) {
1493 for (const Scope *S = CurScope; S; S = S->getParent()) {
1494 if (S->isClassScope()) {
1495 // We're inside a class scope, so this is a nested class.
1496 NonNestedClass = false;
1497 break;
1498 }
1499
1500 if ((S->getFlags() & Scope::FnScope)) {
1501 // If we're in a function or function template declared in the
1502 // body of a class, then this is a local class rather than a
1503 // nested class.
1504 const Scope *Parent = S->getParent();
1505 if (Parent->isTemplateParamScope())
1506 Parent = Parent->getParent();
1507 if (Parent->isClassScope())
1508 break;
1509 }
1510 }
1511 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001512
1513 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001514 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001515
Douglas Gregor6569d682009-05-27 23:11:45 +00001516 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001517 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001518
Douglas Gregorddc29e12009-02-06 22:42:48 +00001519 if (TagDecl)
1520 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001521
1522 if (Tok.is(tok::colon)) {
1523 ParseBaseClause(TagDecl);
1524
1525 if (!Tok.is(tok::l_brace)) {
1526 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00001527
1528 if (TagDecl)
1529 Actions.ActOnTagDefinitionError(CurScope, TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001530 return;
1531 }
1532 }
1533
1534 assert(Tok.is(tok::l_brace));
1535
1536 SourceLocation LBraceLoc = ConsumeBrace();
1537
1538 if (!TagDecl) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001539 SkipUntil(tok::r_brace, false, false);
1540 return;
1541 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001542
John McCallf9368152009-12-20 07:58:13 +00001543 Actions.ActOnStartCXXMemberDeclarations(CurScope, TagDecl, LBraceLoc);
1544
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001545 // C++ 11p3: Members of a class defined with the keyword class are private
1546 // by default. Members of a class defined with the keywords struct or union
1547 // are public by default.
1548 AccessSpecifier CurAS;
1549 if (TagType == DeclSpec::TST_class)
1550 CurAS = AS_private;
1551 else
1552 CurAS = AS_public;
1553
1554 // While we still have something to read, read the member-declarations.
1555 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1556 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001558 // Check for extraneous top-level semicolon.
1559 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +00001560 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001561 << FixItHint::CreateRemoval(Tok.getLocation());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001562 ConsumeToken();
1563 continue;
1564 }
1565
1566 AccessSpecifier AS = getAccessSpecifierIfPresent();
1567 if (AS != AS_none) {
1568 // Current token is a C++ access specifier.
1569 CurAS = AS;
1570 ConsumeToken();
1571 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1572 continue;
1573 }
1574
Douglas Gregor37b372b2009-08-20 22:52:58 +00001575 // FIXME: Make sure we don't have a template here.
Mike Stump1eb44332009-09-09 15:08:12 +00001576
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001577 // Parse all the comma separated declarators.
1578 ParseCXXClassMemberDeclaration(CurAS);
1579 }
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001581 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001583 // If attributes exist after class contents, parse them.
Ted Kremenek1e377652010-02-11 02:19:13 +00001584 llvm::OwningPtr<AttributeList> AttrList;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001585 if (Tok.is(tok::kw___attribute))
Douglas Gregor0b4c9b52010-03-29 14:42:08 +00001586 AttrList.reset(ParseGNUAttributes());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001587
1588 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
Douglas Gregor0b4c9b52010-03-29 14:42:08 +00001589 LBraceLoc, RBraceLoc,
1590 AttrList.get());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001591
1592 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1593 // complete within function bodies, default arguments,
1594 // exception-specifications, and constructor ctor-initializers (including
1595 // such things in nested classes).
1596 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001597 // FIXME: Only function bodies and constructor ctor-initializers are
1598 // parsed correctly, fix the rest.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001599 if (NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001600 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001601 // are complete and we can parse the delayed portions of method
1602 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001603 ParseLexedMethodDeclarations(getCurrentClass());
1604 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001605 }
1606
John McCalldb7bb4a2010-03-17 00:38:33 +00001607 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
1608
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001609 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001610 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001611 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001612}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001613
1614/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1615/// which explicitly initializes the members or base classes of a
1616/// class (C++ [class.base.init]). For example, the three initializers
1617/// after the ':' in the Derived constructor below:
1618///
1619/// @code
1620/// class Base { };
1621/// class Derived : Base {
1622/// int x;
1623/// float f;
1624/// public:
1625/// Derived(float f) : Base(), x(17), f(f) { }
1626/// };
1627/// @endcode
1628///
Mike Stump1eb44332009-09-09 15:08:12 +00001629/// [C++] ctor-initializer:
1630/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001631///
Mike Stump1eb44332009-09-09 15:08:12 +00001632/// [C++] mem-initializer-list:
1633/// mem-initializer
1634/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001635void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001636 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1637
1638 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Douglas Gregor7ad83902008-11-05 04:29:56 +00001640 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001641 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001642
Douglas Gregor7ad83902008-11-05 04:29:56 +00001643 do {
1644 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001645 if (!MemInit.isInvalid())
1646 MemInitializers.push_back(MemInit.get());
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001647 else
1648 AnyErrors = true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001649
Douglas Gregor7ad83902008-11-05 04:29:56 +00001650 if (Tok.is(tok::comma))
1651 ConsumeToken();
1652 else if (Tok.is(tok::l_brace))
1653 break;
1654 else {
1655 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001656 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001657 SkipUntil(tok::l_brace, true, true);
1658 break;
1659 }
1660 } while (true);
1661
Mike Stump1eb44332009-09-09 15:08:12 +00001662 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001663 MemInitializers.data(), MemInitializers.size(),
1664 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001665}
1666
1667/// ParseMemInitializer - Parse a C++ member initializer, which is
1668/// part of a constructor initializer that explicitly initializes one
1669/// member or base class (C++ [class.base.init]). See
1670/// ParseConstructorInitializer for an example.
1671///
1672/// [C++] mem-initializer:
1673/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001674///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001675/// [C++] mem-initializer-id:
1676/// '::'[opt] nested-name-specifier[opt] class-name
1677/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001678Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001679 // parse '::'[opt] nested-name-specifier[opt]
1680 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001681 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001682 TypeTy *TemplateTypeTy = 0;
1683 if (Tok.is(tok::annot_template_id)) {
1684 TemplateIdAnnotation *TemplateId
1685 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001686 if (TemplateId->Kind == TNK_Type_template ||
1687 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanian96174332009-07-01 19:21:19 +00001688 AnnotateTemplateIdTokenAsType(&SS);
1689 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1690 TemplateTypeTy = Tok.getAnnotationValue();
1691 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001692 }
1693 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001694 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001695 return true;
1696 }
Mike Stump1eb44332009-09-09 15:08:12 +00001697
Douglas Gregor7ad83902008-11-05 04:29:56 +00001698 // Get the identifier. This may be a member name or a class name,
1699 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001700 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001701 SourceLocation IdLoc = ConsumeToken();
1702
1703 // Parse the '('.
1704 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001705 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001706 return true;
1707 }
1708 SourceLocation LParenLoc = ConsumeParen();
1709
1710 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001711 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001712 CommaLocsTy CommaLocs;
1713 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1714 SkipUntil(tok::r_paren);
1715 return true;
1716 }
1717
1718 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1719
Fariborz Jahanian96174332009-07-01 19:21:19 +00001720 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1721 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001722 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001723 ArgExprs.size(), CommaLocs.data(),
1724 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001725}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001726
1727/// ParseExceptionSpecification - Parse a C++ exception-specification
1728/// (C++ [except.spec]).
1729///
Douglas Gregora4745612008-12-01 18:00:20 +00001730/// exception-specification:
1731/// 'throw' '(' type-id-list [opt] ')'
1732/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001733///
Douglas Gregora4745612008-12-01 18:00:20 +00001734/// type-id-list:
1735/// type-id
1736/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001737///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001738bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001739 llvm::SmallVector<TypeTy*, 2>
1740 &Exceptions,
1741 llvm::SmallVector<SourceRange, 2>
1742 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001743 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001744 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001745
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001746 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001747
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001748 if (!Tok.is(tok::l_paren)) {
1749 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1750 }
1751 SourceLocation LParenLoc = ConsumeParen();
1752
Douglas Gregora4745612008-12-01 18:00:20 +00001753 // Parse throw(...), a Microsoft extension that means "this function
1754 // can throw anything".
1755 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001756 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001757 SourceLocation EllipsisLoc = ConsumeToken();
1758 if (!getLang().Microsoft)
1759 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001760 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001761 return false;
1762 }
1763
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001764 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001765 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001766 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001767 TypeResult Res(ParseTypeName(&Range));
1768 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001769 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001770 Ranges.push_back(Range);
1771 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001772 if (Tok.is(tok::comma))
1773 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001774 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001775 break;
1776 }
1777
Sebastian Redlab197ba2009-02-09 18:23:29 +00001778 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001779 return false;
1780}
Douglas Gregor6569d682009-05-27 23:11:45 +00001781
1782/// \brief We have just started parsing the definition of a new class,
1783/// so push that class onto our stack of classes that is currently
1784/// being parsed.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001785void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool NonNestedClass) {
1786 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001787 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00001788 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
Douglas Gregor6569d682009-05-27 23:11:45 +00001789}
1790
1791/// \brief Deallocate the given parsed class and all of its nested
1792/// classes.
1793void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1794 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1795 DeallocateParsedClasses(Class->NestedClasses[I]);
1796 delete Class;
1797}
1798
1799/// \brief Pop the top class of the stack of classes that are
1800/// currently being parsed.
1801///
1802/// This routine should be called when we have finished parsing the
1803/// definition of a class, but have not yet popped the Scope
1804/// associated with the class's definition.
1805///
1806/// \returns true if the class we've popped is a top-level class,
1807/// false otherwise.
1808void Parser::PopParsingClass() {
1809 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Douglas Gregor6569d682009-05-27 23:11:45 +00001811 ParsingClass *Victim = ClassStack.top();
1812 ClassStack.pop();
1813 if (Victim->TopLevelClass) {
1814 // Deallocate all of the nested classes of this class,
1815 // recursively: we don't need to keep any of this information.
1816 DeallocateParsedClasses(Victim);
1817 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001818 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001819 assert(!ClassStack.empty() && "Missing top-level class?");
1820
1821 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1822 Victim->NestedClasses.empty()) {
1823 // The victim is a nested class, but we will not need to perform
1824 // any processing after the definition of this class since it has
1825 // no members whose handling was delayed. Therefore, we can just
1826 // remove this nested class.
1827 delete Victim;
1828 return;
1829 }
1830
1831 // This nested class has some members that will need to be processed
1832 // after the top-level class is completely defined. Therefore, add
1833 // it to the list of nested classes within its parent.
1834 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1835 ClassStack.top()->NestedClasses.push_back(Victim);
1836 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1837}
Sean Huntbbd37c62009-11-21 08:43:09 +00001838
1839/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1840/// parses standard attributes.
1841///
1842/// [C++0x] attribute-specifier:
1843/// '[' '[' attribute-list ']' ']'
1844///
1845/// [C++0x] attribute-list:
1846/// attribute[opt]
1847/// attribute-list ',' attribute[opt]
1848///
1849/// [C++0x] attribute:
1850/// attribute-token attribute-argument-clause[opt]
1851///
1852/// [C++0x] attribute-token:
1853/// identifier
1854/// attribute-scoped-token
1855///
1856/// [C++0x] attribute-scoped-token:
1857/// attribute-namespace '::' identifier
1858///
1859/// [C++0x] attribute-namespace:
1860/// identifier
1861///
1862/// [C++0x] attribute-argument-clause:
1863/// '(' balanced-token-seq ')'
1864///
1865/// [C++0x] balanced-token-seq:
1866/// balanced-token
1867/// balanced-token-seq balanced-token
1868///
1869/// [C++0x] balanced-token:
1870/// '(' balanced-token-seq ')'
1871/// '[' balanced-token-seq ']'
1872/// '{' balanced-token-seq '}'
1873/// any token but '(', ')', '[', ']', '{', or '}'
1874CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1875 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1876 && "Not a C++0x attribute list");
1877
1878 SourceLocation StartLoc = Tok.getLocation(), Loc;
1879 AttributeList *CurrAttr = 0;
1880
1881 ConsumeBracket();
1882 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001883
Sean Huntbbd37c62009-11-21 08:43:09 +00001884 if (Tok.is(tok::comma)) {
1885 Diag(Tok.getLocation(), diag::err_expected_ident);
1886 ConsumeToken();
1887 }
1888
1889 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1890 // attribute not present
1891 if (Tok.is(tok::comma)) {
1892 ConsumeToken();
1893 continue;
1894 }
1895
1896 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1897 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001898
Sean Huntbbd37c62009-11-21 08:43:09 +00001899 // scoped attribute
1900 if (Tok.is(tok::coloncolon)) {
1901 ConsumeToken();
1902
1903 if (!Tok.is(tok::identifier)) {
1904 Diag(Tok.getLocation(), diag::err_expected_ident);
1905 SkipUntil(tok::r_square, tok::comma, true, true);
1906 continue;
1907 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001908
Sean Huntbbd37c62009-11-21 08:43:09 +00001909 ScopeName = AttrName;
1910 ScopeLoc = AttrLoc;
1911
1912 AttrName = Tok.getIdentifierInfo();
1913 AttrLoc = ConsumeToken();
1914 }
1915
1916 bool AttrParsed = false;
1917 // No scoped names are supported; ideally we could put all non-standard
1918 // attributes into namespaces.
1919 if (!ScopeName) {
1920 switch(AttributeList::getKind(AttrName))
1921 {
1922 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00001923 case AttributeList::AT_base_check:
1924 case AttributeList::AT_carries_dependency:
Sean Huntbbd37c62009-11-21 08:43:09 +00001925 case AttributeList::AT_final:
Sean Hunt7725e672009-11-25 04:20:27 +00001926 case AttributeList::AT_hiding:
1927 case AttributeList::AT_noreturn:
1928 case AttributeList::AT_override: {
Sean Huntbbd37c62009-11-21 08:43:09 +00001929 if (Tok.is(tok::l_paren)) {
1930 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
1931 << AttrName->getName();
1932 break;
1933 }
1934
1935 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
1936 SourceLocation(), 0, 0, CurrAttr, false,
1937 true);
1938 AttrParsed = true;
1939 break;
1940 }
1941
1942 // One argument; must be a type-id or assignment-expression
1943 case AttributeList::AT_aligned: {
1944 if (Tok.isNot(tok::l_paren)) {
1945 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
1946 << AttrName->getName();
1947 break;
1948 }
1949 SourceLocation ParamLoc = ConsumeParen();
1950
1951 OwningExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
1952
1953 MatchRHSPunctuation(tok::r_paren, ParamLoc);
1954
1955 ExprVector ArgExprs(Actions);
1956 ArgExprs.push_back(ArgExpr.release());
1957 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
1958 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
1959 false, true);
1960
1961 AttrParsed = true;
1962 break;
1963 }
1964
1965 // Silence warnings
1966 default: break;
1967 }
1968 }
1969
1970 // Skip the entire parameter clause, if any
1971 if (!AttrParsed && Tok.is(tok::l_paren)) {
1972 ConsumeParen();
1973 // SkipUntil maintains the balancedness of tokens.
1974 SkipUntil(tok::r_paren, false);
1975 }
1976 }
1977
1978 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1979 SkipUntil(tok::r_square, false);
1980 Loc = Tok.getLocation();
1981 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1982 SkipUntil(tok::r_square, false);
1983
1984 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
1985 return Attr;
1986}
1987
1988/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
1989/// attribute.
1990///
1991/// FIXME: Simply returns an alignof() expression if the argument is a
1992/// type. Ideally, the type should be propagated directly into Sema.
1993///
1994/// [C++0x] 'align' '(' type-id ')'
1995/// [C++0x] 'align' '(' assignment-expression ')'
1996Parser::OwningExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
1997 if (isTypeIdInParens()) {
1998 EnterExpressionEvaluationContext Unevaluated(Actions,
1999 Action::Unevaluated);
2000 SourceLocation TypeLoc = Tok.getLocation();
2001 TypeTy *Ty = ParseTypeName().get();
2002 SourceRange TypeRange(Start, Tok.getLocation());
2003 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, Ty,
2004 TypeRange);
2005 } else
2006 return ParseConstantExpression();
2007}