blob: 813c24ce3d58618d4caba4e7b4fefed22de63edf [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 Kramerddeea562010-02-27 13:44:12 +0000181 Loc, Lang.data(), Lang.size(),
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,
Douglas Gregor124b8782010-02-16 19:09:40 +0000468 const 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
John McCall0f434ec2009-07-31 02:45:11 +0000783 // FIXME: When TUK == TUK_Reference and we have a template-id, we need
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000784 // to turn that template-id into a type.
785
Douglas Gregor402abb52009-05-28 23:31:59 +0000786 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000787 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000788 // Explicit specialization, class template partial specialization,
789 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000790 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000791 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000792 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000793 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000794 TUK == Action::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000795 // This is an explicit instantiation of a class template.
796 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000797 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000798 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000799 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000800 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000801 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000802 SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000803 TemplateTy::make(TemplateId->Template),
804 TemplateId->TemplateNameLoc,
805 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000806 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000807 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000808 AttrList);
Douglas Gregorfc9cd612009-09-26 20:57:03 +0000809 } else if (TUK == Action::TUK_Reference) {
John McCallc4e70192009-09-11 04:59:25 +0000810 TypeResult
John McCall6b2becf2009-09-08 17:47:29 +0000811 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template),
812 TemplateId->TemplateNameLoc,
813 TemplateId->LAngleLoc,
814 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000815 TemplateId->RAngleLoc);
816
John McCallc4e70192009-09-11 04:59:25 +0000817 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK,
818 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000819 } else {
820 // This is an explicit specialization or a class template
821 // partial specialization.
822 TemplateParameterLists FakedParamLists;
823
824 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
825 // This looks like an explicit instantiation, because we have
826 // something like
827 //
828 // template class Foo<X>
829 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000830 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000831 // meant to be an explicit specialization, but the user forgot
832 // the '<>' after 'template'.
John McCall0f434ec2009-07-31 02:45:11 +0000833 assert(TUK == Action::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000834
Mike Stump1eb44332009-09-09 15:08:12 +0000835 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000836 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000837 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000838 diag::err_explicit_instantiation_with_definition)
839 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +0000840 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000841
842 // Create a fake template parameter list that contains only
843 // "template<>", so that we treat this construct as a class
844 // template specialization.
845 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000846 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000847 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000848 LAngleLoc,
849 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000850 LAngleLoc));
851 TemplateParams = &FakedParamLists;
852 }
853
854 // Build the class template specialization.
855 TagOrTempResult
John McCall0f434ec2009-07-31 02:45:11 +0000856 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000857 StartLoc, SS,
Mike Stump1eb44332009-09-09 15:08:12 +0000858 TemplateTy::make(TemplateId->Template),
859 TemplateId->TemplateNameLoc,
860 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000861 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000862 TemplateId->RAngleLoc,
Sean Huntbbd37c62009-11-21 08:43:09 +0000863 AttrList,
Mike Stump1eb44332009-09-09 15:08:12 +0000864 Action::MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000865 TemplateParams? &(*TemplateParams)[0] : 0,
866 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000867 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000868 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000869 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000870 TUK == Action::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000871 // Explicit instantiation of a member of a class template
872 // specialization, e.g.,
873 //
874 // template struct Outer<int>::Inner;
875 //
876 TagOrTempResult
Mike Stump1eb44332009-09-09 15:08:12 +0000877 = Actions.ActOnExplicitInstantiation(CurScope,
Douglas Gregor45f96552009-09-04 06:33:52 +0000878 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000879 TemplateInfo.TemplateLoc,
880 TagType, StartLoc, SS, Name,
Sean Huntbbd37c62009-11-21 08:43:09 +0000881 NameLoc, AttrList);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000882 } else {
883 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCall0f434ec2009-07-31 02:45:11 +0000884 TUK == Action::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000885 // FIXME: Diagnose this particular error.
886 }
887
John McCallc4e70192009-09-11 04:59:25 +0000888 bool IsDependent = false;
889
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000890 // Declaration or definition of a class type
Mike Stump1eb44332009-09-09 15:08:12 +0000891 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS,
Sean Huntbbd37c62009-11-21 08:43:09 +0000892 Name, NameLoc, AttrList, AS,
Mike Stump1eb44332009-09-09 15:08:12 +0000893 Action::MultiTemplateParamsArg(Actions,
Douglas Gregor7cdbc582009-07-22 23:48:44 +0000894 TemplateParams? &(*TemplateParams)[0] : 0,
895 TemplateParams? TemplateParams->size() : 0),
John McCallc4e70192009-09-11 04:59:25 +0000896 Owned, IsDependent);
897
898 // If ActOnTag said the type was dependent, try again with the
899 // less common call.
900 if (IsDependent)
901 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000902 SS, Name, StartLoc, NameLoc);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000903 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000904
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000905 // If there is a body, parse it and inform the actions module.
John McCallbd0dfa52009-12-19 21:48:58 +0000906 if (TUK == Action::TUK_Definition) {
907 assert(Tok.is(tok::l_brace) ||
908 (getLang().CPlusPlus && Tok.is(tok::colon)));
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000909 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +0000910 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +0000911 else
Douglas Gregor212e81c2009-03-25 00:13:59 +0000912 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000913 }
914
John McCallc4e70192009-09-11 04:59:25 +0000915 void *Result;
916 if (!TypeResult.isInvalid()) {
917 TagType = DeclSpec::TST_typename;
918 Result = TypeResult.get();
919 Owned = false;
920 } else if (!TagOrTempResult.isInvalid()) {
921 Result = TagOrTempResult.get().getAs<void>();
922 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000923 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +0000924 return;
925 }
Mike Stump1eb44332009-09-09 15:08:12 +0000926
John McCallfec54012009-08-03 20:12:06 +0000927 const char *PrevSpec = 0;
928 unsigned DiagID;
John McCallc4e70192009-09-11 04:59:25 +0000929
Douglas Gregorb988f9c2010-01-25 16:33:23 +0000930 // FIXME: The DeclSpec should keep the locations of both the keyword and the
931 // name (if there is one).
932 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000933
Douglas Gregorb988f9c2010-01-25 16:33:23 +0000934 if (DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID,
John McCallc4e70192009-09-11 04:59:25 +0000935 Result, Owned))
John McCallfec54012009-08-03 20:12:06 +0000936 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000937
Chris Lattner4ed5d912010-02-02 01:23:29 +0000938 // At this point, we've successfully parsed a class-specifier in 'definition'
939 // form (e.g. "struct foo { int x; }". While we could just return here, we're
940 // going to look at what comes after it to improve error recovery. If an
941 // impossible token occurs next, we assume that the programmer forgot a ; at
942 // the end of the declaration and recover that way.
943 //
944 // This switch enumerates the valid "follow" set for definition.
945 if (TUK == Action::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000946 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000947 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000948 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000949 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +0000950 case tok::star: // struct foo {...} * P;
951 case tok::amp: // struct foo {...} & R = ...
952 case tok::identifier: // struct foo {...} V ;
953 case tok::r_paren: //(struct foo {...} ) {4}
954 case tok::annot_cxxscope: // struct foo {...} a:: b;
955 case tok::annot_typename: // struct foo {...} a ::b;
956 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +0000957 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +0000958 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000959 ExpectedSemi = false;
960 break;
961 // Type qualifiers
962 case tok::kw_const: // struct foo {...} const x;
963 case tok::kw_volatile: // struct foo {...} volatile x;
964 case tok::kw_restrict: // struct foo {...} restrict x;
965 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +0000966 // Storage-class specifiers
967 case tok::kw_static: // struct foo {...} static x;
968 case tok::kw_extern: // struct foo {...} extern x;
969 case tok::kw_typedef: // struct foo {...} typedef x;
970 case tok::kw_register: // struct foo {...} register x;
971 case tok::kw_auto: // struct foo {...} auto x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000972 // As shown above, type qualifiers and storage class specifiers absolutely
973 // can occur after class specifiers according to the grammar. However,
974 // almost noone actually writes code like this. If we see one of these,
975 // it is much more likely that someone missed a semi colon and the
976 // type/storage class specifier we're seeing is part of the *next*
977 // intended declaration, as in:
978 //
979 // struct foo { ... }
980 // typedef int X;
981 //
982 // We'd really like to emit a missing semicolon error instead of emitting
983 // an error on the 'int' saying that you can't have two type specifiers in
984 // the same declaration of X. Because of this, we look ahead past this
985 // token to see if it's a type specifier. If so, we know the code is
986 // otherwise invalid, so we can produce the expected semi error.
987 if (!isKnownToBeTypeSpecifier(NextToken()))
988 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +0000989 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000990
991 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +0000992 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000993 if (!getLang().CPlusPlus)
994 ExpectedSemi = false;
995 break;
996 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000997
Chris Lattnerb3a4e432010-02-28 18:18:36 +0000998 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +0000999 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1000 TagType == DeclSpec::TST_class ? "class"
1001 : TagType == DeclSpec::TST_struct? "struct" : "union");
1002 // Push this token back into the preprocessor and change our current token
1003 // to ';' so that the rest of the code recovers as though there were an
1004 // ';' after the definition.
1005 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001006 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001007 }
1008 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001009}
1010
Mike Stump1eb44332009-09-09 15:08:12 +00001011/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001012///
1013/// base-clause : [C++ class.derived]
1014/// ':' base-specifier-list
1015/// base-specifier-list:
1016/// base-specifier '...'[opt]
1017/// base-specifier-list ',' base-specifier '...'[opt]
Chris Lattnerb28317a2009-03-28 19:18:32 +00001018void Parser::ParseBaseClause(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001019 assert(Tok.is(tok::colon) && "Not a base clause");
1020 ConsumeToken();
1021
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001022 // Build up an array of parsed base specifiers.
1023 llvm::SmallVector<BaseTy *, 8> BaseInfo;
1024
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001025 while (true) {
1026 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001027 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001028 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001029 // Skip the rest of this base specifier, up until the comma or
1030 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001031 SkipUntil(tok::comma, tok::l_brace, true, true);
1032 } else {
1033 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001034 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001035 }
1036
1037 // If the next token is a comma, consume it and keep reading
1038 // base-specifiers.
1039 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001041 // Consume the comma.
1042 ConsumeToken();
1043 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001044
1045 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001046 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001047}
1048
1049/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1050/// one entry in the base class list of a class specifier, for example:
1051/// class foo : public bar, virtual private baz {
1052/// 'public bar' and 'virtual private baz' are each base-specifiers.
1053///
1054/// base-specifier: [C++ class.derived]
1055/// ::[opt] nested-name-specifier[opt] class-name
1056/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1057/// class-name
1058/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1059/// class-name
Chris Lattnerb28317a2009-03-28 19:18:32 +00001060Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001061 bool IsVirtual = false;
1062 SourceLocation StartLoc = Tok.getLocation();
1063
1064 // Parse the 'virtual' keyword.
1065 if (Tok.is(tok::kw_virtual)) {
1066 ConsumeToken();
1067 IsVirtual = true;
1068 }
1069
1070 // Parse an (optional) access specifier.
1071 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001072 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001073 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001075 // Parse the 'virtual' keyword (again!), in case it came after the
1076 // access specifier.
1077 if (Tok.is(tok::kw_virtual)) {
1078 SourceLocation VirtualLoc = ConsumeToken();
1079 if (IsVirtual) {
1080 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001081 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001082 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001083 }
1084
1085 IsVirtual = true;
1086 }
1087
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001088 // Parse optional '::' and optional nested-name-specifier.
1089 CXXScopeSpec SS;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001090 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0,
Douglas Gregord2b43bf2010-03-02 00:25:00 +00001091 /*EnteringContext=*/false);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001092
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001093 // The location of the base class itself.
1094 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001095
1096 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001097 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +00001098 TypeResult BaseType = ParseClassName(EndLocation, &SS);
1099 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001100 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001101
1102 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001103 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001105 // Notify semantic analysis that we have parsed a complete
1106 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001107 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor31a19b62009-04-01 21:51:26 +00001108 BaseType.get(), BaseLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001109}
1110
1111/// getAccessSpecifierIfPresent - Determine whether the next token is
1112/// a C++ access-specifier.
1113///
1114/// access-specifier: [C++ class.derived]
1115/// 'private'
1116/// 'protected'
1117/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001118AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001119 switch (Tok.getKind()) {
1120 default: return AS_none;
1121 case tok::kw_private: return AS_private;
1122 case tok::kw_protected: return AS_protected;
1123 case tok::kw_public: return AS_public;
1124 }
1125}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001126
Eli Friedmand33133c2009-07-22 21:45:50 +00001127void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
1128 DeclPtrTy ThisDecl) {
1129 // We just declared a member function. If this member function
1130 // has any default arguments, we'll need to parse them later.
1131 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001132 DeclaratorChunk::FunctionTypeInfo &FTI
Eli Friedmand33133c2009-07-22 21:45:50 +00001133 = DeclaratorInfo.getTypeObject(0).Fun;
1134 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1135 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1136 if (!LateMethod) {
1137 // Push this method onto the stack of late-parsed method
1138 // declarations.
1139 getCurrentClass().MethodDecls.push_back(
1140 LateParsedMethodDeclaration(ThisDecl));
1141 LateMethod = &getCurrentClass().MethodDecls.back();
Douglas Gregord83d0402009-08-22 00:34:47 +00001142 LateMethod->TemplateScope = CurScope->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001143
1144 // Add all of the parameters prior to this one (they don't
1145 // have default arguments).
1146 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1147 for (unsigned I = 0; I < ParamIdx; ++I)
1148 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001149 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001150 }
1151
1152 // Add this parameter to the list of parameters (it or may
1153 // not have a default argument).
1154 LateMethod->DefaultArgs.push_back(
1155 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1156 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1157 }
1158 }
1159}
1160
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001161/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1162///
1163/// member-declaration:
1164/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1165/// function-definition ';'[opt]
1166/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1167/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001168/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001169/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001170/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001171///
1172/// member-declarator-list:
1173/// member-declarator
1174/// member-declarator-list ',' member-declarator
1175///
1176/// member-declarator:
1177/// declarator pure-specifier[opt]
1178/// declarator constant-initializer[opt]
1179/// identifier[opt] ':' constant-expression
1180///
Sebastian Redle2b68332009-04-12 17:16:29 +00001181/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001182/// '= 0'
1183///
1184/// constant-initializer:
1185/// '=' constant-expression
1186///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001187void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
1188 const ParsedTemplateInfo &TemplateInfo) {
John McCall60fa3cf2009-12-11 02:10:03 +00001189 // Access declarations.
1190 if (!TemplateInfo.Kind &&
1191 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001192 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001193 Tok.is(tok::annot_cxxscope)) {
1194 bool isAccessDecl = false;
1195 if (NextToken().is(tok::identifier))
1196 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1197 else
1198 isAccessDecl = NextToken().is(tok::kw_operator);
1199
1200 if (isAccessDecl) {
1201 // Collect the scope specifier token we annotated earlier.
1202 CXXScopeSpec SS;
1203 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType*/ 0, false);
1204
1205 // Try to parse an unqualified-id.
1206 UnqualifiedId Name;
1207 if (ParseUnqualifiedId(SS, false, true, true, /*ObjectType*/ 0, Name)) {
1208 SkipUntil(tok::semi);
1209 return;
1210 }
1211
1212 // TODO: recover from mistakenly-qualified operator declarations.
1213 if (ExpectAndConsume(tok::semi,
1214 diag::err_expected_semi_after,
1215 "access declaration",
1216 tok::semi))
1217 return;
1218
1219 Actions.ActOnUsingDeclaration(CurScope, AS,
1220 false, SourceLocation(),
1221 SS, Name,
1222 /* AttrList */ 0,
1223 /* IsTypeName */ false,
1224 SourceLocation());
1225 return;
1226 }
1227 }
1228
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001229 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001230 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001231 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001232 SourceLocation DeclEnd;
1233 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001234 return;
1235 }
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Chris Lattner682bf922009-03-29 16:50:03 +00001237 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001238 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001239 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001240 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001241 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001242 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001243 return;
1244 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001245
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001246 // Handle: member-declaration ::= '__extension__' member-declaration
1247 if (Tok.is(tok::kw___extension__)) {
1248 // __extension__ silences extension warnings in the subexpression.
1249 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1250 ConsumeToken();
Douglas Gregor37b372b2009-08-20 22:52:58 +00001251 return ParseCXXClassMemberDeclaration(AS, TemplateInfo);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001252 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001253
Chris Lattner4ed5d912010-02-02 01:23:29 +00001254 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1255 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001256 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001257
Sean Huntbbd37c62009-11-21 08:43:09 +00001258 CXX0XAttributeList AttrList;
1259 // Optional C++0x attribute-specifier
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001260 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())
Sean Huntbbd37c62009-11-21 08:43:09 +00001261 AttrList = ParseCXX0XAttributes();
Sean Huntbbd37c62009-11-21 08:43:09 +00001262
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001263 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001264 // FIXME: Check for template aliases
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001265
Sean Huntbbd37c62009-11-21 08:43:09 +00001266 if (AttrList.HasAttr)
1267 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed)
1268 << AttrList.Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001270 // Eat 'using'.
1271 SourceLocation UsingLoc = ConsumeToken();
1272
1273 if (Tok.is(tok::kw_namespace)) {
1274 Diag(UsingLoc, diag::err_using_namespace_in_class);
1275 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001276 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001277 SourceLocation DeclEnd;
1278 // Otherwise, it must be using-declaration.
Anders Carlsson595adc12009-08-29 19:54:19 +00001279 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001280 }
1281 return;
1282 }
1283
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001284 SourceLocation DSStart = Tok.getLocation();
1285 // decl-specifier-seq:
1286 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001287 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +00001288 DS.AddAttributes(AttrList.AttrList);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001289 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001290
John McCalldd4a3b02009-09-16 22:47:08 +00001291 Action::MultiTemplateParamsArg TemplateParams(Actions,
1292 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1293 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1294
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001295 if (Tok.is(tok::semi)) {
1296 ConsumeToken();
Douglas Gregord85bea22009-09-26 06:47:28 +00001297 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall67d1a672009-08-06 02:15:43 +00001298 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001299 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001300
John McCall54abf7d2009-11-04 02:18:39 +00001301 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001302
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001303 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001304 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1305 ColonProtectionRAIIObject X(*this);
1306
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001307 // Parse the first declarator.
1308 ParseDeclarator(DeclaratorInfo);
1309 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001310 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001311 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001312 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001313 if (Tok.is(tok::semi))
1314 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001315 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001316 }
1317
John Thompson1b2fc0f2009-11-25 22:58:06 +00001318 // If attributes exist after the declarator, but before an '{', parse them.
1319 if (Tok.is(tok::kw___attribute)) {
1320 SourceLocation Loc;
1321 AttributeList *AttrList = ParseGNUAttributes(&Loc);
1322 DeclaratorInfo.AddAttributes(AttrList, Loc);
1323 }
1324
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001325 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001326 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001327 || (DeclaratorInfo.isFunctionDeclarator() &&
1328 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001329 if (!DeclaratorInfo.isFunctionDeclarator()) {
1330 Diag(Tok, diag::err_func_def_no_params);
1331 ConsumeBrace();
1332 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001333 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001334 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001335
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001336 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1337 Diag(Tok, diag::err_function_declared_typedef);
1338 // This recovery skips the entire function body. It would be nice
1339 // to simply call ParseCXXInlineMethodDef() below, however Sema
1340 // assumes the declarator represents a function, not a typedef.
1341 ConsumeBrace();
1342 SkipUntil(tok::r_brace, true);
Chris Lattner682bf922009-03-29 16:50:03 +00001343 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001344 }
1345
Douglas Gregor37b372b2009-08-20 22:52:58 +00001346 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo);
Chris Lattner682bf922009-03-29 16:50:03 +00001347 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001348 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001349 }
1350
1351 // member-declarator-list:
1352 // member-declarator
1353 // member-declarator-list ',' member-declarator
1354
Chris Lattner682bf922009-03-29 16:50:03 +00001355 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001356 OwningExprResult BitfieldSize(Actions);
1357 OwningExprResult Init(Actions);
Sebastian Redle2b68332009-04-12 17:16:29 +00001358 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001359
1360 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001361 // member-declarator:
1362 // declarator pure-specifier[opt]
1363 // declarator constant-initializer[opt]
1364 // identifier[opt] ':' constant-expression
1365
1366 if (Tok.is(tok::colon)) {
1367 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001368 BitfieldSize = ParseConstantExpression();
1369 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001370 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001371 }
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001373 // pure-specifier:
1374 // '= 0'
1375 //
1376 // constant-initializer:
1377 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001378 //
1379 // defaulted/deleted function-definition:
1380 // '=' 'default' [TODO]
1381 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001382
1383 if (Tok.is(tok::equal)) {
1384 ConsumeToken();
Sebastian Redle2b68332009-04-12 17:16:29 +00001385 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
1386 ConsumeToken();
1387 Deleted = true;
1388 } else {
1389 Init = ParseInitializer();
1390 if (Init.isInvalid())
1391 SkipUntil(tok::comma, true, true);
1392 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001393 }
1394
1395 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001396 if (Tok.is(tok::kw___attribute)) {
1397 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001398 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001399 DeclaratorInfo.AddAttributes(AttrList, Loc);
1400 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001401
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001402 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001403 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001404 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001405
1406 DeclPtrTy ThisDecl;
1407 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001408 // TODO: handle initializers, bitfields, 'delete'
1409 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo,
1410 /*IsDefinition*/ false,
1411 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001412 } else {
John McCall67d1a672009-08-06 02:15:43 +00001413 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS,
1414 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001415 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001416 BitfieldSize.release(),
1417 Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001418 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001419 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001420 }
Chris Lattner682bf922009-03-29 16:50:03 +00001421 if (ThisDecl)
1422 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001423
Douglas Gregor72b505b2008-12-16 21:30:33 +00001424 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001425 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001426 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001427 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001428 }
1429
John McCall54abf7d2009-11-04 02:18:39 +00001430 DeclaratorInfo.complete(ThisDecl);
1431
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001432 // If we don't have a comma, it is either the end of the list (a ';')
1433 // or an error, bail out.
1434 if (Tok.isNot(tok::comma))
1435 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001437 // Consume the comma.
1438 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001440 // Parse the next declarator.
1441 DeclaratorInfo.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001442 BitfieldSize = 0;
1443 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001444 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001446 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001447 if (Tok.is(tok::kw___attribute)) {
1448 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001449 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001450 DeclaratorInfo.AddAttributes(AttrList, Loc);
1451 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001452
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001453 if (Tok.isNot(tok::colon))
1454 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001455 }
1456
Chris Lattnerae50d502010-02-02 00:43:15 +00001457 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1458 // Skip to end of block or statement.
1459 SkipUntil(tok::r_brace, true, true);
1460 // If we stopped at a ';', eat it.
1461 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001462 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001463 }
1464
Chris Lattnerae50d502010-02-02 00:43:15 +00001465 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(),
1466 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001467}
1468
1469/// ParseCXXMemberSpecification - Parse the class definition.
1470///
1471/// member-specification:
1472/// member-declaration member-specification[opt]
1473/// access-specifier ':' member-specification[opt]
1474///
1475void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001476 unsigned TagType, DeclPtrTy TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001477 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001478 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001479 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001480
Chris Lattner49f28ca2009-03-05 08:00:35 +00001481 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1482 PP.getSourceManager(),
1483 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Douglas Gregor26997fd2010-01-16 20:52:59 +00001485 // Determine whether this is a non-nested class. Note that local
1486 // classes are *not* considered to be nested classes.
1487 bool NonNestedClass = true;
1488 if (!ClassStack.empty()) {
1489 for (const Scope *S = CurScope; S; S = S->getParent()) {
1490 if (S->isClassScope()) {
1491 // We're inside a class scope, so this is a nested class.
1492 NonNestedClass = false;
1493 break;
1494 }
1495
1496 if ((S->getFlags() & Scope::FnScope)) {
1497 // If we're in a function or function template declared in the
1498 // body of a class, then this is a local class rather than a
1499 // nested class.
1500 const Scope *Parent = S->getParent();
1501 if (Parent->isTemplateParamScope())
1502 Parent = Parent->getParent();
1503 if (Parent->isClassScope())
1504 break;
1505 }
1506 }
1507 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001508
1509 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001510 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001511
Douglas Gregor6569d682009-05-27 23:11:45 +00001512 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001513 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001514
Douglas Gregorddc29e12009-02-06 22:42:48 +00001515 if (TagDecl)
1516 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001517
1518 if (Tok.is(tok::colon)) {
1519 ParseBaseClause(TagDecl);
1520
1521 if (!Tok.is(tok::l_brace)) {
1522 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00001523
1524 if (TagDecl)
1525 Actions.ActOnTagDefinitionError(CurScope, TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001526 return;
1527 }
1528 }
1529
1530 assert(Tok.is(tok::l_brace));
1531
1532 SourceLocation LBraceLoc = ConsumeBrace();
1533
1534 if (!TagDecl) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001535 SkipUntil(tok::r_brace, false, false);
1536 return;
1537 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001538
John McCallf9368152009-12-20 07:58:13 +00001539 Actions.ActOnStartCXXMemberDeclarations(CurScope, TagDecl, LBraceLoc);
1540
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001541 // C++ 11p3: Members of a class defined with the keyword class are private
1542 // by default. Members of a class defined with the keywords struct or union
1543 // are public by default.
1544 AccessSpecifier CurAS;
1545 if (TagType == DeclSpec::TST_class)
1546 CurAS = AS_private;
1547 else
1548 CurAS = AS_public;
1549
1550 // While we still have something to read, read the member-declarations.
1551 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1552 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001554 // Check for extraneous top-level semicolon.
1555 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +00001556 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001557 << FixItHint::CreateRemoval(Tok.getLocation());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001558 ConsumeToken();
1559 continue;
1560 }
1561
1562 AccessSpecifier AS = getAccessSpecifierIfPresent();
1563 if (AS != AS_none) {
1564 // Current token is a C++ access specifier.
1565 CurAS = AS;
1566 ConsumeToken();
1567 ExpectAndConsume(tok::colon, diag::err_expected_colon);
1568 continue;
1569 }
1570
Douglas Gregor37b372b2009-08-20 22:52:58 +00001571 // FIXME: Make sure we don't have a template here.
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001573 // Parse all the comma separated declarators.
1574 ParseCXXClassMemberDeclaration(CurAS);
1575 }
Mike Stump1eb44332009-09-09 15:08:12 +00001576
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001577 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001579 // If attributes exist after class contents, parse them.
Ted Kremenek1e377652010-02-11 02:19:13 +00001580 llvm::OwningPtr<AttributeList> AttrList;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001581 if (Tok.is(tok::kw___attribute))
Douglas Gregor0b4c9b52010-03-29 14:42:08 +00001582 AttrList.reset(ParseGNUAttributes());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001583
1584 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl,
Douglas Gregor0b4c9b52010-03-29 14:42:08 +00001585 LBraceLoc, RBraceLoc,
1586 AttrList.get());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001587
1588 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1589 // complete within function bodies, default arguments,
1590 // exception-specifications, and constructor ctor-initializers (including
1591 // such things in nested classes).
1592 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001593 // FIXME: Only function bodies and constructor ctor-initializers are
1594 // parsed correctly, fix the rest.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001595 if (NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001596 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001597 // are complete and we can parse the delayed portions of method
1598 // declarations and the lexed inline method definitions.
Douglas Gregor6569d682009-05-27 23:11:45 +00001599 ParseLexedMethodDeclarations(getCurrentClass());
1600 ParseLexedMethodDefs(getCurrentClass());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001601 }
1602
John McCalldb7bb4a2010-03-17 00:38:33 +00001603 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
1604
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001605 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001606 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001607 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001608}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001609
1610/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1611/// which explicitly initializes the members or base classes of a
1612/// class (C++ [class.base.init]). For example, the three initializers
1613/// after the ':' in the Derived constructor below:
1614///
1615/// @code
1616/// class Base { };
1617/// class Derived : Base {
1618/// int x;
1619/// float f;
1620/// public:
1621/// Derived(float f) : Base(), x(17), f(f) { }
1622/// };
1623/// @endcode
1624///
Mike Stump1eb44332009-09-09 15:08:12 +00001625/// [C++] ctor-initializer:
1626/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001627///
Mike Stump1eb44332009-09-09 15:08:12 +00001628/// [C++] mem-initializer-list:
1629/// mem-initializer
1630/// mem-initializer , mem-initializer-list
Chris Lattnerb28317a2009-03-28 19:18:32 +00001631void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001632 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1633
1634 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001635
Douglas Gregor7ad83902008-11-05 04:29:56 +00001636 llvm::SmallVector<MemInitTy*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001637 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001638
Douglas Gregor7ad83902008-11-05 04:29:56 +00001639 do {
1640 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001641 if (!MemInit.isInvalid())
1642 MemInitializers.push_back(MemInit.get());
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001643 else
1644 AnyErrors = true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001645
Douglas Gregor7ad83902008-11-05 04:29:56 +00001646 if (Tok.is(tok::comma))
1647 ConsumeToken();
1648 else if (Tok.is(tok::l_brace))
1649 break;
1650 else {
1651 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001652 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001653 SkipUntil(tok::l_brace, true, true);
1654 break;
1655 }
1656 } while (true);
1657
Mike Stump1eb44332009-09-09 15:08:12 +00001658 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001659 MemInitializers.data(), MemInitializers.size(),
1660 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001661}
1662
1663/// ParseMemInitializer - Parse a C++ member initializer, which is
1664/// part of a constructor initializer that explicitly initializes one
1665/// member or base class (C++ [class.base.init]). See
1666/// ParseConstructorInitializer for an example.
1667///
1668/// [C++] mem-initializer:
1669/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001670///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001671/// [C++] mem-initializer-id:
1672/// '::'[opt] nested-name-specifier[opt] class-name
1673/// identifier
Chris Lattnerb28317a2009-03-28 19:18:32 +00001674Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001675 // parse '::'[opt] nested-name-specifier[opt]
1676 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001677 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001678 TypeTy *TemplateTypeTy = 0;
1679 if (Tok.is(tok::annot_template_id)) {
1680 TemplateIdAnnotation *TemplateId
1681 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001682 if (TemplateId->Kind == TNK_Type_template ||
1683 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanian96174332009-07-01 19:21:19 +00001684 AnnotateTemplateIdTokenAsType(&SS);
1685 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1686 TemplateTypeTy = Tok.getAnnotationValue();
1687 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001688 }
1689 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001690 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001691 return true;
1692 }
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Douglas Gregor7ad83902008-11-05 04:29:56 +00001694 // Get the identifier. This may be a member name or a class name,
1695 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001696 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001697 SourceLocation IdLoc = ConsumeToken();
1698
1699 // Parse the '('.
1700 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001701 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001702 return true;
1703 }
1704 SourceLocation LParenLoc = ConsumeParen();
1705
1706 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001707 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001708 CommaLocsTy CommaLocs;
1709 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1710 SkipUntil(tok::r_paren);
1711 return true;
1712 }
1713
1714 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1715
Fariborz Jahanian96174332009-07-01 19:21:19 +00001716 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II,
1717 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001718 LParenLoc, ArgExprs.take(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001719 ArgExprs.size(), CommaLocs.data(),
1720 RParenLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001721}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001722
1723/// ParseExceptionSpecification - Parse a C++ exception-specification
1724/// (C++ [except.spec]).
1725///
Douglas Gregora4745612008-12-01 18:00:20 +00001726/// exception-specification:
1727/// 'throw' '(' type-id-list [opt] ')'
1728/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001729///
Douglas Gregora4745612008-12-01 18:00:20 +00001730/// type-id-list:
1731/// type-id
1732/// type-id-list ',' type-id
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001733///
Sebastian Redl7dc81342009-04-29 17:30:04 +00001734bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
Sebastian Redlef65f062009-05-29 18:02:33 +00001735 llvm::SmallVector<TypeTy*, 2>
1736 &Exceptions,
1737 llvm::SmallVector<SourceRange, 2>
1738 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00001739 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001740 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00001741
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001742 SourceLocation ThrowLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001743
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001744 if (!Tok.is(tok::l_paren)) {
1745 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
1746 }
1747 SourceLocation LParenLoc = ConsumeParen();
1748
Douglas Gregora4745612008-12-01 18:00:20 +00001749 // Parse throw(...), a Microsoft extension that means "this function
1750 // can throw anything".
1751 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001752 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00001753 SourceLocation EllipsisLoc = ConsumeToken();
1754 if (!getLang().Microsoft)
1755 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001756 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00001757 return false;
1758 }
1759
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001760 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00001761 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001762 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001763 TypeResult Res(ParseTypeName(&Range));
1764 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00001765 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00001766 Ranges.push_back(Range);
1767 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001768 if (Tok.is(tok::comma))
1769 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00001770 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001771 break;
1772 }
1773
Sebastian Redlab197ba2009-02-09 18:23:29 +00001774 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001775 return false;
1776}
Douglas Gregor6569d682009-05-27 23:11:45 +00001777
1778/// \brief We have just started parsing the definition of a new class,
1779/// so push that class onto our stack of classes that is currently
1780/// being parsed.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001781void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool NonNestedClass) {
1782 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00001783 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00001784 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
Douglas Gregor6569d682009-05-27 23:11:45 +00001785}
1786
1787/// \brief Deallocate the given parsed class and all of its nested
1788/// classes.
1789void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
1790 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I)
1791 DeallocateParsedClasses(Class->NestedClasses[I]);
1792 delete Class;
1793}
1794
1795/// \brief Pop the top class of the stack of classes that are
1796/// currently being parsed.
1797///
1798/// This routine should be called when we have finished parsing the
1799/// definition of a class, but have not yet popped the Scope
1800/// associated with the class's definition.
1801///
1802/// \returns true if the class we've popped is a top-level class,
1803/// false otherwise.
1804void Parser::PopParsingClass() {
1805 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Douglas Gregor6569d682009-05-27 23:11:45 +00001807 ParsingClass *Victim = ClassStack.top();
1808 ClassStack.pop();
1809 if (Victim->TopLevelClass) {
1810 // Deallocate all of the nested classes of this class,
1811 // recursively: we don't need to keep any of this information.
1812 DeallocateParsedClasses(Victim);
1813 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001814 }
Douglas Gregor6569d682009-05-27 23:11:45 +00001815 assert(!ClassStack.empty() && "Missing top-level class?");
1816
1817 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() &&
1818 Victim->NestedClasses.empty()) {
1819 // The victim is a nested class, but we will not need to perform
1820 // any processing after the definition of this class since it has
1821 // no members whose handling was delayed. Therefore, we can just
1822 // remove this nested class.
1823 delete Victim;
1824 return;
1825 }
1826
1827 // This nested class has some members that will need to be processed
1828 // after the top-level class is completely defined. Therefore, add
1829 // it to the list of nested classes within its parent.
1830 assert(CurScope->isClassScope() && "Nested class outside of class scope?");
1831 ClassStack.top()->NestedClasses.push_back(Victim);
1832 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope();
1833}
Sean Huntbbd37c62009-11-21 08:43:09 +00001834
1835/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
1836/// parses standard attributes.
1837///
1838/// [C++0x] attribute-specifier:
1839/// '[' '[' attribute-list ']' ']'
1840///
1841/// [C++0x] attribute-list:
1842/// attribute[opt]
1843/// attribute-list ',' attribute[opt]
1844///
1845/// [C++0x] attribute:
1846/// attribute-token attribute-argument-clause[opt]
1847///
1848/// [C++0x] attribute-token:
1849/// identifier
1850/// attribute-scoped-token
1851///
1852/// [C++0x] attribute-scoped-token:
1853/// attribute-namespace '::' identifier
1854///
1855/// [C++0x] attribute-namespace:
1856/// identifier
1857///
1858/// [C++0x] attribute-argument-clause:
1859/// '(' balanced-token-seq ')'
1860///
1861/// [C++0x] balanced-token-seq:
1862/// balanced-token
1863/// balanced-token-seq balanced-token
1864///
1865/// [C++0x] balanced-token:
1866/// '(' balanced-token-seq ')'
1867/// '[' balanced-token-seq ']'
1868/// '{' balanced-token-seq '}'
1869/// any token but '(', ')', '[', ']', '{', or '}'
1870CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) {
1871 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
1872 && "Not a C++0x attribute list");
1873
1874 SourceLocation StartLoc = Tok.getLocation(), Loc;
1875 AttributeList *CurrAttr = 0;
1876
1877 ConsumeBracket();
1878 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001879
Sean Huntbbd37c62009-11-21 08:43:09 +00001880 if (Tok.is(tok::comma)) {
1881 Diag(Tok.getLocation(), diag::err_expected_ident);
1882 ConsumeToken();
1883 }
1884
1885 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
1886 // attribute not present
1887 if (Tok.is(tok::comma)) {
1888 ConsumeToken();
1889 continue;
1890 }
1891
1892 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
1893 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001894
Sean Huntbbd37c62009-11-21 08:43:09 +00001895 // scoped attribute
1896 if (Tok.is(tok::coloncolon)) {
1897 ConsumeToken();
1898
1899 if (!Tok.is(tok::identifier)) {
1900 Diag(Tok.getLocation(), diag::err_expected_ident);
1901 SkipUntil(tok::r_square, tok::comma, true, true);
1902 continue;
1903 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001904
Sean Huntbbd37c62009-11-21 08:43:09 +00001905 ScopeName = AttrName;
1906 ScopeLoc = AttrLoc;
1907
1908 AttrName = Tok.getIdentifierInfo();
1909 AttrLoc = ConsumeToken();
1910 }
1911
1912 bool AttrParsed = false;
1913 // No scoped names are supported; ideally we could put all non-standard
1914 // attributes into namespaces.
1915 if (!ScopeName) {
1916 switch(AttributeList::getKind(AttrName))
1917 {
1918 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00001919 case AttributeList::AT_base_check:
1920 case AttributeList::AT_carries_dependency:
Sean Huntbbd37c62009-11-21 08:43:09 +00001921 case AttributeList::AT_final:
Sean Hunt7725e672009-11-25 04:20:27 +00001922 case AttributeList::AT_hiding:
1923 case AttributeList::AT_noreturn:
1924 case AttributeList::AT_override: {
Sean Huntbbd37c62009-11-21 08:43:09 +00001925 if (Tok.is(tok::l_paren)) {
1926 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
1927 << AttrName->getName();
1928 break;
1929 }
1930
1931 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0,
1932 SourceLocation(), 0, 0, CurrAttr, false,
1933 true);
1934 AttrParsed = true;
1935 break;
1936 }
1937
1938 // One argument; must be a type-id or assignment-expression
1939 case AttributeList::AT_aligned: {
1940 if (Tok.isNot(tok::l_paren)) {
1941 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
1942 << AttrName->getName();
1943 break;
1944 }
1945 SourceLocation ParamLoc = ConsumeParen();
1946
1947 OwningExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
1948
1949 MatchRHSPunctuation(tok::r_paren, ParamLoc);
1950
1951 ExprVector ArgExprs(Actions);
1952 ArgExprs.push_back(ArgExpr.release());
1953 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc,
1954 0, ParamLoc, ArgExprs.take(), 1, CurrAttr,
1955 false, true);
1956
1957 AttrParsed = true;
1958 break;
1959 }
1960
1961 // Silence warnings
1962 default: break;
1963 }
1964 }
1965
1966 // Skip the entire parameter clause, if any
1967 if (!AttrParsed && Tok.is(tok::l_paren)) {
1968 ConsumeParen();
1969 // SkipUntil maintains the balancedness of tokens.
1970 SkipUntil(tok::r_paren, false);
1971 }
1972 }
1973
1974 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1975 SkipUntil(tok::r_square, false);
1976 Loc = Tok.getLocation();
1977 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
1978 SkipUntil(tok::r_square, false);
1979
1980 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true);
1981 return Attr;
1982}
1983
1984/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
1985/// attribute.
1986///
1987/// FIXME: Simply returns an alignof() expression if the argument is a
1988/// type. Ideally, the type should be propagated directly into Sema.
1989///
1990/// [C++0x] 'align' '(' type-id ')'
1991/// [C++0x] 'align' '(' assignment-expression ')'
1992Parser::OwningExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
1993 if (isTypeIdInParens()) {
1994 EnterExpressionEvaluationContext Unevaluated(Actions,
1995 Action::Unevaluated);
1996 SourceLocation TypeLoc = Tok.getLocation();
1997 TypeTy *Ty = ParseTypeName().get();
1998 SourceRange TypeRange(Start, Tok.getLocation());
1999 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, Ty,
2000 TypeRange);
2001 } else
2002 return ParseConstantExpression();
2003}