blob: 30dc50077a7c0827b9dd263fd850b7c76e3394d4 [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 McCall0b7e6782011-03-24 11:26:52 +000072 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000073 if (Tok.is(tok::kw___attribute)) {
74 attrTok = Tok;
John McCall7f040a92010-12-24 02:08:15 +000075 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000076 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Douglas Gregor6a588dd2009-06-17 19:49:00 +000078 if (Tok.is(tok::equal)) {
John McCall7f040a92010-12-24 02:08:15 +000079 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +000080 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +000081 if (InlineLoc.isValid())
82 Diag(InlineLoc, diag::err_inline_namespace_alias)
83 << FixItHint::CreateRemoval(InlineLoc);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000084
Chris Lattner97144fc2009-04-02 04:16:50 +000085 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000086 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattner51448322009-03-29 14:02:43 +000088 if (Tok.isNot(tok::l_brace)) {
Mike Stump1eb44332009-09-09 15:08:12 +000089 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000090 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +000091 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +000092 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattner51448322009-03-29 14:02:43 +000094 SourceLocation LBrace = ConsumeBrace();
95
Douglas Gregor23c94db2010-07-02 17:43:08 +000096 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
97 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
98 getCurScope()->getFnParent()) {
Douglas Gregor95f1b152010-05-14 05:08:22 +000099 Diag(LBrace, diag::err_namespace_nonnamespace_scope);
100 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000101 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000102 }
103
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000104 // If we're still good, complain about inline namespaces in non-C++0x now.
105 if (!getLang().CPlusPlus0x && InlineLoc.isValid())
106 Diag(InlineLoc, diag::ext_inline_namespace);
107
Chris Lattner51448322009-03-29 14:02:43 +0000108 // Enter a scope for the namespace.
109 ParseScope NamespaceScope(this, Scope::DeclScope);
110
John McCalld226f652010-08-21 09:40:31 +0000111 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000112 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
113 IdentLoc, Ident, LBrace, attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000114
John McCallf312b1e2010-08-26 23:41:50 +0000115 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
116 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Sean Huntbbd37c62009-11-21 08:43:09 +0000118 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000119 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000120 MaybeParseCXX0XAttributes(attrs);
121 MaybeParseMicrosoftAttributes(attrs);
122 ParseExternalDeclaration(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000123 }
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattner51448322009-03-29 14:02:43 +0000125 // Leave the namespace scope.
126 NamespaceScope.Exit();
127
Chris Lattner97144fc2009-04-02 04:16:50 +0000128 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
129 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000130
Chris Lattner97144fc2009-04-02 04:16:50 +0000131 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000132 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000133}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000134
Anders Carlssonf67606a2009-03-28 04:07:16 +0000135/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
136/// alias definition.
137///
John McCalld226f652010-08-21 09:40:31 +0000138Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000139 SourceLocation AliasLoc,
140 IdentifierInfo *Alias,
141 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000142 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Anders Carlssonf67606a2009-03-28 04:07:16 +0000144 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000146 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000147 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000148 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000149 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000150
Anders Carlssonf67606a2009-03-28 04:07:16 +0000151 CXXScopeSpec SS;
152 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000153 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000154
155 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
156 Diag(Tok, diag::err_expected_namespace_name);
157 // Skip to end of the definition and eat the ';'.
158 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000159 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000160 }
161
162 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000163 IdentifierInfo *Ident = Tok.getIdentifierInfo();
164 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Anders Carlssonf67606a2009-03-28 04:07:16 +0000166 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000167 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000168 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
169 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Douglas Gregor23c94db2010-07-02 17:43:08 +0000171 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000172 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000173}
174
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000175/// ParseLinkage - We know that the current token is a string_literal
176/// and just before that, that extern was seen.
177///
178/// linkage-specification: [C++ 7.5p2: dcl.link]
179/// 'extern' string-literal '{' declaration-seq[opt] '}'
180/// 'extern' string-literal declaration
181///
Chris Lattner7d642712010-11-09 20:15:55 +0000182Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000183 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000184 llvm::SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000185 bool Invalid = false;
186 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
187 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000188 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000189
190 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000191
Douglas Gregor074149e2009-01-05 19:45:36 +0000192 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000193 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000194 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000195 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000196 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000197 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000198 : SourceLocation());
199
John McCall0b7e6782011-03-24 11:26:52 +0000200 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000201 MaybeParseCXX0XAttributes(attrs);
202 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000203
Douglas Gregor074149e2009-01-05 19:45:36 +0000204 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000205 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000206 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000207 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000208 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000209 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000210
Douglas Gregor63a01132010-02-07 08:38:28 +0000211 DS.abort();
212
John McCall7f040a92010-12-24 02:08:15 +0000213 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000214
Douglas Gregorf44515a2008-12-16 22:23:02 +0000215 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000216 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000217 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000218 MaybeParseCXX0XAttributes(attrs);
219 MaybeParseMicrosoftAttributes(attrs);
220 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000221 }
222
Douglas Gregorf44515a2008-12-16 22:23:02 +0000223 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Chris Lattner7d642712010-11-09 20:15:55 +0000224 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
225 RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000226}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000227
Douglas Gregorf780abc2008-12-30 03:27:21 +0000228/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
229/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000230Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000231 const ParsedTemplateInfo &TemplateInfo,
232 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000233 ParsedAttributesWithRange &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000234 assert(Tok.is(tok::kw_using) && "Not using token");
235
236 // Eat 'using'.
237 SourceLocation UsingLoc = ConsumeToken();
238
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000239 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000240 Actions.CodeCompleteUsing(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000241 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000242 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000243
John McCall78b81052010-11-10 02:40:36 +0000244 // 'using namespace' means this is a using-directive.
245 if (Tok.is(tok::kw_namespace)) {
246 // Template parameters are always an error here.
247 if (TemplateInfo.Kind) {
248 SourceRange R = TemplateInfo.getSourceRange();
249 Diag(UsingLoc, diag::err_templated_using_directive)
250 << R << FixItHint::CreateRemoval(R);
251 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000252
John McCall7f040a92010-12-24 02:08:15 +0000253 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000254 }
255
Richard Smith162e1c12011-04-15 14:24:37 +0000256 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000257
258 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000259 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000260
John McCall78b81052010-11-10 02:40:36 +0000261 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000262}
263
264/// ParseUsingDirective - Parse C++ using-directive, assumes
265/// that current token is 'namespace' and 'using' was already parsed.
266///
267/// using-directive: [C++ 7.3.p4: namespace.udir]
268/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
269/// namespace-name ;
270/// [GNU] using-directive:
271/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
272/// namespace-name attributes[opt] ;
273///
John McCalld226f652010-08-21 09:40:31 +0000274Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000275 SourceLocation UsingLoc,
276 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000277 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000278 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
279
280 // Eat 'namespace'.
281 SourceLocation NamespcLoc = ConsumeToken();
282
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000283 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000284 Actions.CodeCompleteUsingDirective(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000285 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000286 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000287
Douglas Gregorf780abc2008-12-30 03:27:21 +0000288 CXXScopeSpec SS;
289 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000290 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000291
Douglas Gregorf780abc2008-12-30 03:27:21 +0000292 IdentifierInfo *NamespcName = 0;
293 SourceLocation IdentLoc = SourceLocation();
294
295 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000296 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000297 Diag(Tok, diag::err_expected_namespace_name);
298 // If there was invalid namespace name, skip to end of decl, and eat ';'.
299 SkipUntil(tok::semi);
300 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000301 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000302 }
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Chris Lattner823c44e2009-01-06 07:27:21 +0000304 // Parse identifier.
305 NamespcName = Tok.getIdentifierInfo();
306 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Chris Lattner823c44e2009-01-06 07:27:21 +0000308 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000309 bool GNUAttr = false;
310 if (Tok.is(tok::kw___attribute)) {
311 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000312 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000313 }
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Chris Lattner823c44e2009-01-06 07:27:21 +0000315 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000316 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000317 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000318 GNUAttr ? diag::err_expected_semi_after_attribute_list
319 : diag::err_expected_semi_after_namespace_name,
320 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000321
Douglas Gregor23c94db2010-07-02 17:43:08 +0000322 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000323 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000324}
325
Richard Smith162e1c12011-04-15 14:24:37 +0000326/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
327/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000328///
329/// using-declaration: [C++ 7.3.p3: namespace.udecl]
330/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000331/// unqualified-id
332/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000333///
Richard Smith162e1c12011-04-15 14:24:37 +0000334/// alias-declaration: C++0x [decl.typedef]p2
335/// 'using' identifier = type-id ;
336///
John McCalld226f652010-08-21 09:40:31 +0000337Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000338 const ParsedTemplateInfo &TemplateInfo,
339 SourceLocation UsingLoc,
340 SourceLocation &DeclEnd,
341 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000342 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000343 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000344 bool IsTypeName;
345
346 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000347 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000348 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000349 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000350 ConsumeToken();
351 IsTypeName = true;
352 }
353 else
354 IsTypeName = false;
355
356 // Parse nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000357 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000358
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000359 // Check nested-name specifier.
360 if (SS.isInvalid()) {
361 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000362 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000363 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000364
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000365 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000366 // destructor names and allow the action module to diagnose any semantic
367 // errors.
368 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000369 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000370 /*EnteringContext=*/false,
371 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000372 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000373 ParsedType(),
Douglas Gregor12c118a2009-11-04 16:30:06 +0000374 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000375 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000376 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000377 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000378
John McCall0b7e6782011-03-24 11:26:52 +0000379 ParsedAttributes attrs(AttrFactory);
Richard Smith162e1c12011-04-15 14:24:37 +0000380
381 // Maybe this is an alias-declaration.
382 bool IsAliasDecl = Tok.is(tok::equal);
383 TypeResult TypeAlias;
384 if (IsAliasDecl) {
385 // TODO: Do we want to support attributes somewhere in an alias declaration?
386 // Can't follow GCC since it doesn't support them yet!
387 ConsumeToken();
388
389 if (!getLang().CPlusPlus0x)
390 Diag(Tok.getLocation(), diag::ext_alias_declaration);
391
392 // Name must be an identifier.
393 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
394 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
395 // No removal fixit: can't recover from this.
396 SkipUntil(tok::semi);
397 return 0;
398 } else if (IsTypeName)
399 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
400 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
401 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
402 else if (SS.isNotEmpty())
403 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
404 << FixItHint::CreateRemoval(SS.getRange());
405
406 TypeAlias = ParseTypeName(0, Declarator::AliasDeclContext);
407 } else
408 // Parse (optional) attributes (most likely GNU strong-using extension).
409 MaybeParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000411 // Eat ';'.
412 DeclEnd = Tok.getLocation();
413 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith162e1c12011-04-15 14:24:37 +0000414 !attrs.empty() ? "attributes list" :
415 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000416 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000417
John McCall78b81052010-11-10 02:40:36 +0000418 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith162e1c12011-04-15 14:24:37 +0000419 // TODO: in C++0x, alias-declarations can be templates:
420 // template <...> using id = type;
John McCall78b81052010-11-10 02:40:36 +0000421 if (TemplateInfo.Kind) {
422 SourceRange R = TemplateInfo.getSourceRange();
423 Diag(UsingLoc, diag::err_templated_using_declaration)
424 << R << FixItHint::CreateRemoval(R);
425
426 // Unfortunately, we have to bail out instead of recovering by
427 // ignoring the parameters, just in case the nested name specifier
428 // depends on the parameters.
429 return 0;
430 }
431
Richard Smith162e1c12011-04-15 14:24:37 +0000432 if (IsAliasDecl)
433 return Actions.ActOnAliasDeclaration(getCurScope(), AS, UsingLoc, Name,
434 TypeAlias);
435
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000436 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000437 Name, attrs.getList(),
438 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000439}
440
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000441/// ParseStaticAssertDeclaration - Parse C++0x or C1X static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000442///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000443/// [C++0x] static_assert-declaration:
444/// static_assert ( constant-expression , string-literal ) ;
445///
446/// [C1X] static_assert-declaration:
447/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000448///
John McCalld226f652010-08-21 09:40:31 +0000449Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000450 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
451 "Not a static_assert declaration");
452
453 if (Tok.is(tok::kw__Static_assert) && !getLang().C1X)
454 Diag(Tok, diag::ext_c1x_static_assert);
455
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000456 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000458 if (Tok.isNot(tok::l_paren)) {
459 Diag(Tok, diag::err_expected_lparen);
John McCalld226f652010-08-21 09:40:31 +0000460 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000461 }
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000463 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000464
John McCall60d7b3a2010-08-24 06:29:42 +0000465 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000466 if (AssertExpr.isInvalid()) {
467 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000468 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000469 }
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Anders Carlssonad5f9602009-03-13 23:29:20 +0000471 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000472 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000473
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000474 if (Tok.isNot(tok::string_literal)) {
475 Diag(Tok, diag::err_expected_string_literal);
476 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000477 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000478 }
Mike Stump1eb44332009-09-09 15:08:12 +0000479
John McCall60d7b3a2010-08-24 06:29:42 +0000480 ExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000481 if (AssertMessage.isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000482 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000483
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000484 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Chris Lattner97144fc2009-04-02 04:16:50 +0000486 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000487 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000488
John McCall9ae2f072010-08-23 23:25:46 +0000489 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
490 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000491 AssertMessage.take(),
492 RParenLoc);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000493}
494
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000495/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
496///
497/// 'decltype' ( expression )
498///
499void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
500 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
501
502 SourceLocation StartLoc = ConsumeToken();
503 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000504
505 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000506 "decltype")) {
507 SkipUntil(tok::r_paren);
508 return;
509 }
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000511 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000513 // C++0x [dcl.type.simple]p4:
514 // The operand of the decltype specifier is an unevaluated operand.
515 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000516 Sema::Unevaluated);
John McCall60d7b3a2010-08-24 06:29:42 +0000517 ExprResult Result = ParseExpression();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000518 if (Result.isInvalid()) {
519 SkipUntil(tok::r_paren);
520 return;
521 }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000523 // Match the ')'
524 SourceLocation RParenLoc;
525 if (Tok.is(tok::r_paren))
526 RParenLoc = ConsumeParen();
527 else
528 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000530 if (RParenLoc.isInvalid())
531 return;
532
533 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000534 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000535 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000536 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000537 DiagID, Result.release()))
538 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000539}
540
Douglas Gregor42a552f2008-11-05 20:51:48 +0000541/// ParseClassName - Parse a C++ class-name, which names a class. Note
542/// that we only check that the result names a type; semantic analysis
543/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000544/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000545/// found.
546///
547/// class-name: [C++ 9.1]
548/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000549/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000550///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000551Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +0000552 CXXScopeSpec &SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000553 // Check whether we have a template-id that names a type.
554 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000555 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000556 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000557 if (TemplateId->Kind == TNK_Type_template ||
558 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000559 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000560
561 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000562 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000563 EndLocation = Tok.getAnnotationEndLoc();
564 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000565
566 if (Type)
567 return Type;
568 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000569 }
570
571 // Fall through to produce an error below.
572 }
573
Douglas Gregor42a552f2008-11-05 20:51:48 +0000574 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000575 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000576 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000577 }
578
Douglas Gregor84d0a192010-01-12 21:28:44 +0000579 IdentifierInfo *Id = Tok.getIdentifierInfo();
580 SourceLocation IdLoc = ConsumeToken();
581
582 if (Tok.is(tok::less)) {
583 // It looks the user intended to write a template-id here, but the
584 // template-name was wrong. Try to fix that.
585 TemplateNameKind TNK = TNK_Type_template;
586 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000587 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000588 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000589 Diag(IdLoc, diag::err_unknown_template_name)
590 << Id;
591 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000592
Douglas Gregor84d0a192010-01-12 21:28:44 +0000593 if (!Template)
594 return true;
595
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000596 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000597 UnqualifiedId TemplateName;
598 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000599
Douglas Gregor84d0a192010-01-12 21:28:44 +0000600 // Parse the full template-id, then turn it into a type.
601 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
602 SourceLocation(), true))
603 return true;
604 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000605 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000606
Douglas Gregor84d0a192010-01-12 21:28:44 +0000607 // If we didn't end up with a typename token, there's nothing more we
608 // can do.
609 if (Tok.isNot(tok::annot_typename))
610 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000611
Douglas Gregor84d0a192010-01-12 21:28:44 +0000612 // Retrieve the type from the annotation token, consume that token, and
613 // return.
614 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000615 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000616 ConsumeToken();
617 return Type;
618 }
619
Douglas Gregor42a552f2008-11-05 20:51:48 +0000620 // We have an identifier; check whether it is actually a type.
Douglas Gregor059101f2011-03-02 00:47:37 +0000621 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000622 false, ParsedType(),
623 /*NonTrivialTypeSourceInfo=*/true);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000624 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000625 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000626 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000627 }
628
629 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000630 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000631
632 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000633 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000634 DS.SetRangeStart(IdLoc);
635 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000636 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000637
638 const char *PrevSpec = 0;
639 unsigned DiagID;
640 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
641
642 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
643 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000644}
645
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000646/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
647/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
648/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000649/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000650///
651/// class-specifier: [C++ class]
652/// class-head '{' member-specification[opt] '}'
653/// class-head '{' member-specification[opt] '}' attributes[opt]
654/// class-head:
655/// class-key identifier[opt] base-clause[opt]
656/// class-key nested-name-specifier identifier base-clause[opt]
657/// class-key nested-name-specifier[opt] simple-template-id
658/// base-clause[opt]
659/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000660/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000661/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000662/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000663/// simple-template-id base-clause[opt]
664/// class-key:
665/// 'class'
666/// 'struct'
667/// 'union'
668///
669/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000670/// class-key ::[opt] nested-name-specifier[opt] identifier
671/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
672/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000673///
674/// Note that the C++ class-specifier and elaborated-type-specifier,
675/// together, subsume the C99 struct-or-union-specifier:
676///
677/// struct-or-union-specifier: [C99 6.7.2.1]
678/// struct-or-union identifier[opt] '{' struct-contents '}'
679/// struct-or-union identifier
680/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
681/// '}' attributes[opt]
682/// [GNU] struct-or-union attributes[opt] identifier
683/// struct-or-union:
684/// 'struct'
685/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000686void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
687 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000688 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redld9bafa72010-02-03 21:21:43 +0000689 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000690 DeclSpec::TST TagType;
691 if (TagTokKind == tok::kw_struct)
692 TagType = DeclSpec::TST_struct;
693 else if (TagTokKind == tok::kw_class)
694 TagType = DeclSpec::TST_class;
695 else {
696 assert(TagTokKind == tok::kw_union && "Not a class specifier");
697 TagType = DeclSpec::TST_union;
698 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000699
Douglas Gregor374929f2009-09-18 15:37:17 +0000700 if (Tok.is(tok::code_completion)) {
701 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000702 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregordc845342010-05-25 05:58:43 +0000703 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +0000704 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000705
Chandler Carruth926c4b42010-06-28 08:39:25 +0000706 // C++03 [temp.explicit] 14.7.2/8:
707 // The usual access checking rules do not apply to names used to specify
708 // explicit instantiations.
709 //
710 // As an extension we do not perform access checking on the names used to
711 // specify explicit specializations either. This is important to allow
712 // specializing traits classes for private types.
713 bool SuppressingAccessChecks = false;
714 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
715 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
716 Actions.ActOnStartSuppressingAccessChecks();
717 SuppressingAccessChecks = true;
718 }
719
John McCall0b7e6782011-03-24 11:26:52 +0000720 ParsedAttributes attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000721 // If attributes exist after tag, parse them.
722 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +0000723 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000724
Steve Narofff59e17e2008-12-24 20:59:21 +0000725 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +0000726 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +0000727 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000728
Sean Huntbbd37c62009-11-21 08:43:09 +0000729 // If C++0x attributes exist here, parse them.
730 // FIXME: Are we consistent with the ordering of parsing of different
731 // styles of attributes?
John McCall7f040a92010-12-24 02:08:15 +0000732 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000733
John Wiegley20c0da72011-04-27 23:09:49 +0000734 if (TagType == DeclSpec::TST_struct &&
735 (Tok.is(tok::kw___is_pod) ||
736 Tok.is(tok::kw___is_empty) ||
737 Tok.is(tok::kw___is_void) ||
738 Tok.is(tok::kw___is_pointer) ||
739 Tok.is(tok::kw___is_arithmetic) ||
740 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregor877222e2011-04-29 01:38:03 +0000741 Tok.is(tok::kw___is_scalar) ||
742 Tok.is(tok::kw___is_same))) {
John Wiegley20c0da72011-04-27 23:09:49 +0000743 // GNU libstdc++ 4.2 uses certain intrinsic names as the name of
744 // struct templates, but these are keywords in GCC >= 4.3 and
745 // Clang. Therefore, when we see the token sequence "struct X", make
746 // X into a normal identifier rather than a keyword, to allow
747 // libstdc++ 4.2 to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000748 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000749 Tok.setKind(tok::identifier);
750 }
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000752 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000753 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000754 if (getLang().CPlusPlus) {
755 // "FOO : BAR" is not a potential typo for "FOO::BAR".
756 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000757
John McCallb3d87482010-08-24 05:47:05 +0000758 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true))
John McCall207014e2010-07-30 06:26:29 +0000759 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +0000760 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +0000761 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
762 Diag(Tok, diag::err_expected_ident);
763 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000764
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000765 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
766
Douglas Gregorcc636682009-02-17 23:15:12 +0000767 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000768 IdentifierInfo *Name = 0;
769 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000770 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000771 if (Tok.is(tok::identifier)) {
772 Name = Tok.getIdentifierInfo();
773 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000774
Douglas Gregor5ee37342010-05-30 22:30:21 +0000775 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000776 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000777 // Eat the template argument list and try to continue parsing this as
778 // a class (or template thereof).
779 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000780 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +0000781 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000782 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000783 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000784 // We couldn't parse the template argument list at all, so don't
785 // try to give any location information for the list.
786 LAngleLoc = RAngleLoc = SourceLocation();
787 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000788
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000789 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000790 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000791 << (TagType == DeclSpec::TST_class? 0
792 : TagType == DeclSpec::TST_struct? 1
793 : 2)
794 << Name
795 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000796
797 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000798 // we've removed its template argument list.
799 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
800 if (TemplateParams && TemplateParams->size() > 1) {
801 TemplateParams->pop_back();
802 } else {
803 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000804 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000805 = ParsedTemplateInfo::NonTemplate;
806 }
807 } else if (TemplateInfo.Kind
808 == ParsedTemplateInfo::ExplicitInstantiation) {
809 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000810 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000811 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000812 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000813 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000814 = SourceLocation();
815 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
816 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000817 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000818 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000819 } else if (Tok.is(tok::annot_template_id)) {
820 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
821 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000822
Douglas Gregor059101f2011-03-02 00:47:37 +0000823 if (TemplateId->Kind != TNK_Type_template &&
824 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000825 // The template-name in the simple-template-id refers to
826 // something other than a class template. Give an appropriate
827 // error message and skip to the ';'.
828 SourceRange Range(NameLoc);
829 if (SS.isNotEmpty())
830 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000831
Douglas Gregor39a8de12009-02-25 19:37:18 +0000832 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
833 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Douglas Gregor39a8de12009-02-25 19:37:18 +0000835 DS.SetTypeSpecError();
836 SkipUntil(tok::semi, false, true);
837 TemplateId->Destroy();
Chandler Carruth926c4b42010-06-28 08:39:25 +0000838 if (SuppressingAccessChecks)
839 Actions.ActOnStopSuppressingAccessChecks();
840
Douglas Gregor39a8de12009-02-25 19:37:18 +0000841 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000842 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000843 }
844
Chandler Carruth926c4b42010-06-28 08:39:25 +0000845 // As soon as we're finished parsing the class's template-id, turn access
846 // checking back on.
847 if (SuppressingAccessChecks)
848 Actions.ActOnStopSuppressingAccessChecks();
849
John McCall67d1a672009-08-06 02:15:43 +0000850 // There are four options here. If we have 'struct foo;', then this
851 // is either a forward declaration or a friend declaration, which
Anders Carlssoncc54d592011-01-22 16:56:46 +0000852 // have to be treated differently. If we have 'struct foo {...',
Anders Carlsson1d209272011-03-25 14:55:14 +0000853 // 'struct foo :...' or 'struct foo final[opt]' then this is a
Anders Carlssoncc54d592011-01-22 16:56:46 +0000854 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000855 // However, in some contexts, things look like declarations but are just
856 // references, e.g.
857 // new struct s;
858 // or
859 // &T::operator struct s;
860 // For these, SuppressDeclarations is true.
John McCallf312b1e2010-08-26 23:41:50 +0000861 Sema::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000862 if (SuppressDeclarations)
John McCallf312b1e2010-08-26 23:41:50 +0000863 TUK = Sema::TUK_Reference;
Anders Carlssoncc54d592011-01-22 16:56:46 +0000864 else if (Tok.is(tok::l_brace) ||
865 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +0000866 isCXX0XFinalKeyword()) {
Douglas Gregord85bea22009-09-26 06:47:28 +0000867 if (DS.isFriendSpecified()) {
868 // C++ [class.friend]p2:
869 // A class shall not be defined in a friend declaration.
870 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
871 << SourceRange(DS.getFriendSpecLoc());
872
873 // Skip everything up to the semicolon, so that this looks like a proper
874 // friend class (or template thereof) declaration.
875 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +0000876 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +0000877 } else {
878 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +0000879 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +0000880 }
881 } else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +0000882 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000883 else
John McCallf312b1e2010-08-26 23:41:50 +0000884 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000885
John McCall207014e2010-07-30 06:26:29 +0000886 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +0000887 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +0000888 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
889 // We have a declaration or reference to an anonymous class.
890 Diag(StartLoc, diag::err_anon_type_definition)
891 << DeclSpec::getSpecifierName(TagType);
892 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000893
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000894 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000895
896 if (TemplateId)
897 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000898 return;
899 }
900
Douglas Gregorddc29e12009-02-06 22:42:48 +0000901 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +0000902 DeclResult TagOrTempResult = true; // invalid
903 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000904
Douglas Gregor402abb52009-05-28 23:31:59 +0000905 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000906 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000907 // Explicit specialization, class template partial specialization,
908 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000909 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000910 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000911 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000912 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000913 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000914 // This is an explicit instantiation of a class template.
915 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000916 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000917 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000918 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000919 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000920 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000921 SS,
John McCall2b5289b2010-08-23 07:28:44 +0000922 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000923 TemplateId->TemplateNameLoc,
924 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000925 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000926 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +0000927 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +0000928
929 // Friend template-ids are treated as references unless
930 // they have template headers, in which case they're ill-formed
931 // (FIXME: "template <class T> friend class A<T>::B<int>;").
932 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +0000933 } else if (TUK == Sema::TUK_Reference ||
934 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +0000935 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000936 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType,
937 StartLoc,
938 TemplateId->SS,
939 TemplateId->Template,
940 TemplateId->TemplateNameLoc,
941 TemplateId->LAngleLoc,
942 TemplateArgsPtr,
943 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000944 } else {
945 // This is an explicit specialization or a class template
946 // partial specialization.
947 TemplateParameterLists FakedParamLists;
948
949 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
950 // This looks like an explicit instantiation, because we have
951 // something like
952 //
953 // template class Foo<X>
954 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000955 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000956 // meant to be an explicit specialization, but the user forgot
957 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +0000958 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000959
Mike Stump1eb44332009-09-09 15:08:12 +0000960 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000961 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000962 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000963 diag::err_explicit_instantiation_with_definition)
964 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +0000965 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000966
967 // Create a fake template parameter list that contains only
968 // "template<>", so that we treat this construct as a class
969 // template specialization.
970 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000971 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000972 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000973 LAngleLoc,
974 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000975 LAngleLoc));
976 TemplateParams = &FakedParamLists;
977 }
978
979 // Build the class template specialization.
980 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000981 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000982 StartLoc, SS,
John McCall2b5289b2010-08-23 07:28:44 +0000983 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000984 TemplateId->TemplateNameLoc,
985 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000986 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000987 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +0000988 attrs.getList(),
John McCallf312b1e2010-08-26 23:41:50 +0000989 MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +0000990 TemplateParams? &(*TemplateParams)[0] : 0,
991 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000992 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000993 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000994 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000995 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000996 // Explicit instantiation of a member of a class template
997 // specialization, e.g.,
998 //
999 // template struct Outer<int>::Inner;
1000 //
1001 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001002 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001003 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001004 TemplateInfo.TemplateLoc,
1005 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001006 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001007 } else if (TUK == Sema::TUK_Friend &&
1008 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
1009 TagOrTempResult =
1010 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1011 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001012 Name, NameLoc, attrs.getList(),
John McCall9a34edb2010-10-19 01:40:49 +00001013 MultiTemplateParamsArg(Actions,
1014 TemplateParams? &(*TemplateParams)[0] : 0,
1015 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001016 } else {
1017 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001018 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001019 // FIXME: Diagnose this particular error.
1020 }
1021
John McCallc4e70192009-09-11 04:59:25 +00001022 bool IsDependent = false;
1023
John McCalla25c4082010-10-19 18:40:57 +00001024 // Don't pass down template parameter lists if this is just a tag
1025 // reference. For example, we don't need the template parameters here:
1026 // template <class T> class A *makeA(T t);
1027 MultiTemplateParamsArg TParams;
1028 if (TUK != Sema::TUK_Reference && TemplateParams)
1029 TParams =
1030 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1031
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001032 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001033 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001034 SS, Name, NameLoc, attrs.getList(), AS,
John McCalla25c4082010-10-19 18:40:57 +00001035 TParams, Owned, IsDependent, false,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001036 false, clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001037
1038 // If ActOnTag said the type was dependent, try again with the
1039 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001040 if (IsDependent) {
1041 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001042 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001043 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001044 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001045 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001046
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001047 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001048 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001049 assert(Tok.is(tok::l_brace) ||
Anders Carlssoncc54d592011-01-22 16:56:46 +00001050 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001051 isCXX0XFinalKeyword());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001052 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +00001053 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001054 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001055 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001056 }
1057
John McCallb3d87482010-08-24 05:47:05 +00001058 const char *PrevSpec = 0;
1059 unsigned DiagID;
1060 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001061 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001062 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1063 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001064 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001065 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001066 Result = DS.SetTypeSpecType(TagType, StartLoc,
1067 NameLoc.isValid() ? NameLoc : StartLoc,
1068 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001069 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001070 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001071 return;
1072 }
Mike Stump1eb44332009-09-09 15:08:12 +00001073
John McCallb3d87482010-08-24 05:47:05 +00001074 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001075 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001076
Chris Lattner4ed5d912010-02-02 01:23:29 +00001077 // At this point, we've successfully parsed a class-specifier in 'definition'
1078 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1079 // going to look at what comes after it to improve error recovery. If an
1080 // impossible token occurs next, we assume that the programmer forgot a ; at
1081 // the end of the declaration and recover that way.
1082 //
1083 // This switch enumerates the valid "follow" set for definition.
John McCallf312b1e2010-08-26 23:41:50 +00001084 if (TUK == Sema::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001085 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001086 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001087 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001088 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +00001089 case tok::star: // struct foo {...} * P;
1090 case tok::amp: // struct foo {...} & R = ...
1091 case tok::identifier: // struct foo {...} V ;
1092 case tok::r_paren: //(struct foo {...} ) {4}
1093 case tok::annot_cxxscope: // struct foo {...} a:: b;
1094 case tok::annot_typename: // struct foo {...} a ::b;
1095 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +00001096 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +00001097 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001098 ExpectedSemi = false;
1099 break;
1100 // Type qualifiers
1101 case tok::kw_const: // struct foo {...} const x;
1102 case tok::kw_volatile: // struct foo {...} volatile x;
1103 case tok::kw_restrict: // struct foo {...} restrict x;
1104 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +00001105 // Storage-class specifiers
1106 case tok::kw_static: // struct foo {...} static x;
1107 case tok::kw_extern: // struct foo {...} extern x;
1108 case tok::kw_typedef: // struct foo {...} typedef x;
1109 case tok::kw_register: // struct foo {...} register x;
1110 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregor33f99242010-05-17 18:19:56 +00001111 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001112 // As shown above, type qualifiers and storage class specifiers absolutely
1113 // can occur after class specifiers according to the grammar. However,
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001114 // almost no one actually writes code like this. If we see one of these,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001115 // it is much more likely that someone missed a semi colon and the
1116 // type/storage class specifier we're seeing is part of the *next*
1117 // intended declaration, as in:
1118 //
1119 // struct foo { ... }
1120 // typedef int X;
1121 //
1122 // We'd really like to emit a missing semicolon error instead of emitting
1123 // an error on the 'int' saying that you can't have two type specifiers in
1124 // the same declaration of X. Because of this, we look ahead past this
1125 // token to see if it's a type specifier. If so, we know the code is
1126 // otherwise invalid, so we can produce the expected semi error.
1127 if (!isKnownToBeTypeSpecifier(NextToken()))
1128 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001129 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001130
1131 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001132 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001133 if (!getLang().CPlusPlus)
1134 ExpectedSemi = false;
1135 break;
1136 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001137
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001138 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001139 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1140 TagType == DeclSpec::TST_class ? "class"
1141 : TagType == DeclSpec::TST_struct? "struct" : "union");
1142 // Push this token back into the preprocessor and change our current token
1143 // to ';' so that the rest of the code recovers as though there were an
1144 // ';' after the definition.
1145 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001146 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001147 }
1148 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001149}
1150
Mike Stump1eb44332009-09-09 15:08:12 +00001151/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001152///
1153/// base-clause : [C++ class.derived]
1154/// ':' base-specifier-list
1155/// base-specifier-list:
1156/// base-specifier '...'[opt]
1157/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001158void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001159 assert(Tok.is(tok::colon) && "Not a base clause");
1160 ConsumeToken();
1161
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001162 // Build up an array of parsed base specifiers.
John McCallca0408f2010-08-23 06:44:23 +00001163 llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001164
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001165 while (true) {
1166 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001167 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001168 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001169 // Skip the rest of this base specifier, up until the comma or
1170 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001171 SkipUntil(tok::comma, tok::l_brace, true, true);
1172 } else {
1173 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001174 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001175 }
1176
1177 // If the next token is a comma, consume it and keep reading
1178 // base-specifiers.
1179 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001181 // Consume the comma.
1182 ConsumeToken();
1183 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001184
1185 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001186 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001187}
1188
1189/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1190/// one entry in the base class list of a class specifier, for example:
1191/// class foo : public bar, virtual private baz {
1192/// 'public bar' and 'virtual private baz' are each base-specifiers.
1193///
1194/// base-specifier: [C++ class.derived]
1195/// ::[opt] nested-name-specifier[opt] class-name
1196/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1197/// class-name
1198/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1199/// class-name
John McCalld226f652010-08-21 09:40:31 +00001200Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001201 bool IsVirtual = false;
1202 SourceLocation StartLoc = Tok.getLocation();
1203
1204 // Parse the 'virtual' keyword.
1205 if (Tok.is(tok::kw_virtual)) {
1206 ConsumeToken();
1207 IsVirtual = true;
1208 }
1209
1210 // Parse an (optional) access specifier.
1211 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001212 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001213 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001215 // Parse the 'virtual' keyword (again!), in case it came after the
1216 // access specifier.
1217 if (Tok.is(tok::kw_virtual)) {
1218 SourceLocation VirtualLoc = ConsumeToken();
1219 if (IsVirtual) {
1220 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001221 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001222 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001223 }
1224
1225 IsVirtual = true;
1226 }
1227
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001228 // Parse optional '::' and optional nested-name-specifier.
1229 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001230 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001231
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001232 // The location of the base class itself.
1233 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001234
1235 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001236 SourceLocation EndLocation;
Douglas Gregor059101f2011-03-02 00:47:37 +00001237 TypeResult BaseType = ParseClassName(EndLocation, SS);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001238 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001239 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001241 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1242 // actually part of the base-specifier-list grammar productions, but we
1243 // parse it here for convenience.
1244 SourceLocation EllipsisLoc;
1245 if (Tok.is(tok::ellipsis))
1246 EllipsisLoc = ConsumeToken();
1247
Mike Stump1eb44332009-09-09 15:08:12 +00001248 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001249 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001251 // Notify semantic analysis that we have parsed a complete
1252 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001253 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001254 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001255}
1256
1257/// getAccessSpecifierIfPresent - Determine whether the next token is
1258/// a C++ access-specifier.
1259///
1260/// access-specifier: [C++ class.derived]
1261/// 'private'
1262/// 'protected'
1263/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001264AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001265 switch (Tok.getKind()) {
1266 default: return AS_none;
1267 case tok::kw_private: return AS_private;
1268 case tok::kw_protected: return AS_protected;
1269 case tok::kw_public: return AS_public;
1270 }
1271}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001272
Eli Friedmand33133c2009-07-22 21:45:50 +00001273void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
John McCalld226f652010-08-21 09:40:31 +00001274 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001275 // We just declared a member function. If this member function
1276 // has any default arguments, we'll need to parse them later.
1277 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001278 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001279 = DeclaratorInfo.getFunctionTypeInfo();
Eli Friedmand33133c2009-07-22 21:45:50 +00001280 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1281 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1282 if (!LateMethod) {
1283 // Push this method onto the stack of late-parsed method
1284 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001285 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1286 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001287 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001288
1289 // Add all of the parameters prior to this one (they don't
1290 // have default arguments).
1291 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1292 for (unsigned I = 0; I < ParamIdx; ++I)
1293 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001294 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001295 }
1296
1297 // Add this parameter to the list of parameters (it or may
1298 // not have a default argument).
1299 LateMethod->DefaultArgs.push_back(
1300 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1301 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1302 }
1303 }
1304}
1305
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001306/// isCXX0XVirtSpecifier - Determine whether the next token is a C++0x
1307/// virt-specifier.
1308///
1309/// virt-specifier:
1310/// override
1311/// final
Anders Carlssoncc54d592011-01-22 16:56:46 +00001312VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier() const {
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001313 if (!getLang().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001314 return VirtSpecifiers::VS_None;
1315
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001316 if (Tok.is(tok::identifier)) {
1317 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001318
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001319 // Initialize the contextual keywords.
1320 if (!Ident_final) {
1321 Ident_final = &PP.getIdentifierTable().get("final");
1322 Ident_override = &PP.getIdentifierTable().get("override");
1323 }
1324
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001325 if (II == Ident_override)
1326 return VirtSpecifiers::VS_Override;
1327
1328 if (II == Ident_final)
1329 return VirtSpecifiers::VS_Final;
1330 }
1331
1332 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001333}
1334
1335/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1336///
1337/// virt-specifier-seq:
1338/// virt-specifier
1339/// virt-specifier-seq virt-specifier
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001340void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001341 while (true) {
Anders Carlssoncc54d592011-01-22 16:56:46 +00001342 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001343 if (Specifier == VirtSpecifiers::VS_None)
1344 return;
1345
1346 // C++ [class.mem]p8:
1347 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001348 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001349 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001350 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1351 << PrevSpec
1352 << FixItHint::CreateRemoval(Tok.getLocation());
1353
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001354 if (!getLang().CPlusPlus0x)
1355 Diag(Tok.getLocation(), diag::ext_override_control_keyword)
1356 << VirtSpecifiers::getSpecifierName(Specifier);
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001357 ConsumeToken();
1358 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001359}
1360
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001361/// isCXX0XFinalKeyword - Determine whether the next token is a C++0x
1362/// contextual 'final' keyword.
1363bool Parser::isCXX0XFinalKeyword() const {
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001364 if (!getLang().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001365 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001366
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001367 if (!Tok.is(tok::identifier))
1368 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001369
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001370 // Initialize the contextual keywords.
1371 if (!Ident_final) {
1372 Ident_final = &PP.getIdentifierTable().get("final");
1373 Ident_override = &PP.getIdentifierTable().get("override");
1374 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001375
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001376 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001377}
1378
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001379/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1380///
1381/// member-declaration:
1382/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1383/// function-definition ';'[opt]
1384/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1385/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001386/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001387/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001388/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001389///
1390/// member-declarator-list:
1391/// member-declarator
1392/// member-declarator-list ',' member-declarator
1393///
1394/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001395/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001396/// declarator constant-initializer[opt]
1397/// identifier[opt] ':' constant-expression
1398///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001399/// virt-specifier-seq:
1400/// virt-specifier
1401/// virt-specifier-seq virt-specifier
1402///
1403/// virt-specifier:
1404/// override
1405/// final
1406/// new
1407///
Sebastian Redle2b68332009-04-12 17:16:29 +00001408/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001409/// '= 0'
1410///
1411/// constant-initializer:
1412/// '=' constant-expression
1413///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001414void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCallc9068d72010-07-16 08:13:16 +00001415 const ParsedTemplateInfo &TemplateInfo,
1416 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001417 if (Tok.is(tok::at)) {
1418 if (getLang().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
1419 Diag(Tok, diag::err_at_defs_cxx);
1420 else
1421 Diag(Tok, diag::err_at_in_class);
1422
1423 ConsumeToken();
1424 SkipUntil(tok::r_brace);
1425 return;
1426 }
1427
John McCall60fa3cf2009-12-11 02:10:03 +00001428 // Access declarations.
1429 if (!TemplateInfo.Kind &&
1430 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001431 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001432 Tok.is(tok::annot_cxxscope)) {
1433 bool isAccessDecl = false;
1434 if (NextToken().is(tok::identifier))
1435 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1436 else
1437 isAccessDecl = NextToken().is(tok::kw_operator);
1438
1439 if (isAccessDecl) {
1440 // Collect the scope specifier token we annotated earlier.
1441 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001442 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
John McCall60fa3cf2009-12-11 02:10:03 +00001443
1444 // Try to parse an unqualified-id.
1445 UnqualifiedId Name;
John McCallb3d87482010-08-24 05:47:05 +00001446 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001447 SkipUntil(tok::semi);
1448 return;
1449 }
1450
1451 // TODO: recover from mistakenly-qualified operator declarations.
1452 if (ExpectAndConsume(tok::semi,
1453 diag::err_expected_semi_after,
1454 "access declaration",
1455 tok::semi))
1456 return;
1457
Douglas Gregor23c94db2010-07-02 17:43:08 +00001458 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001459 false, SourceLocation(),
1460 SS, Name,
1461 /* AttrList */ 0,
1462 /* IsTypeName */ false,
1463 SourceLocation());
1464 return;
1465 }
1466 }
1467
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001468 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001469 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001470 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001471 SourceLocation DeclEnd;
1472 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001473 return;
1474 }
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Chris Lattner682bf922009-03-29 16:50:03 +00001476 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001477 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001478 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001479 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001480 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001481 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001482 return;
1483 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001484
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001485 // Handle: member-declaration ::= '__extension__' member-declaration
1486 if (Tok.is(tok::kw___extension__)) {
1487 // __extension__ silences extension warnings in the subexpression.
1488 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1489 ConsumeToken();
John McCallc9068d72010-07-16 08:13:16 +00001490 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001491 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001492
Chris Lattner4ed5d912010-02-02 01:23:29 +00001493 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1494 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001495 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001496
John McCall0b7e6782011-03-24 11:26:52 +00001497 ParsedAttributesWithRange attrs(AttrFactory);
Sean Huntbbd37c62009-11-21 08:43:09 +00001498 // Optional C++0x attribute-specifier
John McCall7f040a92010-12-24 02:08:15 +00001499 MaybeParseCXX0XAttributes(attrs);
1500 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001501
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001502 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001503 // FIXME: Check for template aliases
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001504
John McCall7f040a92010-12-24 02:08:15 +00001505 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001507 // Eat 'using'.
1508 SourceLocation UsingLoc = ConsumeToken();
1509
1510 if (Tok.is(tok::kw_namespace)) {
1511 Diag(UsingLoc, diag::err_using_namespace_in_class);
1512 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001513 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001514 SourceLocation DeclEnd;
1515 // Otherwise, it must be using-declaration.
John McCall78b81052010-11-10 02:40:36 +00001516 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1517 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001518 }
1519 return;
1520 }
1521
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001522 // decl-specifier-seq:
1523 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001524 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00001525 DS.takeAttributesFrom(attrs);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001526 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001527
John McCallf312b1e2010-08-26 23:41:50 +00001528 MultiTemplateParamsArg TemplateParams(Actions,
John McCalldd4a3b02009-09-16 22:47:08 +00001529 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1530 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1531
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001532 if (Tok.is(tok::semi)) {
1533 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001534 Decl *TheDecl =
John McCallc9068d72010-07-16 08:13:16 +00001535 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
1536 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001537 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001538 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001539
John McCall54abf7d2009-11-04 02:18:39 +00001540 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00001541 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001542
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001543 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001544 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1545 ColonProtectionRAIIObject X(*this);
1546
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001547 // Parse the first declarator.
1548 ParseDeclarator(DeclaratorInfo);
1549 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001550 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001551 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00001552 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001553 if (Tok.is(tok::semi))
1554 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001555 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001556 }
1557
Nico Weber48673472011-01-28 06:07:34 +00001558 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1559
John Thompson1b2fc0f2009-11-25 22:58:06 +00001560 // If attributes exist after the declarator, but before an '{', parse them.
John McCall7f040a92010-12-24 02:08:15 +00001561 MaybeParseGNUAttributes(DeclaratorInfo);
John Thompson1b2fc0f2009-11-25 22:58:06 +00001562
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001563 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001564 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001565 || (DeclaratorInfo.isFunctionDeclarator() &&
1566 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001567 if (!DeclaratorInfo.isFunctionDeclarator()) {
1568 Diag(Tok, diag::err_func_def_no_params);
1569 ConsumeBrace();
1570 SkipUntil(tok::r_brace, true);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001571
1572 // Consume the optional ';'
1573 if (Tok.is(tok::semi))
1574 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001575 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001576 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001577
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001578 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1579 Diag(Tok, diag::err_function_declared_typedef);
1580 // This recovery skips the entire function body. It would be nice
1581 // to simply call ParseCXXInlineMethodDef() below, however Sema
1582 // assumes the declarator represents a function, not a typedef.
1583 ConsumeBrace();
1584 SkipUntil(tok::r_brace, true);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001585
1586 // Consume the optional ';'
1587 if (Tok.is(tok::semi))
1588 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001589 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001590 }
1591
Nico Weber48673472011-01-28 06:07:34 +00001592 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo, VS);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001593 // Consume the optional ';'
1594 if (Tok.is(tok::semi))
1595 ConsumeToken();
1596
Chris Lattner682bf922009-03-29 16:50:03 +00001597 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001598 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001599 }
1600
1601 // member-declarator-list:
1602 // member-declarator
1603 // member-declarator-list ',' member-declarator
1604
John McCalld226f652010-08-21 09:40:31 +00001605 llvm::SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00001606 ExprResult BitfieldSize;
1607 ExprResult Init;
Sebastian Redle2b68332009-04-12 17:16:29 +00001608 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001609
1610 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001611 // member-declarator:
1612 // declarator pure-specifier[opt]
1613 // declarator constant-initializer[opt]
1614 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001615 if (Tok.is(tok::colon)) {
1616 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001617 BitfieldSize = ParseConstantExpression();
1618 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001619 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001620 }
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001622 ParseOptionalCXX0XVirtSpecifierSeq(VS);
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001623
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001624 // pure-specifier:
1625 // '= 0'
1626 //
1627 // constant-initializer:
1628 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001629 //
1630 // defaulted/deleted function-definition:
1631 // '=' 'default' [TODO]
1632 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001633 if (Tok.is(tok::equal)) {
1634 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001635 if (Tok.is(tok::kw_delete)) {
1636 if (!getLang().CPlusPlus0x)
1637 Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
Sebastian Redle2b68332009-04-12 17:16:29 +00001638 ConsumeToken();
1639 Deleted = true;
1640 } else {
1641 Init = ParseInitializer();
1642 if (Init.isInvalid())
1643 SkipUntil(tok::comma, true, true);
1644 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001645 }
1646
Chris Lattnere6563252010-06-13 05:34:18 +00001647 // If a simple-asm-expr is present, parse it.
1648 if (Tok.is(tok::kw_asm)) {
1649 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001650 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00001651 if (AsmLabel.isInvalid())
1652 SkipUntil(tok::comma, true, true);
1653
1654 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1655 DeclaratorInfo.SetRangeEnd(Loc);
1656 }
1657
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001658 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001659 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001660
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001661 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001662 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001663 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001664
John McCalld226f652010-08-21 09:40:31 +00001665 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00001666 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001667 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00001668 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCallbbbcdd92009-09-11 21:02:39 +00001669 /*IsDefinition*/ false,
1670 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001671 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001672 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00001673 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001674 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001675 BitfieldSize.release(),
Anders Carlsson69a87352011-01-20 03:57:25 +00001676 VS, Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001677 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001678 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001679 }
Chris Lattner682bf922009-03-29 16:50:03 +00001680 if (ThisDecl)
1681 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001682
Douglas Gregor72b505b2008-12-16 21:30:33 +00001683 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001684 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001685 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001686 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001687 }
1688
John McCall54abf7d2009-11-04 02:18:39 +00001689 DeclaratorInfo.complete(ThisDecl);
1690
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001691 // If we don't have a comma, it is either the end of the list (a ';')
1692 // or an error, bail out.
1693 if (Tok.isNot(tok::comma))
1694 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001695
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001696 // Consume the comma.
1697 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001699 // Parse the next declarator.
1700 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00001701 VS.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001702 BitfieldSize = 0;
1703 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001704 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001705
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001706 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00001707 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001708
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001709 if (Tok.isNot(tok::colon))
1710 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001711 }
1712
Chris Lattnerae50d502010-02-02 00:43:15 +00001713 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1714 // Skip to end of block or statement.
1715 SkipUntil(tok::r_brace, true, true);
1716 // If we stopped at a ';', eat it.
1717 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001718 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001719 }
1720
Douglas Gregor23c94db2010-07-02 17:43:08 +00001721 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00001722 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001723}
1724
1725/// ParseCXXMemberSpecification - Parse the class definition.
1726///
1727/// member-specification:
1728/// member-declaration member-specification[opt]
1729/// access-specifier ':' member-specification[opt]
1730///
1731void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001732 unsigned TagType, Decl *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001733 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001734 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001735 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001736
John McCallf312b1e2010-08-26 23:41:50 +00001737 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1738 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Douglas Gregor26997fd2010-01-16 20:52:59 +00001740 // Determine whether this is a non-nested class. Note that local
1741 // classes are *not* considered to be nested classes.
1742 bool NonNestedClass = true;
1743 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001744 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001745 if (S->isClassScope()) {
1746 // We're inside a class scope, so this is a nested class.
1747 NonNestedClass = false;
1748 break;
1749 }
1750
1751 if ((S->getFlags() & Scope::FnScope)) {
1752 // If we're in a function or function template declared in the
1753 // body of a class, then this is a local class rather than a
1754 // nested class.
1755 const Scope *Parent = S->getParent();
1756 if (Parent->isTemplateParamScope())
1757 Parent = Parent->getParent();
1758 if (Parent->isClassScope())
1759 break;
1760 }
1761 }
1762 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001763
1764 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001765 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001766
Douglas Gregor6569d682009-05-27 23:11:45 +00001767 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001768 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001769
Douglas Gregorddc29e12009-02-06 22:42:48 +00001770 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001771 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001772
Anders Carlssonb184a182011-03-25 14:46:08 +00001773 SourceLocation FinalLoc;
1774
1775 // Parse the optional 'final' keyword.
1776 if (getLang().CPlusPlus && Tok.is(tok::identifier)) {
1777 IdentifierInfo *II = Tok.getIdentifierInfo();
1778
1779 // Initialize the contextual keywords.
1780 if (!Ident_final) {
1781 Ident_final = &PP.getIdentifierTable().get("final");
1782 Ident_override = &PP.getIdentifierTable().get("override");
1783 }
1784
1785 if (II == Ident_final)
1786 FinalLoc = ConsumeToken();
1787
1788 if (!getLang().CPlusPlus0x)
1789 Diag(FinalLoc, diag::ext_override_control_keyword) << "final";
1790 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001791
John McCallbd0dfa52009-12-19 21:48:58 +00001792 if (Tok.is(tok::colon)) {
1793 ParseBaseClause(TagDecl);
1794
1795 if (!Tok.is(tok::l_brace)) {
1796 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00001797
1798 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001799 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001800 return;
1801 }
1802 }
1803
1804 assert(Tok.is(tok::l_brace));
1805
1806 SourceLocation LBraceLoc = ConsumeBrace();
1807
John McCall42a4f662010-05-28 08:11:17 +00001808 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00001809 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Anders Carlssondfc2f102011-01-22 17:51:53 +00001810 LBraceLoc);
John McCallf9368152009-12-20 07:58:13 +00001811
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001812 // C++ 11p3: Members of a class defined with the keyword class are private
1813 // by default. Members of a class defined with the keywords struct or union
1814 // are public by default.
1815 AccessSpecifier CurAS;
1816 if (TagType == DeclSpec::TST_class)
1817 CurAS = AS_private;
1818 else
1819 CurAS = AS_public;
1820
Douglas Gregor07976d22010-06-21 22:31:09 +00001821 SourceLocation RBraceLoc;
1822 if (TagDecl) {
1823 // While we still have something to read, read the member-declarations.
1824 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1825 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001826
Douglas Gregor07976d22010-06-21 22:31:09 +00001827 // Check for extraneous top-level semicolon.
1828 if (Tok.is(tok::semi)) {
1829 Diag(Tok, diag::ext_extra_struct_semi)
1830 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1831 << FixItHint::CreateRemoval(Tok.getLocation());
1832 ConsumeToken();
1833 continue;
1834 }
1835
1836 AccessSpecifier AS = getAccessSpecifierIfPresent();
1837 if (AS != AS_none) {
1838 // Current token is a C++ access specifier.
1839 CurAS = AS;
1840 SourceLocation ASLoc = Tok.getLocation();
1841 ConsumeToken();
1842 if (Tok.is(tok::colon))
1843 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1844 else
1845 Diag(Tok, diag::err_expected_colon);
1846 ConsumeToken();
1847 continue;
1848 }
1849
1850 // FIXME: Make sure we don't have a template here.
1851
1852 // Parse all the comma separated declarators.
1853 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001854 }
1855
Douglas Gregor07976d22010-06-21 22:31:09 +00001856 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1857 } else {
1858 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001859 }
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001861 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00001862 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001863 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001864
John McCall42a4f662010-05-28 08:11:17 +00001865 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001866 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall42a4f662010-05-28 08:11:17 +00001867 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00001868 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001869
1870 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1871 // complete within function bodies, default arguments,
1872 // exception-specifications, and constructor ctor-initializers (including
1873 // such things in nested classes).
1874 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001875 // FIXME: Only function bodies and constructor ctor-initializers are
1876 // parsed correctly, fix the rest.
Douglas Gregor07976d22010-06-21 22:31:09 +00001877 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001878 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001879 // are complete and we can parse the delayed portions of method
1880 // declarations and the lexed inline method definitions.
Douglas Gregore0cc0472010-06-16 23:45:56 +00001881 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregor6569d682009-05-27 23:11:45 +00001882 ParseLexedMethodDeclarations(getCurrentClass());
1883 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00001884 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001885 }
1886
John McCall42a4f662010-05-28 08:11:17 +00001887 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001888 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCalldb7bb4a2010-03-17 00:38:33 +00001889
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001890 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001891 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001892 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001893}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001894
1895/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1896/// which explicitly initializes the members or base classes of a
1897/// class (C++ [class.base.init]). For example, the three initializers
1898/// after the ':' in the Derived constructor below:
1899///
1900/// @code
1901/// class Base { };
1902/// class Derived : Base {
1903/// int x;
1904/// float f;
1905/// public:
1906/// Derived(float f) : Base(), x(17), f(f) { }
1907/// };
1908/// @endcode
1909///
Mike Stump1eb44332009-09-09 15:08:12 +00001910/// [C++] ctor-initializer:
1911/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001912///
Mike Stump1eb44332009-09-09 15:08:12 +00001913/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00001914/// mem-initializer ...[opt]
1915/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00001916void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001917 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1918
John Wiegley28bbe4b2011-04-28 01:08:34 +00001919 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
1920 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001921 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Sean Huntcbb67482011-01-08 20:30:50 +00001923 llvm::SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001924 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001925
Douglas Gregor7ad83902008-11-05 04:29:56 +00001926 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00001927 if (Tok.is(tok::code_completion)) {
1928 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
1929 MemInitializers.data(),
1930 MemInitializers.size());
1931 ConsumeCodeCompletionToken();
1932 } else {
1933 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
1934 if (!MemInit.isInvalid())
1935 MemInitializers.push_back(MemInit.get());
1936 else
1937 AnyErrors = true;
1938 }
1939
Douglas Gregor7ad83902008-11-05 04:29:56 +00001940 if (Tok.is(tok::comma))
1941 ConsumeToken();
1942 else if (Tok.is(tok::l_brace))
1943 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00001944 // If the next token looks like a base or member initializer, assume that
1945 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00001946 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
1947 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
1948 Diag(Loc, diag::err_ctor_init_missing_comma)
1949 << FixItHint::CreateInsertion(Loc, ", ");
1950 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001951 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001952 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001953 SkipUntil(tok::l_brace, true, true);
1954 break;
1955 }
1956 } while (true);
1957
Mike Stump1eb44332009-09-09 15:08:12 +00001958 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001959 MemInitializers.data(), MemInitializers.size(),
1960 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001961}
1962
1963/// ParseMemInitializer - Parse a C++ member initializer, which is
1964/// part of a constructor initializer that explicitly initializes one
1965/// member or base class (C++ [class.base.init]). See
1966/// ParseConstructorInitializer for an example.
1967///
1968/// [C++] mem-initializer:
1969/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001970///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001971/// [C++] mem-initializer-id:
1972/// '::'[opt] nested-name-specifier[opt] class-name
1973/// identifier
John McCalld226f652010-08-21 09:40:31 +00001974Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001975 // parse '::'[opt] nested-name-specifier[opt]
1976 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001977 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
1978 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00001979 if (Tok.is(tok::annot_template_id)) {
1980 TemplateIdAnnotation *TemplateId
1981 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001982 if (TemplateId->Kind == TNK_Type_template ||
1983 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001984 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00001985 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00001986 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00001987 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00001988 }
1989 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001990 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001991 return true;
1992 }
Mike Stump1eb44332009-09-09 15:08:12 +00001993
Douglas Gregor7ad83902008-11-05 04:29:56 +00001994 // Get the identifier. This may be a member name or a class name,
1995 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00001996 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00001997 SourceLocation IdLoc = ConsumeToken();
1998
1999 // Parse the '('.
2000 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002001 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002002 return true;
2003 }
2004 SourceLocation LParenLoc = ConsumeParen();
2005
2006 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00002007 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002008 CommaLocsTy CommaLocs;
2009 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2010 SkipUntil(tok::r_paren);
2011 return true;
2012 }
2013
2014 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2015
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002016 SourceLocation EllipsisLoc;
2017 if (Tok.is(tok::ellipsis))
2018 EllipsisLoc = ConsumeToken();
2019
Douglas Gregor23c94db2010-07-02 17:43:08 +00002020 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanian96174332009-07-01 19:21:19 +00002021 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00002022 LParenLoc, ArgExprs.take(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002023 ArgExprs.size(), RParenLoc,
2024 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002025}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002026
Sebastian Redl7acafd02011-03-05 14:45:16 +00002027/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002028///
Douglas Gregora4745612008-12-01 18:00:20 +00002029/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002030/// dynamic-exception-specification
2031/// noexcept-specification
2032///
2033/// noexcept-specification:
2034/// 'noexcept'
2035/// 'noexcept' '(' constant-expression ')'
2036ExceptionSpecificationType
2037Parser::MaybeParseExceptionSpecification(SourceRange &SpecificationRange,
2038 llvm::SmallVectorImpl<ParsedType> &DynamicExceptions,
2039 llvm::SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
2040 ExprResult &NoexceptExpr) {
2041 ExceptionSpecificationType Result = EST_None;
2042
2043 // See if there's a dynamic specification.
2044 if (Tok.is(tok::kw_throw)) {
2045 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2046 DynamicExceptions,
2047 DynamicExceptionRanges);
2048 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2049 "Produced different number of exception types and ranges.");
2050 }
2051
2052 // If there's no noexcept specification, we're done.
2053 if (Tok.isNot(tok::kw_noexcept))
2054 return Result;
2055
2056 // If we already had a dynamic specification, parse the noexcept for,
2057 // recovery, but emit a diagnostic and don't store the results.
2058 SourceRange NoexceptRange;
2059 ExceptionSpecificationType NoexceptType = EST_None;
2060
2061 SourceLocation KeywordLoc = ConsumeToken();
2062 if (Tok.is(tok::l_paren)) {
2063 // There is an argument.
2064 SourceLocation LParenLoc = ConsumeParen();
2065 NoexceptType = EST_ComputedNoexcept;
2066 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002067 // The argument must be contextually convertible to bool. We use
2068 // ActOnBooleanCondition for this purpose.
2069 if (!NoexceptExpr.isInvalid())
2070 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2071 NoexceptExpr.get());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002072 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2073 NoexceptRange = SourceRange(KeywordLoc, RParenLoc);
2074 } else {
2075 // There is no argument.
2076 NoexceptType = EST_BasicNoexcept;
2077 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2078 }
2079
2080 if (Result == EST_None) {
2081 SpecificationRange = NoexceptRange;
2082 Result = NoexceptType;
2083
2084 // If there's a dynamic specification after a noexcept specification,
2085 // parse that and ignore the results.
2086 if (Tok.is(tok::kw_throw)) {
2087 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2088 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2089 DynamicExceptionRanges);
2090 }
2091 } else {
2092 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2093 }
2094
2095 return Result;
2096}
2097
2098/// ParseDynamicExceptionSpecification - Parse a C++
2099/// dynamic-exception-specification (C++ [except.spec]).
2100///
2101/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002102/// 'throw' '(' type-id-list [opt] ')'
2103/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002104///
Douglas Gregora4745612008-12-01 18:00:20 +00002105/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002106/// type-id ... [opt]
2107/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002108///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002109ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2110 SourceRange &SpecificationRange,
2111 llvm::SmallVectorImpl<ParsedType> &Exceptions,
2112 llvm::SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002113 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002114
Sebastian Redl7acafd02011-03-05 14:45:16 +00002115 SpecificationRange.setBegin(ConsumeToken());
Mike Stump1eb44332009-09-09 15:08:12 +00002116
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002117 if (!Tok.is(tok::l_paren)) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002118 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2119 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002120 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002121 }
2122 SourceLocation LParenLoc = ConsumeParen();
2123
Douglas Gregora4745612008-12-01 18:00:20 +00002124 // Parse throw(...), a Microsoft extension that means "this function
2125 // can throw anything".
2126 if (Tok.is(tok::ellipsis)) {
2127 SourceLocation EllipsisLoc = ConsumeToken();
2128 if (!getLang().Microsoft)
2129 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redl7acafd02011-03-05 14:45:16 +00002130 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2131 SpecificationRange.setEnd(RParenLoc);
Sebastian Redl60618fa2011-03-12 11:50:43 +00002132 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002133 }
2134
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002135 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002136 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002137 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002138 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002139
Douglas Gregora04426c2010-12-20 23:57:46 +00002140 if (Tok.is(tok::ellipsis)) {
2141 // C++0x [temp.variadic]p5:
2142 // - In a dynamic-exception-specification (15.4); the pattern is a
2143 // type-id.
2144 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002145 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002146 if (!Res.isInvalid())
2147 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2148 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002149
Sebastian Redlef65f062009-05-29 18:02:33 +00002150 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002151 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002152 Ranges.push_back(Range);
2153 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002154
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002155 if (Tok.is(tok::comma))
2156 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002157 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002158 break;
2159 }
2160
Sebastian Redl7acafd02011-03-05 14:45:16 +00002161 SpecificationRange.setEnd(MatchRHSPunctuation(tok::r_paren, LParenLoc));
Sebastian Redl60618fa2011-03-12 11:50:43 +00002162 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002163}
Douglas Gregor6569d682009-05-27 23:11:45 +00002164
Douglas Gregordab60ad2010-10-01 18:44:50 +00002165/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2166/// function declaration.
2167TypeResult Parser::ParseTrailingReturnType() {
2168 assert(Tok.is(tok::arrow) && "expected arrow");
2169
2170 ConsumeToken();
2171
2172 // FIXME: Need to suppress declarations when parsing this typename.
2173 // Otherwise in this function definition:
2174 //
2175 // auto f() -> struct X {}
2176 //
2177 // struct X is parsed as class definition because of the trailing
2178 // brace.
2179
2180 SourceRange Range;
2181 return ParseTypeName(&Range);
2182}
2183
Douglas Gregor6569d682009-05-27 23:11:45 +00002184/// \brief We have just started parsing the definition of a new class,
2185/// so push that class onto our stack of classes that is currently
2186/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002187Sema::ParsingClassState
2188Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002189 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002190 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00002191 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
John McCalleee1d542011-02-14 07:13:47 +00002192 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002193}
2194
2195/// \brief Deallocate the given parsed class and all of its nested
2196/// classes.
2197void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002198 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2199 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002200 delete Class;
2201}
2202
2203/// \brief Pop the top class of the stack of classes that are
2204/// currently being parsed.
2205///
2206/// This routine should be called when we have finished parsing the
2207/// definition of a class, but have not yet popped the Scope
2208/// associated with the class's definition.
2209///
2210/// \returns true if the class we've popped is a top-level class,
2211/// false otherwise.
John McCalleee1d542011-02-14 07:13:47 +00002212void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002213 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002214
John McCalleee1d542011-02-14 07:13:47 +00002215 Actions.PopParsingClass(state);
2216
Douglas Gregor6569d682009-05-27 23:11:45 +00002217 ParsingClass *Victim = ClassStack.top();
2218 ClassStack.pop();
2219 if (Victim->TopLevelClass) {
2220 // Deallocate all of the nested classes of this class,
2221 // recursively: we don't need to keep any of this information.
2222 DeallocateParsedClasses(Victim);
2223 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002224 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002225 assert(!ClassStack.empty() && "Missing top-level class?");
2226
Douglas Gregord54eb442010-10-12 16:25:54 +00002227 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002228 // The victim is a nested class, but we will not need to perform
2229 // any processing after the definition of this class since it has
2230 // no members whose handling was delayed. Therefore, we can just
2231 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002232 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002233 return;
2234 }
2235
2236 // This nested class has some members that will need to be processed
2237 // after the top-level class is completely defined. Therefore, add
2238 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002239 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002240 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002241 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00002242}
Sean Huntbbd37c62009-11-21 08:43:09 +00002243
2244/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
2245/// parses standard attributes.
2246///
2247/// [C++0x] attribute-specifier:
2248/// '[' '[' attribute-list ']' ']'
2249///
2250/// [C++0x] attribute-list:
2251/// attribute[opt]
2252/// attribute-list ',' attribute[opt]
2253///
2254/// [C++0x] attribute:
2255/// attribute-token attribute-argument-clause[opt]
2256///
2257/// [C++0x] attribute-token:
2258/// identifier
2259/// attribute-scoped-token
2260///
2261/// [C++0x] attribute-scoped-token:
2262/// attribute-namespace '::' identifier
2263///
2264/// [C++0x] attribute-namespace:
2265/// identifier
2266///
2267/// [C++0x] attribute-argument-clause:
2268/// '(' balanced-token-seq ')'
2269///
2270/// [C++0x] balanced-token-seq:
2271/// balanced-token
2272/// balanced-token-seq balanced-token
2273///
2274/// [C++0x] balanced-token:
2275/// '(' balanced-token-seq ')'
2276/// '[' balanced-token-seq ']'
2277/// '{' balanced-token-seq '}'
2278/// any token but '(', ')', '[', ']', '{', or '}'
John McCall7f040a92010-12-24 02:08:15 +00002279void Parser::ParseCXX0XAttributes(ParsedAttributesWithRange &attrs,
2280 SourceLocation *endLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002281 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
2282 && "Not a C++0x attribute list");
2283
2284 SourceLocation StartLoc = Tok.getLocation(), Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00002285
2286 ConsumeBracket();
2287 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002288
Sean Huntbbd37c62009-11-21 08:43:09 +00002289 if (Tok.is(tok::comma)) {
2290 Diag(Tok.getLocation(), diag::err_expected_ident);
2291 ConsumeToken();
2292 }
2293
2294 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
2295 // attribute not present
2296 if (Tok.is(tok::comma)) {
2297 ConsumeToken();
2298 continue;
2299 }
2300
2301 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
2302 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002303
Sean Huntbbd37c62009-11-21 08:43:09 +00002304 // scoped attribute
2305 if (Tok.is(tok::coloncolon)) {
2306 ConsumeToken();
2307
2308 if (!Tok.is(tok::identifier)) {
2309 Diag(Tok.getLocation(), diag::err_expected_ident);
2310 SkipUntil(tok::r_square, tok::comma, true, true);
2311 continue;
2312 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002313
Sean Huntbbd37c62009-11-21 08:43:09 +00002314 ScopeName = AttrName;
2315 ScopeLoc = AttrLoc;
2316
2317 AttrName = Tok.getIdentifierInfo();
2318 AttrLoc = ConsumeToken();
2319 }
2320
2321 bool AttrParsed = false;
2322 // No scoped names are supported; ideally we could put all non-standard
2323 // attributes into namespaces.
2324 if (!ScopeName) {
2325 switch(AttributeList::getKind(AttrName))
2326 {
2327 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00002328 case AttributeList::AT_carries_dependency:
Anders Carlsson15e14a22011-01-23 21:33:18 +00002329 case AttributeList::AT_noreturn: {
Sean Huntbbd37c62009-11-21 08:43:09 +00002330 if (Tok.is(tok::l_paren)) {
2331 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
2332 << AttrName->getName();
2333 break;
2334 }
2335
John McCall0b7e6782011-03-24 11:26:52 +00002336 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc, 0,
2337 SourceLocation(), 0, 0, false, true);
Sean Huntbbd37c62009-11-21 08:43:09 +00002338 AttrParsed = true;
2339 break;
2340 }
2341
2342 // One argument; must be a type-id or assignment-expression
2343 case AttributeList::AT_aligned: {
2344 if (Tok.isNot(tok::l_paren)) {
2345 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
2346 << AttrName->getName();
2347 break;
2348 }
2349 SourceLocation ParamLoc = ConsumeParen();
2350
John McCall60d7b3a2010-08-24 06:29:42 +00002351 ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002352
2353 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2354
2355 ExprVector ArgExprs(Actions);
2356 ArgExprs.push_back(ArgExpr.release());
John McCall0b7e6782011-03-24 11:26:52 +00002357 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc,
2358 0, ParamLoc, ArgExprs.take(), 1,
2359 false, true);
Sean Huntbbd37c62009-11-21 08:43:09 +00002360
2361 AttrParsed = true;
2362 break;
2363 }
2364
2365 // Silence warnings
2366 default: break;
2367 }
2368 }
2369
2370 // Skip the entire parameter clause, if any
2371 if (!AttrParsed && Tok.is(tok::l_paren)) {
2372 ConsumeParen();
2373 // SkipUntil maintains the balancedness of tokens.
2374 SkipUntil(tok::r_paren, false);
2375 }
2376 }
2377
2378 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2379 SkipUntil(tok::r_square, false);
2380 Loc = Tok.getLocation();
2381 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2382 SkipUntil(tok::r_square, false);
2383
John McCall7f040a92010-12-24 02:08:15 +00002384 attrs.Range = SourceRange(StartLoc, Loc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002385}
2386
2387/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2388/// attribute.
2389///
2390/// FIXME: Simply returns an alignof() expression if the argument is a
2391/// type. Ideally, the type should be propagated directly into Sema.
2392///
2393/// [C++0x] 'align' '(' type-id ')'
2394/// [C++0x] 'align' '(' assignment-expression ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002395ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002396 if (isTypeIdInParens()) {
John McCallf312b1e2010-08-26 23:41:50 +00002397 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sean Huntbbd37c62009-11-21 08:43:09 +00002398 SourceLocation TypeLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002399 ParsedType Ty = ParseTypeName().get();
Sean Huntbbd37c62009-11-21 08:43:09 +00002400 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002401 return Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2402 Ty.getAsOpaquePtr(), TypeRange);
Sean Huntbbd37c62009-11-21 08:43:09 +00002403 } else
2404 return ParseConstantExpression();
2405}
Francois Pichet334d47e2010-10-11 12:59:39 +00002406
2407/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
2408///
2409/// [MS] ms-attribute:
2410/// '[' token-seq ']'
2411///
2412/// [MS] ms-attribute-seq:
2413/// ms-attribute[opt]
2414/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00002415void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
2416 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00002417 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
2418
2419 while (Tok.is(tok::l_square)) {
2420 ConsumeBracket();
2421 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00002422 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00002423 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
2424 }
2425}