blob: 8c0aa1ba694ce60f4fa25f40fa9e932e24e687db [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Anders Carlsson0c6139d2009-06-27 00:27:47 +000014#include "clang/Basic/OperatorKinds.h"
Douglas Gregor1b7f8982008-04-14 00:13:42 +000015#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000022using namespace clang;
23
24/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000025/// may either be a top level namespace or a block-level namespace alias. If
26/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000027///
28/// namespace-definition: [C++ 7.3: basic.namespace]
29/// named-namespace-definition
30/// unnamed-namespace-definition
31///
32/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000033/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000034///
35/// named-namespace-definition:
36/// original-namespace-definition
37/// extension-namespace-definition
38///
39/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000040/// 'inline'[opt] 'namespace' identifier attributes[opt]
41/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000042///
43/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000044/// 'inline'[opt] 'namespace' original-namespace-name
45/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000046///
Chris Lattner8f08cb72007-08-25 06:57:03 +000047/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
48/// 'namespace' identifier '=' qualified-namespace-specifier ';'
49///
John McCalld226f652010-08-21 09:40:31 +000050Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000051 SourceLocation &DeclEnd,
52 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000053 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000054 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Mike Stump1eb44332009-09-09 15:08:12 +000055
Douglas Gregor49f40bd2009-09-18 19:03:04 +000056 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000057 Actions.CodeCompleteNamespaceDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +000058 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +000059 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000060
Chris Lattner8f08cb72007-08-25 06:57:03 +000061 SourceLocation IdentLoc;
62 IdentifierInfo *Ident = 0;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000063
64 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000065
Chris Lattner04d66662007-10-09 17:33:22 +000066 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000067 Ident = Tok.getIdentifierInfo();
68 IdentLoc = ConsumeToken(); // eat the identifier.
69 }
Mike Stump1eb44332009-09-09 15:08:12 +000070
Chris Lattner8f08cb72007-08-25 06:57:03 +000071 // Read label attributes, if present.
John McCall0b7e6782011-03-24 11:26:52 +000072 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000073 if (Tok.is(tok::kw___attribute)) {
74 attrTok = Tok;
John McCall7f040a92010-12-24 02:08:15 +000075 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000076 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Douglas Gregor6a588dd2009-06-17 19:49:00 +000078 if (Tok.is(tok::equal)) {
John McCall7f040a92010-12-24 02:08:15 +000079 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +000080 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +000081 if (InlineLoc.isValid())
82 Diag(InlineLoc, diag::err_inline_namespace_alias)
83 << FixItHint::CreateRemoval(InlineLoc);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000084
Chris Lattner97144fc2009-04-02 04:16:50 +000085 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000086 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattner51448322009-03-29 14:02:43 +000088 if (Tok.isNot(tok::l_brace)) {
Mike Stump1eb44332009-09-09 15:08:12 +000089 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +000090 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +000091 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +000092 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattner51448322009-03-29 14:02:43 +000094 SourceLocation LBrace = ConsumeBrace();
95
Douglas Gregor23c94db2010-07-02 17:43:08 +000096 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
97 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
98 getCurScope()->getFnParent()) {
Douglas Gregor95f1b152010-05-14 05:08:22 +000099 Diag(LBrace, diag::err_namespace_nonnamespace_scope);
100 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000101 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000102 }
103
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000104 // If we're still good, complain about inline namespaces in non-C++0x now.
105 if (!getLang().CPlusPlus0x && InlineLoc.isValid())
106 Diag(InlineLoc, diag::ext_inline_namespace);
107
Chris Lattner51448322009-03-29 14:02:43 +0000108 // Enter a scope for the namespace.
109 ParseScope NamespaceScope(this, Scope::DeclScope);
110
John McCalld226f652010-08-21 09:40:31 +0000111 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000112 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
113 IdentLoc, Ident, LBrace, attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000114
John McCallf312b1e2010-08-26 23:41:50 +0000115 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
116 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Sean Huntbbd37c62009-11-21 08:43:09 +0000118 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000119 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000120 MaybeParseCXX0XAttributes(attrs);
121 MaybeParseMicrosoftAttributes(attrs);
122 ParseExternalDeclaration(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000123 }
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattner51448322009-03-29 14:02:43 +0000125 // Leave the namespace scope.
126 NamespaceScope.Exit();
127
Chris Lattner97144fc2009-04-02 04:16:50 +0000128 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace);
129 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc);
Chris Lattner51448322009-03-29 14:02:43 +0000130
Chris Lattner97144fc2009-04-02 04:16:50 +0000131 DeclEnd = RBraceLoc;
Chris Lattner51448322009-03-29 14:02:43 +0000132 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000133}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000134
Anders Carlssonf67606a2009-03-28 04:07:16 +0000135/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
136/// alias definition.
137///
John McCalld226f652010-08-21 09:40:31 +0000138Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000139 SourceLocation AliasLoc,
140 IdentifierInfo *Alias,
141 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000142 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Anders Carlssonf67606a2009-03-28 04:07:16 +0000144 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000146 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000147 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000148 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000149 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000150
Anders Carlssonf67606a2009-03-28 04:07:16 +0000151 CXXScopeSpec SS;
152 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000153 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000154
155 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
156 Diag(Tok, diag::err_expected_namespace_name);
157 // Skip to end of the definition and eat the ';'.
158 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000159 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000160 }
161
162 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000163 IdentifierInfo *Ident = Tok.getIdentifierInfo();
164 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Anders Carlssonf67606a2009-03-28 04:07:16 +0000166 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000167 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000168 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
169 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Douglas Gregor23c94db2010-07-02 17:43:08 +0000171 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000172 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000173}
174
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000175/// ParseLinkage - We know that the current token is a string_literal
176/// and just before that, that extern was seen.
177///
178/// linkage-specification: [C++ 7.5p2: dcl.link]
179/// 'extern' string-literal '{' declaration-seq[opt] '}'
180/// 'extern' string-literal declaration
181///
Chris Lattner7d642712010-11-09 20:15:55 +0000182Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000183 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000184 llvm::SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000185 bool Invalid = false;
186 llvm::StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
187 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000188 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000189
190 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000191
Douglas Gregor074149e2009-01-05 19:45:36 +0000192 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000193 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000194 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000195 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000196 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000197 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000198 : SourceLocation());
199
John McCall0b7e6782011-03-24 11:26:52 +0000200 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000201 MaybeParseCXX0XAttributes(attrs);
202 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000203
Douglas Gregor074149e2009-01-05 19:45:36 +0000204 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-05-01 16:25:54 +0000205 // Reset the source range in DS, as the leading "extern"
206 // does not really belong to the inner declaration ...
207 DS.SetRangeStart(SourceLocation());
208 DS.SetRangeEnd(SourceLocation());
209 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000210 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000211 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000212 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000213 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000214 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000215
Douglas Gregor63a01132010-02-07 08:38:28 +0000216 DS.abort();
217
John McCall7f040a92010-12-24 02:08:15 +0000218 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000219
Douglas Gregorf44515a2008-12-16 22:23:02 +0000220 SourceLocation LBrace = ConsumeBrace();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000221 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000222 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000223 MaybeParseCXX0XAttributes(attrs);
224 MaybeParseMicrosoftAttributes(attrs);
225 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000226 }
227
Douglas Gregorf44515a2008-12-16 22:23:02 +0000228 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Chris Lattner7d642712010-11-09 20:15:55 +0000229 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
230 RBrace);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000231}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000232
Douglas Gregorf780abc2008-12-30 03:27:21 +0000233/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
234/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000235Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000236 const ParsedTemplateInfo &TemplateInfo,
237 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000238 ParsedAttributesWithRange &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000239 assert(Tok.is(tok::kw_using) && "Not using token");
240
241 // Eat 'using'.
242 SourceLocation UsingLoc = ConsumeToken();
243
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000244 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000245 Actions.CodeCompleteUsing(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000246 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000247 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000248
John McCall78b81052010-11-10 02:40:36 +0000249 // 'using namespace' means this is a using-directive.
250 if (Tok.is(tok::kw_namespace)) {
251 // Template parameters are always an error here.
252 if (TemplateInfo.Kind) {
253 SourceRange R = TemplateInfo.getSourceRange();
254 Diag(UsingLoc, diag::err_templated_using_directive)
255 << R << FixItHint::CreateRemoval(R);
256 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000257
John McCall7f040a92010-12-24 02:08:15 +0000258 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000259 }
260
Richard Smith162e1c12011-04-15 14:24:37 +0000261 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000262
263 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000264 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000265
John McCall78b81052010-11-10 02:40:36 +0000266 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000267}
268
269/// ParseUsingDirective - Parse C++ using-directive, assumes
270/// that current token is 'namespace' and 'using' was already parsed.
271///
272/// using-directive: [C++ 7.3.p4: namespace.udir]
273/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
274/// namespace-name ;
275/// [GNU] using-directive:
276/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
277/// namespace-name attributes[opt] ;
278///
John McCalld226f652010-08-21 09:40:31 +0000279Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000280 SourceLocation UsingLoc,
281 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000282 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000283 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
284
285 // Eat 'namespace'.
286 SourceLocation NamespcLoc = ConsumeToken();
287
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000288 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000289 Actions.CodeCompleteUsingDirective(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000290 ConsumeCodeCompletionToken();
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000291 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000292
Douglas Gregorf780abc2008-12-30 03:27:21 +0000293 CXXScopeSpec SS;
294 // Parse (optional) nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000295 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000296
Douglas Gregorf780abc2008-12-30 03:27:21 +0000297 IdentifierInfo *NamespcName = 0;
298 SourceLocation IdentLoc = SourceLocation();
299
300 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000301 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000302 Diag(Tok, diag::err_expected_namespace_name);
303 // If there was invalid namespace name, skip to end of decl, and eat ';'.
304 SkipUntil(tok::semi);
305 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000306 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000307 }
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Chris Lattner823c44e2009-01-06 07:27:21 +0000309 // Parse identifier.
310 NamespcName = Tok.getIdentifierInfo();
311 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Chris Lattner823c44e2009-01-06 07:27:21 +0000313 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000314 bool GNUAttr = false;
315 if (Tok.is(tok::kw___attribute)) {
316 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000317 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000318 }
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Chris Lattner823c44e2009-01-06 07:27:21 +0000320 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000321 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000322 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000323 GNUAttr ? diag::err_expected_semi_after_attribute_list
324 : diag::err_expected_semi_after_namespace_name,
325 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000326
Douglas Gregor23c94db2010-07-02 17:43:08 +0000327 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000328 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000329}
330
Richard Smith162e1c12011-04-15 14:24:37 +0000331/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
332/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000333///
334/// using-declaration: [C++ 7.3.p3: namespace.udecl]
335/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000336/// unqualified-id
337/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000338///
Richard Smith162e1c12011-04-15 14:24:37 +0000339/// alias-declaration: C++0x [decl.typedef]p2
340/// 'using' identifier = type-id ;
341///
John McCalld226f652010-08-21 09:40:31 +0000342Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000343 const ParsedTemplateInfo &TemplateInfo,
344 SourceLocation UsingLoc,
345 SourceLocation &DeclEnd,
346 AccessSpecifier AS) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000347 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000348 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000349 bool IsTypeName;
350
351 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000352 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000353 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000354 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000355 ConsumeToken();
356 IsTypeName = true;
357 }
358 else
359 IsTypeName = false;
360
361 // Parse nested-name-specifier.
John McCallb3d87482010-08-24 05:47:05 +0000362 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000363
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000364 // Check nested-name specifier.
365 if (SS.isInvalid()) {
366 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000367 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000368 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000369
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000370 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000371 // destructor names and allow the action module to diagnose any semantic
372 // errors.
373 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000374 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000375 /*EnteringContext=*/false,
376 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000377 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000378 ParsedType(),
Douglas Gregor12c118a2009-11-04 16:30:06 +0000379 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000380 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000381 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000382 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000383
John McCall0b7e6782011-03-24 11:26:52 +0000384 ParsedAttributes attrs(AttrFactory);
Richard Smith162e1c12011-04-15 14:24:37 +0000385
386 // Maybe this is an alias-declaration.
387 bool IsAliasDecl = Tok.is(tok::equal);
388 TypeResult TypeAlias;
389 if (IsAliasDecl) {
390 // TODO: Do we want to support attributes somewhere in an alias declaration?
391 // Can't follow GCC since it doesn't support them yet!
392 ConsumeToken();
393
394 if (!getLang().CPlusPlus0x)
395 Diag(Tok.getLocation(), diag::ext_alias_declaration);
396
397 // Name must be an identifier.
398 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
399 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
400 // No removal fixit: can't recover from this.
401 SkipUntil(tok::semi);
402 return 0;
403 } else if (IsTypeName)
404 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
405 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
406 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
407 else if (SS.isNotEmpty())
408 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
409 << FixItHint::CreateRemoval(SS.getRange());
410
411 TypeAlias = ParseTypeName(0, Declarator::AliasDeclContext);
412 } else
413 // Parse (optional) attributes (most likely GNU strong-using extension).
414 MaybeParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000416 // Eat ';'.
417 DeclEnd = Tok.getLocation();
418 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith162e1c12011-04-15 14:24:37 +0000419 !attrs.empty() ? "attributes list" :
420 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000421 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000422
John McCall78b81052010-11-10 02:40:36 +0000423 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith162e1c12011-04-15 14:24:37 +0000424 // TODO: in C++0x, alias-declarations can be templates:
425 // template <...> using id = type;
John McCall78b81052010-11-10 02:40:36 +0000426 if (TemplateInfo.Kind) {
427 SourceRange R = TemplateInfo.getSourceRange();
428 Diag(UsingLoc, diag::err_templated_using_declaration)
429 << R << FixItHint::CreateRemoval(R);
430
431 // Unfortunately, we have to bail out instead of recovering by
432 // ignoring the parameters, just in case the nested name specifier
433 // depends on the parameters.
434 return 0;
435 }
436
Richard Smith162e1c12011-04-15 14:24:37 +0000437 if (IsAliasDecl)
438 return Actions.ActOnAliasDeclaration(getCurScope(), AS, UsingLoc, Name,
439 TypeAlias);
440
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000441 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000442 Name, attrs.getList(),
443 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000444}
445
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000446/// ParseStaticAssertDeclaration - Parse C++0x or C1X static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000447///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000448/// [C++0x] static_assert-declaration:
449/// static_assert ( constant-expression , string-literal ) ;
450///
451/// [C1X] static_assert-declaration:
452/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000453///
John McCalld226f652010-08-21 09:40:31 +0000454Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000455 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
456 "Not a static_assert declaration");
457
458 if (Tok.is(tok::kw__Static_assert) && !getLang().C1X)
459 Diag(Tok, diag::ext_c1x_static_assert);
460
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000461 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000463 if (Tok.isNot(tok::l_paren)) {
464 Diag(Tok, diag::err_expected_lparen);
John McCalld226f652010-08-21 09:40:31 +0000465 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000466 }
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000468 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregore0762c92009-06-19 23:52:42 +0000469
John McCall60d7b3a2010-08-24 06:29:42 +0000470 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000471 if (AssertExpr.isInvalid()) {
472 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000473 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000474 }
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Anders Carlssonad5f9602009-03-13 23:29:20 +0000476 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000477 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000478
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000479 if (Tok.isNot(tok::string_literal)) {
480 Diag(Tok, diag::err_expected_string_literal);
481 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000482 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000483 }
Mike Stump1eb44332009-09-09 15:08:12 +0000484
John McCall60d7b3a2010-08-24 06:29:42 +0000485 ExprResult AssertMessage(ParseStringLiteralExpression());
Mike Stump1eb44332009-09-09 15:08:12 +0000486 if (AssertMessage.isInvalid())
John McCalld226f652010-08-21 09:40:31 +0000487 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000488
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000489 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Chris Lattner97144fc2009-04-02 04:16:50 +0000491 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000492 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000493
John McCall9ae2f072010-08-23 23:25:46 +0000494 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
495 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000496 AssertMessage.take(),
497 RParenLoc);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000498}
499
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000500/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
501///
502/// 'decltype' ( expression )
503///
504void Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
505 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier");
506
507 SourceLocation StartLoc = ConsumeToken();
508 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000509
510 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000511 "decltype")) {
512 SkipUntil(tok::r_paren);
513 return;
514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000516 // Parse the expression
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000518 // C++0x [dcl.type.simple]p4:
519 // The operand of the decltype specifier is an unevaluated operand.
520 EnterExpressionEvaluationContext Unevaluated(Actions,
John McCallf312b1e2010-08-26 23:41:50 +0000521 Sema::Unevaluated);
John McCall60d7b3a2010-08-24 06:29:42 +0000522 ExprResult Result = ParseExpression();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000523 if (Result.isInvalid()) {
524 SkipUntil(tok::r_paren);
525 return;
526 }
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000528 // Match the ')'
529 SourceLocation RParenLoc;
530 if (Tok.is(tok::r_paren))
531 RParenLoc = ConsumeParen();
532 else
533 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000535 if (RParenLoc.isInvalid())
536 return;
537
538 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000539 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000540 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000541 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000542 DiagID, Result.release()))
543 Diag(StartLoc, DiagID) << PrevSpec;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000544}
545
Douglas Gregor42a552f2008-11-05 20:51:48 +0000546/// ParseClassName - Parse a C++ class-name, which names a class. Note
547/// that we only check that the result names a type; semantic analysis
548/// will need to verify that the type names a class. The result is
Douglas Gregor7f43d672009-02-25 23:52:28 +0000549/// either a type or NULL, depending on whether a type name was
Douglas Gregor42a552f2008-11-05 20:51:48 +0000550/// found.
551///
552/// class-name: [C++ 9.1]
553/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000554/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000555///
Douglas Gregor31a19b62009-04-01 21:51:26 +0000556Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation,
Douglas Gregor059101f2011-03-02 00:47:37 +0000557 CXXScopeSpec &SS) {
Douglas Gregor7f43d672009-02-25 23:52:28 +0000558 // Check whether we have a template-id that names a type.
559 if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000560 TemplateIdAnnotation *TemplateId
Douglas Gregor7f43d672009-02-25 23:52:28 +0000561 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +0000562 if (TemplateId->Kind == TNK_Type_template ||
563 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000564 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000565
566 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000567 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000568 EndLocation = Tok.getAnnotationEndLoc();
569 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000570
571 if (Type)
572 return Type;
573 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000574 }
575
576 // Fall through to produce an error below.
577 }
578
Douglas Gregor42a552f2008-11-05 20:51:48 +0000579 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000580 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000581 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000582 }
583
Douglas Gregor84d0a192010-01-12 21:28:44 +0000584 IdentifierInfo *Id = Tok.getIdentifierInfo();
585 SourceLocation IdLoc = ConsumeToken();
586
587 if (Tok.is(tok::less)) {
588 // It looks the user intended to write a template-id here, but the
589 // template-name was wrong. Try to fix that.
590 TemplateNameKind TNK = TNK_Type_template;
591 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000592 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000593 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000594 Diag(IdLoc, diag::err_unknown_template_name)
595 << Id;
596 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000597
Douglas Gregor84d0a192010-01-12 21:28:44 +0000598 if (!Template)
599 return true;
600
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000601 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000602 UnqualifiedId TemplateName;
603 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000604
Douglas Gregor84d0a192010-01-12 21:28:44 +0000605 // Parse the full template-id, then turn it into a type.
606 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateName,
607 SourceLocation(), true))
608 return true;
609 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000610 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000611
Douglas Gregor84d0a192010-01-12 21:28:44 +0000612 // If we didn't end up with a typename token, there's nothing more we
613 // can do.
614 if (Tok.isNot(tok::annot_typename))
615 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000616
Douglas Gregor84d0a192010-01-12 21:28:44 +0000617 // Retrieve the type from the annotation token, consume that token, and
618 // return.
619 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000620 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000621 ConsumeToken();
622 return Type;
623 }
624
Douglas Gregor42a552f2008-11-05 20:51:48 +0000625 // We have an identifier; check whether it is actually a type.
Douglas Gregor059101f2011-03-02 00:47:37 +0000626 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000627 false, ParsedType(),
628 /*NonTrivialTypeSourceInfo=*/true);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000629 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000630 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000631 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000632 }
633
634 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000635 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000636
637 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000638 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000639 DS.SetRangeStart(IdLoc);
640 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000641 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000642
643 const char *PrevSpec = 0;
644 unsigned DiagID;
645 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
646
647 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
648 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000649}
650
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000651/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
652/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
653/// until we reach the start of a definition or see a token that
Sebastian Redld9bafa72010-02-03 21:21:43 +0000654/// cannot start a definition. If SuppressDeclarations is true, we do know.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000655///
656/// class-specifier: [C++ class]
657/// class-head '{' member-specification[opt] '}'
658/// class-head '{' member-specification[opt] '}' attributes[opt]
659/// class-head:
660/// class-key identifier[opt] base-clause[opt]
661/// class-key nested-name-specifier identifier base-clause[opt]
662/// class-key nested-name-specifier[opt] simple-template-id
663/// base-clause[opt]
664/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000665/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000666/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000667/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000668/// simple-template-id base-clause[opt]
669/// class-key:
670/// 'class'
671/// 'struct'
672/// 'union'
673///
674/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +0000675/// class-key ::[opt] nested-name-specifier[opt] identifier
676/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
677/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000678///
679/// Note that the C++ class-specifier and elaborated-type-specifier,
680/// together, subsume the C99 struct-or-union-specifier:
681///
682/// struct-or-union-specifier: [C99 6.7.2.1]
683/// struct-or-union identifier[opt] '{' struct-contents '}'
684/// struct-or-union identifier
685/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
686/// '}' attributes[opt]
687/// [GNU] struct-or-union attributes[opt] identifier
688/// struct-or-union:
689/// 'struct'
690/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +0000691void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
692 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000693 const ParsedTemplateInfo &TemplateInfo,
Sebastian Redld9bafa72010-02-03 21:21:43 +0000694 AccessSpecifier AS, bool SuppressDeclarations){
Chris Lattner4c97d762009-04-12 21:49:30 +0000695 DeclSpec::TST TagType;
696 if (TagTokKind == tok::kw_struct)
697 TagType = DeclSpec::TST_struct;
698 else if (TagTokKind == tok::kw_class)
699 TagType = DeclSpec::TST_class;
700 else {
701 assert(TagTokKind == tok::kw_union && "Not a class specifier");
702 TagType = DeclSpec::TST_union;
703 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000704
Douglas Gregor374929f2009-09-18 15:37:17 +0000705 if (Tok.is(tok::code_completion)) {
706 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +0000707 Actions.CodeCompleteTag(getCurScope(), TagType);
Douglas Gregordc845342010-05-25 05:58:43 +0000708 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +0000709 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000710
Chandler Carruth926c4b42010-06-28 08:39:25 +0000711 // C++03 [temp.explicit] 14.7.2/8:
712 // The usual access checking rules do not apply to names used to specify
713 // explicit instantiations.
714 //
715 // As an extension we do not perform access checking on the names used to
716 // specify explicit specializations either. This is important to allow
717 // specializing traits classes for private types.
718 bool SuppressingAccessChecks = false;
719 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
720 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization) {
721 Actions.ActOnStartSuppressingAccessChecks();
722 SuppressingAccessChecks = true;
723 }
724
John McCall0b7e6782011-03-24 11:26:52 +0000725 ParsedAttributes attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000726 // If attributes exist after tag, parse them.
727 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +0000728 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000729
Steve Narofff59e17e2008-12-24 20:59:21 +0000730 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +0000731 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +0000732 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000733
Sean Huntbbd37c62009-11-21 08:43:09 +0000734 // If C++0x attributes exist here, parse them.
735 // FIXME: Are we consistent with the ordering of parsing of different
736 // styles of attributes?
John McCall7f040a92010-12-24 02:08:15 +0000737 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000738
John Wiegley20c0da72011-04-27 23:09:49 +0000739 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +0000740 !Tok.is(tok::identifier) &&
741 Tok.getIdentifierInfo() &&
742 (Tok.is(tok::kw___is_arithmetic) ||
743 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +0000744 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +0000745 Tok.is(tok::kw___is_floating_point) ||
746 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +0000747 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +0000748 Tok.is(tok::kw___is_integral) ||
749 Tok.is(tok::kw___is_member_function_pointer) ||
750 Tok.is(tok::kw___is_member_pointer) ||
751 Tok.is(tok::kw___is_pod) ||
752 Tok.is(tok::kw___is_pointer) ||
753 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +0000754 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +0000755 Tok.is(tok::kw___is_signed) ||
756 Tok.is(tok::kw___is_unsigned) ||
757 Tok.is(tok::kw___is_void))) {
758 // GNU libstdc++ 4.2 and libc++ uaw certain intrinsic names as the
759 // name of struct templates, but some are keywords in GCC >= 4.3
760 // and Clang. Therefore, when we see the token sequence "struct
761 // X", make X into a normal identifier rather than a keyword, to
762 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000763 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +0000764 Tok.setKind(tok::identifier);
765 }
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000767 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +0000768 CXXScopeSpec &SS = DS.getTypeSpecScope();
Chris Lattner08d92ec2009-12-10 00:32:41 +0000769 if (getLang().CPlusPlus) {
770 // "FOO : BAR" is not a potential typo for "FOO::BAR".
771 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000772
John McCallb3d87482010-08-24 05:47:05 +0000773 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true))
John McCall207014e2010-07-30 06:26:29 +0000774 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +0000775 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +0000776 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
777 Diag(Tok, diag::err_expected_ident);
778 }
Douglas Gregorcc636682009-02-17 23:15:12 +0000779
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000780 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
781
Douglas Gregorcc636682009-02-17 23:15:12 +0000782 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000783 IdentifierInfo *Name = 0;
784 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +0000785 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000786 if (Tok.is(tok::identifier)) {
787 Name = Tok.getIdentifierInfo();
788 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000789
Douglas Gregor5ee37342010-05-30 22:30:21 +0000790 if (Tok.is(tok::less) && getLang().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000791 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000792 // Eat the template argument list and try to continue parsing this as
793 // a class (or template thereof).
794 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000795 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +0000796 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000797 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +0000798 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000799 // We couldn't parse the template argument list at all, so don't
800 // try to give any location information for the list.
801 LAngleLoc = RAngleLoc = SourceLocation();
802 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000803
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000804 Diag(NameLoc, diag::err_explicit_spec_non_template)
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000805 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000806 << (TagType == DeclSpec::TST_class? 0
807 : TagType == DeclSpec::TST_struct? 1
808 : 2)
809 << Name
810 << SourceRange(LAngleLoc, RAngleLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000811
812 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000813 // we've removed its template argument list.
814 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
815 if (TemplateParams && TemplateParams->size() > 1) {
816 TemplateParams->pop_back();
817 } else {
818 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000819 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000820 = ParsedTemplateInfo::NonTemplate;
821 }
822 } else if (TemplateInfo.Kind
823 == ParsedTemplateInfo::ExplicitInstantiation) {
824 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000825 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000826 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000827 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000828 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +0000829 = SourceLocation();
830 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
831 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000832 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +0000833 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000834 } else if (Tok.is(tok::annot_template_id)) {
835 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
836 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +0000837
Douglas Gregor059101f2011-03-02 00:47:37 +0000838 if (TemplateId->Kind != TNK_Type_template &&
839 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000840 // The template-name in the simple-template-id refers to
841 // something other than a class template. Give an appropriate
842 // error message and skip to the ';'.
843 SourceRange Range(NameLoc);
844 if (SS.isNotEmpty())
845 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +0000846
Douglas Gregor39a8de12009-02-25 19:37:18 +0000847 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
848 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Douglas Gregor39a8de12009-02-25 19:37:18 +0000850 DS.SetTypeSpecError();
851 SkipUntil(tok::semi, false, true);
852 TemplateId->Destroy();
Chandler Carruth926c4b42010-06-28 08:39:25 +0000853 if (SuppressingAccessChecks)
854 Actions.ActOnStopSuppressingAccessChecks();
855
Douglas Gregor39a8de12009-02-25 19:37:18 +0000856 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000857 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000858 }
859
Chandler Carruth926c4b42010-06-28 08:39:25 +0000860 // As soon as we're finished parsing the class's template-id, turn access
861 // checking back on.
862 if (SuppressingAccessChecks)
863 Actions.ActOnStopSuppressingAccessChecks();
864
John McCall67d1a672009-08-06 02:15:43 +0000865 // There are four options here. If we have 'struct foo;', then this
866 // is either a forward declaration or a friend declaration, which
Anders Carlssoncc54d592011-01-22 16:56:46 +0000867 // have to be treated differently. If we have 'struct foo {...',
Anders Carlsson1d209272011-03-25 14:55:14 +0000868 // 'struct foo :...' or 'struct foo final[opt]' then this is a
Anders Carlssoncc54d592011-01-22 16:56:46 +0000869 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Sebastian Redld9bafa72010-02-03 21:21:43 +0000870 // However, in some contexts, things look like declarations but are just
871 // references, e.g.
872 // new struct s;
873 // or
874 // &T::operator struct s;
875 // For these, SuppressDeclarations is true.
John McCallf312b1e2010-08-26 23:41:50 +0000876 Sema::TagUseKind TUK;
Sebastian Redld9bafa72010-02-03 21:21:43 +0000877 if (SuppressDeclarations)
John McCallf312b1e2010-08-26 23:41:50 +0000878 TUK = Sema::TUK_Reference;
Anders Carlssoncc54d592011-01-22 16:56:46 +0000879 else if (Tok.is(tok::l_brace) ||
880 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +0000881 isCXX0XFinalKeyword()) {
Douglas Gregord85bea22009-09-26 06:47:28 +0000882 if (DS.isFriendSpecified()) {
883 // C++ [class.friend]p2:
884 // A class shall not be defined in a friend declaration.
885 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class)
886 << SourceRange(DS.getFriendSpecLoc());
887
888 // Skip everything up to the semicolon, so that this looks like a proper
889 // friend class (or template thereof) declaration.
890 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +0000891 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +0000892 } else {
893 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +0000894 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +0000895 }
896 } else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +0000897 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000898 else
John McCallf312b1e2010-08-26 23:41:50 +0000899 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000900
John McCall207014e2010-07-30 06:26:29 +0000901 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +0000902 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +0000903 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
904 // We have a declaration or reference to an anonymous class.
905 Diag(StartLoc, diag::err_anon_type_definition)
906 << DeclSpec::getSpecifierName(TagType);
907 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000908
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000909 SkipUntil(tok::comma, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +0000910
911 if (TemplateId)
912 TemplateId->Destroy();
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000913 return;
914 }
915
Douglas Gregorddc29e12009-02-06 22:42:48 +0000916 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +0000917 DeclResult TagOrTempResult = true; // invalid
918 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000919
Douglas Gregor402abb52009-05-28 23:31:59 +0000920 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +0000921 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000922 // Explicit specialization, class template partial specialization,
923 // or explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +0000924 ASTTemplateArgsPtr TemplateArgsPtr(Actions,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000925 TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +0000926 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000927 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +0000928 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000929 // This is an explicit instantiation of a class template.
930 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000931 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +0000932 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000933 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000934 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +0000935 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000936 SS,
John McCall2b5289b2010-08-23 07:28:44 +0000937 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000938 TemplateId->TemplateNameLoc,
939 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000940 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +0000941 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +0000942 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +0000943
944 // Friend template-ids are treated as references unless
945 // they have template headers, in which case they're ill-formed
946 // (FIXME: "template <class T> friend class A<T>::B<int>;").
947 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +0000948 } else if (TUK == Sema::TUK_Reference ||
949 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +0000950 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000951 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType,
952 StartLoc,
953 TemplateId->SS,
954 TemplateId->Template,
955 TemplateId->TemplateNameLoc,
956 TemplateId->LAngleLoc,
957 TemplateArgsPtr,
958 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000959 } else {
960 // This is an explicit specialization or a class template
961 // partial specialization.
962 TemplateParameterLists FakedParamLists;
963
964 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
965 // This looks like an explicit instantiation, because we have
966 // something like
967 //
968 // template class Foo<X>
969 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000970 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000971 // meant to be an explicit specialization, but the user forgot
972 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +0000973 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000974
Mike Stump1eb44332009-09-09 15:08:12 +0000975 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000976 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000977 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000978 diag::err_explicit_instantiation_with_definition)
979 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +0000980 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000981
982 // Create a fake template parameter list that contains only
983 // "template<>", so that we treat this construct as a class
984 // template specialization.
985 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +0000986 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000987 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000988 LAngleLoc,
989 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000990 LAngleLoc));
991 TemplateParams = &FakedParamLists;
992 }
993
994 // Build the class template specialization.
995 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +0000996 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor39a8de12009-02-25 19:37:18 +0000997 StartLoc, SS,
John McCall2b5289b2010-08-23 07:28:44 +0000998 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +0000999 TemplateId->TemplateNameLoc,
1000 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001001 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001002 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001003 attrs.getList(),
John McCallf312b1e2010-08-26 23:41:50 +00001004 MultiTemplateParamsArg(Actions,
Douglas Gregorcc636682009-02-17 23:15:12 +00001005 TemplateParams? &(*TemplateParams)[0] : 0,
1006 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001007 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001008 TemplateId->Destroy();
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001009 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001010 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001011 // Explicit instantiation of a member of a class template
1012 // specialization, e.g.,
1013 //
1014 // template struct Outer<int>::Inner;
1015 //
1016 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001017 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001018 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001019 TemplateInfo.TemplateLoc,
1020 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001021 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001022 } else if (TUK == Sema::TUK_Friend &&
1023 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
1024 TagOrTempResult =
1025 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1026 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001027 Name, NameLoc, attrs.getList(),
John McCall9a34edb2010-10-19 01:40:49 +00001028 MultiTemplateParamsArg(Actions,
1029 TemplateParams? &(*TemplateParams)[0] : 0,
1030 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001031 } else {
1032 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001033 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001034 // FIXME: Diagnose this particular error.
1035 }
1036
John McCallc4e70192009-09-11 04:59:25 +00001037 bool IsDependent = false;
1038
John McCalla25c4082010-10-19 18:40:57 +00001039 // Don't pass down template parameter lists if this is just a tag
1040 // reference. For example, we don't need the template parameters here:
1041 // template <class T> class A *makeA(T t);
1042 MultiTemplateParamsArg TParams;
1043 if (TUK != Sema::TUK_Reference && TemplateParams)
1044 TParams =
1045 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1046
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001047 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001048 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001049 SS, Name, NameLoc, attrs.getList(), AS,
John McCalla25c4082010-10-19 18:40:57 +00001050 TParams, Owned, IsDependent, false,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001051 false, clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001052
1053 // If ActOnTag said the type was dependent, try again with the
1054 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001055 if (IsDependent) {
1056 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001057 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001058 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001059 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001060 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001061
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001062 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001063 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001064 assert(Tok.is(tok::l_brace) ||
Anders Carlssoncc54d592011-01-22 16:56:46 +00001065 (getLang().CPlusPlus && Tok.is(tok::colon)) ||
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001066 isCXX0XFinalKeyword());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001067 if (getLang().CPlusPlus)
Douglas Gregor212e81c2009-03-25 00:13:59 +00001068 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001069 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001070 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001071 }
1072
John McCallb3d87482010-08-24 05:47:05 +00001073 const char *PrevSpec = 0;
1074 unsigned DiagID;
1075 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001076 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001077 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1078 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001079 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001080 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001081 Result = DS.SetTypeSpecType(TagType, StartLoc,
1082 NameLoc.isValid() ? NameLoc : StartLoc,
1083 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001084 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001085 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001086 return;
1087 }
Mike Stump1eb44332009-09-09 15:08:12 +00001088
John McCallb3d87482010-08-24 05:47:05 +00001089 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001090 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001091
Chris Lattner4ed5d912010-02-02 01:23:29 +00001092 // At this point, we've successfully parsed a class-specifier in 'definition'
1093 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1094 // going to look at what comes after it to improve error recovery. If an
1095 // impossible token occurs next, we assume that the programmer forgot a ; at
1096 // the end of the declaration and recover that way.
1097 //
1098 // This switch enumerates the valid "follow" set for definition.
John McCallf312b1e2010-08-26 23:41:50 +00001099 if (TUK == Sema::TUK_Definition) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001100 bool ExpectedSemi = true;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001101 switch (Tok.getKind()) {
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001102 default: break;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001103 case tok::semi: // struct foo {...} ;
Chris Lattner99c95202010-02-02 17:32:27 +00001104 case tok::star: // struct foo {...} * P;
1105 case tok::amp: // struct foo {...} & R = ...
1106 case tok::identifier: // struct foo {...} V ;
1107 case tok::r_paren: //(struct foo {...} ) {4}
1108 case tok::annot_cxxscope: // struct foo {...} a:: b;
1109 case tok::annot_typename: // struct foo {...} a ::b;
1110 case tok::annot_template_id: // struct foo {...} a<int> ::b;
Chris Lattnerc2e1c1a2010-02-03 20:41:24 +00001111 case tok::l_paren: // struct foo {...} ( x);
Chris Lattner16acfee2010-02-03 01:45:03 +00001112 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001113 ExpectedSemi = false;
1114 break;
1115 // Type qualifiers
1116 case tok::kw_const: // struct foo {...} const x;
1117 case tok::kw_volatile: // struct foo {...} volatile x;
1118 case tok::kw_restrict: // struct foo {...} restrict x;
1119 case tok::kw_inline: // struct foo {...} inline foo() {};
Chris Lattner99c95202010-02-02 17:32:27 +00001120 // Storage-class specifiers
1121 case tok::kw_static: // struct foo {...} static x;
1122 case tok::kw_extern: // struct foo {...} extern x;
1123 case tok::kw_typedef: // struct foo {...} typedef x;
1124 case tok::kw_register: // struct foo {...} register x;
1125 case tok::kw_auto: // struct foo {...} auto x;
Douglas Gregor33f99242010-05-17 18:19:56 +00001126 case tok::kw_mutable: // struct foo {...} mutable x;
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001127 // As shown above, type qualifiers and storage class specifiers absolutely
1128 // can occur after class specifiers according to the grammar. However,
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001129 // almost no one actually writes code like this. If we see one of these,
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001130 // it is much more likely that someone missed a semi colon and the
1131 // type/storage class specifier we're seeing is part of the *next*
1132 // intended declaration, as in:
1133 //
1134 // struct foo { ... }
1135 // typedef int X;
1136 //
1137 // We'd really like to emit a missing semicolon error instead of emitting
1138 // an error on the 'int' saying that you can't have two type specifiers in
1139 // the same declaration of X. Because of this, we look ahead past this
1140 // token to see if it's a type specifier. If so, we know the code is
1141 // otherwise invalid, so we can produce the expected semi error.
1142 if (!isKnownToBeTypeSpecifier(NextToken()))
1143 ExpectedSemi = false;
Chris Lattner4ed5d912010-02-02 01:23:29 +00001144 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001145
1146 case tok::r_brace: // struct bar { struct foo {...} }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001147 // Missing ';' at end of struct is accepted as an extension in C mode.
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001148 if (!getLang().CPlusPlus)
1149 ExpectedSemi = false;
1150 break;
1151 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001152
Chris Lattnerb3a4e432010-02-28 18:18:36 +00001153 if (ExpectedSemi) {
Chris Lattner4ed5d912010-02-02 01:23:29 +00001154 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1155 TagType == DeclSpec::TST_class ? "class"
1156 : TagType == DeclSpec::TST_struct? "struct" : "union");
1157 // Push this token back into the preprocessor and change our current token
1158 // to ';' so that the rest of the code recovers as though there were an
1159 // ';' after the definition.
1160 PP.EnterToken(Tok);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001161 Tok.setKind(tok::semi);
Chris Lattner4ed5d912010-02-02 01:23:29 +00001162 }
1163 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001164}
1165
Mike Stump1eb44332009-09-09 15:08:12 +00001166/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001167///
1168/// base-clause : [C++ class.derived]
1169/// ':' base-specifier-list
1170/// base-specifier-list:
1171/// base-specifier '...'[opt]
1172/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001173void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001174 assert(Tok.is(tok::colon) && "Not a base clause");
1175 ConsumeToken();
1176
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001177 // Build up an array of parsed base specifiers.
John McCallca0408f2010-08-23 06:44:23 +00001178 llvm::SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001179
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001180 while (true) {
1181 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001182 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001183 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001184 // Skip the rest of this base specifier, up until the comma or
1185 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001186 SkipUntil(tok::comma, tok::l_brace, true, true);
1187 } else {
1188 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001189 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001190 }
1191
1192 // If the next token is a comma, consume it and keep reading
1193 // base-specifiers.
1194 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001196 // Consume the comma.
1197 ConsumeToken();
1198 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001199
1200 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001201 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001202}
1203
1204/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1205/// one entry in the base class list of a class specifier, for example:
1206/// class foo : public bar, virtual private baz {
1207/// 'public bar' and 'virtual private baz' are each base-specifiers.
1208///
1209/// base-specifier: [C++ class.derived]
1210/// ::[opt] nested-name-specifier[opt] class-name
1211/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
1212/// class-name
1213/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
1214/// class-name
John McCalld226f652010-08-21 09:40:31 +00001215Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001216 bool IsVirtual = false;
1217 SourceLocation StartLoc = Tok.getLocation();
1218
1219 // Parse the 'virtual' keyword.
1220 if (Tok.is(tok::kw_virtual)) {
1221 ConsumeToken();
1222 IsVirtual = true;
1223 }
1224
1225 // Parse an (optional) access specifier.
1226 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001227 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001228 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001230 // Parse the 'virtual' keyword (again!), in case it came after the
1231 // access specifier.
1232 if (Tok.is(tok::kw_virtual)) {
1233 SourceLocation VirtualLoc = ConsumeToken();
1234 if (IsVirtual) {
1235 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001236 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001237 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001238 }
1239
1240 IsVirtual = true;
1241 }
1242
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001243 // Parse optional '::' and optional nested-name-specifier.
1244 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001245 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001246
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001247 // The location of the base class itself.
1248 SourceLocation BaseLoc = Tok.getLocation();
Douglas Gregor42a552f2008-11-05 20:51:48 +00001249
1250 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001251 SourceLocation EndLocation;
Douglas Gregor059101f2011-03-02 00:47:37 +00001252 TypeResult BaseType = ParseClassName(EndLocation, SS);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001253 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001254 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001256 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1257 // actually part of the base-specifier-list grammar productions, but we
1258 // parse it here for convenience.
1259 SourceLocation EllipsisLoc;
1260 if (Tok.is(tok::ellipsis))
1261 EllipsisLoc = ConsumeToken();
1262
Mike Stump1eb44332009-09-09 15:08:12 +00001263 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001264 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001266 // Notify semantic analysis that we have parsed a complete
1267 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001268 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001269 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001270}
1271
1272/// getAccessSpecifierIfPresent - Determine whether the next token is
1273/// a C++ access-specifier.
1274///
1275/// access-specifier: [C++ class.derived]
1276/// 'private'
1277/// 'protected'
1278/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001279AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001280 switch (Tok.getKind()) {
1281 default: return AS_none;
1282 case tok::kw_private: return AS_private;
1283 case tok::kw_protected: return AS_protected;
1284 case tok::kw_public: return AS_public;
1285 }
1286}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001287
Eli Friedmand33133c2009-07-22 21:45:50 +00001288void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo,
John McCalld226f652010-08-21 09:40:31 +00001289 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001290 // We just declared a member function. If this member function
1291 // has any default arguments, we'll need to parse them later.
1292 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001293 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001294 = DeclaratorInfo.getFunctionTypeInfo();
Eli Friedmand33133c2009-07-22 21:45:50 +00001295 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1296 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1297 if (!LateMethod) {
1298 // Push this method onto the stack of late-parsed method
1299 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001300 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1301 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001302 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001303
1304 // Add all of the parameters prior to this one (they don't
1305 // have default arguments).
1306 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1307 for (unsigned I = 0; I < ParamIdx; ++I)
1308 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001309 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001310 }
1311
1312 // Add this parameter to the list of parameters (it or may
1313 // not have a default argument).
1314 LateMethod->DefaultArgs.push_back(
1315 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1316 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1317 }
1318 }
1319}
1320
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001321/// isCXX0XVirtSpecifier - Determine whether the next token is a C++0x
1322/// virt-specifier.
1323///
1324/// virt-specifier:
1325/// override
1326/// final
Anders Carlssoncc54d592011-01-22 16:56:46 +00001327VirtSpecifiers::Specifier Parser::isCXX0XVirtSpecifier() const {
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001328 if (!getLang().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001329 return VirtSpecifiers::VS_None;
1330
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001331 if (Tok.is(tok::identifier)) {
1332 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001333
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001334 // Initialize the contextual keywords.
1335 if (!Ident_final) {
1336 Ident_final = &PP.getIdentifierTable().get("final");
1337 Ident_override = &PP.getIdentifierTable().get("override");
1338 }
1339
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001340 if (II == Ident_override)
1341 return VirtSpecifiers::VS_Override;
1342
1343 if (II == Ident_final)
1344 return VirtSpecifiers::VS_Final;
1345 }
1346
1347 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001348}
1349
1350/// ParseOptionalCXX0XVirtSpecifierSeq - Parse a virt-specifier-seq.
1351///
1352/// virt-specifier-seq:
1353/// virt-specifier
1354/// virt-specifier-seq virt-specifier
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001355void Parser::ParseOptionalCXX0XVirtSpecifierSeq(VirtSpecifiers &VS) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001356 while (true) {
Anders Carlssoncc54d592011-01-22 16:56:46 +00001357 VirtSpecifiers::Specifier Specifier = isCXX0XVirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001358 if (Specifier == VirtSpecifiers::VS_None)
1359 return;
1360
1361 // C++ [class.mem]p8:
1362 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001363 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001364 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001365 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1366 << PrevSpec
1367 << FixItHint::CreateRemoval(Tok.getLocation());
1368
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001369 if (!getLang().CPlusPlus0x)
1370 Diag(Tok.getLocation(), diag::ext_override_control_keyword)
1371 << VirtSpecifiers::getSpecifierName(Specifier);
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001372 ConsumeToken();
1373 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001374}
1375
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001376/// isCXX0XFinalKeyword - Determine whether the next token is a C++0x
1377/// contextual 'final' keyword.
1378bool Parser::isCXX0XFinalKeyword() const {
Anders Carlssonce93a7c2011-01-22 23:01:49 +00001379 if (!getLang().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001380 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001381
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001382 if (!Tok.is(tok::identifier))
1383 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001384
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001385 // Initialize the contextual keywords.
1386 if (!Ident_final) {
1387 Ident_final = &PP.getIdentifierTable().get("final");
1388 Ident_override = &PP.getIdentifierTable().get("override");
1389 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001390
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001391 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001392}
1393
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001394/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1395///
1396/// member-declaration:
1397/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1398/// function-definition ';'[opt]
1399/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1400/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001401/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001402/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001403/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001404///
1405/// member-declarator-list:
1406/// member-declarator
1407/// member-declarator-list ',' member-declarator
1408///
1409/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001410/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001411/// declarator constant-initializer[opt]
1412/// identifier[opt] ':' constant-expression
1413///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001414/// virt-specifier-seq:
1415/// virt-specifier
1416/// virt-specifier-seq virt-specifier
1417///
1418/// virt-specifier:
1419/// override
1420/// final
1421/// new
1422///
Sebastian Redle2b68332009-04-12 17:16:29 +00001423/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001424/// '= 0'
1425///
1426/// constant-initializer:
1427/// '=' constant-expression
1428///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001429void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
John McCallc9068d72010-07-16 08:13:16 +00001430 const ParsedTemplateInfo &TemplateInfo,
1431 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001432 if (Tok.is(tok::at)) {
1433 if (getLang().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
1434 Diag(Tok, diag::err_at_defs_cxx);
1435 else
1436 Diag(Tok, diag::err_at_in_class);
1437
1438 ConsumeToken();
1439 SkipUntil(tok::r_brace);
1440 return;
1441 }
1442
John McCall60fa3cf2009-12-11 02:10:03 +00001443 // Access declarations.
1444 if (!TemplateInfo.Kind &&
1445 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) &&
John McCall9ba61662010-02-26 08:45:28 +00001446 !TryAnnotateCXXScopeToken() &&
John McCall60fa3cf2009-12-11 02:10:03 +00001447 Tok.is(tok::annot_cxxscope)) {
1448 bool isAccessDecl = false;
1449 if (NextToken().is(tok::identifier))
1450 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1451 else
1452 isAccessDecl = NextToken().is(tok::kw_operator);
1453
1454 if (isAccessDecl) {
1455 // Collect the scope specifier token we annotated earlier.
1456 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001457 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
John McCall60fa3cf2009-12-11 02:10:03 +00001458
1459 // Try to parse an unqualified-id.
1460 UnqualifiedId Name;
John McCallb3d87482010-08-24 05:47:05 +00001461 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(), Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001462 SkipUntil(tok::semi);
1463 return;
1464 }
1465
1466 // TODO: recover from mistakenly-qualified operator declarations.
1467 if (ExpectAndConsume(tok::semi,
1468 diag::err_expected_semi_after,
1469 "access declaration",
1470 tok::semi))
1471 return;
1472
Douglas Gregor23c94db2010-07-02 17:43:08 +00001473 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001474 false, SourceLocation(),
1475 SS, Name,
1476 /* AttrList */ 0,
1477 /* IsTypeName */ false,
1478 SourceLocation());
1479 return;
1480 }
1481 }
1482
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001483 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001484 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001485 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001486 SourceLocation DeclEnd;
1487 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001488 return;
1489 }
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Chris Lattner682bf922009-03-29 16:50:03 +00001491 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001492 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001493 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001494 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001495 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001496 AS);
Chris Lattner682bf922009-03-29 16:50:03 +00001497 return;
1498 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001499
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001500 // Handle: member-declaration ::= '__extension__' member-declaration
1501 if (Tok.is(tok::kw___extension__)) {
1502 // __extension__ silences extension warnings in the subexpression.
1503 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1504 ConsumeToken();
John McCallc9068d72010-07-16 08:13:16 +00001505 return ParseCXXClassMemberDeclaration(AS, TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001506 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001507
Chris Lattner4ed5d912010-02-02 01:23:29 +00001508 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1509 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001510 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001511
John McCall0b7e6782011-03-24 11:26:52 +00001512 ParsedAttributesWithRange attrs(AttrFactory);
Sean Huntbbd37c62009-11-21 08:43:09 +00001513 // Optional C++0x attribute-specifier
John McCall7f040a92010-12-24 02:08:15 +00001514 MaybeParseCXX0XAttributes(attrs);
1515 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001516
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001517 if (Tok.is(tok::kw_using)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001518 // FIXME: Check for template aliases
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001519
John McCall7f040a92010-12-24 02:08:15 +00001520 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001522 // Eat 'using'.
1523 SourceLocation UsingLoc = ConsumeToken();
1524
1525 if (Tok.is(tok::kw_namespace)) {
1526 Diag(UsingLoc, diag::err_using_namespace_in_class);
1527 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001528 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001529 SourceLocation DeclEnd;
1530 // Otherwise, it must be using-declaration.
John McCall78b81052010-11-10 02:40:36 +00001531 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1532 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001533 }
1534 return;
1535 }
1536
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001537 // decl-specifier-seq:
1538 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001539 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00001540 DS.takeAttributesFrom(attrs);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001541 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001542
John McCallf312b1e2010-08-26 23:41:50 +00001543 MultiTemplateParamsArg TemplateParams(Actions,
John McCalldd4a3b02009-09-16 22:47:08 +00001544 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1545 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1546
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001547 if (Tok.is(tok::semi)) {
1548 ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001549 Decl *TheDecl =
John McCallc9068d72010-07-16 08:13:16 +00001550 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS);
1551 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00001552 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001553 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001554
John McCall54abf7d2009-11-04 02:18:39 +00001555 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00001556 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001557
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001558 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001559 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1560 ColonProtectionRAIIObject X(*this);
1561
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001562 // Parse the first declarator.
1563 ParseDeclarator(DeclaratorInfo);
1564 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00001565 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001566 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00001567 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001568 if (Tok.is(tok::semi))
1569 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001570 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001571 }
1572
Nico Weber48673472011-01-28 06:07:34 +00001573 ParseOptionalCXX0XVirtSpecifierSeq(VS);
1574
John Thompson1b2fc0f2009-11-25 22:58:06 +00001575 // If attributes exist after the declarator, but before an '{', parse them.
John McCall7f040a92010-12-24 02:08:15 +00001576 MaybeParseGNUAttributes(DeclaratorInfo);
John Thompson1b2fc0f2009-11-25 22:58:06 +00001577
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001578 // function-definition:
Douglas Gregor7ad83902008-11-05 04:29:56 +00001579 if (Tok.is(tok::l_brace)
Sebastian Redld3a413d2009-04-26 20:35:05 +00001580 || (DeclaratorInfo.isFunctionDeclarator() &&
1581 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001582 if (!DeclaratorInfo.isFunctionDeclarator()) {
1583 Diag(Tok, diag::err_func_def_no_params);
1584 ConsumeBrace();
1585 SkipUntil(tok::r_brace, true);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001586
1587 // Consume the optional ';'
1588 if (Tok.is(tok::semi))
1589 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001590 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001591 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001592
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001593 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1594 Diag(Tok, diag::err_function_declared_typedef);
1595 // This recovery skips the entire function body. It would be nice
1596 // to simply call ParseCXXInlineMethodDef() below, however Sema
1597 // assumes the declarator represents a function, not a typedef.
1598 ConsumeBrace();
1599 SkipUntil(tok::r_brace, true);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001600
1601 // Consume the optional ';'
1602 if (Tok.is(tok::semi))
1603 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001604 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001605 }
1606
Nico Weber48673472011-01-28 06:07:34 +00001607 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo, VS);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00001608 // Consume the optional ';'
1609 if (Tok.is(tok::semi))
1610 ConsumeToken();
1611
Chris Lattner682bf922009-03-29 16:50:03 +00001612 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001613 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001614 }
1615
1616 // member-declarator-list:
1617 // member-declarator
1618 // member-declarator-list ',' member-declarator
1619
John McCalld226f652010-08-21 09:40:31 +00001620 llvm::SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00001621 ExprResult BitfieldSize;
1622 ExprResult Init;
Sebastian Redle2b68332009-04-12 17:16:29 +00001623 bool Deleted = false;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001624
1625 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001626 // member-declarator:
1627 // declarator pure-specifier[opt]
1628 // declarator constant-initializer[opt]
1629 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001630 if (Tok.is(tok::colon)) {
1631 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001632 BitfieldSize = ParseConstantExpression();
1633 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001634 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001635 }
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001637 ParseOptionalCXX0XVirtSpecifierSeq(VS);
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001638
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001639 // pure-specifier:
1640 // '= 0'
1641 //
1642 // constant-initializer:
1643 // '=' constant-expression
Sebastian Redle2b68332009-04-12 17:16:29 +00001644 //
1645 // defaulted/deleted function-definition:
1646 // '=' 'default' [TODO]
1647 // '=' 'delete'
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001648 if (Tok.is(tok::equal)) {
1649 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001650 if (Tok.is(tok::kw_delete)) {
1651 if (!getLang().CPlusPlus0x)
1652 Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
Sebastian Redle2b68332009-04-12 17:16:29 +00001653 ConsumeToken();
1654 Deleted = true;
1655 } else {
1656 Init = ParseInitializer();
1657 if (Init.isInvalid())
1658 SkipUntil(tok::comma, true, true);
1659 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001660 }
1661
Chris Lattnere6563252010-06-13 05:34:18 +00001662 // If a simple-asm-expr is present, parse it.
1663 if (Tok.is(tok::kw_asm)) {
1664 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00001665 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00001666 if (AsmLabel.isInvalid())
1667 SkipUntil(tok::comma, true, true);
1668
1669 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1670 DeclaratorInfo.SetRangeEnd(Loc);
1671 }
1672
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001673 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001674 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001675
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001676 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00001677 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001678 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00001679
John McCalld226f652010-08-21 09:40:31 +00001680 Decl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00001681 if (DS.isFriendSpecified()) {
John McCallbbbcdd92009-09-11 21:02:39 +00001682 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00001683 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
John McCallbbbcdd92009-09-11 21:02:39 +00001684 /*IsDefinition*/ false,
1685 move(TemplateParams));
Douglas Gregor37b372b2009-08-20 22:52:58 +00001686 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001687 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00001688 DeclaratorInfo,
Douglas Gregor37b372b2009-08-20 22:52:58 +00001689 move(TemplateParams),
John McCall67d1a672009-08-06 02:15:43 +00001690 BitfieldSize.release(),
Anders Carlsson69a87352011-01-20 03:57:25 +00001691 VS, Init.release(),
Sebastian Redld1a78462009-11-24 23:38:44 +00001692 /*IsDefinition*/Deleted,
John McCall67d1a672009-08-06 02:15:43 +00001693 Deleted);
Douglas Gregor37b372b2009-08-20 22:52:58 +00001694 }
Chris Lattner682bf922009-03-29 16:50:03 +00001695 if (ThisDecl)
1696 DeclsInGroup.push_back(ThisDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001697
Douglas Gregor72b505b2008-12-16 21:30:33 +00001698 if (DeclaratorInfo.isFunctionDeclarator() &&
Mike Stump1eb44332009-09-09 15:08:12 +00001699 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Douglas Gregor72b505b2008-12-16 21:30:33 +00001700 != DeclSpec::SCS_typedef) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001701 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl);
Douglas Gregor72b505b2008-12-16 21:30:33 +00001702 }
1703
John McCall54abf7d2009-11-04 02:18:39 +00001704 DeclaratorInfo.complete(ThisDecl);
1705
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001706 // If we don't have a comma, it is either the end of the list (a ';')
1707 // or an error, bail out.
1708 if (Tok.isNot(tok::comma))
1709 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001710
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001711 // Consume the comma.
1712 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001713
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001714 // Parse the next declarator.
1715 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00001716 VS.clear();
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001717 BitfieldSize = 0;
1718 Init = 0;
Sebastian Redle2b68332009-04-12 17:16:29 +00001719 Deleted = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001721 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00001722 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001723
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00001724 if (Tok.isNot(tok::colon))
1725 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001726 }
1727
Chris Lattnerae50d502010-02-02 00:43:15 +00001728 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
1729 // Skip to end of block or statement.
1730 SkipUntil(tok::r_brace, true, true);
1731 // If we stopped at a ';', eat it.
1732 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00001733 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001734 }
1735
Douglas Gregor23c94db2010-07-02 17:43:08 +00001736 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00001737 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001738}
1739
1740/// ParseCXXMemberSpecification - Parse the class definition.
1741///
1742/// member-specification:
1743/// member-declaration member-specification[opt]
1744/// access-specifier ':' member-specification[opt]
1745///
1746void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001747 unsigned TagType, Decl *TagDecl) {
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001748 assert((TagType == DeclSpec::TST_struct ||
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001749 TagType == DeclSpec::TST_union ||
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001750 TagType == DeclSpec::TST_class) && "Invalid TagType!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001751
John McCallf312b1e2010-08-26 23:41:50 +00001752 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1753 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Douglas Gregor26997fd2010-01-16 20:52:59 +00001755 // Determine whether this is a non-nested class. Note that local
1756 // classes are *not* considered to be nested classes.
1757 bool NonNestedClass = true;
1758 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001759 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00001760 if (S->isClassScope()) {
1761 // We're inside a class scope, so this is a nested class.
1762 NonNestedClass = false;
1763 break;
1764 }
1765
1766 if ((S->getFlags() & Scope::FnScope)) {
1767 // If we're in a function or function template declared in the
1768 // body of a class, then this is a local class rather than a
1769 // nested class.
1770 const Scope *Parent = S->getParent();
1771 if (Parent->isTemplateParamScope())
1772 Parent = Parent->getParent();
1773 if (Parent->isClassScope())
1774 break;
1775 }
1776 }
1777 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001778
1779 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001780 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001781
Douglas Gregor6569d682009-05-27 23:11:45 +00001782 // Note that we are parsing a new (potentially-nested) class definition.
Douglas Gregor26997fd2010-01-16 20:52:59 +00001783 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass);
Douglas Gregor6569d682009-05-27 23:11:45 +00001784
Douglas Gregorddc29e12009-02-06 22:42:48 +00001785 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001786 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001787
Anders Carlssonb184a182011-03-25 14:46:08 +00001788 SourceLocation FinalLoc;
1789
1790 // Parse the optional 'final' keyword.
1791 if (getLang().CPlusPlus && Tok.is(tok::identifier)) {
1792 IdentifierInfo *II = Tok.getIdentifierInfo();
1793
1794 // Initialize the contextual keywords.
1795 if (!Ident_final) {
1796 Ident_final = &PP.getIdentifierTable().get("final");
1797 Ident_override = &PP.getIdentifierTable().get("override");
1798 }
1799
1800 if (II == Ident_final)
1801 FinalLoc = ConsumeToken();
1802
1803 if (!getLang().CPlusPlus0x)
1804 Diag(FinalLoc, diag::ext_override_control_keyword) << "final";
1805 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001806
John McCallbd0dfa52009-12-19 21:48:58 +00001807 if (Tok.is(tok::colon)) {
1808 ParseBaseClause(TagDecl);
1809
1810 if (!Tok.is(tok::l_brace)) {
1811 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00001812
1813 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001814 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00001815 return;
1816 }
1817 }
1818
1819 assert(Tok.is(tok::l_brace));
1820
1821 SourceLocation LBraceLoc = ConsumeBrace();
1822
John McCall42a4f662010-05-28 08:11:17 +00001823 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00001824 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Anders Carlssondfc2f102011-01-22 17:51:53 +00001825 LBraceLoc);
John McCallf9368152009-12-20 07:58:13 +00001826
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001827 // C++ 11p3: Members of a class defined with the keyword class are private
1828 // by default. Members of a class defined with the keywords struct or union
1829 // are public by default.
1830 AccessSpecifier CurAS;
1831 if (TagType == DeclSpec::TST_class)
1832 CurAS = AS_private;
1833 else
1834 CurAS = AS_public;
1835
Douglas Gregor07976d22010-06-21 22:31:09 +00001836 SourceLocation RBraceLoc;
1837 if (TagDecl) {
1838 // While we still have something to read, read the member-declarations.
1839 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1840 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001841
Douglas Gregor07976d22010-06-21 22:31:09 +00001842 // Check for extraneous top-level semicolon.
1843 if (Tok.is(tok::semi)) {
1844 Diag(Tok, diag::ext_extra_struct_semi)
1845 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
1846 << FixItHint::CreateRemoval(Tok.getLocation());
1847 ConsumeToken();
1848 continue;
1849 }
1850
1851 AccessSpecifier AS = getAccessSpecifierIfPresent();
1852 if (AS != AS_none) {
1853 // Current token is a C++ access specifier.
1854 CurAS = AS;
1855 SourceLocation ASLoc = Tok.getLocation();
1856 ConsumeToken();
1857 if (Tok.is(tok::colon))
1858 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
1859 else
1860 Diag(Tok, diag::err_expected_colon);
1861 ConsumeToken();
1862 continue;
1863 }
1864
1865 // FIXME: Make sure we don't have a template here.
1866
1867 // Parse all the comma separated declarators.
1868 ParseCXXClassMemberDeclaration(CurAS);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001869 }
1870
Douglas Gregor07976d22010-06-21 22:31:09 +00001871 RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1872 } else {
1873 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001874 }
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001876 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00001877 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001878 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001879
John McCall42a4f662010-05-28 08:11:17 +00001880 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001881 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
John McCall42a4f662010-05-28 08:11:17 +00001882 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00001883 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001884
1885 // C++ 9.2p2: Within the class member-specification, the class is regarded as
1886 // complete within function bodies, default arguments,
1887 // exception-specifications, and constructor ctor-initializers (including
1888 // such things in nested classes).
1889 //
Douglas Gregor72b505b2008-12-16 21:30:33 +00001890 // FIXME: Only function bodies and constructor ctor-initializers are
1891 // parsed correctly, fix the rest.
Douglas Gregor07976d22010-06-21 22:31:09 +00001892 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001893 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00001894 // are complete and we can parse the delayed portions of method
1895 // declarations and the lexed inline method definitions.
Douglas Gregore0cc0472010-06-16 23:45:56 +00001896 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Douglas Gregor6569d682009-05-27 23:11:45 +00001897 ParseLexedMethodDeclarations(getCurrentClass());
1898 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00001899 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001900 }
1901
John McCall42a4f662010-05-28 08:11:17 +00001902 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00001903 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
John McCalldb7bb4a2010-03-17 00:38:33 +00001904
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001905 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00001906 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001907 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001908}
Douglas Gregor7ad83902008-11-05 04:29:56 +00001909
1910/// ParseConstructorInitializer - Parse a C++ constructor initializer,
1911/// which explicitly initializes the members or base classes of a
1912/// class (C++ [class.base.init]). For example, the three initializers
1913/// after the ':' in the Derived constructor below:
1914///
1915/// @code
1916/// class Base { };
1917/// class Derived : Base {
1918/// int x;
1919/// float f;
1920/// public:
1921/// Derived(float f) : Base(), x(17), f(f) { }
1922/// };
1923/// @endcode
1924///
Mike Stump1eb44332009-09-09 15:08:12 +00001925/// [C++] ctor-initializer:
1926/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00001927///
Mike Stump1eb44332009-09-09 15:08:12 +00001928/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00001929/// mem-initializer ...[opt]
1930/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00001931void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001932 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
1933
John Wiegley28bbe4b2011-04-28 01:08:34 +00001934 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
1935 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001936 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001937
Sean Huntcbb67482011-01-08 20:30:50 +00001938 llvm::SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001939 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001940
Douglas Gregor7ad83902008-11-05 04:29:56 +00001941 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00001942 if (Tok.is(tok::code_completion)) {
1943 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
1944 MemInitializers.data(),
1945 MemInitializers.size());
1946 ConsumeCodeCompletionToken();
1947 } else {
1948 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
1949 if (!MemInit.isInvalid())
1950 MemInitializers.push_back(MemInit.get());
1951 else
1952 AnyErrors = true;
1953 }
1954
Douglas Gregor7ad83902008-11-05 04:29:56 +00001955 if (Tok.is(tok::comma))
1956 ConsumeToken();
1957 else if (Tok.is(tok::l_brace))
1958 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00001959 // If the next token looks like a base or member initializer, assume that
1960 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00001961 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
1962 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
1963 Diag(Loc, diag::err_ctor_init_missing_comma)
1964 << FixItHint::CreateInsertion(Loc, ", ");
1965 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00001966 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00001967 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001968 SkipUntil(tok::l_brace, true, true);
1969 break;
1970 }
1971 } while (true);
1972
Mike Stump1eb44332009-09-09 15:08:12 +00001973 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001974 MemInitializers.data(), MemInitializers.size(),
1975 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00001976}
1977
1978/// ParseMemInitializer - Parse a C++ member initializer, which is
1979/// part of a constructor initializer that explicitly initializes one
1980/// member or base class (C++ [class.base.init]). See
1981/// ParseConstructorInitializer for an example.
1982///
1983/// [C++] mem-initializer:
1984/// mem-initializer-id '(' expression-list[opt] ')'
Mike Stump1eb44332009-09-09 15:08:12 +00001985///
Douglas Gregor7ad83902008-11-05 04:29:56 +00001986/// [C++] mem-initializer-id:
1987/// '::'[opt] nested-name-specifier[opt] class-name
1988/// identifier
John McCalld226f652010-08-21 09:40:31 +00001989Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00001990 // parse '::'[opt] nested-name-specifier[opt]
1991 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00001992 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false);
1993 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00001994 if (Tok.is(tok::annot_template_id)) {
1995 TemplateIdAnnotation *TemplateId
1996 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregord9b600c2010-01-12 17:52:59 +00001997 if (TemplateId->Kind == TNK_Type_template ||
1998 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001999 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002000 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002001 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002002 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002003 }
2004 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002005 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002006 return true;
2007 }
Mike Stump1eb44332009-09-09 15:08:12 +00002008
Douglas Gregor7ad83902008-11-05 04:29:56 +00002009 // Get the identifier. This may be a member name or a class name,
2010 // but we'll let the semantic analysis determine which it is.
Fariborz Jahanian96174332009-07-01 19:21:19 +00002011 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002012 SourceLocation IdLoc = ConsumeToken();
2013
2014 // Parse the '('.
2015 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002016 Diag(Tok, diag::err_expected_lparen);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002017 return true;
2018 }
2019 SourceLocation LParenLoc = ConsumeParen();
2020
2021 // Parse the optional expression-list.
Sebastian Redla55e52c2008-11-25 22:21:31 +00002022 ExprVector ArgExprs(Actions);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002023 CommaLocsTy CommaLocs;
2024 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2025 SkipUntil(tok::r_paren);
2026 return true;
2027 }
2028
2029 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2030
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002031 SourceLocation EllipsisLoc;
2032 if (Tok.is(tok::ellipsis))
2033 EllipsisLoc = ConsumeToken();
2034
Douglas Gregor23c94db2010-07-02 17:43:08 +00002035 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
Fariborz Jahanian96174332009-07-01 19:21:19 +00002036 TemplateTypeTy, IdLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +00002037 LParenLoc, ArgExprs.take(),
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002038 ArgExprs.size(), RParenLoc,
2039 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002040}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002041
Sebastian Redl7acafd02011-03-05 14:45:16 +00002042/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002043///
Douglas Gregora4745612008-12-01 18:00:20 +00002044/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002045/// dynamic-exception-specification
2046/// noexcept-specification
2047///
2048/// noexcept-specification:
2049/// 'noexcept'
2050/// 'noexcept' '(' constant-expression ')'
2051ExceptionSpecificationType
2052Parser::MaybeParseExceptionSpecification(SourceRange &SpecificationRange,
2053 llvm::SmallVectorImpl<ParsedType> &DynamicExceptions,
2054 llvm::SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
2055 ExprResult &NoexceptExpr) {
2056 ExceptionSpecificationType Result = EST_None;
2057
2058 // See if there's a dynamic specification.
2059 if (Tok.is(tok::kw_throw)) {
2060 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2061 DynamicExceptions,
2062 DynamicExceptionRanges);
2063 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2064 "Produced different number of exception types and ranges.");
2065 }
2066
2067 // If there's no noexcept specification, we're done.
2068 if (Tok.isNot(tok::kw_noexcept))
2069 return Result;
2070
2071 // If we already had a dynamic specification, parse the noexcept for,
2072 // recovery, but emit a diagnostic and don't store the results.
2073 SourceRange NoexceptRange;
2074 ExceptionSpecificationType NoexceptType = EST_None;
2075
2076 SourceLocation KeywordLoc = ConsumeToken();
2077 if (Tok.is(tok::l_paren)) {
2078 // There is an argument.
2079 SourceLocation LParenLoc = ConsumeParen();
2080 NoexceptType = EST_ComputedNoexcept;
2081 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002082 // The argument must be contextually convertible to bool. We use
2083 // ActOnBooleanCondition for this purpose.
2084 if (!NoexceptExpr.isInvalid())
2085 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2086 NoexceptExpr.get());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002087 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2088 NoexceptRange = SourceRange(KeywordLoc, RParenLoc);
2089 } else {
2090 // There is no argument.
2091 NoexceptType = EST_BasicNoexcept;
2092 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2093 }
2094
2095 if (Result == EST_None) {
2096 SpecificationRange = NoexceptRange;
2097 Result = NoexceptType;
2098
2099 // If there's a dynamic specification after a noexcept specification,
2100 // parse that and ignore the results.
2101 if (Tok.is(tok::kw_throw)) {
2102 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2103 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2104 DynamicExceptionRanges);
2105 }
2106 } else {
2107 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2108 }
2109
2110 return Result;
2111}
2112
2113/// ParseDynamicExceptionSpecification - Parse a C++
2114/// dynamic-exception-specification (C++ [except.spec]).
2115///
2116/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002117/// 'throw' '(' type-id-list [opt] ')'
2118/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002119///
Douglas Gregora4745612008-12-01 18:00:20 +00002120/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002121/// type-id ... [opt]
2122/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002123///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002124ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2125 SourceRange &SpecificationRange,
2126 llvm::SmallVectorImpl<ParsedType> &Exceptions,
2127 llvm::SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002128 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002129
Sebastian Redl7acafd02011-03-05 14:45:16 +00002130 SpecificationRange.setBegin(ConsumeToken());
Mike Stump1eb44332009-09-09 15:08:12 +00002131
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002132 if (!Tok.is(tok::l_paren)) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002133 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2134 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002135 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002136 }
2137 SourceLocation LParenLoc = ConsumeParen();
2138
Douglas Gregora4745612008-12-01 18:00:20 +00002139 // Parse throw(...), a Microsoft extension that means "this function
2140 // can throw anything".
2141 if (Tok.is(tok::ellipsis)) {
2142 SourceLocation EllipsisLoc = ConsumeToken();
2143 if (!getLang().Microsoft)
2144 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Sebastian Redl7acafd02011-03-05 14:45:16 +00002145 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2146 SpecificationRange.setEnd(RParenLoc);
Sebastian Redl60618fa2011-03-12 11:50:43 +00002147 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002148 }
2149
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002150 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002151 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002152 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002153 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002154
Douglas Gregora04426c2010-12-20 23:57:46 +00002155 if (Tok.is(tok::ellipsis)) {
2156 // C++0x [temp.variadic]p5:
2157 // - In a dynamic-exception-specification (15.4); the pattern is a
2158 // type-id.
2159 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002160 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002161 if (!Res.isInvalid())
2162 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2163 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002164
Sebastian Redlef65f062009-05-29 18:02:33 +00002165 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002166 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002167 Ranges.push_back(Range);
2168 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002169
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002170 if (Tok.is(tok::comma))
2171 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002172 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002173 break;
2174 }
2175
Sebastian Redl7acafd02011-03-05 14:45:16 +00002176 SpecificationRange.setEnd(MatchRHSPunctuation(tok::r_paren, LParenLoc));
Sebastian Redl60618fa2011-03-12 11:50:43 +00002177 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002178}
Douglas Gregor6569d682009-05-27 23:11:45 +00002179
Douglas Gregordab60ad2010-10-01 18:44:50 +00002180/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2181/// function declaration.
2182TypeResult Parser::ParseTrailingReturnType() {
2183 assert(Tok.is(tok::arrow) && "expected arrow");
2184
2185 ConsumeToken();
2186
2187 // FIXME: Need to suppress declarations when parsing this typename.
2188 // Otherwise in this function definition:
2189 //
2190 // auto f() -> struct X {}
2191 //
2192 // struct X is parsed as class definition because of the trailing
2193 // brace.
2194
2195 SourceRange Range;
2196 return ParseTypeName(&Range);
2197}
2198
Douglas Gregor6569d682009-05-27 23:11:45 +00002199/// \brief We have just started parsing the definition of a new class,
2200/// so push that class onto our stack of classes that is currently
2201/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002202Sema::ParsingClassState
2203Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002204 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002205 "Nested class without outer class");
Douglas Gregor26997fd2010-01-16 20:52:59 +00002206 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass));
John McCalleee1d542011-02-14 07:13:47 +00002207 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002208}
2209
2210/// \brief Deallocate the given parsed class and all of its nested
2211/// classes.
2212void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002213 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2214 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002215 delete Class;
2216}
2217
2218/// \brief Pop the top class of the stack of classes that are
2219/// currently being parsed.
2220///
2221/// This routine should be called when we have finished parsing the
2222/// definition of a class, but have not yet popped the Scope
2223/// associated with the class's definition.
2224///
2225/// \returns true if the class we've popped is a top-level class,
2226/// false otherwise.
John McCalleee1d542011-02-14 07:13:47 +00002227void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002228 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002229
John McCalleee1d542011-02-14 07:13:47 +00002230 Actions.PopParsingClass(state);
2231
Douglas Gregor6569d682009-05-27 23:11:45 +00002232 ParsingClass *Victim = ClassStack.top();
2233 ClassStack.pop();
2234 if (Victim->TopLevelClass) {
2235 // Deallocate all of the nested classes of this class,
2236 // recursively: we don't need to keep any of this information.
2237 DeallocateParsedClasses(Victim);
2238 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002239 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002240 assert(!ClassStack.empty() && "Missing top-level class?");
2241
Douglas Gregord54eb442010-10-12 16:25:54 +00002242 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002243 // The victim is a nested class, but we will not need to perform
2244 // any processing after the definition of this class since it has
2245 // no members whose handling was delayed. Therefore, we can just
2246 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002247 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002248 return;
2249 }
2250
2251 // This nested class has some members that will need to be processed
2252 // after the top-level class is completely defined. Therefore, add
2253 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002254 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002255 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002256 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00002257}
Sean Huntbbd37c62009-11-21 08:43:09 +00002258
2259/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only
2260/// parses standard attributes.
2261///
2262/// [C++0x] attribute-specifier:
2263/// '[' '[' attribute-list ']' ']'
2264///
2265/// [C++0x] attribute-list:
2266/// attribute[opt]
2267/// attribute-list ',' attribute[opt]
2268///
2269/// [C++0x] attribute:
2270/// attribute-token attribute-argument-clause[opt]
2271///
2272/// [C++0x] attribute-token:
2273/// identifier
2274/// attribute-scoped-token
2275///
2276/// [C++0x] attribute-scoped-token:
2277/// attribute-namespace '::' identifier
2278///
2279/// [C++0x] attribute-namespace:
2280/// identifier
2281///
2282/// [C++0x] attribute-argument-clause:
2283/// '(' balanced-token-seq ')'
2284///
2285/// [C++0x] balanced-token-seq:
2286/// balanced-token
2287/// balanced-token-seq balanced-token
2288///
2289/// [C++0x] balanced-token:
2290/// '(' balanced-token-seq ')'
2291/// '[' balanced-token-seq ']'
2292/// '{' balanced-token-seq '}'
2293/// any token but '(', ')', '[', ']', '{', or '}'
John McCall7f040a92010-12-24 02:08:15 +00002294void Parser::ParseCXX0XAttributes(ParsedAttributesWithRange &attrs,
2295 SourceLocation *endLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002296 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
2297 && "Not a C++0x attribute list");
2298
2299 SourceLocation StartLoc = Tok.getLocation(), Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00002300
2301 ConsumeBracket();
2302 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002303
Sean Huntbbd37c62009-11-21 08:43:09 +00002304 if (Tok.is(tok::comma)) {
2305 Diag(Tok.getLocation(), diag::err_expected_ident);
2306 ConsumeToken();
2307 }
2308
2309 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) {
2310 // attribute not present
2311 if (Tok.is(tok::comma)) {
2312 ConsumeToken();
2313 continue;
2314 }
2315
2316 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo();
2317 SourceLocation ScopeLoc, AttrLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002318
Sean Huntbbd37c62009-11-21 08:43:09 +00002319 // scoped attribute
2320 if (Tok.is(tok::coloncolon)) {
2321 ConsumeToken();
2322
2323 if (!Tok.is(tok::identifier)) {
2324 Diag(Tok.getLocation(), diag::err_expected_ident);
2325 SkipUntil(tok::r_square, tok::comma, true, true);
2326 continue;
2327 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002328
Sean Huntbbd37c62009-11-21 08:43:09 +00002329 ScopeName = AttrName;
2330 ScopeLoc = AttrLoc;
2331
2332 AttrName = Tok.getIdentifierInfo();
2333 AttrLoc = ConsumeToken();
2334 }
2335
2336 bool AttrParsed = false;
2337 // No scoped names are supported; ideally we could put all non-standard
2338 // attributes into namespaces.
2339 if (!ScopeName) {
2340 switch(AttributeList::getKind(AttrName))
2341 {
2342 // No arguments
Sean Hunt7725e672009-11-25 04:20:27 +00002343 case AttributeList::AT_carries_dependency:
Anders Carlsson15e14a22011-01-23 21:33:18 +00002344 case AttributeList::AT_noreturn: {
Sean Huntbbd37c62009-11-21 08:43:09 +00002345 if (Tok.is(tok::l_paren)) {
2346 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments)
2347 << AttrName->getName();
2348 break;
2349 }
2350
John McCall0b7e6782011-03-24 11:26:52 +00002351 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc, 0,
2352 SourceLocation(), 0, 0, false, true);
Sean Huntbbd37c62009-11-21 08:43:09 +00002353 AttrParsed = true;
2354 break;
2355 }
2356
2357 // One argument; must be a type-id or assignment-expression
2358 case AttributeList::AT_aligned: {
2359 if (Tok.isNot(tok::l_paren)) {
2360 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments)
2361 << AttrName->getName();
2362 break;
2363 }
2364 SourceLocation ParamLoc = ConsumeParen();
2365
John McCall60d7b3a2010-08-24 06:29:42 +00002366 ExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002367
2368 MatchRHSPunctuation(tok::r_paren, ParamLoc);
2369
2370 ExprVector ArgExprs(Actions);
2371 ArgExprs.push_back(ArgExpr.release());
John McCall0b7e6782011-03-24 11:26:52 +00002372 attrs.addNew(AttrName, AttrLoc, 0, AttrLoc,
2373 0, ParamLoc, ArgExprs.take(), 1,
2374 false, true);
Sean Huntbbd37c62009-11-21 08:43:09 +00002375
2376 AttrParsed = true;
2377 break;
2378 }
2379
2380 // Silence warnings
2381 default: break;
2382 }
2383 }
2384
2385 // Skip the entire parameter clause, if any
2386 if (!AttrParsed && Tok.is(tok::l_paren)) {
2387 ConsumeParen();
2388 // SkipUntil maintains the balancedness of tokens.
2389 SkipUntil(tok::r_paren, false);
2390 }
2391 }
2392
2393 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2394 SkipUntil(tok::r_square, false);
2395 Loc = Tok.getLocation();
2396 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
2397 SkipUntil(tok::r_square, false);
2398
John McCall7f040a92010-12-24 02:08:15 +00002399 attrs.Range = SourceRange(StartLoc, Loc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002400}
2401
2402/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]]
2403/// attribute.
2404///
2405/// FIXME: Simply returns an alignof() expression if the argument is a
2406/// type. Ideally, the type should be propagated directly into Sema.
2407///
2408/// [C++0x] 'align' '(' type-id ')'
2409/// [C++0x] 'align' '(' assignment-expression ')'
John McCall60d7b3a2010-08-24 06:29:42 +00002410ExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002411 if (isTypeIdInParens()) {
John McCallf312b1e2010-08-26 23:41:50 +00002412 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Sean Huntbbd37c62009-11-21 08:43:09 +00002413 SourceLocation TypeLoc = Tok.getLocation();
John McCallb3d87482010-08-24 05:47:05 +00002414 ParsedType Ty = ParseTypeName().get();
Sean Huntbbd37c62009-11-21 08:43:09 +00002415 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002416 return Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2417 Ty.getAsOpaquePtr(), TypeRange);
Sean Huntbbd37c62009-11-21 08:43:09 +00002418 } else
2419 return ParseConstantExpression();
2420}
Francois Pichet334d47e2010-10-11 12:59:39 +00002421
2422/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
2423///
2424/// [MS] ms-attribute:
2425/// '[' token-seq ']'
2426///
2427/// [MS] ms-attribute-seq:
2428/// ms-attribute[opt]
2429/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00002430void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
2431 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00002432 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
2433
2434 while (Tok.is(tok::l_square)) {
2435 ConsumeBracket();
2436 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00002437 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00002438 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
2439 }
2440}