blob: ee8690fc697b39c4550d21ca2b19b5c084658ae1 [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 McCall53fa7142010-12-24 02:08:15 +000072 ParsedAttributes attrs;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000073 if (Tok.is(tok::kw___attribute)) {
74 attrTok = Tok;
75
Chris Lattnera5235172007-08-25 06:57:03 +000076 // FIXME: save these somewhere.
John McCall53fa7142010-12-24 02:08:15 +000077 ParseGNUAttributes(attrs);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000078 }
Mike Stump11289f42009-09-09 15:08:12 +000079
Douglas Gregor6b6bba42009-06-17 19:49:00 +000080 if (Tok.is(tok::equal)) {
John McCall53fa7142010-12-24 02:08:15 +000081 if (!attrs.empty())
Douglas Gregor6b6bba42009-06-17 19:49:00 +000082 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redl67667942010-08-27 23:12:46 +000083 if (InlineLoc.isValid())
84 Diag(InlineLoc, diag::err_inline_namespace_alias)
85 << FixItHint::CreateRemoval(InlineLoc);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000086
Chris Lattner49836b42009-04-02 04:16:50 +000087 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000088 }
Mike Stump11289f42009-09-09 15:08:12 +000089
Chris Lattner4de55aa2009-03-29 14:02:43 +000090 if (Tok.isNot(tok::l_brace)) {
Mike Stump11289f42009-09-09 15:08:12 +000091 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner4de55aa2009-03-29 14:02:43 +000092 diag::err_expected_ident_lbrace);
John McCall48871652010-08-21 09:40:31 +000093 return 0;
Chris Lattnera5235172007-08-25 06:57:03 +000094 }
Mike Stump11289f42009-09-09 15:08:12 +000095
Chris Lattner4de55aa2009-03-29 14:02:43 +000096 SourceLocation LBrace = ConsumeBrace();
97
Douglas Gregor0be31a22010-07-02 17:43:08 +000098 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
99 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
100 getCurScope()->getFnParent()) {
Douglas Gregor05cfc292010-05-14 05:08:22 +0000101 Diag(LBrace, diag::err_namespace_nonnamespace_scope);
102 SkipUntil(tok::r_brace, false);
John McCall48871652010-08-21 09:40:31 +0000103 return 0;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000104 }
105
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000106 // If we're still good, complain about inline namespaces in non-C++0x now.
107 if (!getLang().CPlusPlus0x && InlineLoc.isValid())
108 Diag(InlineLoc, diag::ext_inline_namespace);
109
Chris Lattner4de55aa2009-03-29 14:02:43 +0000110 // Enter a scope for the namespace.
111 ParseScope NamespaceScope(this, Scope::DeclScope);
112
John McCall48871652010-08-21 09:40:31 +0000113 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000114 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
115 IdentLoc, Ident, LBrace, attrs.getList());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000116
John McCallfaf5fb42010-08-26 23:41:50 +0000117 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
118 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000119
Alexis Hunt96d5c762009-11-21 08:43:09 +0000120 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall53fa7142010-12-24 02:08:15 +0000121 ParsedAttributesWithRange attrs;
122 MaybeParseCXX0XAttributes(attrs);
123 MaybeParseMicrosoftAttributes(attrs);
124 ParseExternalDeclaration(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000125 }
Mike Stump11289f42009-09-09 15:08:12 +0000126
Chris Lattner4de55aa2009-03-29 14:02:43 +0000127 // Leave the namespace scope.
128 NamespaceScope.Exit();
129
Chris Lattner49836b42009-04-02 04:16:50 +0000130 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
131 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000132
Chris Lattner49836b42009-04-02 04:16:50 +0000133 DeclEnd = RBraceLoc;
Chris Lattner4de55aa2009-03-29 14:02:43 +0000134 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000135}
Chris Lattner38376f12008-01-12 07:05:38 +0000136
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000137/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
138/// alias definition.
139///
John McCall48871652010-08-21 09:40:31 +0000140Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000141 SourceLocation AliasLoc,
Chris Lattner49836b42009-04-02 04:16:50 +0000142 IdentifierInfo *Alias,
143 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000144 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000145
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000146 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000147
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000148 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000149 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000150 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000151 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000152
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000153 CXXScopeSpec SS;
154 // Parse (optional) nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000155 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000156
157 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
158 Diag(Tok, diag::err_expected_namespace_name);
159 // Skip to end of the definition and eat the ';'.
160 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000161 return 0;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000162 }
163
164 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000165 IdentifierInfo *Ident = Tok.getIdentifierInfo();
166 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000167
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000168 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000169 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000170 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
171 "", tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000172
Douglas Gregor0be31a22010-07-02 17:43:08 +0000173 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000174 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000175}
176
Chris Lattner38376f12008-01-12 07:05:38 +0000177/// ParseLinkage - We know that the current token is a string_literal
178/// and just before that, that extern was seen.
179///
180/// linkage-specification: [C++ 7.5p2: dcl.link]
181/// 'extern' string-literal '{' declaration-seq[opt] '}'
182/// 'extern' string-literal declaration
183///
Chris Lattner8ea64422010-11-09 20:15:55 +0000184Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregor15799fd2008-11-21 16:10:08 +0000185 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000186 llvm::SmallString<8> LangBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000187 bool Invalid = false;
188 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
189 if (Invalid)
John McCall48871652010-08-21 09:40:31 +0000190 return 0;
Chris Lattner38376f12008-01-12 07:05:38 +0000191
192 SourceLocation Loc = ConsumeStringToken();
Chris Lattner38376f12008-01-12 07:05:38 +0000193
Douglas Gregor07665a62009-01-05 19:45:36 +0000194 ParseScope LinkageScope(this, Scope::DeclScope);
John McCall48871652010-08-21 09:40:31 +0000195 Decl *LinkageSpec
Douglas Gregor0be31a22010-07-02 17:43:08 +0000196 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000197 DS.getSourceRange().getBegin(),
Benjamin Kramerbebee842010-05-03 13:08:54 +0000198 Loc, Lang,
Abramo Bagnaraea947882011-03-08 16:41:52 +0000199 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor07665a62009-01-05 19:45:36 +0000200 : SourceLocation());
201
John McCall53fa7142010-12-24 02:08:15 +0000202 ParsedAttributesWithRange attrs;
203 MaybeParseCXX0XAttributes(attrs);
204 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000205
Douglas Gregor07665a62009-01-05 19:45:36 +0000206 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000207 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000208 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000209 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor07665a62009-01-05 19:45:36 +0000210 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000211 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000212
Douglas Gregorb65a9132010-02-07 08:38:28 +0000213 DS.abort();
214
John McCall53fa7142010-12-24 02:08:15 +0000215 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000216
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000217 SourceLocation LBrace = ConsumeBrace();
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000218 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall53fa7142010-12-24 02:08:15 +0000219 ParsedAttributesWithRange attrs;
220 MaybeParseCXX0XAttributes(attrs);
221 MaybeParseMicrosoftAttributes(attrs);
222 ParseExternalDeclaration(attrs);
Chris Lattner38376f12008-01-12 07:05:38 +0000223 }
224
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000225 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Chris Lattner8ea64422010-11-09 20:15:55 +0000226 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
227 RBrace);
Chris Lattner38376f12008-01-12 07:05:38 +0000228}
Douglas Gregor556877c2008-04-13 21:30:24 +0000229
Douglas Gregord7c4d982008-12-30 03:27:21 +0000230/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
231/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000232Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000233 const ParsedTemplateInfo &TemplateInfo,
234 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000235 ParsedAttributesWithRange &attrs) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000236 assert(Tok.is(tok::kw_using) && "Not using token");
237
238 // Eat 'using'.
239 SourceLocation UsingLoc = ConsumeToken();
240
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000241 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000242 Actions.CodeCompleteUsing(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000243 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000244 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000245
John McCall9b72f892010-11-10 02:40:36 +0000246 // 'using namespace' means this is a using-directive.
247 if (Tok.is(tok::kw_namespace)) {
248 // Template parameters are always an error here.
249 if (TemplateInfo.Kind) {
250 SourceRange R = TemplateInfo.getSourceRange();
251 Diag(UsingLoc, diag::err_templated_using_directive)
252 << R << FixItHint::CreateRemoval(R);
253 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000254
John McCall53fa7142010-12-24 02:08:15 +0000255 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000256 }
257
258 // Otherwise, it must be a using-declaration.
259
260 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000261 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000262
John McCall9b72f892010-11-10 02:40:36 +0000263 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000264}
265
266/// ParseUsingDirective - Parse C++ using-directive, assumes
267/// that current token is 'namespace' and 'using' was already parsed.
268///
269/// using-directive: [C++ 7.3.p4: namespace.udir]
270/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
271/// namespace-name ;
272/// [GNU] using-directive:
273/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
274/// namespace-name attributes[opt] ;
275///
John McCall48871652010-08-21 09:40:31 +0000276Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000277 SourceLocation UsingLoc,
278 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000279 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000280 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
281
282 // Eat 'namespace'.
283 SourceLocation NamespcLoc = ConsumeToken();
284
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000285 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000286 Actions.CodeCompleteUsingDirective(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000287 ConsumeCodeCompletionToken();
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000288 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000289
Douglas Gregord7c4d982008-12-30 03:27:21 +0000290 CXXScopeSpec SS;
291 // Parse (optional) nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000292 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000293
Douglas Gregord7c4d982008-12-30 03:27:21 +0000294 IdentifierInfo *NamespcName = 0;
295 SourceLocation IdentLoc = SourceLocation();
296
297 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000298 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000299 Diag(Tok, diag::err_expected_namespace_name);
300 // If there was invalid namespace name, skip to end of decl, and eat ';'.
301 SkipUntil(tok::semi);
302 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCall48871652010-08-21 09:40:31 +0000303 return 0;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000304 }
Mike Stump11289f42009-09-09 15:08:12 +0000305
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000306 // Parse identifier.
307 NamespcName = Tok.getIdentifierInfo();
308 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000309
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000310 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000311 bool GNUAttr = false;
312 if (Tok.is(tok::kw___attribute)) {
313 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000314 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000315 }
Mike Stump11289f42009-09-09 15:08:12 +0000316
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000317 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000318 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000319 ExpectAndConsume(tok::semi,
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000320 GNUAttr ? diag::err_expected_semi_after_attribute_list
321 : diag::err_expected_semi_after_namespace_name,
322 "", tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000323
Douglas Gregor0be31a22010-07-02 17:43:08 +0000324 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000325 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000326}
327
328/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that
329/// 'using' was already seen.
330///
331/// using-declaration: [C++ 7.3.p3: namespace.udecl]
332/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000333/// unqualified-id
334/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000335///
John McCall48871652010-08-21 09:40:31 +0000336Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000337 const ParsedTemplateInfo &TemplateInfo,
338 SourceLocation UsingLoc,
339 SourceLocation &DeclEnd,
340 AccessSpecifier AS) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000341 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000342 SourceLocation TypenameLoc;
Douglas Gregorfec52632009-06-20 00:51:54 +0000343 bool IsTypeName;
344
John McCall9b72f892010-11-10 02:40:36 +0000345 // TODO: in C++0x, if we have template parameters this must be a
346 // template alias:
347 // template <...> using id = type;
348
Douglas Gregorfec52632009-06-20 00:51:54 +0000349 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000350 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregorfec52632009-06-20 00:51:54 +0000351 if (Tok.is(tok::kw_typename)) {
John McCalle61f2ba2009-11-18 02:36:19 +0000352 TypenameLoc = Tok.getLocation();
Douglas Gregorfec52632009-06-20 00:51:54 +0000353 ConsumeToken();
354 IsTypeName = true;
355 }
356 else
357 IsTypeName = false;
358
359 // Parse nested-name-specifier.
John McCallba7bf592010-08-24 05:47:05 +0000360 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregorfec52632009-06-20 00:51:54 +0000361
Douglas Gregorfec52632009-06-20 00:51:54 +0000362 // Check nested-name specifier.
363 if (SS.isInvalid()) {
364 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000365 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000366 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000367
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000368 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000369 // destructor names and allow the action module to diagnose any semantic
370 // errors.
371 UnqualifiedId Name;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000372 if (ParseUnqualifiedId(SS,
Douglas Gregor220f4272009-11-04 16:30:06 +0000373 /*EnteringContext=*/false,
374 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000375 /*AllowConstructorName=*/true,
John McCallba7bf592010-08-24 05:47:05 +0000376 ParsedType(),
Douglas Gregor220f4272009-11-04 16:30:06 +0000377 Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000378 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000379 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000380 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000381
Douglas Gregorfec52632009-06-20 00:51:54 +0000382 // Parse (optional) attributes (most likely GNU strong-using extension).
John McCall53fa7142010-12-24 02:08:15 +0000383 ParsedAttributes attrs;
384 MaybeParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +0000385
Douglas Gregorfec52632009-06-20 00:51:54 +0000386 // Eat ';'.
387 DeclEnd = Tok.getLocation();
388 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
John McCall53fa7142010-12-24 02:08:15 +0000389 !attrs.empty() ? "attributes list" : "using declaration",
Douglas Gregor220f4272009-11-04 16:30:06 +0000390 tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000391
John McCall9b72f892010-11-10 02:40:36 +0000392 // Diagnose an attempt to declare a templated using-declaration.
393 if (TemplateInfo.Kind) {
394 SourceRange R = TemplateInfo.getSourceRange();
395 Diag(UsingLoc, diag::err_templated_using_declaration)
396 << R << FixItHint::CreateRemoval(R);
397
398 // Unfortunately, we have to bail out instead of recovering by
399 // ignoring the parameters, just in case the nested name specifier
400 // depends on the parameters.
401 return 0;
402 }
403
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000404 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000405 Name, attrs.getList(),
406 IsTypeName, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000407}
408
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000409/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion.
410///
411/// static_assert-declaration:
412/// static_assert ( constant-expression , string-literal ) ;
413///
John McCall48871652010-08-21 09:40:31 +0000414Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000415 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration");
416 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000417
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000418 if (Tok.isNot(tok::l_paren)) {
419 Diag(Tok, diag::err_expected_lparen);
John McCall48871652010-08-21 09:40:31 +0000420 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000421 }
Mike Stump11289f42009-09-09 15:08:12 +0000422
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000423 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000424
John McCalldadc5752010-08-24 06:29:42 +0000425 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000426 if (AssertExpr.isInvalid()) {
427 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000428 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000429 }
Mike Stump11289f42009-09-09 15:08:12 +0000430
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000431 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCall48871652010-08-21 09:40:31 +0000432 return 0;
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000433
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000434 if (Tok.isNot(tok::string_literal)) {
435 Diag(Tok, diag::err_expected_string_literal);
436 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000437 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000438 }
Mike Stump11289f42009-09-09 15:08:12 +0000439
John McCalldadc5752010-08-24 06:29:42 +0000440 ExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump11289f42009-09-09 15:08:12 +0000441 if (AssertMessage.isInvalid())
John McCall48871652010-08-21 09:40:31 +0000442 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000443
Abramo Bagnaraea947882011-03-08 16:41:52 +0000444 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000445
Chris Lattner49836b42009-04-02 04:16:50 +0000446 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000447 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000448
John McCallb268a282010-08-23 23:25:46 +0000449 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
450 AssertExpr.take(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000451 AssertMessage.take(),
452 RParenLoc);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000453}
454
Anders Carlsson74948d02009-06-24 17:47:40 +0000455/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
456///
457/// 'decltype' ( expression )
458///
459void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
460 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
461
462 SourceLocation StartLoc = ConsumeToken();
463 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000464
465 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson74948d02009-06-24 17:47:40 +0000466 "decltype")) {
467 SkipUntil(tok::r_paren);
468 return;
469 }
Mike Stump11289f42009-09-09 15:08:12 +0000470
Anders Carlsson74948d02009-06-24 17:47:40 +0000471 // Parse the expression
Mike Stump11289f42009-09-09 15:08:12 +0000472
Anders Carlsson74948d02009-06-24 17:47:40 +0000473 // C++0x [dcl.type.simple]p4:
474 // The operand of the decltype specifier is an unevaluated operand.
475 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallfaf5fb42010-08-26 23:41:50 +0000476 Sema::Unevaluated);
John McCalldadc5752010-08-24 06:29:42 +0000477 ExprResult Result = ParseExpression();
Anders Carlsson74948d02009-06-24 17:47:40 +0000478 if (Result.isInvalid()) {
479 SkipUntil(tok::r_paren);
480 return;
481 }
Mike Stump11289f42009-09-09 15:08:12 +0000482
Anders Carlsson74948d02009-06-24 17:47:40 +0000483 // Match the ')'
484 SourceLocation RParenLoc;
485 if (Tok.is(tok::r_paren))
486 RParenLoc = ConsumeParen();
487 else
488 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000489
Anders Carlsson74948d02009-06-24 17:47:40 +0000490 if (RParenLoc.isInvalid())
491 return;
492
493 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000494 unsigned DiagID;
Anders Carlsson74948d02009-06-24 17:47:40 +0000495 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump11289f42009-09-09 15:08:12 +0000496 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000497 DiagID, Result.release()))
498 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson74948d02009-06-24 17:47:40 +0000499}
500
Douglas Gregor831c93f2008-11-05 20:51:48 +0000501/// ParseClassName - Parse a C++ class-name, which names a class. Note
502/// that we only check that the result names a type; semantic analysis
503/// will need to verify that the type names a class. The result is
Douglas Gregord54dfb82009-02-25 23:52:28 +0000504/// either a type or NULL, depending on whether a type name was
Douglas Gregor831c93f2008-11-05 20:51:48 +0000505/// found.
506///
507/// class-name: [C++ 9.1]
508/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000509/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000510///
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000511Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Douglas Gregore7c20652011-03-02 00:47:37 +0000512 CXXScopeSpec &SS) {
Douglas Gregord54dfb82009-02-25 23:52:28 +0000513 // Check whether we have a template-id that names a type.
514 if (Tok.is(tok::annot_template_id)) {
Mike Stump11289f42009-09-09 15:08:12 +0000515 TemplateIdAnnotation *TemplateId
Douglas Gregord54dfb82009-02-25 23:52:28 +0000516 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregor46c59612010-01-12 17:52:59 +0000517 if (TemplateId->Kind == TNK_Type_template ||
518 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000519 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000520
521 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000522 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000523 EndLocation = Tok.getAnnotationEndLoc();
524 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000525
526 if (Type)
527 return Type;
528 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000529 }
530
531 // Fall through to produce an error below.
532 }
533
Douglas Gregor831c93f2008-11-05 20:51:48 +0000534 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000535 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000536 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000537 }
538
Douglas Gregor18473f32010-01-12 21:28:44 +0000539 IdentifierInfo *Id = Tok.getIdentifierInfo();
540 SourceLocation IdLoc = ConsumeToken();
541
542 if (Tok.is(tok::less)) {
543 // It looks the user intended to write a template-id here, but the
544 // template-name was wrong. Try to fix that.
545 TemplateNameKind TNK = TNK_Type_template;
546 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000547 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000548 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000549 Diag(IdLoc, diag::err_unknown_template_name)
550 << Id;
551 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000552
Douglas Gregor18473f32010-01-12 21:28:44 +0000553 if (!Template)
554 return true;
555
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000556 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000557 UnqualifiedId TemplateName;
558 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000559
Douglas Gregor18473f32010-01-12 21:28:44 +0000560 // Parse the full template-id, then turn it into a type.
561 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
562 SourceLocation(), true))
563 return true;
564 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +0000565 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000566
Douglas Gregor18473f32010-01-12 21:28:44 +0000567 // If we didn't end up with a typename token, there's nothing more we
568 // can do.
569 if (Tok.isNot(tok::annot_typename))
570 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000571
Douglas Gregor18473f32010-01-12 21:28:44 +0000572 // Retrieve the type from the annotation token, consume that token, and
573 // return.
574 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +0000575 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +0000576 ConsumeToken();
577 return Type;
578 }
579
Douglas Gregor831c93f2008-11-05 20:51:48 +0000580 // We have an identifier; check whether it is actually a type.
Douglas Gregore7c20652011-03-02 00:47:37 +0000581 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +0000582 false, ParsedType(),
583 /*NonTrivialTypeSourceInfo=*/true);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000584 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +0000585 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000586 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000587 }
588
589 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +0000590 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000591
592 // Fake up a Declarator to use with ActOnTypeName.
593 DeclSpec DS;
594 DS.SetRangeStart(IdLoc);
595 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +0000596 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000597
598 const char *PrevSpec = 0;
599 unsigned DiagID;
600 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
601
602 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
603 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000604}
605
Douglas Gregor556877c2008-04-13 21:30:24 +0000606/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
607/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
608/// until we reach the start of a definition or see a token that
Sebastian Redl2b372722010-02-03 21:21:43 +0000609/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregor556877c2008-04-13 21:30:24 +0000610///
611/// class-specifier: [C++ class]
612/// class-head '{' member-specification[opt] '}'
613/// class-head '{' member-specification[opt] '}' attributes[opt]
614/// class-head:
615/// class-key identifier[opt] base-clause[opt]
616/// class-key nested-name-specifier identifier base-clause[opt]
617/// class-key nested-name-specifier[opt] simple-template-id
618/// base-clause[opt]
619/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000620/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +0000621/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000622/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +0000623/// simple-template-id base-clause[opt]
624/// class-key:
625/// 'class'
626/// 'struct'
627/// 'union'
628///
629/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +0000630/// class-key ::[opt] nested-name-specifier[opt] identifier
631/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
632/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +0000633///
634/// Note that the C++ class-specifier and elaborated-type-specifier,
635/// together, subsume the C99 struct-or-union-specifier:
636///
637/// struct-or-union-specifier: [C99 6.7.2.1]
638/// struct-or-union identifier[opt] '{' struct-contents '}'
639/// struct-or-union identifier
640/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
641/// '}' attributes[opt]
642/// [GNU] struct-or-union attributes[opt] identifier
643/// struct-or-union:
644/// 'struct'
645/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000646void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
647 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000648 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redl2b372722010-02-03 21:21:43 +0000649 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattnerffaa0e62009-04-12 21:49:30 +0000650 DeclSpec::TST TagType;
651 if (TagTokKind == tok::kw_struct)
652 TagType = DeclSpec::TST_struct;
653 else if (TagTokKind == tok::kw_class)
654 TagType = DeclSpec::TST_class;
655 else {
656 assert(TagTokKind == tok::kw_union && "Not a class specifier");
657 TagType = DeclSpec::TST_union;
658 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000659
Douglas Gregorf45b0cf2009-09-18 15:37:17 +0000660 if (Tok.is(tok::code_completion)) {
661 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000662 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000663 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +0000664 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000665
Chandler Carruth2d69ec72010-06-28 08:39:25 +0000666 // C++03 [temp.explicit] 14.7.2/8:
667 // The usual access checking rules do not apply to names used to specify
668 // explicit instantiations.
669 //
670 // As an extension we do not perform access checking on the names used to
671 // specify explicit specializations either. This is important to allow
672 // specializing traits classes for private types.
673 bool SuppressingAccessChecks = false;
674 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
675 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
676 Actions.ActOnStartSuppressingAccessChecks();
677 SuppressingAccessChecks = true;
678 }
679
John McCall53fa7142010-12-24 02:08:15 +0000680 ParsedAttributes attrs;
Douglas Gregor556877c2008-04-13 21:30:24 +0000681 // If attributes exist after tag, parse them.
682 if (Tok.is(tok::kw___attribute))
John McCall53fa7142010-12-24 02:08:15 +0000683 ParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +0000684
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000685 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +0000686 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +0000687 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000688
Alexis Hunt96d5c762009-11-21 08:43:09 +0000689 // If C++0x attributes exist here, parse them.
690 // FIXME: Are we consistent with the ordering of parsing of different
691 // styles of attributes?
John McCall53fa7142010-12-24 02:08:15 +0000692 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +0000693
Douglas Gregor119b0c72009-09-04 05:53:02 +0000694 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) {
695 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but
696 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump11289f42009-09-09 15:08:12 +0000697 // token sequence "struct __is_pod", make __is_pod into a normal
Douglas Gregor119b0c72009-09-04 05:53:02 +0000698 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
699 // properly.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000700 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregor119b0c72009-09-04 05:53:02 +0000701 Tok.setKind(tok::identifier);
702 }
703
704 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) {
705 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but
706 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the
Mike Stump11289f42009-09-09 15:08:12 +0000707 // token sequence "struct __is_empty", make __is_empty into a normal
Douglas Gregor119b0c72009-09-04 05:53:02 +0000708 // identifier rather than a keyword, to allow libstdc++ 4.2 to work
709 // properly.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000710 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregor119b0c72009-09-04 05:53:02 +0000711 Tok.setKind(tok::identifier);
712 }
Mike Stump11289f42009-09-09 15:08:12 +0000713
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000714 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +0000715 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000716 if (getLang().CPlusPlus) {
717 // "FOO : BAR" is not a potential typo for "FOO::BAR".
718 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000719
John McCallba7bf592010-08-24 05:47:05 +0000720 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true))
John McCall413021a2010-07-30 06:26:29 +0000721 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +0000722 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +0000723 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
724 Diag(Tok, diag::err_expected_ident);
725 }
Douglas Gregor67a65642009-02-17 23:15:12 +0000726
Douglas Gregor916462b2009-10-30 21:46:58 +0000727 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
728
Douglas Gregor67a65642009-02-17 23:15:12 +0000729 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +0000730 IdentifierInfo *Name = 0;
731 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +0000732 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +0000733 if (Tok.is(tok::identifier)) {
734 Name = Tok.getIdentifierInfo();
735 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000736
Douglas Gregord5a479c2010-05-30 22:30:21 +0000737 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000738 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +0000739 // Eat the template argument list and try to continue parsing this as
740 // a class (or template thereof).
741 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +0000742 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +0000743 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +0000744 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +0000745 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +0000746 // We couldn't parse the template argument list at all, so don't
747 // try to give any location information for the list.
748 LAngleLoc = RAngleLoc = SourceLocation();
749 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000750
Douglas Gregor916462b2009-10-30 21:46:58 +0000751 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000752 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor916462b2009-10-30 21:46:58 +0000753 << (TagType == DeclSpec::TST_class? 0
754 : TagType == DeclSpec::TST_struct? 1
755 : 2)
756 << Name
757 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000758
759 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000760 // we've removed its template argument list.
761 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
762 if (TemplateParams && TemplateParams->size() > 1) {
763 TemplateParams->pop_back();
764 } else {
765 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000766 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000767 = ParsedTemplateInfo::NonTemplate;
768 }
769 } else if (TemplateInfo.Kind
770 == ParsedTemplateInfo::ExplicitInstantiation) {
771 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +0000772 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000773 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +0000774 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000775 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +0000776 = SourceLocation();
777 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
778 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +0000779 }
Douglas Gregor916462b2009-10-30 21:46:58 +0000780 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000781 } else if (Tok.is(tok::annot_template_id)) {
782 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
783 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +0000784
Douglas Gregore7c20652011-03-02 00:47:37 +0000785 if (TemplateId->Kind != TNK_Type_template &&
786 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000787 // The template-name in the simple-template-id refers to
788 // something other than a class template. Give an appropriate
789 // error message and skip to the ';'.
790 SourceRange Range(NameLoc);
791 if (SS.isNotEmpty())
792 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +0000793
Douglas Gregor7f741122009-02-25 19:37:18 +0000794 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
795 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +0000796
Douglas Gregor7f741122009-02-25 19:37:18 +0000797 DS.SetTypeSpecError();
798 SkipUntil(tok::semi, false, true);
799 TemplateId->Destroy();
Chandler Carruth2d69ec72010-06-28 08:39:25 +0000800 if (SuppressingAccessChecks)
801 Actions.ActOnStopSuppressingAccessChecks();
802
Douglas Gregor7f741122009-02-25 19:37:18 +0000803 return;
Douglas Gregor67a65642009-02-17 23:15:12 +0000804 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000805 }
806
Chandler Carruth2d69ec72010-06-28 08:39:25 +0000807 // As soon as we're finished parsing the class's template-id, turn access
808 // checking back on.
809 if (SuppressingAccessChecks)
810 Actions.ActOnStopSuppressingAccessChecks();
811
John McCall07e91c02009-08-06 02:15:43 +0000812 // There are four options here. If we have 'struct foo;', then this
813 // is either a forward declaration or a friend declaration, which
Anders Carlsson4b63d0e2011-01-22 16:56:46 +0000814 // have to be treated differently. If we have 'struct foo {...',
815 // 'struct foo :...' or 'struct foo <class-virt-specifier>' then this is a
816 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Sebastian Redl2b372722010-02-03 21:21:43 +0000817 // However, in some contexts, things look like declarations but are just
818 // references, e.g.
819 // new struct s;
820 // or
821 // &T::operator struct s;
822 // For these, SuppressDeclarations is true.
John McCallfaf5fb42010-08-26 23:41:50 +0000823 Sema::TagUseKind TUK;
Sebastian Redl2b372722010-02-03 21:21:43 +0000824 if (SuppressDeclarations)
John McCallfaf5fb42010-08-26 23:41:50 +0000825 TUK = Sema::TUK_Reference;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +0000826 else if (Tok.is(tok::l_brace) ||
827 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
828 isCXX0XClassVirtSpecifier() != ClassVirtSpecifiers::CVS_None) {
Douglas Gregor3dad8422009-09-26 06:47:28 +0000829 if (DS.isFriendSpecified()) {
830 // C++ [class.friend]p2:
831 // A class shall not be defined in a friend declaration.
832 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
833 << SourceRange(DS.getFriendSpecLoc());
834
835 // Skip everything up to the semicolon, so that this looks like a proper
836 // friend class (or template thereof) declaration.
837 SkipUntil(tok::semi, true, true);
John McCallfaf5fb42010-08-26 23:41:50 +0000838 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +0000839 } else {
840 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +0000841 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +0000842 }
843 } else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +0000844 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregor556877c2008-04-13 21:30:24 +0000845 else
John McCallfaf5fb42010-08-26 23:41:50 +0000846 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +0000847
John McCall413021a2010-07-30 06:26:29 +0000848 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +0000849 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +0000850 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
851 // We have a declaration or reference to an anonymous class.
852 Diag(StartLoc, diag::err_anon_type_definition)
853 << DeclSpec::getSpecifierName(TagType);
854 }
Douglas Gregor556877c2008-04-13 21:30:24 +0000855
Douglas Gregor556877c2008-04-13 21:30:24 +0000856 SkipUntil(tok::comma, true);
Douglas Gregor7f741122009-02-25 19:37:18 +0000857
858 if (TemplateId)
859 TemplateId->Destroy();
Douglas Gregor556877c2008-04-13 21:30:24 +0000860 return;
861 }
862
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000863 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +0000864 DeclResult TagOrTempResult = true; // invalid
865 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000866
Douglas Gregord6ab8742009-05-28 23:31:59 +0000867 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +0000868 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000869 // Explicit specialization, class template partial specialization,
870 // or explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +0000871 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor7f741122009-02-25 19:37:18 +0000872 TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +0000873 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000874 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +0000875 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000876 // This is an explicit instantiation of a class template.
877 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +0000878 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +0000879 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000880 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000881 TagType,
Mike Stump11289f42009-09-09 15:08:12 +0000882 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000883 SS,
John McCall3e56fd42010-08-23 07:28:44 +0000884 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +0000885 TemplateId->TemplateNameLoc,
886 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000887 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +0000888 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +0000889 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +0000890
891 // Friend template-ids are treated as references unless
892 // they have template headers, in which case they're ill-formed
893 // (FIXME: "template <class T> friend class A<T>::B<int>;").
894 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +0000895 } else if (TUK == Sema::TUK_Reference ||
896 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +0000897 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000898 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType,
899 StartLoc,
900 TemplateId->SS,
901 TemplateId->Template,
902 TemplateId->TemplateNameLoc,
903 TemplateId->LAngleLoc,
904 TemplateArgsPtr,
905 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000906 } else {
907 // This is an explicit specialization or a class template
908 // partial specialization.
909 TemplateParameterLists FakedParamLists;
910
911 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
912 // This looks like an explicit instantiation, because we have
913 // something like
914 //
915 // template class Foo<X>
916 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000917 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000918 // meant to be an explicit specialization, but the user forgot
919 // the '<>' after 'template'.
John McCallfaf5fb42010-08-26 23:41:50 +0000920 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000921
Mike Stump11289f42009-09-09 15:08:12 +0000922 SourceLocation LAngleLoc
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000923 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000924 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000925 diag::err_explicit_instantiation_with_definition)
926 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregora771f462010-03-31 17:46:05 +0000927 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000928
929 // Create a fake template parameter list that contains only
930 // "template<>", so that we treat this construct as a class
931 // template specialization.
932 FakedParamLists.push_back(
Mike Stump11289f42009-09-09 15:08:12 +0000933 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000934 TemplateInfo.TemplateLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000935 LAngleLoc,
936 0, 0,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000937 LAngleLoc));
938 TemplateParams = &FakedParamLists;
939 }
940
941 // Build the class template specialization.
942 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +0000943 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor7f741122009-02-25 19:37:18 +0000944 StartLoc, SS,
John McCall3e56fd42010-08-23 07:28:44 +0000945 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +0000946 TemplateId->TemplateNameLoc,
947 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +0000948 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +0000949 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +0000950 attrs.getList(),
John McCallfaf5fb42010-08-26 23:41:50 +0000951 MultiTemplateParamsArg(Actions,
Douglas Gregor67a65642009-02-17 23:15:12 +0000952 TemplateParams? &(*TemplateParams)[0] : 0,
953 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000954 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000955 TemplateId->Destroy();
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000956 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +0000957 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000958 // Explicit instantiation of a member of a class template
959 // specialization, e.g.,
960 //
961 // template struct Outer<int>::Inner;
962 //
963 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +0000964 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +0000965 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000966 TemplateInfo.TemplateLoc,
967 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +0000968 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +0000969 } else if (TUK == Sema::TUK_Friend &&
970 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
971 TagOrTempResult =
972 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
973 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000974 Name, NameLoc, attrs.getList(),
John McCallace48cd2010-10-19 01:40:49 +0000975 MultiTemplateParamsArg(Actions,
976 TemplateParams? &(*TemplateParams)[0] : 0,
977 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000978 } else {
979 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +0000980 TUK == Sema::TUK_Definition) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000981 // FIXME: Diagnose this particular error.
982 }
983
John McCall7f41d982009-09-11 04:59:25 +0000984 bool IsDependent = false;
985
John McCall32723e92010-10-19 18:40:57 +0000986 // Don't pass down template parameter lists if this is just a tag
987 // reference. For example, we don't need the template parameters here:
988 // template <class T> class A *makeA(T t);
989 MultiTemplateParamsArg TParams;
990 if (TUK != Sema::TUK_Reference && TemplateParams)
991 TParams =
992 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
993
Douglas Gregor2ec748c2009-05-14 00:28:11 +0000994 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +0000995 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +0000996 SS, Name, NameLoc, attrs.getList(), AS,
John McCall32723e92010-10-19 18:40:57 +0000997 TParams, Owned, IsDependent, false,
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000998 false, clang::TypeResult());
John McCall7f41d982009-09-11 04:59:25 +0000999
1000 // If ActOnTag said the type was dependent, try again with the
1001 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001002 if (IsDependent) {
1003 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001004 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001005 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001006 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001007 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001008
Douglas Gregor556877c2008-04-13 21:30:24 +00001009 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001010 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001011 assert(Tok.is(tok::l_brace) ||
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001012 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
1013 isCXX0XClassVirtSpecifier() != ClassVirtSpecifiers::CVS_None);
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001014 if (getLang().CPlusPlus)
Douglas Gregorc08f4892009-03-25 00:13:59 +00001015 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001016 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001017 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001018 }
1019
John McCallba7bf592010-08-24 05:47:05 +00001020 const char *PrevSpec = 0;
1021 unsigned DiagID;
1022 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001023 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001024 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1025 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallba7bf592010-08-24 05:47:05 +00001026 PrevSpec, DiagID, TypeResult.get());
John McCall7f41d982009-09-11 04:59:25 +00001027 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001028 Result = DS.SetTypeSpecType(TagType, StartLoc,
1029 NameLoc.isValid() ? NameLoc : StartLoc,
1030 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCall7f41d982009-09-11 04:59:25 +00001031 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001032 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001033 return;
1034 }
Mike Stump11289f42009-09-09 15:08:12 +00001035
John McCallba7bf592010-08-24 05:47:05 +00001036 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001037 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001038
Chris Lattnercf251412010-02-02 01:23:29 +00001039 // At this point, we've successfully parsed a class-specifier in 'definition'
1040 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1041 // going to look at what comes after it to improve error recovery. If an
1042 // impossible token occurs next, we assume that the programmer forgot a ; at
1043 // the end of the declaration and recover that way.
1044 //
1045 // This switch enumerates the valid "follow" set for definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001046 if (TUK == Sema::TUK_Definition) {
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001047 bool ExpectedSemi = true;
Chris Lattnercf251412010-02-02 01:23:29 +00001048 switch (Tok.getKind()) {
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001049 default: break;
Chris Lattnercf251412010-02-02 01:23:29 +00001050 case tok::semi: // struct foo {...} ;
Chris Lattnerafe6a842010-02-02 17:32:27 +00001051 case tok::star: // struct foo {...} * P;
1052 case tok::amp: // struct foo {...} & R = ...
1053 case tok::identifier: // struct foo {...} V ;
1054 case tok::r_paren: //(struct foo {...} ) {4}
1055 case tok::annot_cxxscope: // struct foo {...} a:: b;
1056 case tok::annot_typename: // struct foo {...} a ::b;
1057 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattner5e854b92010-02-03 20:41:24 +00001058 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner35af0ab2010-02-03 01:45:03 +00001059 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001060 ExpectedSemi = false;
1061 break;
1062 // Type qualifiers
1063 case tok::kw_const: // struct foo {...} const x;
1064 case tok::kw_volatile: // struct foo {...} volatile x;
1065 case tok::kw_restrict: // struct foo {...} restrict x;
1066 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattnerafe6a842010-02-02 17:32:27 +00001067 // Storage-class specifiers
1068 case tok::kw_static: // struct foo {...} static x;
1069 case tok::kw_extern: // struct foo {...} extern x;
1070 case tok::kw_typedef: // struct foo {...} typedef x;
1071 case tok::kw_register: // struct foo {...} register x;
1072 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregorc9a99c52010-05-17 18:19:56 +00001073 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001074 // As shown above, type qualifiers and storage class specifiers absolutely
1075 // can occur after class specifiers according to the grammar. However,
1076 // almost noone actually writes code like this. If we see one of these,
1077 // it is much more likely that someone missed a semi colon and the
1078 // type/storage class specifier we're seeing is part of the *next*
1079 // intended declaration, as in:
1080 //
1081 // struct foo { ... }
1082 // typedef int X;
1083 //
1084 // We'd really like to emit a missing semicolon error instead of emitting
1085 // an error on the 'int' saying that you can't have two type specifiers in
1086 // the same declaration of X. Because of this, we look ahead past this
1087 // token to see if it's a type specifier. If so, we know the code is
1088 // otherwise invalid, so we can produce the expected semi error.
1089 if (!isKnownToBeTypeSpecifier(NextToken()))
1090 ExpectedSemi = false;
Chris Lattnercf251412010-02-02 01:23:29 +00001091 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001092
1093 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattnercf251412010-02-02 01:23:29 +00001094 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001095 if (!getLang().CPlusPlus)
1096 ExpectedSemi = false;
1097 break;
1098 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001099
Chris Lattnerfd48afe2010-02-28 18:18:36 +00001100 if (ExpectedSemi) {
Chris Lattnercf251412010-02-02 01:23:29 +00001101 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1102 TagType == DeclSpec::TST_class ? "class"
1103 : TagType == DeclSpec::TST_struct? "struct" : "union");
1104 // Push this token back into the preprocessor and change our current token
1105 // to ';' so that the rest of the code recovers as though there were an
1106 // ';' after the definition.
1107 PP.EnterToken(Tok);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001108 Tok.setKind(tok::semi);
Chris Lattnercf251412010-02-02 01:23:29 +00001109 }
1110 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001111}
1112
Mike Stump11289f42009-09-09 15:08:12 +00001113/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001114///
1115/// base-clause : [C++ class.derived]
1116/// ':' base-specifier-list
1117/// base-specifier-list:
1118/// base-specifier '...'[opt]
1119/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001120void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001121 assert(Tok.is(tok::colon) && "Not a base clause");
1122 ConsumeToken();
1123
Douglas Gregor29a92472008-10-22 17:49:05 +00001124 // Build up an array of parsed base specifiers.
John McCall37ad5512010-08-23 06:44:23 +00001125 llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001126
Douglas Gregor556877c2008-04-13 21:30:24 +00001127 while (true) {
1128 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001129 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001130 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001131 // Skip the rest of this base specifier, up until the comma or
1132 // opening brace.
Douglas Gregor29a92472008-10-22 17:49:05 +00001133 SkipUntil(tok::comma, tok::l_brace, true, true);
1134 } else {
1135 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001136 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001137 }
1138
1139 // If the next token is a comma, consume it and keep reading
1140 // base-specifiers.
1141 if (Tok.isNot(tok::comma)) break;
Mike Stump11289f42009-09-09 15:08:12 +00001142
Douglas Gregor556877c2008-04-13 21:30:24 +00001143 // Consume the comma.
1144 ConsumeToken();
1145 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001146
1147 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001148 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001149}
1150
1151/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1152/// one entry in the base class list of a class specifier, for example:
1153/// class foo : public bar, virtual private baz {
1154/// 'public bar' and 'virtual private baz' are each base-specifiers.
1155///
1156/// base-specifier: [C++ class.derived]
1157/// ::[opt] nested-name-specifier[opt] class-name
1158/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1159/// class-name
1160/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1161/// class-name
John McCall48871652010-08-21 09:40:31 +00001162Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001163 bool IsVirtual = false;
1164 SourceLocation StartLoc = Tok.getLocation();
1165
1166 // Parse the 'virtual' keyword.
1167 if (Tok.is(tok::kw_virtual)) {
1168 ConsumeToken();
1169 IsVirtual = true;
1170 }
1171
1172 // Parse an (optional) access specifier.
1173 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001174 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001175 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001176
Douglas Gregor556877c2008-04-13 21:30:24 +00001177 // Parse the 'virtual' keyword (again!), in case it came after the
1178 // access specifier.
1179 if (Tok.is(tok::kw_virtual)) {
1180 SourceLocation VirtualLoc = ConsumeToken();
1181 if (IsVirtual) {
1182 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001183 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001184 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001185 }
1186
1187 IsVirtual = true;
1188 }
1189
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001190 // Parse optional '::' and optional nested-name-specifier.
1191 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001192 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregor556877c2008-04-13 21:30:24 +00001193
Douglas Gregor556877c2008-04-13 21:30:24 +00001194 // The location of the base class itself.
1195 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor831c93f2008-11-05 20:51:48 +00001196
1197 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001198 SourceLocation EndLocation;
Douglas Gregore7c20652011-03-02 00:47:37 +00001199 TypeResult BaseType = ParseClassName(EndLocation, SS);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001200 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001201 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001202
Douglas Gregor752a5952011-01-03 22:36:02 +00001203 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1204 // actually part of the base-specifier-list grammar productions, but we
1205 // parse it here for convenience.
1206 SourceLocation EllipsisLoc;
1207 if (Tok.is(tok::ellipsis))
1208 EllipsisLoc = ConsumeToken();
1209
Mike Stump11289f42009-09-09 15:08:12 +00001210 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001211 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001212
Douglas Gregor556877c2008-04-13 21:30:24 +00001213 // Notify semantic analysis that we have parsed a complete
1214 // base-specifier.
Sebastian Redl511ed552008-11-25 22:21:31 +00001215 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregor752a5952011-01-03 22:36:02 +00001216 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001217}
1218
1219/// getAccessSpecifierIfPresent - Determine whether the next token is
1220/// a C++ access-specifier.
1221///
1222/// access-specifier: [C++ class.derived]
1223/// 'private'
1224/// 'protected'
1225/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001226AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001227 switch (Tok.getKind()) {
1228 default: return AS_none;
1229 case tok::kw_private: return AS_private;
1230 case tok::kw_protected: return AS_protected;
1231 case tok::kw_public: return AS_public;
1232 }
1233}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001234
Eli Friedman3af2a772009-07-22 21:45:50 +00001235void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
John McCall48871652010-08-21 09:40:31 +00001236 Decl *ThisDecl) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001237 // We just declared a member function. If this member function
1238 // has any default arguments, we'll need to parse them later.
1239 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001240 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001241 = DeclaratorInfo.getFunctionTypeInfo();
Eli Friedman3af2a772009-07-22 21:45:50 +00001242 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1243 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1244 if (!LateMethod) {
1245 // Push this method onto the stack of late-parsed method
1246 // declarations.
Douglas Gregorefc46952010-10-12 16:25:54 +00001247 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1248 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001249 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001250
1251 // Add all of the parameters prior to this one (they don't
1252 // have default arguments).
1253 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1254 for (unsigned I = 0; I < ParamIdx; ++I)
1255 LateMethod->DefaultArgs.push_back(
Douglas Gregor1d85d292010-03-02 01:29:43 +00001256 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001257 }
1258
1259 // Add this parameter to the list of parameters (it or may
1260 // not have a default argument).
1261 LateMethod->DefaultArgs.push_back(
1262 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1263 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1264 }
1265 }
1266}
1267
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001268/// isCXX0XVirtSpecifier - Determine whether the next token is a C++0x
1269/// virt-specifier.
1270///
1271/// virt-specifier:
1272/// override
1273/// final
1274/// new
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001275VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier() const {
Anders Carlsson5a72fdb2011-01-22 23:01:49 +00001276 if (!getLang().CPlusPlus)
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001277 return VirtSpecifiers::VS_None;
1278
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001279 if (Tok.is(tok::kw_new))
Anders Carlsson56104902011-01-17 03:05:47 +00001280 return VirtSpecifiers::VS_New;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001281
Anders Carlsson56104902011-01-17 03:05:47 +00001282 if (Tok.is(tok::identifier)) {
1283 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001284
Anders Carlsson428803b2011-01-20 03:47:08 +00001285 // Initialize the contextual keywords.
1286 if (!Ident_final) {
1287 Ident_final = &PP.getIdentifierTable().get("final");
1288 Ident_override = &PP.getIdentifierTable().get("override");
1289 }
1290
Anders Carlsson56104902011-01-17 03:05:47 +00001291 if (II == Ident_override)
1292 return VirtSpecifiers::VS_Override;
1293
1294 if (II == Ident_final)
1295 return VirtSpecifiers::VS_Final;
1296 }
1297
1298 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001299}
1300
1301/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1302///
1303/// virt-specifier-seq:
1304/// virt-specifier
1305/// virt-specifier-seq virt-specifier
Anders Carlsson56104902011-01-17 03:05:47 +00001306void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) {
Anders Carlsson56104902011-01-17 03:05:47 +00001307 while (true) {
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001308 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001309 if (Specifier == VirtSpecifiers::VS_None)
1310 return;
1311
1312 // C++ [class.mem]p8:
1313 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001314 const char *PrevSpec = 0;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001315 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001316 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1317 << PrevSpec
1318 << FixItHint::CreateRemoval(Tok.getLocation());
1319
Anders Carlsson5a72fdb2011-01-22 23:01:49 +00001320 if (!getLang().CPlusPlus0x)
1321 Diag(Tok.getLocation(), diag::ext_override_control_keyword)
1322 << VirtSpecifiers::getSpecifierName(Specifier);
Anders Carlsson56104902011-01-17 03:05:47 +00001323 ConsumeToken();
1324 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001325}
1326
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001327/// isCXX0XClassVirtSpecifier - Determine whether the next token is a C++0x
1328/// class-virt-specifier.
1329///
1330/// class-virt-specifier:
1331/// final
1332/// explicit
1333ClassVirtSpecifiers::Specifier Parser::isCXX0XClassVirtSpecifier() const {
Anders Carlsson5a72fdb2011-01-22 23:01:49 +00001334 if (!getLang().CPlusPlus)
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001335 return ClassVirtSpecifiers::CVS_None;
1336
1337 if (Tok.is(tok::kw_explicit))
1338 return ClassVirtSpecifiers::CVS_Explicit;
1339
1340 if (Tok.is(tok::identifier)) {
1341 IdentifierInfo *II = Tok.getIdentifierInfo();
1342
1343 // Initialize the contextual keywords.
1344 if (!Ident_final) {
1345 Ident_final = &PP.getIdentifierTable().get("final");
1346 Ident_override = &PP.getIdentifierTable().get("override");
1347 }
1348
1349 if (II == Ident_final)
1350 return ClassVirtSpecifiers::CVS_Final;
1351 }
1352
1353 return ClassVirtSpecifiers::CVS_None;
1354}
1355
1356/// ParseOptionalCXX0XClassVirtSpecifierSeq - Parse a class-virt-specifier-seq.
1357///
1358/// class-virt-specifier-seq:
1359/// class-virt-specifier
1360/// class-virt-specifier-seq class-virt-specifier
1361void Parser::ParseOptionalCXX0XClassVirtSpecifierSeq(ClassVirtSpecifiers &CVS) {
1362 while (true) {
1363 ClassVirtSpecifiers::Specifier Specifier = isCXX0XClassVirtSpecifier();
1364 if (Specifier == ClassVirtSpecifiers::CVS_None)
1365 return;
1366
1367 // C++ [class]p1:
1368 // A class-virt-specifier-seq shall contain at most one of each
1369 // class-virt-specifier.
1370 const char *PrevSpec = 0;
1371 if (CVS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
1372 Diag(Tok.getLocation(), diag::err_duplicate_class_virt_specifier)
1373 << PrevSpec
1374 << FixItHint::CreateRemoval(Tok.getLocation());
Anders Carlsson5a72fdb2011-01-22 23:01:49 +00001375
1376 if (!getLang().CPlusPlus0x)
1377 Diag(Tok.getLocation(), diag::ext_override_control_keyword)
1378 << ClassVirtSpecifiers::getSpecifierName(Specifier);
1379
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001380 ConsumeToken();
1381 }
1382}
1383
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001384/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1385///
1386/// member-declaration:
1387/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1388/// function-definition ';'[opt]
1389/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1390/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001391/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001392/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001393/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001394///
1395/// member-declarator-list:
1396/// member-declarator
1397/// member-declarator-list ',' member-declarator
1398///
1399/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001400/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001401/// declarator constant-initializer[opt]
1402/// identifier[opt] ':' constant-expression
1403///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001404/// virt-specifier-seq:
1405/// virt-specifier
1406/// virt-specifier-seq virt-specifier
1407///
1408/// virt-specifier:
1409/// override
1410/// final
1411/// new
1412///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001413/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001414/// '= 0'
1415///
1416/// constant-initializer:
1417/// '=' constant-expression
1418///
Douglas Gregor3447e762009-08-20 22:52:58 +00001419void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCall796c2a52010-07-16 08:13:16 +00001420 const ParsedTemplateInfo &TemplateInfo,
1421 ParsingDeclRAIIObject *TemplateDiags) {
John McCalla0097262009-12-11 02:10:03 +00001422 // Access declarations.
1423 if (!TemplateInfo.Kind &&
1424 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall1f476a12010-02-26 08:45:28 +00001425 !TryAnnotateCXXScopeToken() &&
John McCalla0097262009-12-11 02:10:03 +00001426 Tok.is(tok::annot_cxxscope)) {
1427 bool isAccessDecl = false;
1428 if (NextToken().is(tok::identifier))
1429 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1430 else
1431 isAccessDecl = NextToken().is(tok::kw_operator);
1432
1433 if (isAccessDecl) {
1434 // Collect the scope specifier token we annotated earlier.
1435 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001436 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
John McCalla0097262009-12-11 02:10:03 +00001437
1438 // Try to parse an unqualified-id.
1439 UnqualifiedId Name;
John McCallba7bf592010-08-24 05:47:05 +00001440 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) {
John McCalla0097262009-12-11 02:10:03 +00001441 SkipUntil(tok::semi);
1442 return;
1443 }
1444
1445 // TODO: recover from mistakenly-qualified operator declarations.
1446 if (ExpectAndConsume(tok::semi,
1447 diag::err_expected_semi_after,
1448 "access declaration",
1449 tok::semi))
1450 return;
1451
Douglas Gregor0be31a22010-07-02 17:43:08 +00001452 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCalla0097262009-12-11 02:10:03 +00001453 false, SourceLocation(),
1454 SS, Name,
1455 /* AttrList */ 0,
1456 /* IsTypeName */ false,
1457 SourceLocation());
1458 return;
1459 }
1460 }
1461
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001462 // static_assert-declaration
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001463 if (Tok.is(tok::kw_static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001464 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00001465 SourceLocation DeclEnd;
1466 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001467 return;
1468 }
Mike Stump11289f42009-09-09 15:08:12 +00001469
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001470 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00001471 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00001472 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00001473 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00001474 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001475 AS);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001476 return;
1477 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001478
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001479 // Handle: member-declaration ::= '__extension__' member-declaration
1480 if (Tok.is(tok::kw___extension__)) {
1481 // __extension__ silences extension warnings in the subexpression.
1482 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1483 ConsumeToken();
John McCall796c2a52010-07-16 08:13:16 +00001484 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001485 }
Douglas Gregorfec52632009-06-20 00:51:54 +00001486
Chris Lattnercf251412010-02-02 01:23:29 +00001487 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1488 // is a bitfield.
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001489 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001490
John McCall53fa7142010-12-24 02:08:15 +00001491 ParsedAttributesWithRange attrs;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001492 // Optional C++0x attribute-specifier
John McCall53fa7142010-12-24 02:08:15 +00001493 MaybeParseCXX0XAttributes(attrs);
1494 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001495
Douglas Gregorfec52632009-06-20 00:51:54 +00001496 if (Tok.is(tok::kw_using)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00001497 // FIXME: Check for template aliases
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001498
John McCall53fa7142010-12-24 02:08:15 +00001499 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001500
Douglas Gregorfec52632009-06-20 00:51:54 +00001501 // Eat 'using'.
1502 SourceLocation UsingLoc = ConsumeToken();
1503
1504 if (Tok.is(tok::kw_namespace)) {
1505 Diag(UsingLoc, diag::err_using_namespace_in_class);
1506 SkipUntil(tok::semi, true, true);
Chris Lattner916dbf12010-02-02 00:43:15 +00001507 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00001508 SourceLocation DeclEnd;
1509 // Otherwise, it must be using-declaration.
John McCall9b72f892010-11-10 02:40:36 +00001510 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1511 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00001512 }
1513 return;
1514 }
1515
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001516 // decl-specifier-seq:
1517 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00001518 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00001519 DS.takeAttributesFrom(attrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00001520 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001521
John McCallfaf5fb42010-08-26 23:41:50 +00001522 MultiTemplateParamsArg TemplateParams(Actions,
John McCall11083da2009-09-16 22:47:08 +00001523 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1524 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1525
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001526 if (Tok.is(tok::semi)) {
1527 ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001528 Decl *TheDecl =
John McCall796c2a52010-07-16 08:13:16 +00001529 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
1530 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00001531 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001532 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001533
John McCall28a6aea2009-11-04 02:18:39 +00001534 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00001535 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001536
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001537 if (Tok.isNot(tok::colon)) {
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001538 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1539 ColonProtectionRAIIObject X(*this);
1540
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001541 // Parse the first declarator.
1542 ParseDeclarator(DeclaratorInfo);
1543 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +00001544 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001545 // If so, skip until the semi-colon or a }.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001546 SkipUntil(tok::r_brace, true);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001547 if (Tok.is(tok::semi))
1548 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001549 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001550 }
1551
Nico Weber24b2a822011-01-28 06:07:34 +00001552 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1553
John Thompson5bc5cbe2009-11-25 22:58:06 +00001554 // If attributes exist after the declarator, but before an '{', parse them.
John McCall53fa7142010-12-24 02:08:15 +00001555 MaybeParseGNUAttributes(DeclaratorInfo);
John Thompson5bc5cbe2009-11-25 22:58:06 +00001556
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001557 // function-definition:
Douglas Gregore8381c02008-11-05 04:29:56 +00001558 if (Tok.is(tok::l_brace)
Sebastian Redla7b98a72009-04-26 20:35:05 +00001559 || (DeclaratorInfo.isFunctionDeclarator() &&
1560 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001561 if (!DeclaratorInfo.isFunctionDeclarator()) {
1562 Diag(Tok, diag::err_func_def_no_params);
1563 ConsumeBrace();
1564 SkipUntil(tok::r_brace, true);
Douglas Gregor8a4db832011-01-19 16:41:58 +00001565
1566 // Consume the optional ';'
1567 if (Tok.is(tok::semi))
1568 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001569 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001570 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001571
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001572 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1573 Diag(Tok, diag::err_function_declared_typedef);
1574 // This recovery skips the entire function body. It would be nice
1575 // to simply call ParseCXXInlineMethodDef() below, however Sema
1576 // assumes the declarator represents a function, not a typedef.
1577 ConsumeBrace();
1578 SkipUntil(tok::r_brace, true);
Douglas Gregor8a4db832011-01-19 16:41:58 +00001579
1580 // Consume the optional ';'
1581 if (Tok.is(tok::semi))
1582 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001583 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001584 }
1585
Nico Weber24b2a822011-01-28 06:07:34 +00001586 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo, VS);
Douglas Gregor8a4db832011-01-19 16:41:58 +00001587 // Consume the optional ';'
1588 if (Tok.is(tok::semi))
1589 ConsumeToken();
1590
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001591 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001592 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001593 }
1594
1595 // member-declarator-list:
1596 // member-declarator
1597 // member-declarator-list ',' member-declarator
1598
John McCall48871652010-08-21 09:40:31 +00001599 llvm::SmallVector<Decl *, 8> DeclsInGroup;
John McCalldadc5752010-08-24 06:29:42 +00001600 ExprResult BitfieldSize;
1601 ExprResult Init;
Sebastian Redl42e92c42009-04-12 17:16:29 +00001602 bool Deleted = false;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001603
1604 while (1) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001605 // member-declarator:
1606 // declarator pure-specifier[opt]
1607 // declarator constant-initializer[opt]
1608 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001609 if (Tok.is(tok::colon)) {
1610 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001611 BitfieldSize = ParseConstantExpression();
1612 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001613 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001614 }
Mike Stump11289f42009-09-09 15:08:12 +00001615
Anders Carlsson56104902011-01-17 03:05:47 +00001616 ParseOptionalCXX0XVirtSpecifierSeq(VS);
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001617
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001618 // pure-specifier:
1619 // '= 0'
1620 //
1621 // constant-initializer:
1622 // '=' constant-expression
Sebastian Redl42e92c42009-04-12 17:16:29 +00001623 //
1624 // defaulted/deleted function-definition:
1625 // '=' 'default' [TODO]
1626 // '=' 'delete'
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001627 if (Tok.is(tok::equal)) {
1628 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001629 if (Tok.is(tok::kw_delete)) {
1630 if (!getLang().CPlusPlus0x)
1631 Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
Sebastian Redl42e92c42009-04-12 17:16:29 +00001632 ConsumeToken();
1633 Deleted = true;
1634 } else {
1635 Init = ParseInitializer();
1636 if (Init.isInvalid())
1637 SkipUntil(tok::comma, true, true);
1638 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001639 }
1640
Chris Lattnerf3d3b362010-06-13 05:34:18 +00001641 // If a simple-asm-expr is present, parse it.
1642 if (Tok.is(tok::kw_asm)) {
1643 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00001644 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnerf3d3b362010-06-13 05:34:18 +00001645 if (AsmLabel.isInvalid())
1646 SkipUntil(tok::comma, true, true);
1647
1648 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1649 DeclaratorInfo.SetRangeEnd(Loc);
1650 }
1651
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001652 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001653 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001654
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001655 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001656 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001657 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00001658
John McCall48871652010-08-21 09:40:31 +00001659 Decl *ThisDecl = 0;
John McCall07e91c02009-08-06 02:15:43 +00001660 if (DS.isFriendSpecified()) {
John McCall2f212b32009-09-11 21:02:39 +00001661 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor0be31a22010-07-02 17:43:08 +00001662 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCall2f212b32009-09-11 21:02:39 +00001663 /*IsDefinition*/ false,
1664 move(TemplateParams));
Douglas Gregor3447e762009-08-20 22:52:58 +00001665 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001666 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00001667 DeclaratorInfo,
Douglas Gregor3447e762009-08-20 22:52:58 +00001668 move(TemplateParams),
John McCall07e91c02009-08-06 02:15:43 +00001669 BitfieldSize.release(),
Anders Carlssondb36b802011-01-20 03:57:25 +00001670 VS, Init.release(),
Sebastian Redld6f78502009-11-24 23:38:44 +00001671 /*IsDefinition*/Deleted,
John McCall07e91c02009-08-06 02:15:43 +00001672 Deleted);
Douglas Gregor3447e762009-08-20 22:52:58 +00001673 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001674 if (ThisDecl)
1675 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001676
Douglas Gregor4d87df52008-12-16 21:30:33 +00001677 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump11289f42009-09-09 15:08:12 +00001678 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor4d87df52008-12-16 21:30:33 +00001679 != DeclSpec::SCS_typedef) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001680 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor4d87df52008-12-16 21:30:33 +00001681 }
1682
John McCall28a6aea2009-11-04 02:18:39 +00001683 DeclaratorInfo.complete(ThisDecl);
1684
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001685 // If we don't have a comma, it is either the end of the list (a ';')
1686 // or an error, bail out.
1687 if (Tok.isNot(tok::comma))
1688 break;
Mike Stump11289f42009-09-09 15:08:12 +00001689
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001690 // Consume the comma.
1691 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001692
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001693 // Parse the next declarator.
1694 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00001695 VS.clear();
Sebastian Redlc13f2682008-12-09 20:22:58 +00001696 BitfieldSize = 0;
1697 Init = 0;
Sebastian Redl42e92c42009-04-12 17:16:29 +00001698 Deleted = false;
Mike Stump11289f42009-09-09 15:08:12 +00001699
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001700 // Attributes are only allowed on the second declarator.
John McCall53fa7142010-12-24 02:08:15 +00001701 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001702
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00001703 if (Tok.isNot(tok::colon))
1704 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001705 }
1706
Chris Lattner916dbf12010-02-02 00:43:15 +00001707 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1708 // Skip to end of block or statement.
1709 SkipUntil(tok::r_brace, true, true);
1710 // If we stopped at a ';', eat it.
1711 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001712 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001713 }
1714
Douglas Gregor0be31a22010-07-02 17:43:08 +00001715 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattner916dbf12010-02-02 00:43:15 +00001716 DeclsInGroup.size());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001717}
1718
1719/// ParseCXXMemberSpecification - Parse the class definition.
1720///
1721/// member-specification:
1722/// member-declaration member-specification[opt]
1723/// access-specifier ':' member-specification[opt]
1724///
1725void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00001726 unsigned TagType, Decl *TagDecl) {
Sanjiv Guptad7959242008-10-31 09:52:39 +00001727 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001728 TagType == DeclSpec::TST_union ||
Sanjiv Guptad7959242008-10-31 09:52:39 +00001729 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001730
John McCallfaf5fb42010-08-26 23:41:50 +00001731 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1732 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00001733
Douglas Gregoredf8f392010-01-16 20:52:59 +00001734 // Determine whether this is a non-nested class. Note that local
1735 // classes are *not* considered to be nested classes.
1736 bool NonNestedClass = true;
1737 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001738 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00001739 if (S->isClassScope()) {
1740 // We're inside a class scope, so this is a nested class.
1741 NonNestedClass = false;
1742 break;
1743 }
1744
1745 if ((S->getFlags() & Scope::FnScope)) {
1746 // If we're in a function or function template declared in the
1747 // body of a class, then this is a local class rather than a
1748 // nested class.
1749 const Scope *Parent = S->getParent();
1750 if (Parent->isTemplateParamScope())
1751 Parent = Parent->getParent();
1752 if (Parent->isClassScope())
1753 break;
1754 }
1755 }
1756 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001757
1758 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00001759 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001760
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001761 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregoredf8f392010-01-16 20:52:59 +00001762 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001763
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001764 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001765 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00001766
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001767 ClassVirtSpecifiers CVS;
1768 ParseOptionalCXX0XClassVirtSpecifierSeq(CVS);
1769
John McCall2d814c32009-12-19 21:48:58 +00001770 if (Tok.is(tok::colon)) {
1771 ParseBaseClause(TagDecl);
1772
1773 if (!Tok.is(tok::l_brace)) {
1774 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCall2ff380a2010-03-17 00:38:33 +00001775
1776 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001777 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00001778 return;
1779 }
1780 }
1781
1782 assert(Tok.is(tok::l_brace));
1783
1784 SourceLocation LBraceLoc = ConsumeBrace();
1785
John McCall08bede42010-05-28 08:11:17 +00001786 if (TagDecl)
Anders Carlssonfc1eef42011-01-22 17:51:53 +00001787 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, CVS,
1788 LBraceLoc);
John McCall1c7e6ec2009-12-20 07:58:13 +00001789
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001790 // C++ 11p3: Members of a class defined with the keyword class are private
1791 // by default. Members of a class defined with the keywords struct or union
1792 // are public by default.
1793 AccessSpecifier CurAS;
1794 if (TagType == DeclSpec::TST_class)
1795 CurAS = AS_private;
1796 else
1797 CurAS = AS_public;
1798
Douglas Gregor9377c822010-06-21 22:31:09 +00001799 SourceLocation RBraceLoc;
1800 if (TagDecl) {
1801 // While we still have something to read, read the member-declarations.
1802 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1803 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001804
Douglas Gregor9377c822010-06-21 22:31:09 +00001805 // Check for extraneous top-level semicolon.
1806 if (Tok.is(tok::semi)) {
1807 Diag(Tok, diag::ext_extra_struct_semi)
1808 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1809 << FixItHint::CreateRemoval(Tok.getLocation());
1810 ConsumeToken();
1811 continue;
1812 }
1813
1814 AccessSpecifier AS = getAccessSpecifierIfPresent();
1815 if (AS != AS_none) {
1816 // Current token is a C++ access specifier.
1817 CurAS = AS;
1818 SourceLocation ASLoc = Tok.getLocation();
1819 ConsumeToken();
1820 if (Tok.is(tok::colon))
1821 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1822 else
1823 Diag(Tok, diag::err_expected_colon);
1824 ConsumeToken();
1825 continue;
1826 }
1827
1828 // FIXME: Make sure we don't have a template here.
1829
1830 // Parse all the comma separated declarators.
1831 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001832 }
1833
Douglas Gregor9377c822010-06-21 22:31:09 +00001834 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1835 } else {
1836 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001837 }
Mike Stump11289f42009-09-09 15:08:12 +00001838
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001839 // If attributes exist after class contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001840 ParsedAttributes attrs;
1841 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001842
John McCall08bede42010-05-28 08:11:17 +00001843 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001844 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall08bede42010-05-28 08:11:17 +00001845 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00001846 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001847
1848 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1849 // complete within function bodies, default arguments,
1850 // exception-specifications, and constructor ctor-initializers (including
1851 // such things in nested classes).
1852 //
Douglas Gregor4d87df52008-12-16 21:30:33 +00001853 // FIXME: Only function bodies and constructor ctor-initializers are
1854 // parsed correctly, fix the rest.
Douglas Gregor9377c822010-06-21 22:31:09 +00001855 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001856 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00001857 // are complete and we can parse the delayed portions of method
1858 // declarations and the lexed inline method definitions.
Douglas Gregor428119e2010-06-16 23:45:56 +00001859 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001860 ParseLexedMethodDeclarations(getCurrentClass());
1861 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00001862 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001863 }
1864
John McCall08bede42010-05-28 08:11:17 +00001865 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00001866 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCall2ff380a2010-03-17 00:38:33 +00001867
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001868 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00001869 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001870 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001871}
Douglas Gregore8381c02008-11-05 04:29:56 +00001872
1873/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1874/// which explicitly initializes the members or base classes of a
1875/// class (C++ [class.base.init]). For example, the three initializers
1876/// after the ':' in the Derived constructor below:
1877///
1878/// @code
1879/// class Base { };
1880/// class Derived : Base {
1881/// int x;
1882/// float f;
1883/// public:
1884/// Derived(float f) : Base(), x(17), f(f) { }
1885/// };
1886/// @endcode
1887///
Mike Stump11289f42009-09-09 15:08:12 +00001888/// [C++] ctor-initializer:
1889/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00001890///
Mike Stump11289f42009-09-09 15:08:12 +00001891/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00001892/// mem-initializer ...[opt]
1893/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00001894void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00001895 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1896
1897 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001898
Alexis Hunt1d792652011-01-08 20:30:50 +00001899 llvm::SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00001900 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001901
Douglas Gregore8381c02008-11-05 04:29:56 +00001902 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00001903 if (Tok.is(tok::code_completion)) {
1904 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
1905 MemInitializers.data(),
1906 MemInitializers.size());
1907 ConsumeCodeCompletionToken();
1908 } else {
1909 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
1910 if (!MemInit.isInvalid())
1911 MemInitializers.push_back(MemInit.get());
1912 else
1913 AnyErrors = true;
1914 }
1915
Douglas Gregore8381c02008-11-05 04:29:56 +00001916 if (Tok.is(tok::comma))
1917 ConsumeToken();
1918 else if (Tok.is(tok::l_brace))
1919 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00001920 // If the next token looks like a base or member initializer, assume that
1921 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00001922 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
1923 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
1924 Diag(Loc, diag::err_ctor_init_missing_comma)
1925 << FixItHint::CreateInsertion(Loc, ", ");
1926 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00001927 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redla7b98a72009-04-26 20:35:05 +00001928 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregore8381c02008-11-05 04:29:56 +00001929 SkipUntil(tok::l_brace, true, true);
1930 break;
1931 }
1932 } while (true);
1933
Mike Stump11289f42009-09-09 15:08:12 +00001934 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00001935 MemInitializers.data(), MemInitializers.size(),
1936 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00001937}
1938
1939/// ParseMemInitializer - Parse a C++ member initializer, which is
1940/// part of a constructor initializer that explicitly initializes one
1941/// member or base class (C++ [class.base.init]). See
1942/// ParseConstructorInitializer for an example.
1943///
1944/// [C++] mem-initializer:
1945/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump11289f42009-09-09 15:08:12 +00001946///
Douglas Gregore8381c02008-11-05 04:29:56 +00001947/// [C++] mem-initializer-id:
1948/// '::'[opt] nested-name-specifier[opt] class-name
1949/// identifier
John McCall48871652010-08-21 09:40:31 +00001950Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00001951 // parse '::'[opt] nested-name-specifier[opt]
1952 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00001953 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
1954 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001955 if (Tok.is(tok::annot_template_id)) {
1956 TemplateIdAnnotation *TemplateId
1957 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregor46c59612010-01-12 17:52:59 +00001958 if (TemplateId->Kind == TNK_Type_template ||
1959 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00001960 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001961 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00001962 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001963 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001964 }
1965 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001966 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00001967 return true;
1968 }
Mike Stump11289f42009-09-09 15:08:12 +00001969
Douglas Gregore8381c02008-11-05 04:29:56 +00001970 // Get the identifier. This may be a member name or a class name,
1971 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001972 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregore8381c02008-11-05 04:29:56 +00001973 SourceLocation IdLoc = ConsumeToken();
1974
1975 // Parse the '('.
1976 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001977 Diag(Tok, diag::err_expected_lparen);
Douglas Gregore8381c02008-11-05 04:29:56 +00001978 return true;
1979 }
1980 SourceLocation LParenLoc = ConsumeParen();
1981
1982 // Parse the optional expression-list.
Sebastian Redl511ed552008-11-25 22:21:31 +00001983 ExprVector ArgExprs(Actions);
Douglas Gregore8381c02008-11-05 04:29:56 +00001984 CommaLocsTy CommaLocs;
1985 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
1986 SkipUntil(tok::r_paren);
1987 return true;
1988 }
1989
1990 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1991
Douglas Gregor44e7df62011-01-04 00:32:56 +00001992 SourceLocation EllipsisLoc;
1993 if (Tok.is(tok::ellipsis))
1994 EllipsisLoc = ConsumeToken();
1995
Douglas Gregor0be31a22010-07-02 17:43:08 +00001996 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00001997 TemplateTypeTy, IdLoc,
Sebastian Redl511ed552008-11-25 22:21:31 +00001998 LParenLoc, ArgExprs.take(),
Douglas Gregor44e7df62011-01-04 00:32:56 +00001999 ArgExprs.size(), RParenLoc,
2000 EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00002001}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002002
Sebastian Redl965b0e32011-03-05 14:45:16 +00002003/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002004///
Douglas Gregor356513d2008-12-01 18:00:20 +00002005/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00002006/// dynamic-exception-specification
2007/// noexcept-specification
2008///
2009/// noexcept-specification:
2010/// 'noexcept'
2011/// 'noexcept' '(' constant-expression ')'
2012ExceptionSpecificationType
2013Parser::MaybeParseExceptionSpecification(SourceRange &SpecificationRange,
2014 llvm::SmallVectorImpl<ParsedType> &DynamicExceptions,
2015 llvm::SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
2016 ExprResult &NoexceptExpr) {
2017 ExceptionSpecificationType Result = EST_None;
2018
2019 // See if there's a dynamic specification.
2020 if (Tok.is(tok::kw_throw)) {
2021 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2022 DynamicExceptions,
2023 DynamicExceptionRanges);
2024 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2025 "Produced different number of exception types and ranges.");
2026 }
2027
2028 // If there's no noexcept specification, we're done.
2029 if (Tok.isNot(tok::kw_noexcept))
2030 return Result;
2031
2032 // If we already had a dynamic specification, parse the noexcept for,
2033 // recovery, but emit a diagnostic and don't store the results.
2034 SourceRange NoexceptRange;
2035 ExceptionSpecificationType NoexceptType = EST_None;
2036
2037 SourceLocation KeywordLoc = ConsumeToken();
2038 if (Tok.is(tok::l_paren)) {
2039 // There is an argument.
2040 SourceLocation LParenLoc = ConsumeParen();
2041 NoexceptType = EST_ComputedNoexcept;
2042 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002043 // The argument must be contextually convertible to bool. We use
2044 // ActOnBooleanCondition for this purpose.
2045 if (!NoexceptExpr.isInvalid())
2046 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2047 NoexceptExpr.get());
Sebastian Redl965b0e32011-03-05 14:45:16 +00002048 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2049 NoexceptRange = SourceRange(KeywordLoc, RParenLoc);
2050 } else {
2051 // There is no argument.
2052 NoexceptType = EST_BasicNoexcept;
2053 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2054 }
2055
2056 if (Result == EST_None) {
2057 SpecificationRange = NoexceptRange;
2058 Result = NoexceptType;
2059
2060 // If there's a dynamic specification after a noexcept specification,
2061 // parse that and ignore the results.
2062 if (Tok.is(tok::kw_throw)) {
2063 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2064 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2065 DynamicExceptionRanges);
2066 }
2067 } else {
2068 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2069 }
2070
2071 return Result;
2072}
2073
2074/// ParseDynamicExceptionSpecification - Parse a C++
2075/// dynamic-exception-specification (C++ [except.spec]).
2076///
2077/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00002078/// 'throw' '(' type-id-list [opt] ')'
2079/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00002080///
Douglas Gregor356513d2008-12-01 18:00:20 +00002081/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00002082/// type-id ... [opt]
2083/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002084///
Sebastian Redl965b0e32011-03-05 14:45:16 +00002085ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2086 SourceRange &SpecificationRange,
2087 llvm::SmallVectorImpl<ParsedType> &Exceptions,
2088 llvm::SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002089 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00002090
Sebastian Redl965b0e32011-03-05 14:45:16 +00002091 SpecificationRange.setBegin(ConsumeToken());
Mike Stump11289f42009-09-09 15:08:12 +00002092
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002093 if (!Tok.is(tok::l_paren)) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002094 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2095 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002096 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002097 }
2098 SourceLocation LParenLoc = ConsumeParen();
2099
Douglas Gregor356513d2008-12-01 18:00:20 +00002100 // Parse throw(...), a Microsoft extension that means "this function
2101 // can throw anything".
2102 if (Tok.is(tok::ellipsis)) {
2103 SourceLocation EllipsisLoc = ConsumeToken();
2104 if (!getLang().Microsoft)
2105 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redl965b0e32011-03-05 14:45:16 +00002106 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2107 SpecificationRange.setEnd(RParenLoc);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002108 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00002109 }
2110
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002111 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00002112 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002113 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00002114 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00002115
Douglas Gregor830837d2010-12-20 23:57:46 +00002116 if (Tok.is(tok::ellipsis)) {
2117 // C++0x [temp.variadic]p5:
2118 // - In a dynamic-exception-specification (15.4); the pattern is a
2119 // type-id.
2120 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00002121 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00002122 if (!Res.isInvalid())
2123 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2124 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00002125
Sebastian Redld6434562009-05-29 18:02:33 +00002126 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002127 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00002128 Ranges.push_back(Range);
2129 }
Douglas Gregor830837d2010-12-20 23:57:46 +00002130
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002131 if (Tok.is(tok::comma))
2132 ConsumeToken();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002133 else
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002134 break;
2135 }
2136
Sebastian Redl965b0e32011-03-05 14:45:16 +00002137 SpecificationRange.setEnd(MatchRHSPunctuation(tok::r_paren, LParenLoc));
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002138 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002139}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002140
Douglas Gregor7fb25412010-10-01 18:44:50 +00002141/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2142/// function declaration.
2143TypeResult Parser::ParseTrailingReturnType() {
2144 assert(Tok.is(tok::arrow) && "expected arrow");
2145
2146 ConsumeToken();
2147
2148 // FIXME: Need to suppress declarations when parsing this typename.
2149 // Otherwise in this function definition:
2150 //
2151 // auto f() -> struct X {}
2152 //
2153 // struct X is parsed as class definition because of the trailing
2154 // brace.
2155
2156 SourceRange Range;
2157 return ParseTypeName(&Range);
2158}
2159
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002160/// \brief We have just started parsing the definition of a new class,
2161/// so push that class onto our stack of classes that is currently
2162/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00002163Sema::ParsingClassState
2164Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002165 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002166 "Nested class without outer class");
Douglas Gregoredf8f392010-01-16 20:52:59 +00002167 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
John McCallc1465822011-02-14 07:13:47 +00002168 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002169}
2170
2171/// \brief Deallocate the given parsed class and all of its nested
2172/// classes.
2173void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00002174 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2175 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002176 delete Class;
2177}
2178
2179/// \brief Pop the top class of the stack of classes that are
2180/// currently being parsed.
2181///
2182/// This routine should be called when we have finished parsing the
2183/// definition of a class, but have not yet popped the Scope
2184/// associated with the class's definition.
2185///
2186/// \returns true if the class we've popped is a top-level class,
2187/// false otherwise.
John McCallc1465822011-02-14 07:13:47 +00002188void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002189 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00002190
John McCallc1465822011-02-14 07:13:47 +00002191 Actions.PopParsingClass(state);
2192
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002193 ParsingClass *Victim = ClassStack.top();
2194 ClassStack.pop();
2195 if (Victim->TopLevelClass) {
2196 // Deallocate all of the nested classes of this class,
2197 // recursively: we don't need to keep any of this information.
2198 DeallocateParsedClasses(Victim);
2199 return;
Mike Stump11289f42009-09-09 15:08:12 +00002200 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002201 assert(!ClassStack.empty() && "Missing top-level class?");
2202
Douglas Gregorefc46952010-10-12 16:25:54 +00002203 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002204 // The victim is a nested class, but we will not need to perform
2205 // any processing after the definition of this class since it has
2206 // no members whose handling was delayed. Therefore, we can just
2207 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00002208 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002209 return;
2210 }
2211
2212 // This nested class has some members that will need to be processed
2213 // after the top-level class is completely defined. Therefore, add
2214 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002215 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00002216 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00002217 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002218}
Alexis Hunt96d5c762009-11-21 08:43:09 +00002219
2220/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
2221/// parses standard attributes.
2222///
2223/// [C++0x] attribute-specifier:
2224/// '[' '[' attribute-list ']' ']'
2225///
2226/// [C++0x] attribute-list:
2227/// attribute[opt]
2228/// attribute-list ',' attribute[opt]
2229///
2230/// [C++0x] attribute:
2231/// attribute-token attribute-argument-clause[opt]
2232///
2233/// [C++0x] attribute-token:
2234/// identifier
2235/// attribute-scoped-token
2236///
2237/// [C++0x] attribute-scoped-token:
2238/// attribute-namespace '::' identifier
2239///
2240/// [C++0x] attribute-namespace:
2241/// identifier
2242///
2243/// [C++0x] attribute-argument-clause:
2244/// '(' balanced-token-seq ')'
2245///
2246/// [C++0x] balanced-token-seq:
2247/// balanced-token
2248/// balanced-token-seq balanced-token
2249///
2250/// [C++0x] balanced-token:
2251/// '(' balanced-token-seq ')'
2252/// '[' balanced-token-seq ']'
2253/// '{' balanced-token-seq '}'
2254/// any token but '(', ')', '[', ']', '{', or '}'
John McCall53fa7142010-12-24 02:08:15 +00002255void Parser::ParseCXX0XAttributes(ParsedAttributesWithRange &attrs,
2256 SourceLocation *endLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002257 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
2258 && "Not a C++0x attribute list");
2259
2260 SourceLocation StartLoc = Tok.getLocation(), Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002261
2262 ConsumeBracket();
2263 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002264
Alexis Hunt96d5c762009-11-21 08:43:09 +00002265 if (Tok.is(tok::comma)) {
2266 Diag(Tok.getLocation(), diag::err_expected_ident);
2267 ConsumeToken();
2268 }
2269
2270 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
2271 // attribute not present
2272 if (Tok.is(tok::comma)) {
2273 ConsumeToken();
2274 continue;
2275 }
2276
2277 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
2278 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002279
Alexis Hunt96d5c762009-11-21 08:43:09 +00002280 // scoped attribute
2281 if (Tok.is(tok::coloncolon)) {
2282 ConsumeToken();
2283
2284 if (!Tok.is(tok::identifier)) {
2285 Diag(Tok.getLocation(), diag::err_expected_ident);
2286 SkipUntil(tok::r_square, tok::comma, true, true);
2287 continue;
2288 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002289
Alexis Hunt96d5c762009-11-21 08:43:09 +00002290 ScopeName = AttrName;
2291 ScopeLoc = AttrLoc;
2292
2293 AttrName = Tok.getIdentifierInfo();
2294 AttrLoc = ConsumeToken();
2295 }
2296
2297 bool AttrParsed = false;
2298 // No scoped names are supported; ideally we could put all non-standard
2299 // attributes into namespaces.
2300 if (!ScopeName) {
2301 switch(AttributeList::getKind(AttrName))
2302 {
2303 // No arguments
Alexis Hunt54a02542009-11-25 04:20:27 +00002304 case AttributeList::AT_carries_dependency:
Anders Carlssone30621b2011-01-23 21:33:18 +00002305 case AttributeList::AT_noreturn: {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002306 if (Tok.is(tok::l_paren)) {
2307 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
2308 << AttrName->getName();
2309 break;
2310 }
2311
John McCall53fa7142010-12-24 02:08:15 +00002312 attrs.add(AttrFactory.Create(AttrName, AttrLoc, 0, AttrLoc, 0,
2313 SourceLocation(), 0, 0, false, true));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002314 AttrParsed = true;
2315 break;
2316 }
2317
2318 // One argument; must be a type-id or assignment-expression
2319 case AttributeList::AT_aligned: {
2320 if (Tok.isNot(tok::l_paren)) {
2321 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
2322 << AttrName->getName();
2323 break;
2324 }
2325 SourceLocation ParamLoc = ConsumeParen();
2326
John McCalldadc5752010-08-24 06:29:42 +00002327 ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002328
2329 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2330
2331 ExprVector ArgExprs(Actions);
2332 ArgExprs.push_back(ArgExpr.release());
John McCall53fa7142010-12-24 02:08:15 +00002333 attrs.add(AttrFactory.Create(AttrName, AttrLoc, 0, AttrLoc,
2334 0, ParamLoc, ArgExprs.take(), 1,
2335 false, true));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002336
2337 AttrParsed = true;
2338 break;
2339 }
2340
2341 // Silence warnings
2342 default: break;
2343 }
2344 }
2345
2346 // Skip the entire parameter clause, if any
2347 if (!AttrParsed && Tok.is(tok::l_paren)) {
2348 ConsumeParen();
2349 // SkipUntil maintains the balancedness of tokens.
2350 SkipUntil(tok::r_paren, false);
2351 }
2352 }
2353
2354 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2355 SkipUntil(tok::r_square, false);
2356 Loc = Tok.getLocation();
2357 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2358 SkipUntil(tok::r_square, false);
2359
John McCall53fa7142010-12-24 02:08:15 +00002360 attrs.Range = SourceRange(StartLoc, Loc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002361}
2362
2363/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2364/// attribute.
2365///
2366/// FIXME: Simply returns an alignof() expression if the argument is a
2367/// type. Ideally, the type should be propagated directly into Sema.
2368///
2369/// [C++0x] 'align' '(' type-id ')'
2370/// [C++0x] 'align' '(' assignment-expression ')'
John McCalldadc5752010-08-24 06:29:42 +00002371ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002372 if (isTypeIdInParens()) {
John McCallfaf5fb42010-08-26 23:41:50 +00002373 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002374 SourceLocation TypeLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002375 ParsedType Ty = ParseTypeName().get();
Alexis Hunt96d5c762009-11-21 08:43:09 +00002376 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbournee190dee2011-03-11 19:24:49 +00002377 return Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2378 Ty.getAsOpaquePtr(), TypeRange);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002379 } else
2380 return ParseConstantExpression();
2381}
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00002382
2383/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
2384///
2385/// [MS] ms-attribute:
2386/// '[' token-seq ']'
2387///
2388/// [MS] ms-attribute-seq:
2389/// ms-attribute[opt]
2390/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00002391void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
2392 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00002393 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
2394
2395 while (Tok.is(tok::l_square)) {
2396 ConsumeBracket();
2397 SkipUntil(tok::r_square, true, true);
John McCall53fa7142010-12-24 02:08:15 +00002398 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00002399 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
2400 }
2401}