blob: 78fe4d2e3117cc3569c057b8949a27cbc90cb249 [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"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000022using namespace clang;
23
24/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000025/// may either be a top level namespace or a block-level namespace alias. If
26/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000027///
28/// namespace-definition: [C++ 7.3: basic.namespace]
29/// named-namespace-definition
30/// unnamed-namespace-definition
31///
32/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000033/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000034///
35/// named-namespace-definition:
36/// original-namespace-definition
37/// extension-namespace-definition
38///
39/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000040/// 'inline'[opt] 'namespace' identifier attributes[opt]
41/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000042///
43/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000044/// 'inline'[opt] 'namespace' original-namespace-name
45/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000046///
Chris Lattner8f08cb72007-08-25 06:57:03 +000047/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
48/// 'namespace' identifier '=' qualified-namespace-specifier ';'
49///
John McCalld226f652010-08-21 09:40:31 +000050Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000051 SourceLocation &DeclEnd,
52 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000053 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000054 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump1eb44332009-09-09 15:08:12 +000055
Douglas Gregor49f40bd2009-09-18 19:03:04 +000056 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000057 Actions.CodeCompleteNamespaceDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +000058 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +000059 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000060
Chris Lattner8f08cb72007-08-25 06:57:03 +000061 SourceLocation IdentLoc;
62 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000063
64 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner04d66662007-10-09 17:33:22 +000066 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000067 Ident = Tok.getIdentifierInfo();
68 IdentLoc = ConsumeToken(); // eat the identifier.
69 }
Mike Stump1eb44332009-09-09 15:08:12 +000070
Chris Lattner8f08cb72007-08-25 06:57:03 +000071 // Read label attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +000072 ParsedAttributes attrs;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000073 if (Tok.is(tok::kw___attribute)) {
74 attrTok = Tok;
75
Chris Lattner8f08cb72007-08-25 06:57:03 +000076 // FIXME: save these somewhere.
John McCall7f040a92010-12-24 02:08:15 +000077 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000078 }
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregor6a588dd2009-06-17 19:49:00 +000080 if (Tok.is(tok::equal)) {
John McCall7f040a92010-12-24 02:08:15 +000081 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +000082 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +000083 if (InlineLoc.isValid())
84 Diag(InlineLoc, diag::err_inline_namespace_alias)
85 << FixItHint::CreateRemoval(InlineLoc);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000086
Chris Lattner97144fc2009-04-02 04:16:50 +000087 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000088 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner51448322009-03-29 14:02:43 +000090 if (Tok.isNot(tok::l_brace)) {
Mike Stump1eb44332009-09-09 15:08:12 +000091 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000092 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +000093 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +000094 }
Mike Stump1eb44332009-09-09 15:08:12 +000095
Chris Lattner51448322009-03-29 14:02:43 +000096 SourceLocation LBrace = ConsumeBrace();
97
Douglas Gregor23c94db2010-07-02 17:43:08 +000098 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
99 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
100 getCurScope()->getFnParent()) {
Douglas Gregor95f1b152010-05-14 05:08:22 +0000101 Diag(LBrace, diag::err_namespace_nonnamespace_scope);
102 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000103 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000104 }
105
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000106 // If we're still good, complain about inline namespaces in non-C++0x now.
107 if (!getLang().CPlusPlus0x && InlineLoc.isValid())
108 Diag(InlineLoc, diag::ext_inline_namespace);
109
Chris Lattner51448322009-03-29 14:02:43 +0000110 // Enter a scope for the namespace.
111 ParseScope NamespaceScope(this, Scope::DeclScope);
112
John McCalld226f652010-08-21 09:40:31 +0000113 Decl *NamespcDecl =
Sebastian Redld078e642010-08-27 23:12:46 +0000114 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +0000115 LBrace, attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000116
John McCallf312b1e2010-08-26 23:41:50 +0000117 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
118 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Sean Huntbbd37c62009-11-21 08:43:09 +0000120 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall7f040a92010-12-24 02:08:15 +0000121 ParsedAttributesWithRange attrs;
122 MaybeParseCXX0XAttributes(attrs);
123 MaybeParseMicrosoftAttributes(attrs);
124 ParseExternalDeclaration(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000125 }
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Chris Lattner51448322009-03-29 14:02:43 +0000127 // Leave the namespace scope.
128 NamespaceScope.Exit();
129
Chris Lattner97144fc2009-04-02 04:16:50 +0000130 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
131 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000132
Chris Lattner97144fc2009-04-02 04:16:50 +0000133 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000134 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000135}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000136
Anders Carlssonf67606a2009-03-28 04:07:16 +0000137/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
138/// alias definition.
139///
John McCalld226f652010-08-21 09:40:31 +0000140Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000141 SourceLocation AliasLoc,
Chris Lattner97144fc2009-04-02 04:16:50 +0000142 IdentifierInfo *Alias,
143 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000144 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Anders Carlssonf67606a2009-03-28 04:07:16 +0000146 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000148 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000149 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000150 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000151 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000152
Anders Carlssonf67606a2009-03-28 04:07:16 +0000153 CXXScopeSpec SS;
154 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000155 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000156
157 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
158 Diag(Tok, diag::err_expected_namespace_name);
159 // Skip to end of the definition and eat the ';'.
160 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000161 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000162 }
163
164 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000165 IdentifierInfo *Ident = Tok.getIdentifierInfo();
166 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Anders Carlssonf67606a2009-03-28 04:07:16 +0000168 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000169 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000170 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
171 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Douglas Gregor23c94db2010-07-02 17:43:08 +0000173 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000174 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000175}
176
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000177/// ParseLinkage - We know that the current token is a string_literal
178/// and just before that, that extern was seen.
179///
180/// linkage-specification: [C++ 7.5p2: dcl.link]
181/// 'extern' string-literal '{' declaration-seq[opt] '}'
182/// 'extern' string-literal declaration
183///
Chris Lattner7d642712010-11-09 20:15:55 +0000184Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000185 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000186 llvm::SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000187 bool Invalid = false;
188 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
189 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000190 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000191
192 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000193
Douglas Gregor074149e2009-01-05 19:45:36 +0000194 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000195 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000196 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Douglas Gregor074149e2009-01-05 19:45:36 +0000197 /*FIXME: */SourceLocation(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000198 Loc, Lang,
Mike Stump1eb44332009-09-09 15:08:12 +0000199 Tok.is(tok::l_brace)? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000200 : SourceLocation());
201
John McCall7f040a92010-12-24 02:08:15 +0000202 ParsedAttributesWithRange attrs;
203 MaybeParseCXX0XAttributes(attrs);
204 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000205
Douglas Gregor074149e2009-01-05 19:45:36 +0000206 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000207 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000208 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000209 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000210 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000211 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000212
Douglas Gregor63a01132010-02-07 08:38:28 +0000213 DS.abort();
214
John McCall7f040a92010-12-24 02:08:15 +0000215 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000216
Douglas Gregorf44515a2008-12-16 22:23:02 +0000217 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000218 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall7f040a92010-12-24 02:08:15 +0000219 ParsedAttributesWithRange attrs;
220 MaybeParseCXX0XAttributes(attrs);
221 MaybeParseMicrosoftAttributes(attrs);
222 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000223 }
224
Douglas Gregorf44515a2008-12-16 22:23:02 +0000225 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Chris Lattner7d642712010-11-09 20:15:55 +0000226 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
227 RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000228}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000229
Douglas Gregorf780abc2008-12-30 03:27:21 +0000230/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
231/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000232Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000233 const ParsedTemplateInfo &TemplateInfo,
234 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000235 ParsedAttributesWithRange &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000236 assert(Tok.is(tok::kw_using) && "Not using token");
237
238 // Eat 'using'.
239 SourceLocation UsingLoc = ConsumeToken();
240
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000241 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000242 Actions.CodeCompleteUsing(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000243 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000244 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000245
John McCall78b81052010-11-10 02:40:36 +0000246 // 'using namespace' means this is a using-directive.
247 if (Tok.is(tok::kw_namespace)) {
248 // Template parameters are always an error here.
249 if (TemplateInfo.Kind) {
250 SourceRange R = TemplateInfo.getSourceRange();
251 Diag(UsingLoc, diag::err_templated_using_directive)
252 << R << FixItHint::CreateRemoval(R);
253 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000254
John McCall7f040a92010-12-24 02:08:15 +0000255 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000256 }
257
258 // Otherwise, it must be a using-declaration.
259
260 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000261 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000262
John McCall78b81052010-11-10 02:40:36 +0000263 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000264}
265
266/// ParseUsingDirective - Parse C++ using-directive, assumes
267/// that current token is 'namespace' and 'using' was already parsed.
268///
269/// using-directive: [C++ 7.3.p4: namespace.udir]
270/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
271/// namespace-name ;
272/// [GNU] using-directive:
273/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
274/// namespace-name attributes[opt] ;
275///
John McCalld226f652010-08-21 09:40:31 +0000276Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000277 SourceLocation UsingLoc,
278 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000279 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000280 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
281
282 // Eat 'namespace'.
283 SourceLocation NamespcLoc = ConsumeToken();
284
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000285 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000286 Actions.CodeCompleteUsingDirective(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000287 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000288 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000289
Douglas Gregorf780abc2008-12-30 03:27:21 +0000290 CXXScopeSpec SS;
291 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000292 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000293
Douglas Gregorf780abc2008-12-30 03:27:21 +0000294 IdentifierInfo *NamespcName = 0;
295 SourceLocation IdentLoc = SourceLocation();
296
297 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000298 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000299 Diag(Tok, diag::err_expected_namespace_name);
300 // If there was invalid namespace name, skip to end of decl, and eat ';'.
301 SkipUntil(tok::semi);
302 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000303 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000304 }
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Chris Lattner823c44e2009-01-06 07:27:21 +0000306 // Parse identifier.
307 NamespcName = Tok.getIdentifierInfo();
308 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Chris Lattner823c44e2009-01-06 07:27:21 +0000310 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000311 bool GNUAttr = false;
312 if (Tok.is(tok::kw___attribute)) {
313 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000314 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Chris Lattner823c44e2009-01-06 07:27:21 +0000317 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000318 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000319 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000320 GNUAttr ? diag::err_expected_semi_after_attribute_list
321 : diag::err_expected_semi_after_namespace_name,
322 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000323
Douglas Gregor23c94db2010-07-02 17:43:08 +0000324 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000325 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000326}
327
328/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
329/// 'using' was already seen.
330///
331/// using-declaration: [C++ 7.3.p3: namespace.udecl]
332/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000333/// unqualified-id
334/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000335///
John McCalld226f652010-08-21 09:40:31 +0000336Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000337 const ParsedTemplateInfo &TemplateInfo,
338 SourceLocation UsingLoc,
339 SourceLocation &DeclEnd,
340 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000341 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000342 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000343 bool IsTypeName;
344
John McCall78b81052010-11-10 02:40:36 +0000345 // TODO: in C++0x, if we have template parameters this must be a
346 // template alias:
347 // template <...> using id = type;
348
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000349 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000350 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000351 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000352 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000353 ConsumeToken();
354 IsTypeName = true;
355 }
356 else
357 IsTypeName = false;
358
359 // Parse nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000360 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000361
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000362 // Check nested-name specifier.
363 if (SS.isInvalid()) {
364 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000365 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000366 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000367
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000368 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000369 // destructor names and allow the action module to diagnose any semantic
370 // errors.
371 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000372 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000373 /*EnteringContext=*/false,
374 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000375 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000376 ParsedType(),
Douglas Gregor12c118a2009-11-04 16:30:06 +0000377 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000378 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000379 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000380 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000381
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000382 // Parse (optional) attributes (most likely GNU strong-using extension).
John McCall7f040a92010-12-24 02:08:15 +0000383 ParsedAttributes attrs;
384 MaybeParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000386 // Eat ';'.
387 DeclEnd = Tok.getLocation();
388 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
John McCall7f040a92010-12-24 02:08:15 +0000389 !attrs.empty() ? "attributes list" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000390 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000391
John McCall78b81052010-11-10 02:40:36 +0000392 // Diagnose an attempt to declare a templated using-declaration.
393 if (TemplateInfo.Kind) {
394 SourceRange R = TemplateInfo.getSourceRange();
395 Diag(UsingLoc, diag::err_templated_using_declaration)
396 << R << FixItHint::CreateRemoval(R);
397
398 // Unfortunately, we have to bail out instead of recovering by
399 // ignoring the parameters, just in case the nested name specifier
400 // depends on the parameters.
401 return 0;
402 }
403
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000404 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000405 Name, attrs.getList(),
406 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000407}
408
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000409/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
410///
411/// static_assert-declaration:
412/// static_assert ( constant-expression , string-literal ) ;
413///
John McCalld226f652010-08-21 09:40:31 +0000414Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000415 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
416 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000418 if (Tok.isNot(tok::l_paren)) {
419 Diag(Tok, diag::err_expected_lparen);
John McCalld226f652010-08-21 09:40:31 +0000420 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000423 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000424
John McCall60d7b3a2010-08-24 06:29:42 +0000425 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000426 if (AssertExpr.isInvalid()) {
427 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000428 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000429 }
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Anders Carlssonad5f9602009-03-13 23:29:20 +0000431 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000432 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000433
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000434 if (Tok.isNot(tok::string_literal)) {
435 Diag(Tok, diag::err_expected_string_literal);
436 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000437 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000438 }
Mike Stump1eb44332009-09-09 15:08:12 +0000439
John McCall60d7b3a2010-08-24 06:29:42 +0000440 ExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000441 if (AssertMessage.isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000442 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000443
Anders Carlsson94b15fb2009-03-15 18:44:04 +0000444 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Chris Lattner97144fc2009-04-02 04:16:50 +0000446 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000447 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000448
John McCall9ae2f072010-08-23 23:25:46 +0000449 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
450 AssertExpr.take(),
451 AssertMessage.take());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000452}
453
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000454/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
455///
456/// 'decltype' ( expression )
457///
458void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
459 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
460
461 SourceLocation StartLoc = ConsumeToken();
462 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000463
464 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000465 "decltype")) {
466 SkipUntil(tok::r_paren);
467 return;
468 }
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000470 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000472 // C++0x [dcl.type.simple]p4:
473 // The operand of the decltype specifier is an unevaluated operand.
474 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000475 Sema::Unevaluated);
John McCall60d7b3a2010-08-24 06:29:42 +0000476 ExprResult Result = ParseExpression();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000477 if (Result.isInvalid()) {
478 SkipUntil(tok::r_paren);
479 return;
480 }
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000482 // Match the ')'
483 SourceLocation RParenLoc;
484 if (Tok.is(tok::r_paren))
485 RParenLoc = ConsumeParen();
486 else
487 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000489 if (RParenLoc.isInvalid())
490 return;
491
492 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000493 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000494 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000495 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000496 DiagID, Result.release()))
497 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000498}
499
Douglas Gregor42a552f2008-11-05 20:51:48 +0000500/// ParseClassName - Parse a C++ class-name, which names a class. Note
501/// that we only check that the result names a type; semantic analysis
502/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000503/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000504/// found.
505///
506/// class-name: [C++ 9.1]
507/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000508/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000509///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000510Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000511 CXXScopeSpec *SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000512 // Check whether we have a template-id that names a type.
513 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000514 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000515 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000516 if (TemplateId->Kind == TNK_Type_template ||
517 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000518 AnnotateTemplateIdTokenAsType(SS);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000519
520 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000521 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000522 EndLocation = Tok.getAnnotationEndLoc();
523 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000524
525 if (Type)
526 return Type;
527 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000528 }
529
530 // Fall through to produce an error below.
531 }
532
Douglas Gregor42a552f2008-11-05 20:51:48 +0000533 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000534 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000535 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000536 }
537
Douglas Gregor84d0a192010-01-12 21:28:44 +0000538 IdentifierInfo *Id = Tok.getIdentifierInfo();
539 SourceLocation IdLoc = ConsumeToken();
540
541 if (Tok.is(tok::less)) {
542 // It looks the user intended to write a template-id here, but the
543 // template-name was wrong. Try to fix that.
544 TemplateNameKind TNK = TNK_Type_template;
545 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000546 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor84d0a192010-01-12 21:28:44 +0000547 SS, Template, TNK)) {
548 Diag(IdLoc, diag::err_unknown_template_name)
549 << Id;
550 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000551
Douglas Gregor84d0a192010-01-12 21:28:44 +0000552 if (!Template)
553 return true;
554
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000555 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000556 UnqualifiedId TemplateName;
557 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000558
Douglas Gregor84d0a192010-01-12 21:28:44 +0000559 // Parse the full template-id, then turn it into a type.
560 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
561 SourceLocation(), true))
562 return true;
563 if (TNK == TNK_Dependent_template_name)
564 AnnotateTemplateIdTokenAsType(SS);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000565
Douglas Gregor84d0a192010-01-12 21:28:44 +0000566 // If we didn't end up with a typename token, there's nothing more we
567 // can do.
568 if (Tok.isNot(tok::annot_typename))
569 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000570
Douglas Gregor84d0a192010-01-12 21:28:44 +0000571 // Retrieve the type from the annotation token, consume that token, and
572 // return.
573 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000574 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000575 ConsumeToken();
576 return Type;
577 }
578
Douglas Gregor42a552f2008-11-05 20:51:48 +0000579 // We have an identifier; check whether it is actually a type.
Douglas Gregor9e876872011-03-01 18:12:44 +0000580 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), SS, true,
581 false, ParsedType(),
582 /*NonTrivialTypeSourceInfo=*/true);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000583 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000584 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000585 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000586 }
587
588 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000589 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000590
591 // Fake up a Declarator to use with ActOnTypeName.
592 DeclSpec DS;
593 DS.SetRangeStart(IdLoc);
594 DS.SetRangeEnd(EndLocation);
595 DS.getTypeSpecScope() = *SS;
596
597 const char *PrevSpec = 0;
598 unsigned DiagID;
599 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
600
601 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
602 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000603}
604
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000605/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
606/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
607/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000608/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000609///
610/// class-specifier: [C++ class]
611/// class-head '{' member-specification[opt] '}'
612/// class-head '{' member-specification[opt] '}' attributes[opt]
613/// class-head:
614/// class-key identifier[opt] base-clause[opt]
615/// class-key nested-name-specifier identifier base-clause[opt]
616/// class-key nested-name-specifier[opt] simple-template-id
617/// base-clause[opt]
618/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000619/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000620/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000621/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000622/// simple-template-id base-clause[opt]
623/// class-key:
624/// 'class'
625/// 'struct'
626/// 'union'
627///
628/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000629/// class-key ::[opt] nested-name-specifier[opt] identifier
630/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
631/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000632///
633/// Note that the C++ class-specifier and elaborated-type-specifier,
634/// together, subsume the C99 struct-or-union-specifier:
635///
636/// struct-or-union-specifier: [C99 6.7.2.1]
637/// struct-or-union identifier[opt] '{' struct-contents '}'
638/// struct-or-union identifier
639/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
640/// '}' attributes[opt]
641/// [GNU] struct-or-union attributes[opt] identifier
642/// struct-or-union:
643/// 'struct'
644/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000645void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
646 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000647 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redld9bafa72010-02-03 21:21:43 +0000648 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000649 DeclSpec::TST TagType;
650 if (TagTokKind == tok::kw_struct)
651 TagType = DeclSpec::TST_struct;
652 else if (TagTokKind == tok::kw_class)
653 TagType = DeclSpec::TST_class;
654 else {
655 assert(TagTokKind == tok::kw_union && "Not a class specifier");
656 TagType = DeclSpec::TST_union;
657 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000658
Douglas Gregor374929f2009-09-18 15:37:17 +0000659 if (Tok.is(tok::code_completion)) {
660 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000661 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregordc845342010-05-25 05:58:43 +0000662 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +0000663 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000664
Chandler Carruth926c4b42010-06-28 08:39:25 +0000665 // C++03 [temp.explicit] 14.7.2/8:
666 // The usual access checking rules do not apply to names used to specify
667 // explicit instantiations.
668 //
669 // As an extension we do not perform access checking on the names used to
670 // specify explicit specializations either. This is important to allow
671 // specializing traits classes for private types.
672 bool SuppressingAccessChecks = false;
673 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
674 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
675 Actions.ActOnStartSuppressingAccessChecks();
676 SuppressingAccessChecks = true;
677 }
678
John McCall7f040a92010-12-24 02:08:15 +0000679 ParsedAttributes attrs;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000680 // If attributes exist after tag, parse them.
681 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +0000682 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000683
Steve Narofff59e17e2008-12-24 20:59:21 +0000684 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +0000685 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +0000686 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000687
Sean Huntbbd37c62009-11-21 08:43:09 +0000688 // If C++0x attributes exist here, parse them.
689 // FIXME: Are we consistent with the ordering of parsing of different
690 // styles of attributes?
John McCall7f040a92010-12-24 02:08:15 +0000691 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Douglas Gregorb117a602009-09-04 05:53:02 +0000693 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
694 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
695 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000696 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000697 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
698 // properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000699 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000700 Tok.setKind(tok::identifier);
701 }
702
703 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
704 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
705 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump1eb44332009-09-09 15:08:12 +0000706 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregorb117a602009-09-04 05:53:02 +0000707 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
708 // properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000709 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000710 Tok.setKind(tok::identifier);
711 }
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000713 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000714 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000715 if (getLang().CPlusPlus) {
716 // "FOO : BAR" is not a potential typo for "FOO::BAR".
717 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000718
John McCallb3d87482010-08-24 05:47:05 +0000719 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true))
John McCall207014e2010-07-30 06:26:29 +0000720 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +0000721 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +0000722 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
723 Diag(Tok, diag::err_expected_ident);
724 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000725
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000726 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
727
Douglas Gregorcc636682009-02-17 23:15:12 +0000728 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000729 IdentifierInfo *Name = 0;
730 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000731 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000732 if (Tok.is(tok::identifier)) {
733 Name = Tok.getIdentifierInfo();
734 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000735
Douglas Gregor5ee37342010-05-30 22:30:21 +0000736 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000737 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000738 // Eat the template argument list and try to continue parsing this as
739 // a class (or template thereof).
740 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000741 SourceLocation LAngleLoc, RAngleLoc;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000742 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000743 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000744 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000745 // We couldn't parse the template argument list at all, so don't
746 // try to give any location information for the list.
747 LAngleLoc = RAngleLoc = SourceLocation();
748 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000749
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000750 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000751 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000752 << (TagType == DeclSpec::TST_class? 0
753 : TagType == DeclSpec::TST_struct? 1
754 : 2)
755 << Name
756 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000757
758 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000759 // we've removed its template argument list.
760 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
761 if (TemplateParams && TemplateParams->size() > 1) {
762 TemplateParams->pop_back();
763 } else {
764 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000765 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000766 = ParsedTemplateInfo::NonTemplate;
767 }
768 } else if (TemplateInfo.Kind
769 == ParsedTemplateInfo::ExplicitInstantiation) {
770 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000771 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000772 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000773 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000774 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000775 = SourceLocation();
776 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
777 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000778 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000779 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000780 } else if (Tok.is(tok::annot_template_id)) {
781 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
782 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000783
Douglas Gregorc45c2322009-03-31 00:43:58 +0000784 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000785 // The template-name in the simple-template-id refers to
786 // something other than a class template. Give an appropriate
787 // error message and skip to the ';'.
788 SourceRange Range(NameLoc);
789 if (SS.isNotEmpty())
790 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000791
Douglas Gregor39a8de12009-02-25 19:37:18 +0000792 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
793 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Douglas Gregor39a8de12009-02-25 19:37:18 +0000795 DS.SetTypeSpecError();
796 SkipUntil(tok::semi, false, true);
797 TemplateId->Destroy();
Chandler Carruth926c4b42010-06-28 08:39:25 +0000798 if (SuppressingAccessChecks)
799 Actions.ActOnStopSuppressingAccessChecks();
800
Douglas Gregor39a8de12009-02-25 19:37:18 +0000801 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000802 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000803 }
804
Chandler Carruth926c4b42010-06-28 08:39:25 +0000805 // As soon as we're finished parsing the class's template-id, turn access
806 // checking back on.
807 if (SuppressingAccessChecks)
808 Actions.ActOnStopSuppressingAccessChecks();
809
John McCall67d1a672009-08-06 02:15:43 +0000810 // There are four options here. If we have 'struct foo;', then this
811 // is either a forward declaration or a friend declaration, which
Anders Carlssoncc54d592011-01-22 16:56:46 +0000812 // have to be treated differently. If we have 'struct foo {...',
813 // 'struct foo :...' or 'struct foo <class-virt-specifier>' then this is a
814 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000815 // However, in some contexts, things look like declarations but are just
816 // references, e.g.
817 // new struct s;
818 // or
819 // &T::operator struct s;
820 // For these, SuppressDeclarations is true.
John McCallf312b1e2010-08-26 23:41:50 +0000821 Sema::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000822 if (SuppressDeclarations)
John McCallf312b1e2010-08-26 23:41:50 +0000823 TUK = Sema::TUK_Reference;
Anders Carlssoncc54d592011-01-22 16:56:46 +0000824 else if (Tok.is(tok::l_brace) ||
825 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
826 isCXX0XClassVirtSpecifier() != ClassVirtSpecifiers::CVS_None) {
Douglas Gregord85bea22009-09-26 06:47:28 +0000827 if (DS.isFriendSpecified()) {
828 // C++ [class.friend]p2:
829 // A class shall not be defined in a friend declaration.
830 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
831 << SourceRange(DS.getFriendSpecLoc());
832
833 // Skip everything up to the semicolon, so that this looks like a proper
834 // friend class (or template thereof) declaration.
835 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +0000836 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +0000837 } else {
838 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +0000839 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +0000840 }
841 } else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +0000842 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000843 else
John McCallf312b1e2010-08-26 23:41:50 +0000844 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000845
John McCall207014e2010-07-30 06:26:29 +0000846 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +0000847 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +0000848 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
849 // We have a declaration or reference to an anonymous class.
850 Diag(StartLoc, diag::err_anon_type_definition)
851 << DeclSpec::getSpecifierName(TagType);
852 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000853
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000854 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000855
856 if (TemplateId)
857 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000858 return;
859 }
860
Douglas Gregorddc29e12009-02-06 22:42:48 +0000861 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +0000862 DeclResult TagOrTempResult = true; // invalid
863 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000864
Douglas Gregor402abb52009-05-28 23:31:59 +0000865 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000866 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000867 // Explicit specialization, class template partial specialization,
868 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000869 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000870 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000871 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000872 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000873 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000874 // This is an explicit instantiation of a class template.
875 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000876 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000877 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000878 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000879 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000880 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000881 SS,
John McCall2b5289b2010-08-23 07:28:44 +0000882 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000883 TemplateId->TemplateNameLoc,
884 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000885 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000886 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +0000887 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +0000888
889 // Friend template-ids are treated as references unless
890 // they have template headers, in which case they're ill-formed
891 // (FIXME: "template <class T> friend class A<T>::B<int>;").
892 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +0000893 } else if (TUK == Sema::TUK_Reference ||
894 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +0000895 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
John McCallc4e70192009-09-11 04:59:25 +0000896 TypeResult
John McCall2b5289b2010-08-23 07:28:44 +0000897 = Actions.ActOnTemplateIdType(TemplateId->Template,
John McCall6b2becf2009-09-08 17:47:29 +0000898 TemplateId->TemplateNameLoc,
899 TemplateId->LAngleLoc,
900 TemplateArgsPtr,
John McCall6b2becf2009-09-08 17:47:29 +0000901 TemplateId->RAngleLoc);
902
Craig Silverstein45ab4b52010-11-18 08:32:02 +0000903 TypeResult = Actions.ActOnTagTemplateIdType(SS, TypeResult, TUK,
John McCallc4e70192009-09-11 04:59:25 +0000904 TagType, StartLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000905 } else {
906 // This is an explicit specialization or a class template
907 // partial specialization.
908 TemplateParameterLists FakedParamLists;
909
910 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
911 // This looks like an explicit instantiation, because we have
912 // something like
913 //
914 // template class Foo<X>
915 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000916 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000917 // meant to be an explicit specialization, but the user forgot
918 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +0000919 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000920
Mike Stump1eb44332009-09-09 15:08:12 +0000921 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000922 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000923 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000924 diag::err_explicit_instantiation_with_definition)
925 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +0000926 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000927
928 // Create a fake template parameter list that contains only
929 // "template<>", so that we treat this construct as a class
930 // template specialization.
931 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000932 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000933 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000934 LAngleLoc,
935 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000936 LAngleLoc));
937 TemplateParams = &FakedParamLists;
938 }
939
940 // Build the class template specialization.
941 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000942 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000943 StartLoc, SS,
John McCall2b5289b2010-08-23 07:28:44 +0000944 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000945 TemplateId->TemplateNameLoc,
946 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000947 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000948 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +0000949 attrs.getList(),
John McCallf312b1e2010-08-26 23:41:50 +0000950 MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000951 TemplateParams? &(*TemplateParams)[0] : 0,
952 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000953 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000954 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000955 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000956 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000957 // Explicit instantiation of a member of a class template
958 // specialization, e.g.,
959 //
960 // template struct Outer<int>::Inner;
961 //
962 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000963 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000964 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000965 TemplateInfo.TemplateLoc,
966 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +0000967 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +0000968 } else if (TUK == Sema::TUK_Friend &&
969 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
970 TagOrTempResult =
971 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
972 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000973 Name, NameLoc, attrs.getList(),
John McCall9a34edb2010-10-19 01:40:49 +0000974 MultiTemplateParamsArg(Actions,
975 TemplateParams? &(*TemplateParams)[0] : 0,
976 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000977 } else {
978 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000979 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000980 // FIXME: Diagnose this particular error.
981 }
982
John McCallc4e70192009-09-11 04:59:25 +0000983 bool IsDependent = false;
984
John McCalla25c4082010-10-19 18:40:57 +0000985 // Don't pass down template parameter lists if this is just a tag
986 // reference. For example, we don't need the template parameters here:
987 // template <class T> class A *makeA(T t);
988 MultiTemplateParamsArg TParams;
989 if (TUK != Sema::TUK_Reference && TemplateParams)
990 TParams =
991 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
992
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000993 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +0000994 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +0000995 SS, Name, NameLoc, attrs.getList(), AS,
John McCalla25c4082010-10-19 18:40:57 +0000996 TParams, Owned, IsDependent, false,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000997 false, clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +0000998
999 // If ActOnTag said the type was dependent, try again with the
1000 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001001 if (IsDependent) {
1002 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001003 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001004 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001005 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001006 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001007
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001008 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001009 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001010 assert(Tok.is(tok::l_brace) ||
Anders Carlssoncc54d592011-01-22 16:56:46 +00001011 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
1012 isCXX0XClassVirtSpecifier() != ClassVirtSpecifiers::CVS_None);
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001013 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +00001014 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001015 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001016 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001017 }
1018
John McCallb3d87482010-08-24 05:47:05 +00001019 // FIXME: The DeclSpec should keep the locations of both the keyword and the
1020 // name (if there is one).
1021 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
1022
1023 const char *PrevSpec = 0;
1024 unsigned DiagID;
1025 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001026 if (!TypeResult.isInvalid()) {
John McCallb3d87482010-08-24 05:47:05 +00001027 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc,
1028 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001029 } else if (!TagOrTempResult.isInvalid()) {
John McCallb3d87482010-08-24 05:47:05 +00001030 Result = DS.SetTypeSpecType(TagType, TSTLoc, PrevSpec, DiagID,
1031 TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001032 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001033 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001034 return;
1035 }
Mike Stump1eb44332009-09-09 15:08:12 +00001036
John McCallb3d87482010-08-24 05:47:05 +00001037 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001038 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001039
Chris Lattner4ed5d912010-02-02 01:23:29 +00001040 // At this point, we've successfully parsed a class-specifier in 'definition'
1041 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1042 // going to look at what comes after it to improve error recovery. If an
1043 // impossible token occurs next, we assume that the programmer forgot a ; at
1044 // the end of the declaration and recover that way.
1045 //
1046 // This switch enumerates the valid "follow" set for definition.
John McCallf312b1e2010-08-26 23:41:50 +00001047 if (TUK == Sema::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001048 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001049 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001050 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001051 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +00001052 case tok::star: // struct foo {...} * P;
1053 case tok::amp: // struct foo {...} & R = ...
1054 case tok::identifier: // struct foo {...} V ;
1055 case tok::r_paren: //(struct foo {...} ) {4}
1056 case tok::annot_cxxscope: // struct foo {...} a:: b;
1057 case tok::annot_typename: // struct foo {...} a ::b;
1058 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +00001059 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +00001060 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001061 ExpectedSemi = false;
1062 break;
1063 // Type qualifiers
1064 case tok::kw_const: // struct foo {...} const x;
1065 case tok::kw_volatile: // struct foo {...} volatile x;
1066 case tok::kw_restrict: // struct foo {...} restrict x;
1067 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +00001068 // Storage-class specifiers
1069 case tok::kw_static: // struct foo {...} static x;
1070 case tok::kw_extern: // struct foo {...} extern x;
1071 case tok::kw_typedef: // struct foo {...} typedef x;
1072 case tok::kw_register: // struct foo {...} register x;
1073 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregor33f99242010-05-17 18:19:56 +00001074 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001075 // As shown above, type qualifiers and storage class specifiers absolutely
1076 // can occur after class specifiers according to the grammar. However,
1077 // almost noone actually writes code like this. If we see one of these,
1078 // it is much more likely that someone missed a semi colon and the
1079 // type/storage class specifier we're seeing is part of the *next*
1080 // intended declaration, as in:
1081 //
1082 // struct foo { ... }
1083 // typedef int X;
1084 //
1085 // We'd really like to emit a missing semicolon error instead of emitting
1086 // an error on the 'int' saying that you can't have two type specifiers in
1087 // the same declaration of X. Because of this, we look ahead past this
1088 // token to see if it's a type specifier. If so, we know the code is
1089 // otherwise invalid, so we can produce the expected semi error.
1090 if (!isKnownToBeTypeSpecifier(NextToken()))
1091 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001092 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001093
1094 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001095 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001096 if (!getLang().CPlusPlus)
1097 ExpectedSemi = false;
1098 break;
1099 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001100
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001101 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001102 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1103 TagType == DeclSpec::TST_class ? "class"
1104 : TagType == DeclSpec::TST_struct? "struct" : "union");
1105 // Push this token back into the preprocessor and change our current token
1106 // to ';' so that the rest of the code recovers as though there were an
1107 // ';' after the definition.
1108 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001109 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001110 }
1111 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001112}
1113
Mike Stump1eb44332009-09-09 15:08:12 +00001114/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001115///
1116/// base-clause : [C++ class.derived]
1117/// ':' base-specifier-list
1118/// base-specifier-list:
1119/// base-specifier '...'[opt]
1120/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001121void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001122 assert(Tok.is(tok::colon) && "Not a base clause");
1123 ConsumeToken();
1124
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001125 // Build up an array of parsed base specifiers.
John McCallca0408f2010-08-23 06:44:23 +00001126 llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001127
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001128 while (true) {
1129 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001130 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001131 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001132 // Skip the rest of this base specifier, up until the comma or
1133 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001134 SkipUntil(tok::comma, tok::l_brace, true, true);
1135 } else {
1136 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001137 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001138 }
1139
1140 // If the next token is a comma, consume it and keep reading
1141 // base-specifiers.
1142 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001144 // Consume the comma.
1145 ConsumeToken();
1146 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001147
1148 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001149 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001150}
1151
1152/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1153/// one entry in the base class list of a class specifier, for example:
1154/// class foo : public bar, virtual private baz {
1155/// 'public bar' and 'virtual private baz' are each base-specifiers.
1156///
1157/// base-specifier: [C++ class.derived]
1158/// ::[opt] nested-name-specifier[opt] class-name
1159/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1160/// class-name
1161/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1162/// class-name
John McCalld226f652010-08-21 09:40:31 +00001163Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001164 bool IsVirtual = false;
1165 SourceLocation StartLoc = Tok.getLocation();
1166
1167 // Parse the 'virtual' keyword.
1168 if (Tok.is(tok::kw_virtual)) {
1169 ConsumeToken();
1170 IsVirtual = true;
1171 }
1172
1173 // Parse an (optional) access specifier.
1174 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001175 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001176 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001178 // Parse the 'virtual' keyword (again!), in case it came after the
1179 // access specifier.
1180 if (Tok.is(tok::kw_virtual)) {
1181 SourceLocation VirtualLoc = ConsumeToken();
1182 if (IsVirtual) {
1183 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001184 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001185 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001186 }
1187
1188 IsVirtual = true;
1189 }
1190
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001191 // Parse optional '::' and optional nested-name-specifier.
1192 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001193 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001194
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001195 // The location of the base class itself.
1196 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001197
1198 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001199 SourceLocation EndLocation;
Douglas Gregor31a19b62009-04-01 21:51:26 +00001200 TypeResult BaseType = ParseClassName(EndLocation, &SS);
1201 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001202 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001204 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1205 // actually part of the base-specifier-list grammar productions, but we
1206 // parse it here for convenience.
1207 SourceLocation EllipsisLoc;
1208 if (Tok.is(tok::ellipsis))
1209 EllipsisLoc = ConsumeToken();
1210
Mike Stump1eb44332009-09-09 15:08:12 +00001211 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001212 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001214 // Notify semantic analysis that we have parsed a complete
1215 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001216 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001217 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001218}
1219
1220/// getAccessSpecifierIfPresent - Determine whether the next token is
1221/// a C++ access-specifier.
1222///
1223/// access-specifier: [C++ class.derived]
1224/// 'private'
1225/// 'protected'
1226/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001227AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001228 switch (Tok.getKind()) {
1229 default: return AS_none;
1230 case tok::kw_private: return AS_private;
1231 case tok::kw_protected: return AS_protected;
1232 case tok::kw_public: return AS_public;
1233 }
1234}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001235
Eli Friedmand33133c2009-07-22 21:45:50 +00001236void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
John McCalld226f652010-08-21 09:40:31 +00001237 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001238 // We just declared a member function. If this member function
1239 // has any default arguments, we'll need to parse them later.
1240 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001241 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001242 = DeclaratorInfo.getFunctionTypeInfo();
Eli Friedmand33133c2009-07-22 21:45:50 +00001243 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1244 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1245 if (!LateMethod) {
1246 // Push this method onto the stack of late-parsed method
1247 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001248 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1249 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001250 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001251
1252 // Add all of the parameters prior to this one (they don't
1253 // have default arguments).
1254 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1255 for (unsigned I = 0; I < ParamIdx; ++I)
1256 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001257 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001258 }
1259
1260 // Add this parameter to the list of parameters (it or may
1261 // not have a default argument).
1262 LateMethod->DefaultArgs.push_back(
1263 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1264 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1265 }
1266 }
1267}
1268
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001269/// isCXX0XVirtSpecifier - Determine whether the next token is a C++0x
1270/// virt-specifier.
1271///
1272/// virt-specifier:
1273/// override
1274/// final
1275/// new
Anders Carlssoncc54d592011-01-22 16:56:46 +00001276VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier() const {
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001277 if (!getLang().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001278 return VirtSpecifiers::VS_None;
1279
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001280 if (Tok.is(tok::kw_new))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001281 return VirtSpecifiers::VS_New;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001282
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001283 if (Tok.is(tok::identifier)) {
1284 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001285
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001286 // Initialize the contextual keywords.
1287 if (!Ident_final) {
1288 Ident_final = &PP.getIdentifierTable().get("final");
1289 Ident_override = &PP.getIdentifierTable().get("override");
1290 }
1291
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001292 if (II == Ident_override)
1293 return VirtSpecifiers::VS_Override;
1294
1295 if (II == Ident_final)
1296 return VirtSpecifiers::VS_Final;
1297 }
1298
1299 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001300}
1301
1302/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1303///
1304/// virt-specifier-seq:
1305/// virt-specifier
1306/// virt-specifier-seq virt-specifier
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001307void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001308 while (true) {
Anders Carlssoncc54d592011-01-22 16:56:46 +00001309 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001310 if (Specifier == VirtSpecifiers::VS_None)
1311 return;
1312
1313 // C++ [class.mem]p8:
1314 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001315 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001316 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001317 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1318 << PrevSpec
1319 << FixItHint::CreateRemoval(Tok.getLocation());
1320
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001321 if (!getLang().CPlusPlus0x)
1322 Diag(Tok.getLocation(), diag::ext_override_control_keyword)
1323 << VirtSpecifiers::getSpecifierName(Specifier);
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001324 ConsumeToken();
1325 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001326}
1327
Anders Carlssoncc54d592011-01-22 16:56:46 +00001328/// isCXX0XClassVirtSpecifier - Determine whether the next token is a C++0x
1329/// class-virt-specifier.
1330///
1331/// class-virt-specifier:
1332/// final
1333/// explicit
1334ClassVirtSpecifiers::Specifier Parser::isCXX0XClassVirtSpecifier() const {
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001335 if (!getLang().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001336 return ClassVirtSpecifiers::CVS_None;
1337
1338 if (Tok.is(tok::kw_explicit))
1339 return ClassVirtSpecifiers::CVS_Explicit;
1340
1341 if (Tok.is(tok::identifier)) {
1342 IdentifierInfo *II = Tok.getIdentifierInfo();
1343
1344 // Initialize the contextual keywords.
1345 if (!Ident_final) {
1346 Ident_final = &PP.getIdentifierTable().get("final");
1347 Ident_override = &PP.getIdentifierTable().get("override");
1348 }
1349
1350 if (II == Ident_final)
1351 return ClassVirtSpecifiers::CVS_Final;
1352 }
1353
1354 return ClassVirtSpecifiers::CVS_None;
1355}
1356
1357/// ParseOptionalCXX0XClassVirtSpecifierSeq - Parse a class-virt-specifier-seq.
1358///
1359/// class-virt-specifier-seq:
1360/// class-virt-specifier
1361/// class-virt-specifier-seq class-virt-specifier
1362void Parser::ParseOptionalCXX0XClassVirtSpecifierSeq(ClassVirtSpecifiers &CVS) {
1363 while (true) {
1364 ClassVirtSpecifiers::Specifier Specifier = isCXX0XClassVirtSpecifier();
1365 if (Specifier == ClassVirtSpecifiers::CVS_None)
1366 return;
1367
1368 // C++ [class]p1:
1369 // A class-virt-specifier-seq shall contain at most one of each
1370 // class-virt-specifier.
1371 const char *PrevSpec = 0;
1372 if (CVS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
1373 Diag(Tok.getLocation(), diag::err_duplicate_class_virt_specifier)
1374 << PrevSpec
1375 << FixItHint::CreateRemoval(Tok.getLocation());
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001376
1377 if (!getLang().CPlusPlus0x)
1378 Diag(Tok.getLocation(), diag::ext_override_control_keyword)
1379 << ClassVirtSpecifiers::getSpecifierName(Specifier);
1380
Anders Carlssoncc54d592011-01-22 16:56:46 +00001381 ConsumeToken();
1382 }
1383}
1384
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001385/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1386///
1387/// member-declaration:
1388/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1389/// function-definition ';'[opt]
1390/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1391/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001392/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001393/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001394/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001395///
1396/// member-declarator-list:
1397/// member-declarator
1398/// member-declarator-list ',' member-declarator
1399///
1400/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001401/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001402/// declarator constant-initializer[opt]
1403/// identifier[opt] ':' constant-expression
1404///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001405/// virt-specifier-seq:
1406/// virt-specifier
1407/// virt-specifier-seq virt-specifier
1408///
1409/// virt-specifier:
1410/// override
1411/// final
1412/// new
1413///
Sebastian Redle2b68332009-04-12 17:16:29 +00001414/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001415/// '= 0'
1416///
1417/// constant-initializer:
1418/// '=' constant-expression
1419///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001420void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCallc9068d72010-07-16 08:13:16 +00001421 const ParsedTemplateInfo &TemplateInfo,
1422 ParsingDeclRAIIObject *TemplateDiags) {
John McCall60fa3cf2009-12-11 02:10:03 +00001423 // Access declarations.
1424 if (!TemplateInfo.Kind &&
1425 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001426 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001427 Tok.is(tok::annot_cxxscope)) {
1428 bool isAccessDecl = false;
1429 if (NextToken().is(tok::identifier))
1430 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1431 else
1432 isAccessDecl = NextToken().is(tok::kw_operator);
1433
1434 if (isAccessDecl) {
1435 // Collect the scope specifier token we annotated earlier.
1436 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001437 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
John McCall60fa3cf2009-12-11 02:10:03 +00001438
1439 // Try to parse an unqualified-id.
1440 UnqualifiedId Name;
John McCallb3d87482010-08-24 05:47:05 +00001441 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001442 SkipUntil(tok::semi);
1443 return;
1444 }
1445
1446 // TODO: recover from mistakenly-qualified operator declarations.
1447 if (ExpectAndConsume(tok::semi,
1448 diag::err_expected_semi_after,
1449 "access declaration",
1450 tok::semi))
1451 return;
1452
Douglas Gregor23c94db2010-07-02 17:43:08 +00001453 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001454 false, SourceLocation(),
1455 SS, Name,
1456 /* AttrList */ 0,
1457 /* IsTypeName */ false,
1458 SourceLocation());
1459 return;
1460 }
1461 }
1462
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001463 // static_assert-declaration
Chris Lattner682bf922009-03-29 16:50:03 +00001464 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001465 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001466 SourceLocation DeclEnd;
1467 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001468 return;
1469 }
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Chris Lattner682bf922009-03-29 16:50:03 +00001471 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001472 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001473 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001474 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001475 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001476 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001477 return;
1478 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001479
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001480 // Handle: member-declaration ::= '__extension__' member-declaration
1481 if (Tok.is(tok::kw___extension__)) {
1482 // __extension__ silences extension warnings in the subexpression.
1483 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1484 ConsumeToken();
John McCallc9068d72010-07-16 08:13:16 +00001485 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001486 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001487
Chris Lattner4ed5d912010-02-02 01:23:29 +00001488 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1489 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001490 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001491
John McCall7f040a92010-12-24 02:08:15 +00001492 ParsedAttributesWithRange attrs;
Sean Huntbbd37c62009-11-21 08:43:09 +00001493 // Optional C++0x attribute-specifier
John McCall7f040a92010-12-24 02:08:15 +00001494 MaybeParseCXX0XAttributes(attrs);
1495 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001496
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001497 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001498 // FIXME: Check for template aliases
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001499
John McCall7f040a92010-12-24 02:08:15 +00001500 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001502 // Eat 'using'.
1503 SourceLocation UsingLoc = ConsumeToken();
1504
1505 if (Tok.is(tok::kw_namespace)) {
1506 Diag(UsingLoc, diag::err_using_namespace_in_class);
1507 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001508 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001509 SourceLocation DeclEnd;
1510 // Otherwise, it must be using-declaration.
John McCall78b81052010-11-10 02:40:36 +00001511 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1512 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001513 }
1514 return;
1515 }
1516
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001517 // decl-specifier-seq:
1518 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001519 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00001520 DS.takeAttributesFrom(attrs);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001521 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001522
John McCallf312b1e2010-08-26 23:41:50 +00001523 MultiTemplateParamsArg TemplateParams(Actions,
John McCalldd4a3b02009-09-16 22:47:08 +00001524 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1525 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1526
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001527 if (Tok.is(tok::semi)) {
1528 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001529 Decl *TheDecl =
John McCallc9068d72010-07-16 08:13:16 +00001530 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
1531 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001532 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001533 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001534
John McCall54abf7d2009-11-04 02:18:39 +00001535 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00001536 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001537
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001538 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001539 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1540 ColonProtectionRAIIObject X(*this);
1541
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001542 // Parse the first declarator.
1543 ParseDeclarator(DeclaratorInfo);
1544 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001545 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001546 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001547 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001548 if (Tok.is(tok::semi))
1549 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001550 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001551 }
1552
Nico Weber48673472011-01-28 06:07:34 +00001553 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1554
John Thompson1b2fc0f2009-11-25 22:58:06 +00001555 // If attributes exist after the declarator, but before an '{', parse them.
John McCall7f040a92010-12-24 02:08:15 +00001556 MaybeParseGNUAttributes(DeclaratorInfo);
John Thompson1b2fc0f2009-11-25 22:58:06 +00001557
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001558 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001559 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001560 || (DeclaratorInfo.isFunctionDeclarator() &&
1561 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001562 if (!DeclaratorInfo.isFunctionDeclarator()) {
1563 Diag(Tok, diag::err_func_def_no_params);
1564 ConsumeBrace();
1565 SkipUntil(tok::r_brace, true);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001566
1567 // Consume the optional ';'
1568 if (Tok.is(tok::semi))
1569 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001570 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001571 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001572
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001573 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1574 Diag(Tok, diag::err_function_declared_typedef);
1575 // This recovery skips the entire function body. It would be nice
1576 // to simply call ParseCXXInlineMethodDef() below, however Sema
1577 // assumes the declarator represents a function, not a typedef.
1578 ConsumeBrace();
1579 SkipUntil(tok::r_brace, true);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001580
1581 // Consume the optional ';'
1582 if (Tok.is(tok::semi))
1583 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001584 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001585 }
1586
Nico Weber48673472011-01-28 06:07:34 +00001587 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo, VS);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001588 // Consume the optional ';'
1589 if (Tok.is(tok::semi))
1590 ConsumeToken();
1591
Chris Lattner682bf922009-03-29 16:50:03 +00001592 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001593 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001594 }
1595
1596 // member-declarator-list:
1597 // member-declarator
1598 // member-declarator-list ',' member-declarator
1599
John McCalld226f652010-08-21 09:40:31 +00001600 llvm::SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00001601 ExprResult BitfieldSize;
1602 ExprResult Init;
Sebastian Redle2b68332009-04-12 17:16:29 +00001603 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001604
1605 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001606 // member-declarator:
1607 // declarator pure-specifier[opt]
1608 // declarator constant-initializer[opt]
1609 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001610 if (Tok.is(tok::colon)) {
1611 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001612 BitfieldSize = ParseConstantExpression();
1613 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001614 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001615 }
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001617 ParseOptionalCXX0XVirtSpecifierSeq(VS);
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001618
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001619 // pure-specifier:
1620 // '= 0'
1621 //
1622 // constant-initializer:
1623 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001624 //
1625 // defaulted/deleted function-definition:
1626 // '=' 'default' [TODO]
1627 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001628 if (Tok.is(tok::equal)) {
1629 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001630 if (Tok.is(tok::kw_delete)) {
1631 if (!getLang().CPlusPlus0x)
1632 Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
Sebastian Redle2b68332009-04-12 17:16:29 +00001633 ConsumeToken();
1634 Deleted = true;
1635 } else {
1636 Init = ParseInitializer();
1637 if (Init.isInvalid())
1638 SkipUntil(tok::comma, true, true);
1639 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001640 }
1641
Chris Lattnere6563252010-06-13 05:34:18 +00001642 // If a simple-asm-expr is present, parse it.
1643 if (Tok.is(tok::kw_asm)) {
1644 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001645 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00001646 if (AsmLabel.isInvalid())
1647 SkipUntil(tok::comma, true, true);
1648
1649 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1650 DeclaratorInfo.SetRangeEnd(Loc);
1651 }
1652
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001653 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001654 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001655
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001656 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001657 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001658 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001659
John McCalld226f652010-08-21 09:40:31 +00001660 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00001661 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001662 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00001663 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCallbbbcdd92009-09-11 21:02:39 +00001664 /*IsDefinition*/ false,
1665 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001666 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001667 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00001668 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001669 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001670 BitfieldSize.release(),
Anders Carlsson69a87352011-01-20 03:57:25 +00001671 VS, Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001672 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001673 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001674 }
Chris Lattner682bf922009-03-29 16:50:03 +00001675 if (ThisDecl)
1676 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001677
Douglas Gregor72b505b2008-12-16 21:30:33 +00001678 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001679 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001680 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001681 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001682 }
1683
John McCall54abf7d2009-11-04 02:18:39 +00001684 DeclaratorInfo.complete(ThisDecl);
1685
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001686 // If we don't have a comma, it is either the end of the list (a ';')
1687 // or an error, bail out.
1688 if (Tok.isNot(tok::comma))
1689 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001691 // Consume the comma.
1692 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001694 // Parse the next declarator.
1695 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00001696 VS.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001697 BitfieldSize = 0;
1698 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001699 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001701 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00001702 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001703
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001704 if (Tok.isNot(tok::colon))
1705 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001706 }
1707
Chris Lattnerae50d502010-02-02 00:43:15 +00001708 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1709 // Skip to end of block or statement.
1710 SkipUntil(tok::r_brace, true, true);
1711 // If we stopped at a ';', eat it.
1712 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001713 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001714 }
1715
Douglas Gregor23c94db2010-07-02 17:43:08 +00001716 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00001717 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001718}
1719
1720/// ParseCXXMemberSpecification - Parse the class definition.
1721///
1722/// member-specification:
1723/// member-declaration member-specification[opt]
1724/// access-specifier ':' member-specification[opt]
1725///
1726void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001727 unsigned TagType, Decl *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001728 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001729 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001730 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001731
John McCallf312b1e2010-08-26 23:41:50 +00001732 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1733 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001734
Douglas Gregor26997fd2010-01-16 20:52:59 +00001735 // Determine whether this is a non-nested class. Note that local
1736 // classes are *not* considered to be nested classes.
1737 bool NonNestedClass = true;
1738 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001739 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001740 if (S->isClassScope()) {
1741 // We're inside a class scope, so this is a nested class.
1742 NonNestedClass = false;
1743 break;
1744 }
1745
1746 if ((S->getFlags() & Scope::FnScope)) {
1747 // If we're in a function or function template declared in the
1748 // body of a class, then this is a local class rather than a
1749 // nested class.
1750 const Scope *Parent = S->getParent();
1751 if (Parent->isTemplateParamScope())
1752 Parent = Parent->getParent();
1753 if (Parent->isClassScope())
1754 break;
1755 }
1756 }
1757 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001758
1759 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001760 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001761
Douglas Gregor6569d682009-05-27 23:11:45 +00001762 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001763 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001764
Douglas Gregorddc29e12009-02-06 22:42:48 +00001765 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001766 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001767
Anders Carlssoncc54d592011-01-22 16:56:46 +00001768 ClassVirtSpecifiers CVS;
1769 ParseOptionalCXX0XClassVirtSpecifierSeq(CVS);
1770
John McCallbd0dfa52009-12-19 21:48:58 +00001771 if (Tok.is(tok::colon)) {
1772 ParseBaseClause(TagDecl);
1773
1774 if (!Tok.is(tok::l_brace)) {
1775 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00001776
1777 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001778 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001779 return;
1780 }
1781 }
1782
1783 assert(Tok.is(tok::l_brace));
1784
1785 SourceLocation LBraceLoc = ConsumeBrace();
1786
John McCall42a4f662010-05-28 08:11:17 +00001787 if (TagDecl)
Anders Carlssondfc2f102011-01-22 17:51:53 +00001788 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, CVS,
1789 LBraceLoc);
John McCallf9368152009-12-20 07:58:13 +00001790
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001791 // C++ 11p3: Members of a class defined with the keyword class are private
1792 // by default. Members of a class defined with the keywords struct or union
1793 // are public by default.
1794 AccessSpecifier CurAS;
1795 if (TagType == DeclSpec::TST_class)
1796 CurAS = AS_private;
1797 else
1798 CurAS = AS_public;
1799
Douglas Gregor07976d22010-06-21 22:31:09 +00001800 SourceLocation RBraceLoc;
1801 if (TagDecl) {
1802 // While we still have something to read, read the member-declarations.
1803 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1804 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001805
Douglas Gregor07976d22010-06-21 22:31:09 +00001806 // Check for extraneous top-level semicolon.
1807 if (Tok.is(tok::semi)) {
1808 Diag(Tok, diag::ext_extra_struct_semi)
1809 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1810 << FixItHint::CreateRemoval(Tok.getLocation());
1811 ConsumeToken();
1812 continue;
1813 }
1814
1815 AccessSpecifier AS = getAccessSpecifierIfPresent();
1816 if (AS != AS_none) {
1817 // Current token is a C++ access specifier.
1818 CurAS = AS;
1819 SourceLocation ASLoc = Tok.getLocation();
1820 ConsumeToken();
1821 if (Tok.is(tok::colon))
1822 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1823 else
1824 Diag(Tok, diag::err_expected_colon);
1825 ConsumeToken();
1826 continue;
1827 }
1828
1829 // FIXME: Make sure we don't have a template here.
1830
1831 // Parse all the comma separated declarators.
1832 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001833 }
1834
Douglas Gregor07976d22010-06-21 22:31:09 +00001835 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1836 } else {
1837 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001838 }
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001840 // If attributes exist after class contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001841 ParsedAttributes attrs;
1842 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001843
John McCall42a4f662010-05-28 08:11:17 +00001844 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001845 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall42a4f662010-05-28 08:11:17 +00001846 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00001847 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001848
1849 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1850 // complete within function bodies, default arguments,
1851 // exception-specifications, and constructor ctor-initializers (including
1852 // such things in nested classes).
1853 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001854 // FIXME: Only function bodies and constructor ctor-initializers are
1855 // parsed correctly, fix the rest.
Douglas Gregor07976d22010-06-21 22:31:09 +00001856 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001857 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001858 // are complete and we can parse the delayed portions of method
1859 // declarations and the lexed inline method definitions.
Douglas Gregore0cc0472010-06-16 23:45:56 +00001860 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregor6569d682009-05-27 23:11:45 +00001861 ParseLexedMethodDeclarations(getCurrentClass());
1862 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00001863 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001864 }
1865
John McCall42a4f662010-05-28 08:11:17 +00001866 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001867 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCalldb7bb4a2010-03-17 00:38:33 +00001868
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001869 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001870 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001871 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001872}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001873
1874/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1875/// which explicitly initializes the members or base classes of a
1876/// class (C++ [class.base.init]). For example, the three initializers
1877/// after the ':' in the Derived constructor below:
1878///
1879/// @code
1880/// class Base { };
1881/// class Derived : Base {
1882/// int x;
1883/// float f;
1884/// public:
1885/// Derived(float f) : Base(), x(17), f(f) { }
1886/// };
1887/// @endcode
1888///
Mike Stump1eb44332009-09-09 15:08:12 +00001889/// [C++] ctor-initializer:
1890/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001891///
Mike Stump1eb44332009-09-09 15:08:12 +00001892/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00001893/// mem-initializer ...[opt]
1894/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00001895void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001896 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1897
1898 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Sean Huntcbb67482011-01-08 20:30:50 +00001900 llvm::SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001901 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001902
Douglas Gregor7ad83902008-11-05 04:29:56 +00001903 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00001904 if (Tok.is(tok::code_completion)) {
1905 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
1906 MemInitializers.data(),
1907 MemInitializers.size());
1908 ConsumeCodeCompletionToken();
1909 } else {
1910 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
1911 if (!MemInit.isInvalid())
1912 MemInitializers.push_back(MemInit.get());
1913 else
1914 AnyErrors = true;
1915 }
1916
Douglas Gregor7ad83902008-11-05 04:29:56 +00001917 if (Tok.is(tok::comma))
1918 ConsumeToken();
1919 else if (Tok.is(tok::l_brace))
1920 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00001921 // If the next token looks like a base or member initializer, assume that
1922 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00001923 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
1924 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
1925 Diag(Loc, diag::err_ctor_init_missing_comma)
1926 << FixItHint::CreateInsertion(Loc, ", ");
1927 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001928 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001929 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001930 SkipUntil(tok::l_brace, true, true);
1931 break;
1932 }
1933 } while (true);
1934
Mike Stump1eb44332009-09-09 15:08:12 +00001935 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001936 MemInitializers.data(), MemInitializers.size(),
1937 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001938}
1939
1940/// ParseMemInitializer - Parse a C++ member initializer, which is
1941/// part of a constructor initializer that explicitly initializes one
1942/// member or base class (C++ [class.base.init]). See
1943/// ParseConstructorInitializer for an example.
1944///
1945/// [C++] mem-initializer:
1946/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001947///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001948/// [C++] mem-initializer-id:
1949/// '::'[opt] nested-name-specifier[opt] class-name
1950/// identifier
John McCalld226f652010-08-21 09:40:31 +00001951Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001952 // parse '::'[opt] nested-name-specifier[opt]
1953 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001954 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
1955 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00001956 if (Tok.is(tok::annot_template_id)) {
1957 TemplateIdAnnotation *TemplateId
1958 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001959 if (TemplateId->Kind == TNK_Type_template ||
1960 TemplateId->Kind == TNK_Dependent_template_name) {
Fariborz Jahanian96174332009-07-01 19:21:19 +00001961 AnnotateTemplateIdTokenAsType(&SS);
1962 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00001963 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001964 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001965 }
1966 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001967 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001968 return true;
1969 }
Mike Stump1eb44332009-09-09 15:08:12 +00001970
Douglas Gregor7ad83902008-11-05 04:29:56 +00001971 // Get the identifier. This may be a member name or a class name,
1972 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001973 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001974 SourceLocation IdLoc = ConsumeToken();
1975
1976 // Parse the '('.
1977 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001978 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001979 return true;
1980 }
1981 SourceLocation LParenLoc = ConsumeParen();
1982
1983 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001984 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001985 CommaLocsTy CommaLocs;
1986 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1987 SkipUntil(tok::r_paren);
1988 return true;
1989 }
1990
1991 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1992
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00001993 SourceLocation EllipsisLoc;
1994 if (Tok.is(tok::ellipsis))
1995 EllipsisLoc = ConsumeToken();
1996
Douglas Gregor23c94db2010-07-02 17:43:08 +00001997 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanian96174332009-07-01 19:21:19 +00001998 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00001999 LParenLoc, ArgExprs.take(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002000 ArgExprs.size(), RParenLoc,
2001 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002002}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002003
2004/// ParseExceptionSpecification - Parse a C++ exception-specification
2005/// (C++ [except.spec]).
2006///
Douglas Gregora4745612008-12-01 18:00:20 +00002007/// exception-specification:
2008/// 'throw' '(' type-id-list [opt] ')'
2009/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002010///
Douglas Gregora4745612008-12-01 18:00:20 +00002011/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002012/// type-id ... [opt]
2013/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002014///
Sebastian Redl7dc81342009-04-29 17:30:04 +00002015bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc,
John McCallb3d87482010-08-24 05:47:05 +00002016 llvm::SmallVectorImpl<ParsedType>
Sebastian Redlef65f062009-05-29 18:02:33 +00002017 &Exceptions,
John McCallb3d87482010-08-24 05:47:05 +00002018 llvm::SmallVectorImpl<SourceRange>
Sebastian Redlef65f062009-05-29 18:02:33 +00002019 &Ranges,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002020 bool &hasAnyExceptionSpec) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002021 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002022
Jeffrey Yasskindec09842011-01-18 02:00:16 +00002023 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002024
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002025 if (!Tok.is(tok::l_paren)) {
2026 return Diag(Tok, diag::err_expected_lparen_after) << "throw";
2027 }
2028 SourceLocation LParenLoc = ConsumeParen();
2029
Douglas Gregora4745612008-12-01 18:00:20 +00002030 // Parse throw(...), a Microsoft extension that means "this function
2031 // can throw anything".
2032 if (Tok.is(tok::ellipsis)) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002033 hasAnyExceptionSpec = true;
Douglas Gregora4745612008-12-01 18:00:20 +00002034 SourceLocation EllipsisLoc = ConsumeToken();
2035 if (!getLang().Microsoft)
2036 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002037 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregora4745612008-12-01 18:00:20 +00002038 return false;
2039 }
2040
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002041 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002042 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002043 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002044 TypeResult Res(ParseTypeName(&Range));
Douglas Gregora04426c2010-12-20 23:57:46 +00002045
2046 if (Tok.is(tok::ellipsis)) {
2047 // C++0x [temp.variadic]p5:
2048 // - In a dynamic-exception-specification (15.4); the pattern is a
2049 // type-id.
2050 SourceLocation Ellipsis = ConsumeToken();
2051 if (!Res.isInvalid())
2052 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2053 }
2054
Sebastian Redlef65f062009-05-29 18:02:33 +00002055 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002056 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002057 Ranges.push_back(Range);
2058 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002059
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002060 if (Tok.is(tok::comma))
2061 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002062 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002063 break;
2064 }
2065
Sebastian Redlab197ba2009-02-09 18:23:29 +00002066 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002067 return false;
2068}
Douglas Gregor6569d682009-05-27 23:11:45 +00002069
Douglas Gregordab60ad2010-10-01 18:44:50 +00002070/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2071/// function declaration.
2072TypeResult Parser::ParseTrailingReturnType() {
2073 assert(Tok.is(tok::arrow) && "expected arrow");
2074
2075 ConsumeToken();
2076
2077 // FIXME: Need to suppress declarations when parsing this typename.
2078 // Otherwise in this function definition:
2079 //
2080 // auto f() -> struct X {}
2081 //
2082 // struct X is parsed as class definition because of the trailing
2083 // brace.
2084
2085 SourceRange Range;
2086 return ParseTypeName(&Range);
2087}
2088
Douglas Gregor6569d682009-05-27 23:11:45 +00002089/// \brief We have just started parsing the definition of a new class,
2090/// so push that class onto our stack of classes that is currently
2091/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002092Sema::ParsingClassState
2093Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002094 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002095 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00002096 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
John McCalleee1d542011-02-14 07:13:47 +00002097 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002098}
2099
2100/// \brief Deallocate the given parsed class and all of its nested
2101/// classes.
2102void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002103 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2104 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002105 delete Class;
2106}
2107
2108/// \brief Pop the top class of the stack of classes that are
2109/// currently being parsed.
2110///
2111/// This routine should be called when we have finished parsing the
2112/// definition of a class, but have not yet popped the Scope
2113/// associated with the class's definition.
2114///
2115/// \returns true if the class we've popped is a top-level class,
2116/// false otherwise.
John McCalleee1d542011-02-14 07:13:47 +00002117void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002118 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002119
John McCalleee1d542011-02-14 07:13:47 +00002120 Actions.PopParsingClass(state);
2121
Douglas Gregor6569d682009-05-27 23:11:45 +00002122 ParsingClass *Victim = ClassStack.top();
2123 ClassStack.pop();
2124 if (Victim->TopLevelClass) {
2125 // Deallocate all of the nested classes of this class,
2126 // recursively: we don't need to keep any of this information.
2127 DeallocateParsedClasses(Victim);
2128 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002129 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002130 assert(!ClassStack.empty() && "Missing top-level class?");
2131
Douglas Gregord54eb442010-10-12 16:25:54 +00002132 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002133 // The victim is a nested class, but we will not need to perform
2134 // any processing after the definition of this class since it has
2135 // no members whose handling was delayed. Therefore, we can just
2136 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002137 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002138 return;
2139 }
2140
2141 // This nested class has some members that will need to be processed
2142 // after the top-level class is completely defined. Therefore, add
2143 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002144 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002145 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002146 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00002147}
Sean Huntbbd37c62009-11-21 08:43:09 +00002148
2149/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
2150/// parses standard attributes.
2151///
2152/// [C++0x] attribute-specifier:
2153/// '[' '[' attribute-list ']' ']'
2154///
2155/// [C++0x] attribute-list:
2156/// attribute[opt]
2157/// attribute-list ',' attribute[opt]
2158///
2159/// [C++0x] attribute:
2160/// attribute-token attribute-argument-clause[opt]
2161///
2162/// [C++0x] attribute-token:
2163/// identifier
2164/// attribute-scoped-token
2165///
2166/// [C++0x] attribute-scoped-token:
2167/// attribute-namespace '::' identifier
2168///
2169/// [C++0x] attribute-namespace:
2170/// identifier
2171///
2172/// [C++0x] attribute-argument-clause:
2173/// '(' balanced-token-seq ')'
2174///
2175/// [C++0x] balanced-token-seq:
2176/// balanced-token
2177/// balanced-token-seq balanced-token
2178///
2179/// [C++0x] balanced-token:
2180/// '(' balanced-token-seq ')'
2181/// '[' balanced-token-seq ']'
2182/// '{' balanced-token-seq '}'
2183/// any token but '(', ')', '[', ']', '{', or '}'
John McCall7f040a92010-12-24 02:08:15 +00002184void Parser::ParseCXX0XAttributes(ParsedAttributesWithRange &attrs,
2185 SourceLocation *endLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002186 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
2187 && "Not a C++0x attribute list");
2188
2189 SourceLocation StartLoc = Tok.getLocation(), Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00002190
2191 ConsumeBracket();
2192 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002193
Sean Huntbbd37c62009-11-21 08:43:09 +00002194 if (Tok.is(tok::comma)) {
2195 Diag(Tok.getLocation(), diag::err_expected_ident);
2196 ConsumeToken();
2197 }
2198
2199 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
2200 // attribute not present
2201 if (Tok.is(tok::comma)) {
2202 ConsumeToken();
2203 continue;
2204 }
2205
2206 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
2207 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002208
Sean Huntbbd37c62009-11-21 08:43:09 +00002209 // scoped attribute
2210 if (Tok.is(tok::coloncolon)) {
2211 ConsumeToken();
2212
2213 if (!Tok.is(tok::identifier)) {
2214 Diag(Tok.getLocation(), diag::err_expected_ident);
2215 SkipUntil(tok::r_square, tok::comma, true, true);
2216 continue;
2217 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002218
Sean Huntbbd37c62009-11-21 08:43:09 +00002219 ScopeName = AttrName;
2220 ScopeLoc = AttrLoc;
2221
2222 AttrName = Tok.getIdentifierInfo();
2223 AttrLoc = ConsumeToken();
2224 }
2225
2226 bool AttrParsed = false;
2227 // No scoped names are supported; ideally we could put all non-standard
2228 // attributes into namespaces.
2229 if (!ScopeName) {
2230 switch(AttributeList::getKind(AttrName))
2231 {
2232 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00002233 case AttributeList::AT_carries_dependency:
Anders Carlsson15e14a22011-01-23 21:33:18 +00002234 case AttributeList::AT_noreturn: {
Sean Huntbbd37c62009-11-21 08:43:09 +00002235 if (Tok.is(tok::l_paren)) {
2236 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
2237 << AttrName->getName();
2238 break;
2239 }
2240
John McCall7f040a92010-12-24 02:08:15 +00002241 attrs.add(AttrFactory.Create(AttrName, AttrLoc, 0, AttrLoc, 0,
2242 SourceLocation(), 0, 0, false, true));
Sean Huntbbd37c62009-11-21 08:43:09 +00002243 AttrParsed = true;
2244 break;
2245 }
2246
2247 // One argument; must be a type-id or assignment-expression
2248 case AttributeList::AT_aligned: {
2249 if (Tok.isNot(tok::l_paren)) {
2250 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
2251 << AttrName->getName();
2252 break;
2253 }
2254 SourceLocation ParamLoc = ConsumeParen();
2255
John McCall60d7b3a2010-08-24 06:29:42 +00002256 ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002257
2258 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2259
2260 ExprVector ArgExprs(Actions);
2261 ArgExprs.push_back(ArgExpr.release());
John McCall7f040a92010-12-24 02:08:15 +00002262 attrs.add(AttrFactory.Create(AttrName, AttrLoc, 0, AttrLoc,
2263 0, ParamLoc, ArgExprs.take(), 1,
2264 false, true));
Sean Huntbbd37c62009-11-21 08:43:09 +00002265
2266 AttrParsed = true;
2267 break;
2268 }
2269
2270 // Silence warnings
2271 default: break;
2272 }
2273 }
2274
2275 // Skip the entire parameter clause, if any
2276 if (!AttrParsed && Tok.is(tok::l_paren)) {
2277 ConsumeParen();
2278 // SkipUntil maintains the balancedness of tokens.
2279 SkipUntil(tok::r_paren, false);
2280 }
2281 }
2282
2283 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2284 SkipUntil(tok::r_square, false);
2285 Loc = Tok.getLocation();
2286 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2287 SkipUntil(tok::r_square, false);
2288
John McCall7f040a92010-12-24 02:08:15 +00002289 attrs.Range = SourceRange(StartLoc, Loc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002290}
2291
2292/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2293/// attribute.
2294///
2295/// FIXME: Simply returns an alignof() expression if the argument is a
2296/// type. Ideally, the type should be propagated directly into Sema.
2297///
2298/// [C++0x] 'align' '(' type-id ')'
2299/// [C++0x] 'align' '(' assignment-expression ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002300ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002301 if (isTypeIdInParens()) {
John McCallf312b1e2010-08-26 23:41:50 +00002302 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sean Huntbbd37c62009-11-21 08:43:09 +00002303 SourceLocation TypeLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002304 ParsedType Ty = ParseTypeName().get();
Sean Huntbbd37c62009-11-21 08:43:09 +00002305 SourceRange TypeRange(Start, Tok.getLocation());
John McCallb3d87482010-08-24 05:47:05 +00002306 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true,
2307 Ty.getAsOpaquePtr(), TypeRange);
Sean Huntbbd37c62009-11-21 08:43:09 +00002308 } else
2309 return ParseConstantExpression();
2310}
Francois Pichet334d47e2010-10-11 12:59:39 +00002311
2312/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
2313///
2314/// [MS] ms-attribute:
2315/// '[' token-seq ']'
2316///
2317/// [MS] ms-attribute-seq:
2318/// ms-attribute[opt]
2319/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00002320void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
2321 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00002322 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
2323
2324 while (Tok.is(tok::l_square)) {
2325 ConsumeBracket();
2326 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00002327 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00002328 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
2329 }
2330}