blob: b252ed5865c0e57fe01c4e1a915e32473122f33f [file] [log] [blame]
Chris Lattnera5235172007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera5235172007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor423984d2008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Erik Verbruggen888d52a2014-01-15 09:15:43 +000016#include "clang/AST/ASTContext.h"
Chandler Carruth757fcd62014-03-04 10:05:20 +000017#include "clang/AST/DeclTemplate.h"
Aaron Ballmanb8e20392014-03-31 17:32:39 +000018#include "clang/Basic/Attributes.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000019#include "clang/Basic/CharInfo.h"
Aaron Ballmanb8e20392014-03-31 17:32:39 +000020#include "clang/Basic/TargetInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Basic/OperatorKinds.h"
Chris Lattner60f36222009-01-29 05:15:15 +000022#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000023#include "clang/Sema/DeclSpec.h"
John McCall8b0666c2010-08-20 18:27:03 +000024#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000025#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "clang/Sema/Scope.h"
John McCalldb632ac2012-09-25 07:32:39 +000027#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000028#include "llvm/ADT/SmallString.h"
Chris Lattnera5235172007-08-25 06:57:03 +000029using namespace clang;
30
31/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redl67667942010-08-27 23:12:46 +000032/// may either be a top level namespace or a block-level namespace alias. If
33/// there was an inline keyword, it has already been parsed.
Chris Lattnera5235172007-08-25 06:57:03 +000034///
35/// namespace-definition: [C++ 7.3: basic.namespace]
36/// named-namespace-definition
37/// unnamed-namespace-definition
38///
39/// unnamed-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000040/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000041///
42/// named-namespace-definition:
43/// original-namespace-definition
44/// extension-namespace-definition
45///
46/// original-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000047/// 'inline'[opt] 'namespace' identifier attributes[opt]
48/// '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000049///
50/// extension-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000051/// 'inline'[opt] 'namespace' original-namespace-name
52/// '{' namespace-body '}'
Mike Stump11289f42009-09-09 15:08:12 +000053///
Chris Lattnera5235172007-08-25 06:57:03 +000054/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
55/// 'namespace' identifier '=' qualified-namespace-specifier ';'
56///
John McCall48871652010-08-21 09:40:31 +000057Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redl67667942010-08-27 23:12:46 +000058 SourceLocation &DeclEnd,
59 SourceLocation InlineLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000060 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000061 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian4bf82622011-08-22 17:59:19 +000062 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000063
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000064 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000065 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000066 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +000067 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000068 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000069
Chris Lattnera5235172007-08-25 06:57:03 +000070 SourceLocation IdentLoc;
Craig Topper161e4db2014-05-21 06:02:52 +000071 IdentifierInfo *Ident = nullptr;
Richard Trieu61384cb2011-05-26 20:11:09 +000072 std::vector<SourceLocation> ExtraIdentLoc;
73 std::vector<IdentifierInfo*> ExtraIdent;
74 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000075
76 Token attrTok;
Mike Stump11289f42009-09-09 15:08:12 +000077
Chris Lattner76c72282007-10-09 17:33:22 +000078 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000079 Ident = Tok.getIdentifierInfo();
80 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieu61384cb2011-05-26 20:11:09 +000081 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
82 ExtraNamespaceLoc.push_back(ConsumeToken());
83 ExtraIdent.push_back(Tok.getIdentifierInfo());
84 ExtraIdentLoc.push_back(ConsumeToken());
85 }
Chris Lattnera5235172007-08-25 06:57:03 +000086 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattnera5235172007-08-25 06:57:03 +000088 // Read label attributes, if present.
John McCall084e83d2011-03-24 11:26:52 +000089 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000090 if (Tok.is(tok::kw___attribute)) {
91 attrTok = Tok;
John McCall53fa7142010-12-24 02:08:15 +000092 ParseGNUAttributes(attrs);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000093 }
Mike Stump11289f42009-09-09 15:08:12 +000094
Douglas Gregor6b6bba42009-06-17 19:49:00 +000095 if (Tok.is(tok::equal)) {
Craig Topper161e4db2014-05-21 06:02:52 +000096 if (!Ident) {
Alp Tokerec543272013-12-24 09:48:30 +000097 Diag(Tok, diag::err_expected) << tok::identifier;
Nico Weber729f1e22012-10-27 23:44:27 +000098 // Skip to end of the definition and eat the ';'.
99 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000100 return nullptr;
Nico Weber729f1e22012-10-27 23:44:27 +0000101 }
John McCall53fa7142010-12-24 02:08:15 +0000102 if (!attrs.empty())
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000103 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redl67667942010-08-27 23:12:46 +0000104 if (InlineLoc.isValid())
105 Diag(InlineLoc, diag::err_inline_namespace_alias)
106 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000107 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000108 }
Mike Stump11289f42009-09-09 15:08:12 +0000109
Richard Trieu61384cb2011-05-26 20:11:09 +0000110
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000111 BalancedDelimiterTracker T(*this, tok::l_brace);
112 if (T.consumeOpen()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000113 if (!ExtraIdent.empty()) {
114 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
115 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
116 }
Alp Tokerec543272013-12-24 09:48:30 +0000117
118 if (Ident)
119 Diag(Tok, diag::err_expected) << tok::l_brace;
120 else
121 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
122
Craig Topper161e4db2014-05-21 06:02:52 +0000123 return nullptr;
Chris Lattnera5235172007-08-25 06:57:03 +0000124 }
Mike Stump11289f42009-09-09 15:08:12 +0000125
Douglas Gregor0be31a22010-07-02 17:43:08 +0000126 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
127 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
128 getCurScope()->getFnParent()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000129 if (!ExtraIdent.empty()) {
130 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
131 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
132 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000133 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000134 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +0000135 return nullptr;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000136 }
137
Richard Trieu61384cb2011-05-26 20:11:09 +0000138 if (!ExtraIdent.empty()) {
139 TentativeParsingAction TPA(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000140 SkipUntil(tok::r_brace, StopBeforeMatch);
Richard Trieu61384cb2011-05-26 20:11:09 +0000141 Token rBraceToken = Tok;
142 TPA.Revert();
143
144 if (!rBraceToken.is(tok::r_brace)) {
145 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
146 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
147 } else {
Benjamin Kramerf546f412011-05-26 21:32:30 +0000148 std::string NamespaceFix;
Richard Trieu61384cb2011-05-26 20:11:09 +0000149 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
150 E = ExtraIdent.end(); I != E; ++I) {
151 NamespaceFix += " { namespace ";
152 NamespaceFix += (*I)->getName();
153 }
Benjamin Kramerf546f412011-05-26 21:32:30 +0000154
Richard Trieu61384cb2011-05-26 20:11:09 +0000155 std::string RBraces;
Benjamin Kramerf546f412011-05-26 21:32:30 +0000156 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieu61384cb2011-05-26 20:11:09 +0000157 RBraces += "} ";
Benjamin Kramerf546f412011-05-26 21:32:30 +0000158
Richard Trieu61384cb2011-05-26 20:11:09 +0000159 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
160 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
161 ExtraIdentLoc.back()),
162 NamespaceFix)
163 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
164 }
165 }
166
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000167 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith5d164bc2011-10-15 05:09:34 +0000168 if (InlineLoc.isValid())
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000169 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000170 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000171
Chris Lattner4de55aa2009-03-29 14:02:43 +0000172 // Enter a scope for the namespace.
173 ParseScope NamespaceScope(this, Scope::DeclScope);
174
John McCall48871652010-08-21 09:40:31 +0000175 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000176 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000177 IdentLoc, Ident, T.getOpenLocation(),
178 attrs.getList());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000179
John McCallfaf5fb42010-08-26 23:41:50 +0000180 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
181 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000182
Richard Trieu61384cb2011-05-26 20:11:09 +0000183 // Parse the contents of the namespace. This includes parsing recovery on
184 // any improperly nested namespaces.
185 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000186 InlineLoc, attrs, T);
Mike Stump11289f42009-09-09 15:08:12 +0000187
Chris Lattner4de55aa2009-03-29 14:02:43 +0000188 // Leave the namespace scope.
189 NamespaceScope.Exit();
190
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000191 DeclEnd = T.getCloseLocation();
192 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000193
194 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000195}
Chris Lattner38376f12008-01-12 07:05:38 +0000196
Richard Trieu61384cb2011-05-26 20:11:09 +0000197/// ParseInnerNamespace - Parse the contents of a namespace.
198void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
199 std::vector<IdentifierInfo*>& Ident,
200 std::vector<SourceLocation>& NamespaceLoc,
201 unsigned int index, SourceLocation& InlineLoc,
Richard Trieu61384cb2011-05-26 20:11:09 +0000202 ParsedAttributes& attrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000203 BalancedDelimiterTracker &Tracker) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000204 if (index == Ident.size()) {
Richard Smith34f30512013-11-23 04:06:09 +0000205 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000206 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000207 MaybeParseCXX11Attributes(attrs);
Richard Trieu61384cb2011-05-26 20:11:09 +0000208 MaybeParseMicrosoftAttributes(attrs);
209 ParseExternalDeclaration(attrs);
210 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000211
212 // The caller is what called check -- we are simply calling
213 // the close for it.
214 Tracker.consumeClose();
Richard Trieu61384cb2011-05-26 20:11:09 +0000215
216 return;
217 }
218
219 // Parse improperly nested namespaces.
220 ParseScope NamespaceScope(this, Scope::DeclScope);
221 Decl *NamespcDecl =
222 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
223 NamespaceLoc[index], IdentLoc[index],
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000224 Ident[index], Tracker.getOpenLocation(),
225 attrs.getList());
Richard Trieu61384cb2011-05-26 20:11:09 +0000226
227 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000228 attrs, Tracker);
Richard Trieu61384cb2011-05-26 20:11:09 +0000229
230 NamespaceScope.Exit();
231
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000232 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieu61384cb2011-05-26 20:11:09 +0000233}
234
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000235/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
236/// alias definition.
237///
John McCall48871652010-08-21 09:40:31 +0000238Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall084e83d2011-03-24 11:26:52 +0000239 SourceLocation AliasLoc,
240 IdentifierInfo *Alias,
241 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000242 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000243
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000244 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000245
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000246 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000247 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000248 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000249 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000250 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000251
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000252 CXXScopeSpec SS;
253 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000254 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000255
256 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
257 Diag(Tok, diag::err_expected_namespace_name);
258 // Skip to end of the definition and eat the ';'.
259 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000260 return nullptr;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000261 }
262
263 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000264 IdentifierInfo *Ident = Tok.getIdentifierInfo();
265 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000266
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000267 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000268 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000269 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
270 SkipUntil(tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000271
Douglas Gregor0be31a22010-07-02 17:43:08 +0000272 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000273 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000274}
275
Chris Lattner38376f12008-01-12 07:05:38 +0000276/// ParseLinkage - We know that the current token is a string_literal
277/// and just before that, that extern was seen.
278///
279/// linkage-specification: [C++ 7.5p2: dcl.link]
280/// 'extern' string-literal '{' declaration-seq[opt] '}'
281/// 'extern' string-literal declaration
282///
Chris Lattner8ea64422010-11-09 20:15:55 +0000283Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Richard Smith4ee696d2014-02-17 23:25:27 +0000284 assert(isTokenStringLiteral() && "Not a string literal!");
285 ExprResult Lang = ParseStringLiteralExpression(false);
Chris Lattner38376f12008-01-12 07:05:38 +0000286
Douglas Gregor07665a62009-01-05 19:45:36 +0000287 ParseScope LinkageScope(this, Scope::DeclScope);
Richard Smith4ee696d2014-02-17 23:25:27 +0000288 Decl *LinkageSpec =
289 Lang.isInvalid()
Craig Topper161e4db2014-05-21 06:02:52 +0000290 ? nullptr
Richard Smith4ee696d2014-02-17 23:25:27 +0000291 : Actions.ActOnStartLinkageSpecification(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000292 getCurScope(), DS.getSourceRange().getBegin(), Lang.get(),
Richard Smith4ee696d2014-02-17 23:25:27 +0000293 Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
Douglas Gregor07665a62009-01-05 19:45:36 +0000294
John McCall084e83d2011-03-24 11:26:52 +0000295 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000296 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000297 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000298
Douglas Gregor07665a62009-01-05 19:45:36 +0000299 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara4d423992011-05-01 16:25:54 +0000300 // Reset the source range in DS, as the leading "extern"
301 // does not really belong to the inner declaration ...
302 DS.SetRangeStart(SourceLocation());
303 DS.SetRangeEnd(SourceLocation());
304 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000305 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000306 ParseExternalDeclaration(attrs, &DS);
Richard Smith4ee696d2014-02-17 23:25:27 +0000307 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
308 getCurScope(), LinkageSpec, SourceLocation())
Craig Topper161e4db2014-05-21 06:02:52 +0000309 : nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000310 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000311
Douglas Gregorb65a9132010-02-07 08:38:28 +0000312 DS.abort();
313
John McCall53fa7142010-12-24 02:08:15 +0000314 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000315
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000316 BalancedDelimiterTracker T(*this, tok::l_brace);
317 T.consumeOpen();
Richard Smith77944862014-03-02 05:58:18 +0000318
319 unsigned NestedModules = 0;
320 while (true) {
321 switch (Tok.getKind()) {
322 case tok::annot_module_begin:
323 ++NestedModules;
324 ParseTopLevelDecl();
325 continue;
326
327 case tok::annot_module_end:
328 if (!NestedModules)
329 break;
330 --NestedModules;
331 ParseTopLevelDecl();
332 continue;
333
334 case tok::annot_module_include:
335 ParseTopLevelDecl();
336 continue;
337
338 case tok::eof:
339 break;
340
341 case tok::r_brace:
342 if (!NestedModules)
343 break;
344 // Fall through.
345 default:
346 ParsedAttributesWithRange attrs(AttrFactory);
347 MaybeParseCXX11Attributes(attrs);
348 MaybeParseMicrosoftAttributes(attrs);
349 ParseExternalDeclaration(attrs);
350 continue;
351 }
352
353 break;
Chris Lattner38376f12008-01-12 07:05:38 +0000354 }
355
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000356 T.consumeClose();
Richard Smith4ee696d2014-02-17 23:25:27 +0000357 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
358 getCurScope(), LinkageSpec, T.getCloseLocation())
Craig Topper161e4db2014-05-21 06:02:52 +0000359 : nullptr;
Chris Lattner38376f12008-01-12 07:05:38 +0000360}
Douglas Gregor556877c2008-04-13 21:30:24 +0000361
Douglas Gregord7c4d982008-12-30 03:27:21 +0000362/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
363/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000364Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000365 const ParsedTemplateInfo &TemplateInfo,
366 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000367 ParsedAttributesWithRange &attrs,
368 Decl **OwnedType) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000369 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000370 ObjCDeclContextSwitch ObjCDC(*this);
371
Douglas Gregord7c4d982008-12-30 03:27:21 +0000372 // Eat 'using'.
373 SourceLocation UsingLoc = ConsumeToken();
374
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000375 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000376 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000377 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000378 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000379 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000380
John McCall9b72f892010-11-10 02:40:36 +0000381 // 'using namespace' means this is a using-directive.
382 if (Tok.is(tok::kw_namespace)) {
383 // Template parameters are always an error here.
384 if (TemplateInfo.Kind) {
385 SourceRange R = TemplateInfo.getSourceRange();
386 Diag(UsingLoc, diag::err_templated_using_directive)
387 << R << FixItHint::CreateRemoval(R);
388 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000389
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000390 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000391 }
392
Richard Smithdda56e42011-04-15 14:24:37 +0000393 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +0000394
395 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000396 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000397
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000398 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000399 AS_none, OwnedType);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000400}
401
402/// ParseUsingDirective - Parse C++ using-directive, assumes
403/// that current token is 'namespace' and 'using' was already parsed.
404///
405/// using-directive: [C++ 7.3.p4: namespace.udir]
406/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
407/// namespace-name ;
408/// [GNU] using-directive:
409/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
410/// namespace-name attributes[opt] ;
411///
John McCall48871652010-08-21 09:40:31 +0000412Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000413 SourceLocation UsingLoc,
414 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000415 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000416 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
417
418 // Eat 'namespace'.
419 SourceLocation NamespcLoc = ConsumeToken();
420
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000421 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000422 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000423 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000424 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000425 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000426
Douglas Gregord7c4d982008-12-30 03:27:21 +0000427 CXXScopeSpec SS;
428 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000429 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000430
Craig Topper161e4db2014-05-21 06:02:52 +0000431 IdentifierInfo *NamespcName = nullptr;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000432 SourceLocation IdentLoc = SourceLocation();
433
434 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000435 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000436 Diag(Tok, diag::err_expected_namespace_name);
437 // If there was invalid namespace name, skip to end of decl, and eat ';'.
438 SkipUntil(tok::semi);
439 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Craig Topper161e4db2014-05-21 06:02:52 +0000440 return nullptr;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000441 }
Mike Stump11289f42009-09-09 15:08:12 +0000442
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000443 // Parse identifier.
444 NamespcName = Tok.getIdentifierInfo();
445 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000446
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000447 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000448 bool GNUAttr = false;
449 if (Tok.is(tok::kw___attribute)) {
450 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000451 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000452 }
Mike Stump11289f42009-09-09 15:08:12 +0000453
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000454 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000455 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000456 if (ExpectAndConsume(tok::semi,
457 GNUAttr ? diag::err_expected_semi_after_attribute_list
458 : diag::err_expected_semi_after_namespace_name))
459 SkipUntil(tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000460
Douglas Gregor0be31a22010-07-02 17:43:08 +0000461 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000462 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000463}
464
Richard Smithdda56e42011-04-15 14:24:37 +0000465/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
466/// Assumes that 'using' was already seen.
Douglas Gregord7c4d982008-12-30 03:27:21 +0000467///
468/// using-declaration: [C++ 7.3.p3: namespace.udecl]
469/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000470/// unqualified-id
471/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000472///
Richard Smith810ad3e2013-01-29 10:02:16 +0000473/// alias-declaration: C++11 [dcl.dcl]p1
474/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smithdda56e42011-04-15 14:24:37 +0000475///
John McCall48871652010-08-21 09:40:31 +0000476Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000477 const ParsedTemplateInfo &TemplateInfo,
478 SourceLocation UsingLoc,
479 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000480 AccessSpecifier AS,
481 Decl **OwnedType) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000482 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000483 SourceLocation TypenameLoc;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000484 bool HasTypenameKeyword = false;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000485
Richard Smithc2c8bb82013-10-15 01:34:54 +0000486 // Check for misplaced attributes before the identifier in an
487 // alias-declaration.
488 ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
489 MaybeParseCXX11Attributes(MisplacedAttrs);
Douglas Gregorfec52632009-06-20 00:51:54 +0000490
491 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000492 // FIXME: This is wrong; we should parse this as a typename-specifier.
Alp Toker97650562014-01-10 11:19:30 +0000493 if (TryConsumeToken(tok::kw_typename, TypenameLoc))
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000494 HasTypenameKeyword = true;
Douglas Gregorfec52632009-06-20 00:51:54 +0000495
496 // Parse nested-name-specifier.
Craig Topper161e4db2014-05-21 06:02:52 +0000497 IdentifierInfo *LastII = nullptr;
Richard Smith7447af42013-03-26 01:15:19 +0000498 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
Craig Topper161e4db2014-05-21 06:02:52 +0000499 /*MayBePseudoDtor=*/nullptr,
500 /*IsTypename=*/false,
Richard Smith7447af42013-03-26 01:15:19 +0000501 /*LastII=*/&LastII);
Douglas Gregorfec52632009-06-20 00:51:54 +0000502
Douglas Gregorfec52632009-06-20 00:51:54 +0000503 // Check nested-name specifier.
504 if (SS.isInvalid()) {
505 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000506 return nullptr;
Douglas Gregorfec52632009-06-20 00:51:54 +0000507 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000508
Richard Smith7447af42013-03-26 01:15:19 +0000509 SourceLocation TemplateKWLoc;
510 UnqualifiedId Name;
511
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000512 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000513 // destructor names and allow the action module to diagnose any semantic
514 // errors.
Richard Smith7447af42013-03-26 01:15:19 +0000515 //
516 // C++11 [class.qual]p2:
517 // [...] in a using-declaration that is a member-declaration, if the name
518 // specified after the nested-name-specifier is the same as the identifier
519 // or the simple-template-id's template-name in the last component of the
520 // nested-name-specifier, the name is [...] considered to name the
521 // constructor.
522 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
523 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
524 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
525 !SS.getScopeRep()->getAsNamespace() &&
526 !SS.getScopeRep()->getAsNamespaceAlias()) {
527 SourceLocation IdLoc = ConsumeToken();
528 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
529 Name.setConstructorName(Type, IdLoc, IdLoc);
530 } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
531 /*AllowDestructorName=*/ true,
532 /*AllowConstructorName=*/ true, ParsedType(),
533 TemplateKWLoc, Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000534 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000535 return nullptr;
Douglas Gregorfec52632009-06-20 00:51:54 +0000536 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000537
Richard Smithc2c8bb82013-10-15 01:34:54 +0000538 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smith37a45dd2013-10-24 01:21:09 +0000539 MaybeParseGNUAttributes(Attrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000540 MaybeParseCXX11Attributes(Attrs);
Richard Smithdda56e42011-04-15 14:24:37 +0000541
542 // Maybe this is an alias-declaration.
Richard Smithdda56e42011-04-15 14:24:37 +0000543 TypeResult TypeAlias;
Richard Smithc2c8bb82013-10-15 01:34:54 +0000544 bool IsAliasDecl = Tok.is(tok::equal);
Richard Smithdda56e42011-04-15 14:24:37 +0000545 if (IsAliasDecl) {
Richard Smithc2c8bb82013-10-15 01:34:54 +0000546 // If we had any misplaced attributes from earlier, this is where they
547 // should have been written.
548 if (MisplacedAttrs.Range.isValid()) {
549 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
550 << FixItHint::CreateInsertionFromRange(
551 Tok.getLocation(),
552 CharSourceRange::getTokenRange(MisplacedAttrs.Range))
553 << FixItHint::CreateRemoval(MisplacedAttrs.Range);
554 Attrs.takeAllFrom(MisplacedAttrs);
555 }
556
Richard Smithdda56e42011-04-15 14:24:37 +0000557 ConsumeToken();
558
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000559 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000560 diag::warn_cxx98_compat_alias_declaration :
561 diag::ext_alias_declaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000562
Richard Smith3f1b5d02011-05-05 21:57:07 +0000563 // Type alias templates cannot be specialized.
564 int SpecKind = -1;
Richard Smith14034022011-05-05 22:36:10 +0000565 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
566 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000567 SpecKind = 0;
568 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
569 SpecKind = 1;
570 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
571 SpecKind = 2;
572 if (SpecKind != -1) {
573 SourceRange Range;
574 if (SpecKind == 0)
575 Range = SourceRange(Name.TemplateId->LAngleLoc,
576 Name.TemplateId->RAngleLoc);
577 else
578 Range = TemplateInfo.getSourceRange();
579 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
580 << SpecKind << Range;
581 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000582 return nullptr;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000583 }
584
Richard Smithdda56e42011-04-15 14:24:37 +0000585 // Name must be an identifier.
586 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
587 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
588 // No removal fixit: can't recover from this.
589 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000590 return nullptr;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000591 } else if (HasTypenameKeyword)
Richard Smithdda56e42011-04-15 14:24:37 +0000592 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
593 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
594 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
595 else if (SS.isNotEmpty())
596 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
597 << FixItHint::CreateRemoval(SS.getRange());
598
Craig Topper161e4db2014-05-21 06:02:52 +0000599 TypeAlias = ParseTypeName(nullptr, TemplateInfo.Kind ?
Richard Smith3f1b5d02011-05-05 21:57:07 +0000600 Declarator::AliasTemplateContext :
Richard Smith54ecd982013-02-20 19:22:51 +0000601 Declarator::AliasDeclContext, AS, OwnedType,
602 &Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000603 } else {
604 // C++11 attributes are not allowed on a using-declaration, but GNU ones
605 // are.
Richard Smithc2c8bb82013-10-15 01:34:54 +0000606 ProhibitAttributes(MisplacedAttrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000607 ProhibitAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000608
Richard Smithdda56e42011-04-15 14:24:37 +0000609 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith54ecd982013-02-20 19:22:51 +0000610 MaybeParseGNUAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000611 }
Mike Stump11289f42009-09-09 15:08:12 +0000612
Douglas Gregorfec52632009-06-20 00:51:54 +0000613 // Eat ';'.
614 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000615 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
616 !Attrs.empty() ? "attributes list"
617 : IsAliasDecl ? "alias declaration"
618 : "using declaration"))
619 SkipUntil(tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000620
John McCall9b72f892010-11-10 02:40:36 +0000621 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith810ad3e2013-01-29 10:02:16 +0000622 // In C++11, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000623 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000624 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-11-10 02:40:36 +0000625 SourceRange R = TemplateInfo.getSourceRange();
626 Diag(UsingLoc, diag::err_templated_using_declaration)
627 << R << FixItHint::CreateRemoval(R);
628
629 // Unfortunately, we have to bail out instead of recovering by
630 // ignoring the parameters, just in case the nested name specifier
631 // depends on the parameters.
Craig Topper161e4db2014-05-21 06:02:52 +0000632 return nullptr;
John McCall9b72f892010-11-10 02:40:36 +0000633 }
634
Douglas Gregor882a61a2011-09-26 14:30:28 +0000635 // "typename" keyword is allowed for identifiers only,
636 // because it may be a type definition.
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000637 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor882a61a2011-09-26 14:30:28 +0000638 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
639 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000640 // Proceed parsing, but reset the HasTypenameKeyword flag.
641 HasTypenameKeyword = false;
Douglas Gregor882a61a2011-09-26 14:30:28 +0000642 }
643
Richard Smith3f1b5d02011-05-05 21:57:07 +0000644 if (IsAliasDecl) {
645 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000646 MultiTemplateParamsArg TemplateParamsArg(
Craig Topper161e4db2014-05-21 06:02:52 +0000647 TemplateParams ? TemplateParams->data() : nullptr,
Richard Smith3f1b5d02011-05-05 21:57:07 +0000648 TemplateParams ? TemplateParams->size() : 0);
649 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith54ecd982013-02-20 19:22:51 +0000650 UsingLoc, Name, Attrs.getList(),
651 TypeAlias);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000652 }
Richard Smithdda56e42011-04-15 14:24:37 +0000653
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000654 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
655 /* HasUsingKeyword */ true, UsingLoc,
656 SS, Name, Attrs.getList(),
657 HasTypenameKeyword, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000658}
659
Benjamin Kramere56f3932011-12-23 17:00:35 +0000660/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000661///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000662/// [C++0x] static_assert-declaration:
663/// static_assert ( constant-expression , string-literal ) ;
664///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000665/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000666/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000667///
John McCall48871652010-08-21 09:40:31 +0000668Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000669 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
670 "Not a static_assert declaration");
671
David Blaikiebbafb8a2012-03-11 07:00:24 +0000672 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000673 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000674 if (Tok.is(tok::kw_static_assert))
675 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000676
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000677 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000678
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000679 BalancedDelimiterTracker T(*this, tok::l_paren);
680 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000681 Diag(Tok, diag::err_expected) << tok::l_paren;
Richard Smith76965712012-09-13 19:12:50 +0000682 SkipMalformedDecl();
Craig Topper161e4db2014-05-21 06:02:52 +0000683 return nullptr;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000684 }
Mike Stump11289f42009-09-09 15:08:12 +0000685
John McCalldadc5752010-08-24 06:29:42 +0000686 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000687 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000688 SkipMalformedDecl();
Craig Topper161e4db2014-05-21 06:02:52 +0000689 return nullptr;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000690 }
Mike Stump11289f42009-09-09 15:08:12 +0000691
Richard Smith085a64f2014-06-20 19:57:12 +0000692 ExprResult AssertMessage;
693 if (Tok.is(tok::r_paren)) {
694 Diag(Tok, getLangOpts().CPlusPlus1z
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000695 ? diag::warn_cxx14_compat_static_assert_no_message
Richard Smith085a64f2014-06-20 19:57:12 +0000696 : diag::ext_static_assert_no_message)
697 << (getLangOpts().CPlusPlus1z
698 ? FixItHint()
699 : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\""));
700 } else {
701 if (ExpectAndConsume(tok::comma)) {
702 SkipUntil(tok::semi);
703 return nullptr;
704 }
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000705
Richard Smith085a64f2014-06-20 19:57:12 +0000706 if (!isTokenStringLiteral()) {
707 Diag(Tok, diag::err_expected_string_literal)
708 << /*Source='static_assert'*/1;
709 SkipMalformedDecl();
710 return nullptr;
711 }
Mike Stump11289f42009-09-09 15:08:12 +0000712
Richard Smith085a64f2014-06-20 19:57:12 +0000713 AssertMessage = ParseStringLiteralExpression();
714 if (AssertMessage.isInvalid()) {
715 SkipMalformedDecl();
716 return nullptr;
717 }
Richard Smithd67aea22012-03-06 03:21:47 +0000718 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000719
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000720 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000721
Chris Lattner49836b42009-04-02 04:16:50 +0000722 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000723 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000724
John McCallb268a282010-08-23 23:25:46 +0000725 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000726 AssertExpr.get(),
727 AssertMessage.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000728 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000729}
730
Richard Smith74aeef52013-04-26 16:15:35 +0000731/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson74948d02009-06-24 17:47:40 +0000732///
733/// 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +0000734/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson74948d02009-06-24 17:47:40 +0000735///
David Blaikie15a430a2011-12-04 05:04:18 +0000736SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
737 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
738 && "Not a decltype specifier");
739
David Blaikie15a430a2011-12-04 05:04:18 +0000740 ExprResult Result;
741 SourceLocation StartLoc = Tok.getLocation();
742 SourceLocation EndLoc;
743
744 if (Tok.is(tok::annot_decltype)) {
745 Result = getExprAnnotation(Tok);
746 EndLoc = Tok.getAnnotationEndLoc();
747 ConsumeToken();
748 if (Result.isInvalid()) {
749 DS.SetTypeSpecError();
750 return EndLoc;
751 }
752 } else {
Richard Smith324df552012-02-24 22:30:04 +0000753 if (Tok.getIdentifierInfo()->isStr("decltype"))
754 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000755
David Blaikie15a430a2011-12-04 05:04:18 +0000756 ConsumeToken();
757
758 BalancedDelimiterTracker T(*this, tok::l_paren);
759 if (T.expectAndConsume(diag::err_expected_lparen_after,
760 "decltype", tok::r_paren)) {
761 DS.SetTypeSpecError();
762 return T.getOpenLocation() == Tok.getLocation() ?
763 StartLoc : T.getOpenLocation();
764 }
765
Richard Smith74aeef52013-04-26 16:15:35 +0000766 // Check for C++1y 'decltype(auto)'.
767 if (Tok.is(tok::kw_auto)) {
768 // No need to disambiguate here: an expression can't start with 'auto',
769 // because the typename-specifier in a function-style cast operation can't
770 // be 'auto'.
771 Diag(Tok.getLocation(),
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000772 getLangOpts().CPlusPlus14
Richard Smith74aeef52013-04-26 16:15:35 +0000773 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
774 : diag::ext_decltype_auto_type_specifier);
775 ConsumeToken();
776 } else {
777 // Parse the expression
David Blaikie15a430a2011-12-04 05:04:18 +0000778
Richard Smith74aeef52013-04-26 16:15:35 +0000779 // C++11 [dcl.type.simple]p4:
780 // The operand of the decltype specifier is an unevaluated operand.
781 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
Craig Topper161e4db2014-05-21 06:02:52 +0000782 nullptr,/*IsDecltype=*/true);
Richard Smith74aeef52013-04-26 16:15:35 +0000783 Result = ParseExpression();
784 if (Result.isInvalid()) {
785 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000786 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smith74aeef52013-04-26 16:15:35 +0000787 EndLoc = ConsumeParen();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000788 } else {
Richard Smith74aeef52013-04-26 16:15:35 +0000789 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
790 // Backtrack to get the location of the last token before the semi.
791 PP.RevertCachedTokens(2);
792 ConsumeToken(); // the semi.
793 EndLoc = ConsumeAnyToken();
794 assert(Tok.is(tok::semi));
795 } else {
796 EndLoc = Tok.getLocation();
797 }
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000798 }
Richard Smith74aeef52013-04-26 16:15:35 +0000799 return EndLoc;
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000800 }
Richard Smith74aeef52013-04-26 16:15:35 +0000801
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000802 Result = Actions.ActOnDecltypeExpression(Result.get());
David Blaikie15a430a2011-12-04 05:04:18 +0000803 }
804
805 // Match the ')'
806 T.consumeClose();
807 if (T.getCloseLocation().isInvalid()) {
808 DS.SetTypeSpecError();
809 // FIXME: this should return the location of the last token
810 // that was consumed (by "consumeClose()")
811 return T.getCloseLocation();
812 }
813
Richard Smithfd555f62012-02-22 02:04:18 +0000814 if (Result.isInvalid()) {
815 DS.SetTypeSpecError();
816 return T.getCloseLocation();
817 }
818
David Blaikie15a430a2011-12-04 05:04:18 +0000819 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000820 }
Richard Smith74aeef52013-04-26 16:15:35 +0000821 assert(!Result.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000822
Craig Topper161e4db2014-05-21 06:02:52 +0000823 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +0000824 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000825 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Anders Carlsson74948d02009-06-24 17:47:40 +0000826 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smith74aeef52013-04-26 16:15:35 +0000827 if (Result.get()
828 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000829 DiagID, Result.get(), Policy)
Richard Smith74aeef52013-04-26 16:15:35 +0000830 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000831 DiagID, Policy)) {
John McCall49bfce42009-08-03 20:12:06 +0000832 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-12-04 05:04:18 +0000833 DS.SetTypeSpecError();
834 }
835 return EndLoc;
836}
837
838void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
839 SourceLocation StartLoc,
840 SourceLocation EndLoc) {
841 // make sure we have a token we can turn into an annotation token
842 if (PP.isBacktrackEnabled())
843 PP.RevertCachedTokens(1);
844 else
845 PP.EnterToken(Tok);
846
847 Tok.setKind(tok::annot_decltype);
Richard Smith74aeef52013-04-26 16:15:35 +0000848 setExprAnnotation(Tok,
849 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
850 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
851 ExprError());
David Blaikie15a430a2011-12-04 05:04:18 +0000852 Tok.setAnnotationEndLoc(EndLoc);
853 Tok.setLocation(StartLoc);
854 PP.AnnotateCachedTokens(Tok);
Anders Carlsson74948d02009-06-24 17:47:40 +0000855}
856
Alexis Hunt4a257072011-05-19 05:37:45 +0000857void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
858 assert(Tok.is(tok::kw___underlying_type) &&
859 "Not an underlying type specifier");
860
861 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000862 BalancedDelimiterTracker T(*this, tok::l_paren);
863 if (T.expectAndConsume(diag::err_expected_lparen_after,
864 "__underlying_type", tok::r_paren)) {
Alexis Hunt4a257072011-05-19 05:37:45 +0000865 return;
866 }
867
868 TypeResult Result = ParseTypeName();
869 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000870 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt4a257072011-05-19 05:37:45 +0000871 return;
872 }
873
874 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000875 T.consumeClose();
876 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +0000877 return;
878
Craig Topper161e4db2014-05-21 06:02:52 +0000879 const char *PrevSpec = nullptr;
Alexis Hunt4a257072011-05-19 05:37:45 +0000880 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000881 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000882 DiagID, Result.get(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000883 Actions.getASTContext().getPrintingPolicy()))
Alexis Hunt4a257072011-05-19 05:37:45 +0000884 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanellaa90af722013-07-06 18:54:58 +0000885 DS.setTypeofParensRange(T.getRange());
Alexis Hunt4a257072011-05-19 05:37:45 +0000886}
887
David Blaikie00ee7a082011-10-25 15:01:20 +0000888/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
889/// class name or decltype-specifier. Note that we only check that the result
890/// names a type; semantic analysis will need to verify that the type names a
891/// class. The result is either a type or null, depending on whether a type
892/// name was found.
Douglas Gregor831c93f2008-11-05 20:51:48 +0000893///
Richard Smith4c96e992013-02-19 23:47:15 +0000894/// base-type-specifier: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000895/// class-or-decltype
Richard Smith4c96e992013-02-19 23:47:15 +0000896/// class-or-decltype: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000897/// nested-name-specifier[opt] class-name
898/// decltype-specifier
Richard Smith4c96e992013-02-19 23:47:15 +0000899/// class-name: [C++ class.name]
Douglas Gregor831c93f2008-11-05 20:51:48 +0000900/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000901/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000902///
Richard Smith4c96e992013-02-19 23:47:15 +0000903/// In C++98, instead of base-type-specifier, we have:
904///
905/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie1cd50022011-10-25 17:10:12 +0000906Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
907 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-10-25 18:46:41 +0000908 // Ignore attempts to use typename
909 if (Tok.is(tok::kw_typename)) {
910 Diag(Tok, diag::err_expected_class_name_not_template)
911 << FixItHint::CreateRemoval(Tok.getLocation());
912 ConsumeToken();
913 }
914
David Blaikieafa155f2011-10-25 18:17:58 +0000915 // Parse optional nested-name-specifier
916 CXXScopeSpec SS;
917 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
918
919 BaseLoc = Tok.getLocation();
920
David Blaikie1cd50022011-10-25 17:10:12 +0000921 // Parse decltype-specifier
David Blaikie15a430a2011-12-04 05:04:18 +0000922 // tok == kw_decltype is just error recovery, it can only happen when SS
923 // isn't empty
924 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikieafa155f2011-10-25 18:17:58 +0000925 if (SS.isNotEmpty())
926 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
927 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie1cd50022011-10-25 17:10:12 +0000928 // Fake up a Declarator to use with ActOnTypeName.
929 DeclSpec DS(AttrFactory);
930
David Blaikie7491e732011-12-08 04:53:15 +0000931 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +0000932
933 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
934 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
935 }
936
Douglas Gregord54dfb82009-02-25 23:52:28 +0000937 // Check whether we have a template-id that names a type.
938 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000939 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +0000940 if (TemplateId->Kind == TNK_Type_template ||
941 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000942 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000943
944 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000945 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000946 EndLocation = Tok.getAnnotationEndLoc();
947 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000948
949 if (Type)
950 return Type;
951 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000952 }
953
954 // Fall through to produce an error below.
955 }
956
Douglas Gregor831c93f2008-11-05 20:51:48 +0000957 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000958 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000959 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000960 }
961
Douglas Gregor18473f32010-01-12 21:28:44 +0000962 IdentifierInfo *Id = Tok.getIdentifierInfo();
963 SourceLocation IdLoc = ConsumeToken();
964
965 if (Tok.is(tok::less)) {
966 // It looks the user intended to write a template-id here, but the
967 // template-name was wrong. Try to fix that.
968 TemplateNameKind TNK = TNK_Type_template;
969 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000970 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000971 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000972 Diag(IdLoc, diag::err_unknown_template_name)
973 << Id;
974 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000975
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000976 if (!Template) {
977 TemplateArgList TemplateArgs;
978 SourceLocation LAngleLoc, RAngleLoc;
979 ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
980 true, LAngleLoc, TemplateArgs, RAngleLoc);
Douglas Gregor18473f32010-01-12 21:28:44 +0000981 return true;
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000982 }
Douglas Gregor18473f32010-01-12 21:28:44 +0000983
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000984 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000985 UnqualifiedId TemplateName;
986 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000987
Douglas Gregor18473f32010-01-12 21:28:44 +0000988 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +0000989 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
990 TemplateName, true))
Douglas Gregor18473f32010-01-12 21:28:44 +0000991 return true;
992 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +0000993 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000994
Douglas Gregor18473f32010-01-12 21:28:44 +0000995 // If we didn't end up with a typename token, there's nothing more we
996 // can do.
997 if (Tok.isNot(tok::annot_typename))
998 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000999
Douglas Gregor18473f32010-01-12 21:28:44 +00001000 // Retrieve the type from the annotation token, consume that token, and
1001 // return.
1002 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +00001003 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +00001004 ConsumeToken();
1005 return Type;
1006 }
1007
Douglas Gregor831c93f2008-11-05 20:51:48 +00001008 // We have an identifier; check whether it is actually a type.
Craig Topper161e4db2014-05-21 06:02:52 +00001009 IdentifierInfo *CorrectedII = nullptr;
Douglas Gregore7c20652011-03-02 00:47:37 +00001010 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +00001011 false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00001012 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +00001013 /*NonTrivialTypeSourceInfo=*/true,
1014 &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001015 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001016 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001017 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +00001018 }
1019
1020 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +00001021 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001022
1023 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +00001024 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +00001025 DS.SetRangeStart(IdLoc);
1026 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +00001027 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001028
Craig Topper161e4db2014-05-21 06:02:52 +00001029 const char *PrevSpec = nullptr;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001030 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001031 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
1032 Actions.getASTContext().getPrintingPolicy());
Nick Lewycky19b9f952010-07-26 16:56:01 +00001033
1034 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1035 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001036}
1037
John McCall8d32c052012-05-22 21:28:12 +00001038void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
1039 while (Tok.is(tok::kw___single_inheritance) ||
1040 Tok.is(tok::kw___multiple_inheritance) ||
1041 Tok.is(tok::kw___virtual_inheritance)) {
1042 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1043 SourceLocation AttrNameLoc = ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001044 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Aaron Ballman8edb5c22013-12-18 23:44:18 +00001045 AttributeList::AS_Keyword);
John McCall8d32c052012-05-22 21:28:12 +00001046 }
1047}
1048
Richard Smith369b9f92012-06-25 21:37:02 +00001049/// Determine whether the following tokens are valid after a type-specifier
1050/// which could be a standalone declaration. This will conservatively return
1051/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith200f47c2012-07-02 19:14:01 +00001052bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-06-25 21:37:02 +00001053 // This switch enumerates the valid "follow" set for type-specifiers.
1054 switch (Tok.getKind()) {
1055 default: break;
1056 case tok::semi: // struct foo {...} ;
1057 case tok::star: // struct foo {...} * P;
1058 case tok::amp: // struct foo {...} & R = ...
Richard Smith1ac67d12013-01-19 03:48:05 +00001059 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +00001060 case tok::identifier: // struct foo {...} V ;
1061 case tok::r_paren: //(struct foo {...} ) {4}
1062 case tok::annot_cxxscope: // struct foo {...} a:: b;
1063 case tok::annot_typename: // struct foo {...} a ::b;
1064 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1065 case tok::l_paren: // struct foo {...} ( x);
1066 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +00001067 case tok::kw_operator: // struct foo operator ++() {...}
Alp Tokerd3f79c52013-11-24 20:24:54 +00001068 case tok::kw___declspec: // struct foo {...} __declspec(...)
Richard Smith843f18f2014-08-13 02:13:15 +00001069 case tok::l_square: // void f(struct f [ 3])
1070 case tok::ellipsis: // void f(struct f ... [Ns])
Abramo Bagnara152eb392014-08-16 08:29:27 +00001071 // FIXME: we should emit semantic diagnostic when declaration
1072 // attribute is in type attribute position.
1073 case tok::kw___attribute: // struct foo __attribute__((used)) x;
Richard Smith369b9f92012-06-25 21:37:02 +00001074 return true;
Richard Smith200f47c2012-07-02 19:14:01 +00001075 case tok::colon:
1076 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smith369b9f92012-06-25 21:37:02 +00001077 // Type qualifiers
1078 case tok::kw_const: // struct foo {...} const x;
1079 case tok::kw_volatile: // struct foo {...} volatile x;
1080 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith843f18f2014-08-13 02:13:15 +00001081 case tok::kw__Atomic: // struct foo {...} _Atomic x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001082 // Function specifiers
1083 // Note, no 'explicit'. An explicit function must be either a conversion
1084 // operator or a constructor. Either way, it can't have a return type.
1085 case tok::kw_inline: // struct foo inline f();
1086 case tok::kw_virtual: // struct foo virtual f();
1087 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +00001088 // Storage-class specifiers
1089 case tok::kw_static: // struct foo {...} static x;
1090 case tok::kw_extern: // struct foo {...} extern x;
1091 case tok::kw_typedef: // struct foo {...} typedef x;
1092 case tok::kw_register: // struct foo {...} register x;
1093 case tok::kw_auto: // struct foo {...} auto x;
1094 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001095 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +00001096 case tok::kw_constexpr: // struct foo {...} constexpr x;
1097 // As shown above, type qualifiers and storage class specifiers absolutely
1098 // can occur after class specifiers according to the grammar. However,
1099 // almost no one actually writes code like this. If we see one of these,
1100 // it is much more likely that someone missed a semi colon and the
1101 // type/storage class specifier we're seeing is part of the *next*
1102 // intended declaration, as in:
1103 //
1104 // struct foo { ... }
1105 // typedef int X;
1106 //
1107 // We'd really like to emit a missing semicolon error instead of emitting
1108 // an error on the 'int' saying that you can't have two type specifiers in
1109 // the same declaration of X. Because of this, we look ahead past this
1110 // token to see if it's a type specifier. If so, we know the code is
1111 // otherwise invalid, so we can produce the expected semi error.
1112 if (!isKnownToBeTypeSpecifier(NextToken()))
1113 return true;
1114 break;
1115 case tok::r_brace: // struct bar { struct foo {...} }
1116 // Missing ';' at end of struct is accepted as an extension in C mode.
1117 if (!getLangOpts().CPlusPlus)
1118 return true;
1119 break;
Richard Smith52c5b872013-01-29 04:13:32 +00001120 case tok::greater:
1121 // template<class T = class X>
1122 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001123 }
1124 return false;
1125}
1126
Douglas Gregor556877c2008-04-13 21:30:24 +00001127/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1128/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1129/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001130/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001131///
1132/// class-specifier: [C++ class]
1133/// class-head '{' member-specification[opt] '}'
1134/// class-head '{' member-specification[opt] '}' attributes[opt]
1135/// class-head:
1136/// class-key identifier[opt] base-clause[opt]
1137/// class-key nested-name-specifier identifier base-clause[opt]
1138/// class-key nested-name-specifier[opt] simple-template-id
1139/// base-clause[opt]
1140/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001141/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001142/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001143/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001144/// simple-template-id base-clause[opt]
1145/// class-key:
1146/// 'class'
1147/// 'struct'
1148/// 'union'
1149///
1150/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001151/// class-key ::[opt] nested-name-specifier[opt] identifier
1152/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1153/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001154///
1155/// Note that the C++ class-specifier and elaborated-type-specifier,
1156/// together, subsume the C99 struct-or-union-specifier:
1157///
1158/// struct-or-union-specifier: [C99 6.7.2.1]
1159/// struct-or-union identifier[opt] '{' struct-contents '}'
1160/// struct-or-union identifier
1161/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1162/// '}' attributes[opt]
1163/// [GNU] struct-or-union attributes[opt] identifier
1164/// struct-or-union:
1165/// 'struct'
1166/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001167void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1168 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001169 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001170 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001171 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001172 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001173 DeclSpec::TST TagType;
1174 if (TagTokKind == tok::kw_struct)
1175 TagType = DeclSpec::TST_struct;
1176 else if (TagTokKind == tok::kw___interface)
1177 TagType = DeclSpec::TST_interface;
1178 else if (TagTokKind == tok::kw_class)
1179 TagType = DeclSpec::TST_class;
1180 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001181 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1182 TagType = DeclSpec::TST_union;
1183 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001184
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001185 if (Tok.is(tok::code_completion)) {
1186 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001187 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001188 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001189 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001190
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001191 // C++03 [temp.explicit] 14.7.2/8:
1192 // The usual access checking rules do not apply to names used to specify
1193 // explicit instantiations.
1194 //
1195 // As an extension we do not perform access checking on the names used to
1196 // specify explicit specializations either. This is important to allow
1197 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001198 //
1199 // Note that we don't suppress if this turns out to be an elaborated
1200 // type specifier.
1201 bool shouldDelayDiagsInTag =
1202 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1203 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1204 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001205
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001206 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001207 // If attributes exist after tag, parse them.
Richard Smith37a45dd2013-10-24 01:21:09 +00001208 MaybeParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +00001209
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001210 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +00001211 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +00001212 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001213
John McCall8d32c052012-05-22 21:28:12 +00001214 // Parse inheritance specifiers.
1215 if (Tok.is(tok::kw___single_inheritance) ||
1216 Tok.is(tok::kw___multiple_inheritance) ||
1217 Tok.is(tok::kw___virtual_inheritance))
Richard Smith37a45dd2013-10-24 01:21:09 +00001218 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCall8d32c052012-05-22 21:28:12 +00001219
Alexis Hunt96d5c762009-11-21 08:43:09 +00001220 // If C++0x attributes exist here, parse them.
1221 // FIXME: Are we consistent with the ordering of parsing of different
1222 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001223 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001224
Michael Han309af292013-01-07 16:57:11 +00001225 // Source location used by FIXIT to insert misplaced
1226 // C++11 attributes
1227 SourceLocation AttrFixitLoc = Tok.getLocation();
1228
Alp Toker53358e42013-12-17 14:12:30 +00001229 // GNU libstdc++ and libc++ use certain intrinsic names as the
1230 // name of struct templates, but some are keywords in GCC >= 4.3
1231 // MSVC and Clang. For compatibility, convert the token to an identifier
1232 // and issue a warning diagnostic.
1233 if (TagType == DeclSpec::TST_struct && !Tok.is(tok::identifier) &&
1234 !Tok.isAnnotation()) {
1235 const IdentifierInfo *II = Tok.getIdentifierInfo();
1236 // We rarely end up here so the following check is efficient.
1237 if (II && II->getName().startswith("__is_"))
1238 TryKeywordIdentFallback(true);
1239 }
Mike Stump11289f42009-09-09 15:08:12 +00001240
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001241 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001242 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001243 if (getLangOpts().CPlusPlus) {
Serge Pavlov458ea762014-07-16 05:16:52 +00001244 // "FOO : BAR" is not a potential typo for "FOO::BAR". In this context it
1245 // is a base-specifier-list.
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001246 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001247
Douglas Gregordf593fb2011-11-07 17:33:42 +00001248 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall413021a2010-07-30 06:26:29 +00001249 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +00001250 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001251 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Alp Tokerec543272013-12-24 09:48:30 +00001252 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001253 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001254
Douglas Gregor916462b2009-10-30 21:46:58 +00001255 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1256
Douglas Gregor67a65642009-02-17 23:15:12 +00001257 // Parse the (optional) class name or simple-template-id.
Craig Topper161e4db2014-05-21 06:02:52 +00001258 IdentifierInfo *Name = nullptr;
Douglas Gregor556877c2008-04-13 21:30:24 +00001259 SourceLocation NameLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001260 TemplateIdAnnotation *TemplateId = nullptr;
Douglas Gregor556877c2008-04-13 21:30:24 +00001261 if (Tok.is(tok::identifier)) {
1262 Name = Tok.getIdentifierInfo();
1263 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001264
David Blaikiebbafb8a2012-03-11 07:00:24 +00001265 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001266 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001267 // Eat the template argument list and try to continue parsing this as
1268 // a class (or template thereof).
1269 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001270 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +00001271 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +00001272 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001273 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001274 // We couldn't parse the template argument list at all, so don't
1275 // try to give any location information for the list.
1276 LAngleLoc = RAngleLoc = SourceLocation();
1277 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001278
Douglas Gregor916462b2009-10-30 21:46:58 +00001279 Diag(NameLoc, diag::err_explicit_spec_non_template)
Alp Toker01d65e12014-01-06 12:54:41 +00001280 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1281 << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
Joao Matose9a3ed42012-08-31 22:18:20 +00001282
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001283 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001284 // we've removed its template argument list.
1285 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1286 if (TemplateParams && TemplateParams->size() > 1) {
1287 TemplateParams->pop_back();
1288 } else {
Craig Topper161e4db2014-05-21 06:02:52 +00001289 TemplateParams = nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001290 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001291 = ParsedTemplateInfo::NonTemplate;
1292 }
1293 } else if (TemplateInfo.Kind
1294 == ParsedTemplateInfo::ExplicitInstantiation) {
1295 // Pretend this is just a forward declaration.
Craig Topper161e4db2014-05-21 06:02:52 +00001296 TemplateParams = nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001297 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001298 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001299 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001300 = SourceLocation();
1301 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1302 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001303 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001304 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001305 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001306 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001307 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001308
Douglas Gregore7c20652011-03-02 00:47:37 +00001309 if (TemplateId->Kind != TNK_Type_template &&
1310 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001311 // The template-name in the simple-template-id refers to
1312 // something other than a class template. Give an appropriate
1313 // error message and skip to the ';'.
1314 SourceRange Range(NameLoc);
1315 if (SS.isNotEmpty())
1316 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001317
Richard Smith72bfbd82013-12-04 00:28:23 +00001318 // FIXME: Name may be null here.
Douglas Gregor7f741122009-02-25 19:37:18 +00001319 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu30f93852013-06-19 22:25:01 +00001320 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001321
Douglas Gregor7f741122009-02-25 19:37:18 +00001322 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001323 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor7f741122009-02-25 19:37:18 +00001324 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001325 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001326 }
1327
Richard Smithbfdb1082012-03-12 08:56:40 +00001328 // There are four options here.
1329 // - If we are in a trailing return type, this is always just a reference,
1330 // and we must not try to parse a definition. For instance,
1331 // [] () -> struct S { };
1332 // does not define a type.
1333 // - If we have 'struct foo {...', 'struct foo :...',
1334 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1335 // - If we have 'struct foo;', then this is either a forward declaration
1336 // or a friend declaration, which have to be treated differently.
1337 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001338 //
1339 // We also detect these erroneous cases to provide better diagnostic for
1340 // C++11 attributes parsing.
1341 // - attributes follow class name:
1342 // struct foo [[]] {};
1343 // - attributes appear before or after 'final':
1344 // struct foo [[]] final [[]] {};
1345 //
Richard Smithc5b05522012-03-12 07:56:15 +00001346 // However, in type-specifier-seq's, things look like declarations but are
1347 // just references, e.g.
1348 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001349 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001350 // &T::operator struct s;
Richard Smith649c7b062014-01-08 00:56:48 +00001351 // For these, DSC is DSC_type_specifier or DSC_alias_declaration.
Michael Han9407e502012-11-26 22:54:45 +00001352
1353 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001354 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001355
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001356 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
John McCallfaf5fb42010-08-26 23:41:50 +00001357 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001358 if (DSC == DSC_trailing)
1359 TUK = Sema::TUK_Reference;
1360 else if (Tok.is(tok::l_brace) ||
1361 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001362 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001363 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001364 if (DS.isFriendSpecified()) {
1365 // C++ [class.friend]p2:
1366 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001367 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001368 << SourceRange(DS.getFriendSpecLoc());
1369
1370 // Skip everything up to the semicolon, so that this looks like a proper
1371 // friend class (or template thereof) declaration.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001372 SkipUntil(tok::semi, StopBeforeMatch);
John McCallfaf5fb42010-08-26 23:41:50 +00001373 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001374 } else {
1375 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001376 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001377 }
Richard Smith434516c2013-02-22 06:46:23 +00001378 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1379 NextToken().is(tok::kw_alignas))) {
Michael Han9407e502012-11-26 22:54:45 +00001380 // We can't tell if this is a definition or reference
1381 // until we skipped the 'final' and C++11 attribute specifiers.
1382 TentativeParsingAction PA(*this);
1383
1384 // Skip the 'final' keyword.
1385 ConsumeToken();
1386
1387 // Skip C++11 attribute specifiers.
1388 while (true) {
1389 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1390 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001391 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001392 break;
Richard Smith434516c2013-02-22 06:46:23 +00001393 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han9407e502012-11-26 22:54:45 +00001394 ConsumeToken();
1395 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001396 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001397 break;
1398 } else {
1399 break;
1400 }
1401 }
1402
1403 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1404 TUK = Sema::TUK_Definition;
1405 else
1406 TUK = Sema::TUK_Reference;
1407
1408 PA.Revert();
Richard Smith649c7b062014-01-08 00:56:48 +00001409 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00001410 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001411 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001412 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001413 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001414 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Joao Matose9a3ed42012-08-31 22:18:20 +00001415 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00001416 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001417 DeclSpec::getSpecifierName(TagType, PPol));
Joao Matose9a3ed42012-08-31 22:18:20 +00001418 PP.EnterToken(Tok);
1419 Tok.setKind(tok::semi);
1420 }
Richard Smith369b9f92012-06-25 21:37:02 +00001421 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001422 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001423
Michael Han9407e502012-11-26 22:54:45 +00001424 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1425 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001426 if (TUK != Sema::TUK_Reference) {
1427 // If this is not a reference, then the only possible
1428 // valid place for C++11 attributes to appear here
1429 // is between class-key and class-name. If there are
1430 // any attributes after class-name, we try a fixit to move
1431 // them to the right place.
1432 SourceRange AttrRange = Attributes.Range;
1433 if (AttrRange.isValid()) {
1434 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1435 << AttrRange
1436 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1437 CharSourceRange(AttrRange, true))
1438 << FixItHint::CreateRemoval(AttrRange);
1439
1440 // Recover by adding misplaced attributes to the attribute list
1441 // of the class so they can be applied on the class later.
1442 attrs.takeAllFrom(Attributes);
1443 }
1444 }
Michael Han9407e502012-11-26 22:54:45 +00001445
John McCall6347b682012-05-07 06:16:58 +00001446 // If this is an elaborated type specifier, and we delayed
1447 // diagnostics before, just merge them into the current pool.
1448 if (shouldDelayDiagsInTag) {
1449 diagsFromTag.done();
1450 if (TUK == Sema::TUK_Reference)
1451 diagsFromTag.redelay();
1452 }
1453
John McCall413021a2010-07-30 06:26:29 +00001454 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001455 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001456 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1457 // We have a declaration or reference to an anonymous class.
1458 Diag(StartLoc, diag::err_anon_type_definition)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001459 << DeclSpec::getSpecifierName(TagType, Policy);
John McCall413021a2010-07-30 06:26:29 +00001460 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001461
David Majnemer3252fd02013-12-05 01:36:53 +00001462 // If we are parsing a definition and stop at a base-clause, continue on
1463 // until the semicolon. Continuing from the comma will just trick us into
1464 // thinking we are seeing a variable declaration.
1465 if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
1466 SkipUntil(tok::semi, StopBeforeMatch);
1467 else
1468 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor556877c2008-04-13 21:30:24 +00001469 return;
1470 }
1471
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001472 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001473 DeclResult TagOrTempResult = true; // invalid
1474 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001475
Douglas Gregord6ab8742009-05-28 23:31:59 +00001476 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +00001477 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001478 // Explicit specialization, class template partial specialization,
1479 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001480 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001481 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001482 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001483 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001484 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001485 ProhibitAttributes(attrs);
1486
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001487 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001488 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001489 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001490 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001491 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001492 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001493 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001494 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001495 TemplateId->TemplateNameLoc,
1496 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001497 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001498 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001499 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001500
1501 // Friend template-ids are treated as references unless
1502 // they have template headers, in which case they're ill-formed
1503 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1504 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001505 } else if (TUK == Sema::TUK_Reference ||
1506 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001507 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001508 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001509 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001510 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001511 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001512 TemplateId->Template,
1513 TemplateId->TemplateNameLoc,
1514 TemplateId->LAngleLoc,
1515 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001516 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001517 } else {
1518 // This is an explicit specialization or a class template
1519 // partial specialization.
1520 TemplateParameterLists FakedParamLists;
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001521 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1522 // This looks like an explicit instantiation, because we have
1523 // something like
1524 //
1525 // template class Foo<X>
1526 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001527 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001528 // meant to be an explicit specialization, but the user forgot
1529 // the '<>' after 'template'.
Richard Smith003c5e12013-11-08 19:03:29 +00001530 // It this is friend declaration however, since it cannot have a
1531 // template header, it is most likely that the user meant to
1532 // remove the 'template' keyword.
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001533 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith003c5e12013-11-08 19:03:29 +00001534 "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001535
Richard Smith003c5e12013-11-08 19:03:29 +00001536 if (TUK == Sema::TUK_Friend) {
1537 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
Craig Topper161e4db2014-05-21 06:02:52 +00001538 TemplateParams = nullptr;
Richard Smith003c5e12013-11-08 19:03:29 +00001539 } else {
1540 SourceLocation LAngleLoc =
1541 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1542 Diag(TemplateId->TemplateNameLoc,
1543 diag::err_explicit_instantiation_with_definition)
1544 << SourceRange(TemplateInfo.TemplateLoc)
1545 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1546
1547 // Create a fake template parameter list that contains only
1548 // "template<>", so that we treat this construct as a class
1549 // template specialization.
1550 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
Craig Topper161e4db2014-05-21 06:02:52 +00001551 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, nullptr,
1552 0, LAngleLoc));
Richard Smith003c5e12013-11-08 19:03:29 +00001553 TemplateParams = &FakedParamLists;
1554 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001555 }
1556
1557 // Build the class template specialization.
Richard Smith4b55a9c2014-04-17 03:29:33 +00001558 TagOrTempResult = Actions.ActOnClassTemplateSpecialization(
1559 getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(),
1560 *TemplateId, attrs.getList(),
Craig Topper161e4db2014-05-21 06:02:52 +00001561 MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0]
1562 : nullptr,
Richard Smith4b55a9c2014-04-17 03:29:33 +00001563 TemplateParams ? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001564 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001565 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001566 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001567 // Explicit instantiation of a member of a class template
1568 // specialization, e.g.,
1569 //
1570 // template struct Outer<int>::Inner;
1571 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001572 ProhibitAttributes(attrs);
1573
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001574 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001575 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001576 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001577 TemplateInfo.TemplateLoc,
1578 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001579 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001580 } else if (TUK == Sema::TUK_Friend &&
1581 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001582 ProhibitAttributes(attrs);
1583
John McCallace48cd2010-10-19 01:40:49 +00001584 TagOrTempResult =
1585 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1586 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001587 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001588 MultiTemplateParamsArg(
Craig Topper161e4db2014-05-21 06:02:52 +00001589 TemplateParams? &(*TemplateParams)[0]
1590 : nullptr,
John McCallace48cd2010-10-19 01:40:49 +00001591 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001592 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001593 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1594 ProhibitAttributes(attrs);
Richard Smith003c5e12013-11-08 19:03:29 +00001595
Larisse Voufo725de3e2013-06-21 00:08:46 +00001596 if (TUK == Sema::TUK_Definition &&
1597 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1598 // If the declarator-id is not a template-id, issue a diagnostic and
1599 // recover by ignoring the 'template' keyword.
1600 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1601 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Craig Topper161e4db2014-05-21 06:02:52 +00001602 TemplateParams = nullptr;
Larisse Voufo725de3e2013-06-21 00:08:46 +00001603 }
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001604
John McCall7f41d982009-09-11 04:59:25 +00001605 bool IsDependent = false;
1606
John McCall32723e92010-10-19 18:40:57 +00001607 // Don't pass down template parameter lists if this is just a tag
1608 // reference. For example, we don't need the template parameters here:
1609 // template <class T> class A *makeA(T t);
1610 MultiTemplateParamsArg TParams;
1611 if (TUK != Sema::TUK_Reference && TemplateParams)
1612 TParams =
1613 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1614
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001615 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001616 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001617 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001618 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001619 TParams, Owned, IsDependent,
1620 SourceLocation(), false,
Richard Smith649c7b062014-01-08 00:56:48 +00001621 clang::TypeResult(),
1622 DSC == DSC_type_specifier);
John McCall7f41d982009-09-11 04:59:25 +00001623
1624 // If ActOnTag said the type was dependent, try again with the
1625 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001626 if (IsDependent) {
1627 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001628 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001629 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001630 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001631 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001632
Douglas Gregor556877c2008-04-13 21:30:24 +00001633 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001634 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001635 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001636 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001637 isCXX11FinalKeyword());
David Blaikiebbafb8a2012-03-11 07:00:24 +00001638 if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001639 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1640 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001641 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001642 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001643 }
1644
Craig Topper161e4db2014-05-21 06:02:52 +00001645 const char *PrevSpec = nullptr;
John McCallba7bf592010-08-24 05:47:05 +00001646 unsigned DiagID;
1647 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001648 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001649 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1650 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001651 PrevSpec, DiagID, TypeResult.get(), Policy);
John McCall7f41d982009-09-11 04:59:25 +00001652 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001653 Result = DS.SetTypeSpecType(TagType, StartLoc,
1654 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001655 PrevSpec, DiagID, TagOrTempResult.get(), Owned,
1656 Policy);
John McCall7f41d982009-09-11 04:59:25 +00001657 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001658 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001659 return;
1660 }
Mike Stump11289f42009-09-09 15:08:12 +00001661
John McCallba7bf592010-08-24 05:47:05 +00001662 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001663 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001664
Chris Lattnercf251412010-02-02 01:23:29 +00001665 // At this point, we've successfully parsed a class-specifier in 'definition'
1666 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1667 // going to look at what comes after it to improve error recovery. If an
1668 // impossible token occurs next, we assume that the programmer forgot a ; at
1669 // the end of the declaration and recover that way.
1670 //
Richard Smith369b9f92012-06-25 21:37:02 +00001671 // Also enforce C++ [temp]p3:
1672 // In a template-declaration which defines a class, no declarator
1673 // is permitted.
Richard Smith843f18f2014-08-13 02:13:15 +00001674 //
1675 // After a type-specifier, we don't expect a semicolon. This only happens in
1676 // C, since definitions are not permitted in this context in C++.
Joao Matose9a3ed42012-08-31 22:18:20 +00001677 if (TUK == Sema::TUK_Definition &&
Richard Smith843f18f2014-08-13 02:13:15 +00001678 (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) &&
Joao Matose9a3ed42012-08-31 22:18:20 +00001679 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001680 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001681 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Alp Toker383d2c42014-01-01 03:08:43 +00001682 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001683 DeclSpec::getSpecifierName(TagType, PPol));
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001684 // Push this token back into the preprocessor and change our current token
1685 // to ';' so that the rest of the code recovers as though there were an
1686 // ';' after the definition.
1687 PP.EnterToken(Tok);
1688 Tok.setKind(tok::semi);
1689 }
Chris Lattnercf251412010-02-02 01:23:29 +00001690 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001691}
1692
Mike Stump11289f42009-09-09 15:08:12 +00001693/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001694///
1695/// base-clause : [C++ class.derived]
1696/// ':' base-specifier-list
1697/// base-specifier-list:
1698/// base-specifier '...'[opt]
1699/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001700void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001701 assert(Tok.is(tok::colon) && "Not a base clause");
1702 ConsumeToken();
1703
Douglas Gregor29a92472008-10-22 17:49:05 +00001704 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001705 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001706
Douglas Gregor556877c2008-04-13 21:30:24 +00001707 while (true) {
1708 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001709 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001710 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001711 // Skip the rest of this base specifier, up until the comma or
1712 // opening brace.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001713 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor29a92472008-10-22 17:49:05 +00001714 } else {
1715 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001716 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001717 }
1718
1719 // If the next token is a comma, consume it and keep reading
1720 // base-specifiers.
Alp Toker97650562014-01-10 11:19:30 +00001721 if (!TryConsumeToken(tok::comma))
1722 break;
Douglas Gregor556877c2008-04-13 21:30:24 +00001723 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001724
1725 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001726 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001727}
1728
1729/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1730/// one entry in the base class list of a class specifier, for example:
1731/// class foo : public bar, virtual private baz {
1732/// 'public bar' and 'virtual private baz' are each base-specifiers.
1733///
1734/// base-specifier: [C++ class.derived]
Richard Smith4c96e992013-02-19 23:47:15 +00001735/// attribute-specifier-seq[opt] base-type-specifier
1736/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1737/// base-type-specifier
1738/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1739/// base-type-specifier
John McCall48871652010-08-21 09:40:31 +00001740Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001741 bool IsVirtual = false;
1742 SourceLocation StartLoc = Tok.getLocation();
1743
Richard Smith4c96e992013-02-19 23:47:15 +00001744 ParsedAttributesWithRange Attributes(AttrFactory);
1745 MaybeParseCXX11Attributes(Attributes);
1746
Douglas Gregor556877c2008-04-13 21:30:24 +00001747 // Parse the 'virtual' keyword.
Alp Toker97650562014-01-10 11:19:30 +00001748 if (TryConsumeToken(tok::kw_virtual))
Douglas Gregor556877c2008-04-13 21:30:24 +00001749 IsVirtual = true;
Douglas Gregor556877c2008-04-13 21:30:24 +00001750
Richard Smith4c96e992013-02-19 23:47:15 +00001751 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1752
Douglas Gregor556877c2008-04-13 21:30:24 +00001753 // Parse an (optional) access specifier.
1754 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001755 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001756 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001757
Richard Smith4c96e992013-02-19 23:47:15 +00001758 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1759
Douglas Gregor556877c2008-04-13 21:30:24 +00001760 // Parse the 'virtual' keyword (again!), in case it came after the
1761 // access specifier.
1762 if (Tok.is(tok::kw_virtual)) {
1763 SourceLocation VirtualLoc = ConsumeToken();
1764 if (IsVirtual) {
1765 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001766 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001767 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001768 }
1769
1770 IsVirtual = true;
1771 }
1772
Richard Smith4c96e992013-02-19 23:47:15 +00001773 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1774
Douglas Gregor831c93f2008-11-05 20:51:48 +00001775 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001776 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00001777 SourceLocation BaseLoc;
1778 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001779 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001780 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001781
Douglas Gregor752a5952011-01-03 22:36:02 +00001782 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1783 // actually part of the base-specifier-list grammar productions, but we
1784 // parse it here for convenience.
1785 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00001786 TryConsumeToken(tok::ellipsis, EllipsisLoc);
1787
Mike Stump11289f42009-09-09 15:08:12 +00001788 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001789 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001790
Douglas Gregor556877c2008-04-13 21:30:24 +00001791 // Notify semantic analysis that we have parsed a complete
1792 // base-specifier.
Richard Smith4c96e992013-02-19 23:47:15 +00001793 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1794 Access, BaseType.get(), BaseLoc,
1795 EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001796}
1797
1798/// getAccessSpecifierIfPresent - Determine whether the next token is
1799/// a C++ access-specifier.
1800///
1801/// access-specifier: [C++ class.derived]
1802/// 'private'
1803/// 'protected'
1804/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001805AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001806 switch (Tok.getKind()) {
1807 default: return AS_none;
1808 case tok::kw_private: return AS_private;
1809 case tok::kw_protected: return AS_protected;
1810 case tok::kw_public: return AS_public;
1811 }
1812}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001813
Douglas Gregor433e0532012-04-16 18:27:27 +00001814/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith2331bbf2012-05-02 22:22:32 +00001815/// delayed, e.g., default arguments, create a late-parsed method declaration
1816/// record to handle the parsing at the end of the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00001817void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1818 Decl *ThisDecl) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001819 // We just declared a member function. If this member function
Richard Smith2331bbf2012-05-02 22:22:32 +00001820 // has any default arguments, we'll need to parse them later.
Craig Topper161e4db2014-05-21 06:02:52 +00001821 LateParsedMethodDeclaration *LateMethod = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001822 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001823 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor433e0532012-04-16 18:27:27 +00001824
Alp Tokerc5350722014-02-26 22:27:52 +00001825 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) {
1826 if (LateMethod || FTI.Params[ParamIdx].DefaultArgTokens) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001827 if (!LateMethod) {
1828 // Push this method onto the stack of late-parsed method
1829 // declarations.
Douglas Gregorefc46952010-10-12 16:25:54 +00001830 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1831 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001832 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001833
1834 // Add all of the parameters prior to this one (they don't
1835 // have default arguments).
Alp Tokerc5350722014-02-26 22:27:52 +00001836 LateMethod->DefaultArgs.reserve(FTI.NumParams);
Eli Friedman3af2a772009-07-22 21:45:50 +00001837 for (unsigned I = 0; I < ParamIdx; ++I)
1838 LateMethod->DefaultArgs.push_back(
Alp Tokerc5350722014-02-26 22:27:52 +00001839 LateParsedDefaultArgument(FTI.Params[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001840 }
1841
Douglas Gregor433e0532012-04-16 18:27:27 +00001842 // Add this parameter to the list of parameters (it may or may
Eli Friedman3af2a772009-07-22 21:45:50 +00001843 // not have a default argument).
Alp Tokerc5350722014-02-26 22:27:52 +00001844 LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
1845 FTI.Params[ParamIdx].Param, FTI.Params[ParamIdx].DefaultArgTokens));
Eli Friedman3af2a772009-07-22 21:45:50 +00001846 }
1847 }
1848}
1849
Richard Smith89645bc2013-01-02 12:01:23 +00001850/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001851/// virt-specifier.
1852///
1853/// virt-specifier:
1854/// override
1855/// final
Richard Smith89645bc2013-01-02 12:01:23 +00001856VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001857 if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001858 return VirtSpecifiers::VS_None;
1859
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001860 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001861
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001862 // Initialize the contextual keywords.
1863 if (!Ident_final) {
1864 Ident_final = &PP.getIdentifierTable().get("final");
1865 if (getLangOpts().MicrosoftExt)
1866 Ident_sealed = &PP.getIdentifierTable().get("sealed");
1867 Ident_override = &PP.getIdentifierTable().get("override");
Anders Carlsson56104902011-01-17 03:05:47 +00001868 }
1869
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001870 if (II == Ident_override)
1871 return VirtSpecifiers::VS_Override;
1872
1873 if (II == Ident_sealed)
1874 return VirtSpecifiers::VS_Sealed;
1875
1876 if (II == Ident_final)
1877 return VirtSpecifiers::VS_Final;
1878
Anders Carlsson56104902011-01-17 03:05:47 +00001879 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001880}
1881
Richard Smith89645bc2013-01-02 12:01:23 +00001882/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001883///
1884/// virt-specifier-seq:
1885/// virt-specifier
1886/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00001887void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
Richard Smith3d1a94c2014-08-12 00:22:39 +00001888 bool IsInterface,
1889 SourceLocation FriendLoc) {
Anders Carlsson56104902011-01-17 03:05:47 +00001890 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00001891 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001892 if (Specifier == VirtSpecifiers::VS_None)
1893 return;
1894
Richard Smith3d1a94c2014-08-12 00:22:39 +00001895 if (FriendLoc.isValid()) {
1896 Diag(Tok.getLocation(), diag::err_friend_decl_spec)
1897 << VirtSpecifiers::getSpecifierName(Specifier)
1898 << FixItHint::CreateRemoval(Tok.getLocation())
1899 << SourceRange(FriendLoc, FriendLoc);
1900 ConsumeToken();
1901 continue;
1902 }
1903
Anders Carlsson56104902011-01-17 03:05:47 +00001904 // C++ [class.mem]p8:
1905 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Craig Topper161e4db2014-05-21 06:02:52 +00001906 const char *PrevSpec = nullptr;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001907 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001908 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1909 << PrevSpec
1910 << FixItHint::CreateRemoval(Tok.getLocation());
1911
David Majnemera5433082013-10-18 00:33:31 +00001912 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
1913 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalldb632ac2012-09-25 07:32:39 +00001914 Diag(Tok.getLocation(), diag::err_override_control_interface)
1915 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemera5433082013-10-18 00:33:31 +00001916 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
1917 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
John McCalldb632ac2012-09-25 07:32:39 +00001918 } else {
David Majnemera5433082013-10-18 00:33:31 +00001919 Diag(Tok.getLocation(),
1920 getLangOpts().CPlusPlus11
1921 ? diag::warn_cxx98_compat_override_control_keyword
1922 : diag::ext_override_control_keyword)
1923 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalldb632ac2012-09-25 07:32:39 +00001924 }
Anders Carlsson56104902011-01-17 03:05:47 +00001925 ConsumeToken();
1926 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001927}
1928
Richard Smith89645bc2013-01-02 12:01:23 +00001929/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001930/// 'final' or Microsoft 'sealed' contextual keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00001931bool Parser::isCXX11FinalKeyword() const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001932 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
1933 return Specifier == VirtSpecifiers::VS_Final ||
1934 Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001935}
1936
Richard Smith72553fc2014-01-23 23:53:27 +00001937/// \brief Parse a C++ member-declarator up to, but not including, the optional
1938/// brace-or-equal-initializer or pure-specifier.
1939void Parser::ParseCXXMemberDeclaratorBeforeInitializer(
1940 Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
1941 LateParsedAttrList &LateParsedAttrs) {
1942 // member-declarator:
1943 // declarator pure-specifier[opt]
1944 // declarator brace-or-equal-initializer[opt]
1945 // identifier[opt] ':' constant-expression
Serge Pavlov458ea762014-07-16 05:16:52 +00001946 if (Tok.isNot(tok::colon))
Richard Smith72553fc2014-01-23 23:53:27 +00001947 ParseDeclarator(DeclaratorInfo);
Richard Smith3d1a94c2014-08-12 00:22:39 +00001948 else
1949 DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation());
Richard Smith72553fc2014-01-23 23:53:27 +00001950
1951 if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
Richard Smith3d1a94c2014-08-12 00:22:39 +00001952 assert(DeclaratorInfo.isPastIdentifier() &&
1953 "don't know where identifier would go yet?");
Richard Smith72553fc2014-01-23 23:53:27 +00001954 BitfieldSize = ParseConstantExpression();
1955 if (BitfieldSize.isInvalid())
1956 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
1957 } else
Richard Smith3d1a94c2014-08-12 00:22:39 +00001958 ParseOptionalCXX11VirtSpecifierSeq(
1959 VS, getCurrentClass().IsInterface,
1960 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Richard Smith72553fc2014-01-23 23:53:27 +00001961
1962 // If a simple-asm-expr is present, parse it.
1963 if (Tok.is(tok::kw_asm)) {
1964 SourceLocation Loc;
1965 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1966 if (AsmLabel.isInvalid())
1967 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
1968
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001969 DeclaratorInfo.setAsmLabel(AsmLabel.get());
Richard Smith72553fc2014-01-23 23:53:27 +00001970 DeclaratorInfo.SetRangeEnd(Loc);
1971 }
1972
1973 // If attributes exist after the declarator, but before an '{', parse them.
1974 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Richard Smith4b5a9492014-01-24 22:34:35 +00001975
1976 // For compatibility with code written to older Clang, also accept a
1977 // virt-specifier *after* the GNU attributes.
Aaron Ballman5d153e32014-08-04 17:03:51 +00001978 if (BitfieldSize.isUnset() && VS.isUnset()) {
Richard Smith3d1a94c2014-08-12 00:22:39 +00001979 ParseOptionalCXX11VirtSpecifierSeq(
1980 VS, getCurrentClass().IsInterface,
1981 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Aaron Ballman5d153e32014-08-04 17:03:51 +00001982 if (!VS.isUnset()) {
1983 // If we saw any GNU-style attributes that are known to GCC followed by a
1984 // virt-specifier, issue a GCC-compat warning.
1985 const AttributeList *Attr = DeclaratorInfo.getAttributes();
1986 while (Attr) {
1987 if (Attr->isKnownToGCC() && !Attr->isCXX11Attribute())
1988 Diag(Attr->getLoc(), diag::warn_gcc_attribute_location);
1989 Attr = Attr->getNext();
1990 }
1991 }
1992 }
Richard Smith72553fc2014-01-23 23:53:27 +00001993}
1994
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001995/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1996///
1997/// member-declaration:
1998/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1999/// function-definition ';'[opt]
2000/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
2001/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00002002/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002003/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002004/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002005///
2006/// member-declarator-list:
2007/// member-declarator
2008/// member-declarator-list ',' member-declarator
2009///
2010/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002011/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002012/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00002013/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002014/// identifier[opt] ':' constant-expression
2015///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002016/// virt-specifier-seq:
2017/// virt-specifier
2018/// virt-specifier-seq virt-specifier
2019///
2020/// virt-specifier:
2021/// override
2022/// final
David Majnemera5433082013-10-18 00:33:31 +00002023/// [MS] sealed
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002024///
Sebastian Redl42e92c42009-04-12 17:16:29 +00002025/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002026/// '= 0'
2027///
2028/// constant-initializer:
2029/// '=' constant-expression
2030///
Douglas Gregor3447e762009-08-20 22:52:58 +00002031void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002032 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00002033 const ParsedTemplateInfo &TemplateInfo,
2034 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00002035 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002036 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00002037 Diag(Tok, diag::err_at_defs_cxx);
2038 else
2039 Diag(Tok, diag::err_at_in_class);
Richard Smithda35e962013-11-09 04:52:51 +00002040
Douglas Gregor23c84762011-04-14 17:21:19 +00002041 ConsumeToken();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002042 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregor23c84762011-04-14 17:21:19 +00002043 return;
2044 }
Richard Smithda35e962013-11-09 04:52:51 +00002045
Serge Pavlov458ea762014-07-16 05:16:52 +00002046 // Turn on colon protection early, while parsing declspec, although there is
2047 // nothing to protect there. It prevents from false errors if error recovery
2048 // incorrectly determines where the declspec ends, as in the example:
2049 // struct A { enum class B { C }; };
2050 // const int C = 4;
2051 // struct D { A::B : C; };
2052 ColonProtectionRAIIObject X(*this);
2053
John McCalla0097262009-12-11 02:10:03 +00002054 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00002055 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00002056 if (!TemplateInfo.Kind &&
Richard Smith45855df2012-05-09 08:23:23 +00002057 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
2058 if (TryAnnotateCXXScopeToken())
2059 MalformedTypeSpec = true;
2060
2061 bool isAccessDecl;
2062 if (Tok.isNot(tok::annot_cxxscope))
2063 isAccessDecl = false;
2064 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00002065 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
2066 else
2067 isAccessDecl = NextToken().is(tok::kw_operator);
2068
2069 if (isAccessDecl) {
2070 // Collect the scope specifier token we annotated earlier.
2071 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002072 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2073 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00002074
Nico Weberef03e702014-09-10 00:59:37 +00002075 if (SS.isInvalid()) {
2076 SkipUntil(tok::semi);
2077 return;
2078 }
2079
John McCalla0097262009-12-11 02:10:03 +00002080 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00002081 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00002082 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +00002083 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
2084 TemplateKWLoc, Name)) {
John McCalla0097262009-12-11 02:10:03 +00002085 SkipUntil(tok::semi);
2086 return;
2087 }
2088
2089 // TODO: recover from mistakenly-qualified operator declarations.
Alp Toker383d2c42014-01-01 03:08:43 +00002090 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
2091 "access declaration")) {
2092 SkipUntil(tok::semi);
John McCalla0097262009-12-11 02:10:03 +00002093 return;
Alp Toker383d2c42014-01-01 03:08:43 +00002094 }
John McCalla0097262009-12-11 02:10:03 +00002095
Douglas Gregor0be31a22010-07-02 17:43:08 +00002096 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002097 /* HasUsingKeyword */ false,
2098 SourceLocation(),
John McCalla0097262009-12-11 02:10:03 +00002099 SS, Name,
Craig Topper161e4db2014-05-21 06:02:52 +00002100 /* AttrList */ nullptr,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002101 /* HasTypenameKeyword */ false,
John McCalla0097262009-12-11 02:10:03 +00002102 SourceLocation());
2103 return;
2104 }
2105 }
2106
Aaron Ballmane7c544d2014-08-04 20:28:35 +00002107 // static_assert-declaration. A templated static_assert declaration is
2108 // diagnosed in Parser::ParseSingleDeclarationAfterTemplate.
2109 if (!TemplateInfo.Kind &&
2110 (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert))) {
Chris Lattner49836b42009-04-02 04:16:50 +00002111 SourceLocation DeclEnd;
2112 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002113 return;
2114 }
Mike Stump11289f42009-09-09 15:08:12 +00002115
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002116 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00002117 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00002118 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00002119 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00002120 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002121 AS, AccessAttrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002122 return;
2123 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002124
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002125 // Handle: member-declaration ::= '__extension__' member-declaration
2126 if (Tok.is(tok::kw___extension__)) {
2127 // __extension__ silences extension warnings in the subexpression.
2128 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2129 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002130 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2131 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002132 }
Douglas Gregorfec52632009-06-20 00:51:54 +00002133
John McCall084e83d2011-03-24 11:26:52 +00002134 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00002135 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002136 // Optional C++11 attribute-specifier
2137 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00002138 // We need to keep these attributes for future diagnostic
2139 // before they are taken over by declaration specifier.
2140 FnAttrs.addAll(attrs.getList());
2141 FnAttrs.Range = attrs.Range;
2142
John McCall53fa7142010-12-24 02:08:15 +00002143 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002144
Douglas Gregorfec52632009-06-20 00:51:54 +00002145 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00002146 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002147
Douglas Gregorfec52632009-06-20 00:51:54 +00002148 // Eat 'using'.
2149 SourceLocation UsingLoc = ConsumeToken();
2150
2151 if (Tok.is(tok::kw_namespace)) {
2152 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002153 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002154 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00002155 SourceLocation DeclEnd;
Richard Smith3f1b5d02011-05-05 21:57:07 +00002156 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +00002157 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2158 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00002159 }
2160 return;
2161 }
2162
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002163 // Hold late-parsed attributes so we can attach a Decl to them later.
2164 LateParsedAttrList CommonLateParsedAttrs;
2165
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002166 // decl-specifier-seq:
2167 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00002168 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00002169 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00002170 if (MalformedTypeSpec)
2171 DS.SetTypeSpecError();
Richard Smith72553fc2014-01-23 23:53:27 +00002172
Serge Pavlov458ea762014-07-16 05:16:52 +00002173 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2174 &CommonLateParsedAttrs);
2175
2176 // Turn off colon protection that was set for declspec.
2177 X.restore();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002178
Richard Smith404dfb42013-11-19 22:47:36 +00002179 // If we had a free-standing type definition with a missing semicolon, we
2180 // may get this far before the problem becomes obvious.
2181 if (DS.hasTagDefinition() &&
2182 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2183 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2184 &CommonLateParsedAttrs))
2185 return;
2186
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002187 MultiTemplateParamsArg TemplateParams(
Craig Topper161e4db2014-05-21 06:02:52 +00002188 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data()
2189 : nullptr,
John McCall11083da2009-09-16 22:47:08 +00002190 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2191
Alp Toker35d87032013-12-30 23:29:50 +00002192 if (TryConsumeToken(tok::semi)) {
Michael Handdc016d2012-11-28 23:17:40 +00002193 if (DS.isFriendSpecified())
2194 ProhibitAttributes(FnAttrs);
2195
John McCall48871652010-08-21 09:40:31 +00002196 Decl *TheDecl =
Chandler Carruth7c9856d2011-05-03 18:35:10 +00002197 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCall796c2a52010-07-16 08:13:16 +00002198 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00002199 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002200 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002201
John McCall28a6aea2009-11-04 02:18:39 +00002202 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002203 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002204
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002205 // Hold late-parsed attributes so we can attach a Decl to them later.
2206 LateParsedAttrList LateParsedAttrs;
2207
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002208 SourceLocation EqualLoc;
2209 bool HasInitializer = false;
2210 ExprResult Init;
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002211
Richard Smith72553fc2014-01-23 23:53:27 +00002212 SmallVector<Decl *, 8> DeclsInGroup;
2213 ExprResult BitfieldSize;
2214 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002215
Richard Smith72553fc2014-01-23 23:53:27 +00002216 // Parse the first declarator.
2217 ParseCXXMemberDeclaratorBeforeInitializer(DeclaratorInfo, VS, BitfieldSize,
2218 LateParsedAttrs);
Nico Weber24b2a822011-01-28 06:07:34 +00002219
Richard Smith72553fc2014-01-23 23:53:27 +00002220 // If this has neither a name nor a bit width, something has gone seriously
2221 // wrong. Skip until the semi-colon or }.
Richard Smith4b5a9492014-01-24 22:34:35 +00002222 if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002223 // If so, skip until the semi-colon or a }.
2224 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2225 TryConsumeToken(tok::semi);
2226 return;
2227 }
John Thompson5bc5cbe2009-11-25 22:58:06 +00002228
Richard Smith72553fc2014-01-23 23:53:27 +00002229 // Check for a member function definition.
Richard Smith4b5a9492014-01-24 22:34:35 +00002230 if (BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002231 // MSVC permits pure specifier on inline functions defined at class scope.
Francois Pichet3abc9b82011-05-11 02:14:46 +00002232 // Hence check for =0 before checking for function definition.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002233 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Richard Smith72553fc2014-01-23 23:53:27 +00002234 DeclaratorInfo.isFunctionDeclarator() &&
Francois Pichet3abc9b82011-05-11 02:14:46 +00002235 NextToken().is(tok::numeric_constant)) {
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002236 EqualLoc = ConsumeToken();
Francois Pichet3abc9b82011-05-11 02:14:46 +00002237 Init = ParseInitializer();
2238 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002239 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002240 else
2241 HasInitializer = true;
Francois Pichet3abc9b82011-05-11 02:14:46 +00002242 }
2243
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002244 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002245 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002246 //
2247 // In C++11, a non-function declarator followed by an open brace is a
2248 // braced-init-list for an in-class member initialization, not an
2249 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002250 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002251 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002252 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002253 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002254 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002255 } else if (Tok.is(tok::equal)) {
2256 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002257 if (KW.is(tok::kw_default))
2258 DefinitionKind = FDK_Defaulted;
2259 else if (KW.is(tok::kw_delete))
2260 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002261 }
2262 }
2263
Michael Handdc016d2012-11-28 23:17:40 +00002264 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2265 // to a friend declaration, that declaration shall be a definition.
2266 if (DeclaratorInfo.isFunctionDeclarator() &&
2267 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2268 // Diagnose attributes that appear before decl specifier:
2269 // [[]] friend int foo();
2270 ProhibitAttributes(FnAttrs);
2271 }
2272
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002273 if (DefinitionKind) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002274 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002275 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002276 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002277 SkipUntil(tok::r_brace);
Michael Handdc016d2012-11-28 23:17:40 +00002278
Douglas Gregor8a4db832011-01-19 16:41:58 +00002279 // Consume the optional ';'
Alp Toker35d87032013-12-30 23:29:50 +00002280 TryConsumeToken(tok::semi);
2281
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002282 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002283 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002284
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002285 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002286 Diag(DeclaratorInfo.getIdentifierLoc(),
2287 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002288
Richard Smith2603b092012-11-15 22:54:20 +00002289 // Recover by treating the 'typedef' as spurious.
2290 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002291 }
2292
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002293 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002294 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002295 VS, DefinitionKind, Init);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002296
David Majnemer23252a32013-08-01 04:22:55 +00002297 if (FunDecl) {
2298 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2299 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2300 }
2301 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2302 LateParsedAttrs[i]->addDecl(FunDecl);
2303 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002304 }
2305 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002306
2307 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002308 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002309 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002310
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002311 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002312 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002313 }
2314
2315 // member-declarator-list:
2316 // member-declarator
2317 // member-declarator-list ',' member-declarator
2318
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002319 while (1) {
Richard Smith2b013182012-06-10 03:12:00 +00002320 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002321 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith938f40b2011-06-11 17:19:42 +00002322 if (BitfieldSize.get()) {
2323 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002324 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith938f40b2011-06-11 17:19:42 +00002325 } else {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002326 HasInitializer = true;
Richard Smith2b013182012-06-10 03:12:00 +00002327 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2328 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smith2b013182012-06-10 03:12:00 +00002329 != DeclSpec::SCS_typedef)
2330 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002331 }
2332 }
2333
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002334 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002335 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002336 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002337
Craig Topper161e4db2014-05-21 06:02:52 +00002338 NamedDecl *ThisDecl = nullptr;
John McCall07e91c02009-08-06 02:15:43 +00002339 if (DS.isFriendSpecified()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002340 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
Michael Handdc016d2012-11-28 23:17:40 +00002341 // to a friend declaration, that declaration shall be a definition.
2342 //
Richard Smith72553fc2014-01-23 23:53:27 +00002343 // Diagnose attributes that appear in a friend member function declarator:
2344 // friend int foo [[]] ();
Michael Handdc016d2012-11-28 23:17:40 +00002345 SmallVector<SourceRange, 4> Ranges;
2346 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
Richard Smith72553fc2014-01-23 23:53:27 +00002347 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
2348 E = Ranges.end(); I != E; ++I)
2349 Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
Michael Handdc016d2012-11-28 23:17:40 +00002350
Douglas Gregor0be31a22010-07-02 17:43:08 +00002351 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002352 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002353 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002354 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002355 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002356 TemplateParams,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002357 BitfieldSize.get(),
Richard Smith2b013182012-06-10 03:12:00 +00002358 VS, HasInClassInit);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002359
2360 if (VarTemplateDecl *VT =
Craig Topper161e4db2014-05-21 06:02:52 +00002361 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr)
Larisse Voufo39a1e502013-08-06 01:03:05 +00002362 // Re-direct this decl to refer to the templated decl so that we can
2363 // initialize it.
2364 ThisDecl = VT->getTemplatedDecl();
2365
David Majnemer23252a32013-08-01 04:22:55 +00002366 if (ThisDecl && AccessAttrs)
Richard Smithf8a75c32013-08-29 00:47:48 +00002367 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00002368 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002369
Douglas Gregor728d00b2011-10-10 14:49:18 +00002370 // Handle the initializer.
David Blaikie35506f82013-01-30 01:22:18 +00002371 if (HasInClassInit != ICIS_NoInit &&
2372 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2373 DeclSpec::SCS_static) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002374 // The initializer was deferred; parse it and cache the tokens.
David Majnemer23252a32013-08-01 04:22:55 +00002375 Diag(Tok, getLangOpts().CPlusPlus11
2376 ? diag::warn_cxx98_compat_nonstatic_member_init
2377 : diag::ext_nonstatic_member_init);
Richard Smith5d164bc2011-10-15 05:09:34 +00002378
Richard Smith938f40b2011-06-11 17:19:42 +00002379 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002380 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2381 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002382 //
2383 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002384 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002385 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002386 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemer23252a32013-08-01 04:22:55 +00002387
2388 // Avoid later warnings about a class member of incomplete type.
David Blaikiecdd91db2012-02-14 09:00:46 +00002389 if (ThisDecl)
David Blaikiecdd91db2012-02-14 09:00:46 +00002390 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002391 } else
2392 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002393 } else if (HasInitializer) {
2394 // Normal initializer.
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002395 if (!Init.isUsable())
David Majnemer23252a32013-08-01 04:22:55 +00002396 Init = ParseCXXMemberInitializer(
2397 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2398
Douglas Gregor728d00b2011-10-10 14:49:18 +00002399 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002400 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002401 else if (ThisDecl)
Sebastian Redleef474c2012-02-22 10:50:08 +00002402 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smith74aeef52013-04-26 16:15:35 +00002403 DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002404 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor728d00b2011-10-10 14:49:18 +00002405 // No initializer.
Richard Smith74aeef52013-04-26 16:15:35 +00002406 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002407
Douglas Gregor728d00b2011-10-10 14:49:18 +00002408 if (ThisDecl) {
David Majnemer23252a32013-08-01 04:22:55 +00002409 if (!ThisDecl->isInvalidDecl()) {
2410 // Set the Decl for any late parsed attributes
2411 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2412 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2413
2414 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2415 LateParsedAttrs[i]->addDecl(ThisDecl);
2416 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002417 Actions.FinalizeDeclaration(ThisDecl);
2418 DeclsInGroup.push_back(ThisDecl);
David Majnemer23252a32013-08-01 04:22:55 +00002419
2420 if (DeclaratorInfo.isFunctionDeclarator() &&
2421 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2422 DeclSpec::SCS_typedef)
2423 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002424 }
David Majnemer23252a32013-08-01 04:22:55 +00002425 LateParsedAttrs.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002426
2427 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002428
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002429 // If we don't have a comma, it is either the end of the list (a ';')
2430 // or an error, bail out.
Alp Toker094e5212014-01-05 03:27:11 +00002431 SourceLocation CommaLoc;
2432 if (!TryConsumeToken(tok::comma, CommaLoc))
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002433 break;
Mike Stump11289f42009-09-09 15:08:12 +00002434
Richard Smithc8a79032012-01-09 22:31:44 +00002435 if (Tok.isAtStartOfLine() &&
2436 !MightBeDeclarator(Declarator::MemberContext)) {
2437 // This comma was followed by a line-break and something which can't be
2438 // the start of a declarator. The comma was probably a typo for a
2439 // semicolon.
2440 Diag(CommaLoc, diag::err_expected_semi_declaration)
2441 << FixItHint::CreateReplacement(CommaLoc, ";");
2442 ExpectSemi = false;
2443 break;
2444 }
Mike Stump11289f42009-09-09 15:08:12 +00002445
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002446 // Parse the next declarator.
2447 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002448 VS.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002449 BitfieldSize = true;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002450 Init = true;
2451 HasInitializer = false;
Richard Smith8d06f422012-01-12 23:53:29 +00002452 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002453
Richard Smith72553fc2014-01-23 23:53:27 +00002454 // GNU attributes are allowed before the second and subsequent declarator.
John McCall53fa7142010-12-24 02:08:15 +00002455 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002456
Richard Smith72553fc2014-01-23 23:53:27 +00002457 ParseCXXMemberDeclaratorBeforeInitializer(DeclaratorInfo, VS, BitfieldSize,
2458 LateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002459 }
2460
Richard Smithc8a79032012-01-09 22:31:44 +00002461 if (ExpectSemi &&
2462 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002463 // Skip to end of block or statement.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002464 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002465 // If we stopped at a ';', eat it.
Alp Toker35d87032013-12-30 23:29:50 +00002466 TryConsumeToken(tok::semi);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002467 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002468 }
2469
Rafael Espindolaab417692013-07-09 12:05:01 +00002470 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002471}
2472
Richard Smith938f40b2011-06-11 17:19:42 +00002473/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2474/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2475/// function definition. The location of the '=', if any, will be placed in
2476/// EqualLoc.
2477///
2478/// pure-specifier:
2479/// '= 0'
Sebastian Redleef474c2012-02-22 10:50:08 +00002480///
Richard Smith938f40b2011-06-11 17:19:42 +00002481/// brace-or-equal-initializer:
2482/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002483/// braced-init-list
2484///
Richard Smith938f40b2011-06-11 17:19:42 +00002485/// initializer-clause:
2486/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002487/// braced-init-list
2488///
Richard Smithda35e962013-11-09 04:52:51 +00002489/// defaulted/deleted function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002490/// '=' 'default'
2491/// '=' 'delete'
2492///
2493/// Prior to C++0x, the assignment-expression in an initializer-clause must
2494/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002495ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002496 SourceLocation &EqualLoc) {
2497 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2498 && "Data member initializer not starting with '=' or '{'");
2499
Douglas Gregor926410d2012-02-21 02:22:07 +00002500 EnterExpressionEvaluationContext Context(Actions,
2501 Sema::PotentiallyEvaluated,
2502 D);
Alp Toker094e5212014-01-05 03:27:11 +00002503 if (TryConsumeToken(tok::equal, EqualLoc)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002504 if (Tok.is(tok::kw_delete)) {
2505 // In principle, an initializer of '= delete p;' is legal, but it will
2506 // never type-check. It's better to diagnose it as an ill-formed expression
2507 // than as an ill-formed deleted non-function member.
2508 // An initializer of '= delete p, foo' will never be parsed, because
2509 // a top-level comma always ends the initializer expression.
2510 const Token &Next = NextToken();
2511 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
Richard Smith34f30512013-11-23 04:06:09 +00002512 Next.is(tok::eof)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002513 if (IsFunction)
2514 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2515 << 1 /* delete */;
2516 else
2517 Diag(ConsumeToken(), diag::err_deleted_non_function);
Richard Smithedcb26e2014-06-11 00:49:52 +00002518 return ExprError();
Richard Smith938f40b2011-06-11 17:19:42 +00002519 }
2520 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002521 if (IsFunction)
2522 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2523 << 0 /* default */;
2524 else
2525 Diag(ConsumeToken(), diag::err_default_special_members);
Richard Smithedcb26e2014-06-11 00:49:52 +00002526 return ExprError();
Richard Smith938f40b2011-06-11 17:19:42 +00002527 }
2528
Sebastian Redleef474c2012-02-22 10:50:08 +00002529 }
2530 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002531}
2532
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002533/// ParseCXXMemberSpecification - Parse the class definition.
2534///
2535/// member-specification:
2536/// member-declaration member-specification[opt]
2537/// access-specifier ':' member-specification[opt]
2538///
Joao Matose9a3ed42012-08-31 22:18:20 +00002539void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00002540 SourceLocation AttrFixitLoc,
Richard Smith4c96e992013-02-19 23:47:15 +00002541 ParsedAttributesWithRange &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00002542 unsigned TagType, Decl *TagDecl) {
2543 assert((TagType == DeclSpec::TST_struct ||
2544 TagType == DeclSpec::TST_interface ||
2545 TagType == DeclSpec::TST_union ||
2546 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2547
John McCallfaf5fb42010-08-26 23:41:50 +00002548 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2549 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00002550
Douglas Gregoredf8f392010-01-16 20:52:59 +00002551 // Determine whether this is a non-nested class. Note that local
2552 // classes are *not* considered to be nested classes.
2553 bool NonNestedClass = true;
2554 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002555 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002556 if (S->isClassScope()) {
2557 // We're inside a class scope, so this is a nested class.
2558 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00002559
2560 // The Microsoft extension __interface does not permit nested classes.
2561 if (getCurrentClass().IsInterface) {
2562 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2563 << /*ErrorType=*/6
2564 << (isa<NamedDecl>(TagDecl)
2565 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
David Blaikieabe1a392014-04-02 05:58:29 +00002566 : "(anonymous)");
John McCalldb632ac2012-09-25 07:32:39 +00002567 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00002568 break;
2569 }
2570
2571 if ((S->getFlags() & Scope::FnScope)) {
2572 // If we're in a function or function template declared in the
2573 // body of a class, then this is a local class rather than a
2574 // nested class.
2575 const Scope *Parent = S->getParent();
2576 if (Parent->isTemplateParamScope())
2577 Parent = Parent->getParent();
2578 if (Parent->isClassScope())
2579 break;
2580 }
2581 }
2582 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002583
2584 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00002585 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002586
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002587 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00002588 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2589 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002590
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002591 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002592 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002593
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002594 SourceLocation FinalLoc;
David Majnemera5433082013-10-18 00:33:31 +00002595 bool IsFinalSpelledSealed = false;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002596
2597 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002598 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemera5433082013-10-18 00:33:31 +00002599 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2600 assert((Specifier == VirtSpecifiers::VS_Final ||
2601 Specifier == VirtSpecifiers::VS_Sealed) &&
2602 "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00002603 FinalLoc = ConsumeToken();
David Majnemera5433082013-10-18 00:33:31 +00002604 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002605
David Majnemera5433082013-10-18 00:33:31 +00002606 if (TagType == DeclSpec::TST_interface)
John McCalldb632ac2012-09-25 07:32:39 +00002607 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemera5433082013-10-18 00:33:31 +00002608 << VirtSpecifiers::getSpecifierName(Specifier);
2609 else if (Specifier == VirtSpecifiers::VS_Final)
2610 Diag(FinalLoc, getLangOpts().CPlusPlus11
2611 ? diag::warn_cxx98_compat_override_control_keyword
2612 : diag::ext_override_control_keyword)
2613 << VirtSpecifiers::getSpecifierName(Specifier);
2614 else if (Specifier == VirtSpecifiers::VS_Sealed)
2615 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Michael Han9407e502012-11-26 22:54:45 +00002616
Michael Han309af292013-01-07 16:57:11 +00002617 // Parse any C++11 attributes after 'final' keyword.
2618 // These attributes are not allowed to appear here,
2619 // and the only possible place for them to appertain
2620 // to the class would be between class-key and class-name.
Richard Smith4c96e992013-02-19 23:47:15 +00002621 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002622 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002623
John McCall2d814c32009-12-19 21:48:58 +00002624 if (Tok.is(tok::colon)) {
2625 ParseBaseClause(TagDecl);
2626
2627 if (!Tok.is(tok::l_brace)) {
2628 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCall2ff380a2010-03-17 00:38:33 +00002629
2630 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002631 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002632 return;
2633 }
2634 }
2635
2636 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002637 BalancedDelimiterTracker T(*this, tok::l_brace);
2638 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00002639
John McCall08bede42010-05-28 08:11:17 +00002640 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00002641 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemera5433082013-10-18 00:33:31 +00002642 IsFinalSpelledSealed,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002643 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00002644
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002645 // C++ 11p3: Members of a class defined with the keyword class are private
2646 // by default. Members of a class defined with the keywords struct or union
2647 // are public by default.
2648 AccessSpecifier CurAS;
2649 if (TagType == DeclSpec::TST_class)
2650 CurAS = AS_private;
2651 else
2652 CurAS = AS_public;
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002653 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002654
Douglas Gregor9377c822010-06-21 22:31:09 +00002655 if (TagDecl) {
2656 // While we still have something to read, read the member-declarations.
Richard Smith34f30512013-11-23 04:06:09 +00002657 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Douglas Gregor9377c822010-06-21 22:31:09 +00002658 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002659
David Blaikiebbafb8a2012-03-11 07:00:24 +00002660 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet8f981d52011-05-25 10:19:49 +00002661 Tok.is(tok::kw___if_not_exists))) {
2662 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2663 continue;
2664 }
2665
Douglas Gregor9377c822010-06-21 22:31:09 +00002666 // Check for extraneous top-level semicolon.
2667 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002668 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor9377c822010-06-21 22:31:09 +00002669 continue;
2670 }
2671
Eli Friedmanec52f922012-02-23 23:47:16 +00002672 if (Tok.is(tok::annot_pragma_vis)) {
2673 HandlePragmaVisibility();
2674 continue;
2675 }
2676
2677 if (Tok.is(tok::annot_pragma_pack)) {
2678 HandlePragmaPack();
2679 continue;
2680 }
2681
Argyrios Kyrtzidis5c2021b2012-10-12 17:39:59 +00002682 if (Tok.is(tok::annot_pragma_align)) {
2683 HandlePragmaAlign();
2684 continue;
2685 }
2686
Alexey Bataeva769e072013-03-22 06:34:35 +00002687 if (Tok.is(tok::annot_pragma_openmp)) {
2688 ParseOpenMPDeclarativeDirective();
2689 continue;
2690 }
2691
David Majnemer4bb09802014-02-10 19:50:15 +00002692 if (Tok.is(tok::annot_pragma_ms_pointers_to_members)) {
2693 HandlePragmaMSPointersToMembers();
2694 continue;
2695 }
2696
Warren Huntc3b18962014-04-08 22:30:47 +00002697 if (Tok.is(tok::annot_pragma_ms_pragma)) {
2698 HandlePragmaMSPragma();
2699 continue;
2700 }
2701
Richard Smithda35e962013-11-09 04:52:51 +00002702 // If we see a namespace here, a close brace was missing somewhere.
2703 if (Tok.is(tok::kw_namespace)) {
Richard Smith2ac43ad2013-11-15 23:00:02 +00002704 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
Richard Smithda35e962013-11-09 04:52:51 +00002705 break;
2706 }
2707
Douglas Gregor9377c822010-06-21 22:31:09 +00002708 AccessSpecifier AS = getAccessSpecifierIfPresent();
2709 if (AS != AS_none) {
2710 // Current token is a C++ access specifier.
2711 CurAS = AS;
2712 SourceLocation ASLoc = Tok.getLocation();
David Blaikieeba32c22011-10-13 06:08:43 +00002713 unsigned TokLength = Tok.getLength();
Douglas Gregor9377c822010-06-21 22:31:09 +00002714 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002715 AccessAttrs.clear();
2716 MaybeParseGNUAttributes(AccessAttrs);
2717
David Blaikieeba32c22011-10-13 06:08:43 +00002718 SourceLocation EndLoc;
Alp Toker35d87032013-12-30 23:29:50 +00002719 if (TryConsumeToken(tok::colon, EndLoc)) {
2720 } else if (TryConsumeToken(tok::semi, EndLoc)) {
2721 Diag(EndLoc, diag::err_expected)
2722 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002723 } else {
2724 EndLoc = ASLoc.getLocWithOffset(TokLength);
Alp Toker35d87032013-12-30 23:29:50 +00002725 Diag(EndLoc, diag::err_expected)
2726 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002727 }
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002728
John McCalldb632ac2012-09-25 07:32:39 +00002729 // The Microsoft extension __interface does not permit non-public
2730 // access specifiers.
2731 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2732 Diag(ASLoc, diag::err_access_specifier_interface)
2733 << (CurAS == AS_protected);
2734 }
2735
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002736 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2737 AccessAttrs.getList())) {
2738 // found another attribute than only annotations
2739 AccessAttrs.clear();
2740 }
2741
Douglas Gregor9377c822010-06-21 22:31:09 +00002742 continue;
2743 }
2744
Douglas Gregor9377c822010-06-21 22:31:09 +00002745 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002746 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002747 }
2748
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002749 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00002750 } else {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002751 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002752 }
Mike Stump11289f42009-09-09 15:08:12 +00002753
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002754 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002755 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002756 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002757
John McCall08bede42010-05-28 08:11:17 +00002758 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002759 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002760 T.getOpenLocation(),
2761 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002762 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002763
Douglas Gregor433e0532012-04-16 18:27:27 +00002764 // C++11 [class.mem]p2:
2765 // Within the class member-specification, the class is regarded as complete
Richard Smith2331bbf2012-05-02 22:22:32 +00002766 // within function bodies, default arguments, and
Douglas Gregor433e0532012-04-16 18:27:27 +00002767 // brace-or-equal-initializers for non-static data members (including such
2768 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00002769 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002770 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00002771 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002772 // declarations and the lexed inline method definitions, along with any
2773 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00002774 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002775 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002776 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00002777
2778 // We've finished with all pending member declarations.
2779 Actions.ActOnFinishCXXMemberDecls();
2780
Richard Smith938f40b2011-06-11 17:19:42 +00002781 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002782 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00002783 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002784 }
2785
John McCall08bede42010-05-28 08:11:17 +00002786 if (TagDecl)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002787 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2788 T.getCloseLocation());
John McCall2ff380a2010-03-17 00:38:33 +00002789
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002790 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002791 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002792 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002793}
Douglas Gregore8381c02008-11-05 04:29:56 +00002794
Richard Smith2ac43ad2013-11-15 23:00:02 +00002795void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithda35e962013-11-09 04:52:51 +00002796 assert(Tok.is(tok::kw_namespace));
2797
2798 // FIXME: Suggest where the close brace should have gone by looking
2799 // at indentation changes within the definition body.
Richard Smith2ac43ad2013-11-15 23:00:02 +00002800 Diag(D->getLocation(),
2801 diag::err_missing_end_of_definition) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002802 Diag(Tok.getLocation(),
Richard Smith2ac43ad2013-11-15 23:00:02 +00002803 diag::note_missing_end_of_definition_before) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002804
2805 // Push '};' onto the token stream to recover.
2806 PP.EnterToken(Tok);
2807
2808 Tok.startToken();
2809 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
2810 Tok.setKind(tok::semi);
2811 PP.EnterToken(Tok);
2812
2813 Tok.setKind(tok::r_brace);
2814}
2815
Douglas Gregore8381c02008-11-05 04:29:56 +00002816/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2817/// which explicitly initializes the members or base classes of a
2818/// class (C++ [class.base.init]). For example, the three initializers
2819/// after the ':' in the Derived constructor below:
2820///
2821/// @code
2822/// class Base { };
2823/// class Derived : Base {
2824/// int x;
2825/// float f;
2826/// public:
2827/// Derived(float f) : Base(), x(17), f(f) { }
2828/// };
2829/// @endcode
2830///
Mike Stump11289f42009-09-09 15:08:12 +00002831/// [C++] ctor-initializer:
2832/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00002833///
Mike Stump11289f42009-09-09 15:08:12 +00002834/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00002835/// mem-initializer ...[opt]
2836/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00002837void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00002838 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2839
John Wiegley1c0675e2011-04-28 01:08:34 +00002840 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2841 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00002842 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002843
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002844 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002845 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002846
Douglas Gregore8381c02008-11-05 04:29:56 +00002847 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002848 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00002849 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2850 MemInitializers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002851 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002852 } else {
2853 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2854 if (!MemInit.isInvalid())
2855 MemInitializers.push_back(MemInit.get());
2856 else
2857 AnyErrors = true;
2858 }
2859
Douglas Gregore8381c02008-11-05 04:29:56 +00002860 if (Tok.is(tok::comma))
2861 ConsumeToken();
2862 else if (Tok.is(tok::l_brace))
2863 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00002864 // If the next token looks like a base or member initializer, assume that
2865 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00002866 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2867 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2868 Diag(Loc, diag::err_ctor_init_missing_comma)
2869 << FixItHint::CreateInsertion(Loc, ", ");
2870 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00002871 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alp Tokerec543272013-12-24 09:48:30 +00002872 Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
2873 << tok::comma;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002874 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregore8381c02008-11-05 04:29:56 +00002875 break;
2876 }
2877 } while (true);
2878
David Blaikie3fc2f912013-01-17 05:26:25 +00002879 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002880 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00002881}
2882
2883/// ParseMemInitializer - Parse a C++ member initializer, which is
2884/// part of a constructor initializer that explicitly initializes one
2885/// member or base class (C++ [class.base.init]). See
2886/// ParseConstructorInitializer for an example.
2887///
2888/// [C++] mem-initializer:
2889/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002890/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00002891///
Douglas Gregore8381c02008-11-05 04:29:56 +00002892/// [C++] mem-initializer-id:
2893/// '::'[opt] nested-name-specifier[opt] class-name
2894/// identifier
John McCall48871652010-08-21 09:40:31 +00002895Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00002896 // parse '::'[opt] nested-name-specifier[opt]
2897 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002898 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00002899 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002900 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002901 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00002902 if (TemplateId->Kind == TNK_Type_template ||
2903 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00002904 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002905 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00002906 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002907 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002908 }
David Blaikie186a8892012-01-24 06:03:59 +00002909 // Uses of decltype will already have been converted to annot_decltype by
2910 // ParseOptionalCXXScopeSpecifier at this point.
2911 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2912 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002913 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00002914 return true;
2915 }
Mike Stump11289f42009-09-09 15:08:12 +00002916
Craig Topper161e4db2014-05-21 06:02:52 +00002917 IdentifierInfo *II = nullptr;
David Blaikie186a8892012-01-24 06:03:59 +00002918 DeclSpec DS(AttrFactory);
2919 SourceLocation IdLoc = Tok.getLocation();
2920 if (Tok.is(tok::annot_decltype)) {
2921 // Get the decltype expression, if there is one.
2922 ParseDecltypeSpecifier(DS);
2923 } else {
2924 if (Tok.is(tok::identifier))
2925 // Get the identifier. This may be a member name or a class name,
2926 // but we'll let the semantic analysis determine which it is.
2927 II = Tok.getIdentifierInfo();
2928 ConsumeToken();
2929 }
2930
Douglas Gregore8381c02008-11-05 04:29:56 +00002931
2932 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002933 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002934 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2935
Sebastian Redla74948d2011-09-24 17:48:25 +00002936 ExprResult InitList = ParseBraceInitializer();
2937 if (InitList.isInvalid())
2938 return true;
2939
2940 SourceLocation EllipsisLoc;
Alp Toker094e5212014-01-05 03:27:11 +00002941 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redla74948d2011-09-24 17:48:25 +00002942
2943 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002944 TemplateTypeTy, DS, IdLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002945 InitList.get(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00002946 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002947 BalancedDelimiterTracker T(*this, tok::l_paren);
2948 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00002949
Sebastian Redl3da34892011-06-05 12:23:16 +00002950 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00002951 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00002952 CommaLocsTy CommaLocs;
2953 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002954 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00002955 return true;
2956 }
2957
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002958 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00002959
2960 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00002961 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00002962
2963 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002964 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenko139474d2013-05-09 23:51:52 +00002965 T.getOpenLocation(), ArgExprs,
2966 T.getCloseLocation(), EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00002967 }
2968
Alp Tokerec543272013-12-24 09:48:30 +00002969 if (getLangOpts().CPlusPlus11)
2970 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
2971 else
2972 return Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregore8381c02008-11-05 04:29:56 +00002973}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002974
Sebastian Redl965b0e32011-03-05 14:45:16 +00002975/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002976///
Douglas Gregor356513d2008-12-01 18:00:20 +00002977/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00002978/// dynamic-exception-specification
2979/// noexcept-specification
2980///
2981/// noexcept-specification:
2982/// 'noexcept'
2983/// 'noexcept' '(' constant-expression ')'
2984ExceptionSpecificationType
Richard Smith2331bbf2012-05-02 22:22:32 +00002985Parser::tryParseExceptionSpecification(
Douglas Gregor433e0532012-04-16 18:27:27 +00002986 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002987 SmallVectorImpl<ParsedType> &DynamicExceptions,
2988 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00002989 ExprResult &NoexceptExpr) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002990 ExceptionSpecificationType Result = EST_None;
2991
2992 // See if there's a dynamic specification.
2993 if (Tok.is(tok::kw_throw)) {
2994 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2995 DynamicExceptions,
2996 DynamicExceptionRanges);
2997 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2998 "Produced different number of exception types and ranges.");
2999 }
3000
3001 // If there's no noexcept specification, we're done.
3002 if (Tok.isNot(tok::kw_noexcept))
3003 return Result;
3004
Richard Smithb15c11c2011-10-17 23:06:20 +00003005 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3006
Sebastian Redl965b0e32011-03-05 14:45:16 +00003007 // If we already had a dynamic specification, parse the noexcept for,
3008 // recovery, but emit a diagnostic and don't store the results.
3009 SourceRange NoexceptRange;
3010 ExceptionSpecificationType NoexceptType = EST_None;
3011
3012 SourceLocation KeywordLoc = ConsumeToken();
3013 if (Tok.is(tok::l_paren)) {
3014 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003015 BalancedDelimiterTracker T(*this, tok::l_paren);
3016 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003017 NoexceptType = EST_ComputedNoexcept;
3018 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003019 // The argument must be contextually convertible to bool. We use
3020 // ActOnBooleanCondition for this purpose.
3021 if (!NoexceptExpr.isInvalid())
3022 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
3023 NoexceptExpr.get());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003024 T.consumeClose();
3025 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl965b0e32011-03-05 14:45:16 +00003026 } else {
3027 // There is no argument.
3028 NoexceptType = EST_BasicNoexcept;
3029 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
3030 }
3031
3032 if (Result == EST_None) {
3033 SpecificationRange = NoexceptRange;
3034 Result = NoexceptType;
3035
3036 // If there's a dynamic specification after a noexcept specification,
3037 // parse that and ignore the results.
3038 if (Tok.is(tok::kw_throw)) {
3039 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3040 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
3041 DynamicExceptionRanges);
3042 }
3043 } else {
3044 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3045 }
3046
3047 return Result;
3048}
3049
Richard Smith8ca78a12013-06-13 02:02:51 +00003050static void diagnoseDynamicExceptionSpecification(
3051 Parser &P, const SourceRange &Range, bool IsNoexcept) {
3052 if (P.getLangOpts().CPlusPlus11) {
3053 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
3054 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
3055 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
3056 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
3057 }
3058}
3059
Sebastian Redl965b0e32011-03-05 14:45:16 +00003060/// ParseDynamicExceptionSpecification - Parse a C++
3061/// dynamic-exception-specification (C++ [except.spec]).
3062///
3063/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00003064/// 'throw' '(' type-id-list [opt] ')'
3065/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00003066///
Douglas Gregor356513d2008-12-01 18:00:20 +00003067/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00003068/// type-id ... [opt]
3069/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003070///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003071ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
3072 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003073 SmallVectorImpl<ParsedType> &Exceptions,
3074 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003075 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00003076
Sebastian Redl965b0e32011-03-05 14:45:16 +00003077 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003078 BalancedDelimiterTracker T(*this, tok::l_paren);
3079 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003080 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3081 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003082 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003083 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003084
Douglas Gregor356513d2008-12-01 18:00:20 +00003085 // Parse throw(...), a Microsoft extension that means "this function
3086 // can throw anything".
3087 if (Tok.is(tok::ellipsis)) {
3088 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003089 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00003090 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003091 T.consumeClose();
3092 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003093 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003094 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00003095 }
3096
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003097 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00003098 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003099 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00003100 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00003101
Douglas Gregor830837d2010-12-20 23:57:46 +00003102 if (Tok.is(tok::ellipsis)) {
3103 // C++0x [temp.variadic]p5:
3104 // - In a dynamic-exception-specification (15.4); the pattern is a
3105 // type-id.
3106 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003107 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00003108 if (!Res.isInvalid())
3109 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3110 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003111
Sebastian Redld6434562009-05-29 18:02:33 +00003112 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003113 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00003114 Ranges.push_back(Range);
3115 }
Alp Toker97650562014-01-10 11:19:30 +00003116
3117 if (!TryConsumeToken(tok::comma))
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003118 break;
3119 }
3120
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003121 T.consumeClose();
3122 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003123 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3124 Exceptions.empty());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003125 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003126}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003127
Douglas Gregor7fb25412010-10-01 18:44:50 +00003128/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3129/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003130TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00003131 assert(Tok.is(tok::arrow) && "expected arrow");
3132
3133 ConsumeToken();
3134
Richard Smithbfdb1082012-03-12 08:56:40 +00003135 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00003136}
3137
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003138/// \brief We have just started parsing the definition of a new class,
3139/// so push that class onto our stack of classes that is currently
3140/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00003141Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00003142Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3143 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003144 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003145 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00003146 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00003147 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003148}
3149
3150/// \brief Deallocate the given parsed class and all of its nested
3151/// classes.
3152void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00003153 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3154 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003155 delete Class;
3156}
3157
3158/// \brief Pop the top class of the stack of classes that are
3159/// currently being parsed.
3160///
3161/// This routine should be called when we have finished parsing the
3162/// definition of a class, but have not yet popped the Scope
3163/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00003164void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003165 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00003166
John McCallc1465822011-02-14 07:13:47 +00003167 Actions.PopParsingClass(state);
3168
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003169 ParsingClass *Victim = ClassStack.top();
3170 ClassStack.pop();
3171 if (Victim->TopLevelClass) {
3172 // Deallocate all of the nested classes of this class,
3173 // recursively: we don't need to keep any of this information.
3174 DeallocateParsedClasses(Victim);
3175 return;
Mike Stump11289f42009-09-09 15:08:12 +00003176 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003177 assert(!ClassStack.empty() && "Missing top-level class?");
3178
Douglas Gregorefc46952010-10-12 16:25:54 +00003179 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003180 // The victim is a nested class, but we will not need to perform
3181 // any processing after the definition of this class since it has
3182 // no members whose handling was delayed. Therefore, we can just
3183 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00003184 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003185 return;
3186 }
3187
3188 // This nested class has some members that will need to be processed
3189 // after the top-level class is completely defined. Therefore, add
3190 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003191 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00003192 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00003193 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003194}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003195
Richard Smith3dff2512012-04-10 03:25:07 +00003196/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3197///
3198/// \return the parsed identifier on success, and 0 if the next token is not an
3199/// attribute-token.
3200///
3201/// C++11 [dcl.attr.grammar]p3:
3202/// If a keyword or an alternative token that satisfies the syntactic
3203/// requirements of an identifier is contained in an attribute-token,
3204/// it is considered an identifier.
3205IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3206 switch (Tok.getKind()) {
3207 default:
3208 // Identifiers and keywords have identifier info attached.
3209 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3210 Loc = ConsumeToken();
3211 return II;
3212 }
Craig Topper161e4db2014-05-21 06:02:52 +00003213 return nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003214
3215 case tok::ampamp: // 'and'
3216 case tok::pipe: // 'bitor'
3217 case tok::pipepipe: // 'or'
3218 case tok::caret: // 'xor'
3219 case tok::tilde: // 'compl'
3220 case tok::amp: // 'bitand'
3221 case tok::ampequal: // 'and_eq'
3222 case tok::pipeequal: // 'or_eq'
3223 case tok::caretequal: // 'xor_eq'
3224 case tok::exclaim: // 'not'
3225 case tok::exclaimequal: // 'not_eq'
3226 // Alternative tokens do not have identifier info, but their spelling
3227 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003228 SmallString<8> SpellingBuf;
Richard Smith3dff2512012-04-10 03:25:07 +00003229 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rosea7d03842013-02-08 22:30:41 +00003230 if (isLetter(Spelling[0])) {
Richard Smith3dff2512012-04-10 03:25:07 +00003231 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003232 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003233 }
Craig Topper161e4db2014-05-21 06:02:52 +00003234 return nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003235 }
3236}
3237
Michael Han23214e52012-10-03 01:56:22 +00003238static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3239 IdentifierInfo *ScopeName) {
3240 switch (AttributeList::getKind(AttrName, ScopeName,
3241 AttributeList::AS_CXX11)) {
3242 case AttributeList::AT_CarriesDependency:
Aaron Ballman35f94212014-04-14 16:03:22 +00003243 case AttributeList::AT_Deprecated:
Michael Han23214e52012-10-03 01:56:22 +00003244 case AttributeList::AT_FallThrough:
Richard Smith10876ef2013-01-17 01:30:42 +00003245 case AttributeList::AT_CXX11NoReturn: {
Michael Han23214e52012-10-03 01:56:22 +00003246 return true;
3247 }
3248
3249 default:
3250 return false;
3251 }
3252}
3253
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003254/// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause.
3255///
3256/// [C++11] attribute-argument-clause:
3257/// '(' balanced-token-seq ')'
3258///
3259/// [C++11] balanced-token-seq:
3260/// balanced-token
3261/// balanced-token-seq balanced-token
3262///
3263/// [C++11] balanced-token:
3264/// '(' balanced-token-seq ')'
3265/// '[' balanced-token-seq ']'
3266/// '{' balanced-token-seq '}'
3267/// any token but '(', ')', '[', ']', '{', or '}'
3268bool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName,
3269 SourceLocation AttrNameLoc,
3270 ParsedAttributes &Attrs,
3271 SourceLocation *EndLoc,
3272 IdentifierInfo *ScopeName,
3273 SourceLocation ScopeLoc) {
3274 assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
Aaron Ballman35f94212014-04-14 16:03:22 +00003275 SourceLocation LParenLoc = Tok.getLocation();
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003276
3277 // If the attribute isn't known, we will not attempt to parse any
3278 // arguments.
3279 if (!hasAttribute(AttrSyntax::CXX, ScopeName, AttrName,
3280 getTargetInfo().getTriple(), getLangOpts())) {
3281 // Eat the left paren, then skip to the ending right paren.
3282 ConsumeParen();
3283 SkipUntil(tok::r_paren);
3284 return false;
3285 }
3286
3287 if (ScopeName && ScopeName->getName() == "gnu")
3288 // GNU-scoped attributes have some special cases to handle GNU-specific
3289 // behaviors.
3290 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
Craig Topper161e4db2014-05-21 06:02:52 +00003291 ScopeLoc, AttributeList::AS_CXX11, nullptr);
Aaron Ballman35f94212014-04-14 16:03:22 +00003292 else {
3293 unsigned NumArgs =
3294 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
3295 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3296
3297 const AttributeList *Attr = Attrs.getList();
3298 if (Attr && IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) {
3299 // If the attribute is a standard or built-in attribute and we are
3300 // parsing an argument list, we need to determine whether this attribute
3301 // was allowed to have an argument list (such as [[deprecated]]), and how
3302 // many arguments were parsed (so we can diagnose on [[deprecated()]]).
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003303 if (Attr->getMaxArgs() && !NumArgs) {
3304 // The attribute was allowed to have arguments, but none were provided
3305 // even though the attribute parsed successfully. This is an error.
3306 // FIXME: This is a good place for a fixit which removes the parens.
3307 Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName;
3308 return false;
3309 } else if (!Attr->getMaxArgs()) {
3310 // The attribute parsed successfully, but was not allowed to have any
3311 // arguments. It doesn't matter whether any were provided -- the
Aaron Ballman35f94212014-04-14 16:03:22 +00003312 // presence of the argument list (even if empty) is diagnosed.
3313 Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments)
3314 << AttrName;
3315 return false;
3316 }
3317 }
3318 }
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003319 return true;
3320}
3321
3322/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003323///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003324/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003325/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003326/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003327///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003328/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003329/// attribute[opt]
3330/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003331/// attribute '...'
3332/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003333///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003334/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003335/// attribute-token attribute-argument-clause[opt]
3336///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003337/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003338/// identifier
3339/// attribute-scoped-token
3340///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003341/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003342/// attribute-namespace '::' identifier
3343///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003344/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003345/// identifier
Richard Smith3dff2512012-04-10 03:25:07 +00003346void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003347 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003348 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003349 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003350 ParseAlignmentSpecifier(attrs, endLoc);
3351 return;
3352 }
3353
Alexis Hunt96d5c762009-11-21 08:43:09 +00003354 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003355 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003356
Richard Smithf679b5b2011-10-14 20:48:27 +00003357 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3358
Alexis Hunt96d5c762009-11-21 08:43:09 +00003359 ConsumeBracket();
3360 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003361
Richard Smith10876ef2013-01-17 01:30:42 +00003362 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3363
Richard Smith3dff2512012-04-10 03:25:07 +00003364 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003365 // attribute not present
Alp Toker97650562014-01-10 11:19:30 +00003366 if (TryConsumeToken(tok::comma))
Alexis Hunt96d5c762009-11-21 08:43:09 +00003367 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003368
Richard Smith3dff2512012-04-10 03:25:07 +00003369 SourceLocation ScopeLoc, AttrLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00003370 IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003371
3372 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3373 if (!AttrName)
3374 // Break out to the "expected ']'" diagnostic.
3375 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003376
Alexis Hunt96d5c762009-11-21 08:43:09 +00003377 // scoped attribute
Alp Toker97650562014-01-10 11:19:30 +00003378 if (TryConsumeToken(tok::coloncolon)) {
Richard Smith3dff2512012-04-10 03:25:07 +00003379 ScopeName = AttrName;
3380 ScopeLoc = AttrLoc;
3381
3382 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3383 if (!AttrName) {
Alp Tokerec543272013-12-24 09:48:30 +00003384 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003385 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003386 continue;
3387 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003388 }
3389
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003390 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003391 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003392
Richard Smith10876ef2013-01-17 01:30:42 +00003393 if (StandardAttr &&
3394 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3395 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003396 << AttrName << SourceRange(SeenAttrs[AttrName]);
Richard Smith10876ef2013-01-17 01:30:42 +00003397
Michael Han23214e52012-10-03 01:56:22 +00003398 // Parse attribute arguments
Aaron Ballman35f94212014-04-14 16:03:22 +00003399 if (Tok.is(tok::l_paren))
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003400 AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3401 ScopeName, ScopeLoc);
Michael Han23214e52012-10-03 01:56:22 +00003402
3403 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003404 attrs.addNew(AttrName,
3405 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3406 AttrLoc),
Craig Topper161e4db2014-05-21 06:02:52 +00003407 ScopeName, ScopeLoc, nullptr, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003408
Alp Toker97650562014-01-10 11:19:30 +00003409 if (TryConsumeToken(tok::ellipsis))
Michael Han23214e52012-10-03 01:56:22 +00003410 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3411 << AttrName->getName();
Alexis Hunt96d5c762009-11-21 08:43:09 +00003412 }
3413
Alp Toker383d2c42014-01-01 03:08:43 +00003414 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003415 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003416 if (endLoc)
3417 *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003418 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003419 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003420}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003421
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003422/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003423///
3424/// attribute-specifier-seq:
3425/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003426void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003427 SourceLocation *endLoc) {
Richard Smith4cabd042013-02-22 09:15:49 +00003428 assert(getLangOpts().CPlusPlus11);
3429
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003430 SourceLocation StartLoc = Tok.getLocation(), Loc;
3431 if (!endLoc)
3432 endLoc = &Loc;
3433
Douglas Gregor6f981002011-10-07 20:35:25 +00003434 do {
Richard Smith3dff2512012-04-10 03:25:07 +00003435 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003436 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003437
3438 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003439}
3440
Richard Smithc2c8bb82013-10-15 01:34:54 +00003441void Parser::DiagnoseAndSkipCXX11Attributes() {
Richard Smithc2c8bb82013-10-15 01:34:54 +00003442 // Start and end location of an attribute or an attribute list.
3443 SourceLocation StartLoc = Tok.getLocation();
Richard Smith955bf012014-06-19 11:42:00 +00003444 SourceLocation EndLoc = SkipCXX11Attributes();
3445
3446 if (EndLoc.isValid()) {
3447 SourceRange Range(StartLoc, EndLoc);
3448 Diag(StartLoc, diag::err_attributes_not_allowed)
3449 << Range;
3450 }
3451}
3452
3453SourceLocation Parser::SkipCXX11Attributes() {
Richard Smithc2c8bb82013-10-15 01:34:54 +00003454 SourceLocation EndLoc;
3455
Richard Smith955bf012014-06-19 11:42:00 +00003456 if (!isCXX11AttributeSpecifier())
3457 return EndLoc;
3458
Richard Smithc2c8bb82013-10-15 01:34:54 +00003459 do {
3460 if (Tok.is(tok::l_square)) {
3461 BalancedDelimiterTracker T(*this, tok::l_square);
3462 T.consumeOpen();
3463 T.skipToEnd();
3464 EndLoc = T.getCloseLocation();
3465 } else {
3466 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3467 ConsumeToken();
3468 BalancedDelimiterTracker T(*this, tok::l_paren);
3469 if (!T.consumeOpen())
3470 T.skipToEnd();
3471 EndLoc = T.getCloseLocation();
3472 }
3473 } while (isCXX11AttributeSpecifier());
3474
Richard Smith955bf012014-06-19 11:42:00 +00003475 return EndLoc;
Richard Smithc2c8bb82013-10-15 01:34:54 +00003476}
3477
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003478/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3479///
3480/// [MS] ms-attribute:
3481/// '[' token-seq ']'
3482///
3483/// [MS] ms-attribute-seq:
3484/// ms-attribute[opt]
3485/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00003486void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3487 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003488 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3489
3490 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003491 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003492 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003493 SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
John McCall53fa7142010-12-24 02:08:15 +00003494 if (endLoc) *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003495 ExpectAndConsume(tok::r_square);
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003496 }
3497}
Francois Pichet8f981d52011-05-25 10:19:49 +00003498
3499void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3500 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00003501 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00003502 if (ParseMicrosoftIfExistsCondition(Result))
3503 return;
3504
Douglas Gregor43edb322011-10-24 22:31:10 +00003505 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3506 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00003507 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet8f981d52011-05-25 10:19:49 +00003508 return;
3509 }
Francois Pichet8f981d52011-05-25 10:19:49 +00003510
Douglas Gregor43edb322011-10-24 22:31:10 +00003511 switch (Result.Behavior) {
3512 case IEB_Parse:
3513 // Parse the declarations below.
3514 break;
3515
3516 case IEB_Dependent:
3517 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3518 << Result.IsIfExists;
3519 // Fall through to skip.
3520
3521 case IEB_Skip:
3522 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00003523 return;
3524 }
3525
Richard Smith34f30512013-11-23 04:06:09 +00003526 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003527 // __if_exists, __if_not_exists can nest.
3528 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3529 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3530 continue;
3531 }
3532
3533 // Check for extraneous top-level semicolon.
3534 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003535 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00003536 continue;
3537 }
3538
3539 AccessSpecifier AS = getAccessSpecifierIfPresent();
3540 if (AS != AS_none) {
3541 // Current token is a C++ access specifier.
3542 CurAS = AS;
3543 SourceLocation ASLoc = Tok.getLocation();
3544 ConsumeToken();
3545 if (Tok.is(tok::colon))
3546 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3547 else
Alp Toker35d87032013-12-30 23:29:50 +00003548 Diag(Tok, diag::err_expected) << tok::colon;
Francois Pichet8f981d52011-05-25 10:19:49 +00003549 ConsumeToken();
3550 continue;
3551 }
3552
3553 // Parse all the comma separated declarators.
Craig Topper161e4db2014-05-21 06:02:52 +00003554 ParseCXXClassMemberDeclaration(CurAS, nullptr);
Francois Pichet8f981d52011-05-25 10:19:49 +00003555 }
Douglas Gregor43edb322011-10-24 22:31:10 +00003556
3557 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00003558}