blob: df2d214cb9397cf8ff0afcd21ac66ae84c4f368c [file] [log] [blame]
Chris Lattnera5235172007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnera5235172007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson74d7f0d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor423984d2008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Chris Lattnera5235172007-08-25 06:57:03 +000022using namespace clang;
23
24/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redl67667942010-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 Lattnera5235172007-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 Redl67667942010-08-27 23:12:46 +000033/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000034///
35/// named-namespace-definition:
36/// original-namespace-definition
37/// extension-namespace-definition
38///
39/// original-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000040/// 'inline'[opt] 'namespace' identifier attributes[opt]
41/// '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000042///
43/// extension-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000044/// 'inline'[opt] 'namespace' original-namespace-name
45/// '{' namespace-body '}'
Mike Stump11289f42009-09-09 15:08:12 +000046///
Chris Lattnera5235172007-08-25 06:57:03 +000047/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
48/// 'namespace' identifier '=' qualified-namespace-specifier ';'
49///
John McCall48871652010-08-21 09:40:31 +000050Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redl67667942010-08-27 23:12:46 +000051 SourceLocation &DeclEnd,
52 SourceLocation InlineLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000053 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000054 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump11289f42009-09-09 15:08:12 +000055
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000056 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000057 Actions.CodeCompleteNamespaceDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +000058 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000059 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000060
Chris Lattnera5235172007-08-25 06:57:03 +000061 SourceLocation IdentLoc;
62 IdentifierInfo *Ident = 0;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000063
64 Token attrTok;
Mike Stump11289f42009-09-09 15:08:12 +000065
Chris Lattner76c72282007-10-09 17:33:22 +000066 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000067 Ident = Tok.getIdentifierInfo();
68 IdentLoc = ConsumeToken(); // eat the identifier.
69 }
Mike Stump11289f42009-09-09 15:08:12 +000070
Chris Lattnera5235172007-08-25 06:57:03 +000071 // Read label attributes, if present.
John McCall084e83d2011-03-24 11:26:52 +000072 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000073 if (Tok.is(tok::kw___attribute)) {
74 attrTok = Tok;
John McCall53fa7142010-12-24 02:08:15 +000075 ParseGNUAttributes(attrs);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000076 }
Mike Stump11289f42009-09-09 15:08:12 +000077
Douglas Gregor6b6bba42009-06-17 19:49:00 +000078 if (Tok.is(tok::equal)) {
John McCall53fa7142010-12-24 02:08:15 +000079 if (!attrs.empty())
Douglas Gregor6b6bba42009-06-17 19:49:00 +000080 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redl67667942010-08-27 23:12:46 +000081 if (InlineLoc.isValid())
82 Diag(InlineLoc, diag::err_inline_namespace_alias)
83 << FixItHint::CreateRemoval(InlineLoc);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000084
Chris Lattner49836b42009-04-02 04:16:50 +000085 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000086 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattner4de55aa2009-03-29 14:02:43 +000088 if (Tok.isNot(tok::l_brace)) {
Mike Stump11289f42009-09-09 15:08:12 +000089 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner4de55aa2009-03-29 14:02:43 +000090 diag::err_expected_ident_lbrace);
John McCall48871652010-08-21 09:40:31 +000091 return 0;
Chris Lattnera5235172007-08-25 06:57:03 +000092 }
Mike Stump11289f42009-09-09 15:08:12 +000093
Chris Lattner4de55aa2009-03-29 14:02:43 +000094 SourceLocation LBrace = ConsumeBrace();
95
Douglas Gregor0be31a22010-07-02 17:43:08 +000096 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
97 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
98 getCurScope()->getFnParent()) {
Douglas Gregor05cfc292010-05-14 05:08:22 +000099 Diag(LBrace, diag::err_namespace_nonnamespace_scope);
100 SkipUntil(tok::r_brace, false);
John McCall48871652010-08-21 09:40:31 +0000101 return 0;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000102 }
103
Sebastian Redl5a5f2c72010-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 Lattner4de55aa2009-03-29 14:02:43 +0000108 // Enter a scope for the namespace.
109 ParseScope NamespaceScope(this, Scope::DeclScope);
110
John McCall48871652010-08-21 09:40:31 +0000111 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000112 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
113 IdentLoc, Ident, LBrace, attrs.getList());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000114
John McCallfaf5fb42010-08-26 23:41:50 +0000115 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
116 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000117
Alexis Hunt96d5c762009-11-21 08:43:09 +0000118 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall084e83d2011-03-24 11:26:52 +0000119 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000120 MaybeParseCXX0XAttributes(attrs);
121 MaybeParseMicrosoftAttributes(attrs);
122 ParseExternalDeclaration(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000123 }
Mike Stump11289f42009-09-09 15:08:12 +0000124
Chris Lattner4de55aa2009-03-29 14:02:43 +0000125 // Leave the namespace scope.
126 NamespaceScope.Exit();
127
Chris Lattner49836b42009-04-02 04:16:50 +0000128 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
129 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000130
Chris Lattner49836b42009-04-02 04:16:50 +0000131 DeclEnd = RBraceLoc;
Chris Lattner4de55aa2009-03-29 14:02:43 +0000132 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000133}
Chris Lattner38376f12008-01-12 07:05:38 +0000134
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000135/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
136/// alias definition.
137///
John McCall48871652010-08-21 09:40:31 +0000138Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall084e83d2011-03-24 11:26:52 +0000139 SourceLocation AliasLoc,
140 IdentifierInfo *Alias,
141 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000142 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000143
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000144 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000145
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000146 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000147 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000148 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000149 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000150
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000151 CXXScopeSpec SS;
152 // Parse (optional) nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000153 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Anders Carlsson1894f0d42009-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 McCall48871652010-08-21 09:40:31 +0000159 return 0;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000160 }
161
162 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000163 IdentifierInfo *Ident = Tok.getIdentifierInfo();
164 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000165
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000166 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000167 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000168 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
169 "", tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000170
Douglas Gregor0be31a22010-07-02 17:43:08 +0000171 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000172 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000173}
174
Chris Lattner38376f12008-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 Lattner8ea64422010-11-09 20:15:55 +0000182Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregor15799fd2008-11-21 16:10:08 +0000183 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000184 llvm::SmallString<8> LangBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000185 bool Invalid = false;
186 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
187 if (Invalid)
John McCall48871652010-08-21 09:40:31 +0000188 return 0;
Chris Lattner38376f12008-01-12 07:05:38 +0000189
190 SourceLocation Loc = ConsumeStringToken();
Chris Lattner38376f12008-01-12 07:05:38 +0000191
Douglas Gregor07665a62009-01-05 19:45:36 +0000192 ParseScope LinkageScope(this, Scope::DeclScope);
John McCall48871652010-08-21 09:40:31 +0000193 Decl *LinkageSpec
Douglas Gregor0be31a22010-07-02 17:43:08 +0000194 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000195 DS.getSourceRange().getBegin(),
Benjamin Kramerbebee842010-05-03 13:08:54 +0000196 Loc, Lang,
Abramo Bagnaraea947882011-03-08 16:41:52 +0000197 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor07665a62009-01-05 19:45:36 +0000198 : SourceLocation());
199
John McCall084e83d2011-03-24 11:26:52 +0000200 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000201 MaybeParseCXX0XAttributes(attrs);
202 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000203
Douglas Gregor07665a62009-01-05 19:45:36 +0000204 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000205 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000206 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000207 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor07665a62009-01-05 19:45:36 +0000208 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000209 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000210
Douglas Gregorb65a9132010-02-07 08:38:28 +0000211 DS.abort();
212
John McCall53fa7142010-12-24 02:08:15 +0000213 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000214
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000215 SourceLocation LBrace = ConsumeBrace();
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000216 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall084e83d2011-03-24 11:26:52 +0000217 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000218 MaybeParseCXX0XAttributes(attrs);
219 MaybeParseMicrosoftAttributes(attrs);
220 ParseExternalDeclaration(attrs);
Chris Lattner38376f12008-01-12 07:05:38 +0000221 }
222
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000223 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Chris Lattner8ea64422010-11-09 20:15:55 +0000224 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
225 RBrace);
Chris Lattner38376f12008-01-12 07:05:38 +0000226}
Douglas Gregor556877c2008-04-13 21:30:24 +0000227
Douglas Gregord7c4d982008-12-30 03:27:21 +0000228/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
229/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000230Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000231 const ParsedTemplateInfo &TemplateInfo,
232 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000233 ParsedAttributesWithRange &attrs) {
Douglas Gregord7c4d982008-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 Gregor7e90c6d2009-09-18 19:03:04 +0000239 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000240 Actions.CodeCompleteUsing(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000241 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000242 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000243
John McCall9b72f892010-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 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000252
John McCall53fa7142010-12-24 02:08:15 +0000253 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000254 }
255
Richard Smithdda56e42011-04-15 14:24:37 +0000256 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +0000257
258 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000259 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000260
John McCall9b72f892010-11-10 02:40:36 +0000261 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd);
Douglas Gregord7c4d982008-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 McCall48871652010-08-21 09:40:31 +0000274Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000275 SourceLocation UsingLoc,
276 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000277 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-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 Gregor7e90c6d2009-09-18 19:03:04 +0000283 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000284 Actions.CodeCompleteUsingDirective(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000285 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000286 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000287
Douglas Gregord7c4d982008-12-30 03:27:21 +0000288 CXXScopeSpec SS;
289 // Parse (optional) nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000290 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000291
Douglas Gregord7c4d982008-12-30 03:27:21 +0000292 IdentifierInfo *NamespcName = 0;
293 SourceLocation IdentLoc = SourceLocation();
294
295 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000296 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-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 McCall48871652010-08-21 09:40:31 +0000301 return 0;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000302 }
Mike Stump11289f42009-09-09 15:08:12 +0000303
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000304 // Parse identifier.
305 NamespcName = Tok.getIdentifierInfo();
306 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000307
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000308 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000309 bool GNUAttr = false;
310 if (Tok.is(tok::kw___attribute)) {
311 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000312 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000313 }
Mike Stump11289f42009-09-09 15:08:12 +0000314
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000315 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000316 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000317 ExpectAndConsume(tok::semi,
Douglas Gregor45d6bdf2010-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 Gregord7c4d982008-12-30 03:27:21 +0000321
Douglas Gregor0be31a22010-07-02 17:43:08 +0000322 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000323 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000324}
325
Richard Smithdda56e42011-04-15 14:24:37 +0000326/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
327/// Assumes that 'using' was already seen.
Douglas Gregord7c4d982008-12-30 03:27:21 +0000328///
329/// using-declaration: [C++ 7.3.p3: namespace.udecl]
330/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000331/// unqualified-id
332/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000333///
Richard Smithdda56e42011-04-15 14:24:37 +0000334/// alias-declaration: C++0x [decl.typedef]p2
335/// 'using' identifier = type-id ;
336///
John McCall48871652010-08-21 09:40:31 +0000337Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000338 const ParsedTemplateInfo &TemplateInfo,
339 SourceLocation UsingLoc,
340 SourceLocation &DeclEnd,
341 AccessSpecifier AS) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000342 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000343 SourceLocation TypenameLoc;
Douglas Gregorfec52632009-06-20 00:51:54 +0000344 bool IsTypeName;
345
346 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000347 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregorfec52632009-06-20 00:51:54 +0000348 if (Tok.is(tok::kw_typename)) {
John McCalle61f2ba2009-11-18 02:36:19 +0000349 TypenameLoc = Tok.getLocation();
Douglas Gregorfec52632009-06-20 00:51:54 +0000350 ConsumeToken();
351 IsTypeName = true;
352 }
353 else
354 IsTypeName = false;
355
356 // Parse nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000357 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregorfec52632009-06-20 00:51:54 +0000358
Douglas Gregorfec52632009-06-20 00:51:54 +0000359 // Check nested-name specifier.
360 if (SS.isInvalid()) {
361 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000362 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000363 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000364
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000365 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000366 // destructor names and allow the action module to diagnose any semantic
367 // errors.
368 UnqualifiedId Name;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000369 if (ParseUnqualifiedId(SS,
Douglas Gregor220f4272009-11-04 16:30:06 +0000370 /*EnteringContext=*/false,
371 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000372 /*AllowConstructorName=*/true,
John McCallba7bf592010-08-24 05:47:05 +0000373 ParsedType(),
Douglas Gregor220f4272009-11-04 16:30:06 +0000374 Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000375 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000376 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000377 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000378
John McCall084e83d2011-03-24 11:26:52 +0000379 ParsedAttributes attrs(AttrFactory);
Richard Smithdda56e42011-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 Stump11289f42009-09-09 15:08:12 +0000410
Douglas Gregorfec52632009-06-20 00:51:54 +0000411 // Eat ';'.
412 DeclEnd = Tok.getLocation();
413 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smithdda56e42011-04-15 14:24:37 +0000414 !attrs.empty() ? "attributes list" :
415 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor220f4272009-11-04 16:30:06 +0000416 tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000417
John McCall9b72f892010-11-10 02:40:36 +0000418 // Diagnose an attempt to declare a templated using-declaration.
Richard Smithdda56e42011-04-15 14:24:37 +0000419 // TODO: in C++0x, alias-declarations can be templates:
420 // template <...> using id = type;
John McCall9b72f892010-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 Smithdda56e42011-04-15 14:24:37 +0000432 if (IsAliasDecl)
433 return Actions.ActOnAliasDeclaration(getCurScope(), AS, UsingLoc, Name,
434 TypeAlias);
435
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000436 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000437 Name, attrs.getList(),
438 IsTypeName, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000439}
440
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000441/// ParseStaticAssertDeclaration - Parse C++0x or C1X static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000442///
Peter Collingbourne3d9cbdc2011-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 Carlssonf24fcff62009-03-11 16:27:10 +0000448///
John McCall48871652010-08-21 09:40:31 +0000449Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-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 Carlssonf24fcff62009-03-11 16:27:10 +0000456 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000457
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000458 if (Tok.isNot(tok::l_paren)) {
459 Diag(Tok, diag::err_expected_lparen);
John McCall48871652010-08-21 09:40:31 +0000460 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000461 }
Mike Stump11289f42009-09-09 15:08:12 +0000462
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000463 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000464
John McCalldadc5752010-08-24 06:29:42 +0000465 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000466 if (AssertExpr.isInvalid()) {
467 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000468 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000469 }
Mike Stump11289f42009-09-09 15:08:12 +0000470
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000471 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCall48871652010-08-21 09:40:31 +0000472 return 0;
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000473
Anders Carlssonf24fcff62009-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 McCall48871652010-08-21 09:40:31 +0000477 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000478 }
Mike Stump11289f42009-09-09 15:08:12 +0000479
John McCalldadc5752010-08-24 06:29:42 +0000480 ExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump11289f42009-09-09 15:08:12 +0000481 if (AssertMessage.isInvalid())
John McCall48871652010-08-21 09:40:31 +0000482 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000483
Abramo Bagnaraea947882011-03-08 16:41:52 +0000484 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000485
Chris Lattner49836b42009-04-02 04:16:50 +0000486 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000487 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000488
John McCallb268a282010-08-23 23:25:46 +0000489 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
490 AssertExpr.take(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000491 AssertMessage.take(),
492 RParenLoc);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000493}
494
Anders Carlsson74948d02009-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 Stump11289f42009-09-09 15:08:12 +0000504
505 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson74948d02009-06-24 17:47:40 +0000506 "decltype")) {
507 SkipUntil(tok::r_paren);
508 return;
509 }
Mike Stump11289f42009-09-09 15:08:12 +0000510
Anders Carlsson74948d02009-06-24 17:47:40 +0000511 // Parse the expression
Mike Stump11289f42009-09-09 15:08:12 +0000512
Anders Carlsson74948d02009-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 McCallfaf5fb42010-08-26 23:41:50 +0000516 Sema::Unevaluated);
John McCalldadc5752010-08-24 06:29:42 +0000517 ExprResult Result = ParseExpression();
Anders Carlsson74948d02009-06-24 17:47:40 +0000518 if (Result.isInvalid()) {
519 SkipUntil(tok::r_paren);
520 return;
521 }
Mike Stump11289f42009-09-09 15:08:12 +0000522
Anders Carlsson74948d02009-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 Stump11289f42009-09-09 15:08:12 +0000529
Anders Carlsson74948d02009-06-24 17:47:40 +0000530 if (RParenLoc.isInvalid())
531 return;
532
533 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000534 unsigned DiagID;
Anders Carlsson74948d02009-06-24 17:47:40 +0000535 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump11289f42009-09-09 15:08:12 +0000536 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000537 DiagID, Result.release()))
538 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson74948d02009-06-24 17:47:40 +0000539}
540
Douglas Gregor831c93f2008-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 Gregord54dfb82009-02-25 23:52:28 +0000544/// either a type or NULL, depending on whether a type name was
Douglas Gregor831c93f2008-11-05 20:51:48 +0000545/// found.
546///
547/// class-name: [C++ 9.1]
548/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000549/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000550///
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000551Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Douglas Gregore7c20652011-03-02 00:47:37 +0000552 CXXScopeSpec &SS) {
Douglas Gregord54dfb82009-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 Stump11289f42009-09-09 15:08:12 +0000555 TemplateIdAnnotation *TemplateId
Douglas Gregord54dfb82009-02-25 23:52:28 +0000556 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregor46c59612010-01-12 17:52:59 +0000557 if (TemplateId->Kind == TNK_Type_template ||
558 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000559 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000560
561 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000562 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000563 EndLocation = Tok.getAnnotationEndLoc();
564 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000565
566 if (Type)
567 return Type;
568 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000569 }
570
571 // Fall through to produce an error below.
572 }
573
Douglas Gregor831c93f2008-11-05 20:51:48 +0000574 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000575 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000576 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000577 }
578
Douglas Gregor18473f32010-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 Gregor0be31a22010-07-02 17:43:08 +0000587 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000588 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000589 Diag(IdLoc, diag::err_unknown_template_name)
590 << Id;
591 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000592
Douglas Gregor18473f32010-01-12 21:28:44 +0000593 if (!Template)
594 return true;
595
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000596 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000597 UnqualifiedId TemplateName;
598 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000599
Douglas Gregor18473f32010-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 Gregore7c20652011-03-02 00:47:37 +0000605 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000606
Douglas Gregor18473f32010-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 Rajaratnamba2c6522010-03-13 10:17:05 +0000611
Douglas Gregor18473f32010-01-12 21:28:44 +0000612 // Retrieve the type from the annotation token, consume that token, and
613 // return.
614 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +0000615 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +0000616 ConsumeToken();
617 return Type;
618 }
619
Douglas Gregor831c93f2008-11-05 20:51:48 +0000620 // We have an identifier; check whether it is actually a type.
Douglas Gregore7c20652011-03-02 00:47:37 +0000621 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +0000622 false, ParsedType(),
623 /*NonTrivialTypeSourceInfo=*/true);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000624 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +0000625 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000626 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000627 }
628
629 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +0000630 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000631
632 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000633 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +0000634 DS.SetRangeStart(IdLoc);
635 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +0000636 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-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 Gregor831c93f2008-11-05 20:51:48 +0000644}
645
Douglas Gregor556877c2008-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 Redl2b372722010-02-03 21:21:43 +0000649/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregor556877c2008-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 Stump11289f42009-09-09 15:08:12 +0000660/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +0000661/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000662/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-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 Stump11289f42009-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 Gregor556877c2008-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 Lattnerffaa0e62009-04-12 21:49:30 +0000686void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
687 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000688 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redl2b372722010-02-03 21:21:43 +0000689 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattnerffaa0e62009-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 Gregor556877c2008-04-13 21:30:24 +0000699
Douglas Gregorf45b0cf2009-09-18 15:37:17 +0000700 if (Tok.is(tok::code_completion)) {
701 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000702 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000703 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +0000704 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000705
Chandler Carruth2d69ec72010-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 McCall084e83d2011-03-24 11:26:52 +0000720 ParsedAttributes attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +0000721 // If attributes exist after tag, parse them.
722 if (Tok.is(tok::kw___attribute))
John McCall53fa7142010-12-24 02:08:15 +0000723 ParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +0000724
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000725 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +0000726 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +0000727 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000728
Alexis Hunt96d5c762009-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 McCall53fa7142010-12-24 02:08:15 +0000732 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +0000733
John Wiegley65497cc2011-04-27 23:09:49 +0000734 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorf1fce5d2011-04-29 15:31:39 +0000735 !Tok.is(tok::identifier) &&
736 Tok.getIdentifierInfo() &&
737 (Tok.is(tok::kw___is_arithmetic) ||
738 Tok.is(tok::kw___is_convertible) ||
John Wiegley65497cc2011-04-27 23:09:49 +0000739 Tok.is(tok::kw___is_empty) ||
Douglas Gregorf1fce5d2011-04-29 15:31:39 +0000740 Tok.is(tok::kw___is_floating_point) ||
741 Tok.is(tok::kw___is_function) ||
John Wiegley65497cc2011-04-27 23:09:49 +0000742 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorf1fce5d2011-04-29 15:31:39 +0000743 Tok.is(tok::kw___is_integral) ||
744 Tok.is(tok::kw___is_member_function_pointer) ||
745 Tok.is(tok::kw___is_member_pointer) ||
746 Tok.is(tok::kw___is_pod) ||
747 Tok.is(tok::kw___is_pointer) ||
748 Tok.is(tok::kw___is_same) ||
Douglas Gregor63180b12011-04-29 01:38:03 +0000749 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorf1fce5d2011-04-29 15:31:39 +0000750 Tok.is(tok::kw___is_signed) ||
751 Tok.is(tok::kw___is_unsigned) ||
752 Tok.is(tok::kw___is_void))) {
753 // GNU libstdc++ 4.2 and libc++ uaw certain intrinsic names as the
754 // name of struct templates, but some are keywords in GCC >= 4.3
755 // and Clang. Therefore, when we see the token sequence "struct
756 // X", make X into a normal identifier rather than a keyword, to
757 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000758 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregor119b0c72009-09-04 05:53:02 +0000759 Tok.setKind(tok::identifier);
760 }
Mike Stump11289f42009-09-09 15:08:12 +0000761
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000762 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +0000763 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000764 if (getLang().CPlusPlus) {
765 // "FOO : BAR" is not a potential typo for "FOO::BAR".
766 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000767
John McCallba7bf592010-08-24 05:47:05 +0000768 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true))
John McCall413021a2010-07-30 06:26:29 +0000769 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +0000770 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000771 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
772 Diag(Tok, diag::err_expected_ident);
773 }
Douglas Gregor67a65642009-02-17 23:15:12 +0000774
Douglas Gregor916462b2009-10-30 21:46:58 +0000775 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
776
Douglas Gregor67a65642009-02-17 23:15:12 +0000777 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +0000778 IdentifierInfo *Name = 0;
779 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +0000780 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +0000781 if (Tok.is(tok::identifier)) {
782 Name = Tok.getIdentifierInfo();
783 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000784
Douglas Gregord5a479c2010-05-30 22:30:21 +0000785 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000786 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +0000787 // Eat the template argument list and try to continue parsing this as
788 // a class (or template thereof).
789 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +0000790 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +0000791 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +0000792 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000793 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +0000794 // We couldn't parse the template argument list at all, so don't
795 // try to give any location information for the list.
796 LAngleLoc = RAngleLoc = SourceLocation();
797 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000798
Douglas Gregor916462b2009-10-30 21:46:58 +0000799 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000800 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor916462b2009-10-30 21:46:58 +0000801 << (TagType == DeclSpec::TST_class? 0
802 : TagType == DeclSpec::TST_struct? 1
803 : 2)
804 << Name
805 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000806
807 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000808 // we've removed its template argument list.
809 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
810 if (TemplateParams && TemplateParams->size() > 1) {
811 TemplateParams->pop_back();
812 } else {
813 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000814 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000815 = ParsedTemplateInfo::NonTemplate;
816 }
817 } else if (TemplateInfo.Kind
818 == ParsedTemplateInfo::ExplicitInstantiation) {
819 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +0000820 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000821 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +0000822 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000823 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000824 = SourceLocation();
825 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
826 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +0000827 }
Douglas Gregor916462b2009-10-30 21:46:58 +0000828 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000829 } else if (Tok.is(tok::annot_template_id)) {
830 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
831 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +0000832
Douglas Gregore7c20652011-03-02 00:47:37 +0000833 if (TemplateId->Kind != TNK_Type_template &&
834 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000835 // The template-name in the simple-template-id refers to
836 // something other than a class template. Give an appropriate
837 // error message and skip to the ';'.
838 SourceRange Range(NameLoc);
839 if (SS.isNotEmpty())
840 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +0000841
Douglas Gregor7f741122009-02-25 19:37:18 +0000842 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
843 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +0000844
Douglas Gregor7f741122009-02-25 19:37:18 +0000845 DS.SetTypeSpecError();
846 SkipUntil(tok::semi, false, true);
847 TemplateId->Destroy();
Chandler Carruth2d69ec72010-06-28 08:39:25 +0000848 if (SuppressingAccessChecks)
849 Actions.ActOnStopSuppressingAccessChecks();
850
Douglas Gregor7f741122009-02-25 19:37:18 +0000851 return;
Douglas Gregor67a65642009-02-17 23:15:12 +0000852 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000853 }
854
Chandler Carruth2d69ec72010-06-28 08:39:25 +0000855 // As soon as we're finished parsing the class's template-id, turn access
856 // checking back on.
857 if (SuppressingAccessChecks)
858 Actions.ActOnStopSuppressingAccessChecks();
859
John McCall07e91c02009-08-06 02:15:43 +0000860 // There are four options here. If we have 'struct foo;', then this
861 // is either a forward declaration or a friend declaration, which
Anders Carlsson4b63d0e2011-01-22 16:56:46 +0000862 // have to be treated differently. If we have 'struct foo {...',
Anders Carlsson65c76d32011-03-25 14:55:14 +0000863 // 'struct foo :...' or 'struct foo final[opt]' then this is a
Anders Carlsson4b63d0e2011-01-22 16:56:46 +0000864 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Sebastian Redl2b372722010-02-03 21:21:43 +0000865 // However, in some contexts, things look like declarations but are just
866 // references, e.g.
867 // new struct s;
868 // or
869 // &T::operator struct s;
870 // For these, SuppressDeclarations is true.
John McCallfaf5fb42010-08-26 23:41:50 +0000871 Sema::TagUseKind TUK;
Sebastian Redl2b372722010-02-03 21:21:43 +0000872 if (SuppressDeclarations)
John McCallfaf5fb42010-08-26 23:41:50 +0000873 TUK = Sema::TUK_Reference;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +0000874 else if (Tok.is(tok::l_brace) ||
875 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlssoncafbab72011-03-25 14:53:29 +0000876 isCXX0XFinalKeyword()) {
Douglas Gregor3dad8422009-09-26 06:47:28 +0000877 if (DS.isFriendSpecified()) {
878 // C++ [class.friend]p2:
879 // A class shall not be defined in a friend declaration.
880 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
881 << SourceRange(DS.getFriendSpecLoc());
882
883 // Skip everything up to the semicolon, so that this looks like a proper
884 // friend class (or template thereof) declaration.
885 SkipUntil(tok::semi, true, true);
John McCallfaf5fb42010-08-26 23:41:50 +0000886 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +0000887 } else {
888 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +0000889 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +0000890 }
891 } else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +0000892 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregor556877c2008-04-13 21:30:24 +0000893 else
John McCallfaf5fb42010-08-26 23:41:50 +0000894 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +0000895
John McCall413021a2010-07-30 06:26:29 +0000896 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +0000897 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +0000898 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
899 // We have a declaration or reference to an anonymous class.
900 Diag(StartLoc, diag::err_anon_type_definition)
901 << DeclSpec::getSpecifierName(TagType);
902 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000903
Douglas Gregor556877c2008-04-13 21:30:24 +0000904 SkipUntil(tok::comma, true);
Douglas Gregor7f741122009-02-25 19:37:18 +0000905
906 if (TemplateId)
907 TemplateId->Destroy();
Douglas Gregor556877c2008-04-13 21:30:24 +0000908 return;
909 }
910
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000911 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +0000912 DeclResult TagOrTempResult = true; // invalid
913 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000914
Douglas Gregord6ab8742009-05-28 23:31:59 +0000915 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +0000916 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000917 // Explicit specialization, class template partial specialization,
918 // or explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +0000919 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor7f741122009-02-25 19:37:18 +0000920 TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +0000921 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000922 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +0000923 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000924 // This is an explicit instantiation of a class template.
925 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +0000926 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +0000927 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000928 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000929 TagType,
Mike Stump11289f42009-09-09 15:08:12 +0000930 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000931 SS,
John McCall3e56fd42010-08-23 07:28:44 +0000932 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +0000933 TemplateId->TemplateNameLoc,
934 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000935 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +0000936 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +0000937 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +0000938
939 // Friend template-ids are treated as references unless
940 // they have template headers, in which case they're ill-formed
941 // (FIXME: "template <class T> friend class A<T>::B<int>;").
942 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +0000943 } else if (TUK == Sema::TUK_Reference ||
944 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +0000945 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000946 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType,
947 StartLoc,
948 TemplateId->SS,
949 TemplateId->Template,
950 TemplateId->TemplateNameLoc,
951 TemplateId->LAngleLoc,
952 TemplateArgsPtr,
953 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000954 } else {
955 // This is an explicit specialization or a class template
956 // partial specialization.
957 TemplateParameterLists FakedParamLists;
958
959 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
960 // This looks like an explicit instantiation, because we have
961 // something like
962 //
963 // template class Foo<X>
964 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000965 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000966 // meant to be an explicit specialization, but the user forgot
967 // the '<>' after 'template'.
John McCallfaf5fb42010-08-26 23:41:50 +0000968 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000969
Mike Stump11289f42009-09-09 15:08:12 +0000970 SourceLocation LAngleLoc
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000971 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000972 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000973 diag::err_explicit_instantiation_with_definition)
974 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregora771f462010-03-31 17:46:05 +0000975 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000976
977 // Create a fake template parameter list that contains only
978 // "template<>", so that we treat this construct as a class
979 // template specialization.
980 FakedParamLists.push_back(
Mike Stump11289f42009-09-09 15:08:12 +0000981 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000982 TemplateInfo.TemplateLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000983 LAngleLoc,
984 0, 0,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000985 LAngleLoc));
986 TemplateParams = &FakedParamLists;
987 }
988
989 // Build the class template specialization.
990 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +0000991 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor7f741122009-02-25 19:37:18 +0000992 StartLoc, SS,
John McCall3e56fd42010-08-23 07:28:44 +0000993 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +0000994 TemplateId->TemplateNameLoc,
995 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +0000996 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +0000997 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +0000998 attrs.getList(),
John McCallfaf5fb42010-08-26 23:41:50 +0000999 MultiTemplateParamsArg(Actions,
Douglas Gregor67a65642009-02-17 23:15:12 +00001000 TemplateParams? &(*TemplateParams)[0] : 0,
1001 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001002 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001003 TemplateId->Destroy();
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001004 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001005 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001006 // Explicit instantiation of a member of a class template
1007 // specialization, e.g.,
1008 //
1009 // template struct Outer<int>::Inner;
1010 //
1011 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001012 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001013 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001014 TemplateInfo.TemplateLoc,
1015 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001016 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001017 } else if (TUK == Sema::TUK_Friend &&
1018 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
1019 TagOrTempResult =
1020 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1021 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001022 Name, NameLoc, attrs.getList(),
John McCallace48cd2010-10-19 01:40:49 +00001023 MultiTemplateParamsArg(Actions,
1024 TemplateParams? &(*TemplateParams)[0] : 0,
1025 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001026 } else {
1027 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001028 TUK == Sema::TUK_Definition) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001029 // FIXME: Diagnose this particular error.
1030 }
1031
John McCall7f41d982009-09-11 04:59:25 +00001032 bool IsDependent = false;
1033
John McCall32723e92010-10-19 18:40:57 +00001034 // Don't pass down template parameter lists if this is just a tag
1035 // reference. For example, we don't need the template parameters here:
1036 // template <class T> class A *makeA(T t);
1037 MultiTemplateParamsArg TParams;
1038 if (TUK != Sema::TUK_Reference && TemplateParams)
1039 TParams =
1040 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1041
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001042 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001043 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001044 SS, Name, NameLoc, attrs.getList(), AS,
John McCall32723e92010-10-19 18:40:57 +00001045 TParams, Owned, IsDependent, false,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001046 false, clang::TypeResult());
John McCall7f41d982009-09-11 04:59:25 +00001047
1048 // If ActOnTag said the type was dependent, try again with the
1049 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001050 if (IsDependent) {
1051 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001052 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001053 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001054 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001055 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001056
Douglas Gregor556877c2008-04-13 21:30:24 +00001057 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001058 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001059 assert(Tok.is(tok::l_brace) ||
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001060 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlssoncafbab72011-03-25 14:53:29 +00001061 isCXX0XFinalKeyword());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001062 if (getLang().CPlusPlus)
Douglas Gregorc08f4892009-03-25 00:13:59 +00001063 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001064 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001065 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001066 }
1067
John McCallba7bf592010-08-24 05:47:05 +00001068 const char *PrevSpec = 0;
1069 unsigned DiagID;
1070 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001071 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001072 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1073 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallba7bf592010-08-24 05:47:05 +00001074 PrevSpec, DiagID, TypeResult.get());
John McCall7f41d982009-09-11 04:59:25 +00001075 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001076 Result = DS.SetTypeSpecType(TagType, StartLoc,
1077 NameLoc.isValid() ? NameLoc : StartLoc,
1078 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCall7f41d982009-09-11 04:59:25 +00001079 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001080 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001081 return;
1082 }
Mike Stump11289f42009-09-09 15:08:12 +00001083
John McCallba7bf592010-08-24 05:47:05 +00001084 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001085 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001086
Chris Lattnercf251412010-02-02 01:23:29 +00001087 // At this point, we've successfully parsed a class-specifier in 'definition'
1088 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1089 // going to look at what comes after it to improve error recovery. If an
1090 // impossible token occurs next, we assume that the programmer forgot a ; at
1091 // the end of the declaration and recover that way.
1092 //
1093 // This switch enumerates the valid "follow" set for definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001094 if (TUK == Sema::TUK_Definition) {
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001095 bool ExpectedSemi = true;
Chris Lattnercf251412010-02-02 01:23:29 +00001096 switch (Tok.getKind()) {
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001097 default: break;
Chris Lattnercf251412010-02-02 01:23:29 +00001098 case tok::semi: // struct foo {...} ;
Chris Lattnerafe6a842010-02-02 17:32:27 +00001099 case tok::star: // struct foo {...} * P;
1100 case tok::amp: // struct foo {...} & R = ...
1101 case tok::identifier: // struct foo {...} V ;
1102 case tok::r_paren: //(struct foo {...} ) {4}
1103 case tok::annot_cxxscope: // struct foo {...} a:: b;
1104 case tok::annot_typename: // struct foo {...} a ::b;
1105 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattner5e854b92010-02-03 20:41:24 +00001106 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner35af0ab2010-02-03 01:45:03 +00001107 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001108 ExpectedSemi = false;
1109 break;
1110 // Type qualifiers
1111 case tok::kw_const: // struct foo {...} const x;
1112 case tok::kw_volatile: // struct foo {...} volatile x;
1113 case tok::kw_restrict: // struct foo {...} restrict x;
1114 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattnerafe6a842010-02-02 17:32:27 +00001115 // Storage-class specifiers
1116 case tok::kw_static: // struct foo {...} static x;
1117 case tok::kw_extern: // struct foo {...} extern x;
1118 case tok::kw_typedef: // struct foo {...} typedef x;
1119 case tok::kw_register: // struct foo {...} register x;
1120 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregorc9a99c52010-05-17 18:19:56 +00001121 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001122 // As shown above, type qualifiers and storage class specifiers absolutely
1123 // can occur after class specifiers according to the grammar. However,
Chris Lattner57540c52011-04-15 05:22:18 +00001124 // almost no one actually writes code like this. If we see one of these,
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001125 // it is much more likely that someone missed a semi colon and the
1126 // type/storage class specifier we're seeing is part of the *next*
1127 // intended declaration, as in:
1128 //
1129 // struct foo { ... }
1130 // typedef int X;
1131 //
1132 // We'd really like to emit a missing semicolon error instead of emitting
1133 // an error on the 'int' saying that you can't have two type specifiers in
1134 // the same declaration of X. Because of this, we look ahead past this
1135 // token to see if it's a type specifier. If so, we know the code is
1136 // otherwise invalid, so we can produce the expected semi error.
1137 if (!isKnownToBeTypeSpecifier(NextToken()))
1138 ExpectedSemi = false;
Chris Lattnercf251412010-02-02 01:23:29 +00001139 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001140
1141 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattnercf251412010-02-02 01:23:29 +00001142 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001143 if (!getLang().CPlusPlus)
1144 ExpectedSemi = false;
1145 break;
1146 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001147
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001148 if (ExpectedSemi) {
Chris Lattnercf251412010-02-02 01:23:29 +00001149 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1150 TagType == DeclSpec::TST_class ? "class"
1151 : TagType == DeclSpec::TST_struct? "struct" : "union");
1152 // Push this token back into the preprocessor and change our current token
1153 // to ';' so that the rest of the code recovers as though there were an
1154 // ';' after the definition.
1155 PP.EnterToken(Tok);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001156 Tok.setKind(tok::semi);
Chris Lattnercf251412010-02-02 01:23:29 +00001157 }
1158 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001159}
1160
Mike Stump11289f42009-09-09 15:08:12 +00001161/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001162///
1163/// base-clause : [C++ class.derived]
1164/// ':' base-specifier-list
1165/// base-specifier-list:
1166/// base-specifier '...'[opt]
1167/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001168void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001169 assert(Tok.is(tok::colon) && "Not a base clause");
1170 ConsumeToken();
1171
Douglas Gregor29a92472008-10-22 17:49:05 +00001172 // Build up an array of parsed base specifiers.
John McCall37ad5512010-08-23 06:44:23 +00001173 llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001174
Douglas Gregor556877c2008-04-13 21:30:24 +00001175 while (true) {
1176 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001177 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001178 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001179 // Skip the rest of this base specifier, up until the comma or
1180 // opening brace.
Douglas Gregor29a92472008-10-22 17:49:05 +00001181 SkipUntil(tok::comma, tok::l_brace, true, true);
1182 } else {
1183 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001184 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001185 }
1186
1187 // If the next token is a comma, consume it and keep reading
1188 // base-specifiers.
1189 if (Tok.isNot(tok::comma)) break;
Mike Stump11289f42009-09-09 15:08:12 +00001190
Douglas Gregor556877c2008-04-13 21:30:24 +00001191 // Consume the comma.
1192 ConsumeToken();
1193 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001194
1195 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001196 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001197}
1198
1199/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1200/// one entry in the base class list of a class specifier, for example:
1201/// class foo : public bar, virtual private baz {
1202/// 'public bar' and 'virtual private baz' are each base-specifiers.
1203///
1204/// base-specifier: [C++ class.derived]
1205/// ::[opt] nested-name-specifier[opt] class-name
1206/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1207/// class-name
1208/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1209/// class-name
John McCall48871652010-08-21 09:40:31 +00001210Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001211 bool IsVirtual = false;
1212 SourceLocation StartLoc = Tok.getLocation();
1213
1214 // Parse the 'virtual' keyword.
1215 if (Tok.is(tok::kw_virtual)) {
1216 ConsumeToken();
1217 IsVirtual = true;
1218 }
1219
1220 // Parse an (optional) access specifier.
1221 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001222 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001223 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001224
Douglas Gregor556877c2008-04-13 21:30:24 +00001225 // Parse the 'virtual' keyword (again!), in case it came after the
1226 // access specifier.
1227 if (Tok.is(tok::kw_virtual)) {
1228 SourceLocation VirtualLoc = ConsumeToken();
1229 if (IsVirtual) {
1230 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001231 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001232 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001233 }
1234
1235 IsVirtual = true;
1236 }
1237
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001238 // Parse optional '::' and optional nested-name-specifier.
1239 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001240 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregor556877c2008-04-13 21:30:24 +00001241
Douglas Gregor556877c2008-04-13 21:30:24 +00001242 // The location of the base class itself.
1243 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor831c93f2008-11-05 20:51:48 +00001244
1245 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001246 SourceLocation EndLocation;
Douglas Gregore7c20652011-03-02 00:47:37 +00001247 TypeResult BaseType = ParseClassName(EndLocation, SS);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001248 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001249 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001250
Douglas Gregor752a5952011-01-03 22:36:02 +00001251 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1252 // actually part of the base-specifier-list grammar productions, but we
1253 // parse it here for convenience.
1254 SourceLocation EllipsisLoc;
1255 if (Tok.is(tok::ellipsis))
1256 EllipsisLoc = ConsumeToken();
1257
Mike Stump11289f42009-09-09 15:08:12 +00001258 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001259 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001260
Douglas Gregor556877c2008-04-13 21:30:24 +00001261 // Notify semantic analysis that we have parsed a complete
1262 // base-specifier.
Sebastian Redl511ed552008-11-25 22:21:31 +00001263 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor752a5952011-01-03 22:36:02 +00001264 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001265}
1266
1267/// getAccessSpecifierIfPresent - Determine whether the next token is
1268/// a C++ access-specifier.
1269///
1270/// access-specifier: [C++ class.derived]
1271/// 'private'
1272/// 'protected'
1273/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001274AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001275 switch (Tok.getKind()) {
1276 default: return AS_none;
1277 case tok::kw_private: return AS_private;
1278 case tok::kw_protected: return AS_protected;
1279 case tok::kw_public: return AS_public;
1280 }
1281}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001282
Eli Friedman3af2a772009-07-22 21:45:50 +00001283void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
John McCall48871652010-08-21 09:40:31 +00001284 Decl *ThisDecl) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001285 // We just declared a member function. If this member function
1286 // has any default arguments, we'll need to parse them later.
1287 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001288 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001289 = DeclaratorInfo.getFunctionTypeInfo();
Eli Friedman3af2a772009-07-22 21:45:50 +00001290 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1291 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1292 if (!LateMethod) {
1293 // Push this method onto the stack of late-parsed method
1294 // declarations.
Douglas Gregorefc46952010-10-12 16:25:54 +00001295 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1296 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001297 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001298
1299 // Add all of the parameters prior to this one (they don't
1300 // have default arguments).
1301 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1302 for (unsigned I = 0; I < ParamIdx; ++I)
1303 LateMethod->DefaultArgs.push_back(
Douglas Gregor1d85d292010-03-02 01:29:43 +00001304 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001305 }
1306
1307 // Add this parameter to the list of parameters (it or may
1308 // not have a default argument).
1309 LateMethod->DefaultArgs.push_back(
1310 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1311 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1312 }
1313 }
1314}
1315
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001316/// isCXX0XVirtSpecifier - Determine whether the next token is a C++0x
1317/// virt-specifier.
1318///
1319/// virt-specifier:
1320/// override
1321/// final
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001322VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier() const {
Anders Carlsson5a72fdb2011-01-22 23:01:49 +00001323 if (!getLang().CPlusPlus)
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001324 return VirtSpecifiers::VS_None;
1325
Anders Carlsson56104902011-01-17 03:05:47 +00001326 if (Tok.is(tok::identifier)) {
1327 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001328
Anders Carlsson428803b2011-01-20 03:47:08 +00001329 // Initialize the contextual keywords.
1330 if (!Ident_final) {
1331 Ident_final = &PP.getIdentifierTable().get("final");
1332 Ident_override = &PP.getIdentifierTable().get("override");
1333 }
1334
Anders Carlsson56104902011-01-17 03:05:47 +00001335 if (II == Ident_override)
1336 return VirtSpecifiers::VS_Override;
1337
1338 if (II == Ident_final)
1339 return VirtSpecifiers::VS_Final;
1340 }
1341
1342 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001343}
1344
1345/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1346///
1347/// virt-specifier-seq:
1348/// virt-specifier
1349/// virt-specifier-seq virt-specifier
Anders Carlsson56104902011-01-17 03:05:47 +00001350void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) {
Anders Carlsson56104902011-01-17 03:05:47 +00001351 while (true) {
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001352 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001353 if (Specifier == VirtSpecifiers::VS_None)
1354 return;
1355
1356 // C++ [class.mem]p8:
1357 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001358 const char *PrevSpec = 0;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001359 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001360 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1361 << PrevSpec
1362 << FixItHint::CreateRemoval(Tok.getLocation());
1363
Anders Carlsson5a72fdb2011-01-22 23:01:49 +00001364 if (!getLang().CPlusPlus0x)
1365 Diag(Tok.getLocation(), diag::ext_override_control_keyword)
1366 << VirtSpecifiers::getSpecifierName(Specifier);
Anders Carlsson56104902011-01-17 03:05:47 +00001367 ConsumeToken();
1368 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001369}
1370
Anders Carlssoncafbab72011-03-25 14:53:29 +00001371/// isCXX0XFinalKeyword - Determine whether the next token is a C++0x
1372/// contextual 'final' keyword.
1373bool Parser::isCXX0XFinalKeyword() const {
Anders Carlsson5a72fdb2011-01-22 23:01:49 +00001374 if (!getLang().CPlusPlus)
Anders Carlssoncafbab72011-03-25 14:53:29 +00001375 return false;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001376
Anders Carlssoncafbab72011-03-25 14:53:29 +00001377 if (!Tok.is(tok::identifier))
1378 return false;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001379
Anders Carlssoncafbab72011-03-25 14:53:29 +00001380 // Initialize the contextual keywords.
1381 if (!Ident_final) {
1382 Ident_final = &PP.getIdentifierTable().get("final");
1383 Ident_override = &PP.getIdentifierTable().get("override");
1384 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001385
Anders Carlssoncafbab72011-03-25 14:53:29 +00001386 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001387}
1388
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001389/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1390///
1391/// member-declaration:
1392/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1393/// function-definition ';'[opt]
1394/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1395/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001396/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001397/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001398/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001399///
1400/// member-declarator-list:
1401/// member-declarator
1402/// member-declarator-list ',' member-declarator
1403///
1404/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001405/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001406/// declarator constant-initializer[opt]
1407/// identifier[opt] ':' constant-expression
1408///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001409/// virt-specifier-seq:
1410/// virt-specifier
1411/// virt-specifier-seq virt-specifier
1412///
1413/// virt-specifier:
1414/// override
1415/// final
1416/// new
1417///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001418/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001419/// '= 0'
1420///
1421/// constant-initializer:
1422/// '=' constant-expression
1423///
Douglas Gregor3447e762009-08-20 22:52:58 +00001424void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCall796c2a52010-07-16 08:13:16 +00001425 const ParsedTemplateInfo &TemplateInfo,
1426 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00001427 if (Tok.is(tok::at)) {
1428 if (getLang().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
1429 Diag(Tok, diag::err_at_defs_cxx);
1430 else
1431 Diag(Tok, diag::err_at_in_class);
1432
1433 ConsumeToken();
1434 SkipUntil(tok::r_brace);
1435 return;
1436 }
1437
John McCalla0097262009-12-11 02:10:03 +00001438 // Access declarations.
1439 if (!TemplateInfo.Kind &&
1440 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall1f476a12010-02-26 08:45:28 +00001441 !TryAnnotateCXXScopeToken() &&
John McCalla0097262009-12-11 02:10:03 +00001442 Tok.is(tok::annot_cxxscope)) {
1443 bool isAccessDecl = false;
1444 if (NextToken().is(tok::identifier))
1445 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1446 else
1447 isAccessDecl = NextToken().is(tok::kw_operator);
1448
1449 if (isAccessDecl) {
1450 // Collect the scope specifier token we annotated earlier.
1451 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001452 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
John McCalla0097262009-12-11 02:10:03 +00001453
1454 // Try to parse an unqualified-id.
1455 UnqualifiedId Name;
John McCallba7bf592010-08-24 05:47:05 +00001456 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) {
John McCalla0097262009-12-11 02:10:03 +00001457 SkipUntil(tok::semi);
1458 return;
1459 }
1460
1461 // TODO: recover from mistakenly-qualified operator declarations.
1462 if (ExpectAndConsume(tok::semi,
1463 diag::err_expected_semi_after,
1464 "access declaration",
1465 tok::semi))
1466 return;
1467
Douglas Gregor0be31a22010-07-02 17:43:08 +00001468 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCalla0097262009-12-11 02:10:03 +00001469 false, SourceLocation(),
1470 SS, Name,
1471 /* AttrList */ 0,
1472 /* IsTypeName */ false,
1473 SourceLocation());
1474 return;
1475 }
1476 }
1477
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001478 // static_assert-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001479 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001480 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00001481 SourceLocation DeclEnd;
1482 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001483 return;
1484 }
Mike Stump11289f42009-09-09 15:08:12 +00001485
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001486 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00001487 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00001488 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00001489 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00001490 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001491 AS);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001492 return;
1493 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001494
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001495 // Handle: member-declaration ::= '__extension__' member-declaration
1496 if (Tok.is(tok::kw___extension__)) {
1497 // __extension__ silences extension warnings in the subexpression.
1498 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1499 ConsumeToken();
John McCall796c2a52010-07-16 08:13:16 +00001500 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001501 }
Douglas Gregorfec52632009-06-20 00:51:54 +00001502
Chris Lattnercf251412010-02-02 01:23:29 +00001503 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1504 // is a bitfield.
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001505 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001506
John McCall084e83d2011-03-24 11:26:52 +00001507 ParsedAttributesWithRange attrs(AttrFactory);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001508 // Optional C++0x attribute-specifier
John McCall53fa7142010-12-24 02:08:15 +00001509 MaybeParseCXX0XAttributes(attrs);
1510 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001511
Douglas Gregorfec52632009-06-20 00:51:54 +00001512 if (Tok.is(tok::kw_using)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001513 // FIXME: Check for template aliases
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001514
John McCall53fa7142010-12-24 02:08:15 +00001515 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001516
Douglas Gregorfec52632009-06-20 00:51:54 +00001517 // Eat 'using'.
1518 SourceLocation UsingLoc = ConsumeToken();
1519
1520 if (Tok.is(tok::kw_namespace)) {
1521 Diag(UsingLoc, diag::err_using_namespace_in_class);
1522 SkipUntil(tok::semi, true, true);
Chris Lattner916dbf12010-02-02 00:43:15 +00001523 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00001524 SourceLocation DeclEnd;
1525 // Otherwise, it must be using-declaration.
John McCall9b72f892010-11-10 02:40:36 +00001526 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1527 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00001528 }
1529 return;
1530 }
1531
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001532 // decl-specifier-seq:
1533 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00001534 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00001535 DS.takeAttributesFrom(attrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00001536 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001537
John McCallfaf5fb42010-08-26 23:41:50 +00001538 MultiTemplateParamsArg TemplateParams(Actions,
John McCall11083da2009-09-16 22:47:08 +00001539 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1540 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1541
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001542 if (Tok.is(tok::semi)) {
1543 ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001544 Decl *TheDecl =
John McCall796c2a52010-07-16 08:13:16 +00001545 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
1546 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00001547 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001548 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001549
John McCall28a6aea2009-11-04 02:18:39 +00001550 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00001551 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001552
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001553 if (Tok.isNot(tok::colon)) {
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001554 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1555 ColonProtectionRAIIObject X(*this);
1556
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001557 // Parse the first declarator.
1558 ParseDeclarator(DeclaratorInfo);
1559 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +00001560 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001561 // If so, skip until the semi-colon or a }.
Sebastian Redl83f3b852011-04-24 16:27:48 +00001562 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001563 if (Tok.is(tok::semi))
1564 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001565 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001566 }
1567
Nico Weber24b2a822011-01-28 06:07:34 +00001568 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1569
John Thompson5bc5cbe2009-11-25 22:58:06 +00001570 // If attributes exist after the declarator, but before an '{', parse them.
John McCall53fa7142010-12-24 02:08:15 +00001571 MaybeParseGNUAttributes(DeclaratorInfo);
John Thompson5bc5cbe2009-11-25 22:58:06 +00001572
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001573 // function-definition:
Douglas Gregore8381c02008-11-05 04:29:56 +00001574 if (Tok.is(tok::l_brace)
Sebastian Redla7b98a72009-04-26 20:35:05 +00001575 || (DeclaratorInfo.isFunctionDeclarator() &&
1576 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001577 if (!DeclaratorInfo.isFunctionDeclarator()) {
1578 Diag(Tok, diag::err_func_def_no_params);
1579 ConsumeBrace();
1580 SkipUntil(tok::r_brace, true);
Douglas Gregor8a4db832011-01-19 16:41:58 +00001581
1582 // Consume the optional ';'
1583 if (Tok.is(tok::semi))
1584 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001585 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001586 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001587
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001588 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1589 Diag(Tok, diag::err_function_declared_typedef);
1590 // This recovery skips the entire function body. It would be nice
1591 // to simply call ParseCXXInlineMethodDef() below, however Sema
1592 // assumes the declarator represents a function, not a typedef.
1593 ConsumeBrace();
1594 SkipUntil(tok::r_brace, true);
Douglas Gregor8a4db832011-01-19 16:41:58 +00001595
1596 // Consume the optional ';'
1597 if (Tok.is(tok::semi))
1598 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001599 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001600 }
1601
Nico Weber24b2a822011-01-28 06:07:34 +00001602 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo, VS);
Douglas Gregor8a4db832011-01-19 16:41:58 +00001603 // Consume the optional ';'
1604 if (Tok.is(tok::semi))
1605 ConsumeToken();
1606
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001607 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001608 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001609 }
1610
1611 // member-declarator-list:
1612 // member-declarator
1613 // member-declarator-list ',' member-declarator
1614
John McCall48871652010-08-21 09:40:31 +00001615 llvm::SmallVector<Decl *, 8> DeclsInGroup;
John McCalldadc5752010-08-24 06:29:42 +00001616 ExprResult BitfieldSize;
1617 ExprResult Init;
Sebastian Redl42e92c42009-04-12 17:16:29 +00001618 bool Deleted = false;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001619
1620 while (1) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001621 // member-declarator:
1622 // declarator pure-specifier[opt]
1623 // declarator constant-initializer[opt]
1624 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001625 if (Tok.is(tok::colon)) {
1626 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001627 BitfieldSize = ParseConstantExpression();
1628 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001629 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001630 }
Mike Stump11289f42009-09-09 15:08:12 +00001631
Anders Carlsson56104902011-01-17 03:05:47 +00001632 ParseOptionalCXX0XVirtSpecifierSeq(VS);
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001633
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001634 // pure-specifier:
1635 // '= 0'
1636 //
1637 // constant-initializer:
1638 // '=' constant-expression
Sebastian Redl42e92c42009-04-12 17:16:29 +00001639 //
1640 // defaulted/deleted function-definition:
1641 // '=' 'default' [TODO]
1642 // '=' 'delete'
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001643 if (Tok.is(tok::equal)) {
1644 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001645 if (Tok.is(tok::kw_delete)) {
1646 if (!getLang().CPlusPlus0x)
1647 Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
Sebastian Redl42e92c42009-04-12 17:16:29 +00001648 ConsumeToken();
1649 Deleted = true;
1650 } else {
1651 Init = ParseInitializer();
1652 if (Init.isInvalid())
1653 SkipUntil(tok::comma, true, true);
1654 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001655 }
1656
Chris Lattnerf3d3b362010-06-13 05:34:18 +00001657 // If a simple-asm-expr is present, parse it.
1658 if (Tok.is(tok::kw_asm)) {
1659 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001660 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnerf3d3b362010-06-13 05:34:18 +00001661 if (AsmLabel.isInvalid())
1662 SkipUntil(tok::comma, true, true);
1663
1664 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1665 DeclaratorInfo.SetRangeEnd(Loc);
1666 }
1667
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001668 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001669 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001670
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001671 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001672 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001673 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00001674
John McCall48871652010-08-21 09:40:31 +00001675 Decl *ThisDecl = 0;
John McCall07e91c02009-08-06 02:15:43 +00001676 if (DS.isFriendSpecified()) {
John McCall2f212b32009-09-11 21:02:39 +00001677 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor0be31a22010-07-02 17:43:08 +00001678 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCall2f212b32009-09-11 21:02:39 +00001679 /*IsDefinition*/ false,
1680 move(TemplateParams));
Douglas Gregor3447e762009-08-20 22:52:58 +00001681 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001682 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00001683 DeclaratorInfo,
Douglas Gregor3447e762009-08-20 22:52:58 +00001684 move(TemplateParams),
John McCall07e91c02009-08-06 02:15:43 +00001685 BitfieldSize.release(),
Anders Carlssondb36b802011-01-20 03:57:25 +00001686 VS, Init.release(),
Sebastian Redld6f78502009-11-24 23:38:44 +00001687 /*IsDefinition*/Deleted,
John McCall07e91c02009-08-06 02:15:43 +00001688 Deleted);
Douglas Gregor3447e762009-08-20 22:52:58 +00001689 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001690 if (ThisDecl)
1691 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001692
Douglas Gregor4d87df52008-12-16 21:30:33 +00001693 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump11289f42009-09-09 15:08:12 +00001694 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor4d87df52008-12-16 21:30:33 +00001695 != DeclSpec::SCS_typedef) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001696 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor4d87df52008-12-16 21:30:33 +00001697 }
1698
John McCall28a6aea2009-11-04 02:18:39 +00001699 DeclaratorInfo.complete(ThisDecl);
1700
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001701 // If we don't have a comma, it is either the end of the list (a ';')
1702 // or an error, bail out.
1703 if (Tok.isNot(tok::comma))
1704 break;
Mike Stump11289f42009-09-09 15:08:12 +00001705
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001706 // Consume the comma.
1707 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001708
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001709 // Parse the next declarator.
1710 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00001711 VS.clear();
Sebastian Redlc13f2682008-12-09 20:22:58 +00001712 BitfieldSize = 0;
1713 Init = 0;
Sebastian Redl42e92c42009-04-12 17:16:29 +00001714 Deleted = false;
Mike Stump11289f42009-09-09 15:08:12 +00001715
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001716 // Attributes are only allowed on the second declarator.
John McCall53fa7142010-12-24 02:08:15 +00001717 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001718
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001719 if (Tok.isNot(tok::colon))
1720 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001721 }
1722
Chris Lattner916dbf12010-02-02 00:43:15 +00001723 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1724 // Skip to end of block or statement.
1725 SkipUntil(tok::r_brace, true, true);
1726 // If we stopped at a ';', eat it.
1727 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001728 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001729 }
1730
Douglas Gregor0be31a22010-07-02 17:43:08 +00001731 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattner916dbf12010-02-02 00:43:15 +00001732 DeclsInGroup.size());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001733}
1734
1735/// ParseCXXMemberSpecification - Parse the class definition.
1736///
1737/// member-specification:
1738/// member-declaration member-specification[opt]
1739/// access-specifier ':' member-specification[opt]
1740///
1741void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00001742 unsigned TagType, Decl *TagDecl) {
Sanjiv Guptad7959242008-10-31 09:52:39 +00001743 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001744 TagType == DeclSpec::TST_union ||
Sanjiv Guptad7959242008-10-31 09:52:39 +00001745 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001746
John McCallfaf5fb42010-08-26 23:41:50 +00001747 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1748 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00001749
Douglas Gregoredf8f392010-01-16 20:52:59 +00001750 // Determine whether this is a non-nested class. Note that local
1751 // classes are *not* considered to be nested classes.
1752 bool NonNestedClass = true;
1753 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001754 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00001755 if (S->isClassScope()) {
1756 // We're inside a class scope, so this is a nested class.
1757 NonNestedClass = false;
1758 break;
1759 }
1760
1761 if ((S->getFlags() & Scope::FnScope)) {
1762 // If we're in a function or function template declared in the
1763 // body of a class, then this is a local class rather than a
1764 // nested class.
1765 const Scope *Parent = S->getParent();
1766 if (Parent->isTemplateParamScope())
1767 Parent = Parent->getParent();
1768 if (Parent->isClassScope())
1769 break;
1770 }
1771 }
1772 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001773
1774 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00001775 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001776
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001777 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregoredf8f392010-01-16 20:52:59 +00001778 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001779
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001780 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001781 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00001782
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00001783 SourceLocation FinalLoc;
1784
1785 // Parse the optional 'final' keyword.
1786 if (getLang().CPlusPlus && Tok.is(tok::identifier)) {
1787 IdentifierInfo *II = Tok.getIdentifierInfo();
1788
1789 // Initialize the contextual keywords.
1790 if (!Ident_final) {
1791 Ident_final = &PP.getIdentifierTable().get("final");
1792 Ident_override = &PP.getIdentifierTable().get("override");
1793 }
1794
1795 if (II == Ident_final)
1796 FinalLoc = ConsumeToken();
1797
1798 if (!getLang().CPlusPlus0x)
1799 Diag(FinalLoc, diag::ext_override_control_keyword) << "final";
1800 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001801
John McCall2d814c32009-12-19 21:48:58 +00001802 if (Tok.is(tok::colon)) {
1803 ParseBaseClause(TagDecl);
1804
1805 if (!Tok.is(tok::l_brace)) {
1806 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCall2ff380a2010-03-17 00:38:33 +00001807
1808 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001809 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00001810 return;
1811 }
1812 }
1813
1814 assert(Tok.is(tok::l_brace));
1815
1816 SourceLocation LBraceLoc = ConsumeBrace();
1817
John McCall08bede42010-05-28 08:11:17 +00001818 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00001819 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Anders Carlssonfc1eef42011-01-22 17:51:53 +00001820 LBraceLoc);
John McCall1c7e6ec2009-12-20 07:58:13 +00001821
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001822 // C++ 11p3: Members of a class defined with the keyword class are private
1823 // by default. Members of a class defined with the keywords struct or union
1824 // are public by default.
1825 AccessSpecifier CurAS;
1826 if (TagType == DeclSpec::TST_class)
1827 CurAS = AS_private;
1828 else
1829 CurAS = AS_public;
1830
Douglas Gregor9377c822010-06-21 22:31:09 +00001831 SourceLocation RBraceLoc;
1832 if (TagDecl) {
1833 // While we still have something to read, read the member-declarations.
1834 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1835 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001836
Douglas Gregor9377c822010-06-21 22:31:09 +00001837 // Check for extraneous top-level semicolon.
1838 if (Tok.is(tok::semi)) {
1839 Diag(Tok, diag::ext_extra_struct_semi)
1840 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1841 << FixItHint::CreateRemoval(Tok.getLocation());
1842 ConsumeToken();
1843 continue;
1844 }
1845
1846 AccessSpecifier AS = getAccessSpecifierIfPresent();
1847 if (AS != AS_none) {
1848 // Current token is a C++ access specifier.
1849 CurAS = AS;
1850 SourceLocation ASLoc = Tok.getLocation();
1851 ConsumeToken();
1852 if (Tok.is(tok::colon))
1853 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1854 else
1855 Diag(Tok, diag::err_expected_colon);
1856 ConsumeToken();
1857 continue;
1858 }
1859
1860 // FIXME: Make sure we don't have a template here.
1861
1862 // Parse all the comma separated declarators.
1863 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001864 }
1865
Douglas Gregor9377c822010-06-21 22:31:09 +00001866 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1867 } else {
1868 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001869 }
Mike Stump11289f42009-09-09 15:08:12 +00001870
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001871 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00001872 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001873 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001874
John McCall08bede42010-05-28 08:11:17 +00001875 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001876 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall08bede42010-05-28 08:11:17 +00001877 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00001878 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001879
1880 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1881 // complete within function bodies, default arguments,
1882 // exception-specifications, and constructor ctor-initializers (including
1883 // such things in nested classes).
1884 //
Douglas Gregor4d87df52008-12-16 21:30:33 +00001885 // FIXME: Only function bodies and constructor ctor-initializers are
1886 // parsed correctly, fix the rest.
Douglas Gregor9377c822010-06-21 22:31:09 +00001887 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001888 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00001889 // are complete and we can parse the delayed portions of method
1890 // declarations and the lexed inline method definitions.
Douglas Gregor428119e2010-06-16 23:45:56 +00001891 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001892 ParseLexedMethodDeclarations(getCurrentClass());
1893 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00001894 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001895 }
1896
John McCall08bede42010-05-28 08:11:17 +00001897 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001898 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCall2ff380a2010-03-17 00:38:33 +00001899
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001900 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001901 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001902 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001903}
Douglas Gregore8381c02008-11-05 04:29:56 +00001904
1905/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1906/// which explicitly initializes the members or base classes of a
1907/// class (C++ [class.base.init]). For example, the three initializers
1908/// after the ':' in the Derived constructor below:
1909///
1910/// @code
1911/// class Base { };
1912/// class Derived : Base {
1913/// int x;
1914/// float f;
1915/// public:
1916/// Derived(float f) : Base(), x(17), f(f) { }
1917/// };
1918/// @endcode
1919///
Mike Stump11289f42009-09-09 15:08:12 +00001920/// [C++] ctor-initializer:
1921/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00001922///
Mike Stump11289f42009-09-09 15:08:12 +00001923/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00001924/// mem-initializer ...[opt]
1925/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00001926void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00001927 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1928
John Wiegley1c0675e2011-04-28 01:08:34 +00001929 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
1930 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00001931 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001932
Alexis Hunt1d792652011-01-08 20:30:50 +00001933 llvm::SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00001934 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001935
Douglas Gregore8381c02008-11-05 04:29:56 +00001936 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00001937 if (Tok.is(tok::code_completion)) {
1938 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
1939 MemInitializers.data(),
1940 MemInitializers.size());
1941 ConsumeCodeCompletionToken();
1942 } else {
1943 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
1944 if (!MemInit.isInvalid())
1945 MemInitializers.push_back(MemInit.get());
1946 else
1947 AnyErrors = true;
1948 }
1949
Douglas Gregore8381c02008-11-05 04:29:56 +00001950 if (Tok.is(tok::comma))
1951 ConsumeToken();
1952 else if (Tok.is(tok::l_brace))
1953 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00001954 // If the next token looks like a base or member initializer, assume that
1955 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00001956 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
1957 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
1958 Diag(Loc, diag::err_ctor_init_missing_comma)
1959 << FixItHint::CreateInsertion(Loc, ", ");
1960 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00001961 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redla7b98a72009-04-26 20:35:05 +00001962 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregore8381c02008-11-05 04:29:56 +00001963 SkipUntil(tok::l_brace, true, true);
1964 break;
1965 }
1966 } while (true);
1967
Mike Stump11289f42009-09-09 15:08:12 +00001968 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00001969 MemInitializers.data(), MemInitializers.size(),
1970 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00001971}
1972
1973/// ParseMemInitializer - Parse a C++ member initializer, which is
1974/// part of a constructor initializer that explicitly initializes one
1975/// member or base class (C++ [class.base.init]). See
1976/// ParseConstructorInitializer for an example.
1977///
1978/// [C++] mem-initializer:
1979/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump11289f42009-09-09 15:08:12 +00001980///
Douglas Gregore8381c02008-11-05 04:29:56 +00001981/// [C++] mem-initializer-id:
1982/// '::'[opt] nested-name-specifier[opt] class-name
1983/// identifier
John McCall48871652010-08-21 09:40:31 +00001984Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00001985 // parse '::'[opt] nested-name-specifier[opt]
1986 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001987 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
1988 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001989 if (Tok.is(tok::annot_template_id)) {
1990 TemplateIdAnnotation *TemplateId
1991 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregor46c59612010-01-12 17:52:59 +00001992 if (TemplateId->Kind == TNK_Type_template ||
1993 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00001994 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001995 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00001996 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001997 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001998 }
1999 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002000 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00002001 return true;
2002 }
Mike Stump11289f42009-09-09 15:08:12 +00002003
Douglas Gregore8381c02008-11-05 04:29:56 +00002004 // Get the identifier. This may be a member name or a class name,
2005 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002006 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregore8381c02008-11-05 04:29:56 +00002007 SourceLocation IdLoc = ConsumeToken();
2008
2009 // Parse the '('.
2010 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002011 Diag(Tok, diag::err_expected_lparen);
Douglas Gregore8381c02008-11-05 04:29:56 +00002012 return true;
2013 }
2014 SourceLocation LParenLoc = ConsumeParen();
2015
2016 // Parse the optional expression-list.
Sebastian Redl511ed552008-11-25 22:21:31 +00002017 ExprVector ArgExprs(Actions);
Douglas Gregore8381c02008-11-05 04:29:56 +00002018 CommaLocsTy CommaLocs;
2019 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2020 SkipUntil(tok::r_paren);
2021 return true;
2022 }
2023
2024 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2025
Douglas Gregor44e7df62011-01-04 00:32:56 +00002026 SourceLocation EllipsisLoc;
2027 if (Tok.is(tok::ellipsis))
2028 EllipsisLoc = ConsumeToken();
2029
Douglas Gregor0be31a22010-07-02 17:43:08 +00002030 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002031 TemplateTypeTy, IdLoc,
Sebastian Redl511ed552008-11-25 22:21:31 +00002032 LParenLoc, ArgExprs.take(),
Douglas Gregor44e7df62011-01-04 00:32:56 +00002033 ArgExprs.size(), RParenLoc,
2034 EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00002035}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002036
Sebastian Redl965b0e32011-03-05 14:45:16 +00002037/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002038///
Douglas Gregor356513d2008-12-01 18:00:20 +00002039/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00002040/// dynamic-exception-specification
2041/// noexcept-specification
2042///
2043/// noexcept-specification:
2044/// 'noexcept'
2045/// 'noexcept' '(' constant-expression ')'
2046ExceptionSpecificationType
2047Parser::MaybeParseExceptionSpecification(SourceRange &SpecificationRange,
2048 llvm::SmallVectorImpl<ParsedType> &DynamicExceptions,
2049 llvm::SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
2050 ExprResult &NoexceptExpr) {
2051 ExceptionSpecificationType Result = EST_None;
2052
2053 // See if there's a dynamic specification.
2054 if (Tok.is(tok::kw_throw)) {
2055 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2056 DynamicExceptions,
2057 DynamicExceptionRanges);
2058 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2059 "Produced different number of exception types and ranges.");
2060 }
2061
2062 // If there's no noexcept specification, we're done.
2063 if (Tok.isNot(tok::kw_noexcept))
2064 return Result;
2065
2066 // If we already had a dynamic specification, parse the noexcept for,
2067 // recovery, but emit a diagnostic and don't store the results.
2068 SourceRange NoexceptRange;
2069 ExceptionSpecificationType NoexceptType = EST_None;
2070
2071 SourceLocation KeywordLoc = ConsumeToken();
2072 if (Tok.is(tok::l_paren)) {
2073 // There is an argument.
2074 SourceLocation LParenLoc = ConsumeParen();
2075 NoexceptType = EST_ComputedNoexcept;
2076 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002077 // The argument must be contextually convertible to bool. We use
2078 // ActOnBooleanCondition for this purpose.
2079 if (!NoexceptExpr.isInvalid())
2080 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2081 NoexceptExpr.get());
Sebastian Redl965b0e32011-03-05 14:45:16 +00002082 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2083 NoexceptRange = SourceRange(KeywordLoc, RParenLoc);
2084 } else {
2085 // There is no argument.
2086 NoexceptType = EST_BasicNoexcept;
2087 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2088 }
2089
2090 if (Result == EST_None) {
2091 SpecificationRange = NoexceptRange;
2092 Result = NoexceptType;
2093
2094 // If there's a dynamic specification after a noexcept specification,
2095 // parse that and ignore the results.
2096 if (Tok.is(tok::kw_throw)) {
2097 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2098 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2099 DynamicExceptionRanges);
2100 }
2101 } else {
2102 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2103 }
2104
2105 return Result;
2106}
2107
2108/// ParseDynamicExceptionSpecification - Parse a C++
2109/// dynamic-exception-specification (C++ [except.spec]).
2110///
2111/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00002112/// 'throw' '(' type-id-list [opt] ')'
2113/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00002114///
Douglas Gregor356513d2008-12-01 18:00:20 +00002115/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00002116/// type-id ... [opt]
2117/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002118///
Sebastian Redl965b0e32011-03-05 14:45:16 +00002119ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2120 SourceRange &SpecificationRange,
2121 llvm::SmallVectorImpl<ParsedType> &Exceptions,
2122 llvm::SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002123 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00002124
Sebastian Redl965b0e32011-03-05 14:45:16 +00002125 SpecificationRange.setBegin(ConsumeToken());
Mike Stump11289f42009-09-09 15:08:12 +00002126
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002127 if (!Tok.is(tok::l_paren)) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002128 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2129 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002130 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002131 }
2132 SourceLocation LParenLoc = ConsumeParen();
2133
Douglas Gregor356513d2008-12-01 18:00:20 +00002134 // Parse throw(...), a Microsoft extension that means "this function
2135 // can throw anything".
2136 if (Tok.is(tok::ellipsis)) {
2137 SourceLocation EllipsisLoc = ConsumeToken();
2138 if (!getLang().Microsoft)
2139 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redl965b0e32011-03-05 14:45:16 +00002140 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2141 SpecificationRange.setEnd(RParenLoc);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002142 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00002143 }
2144
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002145 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00002146 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002147 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00002148 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00002149
Douglas Gregor830837d2010-12-20 23:57:46 +00002150 if (Tok.is(tok::ellipsis)) {
2151 // C++0x [temp.variadic]p5:
2152 // - In a dynamic-exception-specification (15.4); the pattern is a
2153 // type-id.
2154 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00002155 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00002156 if (!Res.isInvalid())
2157 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2158 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00002159
Sebastian Redld6434562009-05-29 18:02:33 +00002160 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002161 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00002162 Ranges.push_back(Range);
2163 }
Douglas Gregor830837d2010-12-20 23:57:46 +00002164
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002165 if (Tok.is(tok::comma))
2166 ConsumeToken();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002167 else
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002168 break;
2169 }
2170
Sebastian Redl965b0e32011-03-05 14:45:16 +00002171 SpecificationRange.setEnd(MatchRHSPunctuation(tok::r_paren, LParenLoc));
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002172 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002173}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002174
Douglas Gregor7fb25412010-10-01 18:44:50 +00002175/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2176/// function declaration.
2177TypeResult Parser::ParseTrailingReturnType() {
2178 assert(Tok.is(tok::arrow) && "expected arrow");
2179
2180 ConsumeToken();
2181
2182 // FIXME: Need to suppress declarations when parsing this typename.
2183 // Otherwise in this function definition:
2184 //
2185 // auto f() -> struct X {}
2186 //
2187 // struct X is parsed as class definition because of the trailing
2188 // brace.
2189
2190 SourceRange Range;
2191 return ParseTypeName(&Range);
2192}
2193
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002194/// \brief We have just started parsing the definition of a new class,
2195/// so push that class onto our stack of classes that is currently
2196/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00002197Sema::ParsingClassState
2198Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002199 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002200 "Nested class without outer class");
Douglas Gregoredf8f392010-01-16 20:52:59 +00002201 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
John McCallc1465822011-02-14 07:13:47 +00002202 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002203}
2204
2205/// \brief Deallocate the given parsed class and all of its nested
2206/// classes.
2207void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00002208 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2209 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002210 delete Class;
2211}
2212
2213/// \brief Pop the top class of the stack of classes that are
2214/// currently being parsed.
2215///
2216/// This routine should be called when we have finished parsing the
2217/// definition of a class, but have not yet popped the Scope
2218/// associated with the class's definition.
2219///
2220/// \returns true if the class we've popped is a top-level class,
2221/// false otherwise.
John McCallc1465822011-02-14 07:13:47 +00002222void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002223 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00002224
John McCallc1465822011-02-14 07:13:47 +00002225 Actions.PopParsingClass(state);
2226
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002227 ParsingClass *Victim = ClassStack.top();
2228 ClassStack.pop();
2229 if (Victim->TopLevelClass) {
2230 // Deallocate all of the nested classes of this class,
2231 // recursively: we don't need to keep any of this information.
2232 DeallocateParsedClasses(Victim);
2233 return;
Mike Stump11289f42009-09-09 15:08:12 +00002234 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002235 assert(!ClassStack.empty() && "Missing top-level class?");
2236
Douglas Gregorefc46952010-10-12 16:25:54 +00002237 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002238 // The victim is a nested class, but we will not need to perform
2239 // any processing after the definition of this class since it has
2240 // no members whose handling was delayed. Therefore, we can just
2241 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00002242 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002243 return;
2244 }
2245
2246 // This nested class has some members that will need to be processed
2247 // after the top-level class is completely defined. Therefore, add
2248 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002249 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00002250 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00002251 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002252}
Alexis Hunt96d5c762009-11-21 08:43:09 +00002253
2254/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
2255/// parses standard attributes.
2256///
2257/// [C++0x] attribute-specifier:
2258/// '[' '[' attribute-list ']' ']'
2259///
2260/// [C++0x] attribute-list:
2261/// attribute[opt]
2262/// attribute-list ',' attribute[opt]
2263///
2264/// [C++0x] attribute:
2265/// attribute-token attribute-argument-clause[opt]
2266///
2267/// [C++0x] attribute-token:
2268/// identifier
2269/// attribute-scoped-token
2270///
2271/// [C++0x] attribute-scoped-token:
2272/// attribute-namespace '::' identifier
2273///
2274/// [C++0x] attribute-namespace:
2275/// identifier
2276///
2277/// [C++0x] attribute-argument-clause:
2278/// '(' balanced-token-seq ')'
2279///
2280/// [C++0x] balanced-token-seq:
2281/// balanced-token
2282/// balanced-token-seq balanced-token
2283///
2284/// [C++0x] balanced-token:
2285/// '(' balanced-token-seq ')'
2286/// '[' balanced-token-seq ']'
2287/// '{' balanced-token-seq '}'
2288/// any token but '(', ')', '[', ']', '{', or '}'
John McCall53fa7142010-12-24 02:08:15 +00002289void Parser::ParseCXX0XAttributes(ParsedAttributesWithRange &attrs,
2290 SourceLocation *endLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002291 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
2292 && "Not a C++0x attribute list");
2293
2294 SourceLocation StartLoc = Tok.getLocation(), Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002295
2296 ConsumeBracket();
2297 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002298
Alexis Hunt96d5c762009-11-21 08:43:09 +00002299 if (Tok.is(tok::comma)) {
2300 Diag(Tok.getLocation(), diag::err_expected_ident);
2301 ConsumeToken();
2302 }
2303
2304 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
2305 // attribute not present
2306 if (Tok.is(tok::comma)) {
2307 ConsumeToken();
2308 continue;
2309 }
2310
2311 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
2312 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002313
Alexis Hunt96d5c762009-11-21 08:43:09 +00002314 // scoped attribute
2315 if (Tok.is(tok::coloncolon)) {
2316 ConsumeToken();
2317
2318 if (!Tok.is(tok::identifier)) {
2319 Diag(Tok.getLocation(), diag::err_expected_ident);
2320 SkipUntil(tok::r_square, tok::comma, true, true);
2321 continue;
2322 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002323
Alexis Hunt96d5c762009-11-21 08:43:09 +00002324 ScopeName = AttrName;
2325 ScopeLoc = AttrLoc;
2326
2327 AttrName = Tok.getIdentifierInfo();
2328 AttrLoc = ConsumeToken();
2329 }
2330
2331 bool AttrParsed = false;
2332 // No scoped names are supported; ideally we could put all non-standard
2333 // attributes into namespaces.
2334 if (!ScopeName) {
2335 switch(AttributeList::getKind(AttrName))
2336 {
2337 // No arguments
Alexis Hunt54a02542009-11-25 04:20:27 +00002338 case AttributeList::AT_carries_dependency:
Anders Carlssone30621b2011-01-23 21:33:18 +00002339 case AttributeList::AT_noreturn: {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002340 if (Tok.is(tok::l_paren)) {
2341 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
2342 << AttrName->getName();
2343 break;
2344 }
2345
John McCall084e83d2011-03-24 11:26:52 +00002346 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc, 0,
2347 SourceLocation(), 0, 0, false, true);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002348 AttrParsed = true;
2349 break;
2350 }
2351
2352 // One argument; must be a type-id or assignment-expression
2353 case AttributeList::AT_aligned: {
2354 if (Tok.isNot(tok::l_paren)) {
2355 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
2356 << AttrName->getName();
2357 break;
2358 }
2359 SourceLocation ParamLoc = ConsumeParen();
2360
John McCalldadc5752010-08-24 06:29:42 +00002361 ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002362
2363 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2364
2365 ExprVector ArgExprs(Actions);
2366 ArgExprs.push_back(ArgExpr.release());
John McCall084e83d2011-03-24 11:26:52 +00002367 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc,
2368 0, ParamLoc, ArgExprs.take(), 1,
2369 false, true);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002370
2371 AttrParsed = true;
2372 break;
2373 }
2374
2375 // Silence warnings
2376 default: break;
2377 }
2378 }
2379
2380 // Skip the entire parameter clause, if any
2381 if (!AttrParsed && Tok.is(tok::l_paren)) {
2382 ConsumeParen();
2383 // SkipUntil maintains the balancedness of tokens.
2384 SkipUntil(tok::r_paren, false);
2385 }
2386 }
2387
2388 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2389 SkipUntil(tok::r_square, false);
2390 Loc = Tok.getLocation();
2391 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2392 SkipUntil(tok::r_square, false);
2393
John McCall53fa7142010-12-24 02:08:15 +00002394 attrs.Range = SourceRange(StartLoc, Loc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002395}
2396
2397/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2398/// attribute.
2399///
2400/// FIXME: Simply returns an alignof() expression if the argument is a
2401/// type. Ideally, the type should be propagated directly into Sema.
2402///
2403/// [C++0x] 'align' '(' type-id ')'
2404/// [C++0x] 'align' '(' assignment-expression ')'
John McCalldadc5752010-08-24 06:29:42 +00002405ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002406 if (isTypeIdInParens()) {
John McCallfaf5fb42010-08-26 23:41:50 +00002407 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002408 SourceLocation TypeLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002409 ParsedType Ty = ParseTypeName().get();
Alexis Hunt96d5c762009-11-21 08:43:09 +00002410 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbournee190dee2011-03-11 19:24:49 +00002411 return Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2412 Ty.getAsOpaquePtr(), TypeRange);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002413 } else
2414 return ParseConstantExpression();
2415}
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00002416
2417/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
2418///
2419/// [MS] ms-attribute:
2420/// '[' token-seq ']'
2421///
2422/// [MS] ms-attribute-seq:
2423/// ms-attribute[opt]
2424/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00002425void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
2426 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00002427 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
2428
2429 while (Tok.is(tok::l_square)) {
2430 ConsumeBracket();
2431 SkipUntil(tok::r_square, true, true);
John McCall53fa7142010-12-24 02:08:15 +00002432 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00002433 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
2434 }
2435}