blob: c0e17d7a5b6f26274c6bd4306d778e1fb762bf02 [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
Douglas Gregor1b7f8982008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/OperatorKinds.h"
Larisse Voufoef4579c2013-08-06 01:03:05 +000018#include "clang/AST/DeclTemplate.h"
Chris Lattner500d3292009-01-29 05:15:15 +000019#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000020#include "clang/Sema/DeclSpec.h"
John McCall19510852010-08-20 18:27:03 +000021#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000022#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Sema/Scope.h"
John McCalle402e722012-09-25 07:32:39 +000024#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000026using namespace clang;
27
28/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000029/// may either be a top level namespace or a block-level namespace alias. If
30/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000031///
32/// namespace-definition: [C++ 7.3: basic.namespace]
33/// named-namespace-definition
34/// unnamed-namespace-definition
35///
36/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000037/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000038///
39/// named-namespace-definition:
40/// original-namespace-definition
41/// extension-namespace-definition
42///
43/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000044/// 'inline'[opt] 'namespace' identifier attributes[opt]
45/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000046///
47/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000048/// 'inline'[opt] 'namespace' original-namespace-name
49/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000050///
Chris Lattner8f08cb72007-08-25 06:57:03 +000051/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
52/// 'namespace' identifier '=' qualified-namespace-specifier ';'
53///
John McCalld226f652010-08-21 09:40:31 +000054Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000055 SourceLocation &DeclEnd,
56 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000057 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000058 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +000059 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000060
Douglas Gregor49f40bd2009-09-18 19:03:04 +000061 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000062 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000063 cutOffParsing();
64 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +000065 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000066
Chris Lattner8f08cb72007-08-25 06:57:03 +000067 SourceLocation IdentLoc;
68 IdentifierInfo *Ident = 0;
Richard Trieuf858bd82011-05-26 20:11:09 +000069 std::vector<SourceLocation> ExtraIdentLoc;
70 std::vector<IdentifierInfo*> ExtraIdent;
71 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000072
73 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000074
Chris Lattner04d66662007-10-09 17:33:22 +000075 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000076 Ident = Tok.getIdentifierInfo();
77 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieuf858bd82011-05-26 20:11:09 +000078 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
79 ExtraNamespaceLoc.push_back(ConsumeToken());
80 ExtraIdent.push_back(Tok.getIdentifierInfo());
81 ExtraIdentLoc.push_back(ConsumeToken());
82 }
Chris Lattner8f08cb72007-08-25 06:57:03 +000083 }
Mike Stump1eb44332009-09-09 15:08:12 +000084
Chris Lattner8f08cb72007-08-25 06:57:03 +000085 // Read label attributes, if present.
John McCall0b7e6782011-03-24 11:26:52 +000086 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000087 if (Tok.is(tok::kw___attribute)) {
88 attrTok = Tok;
John McCall7f040a92010-12-24 02:08:15 +000089 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000090 }
Mike Stump1eb44332009-09-09 15:08:12 +000091
Douglas Gregor6a588dd2009-06-17 19:49:00 +000092 if (Tok.is(tok::equal)) {
Nico Webere1bb3292012-10-27 23:44:27 +000093 if (Ident == 0) {
94 Diag(Tok, diag::err_expected_ident);
95 // Skip to end of the definition and eat the ';'.
96 SkipUntil(tok::semi);
97 return 0;
98 }
John McCall7f040a92010-12-24 02:08:15 +000099 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000100 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +0000101 if (InlineLoc.isValid())
102 Diag(InlineLoc, diag::err_inline_namespace_alias)
103 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000104 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Richard Trieuf858bd82011-05-26 20:11:09 +0000107
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000108 BalancedDelimiterTracker T(*this, tok::l_brace);
109 if (T.consumeOpen()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000110 if (!ExtraIdent.empty()) {
111 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
112 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
113 }
Mike Stump1eb44332009-09-09 15:08:12 +0000114 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +0000115 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +0000116 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000117 }
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Douglas Gregor23c94db2010-07-02 17:43:08 +0000119 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
120 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
121 getCurScope()->getFnParent()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000122 if (!ExtraIdent.empty()) {
123 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
124 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
125 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000126 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Douglas Gregor95f1b152010-05-14 05:08:22 +0000127 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000128 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000129 }
130
Richard Trieuf858bd82011-05-26 20:11:09 +0000131 if (!ExtraIdent.empty()) {
132 TentativeParsingAction TPA(*this);
133 SkipUntil(tok::r_brace, /*StopAtSemi*/false, /*DontConsume*/true);
134 Token rBraceToken = Tok;
135 TPA.Revert();
136
137 if (!rBraceToken.is(tok::r_brace)) {
138 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
139 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
140 } else {
Benjamin Kramer9910df02011-05-26 21:32:30 +0000141 std::string NamespaceFix;
Richard Trieuf858bd82011-05-26 20:11:09 +0000142 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
143 E = ExtraIdent.end(); I != E; ++I) {
144 NamespaceFix += " { namespace ";
145 NamespaceFix += (*I)->getName();
146 }
Benjamin Kramer9910df02011-05-26 21:32:30 +0000147
Richard Trieuf858bd82011-05-26 20:11:09 +0000148 std::string RBraces;
Benjamin Kramer9910df02011-05-26 21:32:30 +0000149 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieuf858bd82011-05-26 20:11:09 +0000150 RBraces += "} ";
Benjamin Kramer9910df02011-05-26 21:32:30 +0000151
Richard Trieuf858bd82011-05-26 20:11:09 +0000152 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
153 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
154 ExtraIdentLoc.back()),
155 NamespaceFix)
156 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
157 }
158 }
159
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000160 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith7fe62082011-10-15 05:09:34 +0000161 if (InlineLoc.isValid())
Richard Smith80ad52f2013-01-02 11:42:31 +0000162 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000163 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000164
Chris Lattner51448322009-03-29 14:02:43 +0000165 // Enter a scope for the namespace.
166 ParseScope NamespaceScope(this, Scope::DeclScope);
167
John McCalld226f652010-08-21 09:40:31 +0000168 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000169 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000170 IdentLoc, Ident, T.getOpenLocation(),
171 attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000172
John McCallf312b1e2010-08-26 23:41:50 +0000173 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
174 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Richard Trieuf858bd82011-05-26 20:11:09 +0000176 // Parse the contents of the namespace. This includes parsing recovery on
177 // any improperly nested namespaces.
178 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000179 InlineLoc, attrs, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattner51448322009-03-29 14:02:43 +0000181 // Leave the namespace scope.
182 NamespaceScope.Exit();
183
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000184 DeclEnd = T.getCloseLocation();
185 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner51448322009-03-29 14:02:43 +0000186
187 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000188}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000189
Richard Trieuf858bd82011-05-26 20:11:09 +0000190/// ParseInnerNamespace - Parse the contents of a namespace.
191void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
192 std::vector<IdentifierInfo*>& Ident,
193 std::vector<SourceLocation>& NamespaceLoc,
194 unsigned int index, SourceLocation& InlineLoc,
Richard Trieuf858bd82011-05-26 20:11:09 +0000195 ParsedAttributes& attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000196 BalancedDelimiterTracker &Tracker) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000197 if (index == Ident.size()) {
198 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
199 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000200 MaybeParseCXX11Attributes(attrs);
Richard Trieuf858bd82011-05-26 20:11:09 +0000201 MaybeParseMicrosoftAttributes(attrs);
202 ParseExternalDeclaration(attrs);
203 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000204
205 // The caller is what called check -- we are simply calling
206 // the close for it.
207 Tracker.consumeClose();
Richard Trieuf858bd82011-05-26 20:11:09 +0000208
209 return;
210 }
211
212 // Parse improperly nested namespaces.
213 ParseScope NamespaceScope(this, Scope::DeclScope);
214 Decl *NamespcDecl =
215 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
216 NamespaceLoc[index], IdentLoc[index],
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000217 Ident[index], Tracker.getOpenLocation(),
218 attrs.getList());
Richard Trieuf858bd82011-05-26 20:11:09 +0000219
220 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000221 attrs, Tracker);
Richard Trieuf858bd82011-05-26 20:11:09 +0000222
223 NamespaceScope.Exit();
224
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000225 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieuf858bd82011-05-26 20:11:09 +0000226}
227
Anders Carlssonf67606a2009-03-28 04:07:16 +0000228/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
229/// alias definition.
230///
John McCalld226f652010-08-21 09:40:31 +0000231Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000232 SourceLocation AliasLoc,
233 IdentifierInfo *Alias,
234 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000235 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Anders Carlssonf67606a2009-03-28 04:07:16 +0000237 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000239 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000240 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000241 cutOffParsing();
242 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000243 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000244
Anders Carlssonf67606a2009-03-28 04:07:16 +0000245 CXXScopeSpec SS;
246 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000247 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000248
249 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
250 Diag(Tok, diag::err_expected_namespace_name);
251 // Skip to end of the definition and eat the ';'.
252 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000253 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000254 }
255
256 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000257 IdentifierInfo *Ident = Tok.getIdentifierInfo();
258 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Anders Carlssonf67606a2009-03-28 04:07:16 +0000260 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000261 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000262 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
263 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Douglas Gregor23c94db2010-07-02 17:43:08 +0000265 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000266 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000267}
268
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000269/// ParseLinkage - We know that the current token is a string_literal
270/// and just before that, that extern was seen.
271///
272/// linkage-specification: [C++ 7.5p2: dcl.link]
273/// 'extern' string-literal '{' declaration-seq[opt] '}'
274/// 'extern' string-literal declaration
275///
Chris Lattner7d642712010-11-09 20:15:55 +0000276Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000277 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000278 SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000279 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000280 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000281 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000282 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000283
Richard Smith99831e42012-03-06 03:21:47 +0000284 // FIXME: This is incorrect: linkage-specifiers are parsed in translation
285 // phase 7, so string-literal concatenation is supposed to occur.
286 // extern "" "C" "" "+" "+" { } is legal.
287 if (Tok.hasUDSuffix())
288 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000289 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000290
Douglas Gregor074149e2009-01-05 19:45:36 +0000291 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000292 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000293 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000294 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000295 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000296 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000297 : SourceLocation());
298
John McCall0b7e6782011-03-24 11:26:52 +0000299 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000300 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000301 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000302
Douglas Gregor074149e2009-01-05 19:45:36 +0000303 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-05-01 16:25:54 +0000304 // Reset the source range in DS, as the leading "extern"
305 // does not really belong to the inner declaration ...
306 DS.SetRangeStart(SourceLocation());
307 DS.SetRangeEnd(SourceLocation());
308 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000309 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000310 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000311 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000312 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000313 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000314
Douglas Gregor63a01132010-02-07 08:38:28 +0000315 DS.abort();
316
John McCall7f040a92010-12-24 02:08:15 +0000317 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000318
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000319 BalancedDelimiterTracker T(*this, tok::l_brace);
320 T.consumeOpen();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000321 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000322 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000323 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000324 MaybeParseMicrosoftAttributes(attrs);
325 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000326 }
327
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000328 T.consumeClose();
Chris Lattner7d642712010-11-09 20:15:55 +0000329 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000330 T.getCloseLocation());
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000331}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000332
Douglas Gregorf780abc2008-12-30 03:27:21 +0000333/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
334/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000335Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000336 const ParsedTemplateInfo &TemplateInfo,
337 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000338 ParsedAttributesWithRange &attrs,
339 Decl **OwnedType) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000340 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000341 ObjCDeclContextSwitch ObjCDC(*this);
342
Douglas Gregorf780abc2008-12-30 03:27:21 +0000343 // Eat 'using'.
344 SourceLocation UsingLoc = ConsumeToken();
345
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000346 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000347 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000348 cutOffParsing();
349 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000350 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000351
John McCall78b81052010-11-10 02:40:36 +0000352 // 'using namespace' means this is a using-directive.
353 if (Tok.is(tok::kw_namespace)) {
354 // Template parameters are always an error here.
355 if (TemplateInfo.Kind) {
356 SourceRange R = TemplateInfo.getSourceRange();
357 Diag(UsingLoc, diag::err_templated_using_directive)
358 << R << FixItHint::CreateRemoval(R);
359 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000360
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000361 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000362 }
363
Richard Smith162e1c12011-04-15 14:24:37 +0000364 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000365
366 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000367 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000368
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000369 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000370 AS_none, OwnedType);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000371}
372
373/// ParseUsingDirective - Parse C++ using-directive, assumes
374/// that current token is 'namespace' and 'using' was already parsed.
375///
376/// using-directive: [C++ 7.3.p4: namespace.udir]
377/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
378/// namespace-name ;
379/// [GNU] using-directive:
380/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
381/// namespace-name attributes[opt] ;
382///
John McCalld226f652010-08-21 09:40:31 +0000383Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000384 SourceLocation UsingLoc,
385 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000386 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000387 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
388
389 // Eat 'namespace'.
390 SourceLocation NamespcLoc = ConsumeToken();
391
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000392 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000393 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000394 cutOffParsing();
395 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000396 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000397
Douglas Gregorf780abc2008-12-30 03:27:21 +0000398 CXXScopeSpec SS;
399 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000400 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000401
Douglas Gregorf780abc2008-12-30 03:27:21 +0000402 IdentifierInfo *NamespcName = 0;
403 SourceLocation IdentLoc = SourceLocation();
404
405 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000406 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000407 Diag(Tok, diag::err_expected_namespace_name);
408 // If there was invalid namespace name, skip to end of decl, and eat ';'.
409 SkipUntil(tok::semi);
410 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000411 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000412 }
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Chris Lattner823c44e2009-01-06 07:27:21 +0000414 // Parse identifier.
415 NamespcName = Tok.getIdentifierInfo();
416 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Chris Lattner823c44e2009-01-06 07:27:21 +0000418 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000419 bool GNUAttr = false;
420 if (Tok.is(tok::kw___attribute)) {
421 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000422 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000423 }
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattner823c44e2009-01-06 07:27:21 +0000425 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000426 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000427 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000428 GNUAttr ? diag::err_expected_semi_after_attribute_list
429 : diag::err_expected_semi_after_namespace_name,
430 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000431
Douglas Gregor23c94db2010-07-02 17:43:08 +0000432 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000433 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000434}
435
Richard Smith162e1c12011-04-15 14:24:37 +0000436/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
437/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000438///
439/// using-declaration: [C++ 7.3.p3: namespace.udecl]
440/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000441/// unqualified-id
442/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000443///
Richard Smithd03de6a2013-01-29 10:02:16 +0000444/// alias-declaration: C++11 [dcl.dcl]p1
445/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smith162e1c12011-04-15 14:24:37 +0000446///
John McCalld226f652010-08-21 09:40:31 +0000447Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000448 const ParsedTemplateInfo &TemplateInfo,
449 SourceLocation UsingLoc,
450 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000451 AccessSpecifier AS,
452 Decl **OwnedType) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000453 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000454 SourceLocation TypenameLoc;
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000455 bool HasTypenameKeyword = false;
Sean Hunt2edf0a22012-06-23 05:07:58 +0000456
Richard Smith5eed7e02013-10-15 01:34:54 +0000457 // Check for misplaced attributes before the identifier in an
458 // alias-declaration.
459 ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
460 MaybeParseCXX11Attributes(MisplacedAttrs);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000461
462 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000463 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000464 if (Tok.is(tok::kw_typename)) {
Richard Smith6b3d3e52013-02-20 19:22:51 +0000465 TypenameLoc = ConsumeToken();
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000466 HasTypenameKeyword = true;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000467 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000468
469 // Parse nested-name-specifier.
Richard Smith2db075b2013-03-26 01:15:19 +0000470 IdentifierInfo *LastII = 0;
471 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
472 /*MayBePseudoDtor=*/0, /*IsTypename=*/false,
473 /*LastII=*/&LastII);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000474
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000475 // Check nested-name specifier.
476 if (SS.isInvalid()) {
477 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000478 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000479 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000480
Richard Smith2db075b2013-03-26 01:15:19 +0000481 SourceLocation TemplateKWLoc;
482 UnqualifiedId Name;
483
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000484 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000485 // destructor names and allow the action module to diagnose any semantic
486 // errors.
Richard Smith2db075b2013-03-26 01:15:19 +0000487 //
488 // C++11 [class.qual]p2:
489 // [...] in a using-declaration that is a member-declaration, if the name
490 // specified after the nested-name-specifier is the same as the identifier
491 // or the simple-template-id's template-name in the last component of the
492 // nested-name-specifier, the name is [...] considered to name the
493 // constructor.
494 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
495 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
496 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
497 !SS.getScopeRep()->getAsNamespace() &&
498 !SS.getScopeRep()->getAsNamespaceAlias()) {
499 SourceLocation IdLoc = ConsumeToken();
500 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
501 Name.setConstructorName(Type, IdLoc, IdLoc);
502 } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
503 /*AllowDestructorName=*/ true,
504 /*AllowConstructorName=*/ true, ParsedType(),
505 TemplateKWLoc, Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000506 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000507 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000508 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000509
Richard Smith5eed7e02013-10-15 01:34:54 +0000510 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smithdf1cce52013-10-24 01:21:09 +0000511 MaybeParseGNUAttributes(Attrs);
Richard Smith6b3d3e52013-02-20 19:22:51 +0000512 MaybeParseCXX11Attributes(Attrs);
Richard Smith162e1c12011-04-15 14:24:37 +0000513
514 // Maybe this is an alias-declaration.
Richard Smith162e1c12011-04-15 14:24:37 +0000515 TypeResult TypeAlias;
Richard Smith5eed7e02013-10-15 01:34:54 +0000516 bool IsAliasDecl = Tok.is(tok::equal);
Richard Smith162e1c12011-04-15 14:24:37 +0000517 if (IsAliasDecl) {
Richard Smith5eed7e02013-10-15 01:34:54 +0000518 // If we had any misplaced attributes from earlier, this is where they
519 // should have been written.
520 if (MisplacedAttrs.Range.isValid()) {
521 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
522 << FixItHint::CreateInsertionFromRange(
523 Tok.getLocation(),
524 CharSourceRange::getTokenRange(MisplacedAttrs.Range))
525 << FixItHint::CreateRemoval(MisplacedAttrs.Range);
526 Attrs.takeAllFrom(MisplacedAttrs);
527 }
528
Richard Smith162e1c12011-04-15 14:24:37 +0000529 ConsumeToken();
530
Richard Smith80ad52f2013-01-02 11:42:31 +0000531 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000532 diag::warn_cxx98_compat_alias_declaration :
533 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000534
Richard Smith3e4c6c42011-05-05 21:57:07 +0000535 // Type alias templates cannot be specialized.
536 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000537 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
538 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000539 SpecKind = 0;
540 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
541 SpecKind = 1;
542 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
543 SpecKind = 2;
544 if (SpecKind != -1) {
545 SourceRange Range;
546 if (SpecKind == 0)
547 Range = SourceRange(Name.TemplateId->LAngleLoc,
548 Name.TemplateId->RAngleLoc);
549 else
550 Range = TemplateInfo.getSourceRange();
551 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
552 << SpecKind << Range;
553 SkipUntil(tok::semi);
554 return 0;
555 }
556
Richard Smith162e1c12011-04-15 14:24:37 +0000557 // Name must be an identifier.
558 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
559 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
560 // No removal fixit: can't recover from this.
561 SkipUntil(tok::semi);
562 return 0;
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000563 } else if (HasTypenameKeyword)
Richard Smith162e1c12011-04-15 14:24:37 +0000564 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
565 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
566 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
567 else if (SS.isNotEmpty())
568 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
569 << FixItHint::CreateRemoval(SS.getRange());
570
Richard Smith3e4c6c42011-05-05 21:57:07 +0000571 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
572 Declarator::AliasTemplateContext :
Richard Smith6b3d3e52013-02-20 19:22:51 +0000573 Declarator::AliasDeclContext, AS, OwnedType,
574 &Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000575 } else {
576 // C++11 attributes are not allowed on a using-declaration, but GNU ones
577 // are.
Richard Smith5eed7e02013-10-15 01:34:54 +0000578 ProhibitAttributes(MisplacedAttrs);
Richard Smith6b3d3e52013-02-20 19:22:51 +0000579 ProhibitAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000580
Richard Smith162e1c12011-04-15 14:24:37 +0000581 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith6b3d3e52013-02-20 19:22:51 +0000582 MaybeParseGNUAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000585 // Eat ';'.
586 DeclEnd = Tok.getLocation();
587 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000588 !Attrs.empty() ? "attributes list" :
Richard Smith162e1c12011-04-15 14:24:37 +0000589 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000590 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000591
John McCall78b81052010-11-10 02:40:36 +0000592 // Diagnose an attempt to declare a templated using-declaration.
Richard Smithd03de6a2013-01-29 10:02:16 +0000593 // In C++11, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000594 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000595 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000596 SourceRange R = TemplateInfo.getSourceRange();
597 Diag(UsingLoc, diag::err_templated_using_declaration)
598 << R << FixItHint::CreateRemoval(R);
599
600 // Unfortunately, we have to bail out instead of recovering by
601 // ignoring the parameters, just in case the nested name specifier
602 // depends on the parameters.
603 return 0;
604 }
605
Douglas Gregor480b53c2011-09-26 14:30:28 +0000606 // "typename" keyword is allowed for identifiers only,
607 // because it may be a type definition.
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000608 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor480b53c2011-09-26 14:30:28 +0000609 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
610 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000611 // Proceed parsing, but reset the HasTypenameKeyword flag.
612 HasTypenameKeyword = false;
Douglas Gregor480b53c2011-09-26 14:30:28 +0000613 }
614
Richard Smith3e4c6c42011-05-05 21:57:07 +0000615 if (IsAliasDecl) {
616 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramer5354e772012-08-23 23:38:35 +0000617 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3e4c6c42011-05-05 21:57:07 +0000618 TemplateParams ? TemplateParams->data() : 0,
619 TemplateParams ? TemplateParams->size() : 0);
620 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000621 UsingLoc, Name, Attrs.getList(),
622 TypeAlias);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000623 }
Richard Smith162e1c12011-04-15 14:24:37 +0000624
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000625 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
626 /* HasUsingKeyword */ true, UsingLoc,
627 SS, Name, Attrs.getList(),
628 HasTypenameKeyword, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000629}
630
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000631/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000632///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000633/// [C++0x] static_assert-declaration:
634/// static_assert ( constant-expression , string-literal ) ;
635///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000636/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000637/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000638///
John McCalld226f652010-08-21 09:40:31 +0000639Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000640 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
641 "Not a static_assert declaration");
642
David Blaikie4e4d0842012-03-11 07:00:24 +0000643 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000644 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000645 if (Tok.is(tok::kw_static_assert))
646 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000647
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000648 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000650 BalancedDelimiterTracker T(*this, tok::l_paren);
651 if (T.consumeOpen()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000652 Diag(Tok, diag::err_expected_lparen);
Richard Smith3686c712012-09-13 19:12:50 +0000653 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000654 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000655 }
Mike Stump1eb44332009-09-09 15:08:12 +0000656
John McCall60d7b3a2010-08-24 06:29:42 +0000657 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000658 if (AssertExpr.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000659 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000660 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000661 }
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Anders Carlssonad5f9602009-03-13 23:29:20 +0000663 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000664 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000665
Richard Smith0cc323c2012-03-05 23:20:05 +0000666 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000667 Diag(Tok, diag::err_expected_string_literal)
668 << /*Source='static_assert'*/1;
Richard Smith3686c712012-09-13 19:12:50 +0000669 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000670 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000671 }
Mike Stump1eb44332009-09-09 15:08:12 +0000672
John McCall60d7b3a2010-08-24 06:29:42 +0000673 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smith99831e42012-03-06 03:21:47 +0000674 if (AssertMessage.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000675 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000676 return 0;
Richard Smith99831e42012-03-06 03:21:47 +0000677 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000678
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000679 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Chris Lattner97144fc2009-04-02 04:16:50 +0000681 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000682 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000683
John McCall9ae2f072010-08-23 23:25:46 +0000684 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
685 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000686 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000687 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000688}
689
Richard Smitha2c36462013-04-26 16:15:35 +0000690/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000691///
692/// 'decltype' ( expression )
Richard Smitha2c36462013-04-26 16:15:35 +0000693/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000694///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000695SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
696 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
697 && "Not a decltype specifier");
698
David Blaikie42d6d0c2011-12-04 05:04:18 +0000699 ExprResult Result;
700 SourceLocation StartLoc = Tok.getLocation();
701 SourceLocation EndLoc;
702
703 if (Tok.is(tok::annot_decltype)) {
704 Result = getExprAnnotation(Tok);
705 EndLoc = Tok.getAnnotationEndLoc();
706 ConsumeToken();
707 if (Result.isInvalid()) {
708 DS.SetTypeSpecError();
709 return EndLoc;
710 }
711 } else {
Richard Smithc7b55432012-02-24 22:30:04 +0000712 if (Tok.getIdentifierInfo()->isStr("decltype"))
713 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000714
David Blaikie42d6d0c2011-12-04 05:04:18 +0000715 ConsumeToken();
716
717 BalancedDelimiterTracker T(*this, tok::l_paren);
718 if (T.expectAndConsume(diag::err_expected_lparen_after,
719 "decltype", tok::r_paren)) {
720 DS.SetTypeSpecError();
721 return T.getOpenLocation() == Tok.getLocation() ?
722 StartLoc : T.getOpenLocation();
723 }
724
Richard Smitha2c36462013-04-26 16:15:35 +0000725 // Check for C++1y 'decltype(auto)'.
726 if (Tok.is(tok::kw_auto)) {
727 // No need to disambiguate here: an expression can't start with 'auto',
728 // because the typename-specifier in a function-style cast operation can't
729 // be 'auto'.
730 Diag(Tok.getLocation(),
731 getLangOpts().CPlusPlus1y
732 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
733 : diag::ext_decltype_auto_type_specifier);
734 ConsumeToken();
735 } else {
736 // Parse the expression
David Blaikie42d6d0c2011-12-04 05:04:18 +0000737
Richard Smitha2c36462013-04-26 16:15:35 +0000738 // C++11 [dcl.type.simple]p4:
739 // The operand of the decltype specifier is an unevaluated operand.
740 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
741 0, /*IsDecltype=*/true);
742 Result = ParseExpression();
743 if (Result.isInvalid()) {
744 DS.SetTypeSpecError();
745 if (SkipUntil(tok::r_paren, /*StopAtSemi=*/true,
746 /*DontConsume=*/true)) {
747 EndLoc = ConsumeParen();
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000748 } else {
Richard Smitha2c36462013-04-26 16:15:35 +0000749 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
750 // Backtrack to get the location of the last token before the semi.
751 PP.RevertCachedTokens(2);
752 ConsumeToken(); // the semi.
753 EndLoc = ConsumeAnyToken();
754 assert(Tok.is(tok::semi));
755 } else {
756 EndLoc = Tok.getLocation();
757 }
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000758 }
Richard Smitha2c36462013-04-26 16:15:35 +0000759 return EndLoc;
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000760 }
Richard Smitha2c36462013-04-26 16:15:35 +0000761
762 Result = Actions.ActOnDecltypeExpression(Result.take());
David Blaikie42d6d0c2011-12-04 05:04:18 +0000763 }
764
765 // Match the ')'
766 T.consumeClose();
767 if (T.getCloseLocation().isInvalid()) {
768 DS.SetTypeSpecError();
769 // FIXME: this should return the location of the last token
770 // that was consumed (by "consumeClose()")
771 return T.getCloseLocation();
772 }
773
Richard Smith76f3f692012-02-22 02:04:18 +0000774 if (Result.isInvalid()) {
775 DS.SetTypeSpecError();
776 return T.getCloseLocation();
777 }
778
David Blaikie42d6d0c2011-12-04 05:04:18 +0000779 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000780 }
Richard Smitha2c36462013-04-26 16:15:35 +0000781 assert(!Result.isInvalid());
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000783 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000784 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000785 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smitha2c36462013-04-26 16:15:35 +0000786 if (Result.get()
787 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
788 DiagID, Result.release())
789 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
790 DiagID)) {
John McCallfec54012009-08-03 20:12:06 +0000791 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000792 DS.SetTypeSpecError();
793 }
794 return EndLoc;
795}
796
797void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
798 SourceLocation StartLoc,
799 SourceLocation EndLoc) {
800 // make sure we have a token we can turn into an annotation token
801 if (PP.isBacktrackEnabled())
802 PP.RevertCachedTokens(1);
803 else
804 PP.EnterToken(Tok);
805
806 Tok.setKind(tok::annot_decltype);
Richard Smitha2c36462013-04-26 16:15:35 +0000807 setExprAnnotation(Tok,
808 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
809 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
810 ExprError());
David Blaikie42d6d0c2011-12-04 05:04:18 +0000811 Tok.setAnnotationEndLoc(EndLoc);
812 Tok.setLocation(StartLoc);
813 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000814}
815
Sean Huntdb5d44b2011-05-19 05:37:45 +0000816void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
817 assert(Tok.is(tok::kw___underlying_type) &&
818 "Not an underlying type specifier");
819
820 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000821 BalancedDelimiterTracker T(*this, tok::l_paren);
822 if (T.expectAndConsume(diag::err_expected_lparen_after,
823 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000824 return;
825 }
826
827 TypeResult Result = ParseTypeName();
828 if (Result.isInvalid()) {
829 SkipUntil(tok::r_paren);
830 return;
831 }
832
833 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000834 T.consumeClose();
835 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000836 return;
837
838 const char *PrevSpec = 0;
839 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000840 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000841 DiagID, Result.release()))
842 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanella2d776342013-07-06 18:54:58 +0000843 DS.setTypeofParensRange(T.getRange());
Sean Huntdb5d44b2011-05-19 05:37:45 +0000844}
845
David Blaikie09048df2011-10-25 15:01:20 +0000846/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
847/// class name or decltype-specifier. Note that we only check that the result
848/// names a type; semantic analysis will need to verify that the type names a
849/// class. The result is either a type or null, depending on whether a type
850/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000851///
Richard Smith05321402013-02-19 23:47:15 +0000852/// base-type-specifier: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000853/// class-or-decltype
Richard Smith05321402013-02-19 23:47:15 +0000854/// class-or-decltype: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000855/// nested-name-specifier[opt] class-name
856/// decltype-specifier
Richard Smith05321402013-02-19 23:47:15 +0000857/// class-name: [C++ class.name]
Douglas Gregor42a552f2008-11-05 20:51:48 +0000858/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000859/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000860///
Richard Smith05321402013-02-19 23:47:15 +0000861/// In C++98, instead of base-type-specifier, we have:
862///
863/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie22216eb2011-10-25 17:10:12 +0000864Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
865 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000866 // Ignore attempts to use typename
867 if (Tok.is(tok::kw_typename)) {
868 Diag(Tok, diag::err_expected_class_name_not_template)
869 << FixItHint::CreateRemoval(Tok.getLocation());
870 ConsumeToken();
871 }
872
David Blaikie152aa4b2011-10-25 18:17:58 +0000873 // Parse optional nested-name-specifier
874 CXXScopeSpec SS;
875 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
876
877 BaseLoc = Tok.getLocation();
878
David Blaikie22216eb2011-10-25 17:10:12 +0000879 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000880 // tok == kw_decltype is just error recovery, it can only happen when SS
881 // isn't empty
882 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000883 if (SS.isNotEmpty())
884 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
885 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000886 // Fake up a Declarator to use with ActOnTypeName.
887 DeclSpec DS(AttrFactory);
888
David Blaikieb5777572011-12-08 04:53:15 +0000889 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000890
891 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
892 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
893 }
894
Douglas Gregor7f43d672009-02-25 23:52:28 +0000895 // Check whether we have a template-id that names a type.
896 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000897 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000898 if (TemplateId->Kind == TNK_Type_template ||
899 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000900 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000901
902 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000903 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000904 EndLocation = Tok.getAnnotationEndLoc();
905 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000906
907 if (Type)
908 return Type;
909 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000910 }
911
912 // Fall through to produce an error below.
913 }
914
Douglas Gregor42a552f2008-11-05 20:51:48 +0000915 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000916 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000917 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000918 }
919
Douglas Gregor84d0a192010-01-12 21:28:44 +0000920 IdentifierInfo *Id = Tok.getIdentifierInfo();
921 SourceLocation IdLoc = ConsumeToken();
922
923 if (Tok.is(tok::less)) {
924 // It looks the user intended to write a template-id here, but the
925 // template-name was wrong. Try to fix that.
926 TemplateNameKind TNK = TNK_Type_template;
927 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000928 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000929 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000930 Diag(IdLoc, diag::err_unknown_template_name)
931 << Id;
932 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000933
Serge Pavlov62f675c2013-08-10 05:54:47 +0000934 if (!Template) {
935 TemplateArgList TemplateArgs;
936 SourceLocation LAngleLoc, RAngleLoc;
937 ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
938 true, LAngleLoc, TemplateArgs, RAngleLoc);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000939 return true;
Serge Pavlov62f675c2013-08-10 05:54:47 +0000940 }
Douglas Gregor84d0a192010-01-12 21:28:44 +0000941
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000942 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000943 UnqualifiedId TemplateName;
944 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000945
Douglas Gregor84d0a192010-01-12 21:28:44 +0000946 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000947 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
948 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000949 return true;
950 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000951 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000952
Douglas Gregor84d0a192010-01-12 21:28:44 +0000953 // If we didn't end up with a typename token, there's nothing more we
954 // can do.
955 if (Tok.isNot(tok::annot_typename))
956 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000957
Douglas Gregor84d0a192010-01-12 21:28:44 +0000958 // Retrieve the type from the annotation token, consume that token, and
959 // return.
960 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000961 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000962 ConsumeToken();
963 return Type;
964 }
965
Douglas Gregor42a552f2008-11-05 20:51:48 +0000966 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000967 IdentifierInfo *CorrectedII = 0;
Douglas Gregor059101f2011-03-02 00:47:37 +0000968 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000969 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000970 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000971 /*NonTrivialTypeSourceInfo=*/true,
972 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000973 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000974 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000975 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000976 }
977
978 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000979 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000980
981 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000982 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000983 DS.SetRangeStart(IdLoc);
984 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000985 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000986
987 const char *PrevSpec = 0;
988 unsigned DiagID;
989 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
990
991 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
992 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000993}
994
John McCallc052dbb2012-05-22 21:28:12 +0000995void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
996 while (Tok.is(tok::kw___single_inheritance) ||
997 Tok.is(tok::kw___multiple_inheritance) ||
998 Tok.is(tok::kw___virtual_inheritance)) {
999 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1000 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman624421f2013-08-31 01:11:41 +00001001 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
1002 AttributeList::AS_GNU);
John McCallc052dbb2012-05-22 21:28:12 +00001003 }
1004}
1005
Richard Smithc9f35172012-06-25 21:37:02 +00001006/// Determine whether the following tokens are valid after a type-specifier
1007/// which could be a standalone declaration. This will conservatively return
1008/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith139be702012-07-02 19:14:01 +00001009bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smithc9f35172012-06-25 21:37:02 +00001010 // This switch enumerates the valid "follow" set for type-specifiers.
1011 switch (Tok.getKind()) {
1012 default: break;
1013 case tok::semi: // struct foo {...} ;
1014 case tok::star: // struct foo {...} * P;
1015 case tok::amp: // struct foo {...} & R = ...
Richard Smithba65f502013-01-19 03:48:05 +00001016 case tok::ampamp: // struct foo {...} && R = ...
Richard Smithc9f35172012-06-25 21:37:02 +00001017 case tok::identifier: // struct foo {...} V ;
1018 case tok::r_paren: //(struct foo {...} ) {4}
1019 case tok::annot_cxxscope: // struct foo {...} a:: b;
1020 case tok::annot_typename: // struct foo {...} a ::b;
1021 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1022 case tok::l_paren: // struct foo {...} ( x);
1023 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smithba65f502013-01-19 03:48:05 +00001024 case tok::kw_operator: // struct foo operator ++() {...}
Richard Smithc9f35172012-06-25 21:37:02 +00001025 return true;
Richard Smith139be702012-07-02 19:14:01 +00001026 case tok::colon:
1027 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smithc9f35172012-06-25 21:37:02 +00001028 // Type qualifiers
1029 case tok::kw_const: // struct foo {...} const x;
1030 case tok::kw_volatile: // struct foo {...} volatile x;
1031 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smithba65f502013-01-19 03:48:05 +00001032 // Function specifiers
1033 // Note, no 'explicit'. An explicit function must be either a conversion
1034 // operator or a constructor. Either way, it can't have a return type.
1035 case tok::kw_inline: // struct foo inline f();
1036 case tok::kw_virtual: // struct foo virtual f();
1037 case tok::kw_friend: // struct foo friend f();
Richard Smithc9f35172012-06-25 21:37:02 +00001038 // Storage-class specifiers
1039 case tok::kw_static: // struct foo {...} static x;
1040 case tok::kw_extern: // struct foo {...} extern x;
1041 case tok::kw_typedef: // struct foo {...} typedef x;
1042 case tok::kw_register: // struct foo {...} register x;
1043 case tok::kw_auto: // struct foo {...} auto x;
1044 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smithba65f502013-01-19 03:48:05 +00001045 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smithc9f35172012-06-25 21:37:02 +00001046 case tok::kw_constexpr: // struct foo {...} constexpr x;
1047 // As shown above, type qualifiers and storage class specifiers absolutely
1048 // can occur after class specifiers according to the grammar. However,
1049 // almost no one actually writes code like this. If we see one of these,
1050 // it is much more likely that someone missed a semi colon and the
1051 // type/storage class specifier we're seeing is part of the *next*
1052 // intended declaration, as in:
1053 //
1054 // struct foo { ... }
1055 // typedef int X;
1056 //
1057 // We'd really like to emit a missing semicolon error instead of emitting
1058 // an error on the 'int' saying that you can't have two type specifiers in
1059 // the same declaration of X. Because of this, we look ahead past this
1060 // token to see if it's a type specifier. If so, we know the code is
1061 // otherwise invalid, so we can produce the expected semi error.
1062 if (!isKnownToBeTypeSpecifier(NextToken()))
1063 return true;
1064 break;
1065 case tok::r_brace: // struct bar { struct foo {...} }
1066 // Missing ';' at end of struct is accepted as an extension in C mode.
1067 if (!getLangOpts().CPlusPlus)
1068 return true;
1069 break;
Richard Smithba65f502013-01-19 03:48:05 +00001070 // C++11 attributes
1071 case tok::l_square: // enum E [[]] x
1072 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1073 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith8338a9d2013-01-29 04:13:32 +00001074 case tok::greater:
1075 // template<class T = class X>
1076 return getLangOpts().CPlusPlus;
Richard Smithc9f35172012-06-25 21:37:02 +00001077 }
1078 return false;
1079}
1080
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001081/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1082/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1083/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +00001084/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001085///
1086/// class-specifier: [C++ class]
1087/// class-head '{' member-specification[opt] '}'
1088/// class-head '{' member-specification[opt] '}' attributes[opt]
1089/// class-head:
1090/// class-key identifier[opt] base-clause[opt]
1091/// class-key nested-name-specifier identifier base-clause[opt]
1092/// class-key nested-name-specifier[opt] simple-template-id
1093/// base-clause[opt]
1094/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001095/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001096/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001097/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001098/// simple-template-id base-clause[opt]
1099/// class-key:
1100/// 'class'
1101/// 'struct'
1102/// 'union'
1103///
1104/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +00001105/// class-key ::[opt] nested-name-specifier[opt] identifier
1106/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1107/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001108///
1109/// Note that the C++ class-specifier and elaborated-type-specifier,
1110/// together, subsume the C99 struct-or-union-specifier:
1111///
1112/// struct-or-union-specifier: [C99 6.7.2.1]
1113/// struct-or-union identifier[opt] '{' struct-contents '}'
1114/// struct-or-union identifier
1115/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1116/// '}' attributes[opt]
1117/// [GNU] struct-or-union attributes[opt] identifier
1118/// struct-or-union:
1119/// 'struct'
1120/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +00001121void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1122 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001123 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001124 AccessSpecifier AS,
Michael Han2e397132012-11-26 22:54:45 +00001125 bool EnteringContext, DeclSpecContext DSC,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001126 ParsedAttributesWithRange &Attributes) {
Joao Matos17d35c32012-08-31 22:18:20 +00001127 DeclSpec::TST TagType;
1128 if (TagTokKind == tok::kw_struct)
1129 TagType = DeclSpec::TST_struct;
1130 else if (TagTokKind == tok::kw___interface)
1131 TagType = DeclSpec::TST_interface;
1132 else if (TagTokKind == tok::kw_class)
1133 TagType = DeclSpec::TST_class;
1134 else {
Chris Lattner4c97d762009-04-12 21:49:30 +00001135 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1136 TagType = DeclSpec::TST_union;
1137 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001138
Douglas Gregor374929f2009-09-18 15:37:17 +00001139 if (Tok.is(tok::code_completion)) {
1140 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001141 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001142 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00001143 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001144
Chandler Carruth926c4b42010-06-28 08:39:25 +00001145 // C++03 [temp.explicit] 14.7.2/8:
1146 // The usual access checking rules do not apply to names used to specify
1147 // explicit instantiations.
1148 //
1149 // As an extension we do not perform access checking on the names used to
1150 // specify explicit specializations either. This is important to allow
1151 // specializing traits classes for private types.
John McCall13489672012-05-07 06:16:58 +00001152 //
1153 // Note that we don't suppress if this turns out to be an elaborated
1154 // type specifier.
1155 bool shouldDelayDiagsInTag =
1156 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1157 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1158 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001159
Sean Hunt2edf0a22012-06-23 05:07:58 +00001160 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001161 // If attributes exist after tag, parse them.
Richard Smithdf1cce52013-10-24 01:21:09 +00001162 MaybeParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001163
Steve Narofff59e17e2008-12-24 20:59:21 +00001164 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001165 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001166 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001167
John McCallc052dbb2012-05-22 21:28:12 +00001168 // Parse inheritance specifiers.
1169 if (Tok.is(tok::kw___single_inheritance) ||
1170 Tok.is(tok::kw___multiple_inheritance) ||
1171 Tok.is(tok::kw___virtual_inheritance))
Richard Smithdf1cce52013-10-24 01:21:09 +00001172 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCallc052dbb2012-05-22 21:28:12 +00001173
Sean Huntbbd37c62009-11-21 08:43:09 +00001174 // If C++0x attributes exist here, parse them.
1175 // FIXME: Are we consistent with the ordering of parsing of different
1176 // styles of attributes?
Richard Smith4e24f0f2013-01-02 12:01:23 +00001177 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Michael Han07fc1ba2013-01-07 16:57:11 +00001179 // Source location used by FIXIT to insert misplaced
1180 // C++11 attributes
1181 SourceLocation AttrFixitLoc = Tok.getLocation();
1182
John Wiegley20c0da72011-04-27 23:09:49 +00001183 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +00001184 !Tok.is(tok::identifier) &&
1185 Tok.getIdentifierInfo() &&
1186 (Tok.is(tok::kw___is_arithmetic) ||
1187 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001188 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001189 Tok.is(tok::kw___is_floating_point) ||
1190 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001191 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001192 Tok.is(tok::kw___is_integral) ||
1193 Tok.is(tok::kw___is_member_function_pointer) ||
1194 Tok.is(tok::kw___is_member_pointer) ||
1195 Tok.is(tok::kw___is_pod) ||
1196 Tok.is(tok::kw___is_pointer) ||
1197 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001198 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001199 Tok.is(tok::kw___is_signed) ||
1200 Tok.is(tok::kw___is_unsigned) ||
1201 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001202 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001203 // name of struct templates, but some are keywords in GCC >= 4.3
1204 // and Clang. Therefore, when we see the token sequence "struct
1205 // X", make X into a normal identifier rather than a keyword, to
1206 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001207 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001208 Tok.setKind(tok::identifier);
1209 }
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001211 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001212 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001213 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001214 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1215 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001216
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001217 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001218 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001219 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001220 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1221 Diag(Tok, diag::err_expected_ident);
1222 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001223
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001224 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1225
Douglas Gregorcc636682009-02-17 23:15:12 +00001226 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001227 IdentifierInfo *Name = 0;
1228 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001229 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001230 if (Tok.is(tok::identifier)) {
1231 Name = Tok.getIdentifierInfo();
1232 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001233
David Blaikie4e4d0842012-03-11 07:00:24 +00001234 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001235 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001236 // Eat the template argument list and try to continue parsing this as
1237 // a class (or template thereof).
1238 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001239 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001240 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001241 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001242 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001243 // We couldn't parse the template argument list at all, so don't
1244 // try to give any location information for the list.
1245 LAngleLoc = RAngleLoc = SourceLocation();
1246 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001247
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001248 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matos17d35c32012-08-31 22:18:20 +00001249 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1250 << (TagType == DeclSpec::TST_class? 0
1251 : TagType == DeclSpec::TST_struct? 1
1252 : TagType == DeclSpec::TST_interface? 2
1253 : 3)
1254 << Name
1255 << SourceRange(LAngleLoc, RAngleLoc);
1256
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001257 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001258 // we've removed its template argument list.
1259 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1260 if (TemplateParams && TemplateParams->size() > 1) {
1261 TemplateParams->pop_back();
1262 } else {
1263 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001264 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001265 = ParsedTemplateInfo::NonTemplate;
1266 }
1267 } else if (TemplateInfo.Kind
1268 == ParsedTemplateInfo::ExplicitInstantiation) {
1269 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001270 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001271 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001272 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001273 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001274 = SourceLocation();
1275 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1276 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001277 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001278 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001279 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001280 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001281 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001282
Douglas Gregor059101f2011-03-02 00:47:37 +00001283 if (TemplateId->Kind != TNK_Type_template &&
1284 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001285 // The template-name in the simple-template-id refers to
1286 // something other than a class template. Give an appropriate
1287 // error message and skip to the ';'.
1288 SourceRange Range(NameLoc);
1289 if (SS.isNotEmpty())
1290 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001291
Douglas Gregor39a8de12009-02-25 19:37:18 +00001292 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu6e91f4b2013-06-19 22:25:01 +00001293 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Douglas Gregor39a8de12009-02-25 19:37:18 +00001295 DS.SetTypeSpecError();
1296 SkipUntil(tok::semi, false, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001297 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001298 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001299 }
1300
Richard Smith7796eb52012-03-12 08:56:40 +00001301 // There are four options here.
1302 // - If we are in a trailing return type, this is always just a reference,
1303 // and we must not try to parse a definition. For instance,
1304 // [] () -> struct S { };
1305 // does not define a type.
1306 // - If we have 'struct foo {...', 'struct foo :...',
1307 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1308 // - If we have 'struct foo;', then this is either a forward declaration
1309 // or a friend declaration, which have to be treated differently.
1310 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han2e397132012-11-26 22:54:45 +00001311 //
1312 // We also detect these erroneous cases to provide better diagnostic for
1313 // C++11 attributes parsing.
1314 // - attributes follow class name:
1315 // struct foo [[]] {};
1316 // - attributes appear before or after 'final':
1317 // struct foo [[]] final [[]] {};
1318 //
Richard Smith69730c12012-03-12 07:56:15 +00001319 // However, in type-specifier-seq's, things look like declarations but are
1320 // just references, e.g.
1321 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001322 // or
Richard Smith69730c12012-03-12 07:56:15 +00001323 // &T::operator struct s;
1324 // For these, DSC is DSC_type_specifier.
Michael Han2e397132012-11-26 22:54:45 +00001325
1326 // If there are attributes after class name, parse them.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001327 MaybeParseCXX11Attributes(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00001328
John McCallf312b1e2010-08-26 23:41:50 +00001329 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001330 if (DSC == DSC_trailing)
1331 TUK = Sema::TUK_Reference;
1332 else if (Tok.is(tok::l_brace) ||
1333 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001334 (isCXX11FinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001335 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001336 if (DS.isFriendSpecified()) {
1337 // C++ [class.friend]p2:
1338 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001339 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001340 << SourceRange(DS.getFriendSpecLoc());
1341
1342 // Skip everything up to the semicolon, so that this looks like a proper
1343 // friend class (or template thereof) declaration.
1344 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001345 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001346 } else {
1347 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001348 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001349 }
Richard Smith150d8532013-02-22 06:46:23 +00001350 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1351 NextToken().is(tok::kw_alignas))) {
Michael Han2e397132012-11-26 22:54:45 +00001352 // We can't tell if this is a definition or reference
1353 // until we skipped the 'final' and C++11 attribute specifiers.
1354 TentativeParsingAction PA(*this);
1355
1356 // Skip the 'final' keyword.
1357 ConsumeToken();
1358
1359 // Skip C++11 attribute specifiers.
1360 while (true) {
1361 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1362 ConsumeBracket();
1363 if (!SkipUntil(tok::r_square))
1364 break;
Richard Smith150d8532013-02-22 06:46:23 +00001365 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han2e397132012-11-26 22:54:45 +00001366 ConsumeToken();
1367 ConsumeParen();
1368 if (!SkipUntil(tok::r_paren))
1369 break;
1370 } else {
1371 break;
1372 }
1373 }
1374
1375 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1376 TUK = Sema::TUK_Definition;
1377 else
1378 TUK = Sema::TUK_Reference;
1379
1380 PA.Revert();
Richard Smithc9f35172012-06-25 21:37:02 +00001381 } else if (DSC != DSC_type_specifier &&
1382 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00001383 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallf312b1e2010-08-26 23:41:50 +00001384 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matos17d35c32012-08-31 22:18:20 +00001385 if (Tok.isNot(tok::semi)) {
1386 // A semicolon was missing after this declaration. Diagnose and recover.
1387 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1388 DeclSpec::getSpecifierName(TagType));
1389 PP.EnterToken(Tok);
1390 Tok.setKind(tok::semi);
1391 }
Richard Smithc9f35172012-06-25 21:37:02 +00001392 } else
John McCallf312b1e2010-08-26 23:41:50 +00001393 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001394
Michael Han2e397132012-11-26 22:54:45 +00001395 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1396 // to caller to handle.
Michael Han07fc1ba2013-01-07 16:57:11 +00001397 if (TUK != Sema::TUK_Reference) {
1398 // If this is not a reference, then the only possible
1399 // valid place for C++11 attributes to appear here
1400 // is between class-key and class-name. If there are
1401 // any attributes after class-name, we try a fixit to move
1402 // them to the right place.
1403 SourceRange AttrRange = Attributes.Range;
1404 if (AttrRange.isValid()) {
1405 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1406 << AttrRange
1407 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1408 CharSourceRange(AttrRange, true))
1409 << FixItHint::CreateRemoval(AttrRange);
1410
1411 // Recover by adding misplaced attributes to the attribute list
1412 // of the class so they can be applied on the class later.
1413 attrs.takeAllFrom(Attributes);
1414 }
1415 }
Michael Han2e397132012-11-26 22:54:45 +00001416
John McCall13489672012-05-07 06:16:58 +00001417 // If this is an elaborated type specifier, and we delayed
1418 // diagnostics before, just merge them into the current pool.
1419 if (shouldDelayDiagsInTag) {
1420 diagsFromTag.done();
1421 if (TUK == Sema::TUK_Reference)
1422 diagsFromTag.redelay();
1423 }
1424
John McCall207014e2010-07-30 06:26:29 +00001425 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001426 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001427 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1428 // We have a declaration or reference to an anonymous class.
1429 Diag(StartLoc, diag::err_anon_type_definition)
1430 << DeclSpec::getSpecifierName(TagType);
1431 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001432
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001433 SkipUntil(tok::comma, true);
1434 return;
1435 }
1436
Douglas Gregorddc29e12009-02-06 22:42:48 +00001437 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001438 DeclResult TagOrTempResult = true; // invalid
1439 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001440
Douglas Gregor402abb52009-05-28 23:31:59 +00001441 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001442 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001443 // Explicit specialization, class template partial specialization,
1444 // or explicit instantiation.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001445 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001446 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001447 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001448 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001449 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001450 ProhibitAttributes(attrs);
1451
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001452 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001453 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001454 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001455 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001456 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001457 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001458 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001459 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001460 TemplateId->TemplateNameLoc,
1461 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001462 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001463 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001464 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001465
1466 // Friend template-ids are treated as references unless
1467 // they have template headers, in which case they're ill-formed
1468 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1469 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001470 } else if (TUK == Sema::TUK_Reference ||
1471 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001472 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001473 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001474 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001475 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001476 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001477 TemplateId->Template,
1478 TemplateId->TemplateNameLoc,
1479 TemplateId->LAngleLoc,
1480 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001481 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001482 } else {
1483 // This is an explicit specialization or a class template
1484 // partial specialization.
1485 TemplateParameterLists FakedParamLists;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001486 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1487 // This looks like an explicit instantiation, because we have
1488 // something like
1489 //
1490 // template class Foo<X>
1491 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001492 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001493 // meant to be an explicit specialization, but the user forgot
1494 // the '<>' after 'template'.
Richard Smith61dfea92013-11-08 19:03:29 +00001495 // It this is friend declaration however, since it cannot have a
1496 // template header, it is most likely that the user meant to
1497 // remove the 'template' keyword.
Larisse Voufo49854292013-06-22 13:56:11 +00001498 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith61dfea92013-11-08 19:03:29 +00001499 "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001500
Richard Smith61dfea92013-11-08 19:03:29 +00001501 if (TUK == Sema::TUK_Friend) {
1502 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
1503 TemplateParams = 0;
1504 } else {
1505 SourceLocation LAngleLoc =
1506 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1507 Diag(TemplateId->TemplateNameLoc,
1508 diag::err_explicit_instantiation_with_definition)
1509 << SourceRange(TemplateInfo.TemplateLoc)
1510 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1511
1512 // Create a fake template parameter list that contains only
1513 // "template<>", so that we treat this construct as a class
1514 // template specialization.
1515 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1516 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1517 LAngleLoc));
1518 TemplateParams = &FakedParamLists;
1519 }
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001520 }
1521
1522 // Build the class template specialization.
1523 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001524 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001525 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001526 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001527 TemplateId->TemplateNameLoc,
1528 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001529 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001530 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001531 attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001532 MultiTemplateParamsArg(
Douglas Gregorcc636682009-02-17 23:15:12 +00001533 TemplateParams? &(*TemplateParams)[0] : 0,
1534 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001535 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001536 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001537 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001538 // Explicit instantiation of a member of a class template
1539 // specialization, e.g.,
1540 //
1541 // template struct Outer<int>::Inner;
1542 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001543 ProhibitAttributes(attrs);
1544
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001545 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001546 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001547 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001548 TemplateInfo.TemplateLoc,
1549 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001550 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001551 } else if (TUK == Sema::TUK_Friend &&
1552 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001553 ProhibitAttributes(attrs);
1554
John McCall9a34edb2010-10-19 01:40:49 +00001555 TagOrTempResult =
1556 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1557 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001558 Name, NameLoc, attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001559 MultiTemplateParamsArg(
John McCall9a34edb2010-10-19 01:40:49 +00001560 TemplateParams? &(*TemplateParams)[0] : 0,
1561 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001562 } else {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001563 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1564 ProhibitAttributes(attrs);
Richard Smith61dfea92013-11-08 19:03:29 +00001565
Larisse Voufo7c64ef02013-06-21 00:08:46 +00001566 if (TUK == Sema::TUK_Definition &&
1567 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1568 // If the declarator-id is not a template-id, issue a diagnostic and
1569 // recover by ignoring the 'template' keyword.
1570 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1571 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Larisse Voufo49854292013-06-22 13:56:11 +00001572 TemplateParams = 0;
Larisse Voufo7c64ef02013-06-21 00:08:46 +00001573 }
Sean Hunt2edf0a22012-06-23 05:07:58 +00001574
John McCallc4e70192009-09-11 04:59:25 +00001575 bool IsDependent = false;
1576
John McCalla25c4082010-10-19 18:40:57 +00001577 // Don't pass down template parameter lists if this is just a tag
1578 // reference. For example, we don't need the template parameters here:
1579 // template <class T> class A *makeA(T t);
1580 MultiTemplateParamsArg TParams;
1581 if (TUK != Sema::TUK_Reference && TemplateParams)
1582 TParams =
1583 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1584
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001585 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001586 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001587 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001588 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001589 TParams, Owned, IsDependent,
1590 SourceLocation(), false,
1591 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001592
1593 // If ActOnTag said the type was dependent, try again with the
1594 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001595 if (IsDependent) {
1596 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001597 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001598 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001599 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001600 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001601
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001602 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001603 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001604 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001605 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001606 isCXX11FinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001607 if (getLangOpts().CPlusPlus)
Michael Han07fc1ba2013-01-07 16:57:11 +00001608 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1609 TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001610 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001611 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001612 }
1613
John McCallb3d87482010-08-24 05:47:05 +00001614 const char *PrevSpec = 0;
1615 unsigned DiagID;
1616 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001617 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001618 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1619 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001620 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001621 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001622 Result = DS.SetTypeSpecType(TagType, StartLoc,
1623 NameLoc.isValid() ? NameLoc : StartLoc,
1624 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001625 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001626 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001627 return;
1628 }
Mike Stump1eb44332009-09-09 15:08:12 +00001629
John McCallb3d87482010-08-24 05:47:05 +00001630 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001631 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001632
Chris Lattner4ed5d912010-02-02 01:23:29 +00001633 // At this point, we've successfully parsed a class-specifier in 'definition'
1634 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1635 // going to look at what comes after it to improve error recovery. If an
1636 // impossible token occurs next, we assume that the programmer forgot a ; at
1637 // the end of the declaration and recover that way.
1638 //
Richard Smithc9f35172012-06-25 21:37:02 +00001639 // Also enforce C++ [temp]p3:
1640 // In a template-declaration which defines a class, no declarator
1641 // is permitted.
Joao Matos17d35c32012-08-31 22:18:20 +00001642 if (TUK == Sema::TUK_Definition &&
1643 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidis7d033b22012-12-17 20:10:43 +00001644 if (Tok.isNot(tok::semi)) {
1645 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1646 DeclSpec::getSpecifierName(TagType));
1647 // Push this token back into the preprocessor and change our current token
1648 // to ';' so that the rest of the code recovers as though there were an
1649 // ';' after the definition.
1650 PP.EnterToken(Tok);
1651 Tok.setKind(tok::semi);
1652 }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001653 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001654}
1655
Mike Stump1eb44332009-09-09 15:08:12 +00001656/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001657///
1658/// base-clause : [C++ class.derived]
1659/// ':' base-specifier-list
1660/// base-specifier-list:
1661/// base-specifier '...'[opt]
1662/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001663void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001664 assert(Tok.is(tok::colon) && "Not a base clause");
1665 ConsumeToken();
1666
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001667 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001668 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001669
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001670 while (true) {
1671 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001672 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001673 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001674 // Skip the rest of this base specifier, up until the comma or
1675 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001676 SkipUntil(tok::comma, tok::l_brace, true, true);
1677 } else {
1678 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001679 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001680 }
1681
1682 // If the next token is a comma, consume it and keep reading
1683 // base-specifiers.
1684 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001686 // Consume the comma.
1687 ConsumeToken();
1688 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001689
1690 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001691 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001692}
1693
1694/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1695/// one entry in the base class list of a class specifier, for example:
1696/// class foo : public bar, virtual private baz {
1697/// 'public bar' and 'virtual private baz' are each base-specifiers.
1698///
1699/// base-specifier: [C++ class.derived]
Richard Smith05321402013-02-19 23:47:15 +00001700/// attribute-specifier-seq[opt] base-type-specifier
1701/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1702/// base-type-specifier
1703/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1704/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001705Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001706 bool IsVirtual = false;
1707 SourceLocation StartLoc = Tok.getLocation();
1708
Richard Smith05321402013-02-19 23:47:15 +00001709 ParsedAttributesWithRange Attributes(AttrFactory);
1710 MaybeParseCXX11Attributes(Attributes);
1711
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001712 // Parse the 'virtual' keyword.
1713 if (Tok.is(tok::kw_virtual)) {
1714 ConsumeToken();
1715 IsVirtual = true;
1716 }
1717
Richard Smith05321402013-02-19 23:47:15 +00001718 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1719
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001720 // Parse an (optional) access specifier.
1721 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001722 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001723 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001724
Richard Smith05321402013-02-19 23:47:15 +00001725 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1726
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001727 // Parse the 'virtual' keyword (again!), in case it came after the
1728 // access specifier.
1729 if (Tok.is(tok::kw_virtual)) {
1730 SourceLocation VirtualLoc = ConsumeToken();
1731 if (IsVirtual) {
1732 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001733 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001734 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001735 }
1736
1737 IsVirtual = true;
1738 }
1739
Richard Smith05321402013-02-19 23:47:15 +00001740 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1741
Douglas Gregor42a552f2008-11-05 20:51:48 +00001742 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001743 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001744 SourceLocation BaseLoc;
1745 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001746 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001747 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001748
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001749 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1750 // actually part of the base-specifier-list grammar productions, but we
1751 // parse it here for convenience.
1752 SourceLocation EllipsisLoc;
1753 if (Tok.is(tok::ellipsis))
1754 EllipsisLoc = ConsumeToken();
1755
Mike Stump1eb44332009-09-09 15:08:12 +00001756 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001757 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001759 // Notify semantic analysis that we have parsed a complete
1760 // base-specifier.
Richard Smith05321402013-02-19 23:47:15 +00001761 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1762 Access, BaseType.get(), BaseLoc,
1763 EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001764}
1765
1766/// getAccessSpecifierIfPresent - Determine whether the next token is
1767/// a C++ access-specifier.
1768///
1769/// access-specifier: [C++ class.derived]
1770/// 'private'
1771/// 'protected'
1772/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001773AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001774 switch (Tok.getKind()) {
1775 default: return AS_none;
1776 case tok::kw_private: return AS_private;
1777 case tok::kw_protected: return AS_protected;
1778 case tok::kw_public: return AS_public;
1779 }
1780}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001781
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001782/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001783/// delayed, e.g., default arguments, create a late-parsed method declaration
1784/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001785void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1786 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001787 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001788 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001789 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001790 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001791 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001792
Eli Friedmand33133c2009-07-22 21:45:50 +00001793 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1794 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1795 if (!LateMethod) {
1796 // Push this method onto the stack of late-parsed method
1797 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001798 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1799 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001800 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001801
1802 // Add all of the parameters prior to this one (they don't
1803 // have default arguments).
1804 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1805 for (unsigned I = 0; I < ParamIdx; ++I)
1806 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001807 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001808 }
1809
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001810 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001811 // not have a default argument).
1812 LateMethod->DefaultArgs.push_back(
1813 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1814 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1815 }
1816 }
1817}
1818
Richard Smith4e24f0f2013-01-02 12:01:23 +00001819/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001820/// virt-specifier.
1821///
1822/// virt-specifier:
1823/// override
1824/// final
Richard Smith4e24f0f2013-01-02 12:01:23 +00001825VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001826 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001827 return VirtSpecifiers::VS_None;
1828
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001829 if (Tok.is(tok::identifier)) {
1830 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001831
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001832 // Initialize the contextual keywords.
1833 if (!Ident_final) {
1834 Ident_final = &PP.getIdentifierTable().get("final");
David Majnemer7121bdb2013-10-18 00:33:31 +00001835 if (getLangOpts().MicrosoftExt)
1836 Ident_sealed = &PP.getIdentifierTable().get("sealed");
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001837 Ident_override = &PP.getIdentifierTable().get("override");
1838 }
1839
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001840 if (II == Ident_override)
1841 return VirtSpecifiers::VS_Override;
1842
David Majnemer7121bdb2013-10-18 00:33:31 +00001843 if (II == Ident_sealed)
1844 return VirtSpecifiers::VS_Sealed;
1845
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001846 if (II == Ident_final)
1847 return VirtSpecifiers::VS_Final;
1848 }
1849
1850 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001851}
1852
Richard Smith4e24f0f2013-01-02 12:01:23 +00001853/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001854///
1855/// virt-specifier-seq:
1856/// virt-specifier
1857/// virt-specifier-seq virt-specifier
Richard Smith4e24f0f2013-01-02 12:01:23 +00001858void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalle402e722012-09-25 07:32:39 +00001859 bool IsInterface) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001860 while (true) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001861 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001862 if (Specifier == VirtSpecifiers::VS_None)
1863 return;
1864
1865 // C++ [class.mem]p8:
1866 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001867 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001868 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001869 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1870 << PrevSpec
1871 << FixItHint::CreateRemoval(Tok.getLocation());
1872
David Majnemer7121bdb2013-10-18 00:33:31 +00001873 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
1874 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalle402e722012-09-25 07:32:39 +00001875 Diag(Tok.getLocation(), diag::err_override_control_interface)
1876 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemer7121bdb2013-10-18 00:33:31 +00001877 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
1878 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
John McCalle402e722012-09-25 07:32:39 +00001879 } else {
David Majnemer7121bdb2013-10-18 00:33:31 +00001880 Diag(Tok.getLocation(),
1881 getLangOpts().CPlusPlus11
1882 ? diag::warn_cxx98_compat_override_control_keyword
1883 : diag::ext_override_control_keyword)
1884 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalle402e722012-09-25 07:32:39 +00001885 }
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001886 ConsumeToken();
1887 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001888}
1889
Richard Smith4e24f0f2013-01-02 12:01:23 +00001890/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001891/// contextual 'final' keyword.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001892bool Parser::isCXX11FinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001893 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001894 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001895
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001896 if (!Tok.is(tok::identifier))
1897 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001898
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001899 // Initialize the contextual keywords.
1900 if (!Ident_final) {
1901 Ident_final = &PP.getIdentifierTable().get("final");
David Majnemer7121bdb2013-10-18 00:33:31 +00001902 if (getLangOpts().MicrosoftExt)
1903 Ident_sealed = &PP.getIdentifierTable().get("sealed");
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001904 Ident_override = &PP.getIdentifierTable().get("override");
1905 }
David Majnemer7121bdb2013-10-18 00:33:31 +00001906
1907 return Tok.getIdentifierInfo() == Ident_final ||
1908 Tok.getIdentifierInfo() == Ident_sealed;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001909}
1910
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001911/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1912///
1913/// member-declaration:
1914/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1915/// function-definition ';'[opt]
1916/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1917/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001918/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001919/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001920/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001921///
1922/// member-declarator-list:
1923/// member-declarator
1924/// member-declarator-list ',' member-declarator
1925///
1926/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001927/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001928/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001929/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001930/// identifier[opt] ':' constant-expression
1931///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001932/// virt-specifier-seq:
1933/// virt-specifier
1934/// virt-specifier-seq virt-specifier
1935///
1936/// virt-specifier:
1937/// override
1938/// final
David Majnemer7121bdb2013-10-18 00:33:31 +00001939/// [MS] sealed
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001940///
Sebastian Redle2b68332009-04-12 17:16:29 +00001941/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001942/// '= 0'
1943///
1944/// constant-initializer:
1945/// '=' constant-expression
1946///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001947void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001948 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001949 const ParsedTemplateInfo &TemplateInfo,
1950 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001951 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001952 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001953 Diag(Tok, diag::err_at_defs_cxx);
1954 else
1955 Diag(Tok, diag::err_at_in_class);
1956
1957 ConsumeToken();
1958 SkipUntil(tok::r_brace);
1959 return;
1960 }
1961
John McCall60fa3cf2009-12-11 02:10:03 +00001962 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001963 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001964 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001965 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1966 if (TryAnnotateCXXScopeToken())
1967 MalformedTypeSpec = true;
1968
1969 bool isAccessDecl;
1970 if (Tok.isNot(tok::annot_cxxscope))
1971 isAccessDecl = false;
1972 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001973 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1974 else
1975 isAccessDecl = NextToken().is(tok::kw_operator);
1976
1977 if (isAccessDecl) {
1978 // Collect the scope specifier token we annotated earlier.
1979 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001980 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1981 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001982
1983 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001984 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001985 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001986 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1987 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001988 SkipUntil(tok::semi);
1989 return;
1990 }
1991
1992 // TODO: recover from mistakenly-qualified operator declarations.
1993 if (ExpectAndConsume(tok::semi,
1994 diag::err_expected_semi_after,
1995 "access declaration",
1996 tok::semi))
1997 return;
1998
Douglas Gregor23c94db2010-07-02 17:43:08 +00001999 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanella8d030c72013-07-22 10:54:09 +00002000 /* HasUsingKeyword */ false,
2001 SourceLocation(),
John McCall60fa3cf2009-12-11 02:10:03 +00002002 SS, Name,
2003 /* AttrList */ 0,
Enea Zaffanella8d030c72013-07-22 10:54:09 +00002004 /* HasTypenameKeyword */ false,
John McCall60fa3cf2009-12-11 02:10:03 +00002005 SourceLocation());
2006 return;
2007 }
2008 }
2009
Anders Carlsson511d7ab2009-03-11 16:27:10 +00002010 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00002011 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00002012 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00002013 SourceLocation DeclEnd;
2014 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00002015 return;
2016 }
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Chris Lattner682bf922009-03-29 16:50:03 +00002018 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002019 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00002020 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00002021 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00002022 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002023 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00002024 return;
2025 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00002026
Chris Lattnerbc8d5642008-12-18 01:12:00 +00002027 // Handle: member-declaration ::= '__extension__' member-declaration
2028 if (Tok.is(tok::kw___extension__)) {
2029 // __extension__ silences extension warnings in the subexpression.
2030 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2031 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002032 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2033 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00002034 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002035
Chris Lattner4ed5d912010-02-02 01:23:29 +00002036 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
2037 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002038 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002039
John McCall0b7e6782011-03-24 11:26:52 +00002040 ParsedAttributesWithRange attrs(AttrFactory);
Michael Han52b501c2012-11-28 23:17:40 +00002041 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00002042 // Optional C++11 attribute-specifier
2043 MaybeParseCXX11Attributes(attrs);
Michael Han52b501c2012-11-28 23:17:40 +00002044 // We need to keep these attributes for future diagnostic
2045 // before they are taken over by declaration specifier.
2046 FnAttrs.addAll(attrs.getList());
2047 FnAttrs.Range = attrs.Range;
2048
John McCall7f040a92010-12-24 02:08:15 +00002049 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002050
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002051 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00002052 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002053
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002054 // Eat 'using'.
2055 SourceLocation UsingLoc = ConsumeToken();
2056
2057 if (Tok.is(tok::kw_namespace)) {
2058 Diag(UsingLoc, diag::err_using_namespace_in_class);
2059 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00002060 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002061 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00002062 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00002063 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2064 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002065 }
2066 return;
2067 }
2068
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002069 // Hold late-parsed attributes so we can attach a Decl to them later.
2070 LateParsedAttrList CommonLateParsedAttrs;
2071
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002072 // decl-specifier-seq:
2073 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00002074 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00002075 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00002076 if (MalformedTypeSpec)
2077 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002078 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2079 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002080
Benjamin Kramer5354e772012-08-23 23:38:35 +00002081 MultiTemplateParamsArg TemplateParams(
John McCalldd4a3b02009-09-16 22:47:08 +00002082 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2083 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2084
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002085 if (Tok.is(tok::semi)) {
2086 ConsumeToken();
Michael Han52b501c2012-11-28 23:17:40 +00002087
2088 if (DS.isFriendSpecified())
2089 ProhibitAttributes(FnAttrs);
2090
John McCalld226f652010-08-21 09:40:31 +00002091 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00002092 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00002093 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00002094 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002095 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002096
John McCall54abf7d2009-11-04 02:18:39 +00002097 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00002098 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002099
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002100 // Hold late-parsed attributes so we can attach a Decl to them later.
2101 LateParsedAttrList LateParsedAttrs;
2102
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002103 SourceLocation EqualLoc;
2104 bool HasInitializer = false;
2105 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002106 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002107 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2108 ColonProtectionRAIIObject X(*this);
2109
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002110 // Parse the first declarator.
2111 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00002112 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00002113 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002114 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00002115 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002116 if (Tok.is(tok::semi))
2117 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002118 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002119 }
2120
Richard Smith4e24f0f2013-01-02 12:01:23 +00002121 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber48673472011-01-28 06:07:34 +00002122
John Thompson1b2fc0f2009-11-25 22:58:06 +00002123 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002124 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00002125
Francois Pichet6a247472011-05-11 02:14:46 +00002126 // MSVC permits pure specifier on inline functions declared at class scope.
2127 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00002128 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00002129 DeclaratorInfo.isFunctionDeclarator() &&
2130 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002131 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00002132 Init = ParseInitializer();
2133 if (Init.isInvalid())
2134 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002135 else
2136 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00002137 }
2138
Douglas Gregor45fa5602011-11-07 20:56:01 +00002139 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002140 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00002141 //
2142 // In C++11, a non-function declarator followed by an open brace is a
2143 // braced-init-list for an in-class member initialization, not an
2144 // erroneous function definition.
Richard Smith80ad52f2013-01-02 11:42:31 +00002145 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002146 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002147 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00002148 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002149 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002150 } else if (Tok.is(tok::equal)) {
2151 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00002152 if (KW.is(tok::kw_default))
2153 DefinitionKind = FDK_Defaulted;
2154 else if (KW.is(tok::kw_delete))
2155 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00002156 }
2157 }
2158
Michael Han52b501c2012-11-28 23:17:40 +00002159 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2160 // to a friend declaration, that declaration shall be a definition.
2161 if (DeclaratorInfo.isFunctionDeclarator() &&
2162 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2163 // Diagnose attributes that appear before decl specifier:
2164 // [[]] friend int foo();
2165 ProhibitAttributes(FnAttrs);
2166 }
2167
Douglas Gregor45fa5602011-11-07 20:56:01 +00002168 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002169 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002170 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002171 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00002172 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Michael Han52b501c2012-11-28 23:17:40 +00002173
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002174 // Consume the optional ';'
2175 if (Tok.is(tok::semi))
2176 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002177 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002178 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002179
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002180 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002181 Diag(DeclaratorInfo.getIdentifierLoc(),
2182 diag::err_function_declared_typedef);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002183
Richard Smith6f9a4452012-11-15 22:54:20 +00002184 // Recover by treating the 'typedef' as spurious.
2185 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002186 }
2187
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002188 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002189 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00002190 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002191
David Majnemerfcbe2082013-08-01 04:22:55 +00002192 if (FunDecl) {
2193 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2194 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2195 }
2196 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2197 LateParsedAttrs[i]->addDecl(FunDecl);
2198 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002199 }
2200 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00002201
2202 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002203 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00002204 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002205
Chris Lattner682bf922009-03-29 16:50:03 +00002206 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002207 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002208 }
2209
2210 // member-declarator-list:
2211 // member-declarator
2212 // member-declarator-list ',' member-declarator
2213
Chris Lattner5f9e2722011-07-23 10:55:15 +00002214 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00002215 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00002216 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002217
2218 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002219 // member-declarator:
2220 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002221 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002222 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002223 if (Tok.is(tok::colon)) {
2224 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002225 BitfieldSize = ParseConstantExpression();
2226 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002227 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002228 }
Mike Stump1eb44332009-09-09 15:08:12 +00002229
Chris Lattnere6563252010-06-13 05:34:18 +00002230 // If a simple-asm-expr is present, parse it.
2231 if (Tok.is(tok::kw_asm)) {
2232 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002233 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002234 if (AsmLabel.isInvalid())
2235 SkipUntil(tok::comma, true, true);
2236
2237 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2238 DeclaratorInfo.SetRangeEnd(Loc);
2239 }
2240
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002241 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002242 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002243
Richard Smith7a614d82011-06-11 17:19:42 +00002244 // FIXME: When g++ adds support for this, we'll need to check whether it
2245 // goes before or after the GNU attributes and __asm__.
Richard Smith4e24f0f2013-01-02 12:01:23 +00002246 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith7a614d82011-06-11 17:19:42 +00002247
Richard Smithca523302012-06-10 03:12:00 +00002248 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002249 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002250 if (BitfieldSize.get()) {
2251 Diag(Tok, diag::err_bitfield_member_init);
2252 SkipUntil(tok::comma, true, true);
2253 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002254 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002255 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2256 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smithca523302012-06-10 03:12:00 +00002257 != DeclSpec::SCS_typedef)
2258 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002259 }
2260 }
2261
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002262 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002263 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002264 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002265
Rafael Espindolafc35cbc2013-01-08 20:44:06 +00002266 NamedDecl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002267 if (DS.isFriendSpecified()) {
Michael Han52b501c2012-11-28 23:17:40 +00002268 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2269 // to a friend declaration, that declaration shall be a definition.
2270 //
2271 // Diagnose attributes appear after friend member function declarator:
2272 // foo [[]] ();
2273 SmallVector<SourceRange, 4> Ranges;
2274 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2275 if (!Ranges.empty()) {
Craig Topper09d19ef2013-07-04 03:08:24 +00002276 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
Michael Han52b501c2012-11-28 23:17:40 +00002277 E = Ranges.end(); I != E; ++I) {
2278 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2279 << *I;
2280 }
2281 }
2282
John McCallbbbcdd92009-09-11 21:02:39 +00002283 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002284 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002285 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002286 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002287 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002288 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002289 TemplateParams,
John McCall67d1a672009-08-06 02:15:43 +00002290 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002291 VS, HasInClassInit);
Larisse Voufoef4579c2013-08-06 01:03:05 +00002292
2293 if (VarTemplateDecl *VT =
2294 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : 0)
2295 // Re-direct this decl to refer to the templated decl so that we can
2296 // initialize it.
2297 ThisDecl = VT->getTemplatedDecl();
2298
David Majnemerfcbe2082013-08-01 04:22:55 +00002299 if (ThisDecl && AccessAttrs)
Richard Smith4a97b8e2013-08-29 00:47:48 +00002300 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002301 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002302
Douglas Gregor147545d2011-10-10 14:49:18 +00002303 // Handle the initializer.
David Blaikie1d87fba2013-01-30 01:22:18 +00002304 if (HasInClassInit != ICIS_NoInit &&
2305 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2306 DeclSpec::SCS_static) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002307 // The initializer was deferred; parse it and cache the tokens.
David Majnemerfcbe2082013-08-01 04:22:55 +00002308 Diag(Tok, getLangOpts().CPlusPlus11
2309 ? diag::warn_cxx98_compat_nonstatic_member_init
2310 : diag::ext_nonstatic_member_init);
Richard Smith7fe62082011-10-15 05:09:34 +00002311
Richard Smith7a614d82011-06-11 17:19:42 +00002312 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002313 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2314 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002315 //
2316 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002317 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002318 Diag(Tok, diag::err_incomplete_array_member_init);
2319 SkipUntil(tok::comma, true, true);
David Majnemerfcbe2082013-08-01 04:22:55 +00002320
2321 // Avoid later warnings about a class member of incomplete type.
David Blaikie3164c142012-02-14 09:00:46 +00002322 if (ThisDecl)
David Blaikie3164c142012-02-14 09:00:46 +00002323 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002324 } else
2325 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002326 } else if (HasInitializer) {
2327 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002328 if (!Init.isUsable())
David Majnemerfcbe2082013-08-01 04:22:55 +00002329 Init = ParseCXXMemberInitializer(
2330 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2331
Douglas Gregor147545d2011-10-10 14:49:18 +00002332 if (Init.isInvalid())
2333 SkipUntil(tok::comma, true, true);
2334 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002335 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smitha2c36462013-04-26 16:15:35 +00002336 DS.containsPlaceholderType());
David Majnemerfcbe2082013-08-01 04:22:55 +00002337 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor147545d2011-10-10 14:49:18 +00002338 // No initializer.
Richard Smitha2c36462013-04-26 16:15:35 +00002339 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemerfcbe2082013-08-01 04:22:55 +00002340
Douglas Gregor147545d2011-10-10 14:49:18 +00002341 if (ThisDecl) {
David Majnemerfcbe2082013-08-01 04:22:55 +00002342 if (!ThisDecl->isInvalidDecl()) {
2343 // Set the Decl for any late parsed attributes
2344 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2345 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2346
2347 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2348 LateParsedAttrs[i]->addDecl(ThisDecl);
2349 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002350 Actions.FinalizeDeclaration(ThisDecl);
2351 DeclsInGroup.push_back(ThisDecl);
David Majnemerfcbe2082013-08-01 04:22:55 +00002352
2353 if (DeclaratorInfo.isFunctionDeclarator() &&
2354 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2355 DeclSpec::SCS_typedef)
2356 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002357 }
David Majnemerfcbe2082013-08-01 04:22:55 +00002358 LateParsedAttrs.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002359
2360 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002361
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002362 // If we don't have a comma, it is either the end of the list (a ';')
2363 // or an error, bail out.
2364 if (Tok.isNot(tok::comma))
2365 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002366
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002367 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002368 SourceLocation CommaLoc = ConsumeToken();
2369
2370 if (Tok.isAtStartOfLine() &&
2371 !MightBeDeclarator(Declarator::MemberContext)) {
2372 // This comma was followed by a line-break and something which can't be
2373 // the start of a declarator. The comma was probably a typo for a
2374 // semicolon.
2375 Diag(CommaLoc, diag::err_expected_semi_declaration)
2376 << FixItHint::CreateReplacement(CommaLoc, ";");
2377 ExpectSemi = false;
2378 break;
2379 }
Mike Stump1eb44332009-09-09 15:08:12 +00002380
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002381 // Parse the next declarator.
2382 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002383 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002384 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002385 Init = true;
2386 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002387 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002388
Bill Wendlingad017fa2012-12-20 19:22:21 +00002389 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002390 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002391
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002392 if (Tok.isNot(tok::colon))
2393 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002394 }
2395
Richard Smith1c94c162012-01-09 22:31:44 +00002396 if (ExpectSemi &&
2397 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002398 // Skip to end of block or statement.
2399 SkipUntil(tok::r_brace, true, true);
2400 // If we stopped at a ';', eat it.
2401 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002402 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002403 }
2404
Rafael Espindola4549d7f2013-07-09 12:05:01 +00002405 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002406}
2407
Richard Smith7a614d82011-06-11 17:19:42 +00002408/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2409/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2410/// function definition. The location of the '=', if any, will be placed in
2411/// EqualLoc.
2412///
2413/// pure-specifier:
2414/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002415///
Richard Smith7a614d82011-06-11 17:19:42 +00002416/// brace-or-equal-initializer:
2417/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002418/// braced-init-list
2419///
Richard Smith7a614d82011-06-11 17:19:42 +00002420/// initializer-clause:
2421/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002422/// braced-init-list
2423///
Richard Smith7a614d82011-06-11 17:19:42 +00002424/// defaulted/deleted function-definition:
2425/// '=' 'default'
2426/// '=' 'delete'
2427///
2428/// Prior to C++0x, the assignment-expression in an initializer-clause must
2429/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002430ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002431 SourceLocation &EqualLoc) {
2432 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2433 && "Data member initializer not starting with '=' or '{'");
2434
Douglas Gregor552e2992012-02-21 02:22:07 +00002435 EnterExpressionEvaluationContext Context(Actions,
2436 Sema::PotentiallyEvaluated,
2437 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002438 if (Tok.is(tok::equal)) {
2439 EqualLoc = ConsumeToken();
2440 if (Tok.is(tok::kw_delete)) {
2441 // In principle, an initializer of '= delete p;' is legal, but it will
2442 // never type-check. It's better to diagnose it as an ill-formed expression
2443 // than as an ill-formed deleted non-function member.
2444 // An initializer of '= delete p, foo' will never be parsed, because
2445 // a top-level comma always ends the initializer expression.
2446 const Token &Next = NextToken();
2447 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2448 Next.is(tok::eof)) {
2449 if (IsFunction)
2450 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2451 << 1 /* delete */;
2452 else
2453 Diag(ConsumeToken(), diag::err_deleted_non_function);
2454 return ExprResult();
2455 }
2456 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002457 if (IsFunction)
2458 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2459 << 0 /* default */;
2460 else
2461 Diag(ConsumeToken(), diag::err_default_special_members);
2462 return ExprResult();
2463 }
2464
Sebastian Redl33deb352012-02-22 10:50:08 +00002465 }
2466 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002467}
2468
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002469/// ParseCXXMemberSpecification - Parse the class definition.
2470///
2471/// member-specification:
2472/// member-declaration member-specification[opt]
2473/// access-specifier ':' member-specification[opt]
2474///
Joao Matos17d35c32012-08-31 22:18:20 +00002475void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han07fc1ba2013-01-07 16:57:11 +00002476 SourceLocation AttrFixitLoc,
Richard Smith05321402013-02-19 23:47:15 +00002477 ParsedAttributesWithRange &Attrs,
Joao Matos17d35c32012-08-31 22:18:20 +00002478 unsigned TagType, Decl *TagDecl) {
2479 assert((TagType == DeclSpec::TST_struct ||
2480 TagType == DeclSpec::TST_interface ||
2481 TagType == DeclSpec::TST_union ||
2482 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2483
John McCallf312b1e2010-08-26 23:41:50 +00002484 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2485 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002486
Douglas Gregor26997fd2010-01-16 20:52:59 +00002487 // Determine whether this is a non-nested class. Note that local
2488 // classes are *not* considered to be nested classes.
2489 bool NonNestedClass = true;
2490 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002491 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002492 if (S->isClassScope()) {
2493 // We're inside a class scope, so this is a nested class.
2494 NonNestedClass = false;
John McCalle402e722012-09-25 07:32:39 +00002495
2496 // The Microsoft extension __interface does not permit nested classes.
2497 if (getCurrentClass().IsInterface) {
2498 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2499 << /*ErrorType=*/6
2500 << (isa<NamedDecl>(TagDecl)
2501 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2502 : "<anonymous>");
2503 }
Douglas Gregor26997fd2010-01-16 20:52:59 +00002504 break;
2505 }
2506
2507 if ((S->getFlags() & Scope::FnScope)) {
2508 // If we're in a function or function template declared in the
2509 // body of a class, then this is a local class rather than a
2510 // nested class.
2511 const Scope *Parent = S->getParent();
2512 if (Parent->isTemplateParamScope())
2513 Parent = Parent->getParent();
2514 if (Parent->isClassScope())
2515 break;
2516 }
2517 }
2518 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002519
2520 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002521 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002522
Douglas Gregor6569d682009-05-27 23:11:45 +00002523 // Note that we are parsing a new (potentially-nested) class definition.
John McCalle402e722012-09-25 07:32:39 +00002524 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2525 TagType == DeclSpec::TST_interface);
Douglas Gregor6569d682009-05-27 23:11:45 +00002526
Douglas Gregorddc29e12009-02-06 22:42:48 +00002527 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002528 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002529
Anders Carlssonb184a182011-03-25 14:46:08 +00002530 SourceLocation FinalLoc;
David Majnemer7121bdb2013-10-18 00:33:31 +00002531 bool IsFinalSpelledSealed = false;
Anders Carlssonb184a182011-03-25 14:46:08 +00002532
2533 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002534 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemer7121bdb2013-10-18 00:33:31 +00002535 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2536 assert((Specifier == VirtSpecifiers::VS_Final ||
2537 Specifier == VirtSpecifiers::VS_Sealed) &&
2538 "not a class definition");
Richard Smith8b11b5e2011-10-15 04:21:46 +00002539 FinalLoc = ConsumeToken();
David Majnemer7121bdb2013-10-18 00:33:31 +00002540 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonb184a182011-03-25 14:46:08 +00002541
David Majnemer7121bdb2013-10-18 00:33:31 +00002542 if (TagType == DeclSpec::TST_interface)
John McCalle402e722012-09-25 07:32:39 +00002543 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemer7121bdb2013-10-18 00:33:31 +00002544 << VirtSpecifiers::getSpecifierName(Specifier);
2545 else if (Specifier == VirtSpecifiers::VS_Final)
2546 Diag(FinalLoc, getLangOpts().CPlusPlus11
2547 ? diag::warn_cxx98_compat_override_control_keyword
2548 : diag::ext_override_control_keyword)
2549 << VirtSpecifiers::getSpecifierName(Specifier);
2550 else if (Specifier == VirtSpecifiers::VS_Sealed)
2551 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Michael Han2e397132012-11-26 22:54:45 +00002552
Michael Han07fc1ba2013-01-07 16:57:11 +00002553 // Parse any C++11 attributes after 'final' keyword.
2554 // These attributes are not allowed to appear here,
2555 // and the only possible place for them to appertain
2556 // to the class would be between class-key and class-name.
Richard Smith05321402013-02-19 23:47:15 +00002557 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonb184a182011-03-25 14:46:08 +00002558 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002559
John McCallbd0dfa52009-12-19 21:48:58 +00002560 if (Tok.is(tok::colon)) {
2561 ParseBaseClause(TagDecl);
2562
2563 if (!Tok.is(tok::l_brace)) {
2564 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002565
2566 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002567 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002568 return;
2569 }
2570 }
2571
2572 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002573 BalancedDelimiterTracker T(*this, tok::l_brace);
2574 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002575
John McCall42a4f662010-05-28 08:11:17 +00002576 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002577 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemer7121bdb2013-10-18 00:33:31 +00002578 IsFinalSpelledSealed,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002579 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002580
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002581 // C++ 11p3: Members of a class defined with the keyword class are private
2582 // by default. Members of a class defined with the keywords struct or union
2583 // are public by default.
2584 AccessSpecifier CurAS;
2585 if (TagType == DeclSpec::TST_class)
2586 CurAS = AS_private;
2587 else
2588 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002589 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002590
Douglas Gregor07976d22010-06-21 22:31:09 +00002591 if (TagDecl) {
2592 // While we still have something to read, read the member-declarations.
2593 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2594 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002595
David Blaikie4e4d0842012-03-11 07:00:24 +00002596 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002597 Tok.is(tok::kw___if_not_exists))) {
2598 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2599 continue;
2600 }
2601
Douglas Gregor07976d22010-06-21 22:31:09 +00002602 // Check for extraneous top-level semicolon.
2603 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002604 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002605 continue;
2606 }
2607
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002608 if (Tok.is(tok::annot_pragma_vis)) {
2609 HandlePragmaVisibility();
2610 continue;
2611 }
2612
2613 if (Tok.is(tok::annot_pragma_pack)) {
2614 HandlePragmaPack();
2615 continue;
2616 }
2617
Argyrios Kyrtzidisf4deaef2012-10-12 17:39:59 +00002618 if (Tok.is(tok::annot_pragma_align)) {
2619 HandlePragmaAlign();
2620 continue;
2621 }
2622
Alexey Bataevc6400582013-03-22 06:34:35 +00002623 if (Tok.is(tok::annot_pragma_openmp)) {
2624 ParseOpenMPDeclarativeDirective();
2625 continue;
2626 }
2627
Douglas Gregor07976d22010-06-21 22:31:09 +00002628 AccessSpecifier AS = getAccessSpecifierIfPresent();
2629 if (AS != AS_none) {
2630 // Current token is a C++ access specifier.
2631 CurAS = AS;
2632 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002633 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002634 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002635 AccessAttrs.clear();
2636 MaybeParseGNUAttributes(AccessAttrs);
2637
David Blaikie13f8daf2011-10-13 06:08:43 +00002638 SourceLocation EndLoc;
2639 if (Tok.is(tok::colon)) {
2640 EndLoc = Tok.getLocation();
2641 ConsumeToken();
2642 } else if (Tok.is(tok::semi)) {
2643 EndLoc = Tok.getLocation();
2644 ConsumeToken();
2645 Diag(EndLoc, diag::err_expected_colon)
2646 << FixItHint::CreateReplacement(EndLoc, ":");
2647 } else {
2648 EndLoc = ASLoc.getLocWithOffset(TokLength);
2649 Diag(EndLoc, diag::err_expected_colon)
2650 << FixItHint::CreateInsertion(EndLoc, ":");
2651 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002652
John McCalle402e722012-09-25 07:32:39 +00002653 // The Microsoft extension __interface does not permit non-public
2654 // access specifiers.
2655 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2656 Diag(ASLoc, diag::err_access_specifier_interface)
2657 << (CurAS == AS_protected);
2658 }
2659
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002660 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2661 AccessAttrs.getList())) {
2662 // found another attribute than only annotations
2663 AccessAttrs.clear();
2664 }
2665
Douglas Gregor07976d22010-06-21 22:31:09 +00002666 continue;
2667 }
2668
2669 // FIXME: Make sure we don't have a template here.
2670
2671 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002672 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002673 }
2674
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002675 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002676 } else {
2677 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002678 }
Mike Stump1eb44332009-09-09 15:08:12 +00002679
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002680 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002681 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002682 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002683
John McCall42a4f662010-05-28 08:11:17 +00002684 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002685 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002686 T.getOpenLocation(),
2687 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002688 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002689
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002690 // C++11 [class.mem]p2:
2691 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002692 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002693 // brace-or-equal-initializers for non-static data members (including such
2694 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002695 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002696 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002697 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002698 // declarations and the lexed inline method definitions, along with any
2699 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002700 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002701 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002702 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002703
2704 // We've finished with all pending member declarations.
2705 Actions.ActOnFinishCXXMemberDecls();
2706
Richard Smith7a614d82011-06-11 17:19:42 +00002707 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002708 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002709 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002710 }
2711
John McCall42a4f662010-05-28 08:11:17 +00002712 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002713 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2714 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002715
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002716 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002717 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002718 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002719}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002720
2721/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2722/// which explicitly initializes the members or base classes of a
2723/// class (C++ [class.base.init]). For example, the three initializers
2724/// after the ':' in the Derived constructor below:
2725///
2726/// @code
2727/// class Base { };
2728/// class Derived : Base {
2729/// int x;
2730/// float f;
2731/// public:
2732/// Derived(float f) : Base(), x(17), f(f) { }
2733/// };
2734/// @endcode
2735///
Mike Stump1eb44332009-09-09 15:08:12 +00002736/// [C++] ctor-initializer:
2737/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002738///
Mike Stump1eb44332009-09-09 15:08:12 +00002739/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002740/// mem-initializer ...[opt]
2741/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002742void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002743 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2744
John Wiegley28bbe4b2011-04-28 01:08:34 +00002745 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2746 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002747 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002748
Chris Lattner5f9e2722011-07-23 10:55:15 +00002749 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002750 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002751
Douglas Gregor7ad83902008-11-05 04:29:56 +00002752 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002753 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko572cf582013-06-23 22:58:02 +00002754 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2755 MemInitializers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002756 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002757 } else {
2758 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2759 if (!MemInit.isInvalid())
2760 MemInitializers.push_back(MemInit.get());
2761 else
2762 AnyErrors = true;
2763 }
2764
Douglas Gregor7ad83902008-11-05 04:29:56 +00002765 if (Tok.is(tok::comma))
2766 ConsumeToken();
2767 else if (Tok.is(tok::l_brace))
2768 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002769 // If the next token looks like a base or member initializer, assume that
2770 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002771 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2772 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2773 Diag(Loc, diag::err_ctor_init_missing_comma)
2774 << FixItHint::CreateInsertion(Loc, ", ");
2775 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002776 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002777 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002778 SkipUntil(tok::l_brace, true, true);
2779 break;
2780 }
2781 } while (true);
2782
David Blaikie93c86172013-01-17 05:26:25 +00002783 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002784 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002785}
2786
2787/// ParseMemInitializer - Parse a C++ member initializer, which is
2788/// part of a constructor initializer that explicitly initializes one
2789/// member or base class (C++ [class.base.init]). See
2790/// ParseConstructorInitializer for an example.
2791///
2792/// [C++] mem-initializer:
2793/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002794/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002795///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002796/// [C++] mem-initializer-id:
2797/// '::'[opt] nested-name-specifier[opt] class-name
2798/// identifier
John McCalld226f652010-08-21 09:40:31 +00002799Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002800 // parse '::'[opt] nested-name-specifier[opt]
2801 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002802 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002803 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002804 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002805 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002806 if (TemplateId->Kind == TNK_Type_template ||
2807 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002808 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002809 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002810 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002811 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002812 }
David Blaikief2116622012-01-24 06:03:59 +00002813 // Uses of decltype will already have been converted to annot_decltype by
2814 // ParseOptionalCXXScopeSpecifier at this point.
2815 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2816 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002817 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002818 return true;
2819 }
Mike Stump1eb44332009-09-09 15:08:12 +00002820
David Blaikief2116622012-01-24 06:03:59 +00002821 IdentifierInfo *II = 0;
2822 DeclSpec DS(AttrFactory);
2823 SourceLocation IdLoc = Tok.getLocation();
2824 if (Tok.is(tok::annot_decltype)) {
2825 // Get the decltype expression, if there is one.
2826 ParseDecltypeSpecifier(DS);
2827 } else {
2828 if (Tok.is(tok::identifier))
2829 // Get the identifier. This may be a member name or a class name,
2830 // but we'll let the semantic analysis determine which it is.
2831 II = Tok.getIdentifierInfo();
2832 ConsumeToken();
2833 }
2834
Douglas Gregor7ad83902008-11-05 04:29:56 +00002835
2836 // Parse the '('.
Richard Smith80ad52f2013-01-02 11:42:31 +00002837 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002838 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2839
Sebastian Redl6df65482011-09-24 17:48:25 +00002840 ExprResult InitList = ParseBraceInitializer();
2841 if (InitList.isInvalid())
2842 return true;
2843
2844 SourceLocation EllipsisLoc;
2845 if (Tok.is(tok::ellipsis))
2846 EllipsisLoc = ConsumeToken();
2847
2848 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002849 TemplateTypeTy, DS, IdLoc,
2850 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002851 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002852 BalancedDelimiterTracker T(*this, tok::l_paren);
2853 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002854
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002855 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002856 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002857 CommaLocsTy CommaLocs;
2858 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2859 SkipUntil(tok::r_paren);
2860 return true;
2861 }
2862
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002863 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002864
2865 SourceLocation EllipsisLoc;
2866 if (Tok.is(tok::ellipsis))
2867 EllipsisLoc = ConsumeToken();
2868
2869 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002870 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenkoa36bbac2013-05-09 23:51:52 +00002871 T.getOpenLocation(), ArgExprs,
2872 T.getCloseLocation(), EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002873 }
2874
Richard Smith80ad52f2013-01-02 11:42:31 +00002875 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002876 : diag::err_expected_lparen);
2877 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002878}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002879
Sebastian Redl7acafd02011-03-05 14:45:16 +00002880/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002881///
Douglas Gregora4745612008-12-01 18:00:20 +00002882/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002883/// dynamic-exception-specification
2884/// noexcept-specification
2885///
2886/// noexcept-specification:
2887/// 'noexcept'
2888/// 'noexcept' '(' constant-expression ')'
2889ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002890Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002891 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002892 SmallVectorImpl<ParsedType> &DynamicExceptions,
2893 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002894 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002895 ExceptionSpecificationType Result = EST_None;
2896
2897 // See if there's a dynamic specification.
2898 if (Tok.is(tok::kw_throw)) {
2899 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2900 DynamicExceptions,
2901 DynamicExceptionRanges);
2902 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2903 "Produced different number of exception types and ranges.");
2904 }
2905
2906 // If there's no noexcept specification, we're done.
2907 if (Tok.isNot(tok::kw_noexcept))
2908 return Result;
2909
Richard Smith841804b2011-10-17 23:06:20 +00002910 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2911
Sebastian Redl7acafd02011-03-05 14:45:16 +00002912 // If we already had a dynamic specification, parse the noexcept for,
2913 // recovery, but emit a diagnostic and don't store the results.
2914 SourceRange NoexceptRange;
2915 ExceptionSpecificationType NoexceptType = EST_None;
2916
2917 SourceLocation KeywordLoc = ConsumeToken();
2918 if (Tok.is(tok::l_paren)) {
2919 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002920 BalancedDelimiterTracker T(*this, tok::l_paren);
2921 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002922 NoexceptType = EST_ComputedNoexcept;
2923 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002924 // The argument must be contextually convertible to bool. We use
2925 // ActOnBooleanCondition for this purpose.
2926 if (!NoexceptExpr.isInvalid())
2927 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2928 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002929 T.consumeClose();
2930 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002931 } else {
2932 // There is no argument.
2933 NoexceptType = EST_BasicNoexcept;
2934 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2935 }
2936
2937 if (Result == EST_None) {
2938 SpecificationRange = NoexceptRange;
2939 Result = NoexceptType;
2940
2941 // If there's a dynamic specification after a noexcept specification,
2942 // parse that and ignore the results.
2943 if (Tok.is(tok::kw_throw)) {
2944 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2945 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2946 DynamicExceptionRanges);
2947 }
2948 } else {
2949 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2950 }
2951
2952 return Result;
2953}
2954
Richard Smith79f4bb72013-06-13 02:02:51 +00002955static void diagnoseDynamicExceptionSpecification(
2956 Parser &P, const SourceRange &Range, bool IsNoexcept) {
2957 if (P.getLangOpts().CPlusPlus11) {
2958 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
2959 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
2960 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
2961 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
2962 }
2963}
2964
Sebastian Redl7acafd02011-03-05 14:45:16 +00002965/// ParseDynamicExceptionSpecification - Parse a C++
2966/// dynamic-exception-specification (C++ [except.spec]).
2967///
2968/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002969/// 'throw' '(' type-id-list [opt] ')'
2970/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002971///
Douglas Gregora4745612008-12-01 18:00:20 +00002972/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002973/// type-id ... [opt]
2974/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002975///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002976ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2977 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002978 SmallVectorImpl<ParsedType> &Exceptions,
2979 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002980 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002981
Sebastian Redl7acafd02011-03-05 14:45:16 +00002982 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002983 BalancedDelimiterTracker T(*this, tok::l_paren);
2984 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002985 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2986 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002987 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002988 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002989
Douglas Gregora4745612008-12-01 18:00:20 +00002990 // Parse throw(...), a Microsoft extension that means "this function
2991 // can throw anything".
2992 if (Tok.is(tok::ellipsis)) {
2993 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002994 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002995 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002996 T.consumeClose();
2997 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith79f4bb72013-06-13 02:02:51 +00002998 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redl60618fa2011-03-12 11:50:43 +00002999 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00003000 }
3001
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003002 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00003003 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003004 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00003005 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00003006
Douglas Gregora04426c2010-12-20 23:57:46 +00003007 if (Tok.is(tok::ellipsis)) {
3008 // C++0x [temp.variadic]p5:
3009 // - In a dynamic-exception-specification (15.4); the pattern is a
3010 // type-id.
3011 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00003012 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00003013 if (!Res.isInvalid())
3014 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3015 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00003016
Sebastian Redlef65f062009-05-29 18:02:33 +00003017 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00003018 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00003019 Ranges.push_back(Range);
3020 }
Douglas Gregora04426c2010-12-20 23:57:46 +00003021
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003022 if (Tok.is(tok::comma))
3023 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00003024 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003025 break;
3026 }
3027
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003028 T.consumeClose();
3029 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith79f4bb72013-06-13 02:02:51 +00003030 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3031 Exceptions.empty());
Sebastian Redl60618fa2011-03-12 11:50:43 +00003032 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003033}
Douglas Gregor6569d682009-05-27 23:11:45 +00003034
Douglas Gregordab60ad2010-10-01 18:44:50 +00003035/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3036/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00003037TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00003038 assert(Tok.is(tok::arrow) && "expected arrow");
3039
3040 ConsumeToken();
3041
Richard Smith7796eb52012-03-12 08:56:40 +00003042 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00003043}
3044
Douglas Gregor6569d682009-05-27 23:11:45 +00003045/// \brief We have just started parsing the definition of a new class,
3046/// so push that class onto our stack of classes that is currently
3047/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00003048Sema::ParsingClassState
John McCalle402e722012-09-25 07:32:39 +00003049Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3050 bool IsInterface) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00003051 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00003052 "Nested class without outer class");
John McCalle402e722012-09-25 07:32:39 +00003053 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCalleee1d542011-02-14 07:13:47 +00003054 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00003055}
3056
3057/// \brief Deallocate the given parsed class and all of its nested
3058/// classes.
3059void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00003060 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3061 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00003062 delete Class;
3063}
3064
3065/// \brief Pop the top class of the stack of classes that are
3066/// currently being parsed.
3067///
3068/// This routine should be called when we have finished parsing the
3069/// definition of a class, but have not yet popped the Scope
3070/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00003071void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00003072 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00003073
John McCalleee1d542011-02-14 07:13:47 +00003074 Actions.PopParsingClass(state);
3075
Douglas Gregor6569d682009-05-27 23:11:45 +00003076 ParsingClass *Victim = ClassStack.top();
3077 ClassStack.pop();
3078 if (Victim->TopLevelClass) {
3079 // Deallocate all of the nested classes of this class,
3080 // recursively: we don't need to keep any of this information.
3081 DeallocateParsedClasses(Victim);
3082 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003083 }
Douglas Gregor6569d682009-05-27 23:11:45 +00003084 assert(!ClassStack.empty() && "Missing top-level class?");
3085
Douglas Gregord54eb442010-10-12 16:25:54 +00003086 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00003087 // The victim is a nested class, but we will not need to perform
3088 // any processing after the definition of this class since it has
3089 // no members whose handling was delayed. Therefore, we can just
3090 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00003091 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00003092 return;
3093 }
3094
3095 // This nested class has some members that will need to be processed
3096 // after the top-level class is completely defined. Therefore, add
3097 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003098 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00003099 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00003100 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00003101}
Sean Huntbbd37c62009-11-21 08:43:09 +00003102
Richard Smithc56298d2012-04-10 03:25:07 +00003103/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3104///
3105/// \return the parsed identifier on success, and 0 if the next token is not an
3106/// attribute-token.
3107///
3108/// C++11 [dcl.attr.grammar]p3:
3109/// If a keyword or an alternative token that satisfies the syntactic
3110/// requirements of an identifier is contained in an attribute-token,
3111/// it is considered an identifier.
3112IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3113 switch (Tok.getKind()) {
3114 default:
3115 // Identifiers and keywords have identifier info attached.
3116 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3117 Loc = ConsumeToken();
3118 return II;
3119 }
3120 return 0;
3121
3122 case tok::ampamp: // 'and'
3123 case tok::pipe: // 'bitor'
3124 case tok::pipepipe: // 'or'
3125 case tok::caret: // 'xor'
3126 case tok::tilde: // 'compl'
3127 case tok::amp: // 'bitand'
3128 case tok::ampequal: // 'and_eq'
3129 case tok::pipeequal: // 'or_eq'
3130 case tok::caretequal: // 'xor_eq'
3131 case tok::exclaim: // 'not'
3132 case tok::exclaimequal: // 'not_eq'
3133 // Alternative tokens do not have identifier info, but their spelling
3134 // starts with an alphabetical character.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003135 SmallString<8> SpellingBuf;
Richard Smithc56298d2012-04-10 03:25:07 +00003136 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rose3f6f51e2013-02-08 22:30:41 +00003137 if (isLetter(Spelling[0])) {
Richard Smithc56298d2012-04-10 03:25:07 +00003138 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00003139 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00003140 }
3141 return 0;
3142 }
3143}
3144
Michael Han6880f492012-10-03 01:56:22 +00003145static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3146 IdentifierInfo *ScopeName) {
3147 switch (AttributeList::getKind(AttrName, ScopeName,
3148 AttributeList::AS_CXX11)) {
3149 case AttributeList::AT_CarriesDependency:
3150 case AttributeList::AT_FallThrough:
Richard Smithcd8ab512013-01-17 01:30:42 +00003151 case AttributeList::AT_CXX11NoReturn: {
Michael Han6880f492012-10-03 01:56:22 +00003152 return true;
3153 }
3154
3155 default:
3156 return false;
3157 }
3158}
3159
Richard Smithc56298d2012-04-10 03:25:07 +00003160/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003161/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00003162///
Richard Smith6ee326a2012-04-10 01:32:12 +00003163/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00003164/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003165/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00003166///
Richard Smith6ee326a2012-04-10 01:32:12 +00003167/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00003168/// attribute[opt]
3169/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00003170/// attribute '...'
3171/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00003172///
Richard Smith6ee326a2012-04-10 01:32:12 +00003173/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00003174/// attribute-token attribute-argument-clause[opt]
3175///
Richard Smith6ee326a2012-04-10 01:32:12 +00003176/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003177/// identifier
3178/// attribute-scoped-token
3179///
Richard Smith6ee326a2012-04-10 01:32:12 +00003180/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003181/// attribute-namespace '::' identifier
3182///
Richard Smith6ee326a2012-04-10 01:32:12 +00003183/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00003184/// identifier
3185///
Richard Smith6ee326a2012-04-10 01:32:12 +00003186/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00003187/// '(' balanced-token-seq ')'
3188///
Richard Smith6ee326a2012-04-10 01:32:12 +00003189/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00003190/// balanced-token
3191/// balanced-token-seq balanced-token
3192///
Richard Smith6ee326a2012-04-10 01:32:12 +00003193/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003194/// '(' balanced-token-seq ')'
3195/// '[' balanced-token-seq ']'
3196/// '{' balanced-token-seq '}'
3197/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00003198void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003199 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003200 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00003201 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003202 ParseAlignmentSpecifier(attrs, endLoc);
3203 return;
3204 }
3205
Sean Huntbbd37c62009-11-21 08:43:09 +00003206 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00003207 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00003208
Richard Smith41be6732011-10-14 20:48:27 +00003209 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3210
Sean Huntbbd37c62009-11-21 08:43:09 +00003211 ConsumeBracket();
3212 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003213
Richard Smithcd8ab512013-01-17 01:30:42 +00003214 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3215
Richard Smithc56298d2012-04-10 03:25:07 +00003216 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003217 // attribute not present
3218 if (Tok.is(tok::comma)) {
3219 ConsumeToken();
3220 continue;
3221 }
3222
Richard Smithc56298d2012-04-10 03:25:07 +00003223 SourceLocation ScopeLoc, AttrLoc;
3224 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3225
3226 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3227 if (!AttrName)
3228 // Break out to the "expected ']'" diagnostic.
3229 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003230
Sean Huntbbd37c62009-11-21 08:43:09 +00003231 // scoped attribute
3232 if (Tok.is(tok::coloncolon)) {
3233 ConsumeToken();
3234
Richard Smithc56298d2012-04-10 03:25:07 +00003235 ScopeName = AttrName;
3236 ScopeLoc = AttrLoc;
3237
3238 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3239 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003240 Diag(Tok.getLocation(), diag::err_expected_ident);
3241 SkipUntil(tok::r_square, tok::comma, true, true);
3242 continue;
3243 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003244 }
3245
Michael Han6880f492012-10-03 01:56:22 +00003246 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Sean Huntbbd37c62009-11-21 08:43:09 +00003247 bool AttrParsed = false;
Sean Huntbbd37c62009-11-21 08:43:09 +00003248
Richard Smithcd8ab512013-01-17 01:30:42 +00003249 if (StandardAttr &&
3250 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3251 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3252 << AttrName << SourceRange(SeenAttrs[AttrName]);
3253
Michael Han6880f492012-10-03 01:56:22 +00003254 // Parse attribute arguments
3255 if (Tok.is(tok::l_paren)) {
3256 if (ScopeName && ScopeName->getName() == "gnu") {
3257 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3258 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3259 AttrParsed = true;
3260 } else {
3261 if (StandardAttr)
3262 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3263 << AttrName->getName();
3264
3265 // FIXME: handle other formats of c++11 attribute arguments
3266 ConsumeParen();
3267 SkipUntil(tok::r_paren, false);
3268 }
3269 }
3270
3271 if (!AttrParsed)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00003272 attrs.addNew(AttrName,
3273 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3274 AttrLoc),
Aaron Ballman624421f2013-08-31 01:11:41 +00003275 ScopeName, ScopeLoc, 0, 0, AttributeList::AS_CXX11);
Richard Smith6ee326a2012-04-10 01:32:12 +00003276
Richard Smithc56298d2012-04-10 03:25:07 +00003277 if (Tok.is(tok::ellipsis)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003278 ConsumeToken();
Michael Han6880f492012-10-03 01:56:22 +00003279
3280 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3281 << AttrName->getName();
Richard Smithc56298d2012-04-10 03:25:07 +00003282 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003283 }
3284
3285 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3286 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003287 if (endLoc)
3288 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00003289 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3290 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003291}
Sean Huntbbd37c62009-11-21 08:43:09 +00003292
Sean Hunt2edf0a22012-06-23 05:07:58 +00003293/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003294///
3295/// attribute-specifier-seq:
3296/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00003297void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003298 SourceLocation *endLoc) {
Richard Smith672edb02013-02-22 09:15:49 +00003299 assert(getLangOpts().CPlusPlus11);
3300
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003301 SourceLocation StartLoc = Tok.getLocation(), Loc;
3302 if (!endLoc)
3303 endLoc = &Loc;
3304
Douglas Gregor8828ee72011-10-07 20:35:25 +00003305 do {
Richard Smithc56298d2012-04-10 03:25:07 +00003306 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003307 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003308
3309 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003310}
3311
Richard Smith5eed7e02013-10-15 01:34:54 +00003312void Parser::DiagnoseAndSkipCXX11Attributes() {
3313 if (!isCXX11AttributeSpecifier())
3314 return;
3315
3316 // Start and end location of an attribute or an attribute list.
3317 SourceLocation StartLoc = Tok.getLocation();
3318 SourceLocation EndLoc;
3319
3320 do {
3321 if (Tok.is(tok::l_square)) {
3322 BalancedDelimiterTracker T(*this, tok::l_square);
3323 T.consumeOpen();
3324 T.skipToEnd();
3325 EndLoc = T.getCloseLocation();
3326 } else {
3327 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3328 ConsumeToken();
3329 BalancedDelimiterTracker T(*this, tok::l_paren);
3330 if (!T.consumeOpen())
3331 T.skipToEnd();
3332 EndLoc = T.getCloseLocation();
3333 }
3334 } while (isCXX11AttributeSpecifier());
3335
3336 if (EndLoc.isValid()) {
3337 SourceRange Range(StartLoc, EndLoc);
3338 Diag(StartLoc, diag::err_attributes_not_allowed)
3339 << Range;
3340 }
3341}
3342
Francois Pichet334d47e2010-10-11 12:59:39 +00003343/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3344///
3345/// [MS] ms-attribute:
3346/// '[' token-seq ']'
3347///
3348/// [MS] ms-attribute-seq:
3349/// ms-attribute[opt]
3350/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003351void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3352 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003353 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3354
3355 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003356 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003357 ConsumeBracket();
3358 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003359 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003360 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3361 }
3362}
Francois Pichet563a6452011-05-25 10:19:49 +00003363
3364void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3365 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003366 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003367 if (ParseMicrosoftIfExistsCondition(Result))
3368 return;
3369
Douglas Gregor3896fc52011-10-24 22:31:10 +00003370 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3371 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003372 Diag(Tok, diag::err_expected_lbrace);
3373 return;
3374 }
Francois Pichet563a6452011-05-25 10:19:49 +00003375
Douglas Gregor3896fc52011-10-24 22:31:10 +00003376 switch (Result.Behavior) {
3377 case IEB_Parse:
3378 // Parse the declarations below.
3379 break;
3380
3381 case IEB_Dependent:
3382 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3383 << Result.IsIfExists;
3384 // Fall through to skip.
3385
3386 case IEB_Skip:
3387 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003388 return;
3389 }
3390
Douglas Gregor3896fc52011-10-24 22:31:10 +00003391 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003392 // __if_exists, __if_not_exists can nest.
3393 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3394 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3395 continue;
3396 }
3397
3398 // Check for extraneous top-level semicolon.
3399 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003400 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003401 continue;
3402 }
3403
3404 AccessSpecifier AS = getAccessSpecifierIfPresent();
3405 if (AS != AS_none) {
3406 // Current token is a C++ access specifier.
3407 CurAS = AS;
3408 SourceLocation ASLoc = Tok.getLocation();
3409 ConsumeToken();
3410 if (Tok.is(tok::colon))
3411 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3412 else
3413 Diag(Tok, diag::err_expected_colon);
3414 ConsumeToken();
3415 continue;
3416 }
3417
3418 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003419 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003420 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003421
3422 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003423}