blob: b95eb87a6fac0bbe73d692fefde51d5db70a84f5 [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"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000016#include "clang/AST/DeclTemplate.h"
Erik Verbruggen888d52a2014-01-15 09:15:43 +000017#include "clang/AST/ASTContext.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000018#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Basic/OperatorKinds.h"
Chris Lattner60f36222009-01-29 05:15:15 +000020#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/DeclSpec.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Sema/Scope.h"
John McCalldb632ac2012-09-25 07:32:39 +000025#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000026#include "llvm/ADT/SmallString.h"
Chris Lattnera5235172007-08-25 06:57:03 +000027using namespace clang;
28
29/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redl67667942010-08-27 23:12:46 +000030/// may either be a top level namespace or a block-level namespace alias. If
31/// there was an inline keyword, it has already been parsed.
Chris Lattnera5235172007-08-25 06:57:03 +000032///
33/// namespace-definition: [C++ 7.3: basic.namespace]
34/// named-namespace-definition
35/// unnamed-namespace-definition
36///
37/// unnamed-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000038/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000039///
40/// named-namespace-definition:
41/// original-namespace-definition
42/// extension-namespace-definition
43///
44/// original-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000045/// 'inline'[opt] 'namespace' identifier attributes[opt]
46/// '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000047///
48/// extension-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000049/// 'inline'[opt] 'namespace' original-namespace-name
50/// '{' namespace-body '}'
Mike Stump11289f42009-09-09 15:08:12 +000051///
Chris Lattnera5235172007-08-25 06:57:03 +000052/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
53/// 'namespace' identifier '=' qualified-namespace-specifier ';'
54///
John McCall48871652010-08-21 09:40:31 +000055Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redl67667942010-08-27 23:12:46 +000056 SourceLocation &DeclEnd,
57 SourceLocation InlineLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000058 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000059 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian4bf82622011-08-22 17:59:19 +000060 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000061
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000062 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000063 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000064 cutOffParsing();
65 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000066 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000067
Chris Lattnera5235172007-08-25 06:57:03 +000068 SourceLocation IdentLoc;
69 IdentifierInfo *Ident = 0;
Richard Trieu61384cb2011-05-26 20:11:09 +000070 std::vector<SourceLocation> ExtraIdentLoc;
71 std::vector<IdentifierInfo*> ExtraIdent;
72 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000073
74 Token attrTok;
Mike Stump11289f42009-09-09 15:08:12 +000075
Chris Lattner76c72282007-10-09 17:33:22 +000076 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000077 Ident = Tok.getIdentifierInfo();
78 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieu61384cb2011-05-26 20:11:09 +000079 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
80 ExtraNamespaceLoc.push_back(ConsumeToken());
81 ExtraIdent.push_back(Tok.getIdentifierInfo());
82 ExtraIdentLoc.push_back(ConsumeToken());
83 }
Chris Lattnera5235172007-08-25 06:57:03 +000084 }
Mike Stump11289f42009-09-09 15:08:12 +000085
Chris Lattnera5235172007-08-25 06:57:03 +000086 // Read label attributes, if present.
John McCall084e83d2011-03-24 11:26:52 +000087 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000088 if (Tok.is(tok::kw___attribute)) {
89 attrTok = Tok;
John McCall53fa7142010-12-24 02:08:15 +000090 ParseGNUAttributes(attrs);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000091 }
Mike Stump11289f42009-09-09 15:08:12 +000092
Douglas Gregor6b6bba42009-06-17 19:49:00 +000093 if (Tok.is(tok::equal)) {
Nico Weber729f1e22012-10-27 23:44:27 +000094 if (Ident == 0) {
Alp Tokerec543272013-12-24 09:48:30 +000095 Diag(Tok, diag::err_expected) << tok::identifier;
Nico Weber729f1e22012-10-27 23:44:27 +000096 // Skip to end of the definition and eat the ';'.
97 SkipUntil(tok::semi);
98 return 0;
99 }
John McCall53fa7142010-12-24 02:08:15 +0000100 if (!attrs.empty())
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000101 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redl67667942010-08-27 23:12:46 +0000102 if (InlineLoc.isValid())
103 Diag(InlineLoc, diag::err_inline_namespace_alias)
104 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000105 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000106 }
Mike Stump11289f42009-09-09 15:08:12 +0000107
Richard Trieu61384cb2011-05-26 20:11:09 +0000108
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000109 BalancedDelimiterTracker T(*this, tok::l_brace);
110 if (T.consumeOpen()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000111 if (!ExtraIdent.empty()) {
112 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
113 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
114 }
Alp Tokerec543272013-12-24 09:48:30 +0000115
116 if (Ident)
117 Diag(Tok, diag::err_expected) << tok::l_brace;
118 else
119 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
120
John McCall48871652010-08-21 09:40:31 +0000121 return 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000122 }
Mike Stump11289f42009-09-09 15:08:12 +0000123
Douglas Gregor0be31a22010-07-02 17:43:08 +0000124 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
125 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
126 getCurScope()->getFnParent()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000127 if (!ExtraIdent.empty()) {
128 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
129 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
130 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000131 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000132 SkipUntil(tok::r_brace);
John McCall48871652010-08-21 09:40:31 +0000133 return 0;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000134 }
135
Richard Trieu61384cb2011-05-26 20:11:09 +0000136 if (!ExtraIdent.empty()) {
137 TentativeParsingAction TPA(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000138 SkipUntil(tok::r_brace, StopBeforeMatch);
Richard Trieu61384cb2011-05-26 20:11:09 +0000139 Token rBraceToken = Tok;
140 TPA.Revert();
141
142 if (!rBraceToken.is(tok::r_brace)) {
143 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
144 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
145 } else {
Benjamin Kramerf546f412011-05-26 21:32:30 +0000146 std::string NamespaceFix;
Richard Trieu61384cb2011-05-26 20:11:09 +0000147 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
148 E = ExtraIdent.end(); I != E; ++I) {
149 NamespaceFix += " { namespace ";
150 NamespaceFix += (*I)->getName();
151 }
Benjamin Kramerf546f412011-05-26 21:32:30 +0000152
Richard Trieu61384cb2011-05-26 20:11:09 +0000153 std::string RBraces;
Benjamin Kramerf546f412011-05-26 21:32:30 +0000154 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieu61384cb2011-05-26 20:11:09 +0000155 RBraces += "} ";
Benjamin Kramerf546f412011-05-26 21:32:30 +0000156
Richard Trieu61384cb2011-05-26 20:11:09 +0000157 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
158 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
159 ExtraIdentLoc.back()),
160 NamespaceFix)
161 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
162 }
163 }
164
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000165 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith5d164bc2011-10-15 05:09:34 +0000166 if (InlineLoc.isValid())
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000167 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000168 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000169
Chris Lattner4de55aa2009-03-29 14:02:43 +0000170 // Enter a scope for the namespace.
171 ParseScope NamespaceScope(this, Scope::DeclScope);
172
John McCall48871652010-08-21 09:40:31 +0000173 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000174 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000175 IdentLoc, Ident, T.getOpenLocation(),
176 attrs.getList());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000177
John McCallfaf5fb42010-08-26 23:41:50 +0000178 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
179 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000180
Richard Trieu61384cb2011-05-26 20:11:09 +0000181 // Parse the contents of the namespace. This includes parsing recovery on
182 // any improperly nested namespaces.
183 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000184 InlineLoc, attrs, T);
Mike Stump11289f42009-09-09 15:08:12 +0000185
Chris Lattner4de55aa2009-03-29 14:02:43 +0000186 // Leave the namespace scope.
187 NamespaceScope.Exit();
188
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000189 DeclEnd = T.getCloseLocation();
190 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000191
192 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000193}
Chris Lattner38376f12008-01-12 07:05:38 +0000194
Richard Trieu61384cb2011-05-26 20:11:09 +0000195/// ParseInnerNamespace - Parse the contents of a namespace.
196void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
197 std::vector<IdentifierInfo*>& Ident,
198 std::vector<SourceLocation>& NamespaceLoc,
199 unsigned int index, SourceLocation& InlineLoc,
Richard Trieu61384cb2011-05-26 20:11:09 +0000200 ParsedAttributes& attrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000201 BalancedDelimiterTracker &Tracker) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000202 if (index == Ident.size()) {
Richard Smith34f30512013-11-23 04:06:09 +0000203 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000204 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000205 MaybeParseCXX11Attributes(attrs);
Richard Trieu61384cb2011-05-26 20:11:09 +0000206 MaybeParseMicrosoftAttributes(attrs);
207 ParseExternalDeclaration(attrs);
208 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000209
210 // The caller is what called check -- we are simply calling
211 // the close for it.
212 Tracker.consumeClose();
Richard Trieu61384cb2011-05-26 20:11:09 +0000213
214 return;
215 }
216
217 // Parse improperly nested namespaces.
218 ParseScope NamespaceScope(this, Scope::DeclScope);
219 Decl *NamespcDecl =
220 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
221 NamespaceLoc[index], IdentLoc[index],
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000222 Ident[index], Tracker.getOpenLocation(),
223 attrs.getList());
Richard Trieu61384cb2011-05-26 20:11:09 +0000224
225 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000226 attrs, Tracker);
Richard Trieu61384cb2011-05-26 20:11:09 +0000227
228 NamespaceScope.Exit();
229
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000230 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieu61384cb2011-05-26 20:11:09 +0000231}
232
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000233/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
234/// alias definition.
235///
John McCall48871652010-08-21 09:40:31 +0000236Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall084e83d2011-03-24 11:26:52 +0000237 SourceLocation AliasLoc,
238 IdentifierInfo *Alias,
239 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000240 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000241
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000242 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000243
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000244 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000245 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000246 cutOffParsing();
247 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000248 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000249
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000250 CXXScopeSpec SS;
251 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000252 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000253
254 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
255 Diag(Tok, diag::err_expected_namespace_name);
256 // Skip to end of the definition and eat the ';'.
257 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000258 return 0;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000259 }
260
261 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000262 IdentifierInfo *Ident = Tok.getIdentifierInfo();
263 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000264
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000265 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000266 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000267 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
268 SkipUntil(tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000269
Douglas Gregor0be31a22010-07-02 17:43:08 +0000270 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000271 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000272}
273
Chris Lattner38376f12008-01-12 07:05:38 +0000274/// ParseLinkage - We know that the current token is a string_literal
275/// and just before that, that extern was seen.
276///
277/// linkage-specification: [C++ 7.5p2: dcl.link]
278/// 'extern' string-literal '{' declaration-seq[opt] '}'
279/// 'extern' string-literal declaration
280///
Chris Lattner8ea64422010-11-09 20:15:55 +0000281Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Richard Smith4ee696d2014-02-17 23:25:27 +0000282 assert(isTokenStringLiteral() && "Not a string literal!");
283 ExprResult Lang = ParseStringLiteralExpression(false);
Chris Lattner38376f12008-01-12 07:05:38 +0000284
Douglas Gregor07665a62009-01-05 19:45:36 +0000285 ParseScope LinkageScope(this, Scope::DeclScope);
Richard Smith4ee696d2014-02-17 23:25:27 +0000286 Decl *LinkageSpec =
287 Lang.isInvalid()
288 ? 0
289 : Actions.ActOnStartLinkageSpecification(
290 getCurScope(), DS.getSourceRange().getBegin(), Lang.take(),
291 Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
Douglas Gregor07665a62009-01-05 19:45:36 +0000292
John McCall084e83d2011-03-24 11:26:52 +0000293 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000294 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000295 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000296
Douglas Gregor07665a62009-01-05 19:45:36 +0000297 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara4d423992011-05-01 16:25:54 +0000298 // Reset the source range in DS, as the leading "extern"
299 // does not really belong to the inner declaration ...
300 DS.SetRangeStart(SourceLocation());
301 DS.SetRangeEnd(SourceLocation());
302 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000303 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000304 ParseExternalDeclaration(attrs, &DS);
Richard Smith4ee696d2014-02-17 23:25:27 +0000305 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
306 getCurScope(), LinkageSpec, SourceLocation())
307 : 0;
Mike Stump11289f42009-09-09 15:08:12 +0000308 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000309
Douglas Gregorb65a9132010-02-07 08:38:28 +0000310 DS.abort();
311
John McCall53fa7142010-12-24 02:08:15 +0000312 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000313
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000314 BalancedDelimiterTracker T(*this, tok::l_brace);
315 T.consumeOpen();
Richard Smith34f30512013-11-23 04:06:09 +0000316 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
John McCall084e83d2011-03-24 11:26:52 +0000317 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000318 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000319 MaybeParseMicrosoftAttributes(attrs);
320 ParseExternalDeclaration(attrs);
Chris Lattner38376f12008-01-12 07:05:38 +0000321 }
322
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000323 T.consumeClose();
Richard Smith4ee696d2014-02-17 23:25:27 +0000324 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
325 getCurScope(), LinkageSpec, T.getCloseLocation())
326 : 0;
Chris Lattner38376f12008-01-12 07:05:38 +0000327}
Douglas Gregor556877c2008-04-13 21:30:24 +0000328
Douglas Gregord7c4d982008-12-30 03:27:21 +0000329/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
330/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000331Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000332 const ParsedTemplateInfo &TemplateInfo,
333 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000334 ParsedAttributesWithRange &attrs,
335 Decl **OwnedType) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000336 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000337 ObjCDeclContextSwitch ObjCDC(*this);
338
Douglas Gregord7c4d982008-12-30 03:27:21 +0000339 // Eat 'using'.
340 SourceLocation UsingLoc = ConsumeToken();
341
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000342 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000343 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000344 cutOffParsing();
345 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000346 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000347
John McCall9b72f892010-11-10 02:40:36 +0000348 // 'using namespace' means this is a using-directive.
349 if (Tok.is(tok::kw_namespace)) {
350 // Template parameters are always an error here.
351 if (TemplateInfo.Kind) {
352 SourceRange R = TemplateInfo.getSourceRange();
353 Diag(UsingLoc, diag::err_templated_using_directive)
354 << R << FixItHint::CreateRemoval(R);
355 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000356
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000357 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000358 }
359
Richard Smithdda56e42011-04-15 14:24:37 +0000360 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +0000361
362 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000363 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000364
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000365 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000366 AS_none, OwnedType);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000367}
368
369/// ParseUsingDirective - Parse C++ using-directive, assumes
370/// that current token is 'namespace' and 'using' was already parsed.
371///
372/// using-directive: [C++ 7.3.p4: namespace.udir]
373/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
374/// namespace-name ;
375/// [GNU] using-directive:
376/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
377/// namespace-name attributes[opt] ;
378///
John McCall48871652010-08-21 09:40:31 +0000379Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000380 SourceLocation UsingLoc,
381 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000382 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000383 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
384
385 // Eat 'namespace'.
386 SourceLocation NamespcLoc = ConsumeToken();
387
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000388 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000389 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000390 cutOffParsing();
391 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000392 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000393
Douglas Gregord7c4d982008-12-30 03:27:21 +0000394 CXXScopeSpec SS;
395 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000396 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000397
Douglas Gregord7c4d982008-12-30 03:27:21 +0000398 IdentifierInfo *NamespcName = 0;
399 SourceLocation IdentLoc = SourceLocation();
400
401 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000402 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000403 Diag(Tok, diag::err_expected_namespace_name);
404 // If there was invalid namespace name, skip to end of decl, and eat ';'.
405 SkipUntil(tok::semi);
406 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCall48871652010-08-21 09:40:31 +0000407 return 0;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000408 }
Mike Stump11289f42009-09-09 15:08:12 +0000409
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000410 // Parse identifier.
411 NamespcName = Tok.getIdentifierInfo();
412 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000413
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000414 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000415 bool GNUAttr = false;
416 if (Tok.is(tok::kw___attribute)) {
417 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000418 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000419 }
Mike Stump11289f42009-09-09 15:08:12 +0000420
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000421 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000422 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000423 if (ExpectAndConsume(tok::semi,
424 GNUAttr ? diag::err_expected_semi_after_attribute_list
425 : diag::err_expected_semi_after_namespace_name))
426 SkipUntil(tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000427
Douglas Gregor0be31a22010-07-02 17:43:08 +0000428 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000429 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000430}
431
Richard Smithdda56e42011-04-15 14:24:37 +0000432/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
433/// Assumes that 'using' was already seen.
Douglas Gregord7c4d982008-12-30 03:27:21 +0000434///
435/// using-declaration: [C++ 7.3.p3: namespace.udecl]
436/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000437/// unqualified-id
438/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000439///
Richard Smith810ad3e2013-01-29 10:02:16 +0000440/// alias-declaration: C++11 [dcl.dcl]p1
441/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smithdda56e42011-04-15 14:24:37 +0000442///
John McCall48871652010-08-21 09:40:31 +0000443Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000444 const ParsedTemplateInfo &TemplateInfo,
445 SourceLocation UsingLoc,
446 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000447 AccessSpecifier AS,
448 Decl **OwnedType) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000449 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000450 SourceLocation TypenameLoc;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000451 bool HasTypenameKeyword = false;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000452
Richard Smithc2c8bb82013-10-15 01:34:54 +0000453 // Check for misplaced attributes before the identifier in an
454 // alias-declaration.
455 ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
456 MaybeParseCXX11Attributes(MisplacedAttrs);
Douglas Gregorfec52632009-06-20 00:51:54 +0000457
458 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000459 // FIXME: This is wrong; we should parse this as a typename-specifier.
Alp Toker97650562014-01-10 11:19:30 +0000460 if (TryConsumeToken(tok::kw_typename, TypenameLoc))
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000461 HasTypenameKeyword = true;
Douglas Gregorfec52632009-06-20 00:51:54 +0000462
463 // Parse nested-name-specifier.
Richard Smith7447af42013-03-26 01:15:19 +0000464 IdentifierInfo *LastII = 0;
465 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
466 /*MayBePseudoDtor=*/0, /*IsTypename=*/false,
467 /*LastII=*/&LastII);
Douglas Gregorfec52632009-06-20 00:51:54 +0000468
Douglas Gregorfec52632009-06-20 00:51:54 +0000469 // Check nested-name specifier.
470 if (SS.isInvalid()) {
471 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000472 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000473 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000474
Richard Smith7447af42013-03-26 01:15:19 +0000475 SourceLocation TemplateKWLoc;
476 UnqualifiedId Name;
477
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000478 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000479 // destructor names and allow the action module to diagnose any semantic
480 // errors.
Richard Smith7447af42013-03-26 01:15:19 +0000481 //
482 // C++11 [class.qual]p2:
483 // [...] in a using-declaration that is a member-declaration, if the name
484 // specified after the nested-name-specifier is the same as the identifier
485 // or the simple-template-id's template-name in the last component of the
486 // nested-name-specifier, the name is [...] considered to name the
487 // constructor.
488 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
489 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
490 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
491 !SS.getScopeRep()->getAsNamespace() &&
492 !SS.getScopeRep()->getAsNamespaceAlias()) {
493 SourceLocation IdLoc = ConsumeToken();
494 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
495 Name.setConstructorName(Type, IdLoc, IdLoc);
496 } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
497 /*AllowDestructorName=*/ true,
498 /*AllowConstructorName=*/ true, ParsedType(),
499 TemplateKWLoc, Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000500 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000501 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000502 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000503
Richard Smithc2c8bb82013-10-15 01:34:54 +0000504 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smith37a45dd2013-10-24 01:21:09 +0000505 MaybeParseGNUAttributes(Attrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000506 MaybeParseCXX11Attributes(Attrs);
Richard Smithdda56e42011-04-15 14:24:37 +0000507
508 // Maybe this is an alias-declaration.
Richard Smithdda56e42011-04-15 14:24:37 +0000509 TypeResult TypeAlias;
Richard Smithc2c8bb82013-10-15 01:34:54 +0000510 bool IsAliasDecl = Tok.is(tok::equal);
Richard Smithdda56e42011-04-15 14:24:37 +0000511 if (IsAliasDecl) {
Richard Smithc2c8bb82013-10-15 01:34:54 +0000512 // If we had any misplaced attributes from earlier, this is where they
513 // should have been written.
514 if (MisplacedAttrs.Range.isValid()) {
515 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
516 << FixItHint::CreateInsertionFromRange(
517 Tok.getLocation(),
518 CharSourceRange::getTokenRange(MisplacedAttrs.Range))
519 << FixItHint::CreateRemoval(MisplacedAttrs.Range);
520 Attrs.takeAllFrom(MisplacedAttrs);
521 }
522
Richard Smithdda56e42011-04-15 14:24:37 +0000523 ConsumeToken();
524
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000525 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000526 diag::warn_cxx98_compat_alias_declaration :
527 diag::ext_alias_declaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000528
Richard Smith3f1b5d02011-05-05 21:57:07 +0000529 // Type alias templates cannot be specialized.
530 int SpecKind = -1;
Richard Smith14034022011-05-05 22:36:10 +0000531 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
532 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000533 SpecKind = 0;
534 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
535 SpecKind = 1;
536 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
537 SpecKind = 2;
538 if (SpecKind != -1) {
539 SourceRange Range;
540 if (SpecKind == 0)
541 Range = SourceRange(Name.TemplateId->LAngleLoc,
542 Name.TemplateId->RAngleLoc);
543 else
544 Range = TemplateInfo.getSourceRange();
545 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
546 << SpecKind << Range;
547 SkipUntil(tok::semi);
548 return 0;
549 }
550
Richard Smithdda56e42011-04-15 14:24:37 +0000551 // Name must be an identifier.
552 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
553 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
554 // No removal fixit: can't recover from this.
555 SkipUntil(tok::semi);
556 return 0;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000557 } else if (HasTypenameKeyword)
Richard Smithdda56e42011-04-15 14:24:37 +0000558 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
559 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
560 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
561 else if (SS.isNotEmpty())
562 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
563 << FixItHint::CreateRemoval(SS.getRange());
564
Richard Smith3f1b5d02011-05-05 21:57:07 +0000565 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
566 Declarator::AliasTemplateContext :
Richard Smith54ecd982013-02-20 19:22:51 +0000567 Declarator::AliasDeclContext, AS, OwnedType,
568 &Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000569 } else {
570 // C++11 attributes are not allowed on a using-declaration, but GNU ones
571 // are.
Richard Smithc2c8bb82013-10-15 01:34:54 +0000572 ProhibitAttributes(MisplacedAttrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000573 ProhibitAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000574
Richard Smithdda56e42011-04-15 14:24:37 +0000575 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith54ecd982013-02-20 19:22:51 +0000576 MaybeParseGNUAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000577 }
Mike Stump11289f42009-09-09 15:08:12 +0000578
Douglas Gregorfec52632009-06-20 00:51:54 +0000579 // Eat ';'.
580 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000581 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
582 !Attrs.empty() ? "attributes list"
583 : IsAliasDecl ? "alias declaration"
584 : "using declaration"))
585 SkipUntil(tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000586
John McCall9b72f892010-11-10 02:40:36 +0000587 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith810ad3e2013-01-29 10:02:16 +0000588 // In C++11, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000589 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000590 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-11-10 02:40:36 +0000591 SourceRange R = TemplateInfo.getSourceRange();
592 Diag(UsingLoc, diag::err_templated_using_declaration)
593 << R << FixItHint::CreateRemoval(R);
594
595 // Unfortunately, we have to bail out instead of recovering by
596 // ignoring the parameters, just in case the nested name specifier
597 // depends on the parameters.
598 return 0;
599 }
600
Douglas Gregor882a61a2011-09-26 14:30:28 +0000601 // "typename" keyword is allowed for identifiers only,
602 // because it may be a type definition.
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000603 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor882a61a2011-09-26 14:30:28 +0000604 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
605 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000606 // Proceed parsing, but reset the HasTypenameKeyword flag.
607 HasTypenameKeyword = false;
Douglas Gregor882a61a2011-09-26 14:30:28 +0000608 }
609
Richard Smith3f1b5d02011-05-05 21:57:07 +0000610 if (IsAliasDecl) {
611 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000612 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3f1b5d02011-05-05 21:57:07 +0000613 TemplateParams ? TemplateParams->data() : 0,
614 TemplateParams ? TemplateParams->size() : 0);
615 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith54ecd982013-02-20 19:22:51 +0000616 UsingLoc, Name, Attrs.getList(),
617 TypeAlias);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000618 }
Richard Smithdda56e42011-04-15 14:24:37 +0000619
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000620 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
621 /* HasUsingKeyword */ true, UsingLoc,
622 SS, Name, Attrs.getList(),
623 HasTypenameKeyword, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000624}
625
Benjamin Kramere56f3932011-12-23 17:00:35 +0000626/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000627///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000628/// [C++0x] static_assert-declaration:
629/// static_assert ( constant-expression , string-literal ) ;
630///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000631/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000632/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000633///
John McCall48871652010-08-21 09:40:31 +0000634Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000635 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
636 "Not a static_assert declaration");
637
David Blaikiebbafb8a2012-03-11 07:00:24 +0000638 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000639 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000640 if (Tok.is(tok::kw_static_assert))
641 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000642
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000643 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000644
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000645 BalancedDelimiterTracker T(*this, tok::l_paren);
646 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000647 Diag(Tok, diag::err_expected) << tok::l_paren;
Richard Smith76965712012-09-13 19:12:50 +0000648 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000649 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000650 }
Mike Stump11289f42009-09-09 15:08:12 +0000651
John McCalldadc5752010-08-24 06:29:42 +0000652 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000653 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000654 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000655 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000656 }
Mike Stump11289f42009-09-09 15:08:12 +0000657
Alp Toker383d2c42014-01-01 03:08:43 +0000658 if (ExpectAndConsume(tok::comma)) {
659 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000660 return 0;
Alp Toker383d2c42014-01-01 03:08:43 +0000661 }
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000662
Richard Smithf506eaf2012-03-05 23:20:05 +0000663 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000664 Diag(Tok, diag::err_expected_string_literal)
665 << /*Source='static_assert'*/1;
Richard Smith76965712012-09-13 19:12:50 +0000666 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000667 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000668 }
Mike Stump11289f42009-09-09 15:08:12 +0000669
John McCalldadc5752010-08-24 06:29:42 +0000670 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smithd67aea22012-03-06 03:21:47 +0000671 if (AssertMessage.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000672 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000673 return 0;
Richard Smithd67aea22012-03-06 03:21:47 +0000674 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000675
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000676 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000677
Chris Lattner49836b42009-04-02 04:16:50 +0000678 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000679 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000680
John McCallb268a282010-08-23 23:25:46 +0000681 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
682 AssertExpr.take(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000683 AssertMessage.take(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000684 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000685}
686
Richard Smith74aeef52013-04-26 16:15:35 +0000687/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson74948d02009-06-24 17:47:40 +0000688///
689/// 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +0000690/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson74948d02009-06-24 17:47:40 +0000691///
David Blaikie15a430a2011-12-04 05:04:18 +0000692SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
693 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
694 && "Not a decltype specifier");
695
David Blaikie15a430a2011-12-04 05:04:18 +0000696 ExprResult Result;
697 SourceLocation StartLoc = Tok.getLocation();
698 SourceLocation EndLoc;
699
700 if (Tok.is(tok::annot_decltype)) {
701 Result = getExprAnnotation(Tok);
702 EndLoc = Tok.getAnnotationEndLoc();
703 ConsumeToken();
704 if (Result.isInvalid()) {
705 DS.SetTypeSpecError();
706 return EndLoc;
707 }
708 } else {
Richard Smith324df552012-02-24 22:30:04 +0000709 if (Tok.getIdentifierInfo()->isStr("decltype"))
710 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000711
David Blaikie15a430a2011-12-04 05:04:18 +0000712 ConsumeToken();
713
714 BalancedDelimiterTracker T(*this, tok::l_paren);
715 if (T.expectAndConsume(diag::err_expected_lparen_after,
716 "decltype", tok::r_paren)) {
717 DS.SetTypeSpecError();
718 return T.getOpenLocation() == Tok.getLocation() ?
719 StartLoc : T.getOpenLocation();
720 }
721
Richard Smith74aeef52013-04-26 16:15:35 +0000722 // Check for C++1y 'decltype(auto)'.
723 if (Tok.is(tok::kw_auto)) {
724 // No need to disambiguate here: an expression can't start with 'auto',
725 // because the typename-specifier in a function-style cast operation can't
726 // be 'auto'.
727 Diag(Tok.getLocation(),
728 getLangOpts().CPlusPlus1y
729 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
730 : diag::ext_decltype_auto_type_specifier);
731 ConsumeToken();
732 } else {
733 // Parse the expression
David Blaikie15a430a2011-12-04 05:04:18 +0000734
Richard Smith74aeef52013-04-26 16:15:35 +0000735 // C++11 [dcl.type.simple]p4:
736 // The operand of the decltype specifier is an unevaluated operand.
737 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
738 0, /*IsDecltype=*/true);
739 Result = ParseExpression();
740 if (Result.isInvalid()) {
741 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000742 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smith74aeef52013-04-26 16:15:35 +0000743 EndLoc = ConsumeParen();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000744 } else {
Richard Smith74aeef52013-04-26 16:15:35 +0000745 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
746 // Backtrack to get the location of the last token before the semi.
747 PP.RevertCachedTokens(2);
748 ConsumeToken(); // the semi.
749 EndLoc = ConsumeAnyToken();
750 assert(Tok.is(tok::semi));
751 } else {
752 EndLoc = Tok.getLocation();
753 }
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000754 }
Richard Smith74aeef52013-04-26 16:15:35 +0000755 return EndLoc;
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000756 }
Richard Smith74aeef52013-04-26 16:15:35 +0000757
758 Result = Actions.ActOnDecltypeExpression(Result.take());
David Blaikie15a430a2011-12-04 05:04:18 +0000759 }
760
761 // Match the ')'
762 T.consumeClose();
763 if (T.getCloseLocation().isInvalid()) {
764 DS.SetTypeSpecError();
765 // FIXME: this should return the location of the last token
766 // that was consumed (by "consumeClose()")
767 return T.getCloseLocation();
768 }
769
Richard Smithfd555f62012-02-22 02:04:18 +0000770 if (Result.isInvalid()) {
771 DS.SetTypeSpecError();
772 return T.getCloseLocation();
773 }
774
David Blaikie15a430a2011-12-04 05:04:18 +0000775 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000776 }
Richard Smith74aeef52013-04-26 16:15:35 +0000777 assert(!Result.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000778
Anders Carlsson74948d02009-06-24 17:47:40 +0000779 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000780 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000781 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Anders Carlsson74948d02009-06-24 17:47:40 +0000782 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smith74aeef52013-04-26 16:15:35 +0000783 if (Result.get()
784 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000785 DiagID, Result.release(), Policy)
Richard Smith74aeef52013-04-26 16:15:35 +0000786 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000787 DiagID, Policy)) {
John McCall49bfce42009-08-03 20:12:06 +0000788 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-12-04 05:04:18 +0000789 DS.SetTypeSpecError();
790 }
791 return EndLoc;
792}
793
794void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
795 SourceLocation StartLoc,
796 SourceLocation EndLoc) {
797 // make sure we have a token we can turn into an annotation token
798 if (PP.isBacktrackEnabled())
799 PP.RevertCachedTokens(1);
800 else
801 PP.EnterToken(Tok);
802
803 Tok.setKind(tok::annot_decltype);
Richard Smith74aeef52013-04-26 16:15:35 +0000804 setExprAnnotation(Tok,
805 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
806 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
807 ExprError());
David Blaikie15a430a2011-12-04 05:04:18 +0000808 Tok.setAnnotationEndLoc(EndLoc);
809 Tok.setLocation(StartLoc);
810 PP.AnnotateCachedTokens(Tok);
Anders Carlsson74948d02009-06-24 17:47:40 +0000811}
812
Alexis Hunt4a257072011-05-19 05:37:45 +0000813void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
814 assert(Tok.is(tok::kw___underlying_type) &&
815 "Not an underlying type specifier");
816
817 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000818 BalancedDelimiterTracker T(*this, tok::l_paren);
819 if (T.expectAndConsume(diag::err_expected_lparen_after,
820 "__underlying_type", tok::r_paren)) {
Alexis Hunt4a257072011-05-19 05:37:45 +0000821 return;
822 }
823
824 TypeResult Result = ParseTypeName();
825 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000826 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt4a257072011-05-19 05:37:45 +0000827 return;
828 }
829
830 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000831 T.consumeClose();
832 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +0000833 return;
834
835 const char *PrevSpec = 0;
836 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000837 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000838 DiagID, Result.release(),
839 Actions.getASTContext().getPrintingPolicy()))
Alexis Hunt4a257072011-05-19 05:37:45 +0000840 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanellaa90af722013-07-06 18:54:58 +0000841 DS.setTypeofParensRange(T.getRange());
Alexis Hunt4a257072011-05-19 05:37:45 +0000842}
843
David Blaikie00ee7a082011-10-25 15:01:20 +0000844/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
845/// class name or decltype-specifier. Note that we only check that the result
846/// names a type; semantic analysis will need to verify that the type names a
847/// class. The result is either a type or null, depending on whether a type
848/// name was found.
Douglas Gregor831c93f2008-11-05 20:51:48 +0000849///
Richard Smith4c96e992013-02-19 23:47:15 +0000850/// base-type-specifier: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000851/// class-or-decltype
Richard Smith4c96e992013-02-19 23:47:15 +0000852/// class-or-decltype: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000853/// nested-name-specifier[opt] class-name
854/// decltype-specifier
Richard Smith4c96e992013-02-19 23:47:15 +0000855/// class-name: [C++ class.name]
Douglas Gregor831c93f2008-11-05 20:51:48 +0000856/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000857/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000858///
Richard Smith4c96e992013-02-19 23:47:15 +0000859/// In C++98, instead of base-type-specifier, we have:
860///
861/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie1cd50022011-10-25 17:10:12 +0000862Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
863 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-10-25 18:46:41 +0000864 // Ignore attempts to use typename
865 if (Tok.is(tok::kw_typename)) {
866 Diag(Tok, diag::err_expected_class_name_not_template)
867 << FixItHint::CreateRemoval(Tok.getLocation());
868 ConsumeToken();
869 }
870
David Blaikieafa155f2011-10-25 18:17:58 +0000871 // Parse optional nested-name-specifier
872 CXXScopeSpec SS;
873 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
874
875 BaseLoc = Tok.getLocation();
876
David Blaikie1cd50022011-10-25 17:10:12 +0000877 // Parse decltype-specifier
David Blaikie15a430a2011-12-04 05:04:18 +0000878 // tok == kw_decltype is just error recovery, it can only happen when SS
879 // isn't empty
880 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikieafa155f2011-10-25 18:17:58 +0000881 if (SS.isNotEmpty())
882 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
883 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie1cd50022011-10-25 17:10:12 +0000884 // Fake up a Declarator to use with ActOnTypeName.
885 DeclSpec DS(AttrFactory);
886
David Blaikie7491e732011-12-08 04:53:15 +0000887 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +0000888
889 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
890 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
891 }
892
Douglas Gregord54dfb82009-02-25 23:52:28 +0000893 // Check whether we have a template-id that names a type.
894 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000895 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +0000896 if (TemplateId->Kind == TNK_Type_template ||
897 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000898 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000899
900 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000901 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000902 EndLocation = Tok.getAnnotationEndLoc();
903 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000904
905 if (Type)
906 return Type;
907 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000908 }
909
910 // Fall through to produce an error below.
911 }
912
Douglas Gregor831c93f2008-11-05 20:51:48 +0000913 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000914 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000915 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000916 }
917
Douglas Gregor18473f32010-01-12 21:28:44 +0000918 IdentifierInfo *Id = Tok.getIdentifierInfo();
919 SourceLocation IdLoc = ConsumeToken();
920
921 if (Tok.is(tok::less)) {
922 // It looks the user intended to write a template-id here, but the
923 // template-name was wrong. Try to fix that.
924 TemplateNameKind TNK = TNK_Type_template;
925 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000926 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000927 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000928 Diag(IdLoc, diag::err_unknown_template_name)
929 << Id;
930 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000931
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000932 if (!Template) {
933 TemplateArgList TemplateArgs;
934 SourceLocation LAngleLoc, RAngleLoc;
935 ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
936 true, LAngleLoc, TemplateArgs, RAngleLoc);
Douglas Gregor18473f32010-01-12 21:28:44 +0000937 return true;
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000938 }
Douglas Gregor18473f32010-01-12 21:28:44 +0000939
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000940 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000941 UnqualifiedId TemplateName;
942 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000943
Douglas Gregor18473f32010-01-12 21:28:44 +0000944 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +0000945 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
946 TemplateName, true))
Douglas Gregor18473f32010-01-12 21:28:44 +0000947 return true;
948 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +0000949 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000950
Douglas Gregor18473f32010-01-12 21:28:44 +0000951 // If we didn't end up with a typename token, there's nothing more we
952 // can do.
953 if (Tok.isNot(tok::annot_typename))
954 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000955
Douglas Gregor18473f32010-01-12 21:28:44 +0000956 // Retrieve the type from the annotation token, consume that token, and
957 // return.
958 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +0000959 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +0000960 ConsumeToken();
961 return Type;
962 }
963
Douglas Gregor831c93f2008-11-05 20:51:48 +0000964 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +0000965 IdentifierInfo *CorrectedII = 0;
Douglas Gregore7c20652011-03-02 00:47:37 +0000966 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +0000967 false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +0000968 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +0000969 /*NonTrivialTypeSourceInfo=*/true,
970 &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000971 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +0000972 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000973 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000974 }
975
976 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +0000977 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000978
979 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000980 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +0000981 DS.SetRangeStart(IdLoc);
982 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +0000983 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000984
985 const char *PrevSpec = 0;
986 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000987 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
988 Actions.getASTContext().getPrintingPolicy());
Nick Lewycky19b9f952010-07-26 16:56:01 +0000989
990 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
991 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000992}
993
John McCall8d32c052012-05-22 21:28:12 +0000994void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
995 while (Tok.is(tok::kw___single_inheritance) ||
996 Tok.is(tok::kw___multiple_inheritance) ||
997 Tok.is(tok::kw___virtual_inheritance)) {
998 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
999 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +00001000 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
Aaron Ballman8edb5c22013-12-18 23:44:18 +00001001 AttributeList::AS_Keyword);
John McCall8d32c052012-05-22 21:28:12 +00001002 }
1003}
1004
Richard Smith369b9f92012-06-25 21:37:02 +00001005/// Determine whether the following tokens are valid after a type-specifier
1006/// which could be a standalone declaration. This will conservatively return
1007/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith200f47c2012-07-02 19:14:01 +00001008bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-06-25 21:37:02 +00001009 // This switch enumerates the valid "follow" set for type-specifiers.
1010 switch (Tok.getKind()) {
1011 default: break;
1012 case tok::semi: // struct foo {...} ;
1013 case tok::star: // struct foo {...} * P;
1014 case tok::amp: // struct foo {...} & R = ...
Richard Smith1ac67d12013-01-19 03:48:05 +00001015 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +00001016 case tok::identifier: // struct foo {...} V ;
1017 case tok::r_paren: //(struct foo {...} ) {4}
1018 case tok::annot_cxxscope: // struct foo {...} a:: b;
1019 case tok::annot_typename: // struct foo {...} a ::b;
1020 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1021 case tok::l_paren: // struct foo {...} ( x);
1022 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +00001023 case tok::kw_operator: // struct foo operator ++() {...}
Alp Tokerd3f79c52013-11-24 20:24:54 +00001024 case tok::kw___declspec: // struct foo {...} __declspec(...)
Richard Smith369b9f92012-06-25 21:37:02 +00001025 return true;
Richard Smith200f47c2012-07-02 19:14:01 +00001026 case tok::colon:
1027 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smith369b9f92012-06-25 21:37:02 +00001028 // Type qualifiers
1029 case tok::kw_const: // struct foo {...} const x;
1030 case tok::kw_volatile: // struct foo {...} volatile x;
1031 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001032 // Function specifiers
1033 // Note, no 'explicit'. An explicit function must be either a conversion
1034 // operator or a constructor. Either way, it can't have a return type.
1035 case tok::kw_inline: // struct foo inline f();
1036 case tok::kw_virtual: // struct foo virtual f();
1037 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +00001038 // Storage-class specifiers
1039 case tok::kw_static: // struct foo {...} static x;
1040 case tok::kw_extern: // struct foo {...} extern x;
1041 case tok::kw_typedef: // struct foo {...} typedef x;
1042 case tok::kw_register: // struct foo {...} register x;
1043 case tok::kw_auto: // struct foo {...} auto x;
1044 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001045 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +00001046 case tok::kw_constexpr: // struct foo {...} constexpr x;
1047 // As shown above, type qualifiers and storage class specifiers absolutely
1048 // can occur after class specifiers according to the grammar. However,
1049 // almost no one actually writes code like this. If we see one of these,
1050 // it is much more likely that someone missed a semi colon and the
1051 // type/storage class specifier we're seeing is part of the *next*
1052 // intended declaration, as in:
1053 //
1054 // struct foo { ... }
1055 // typedef int X;
1056 //
1057 // We'd really like to emit a missing semicolon error instead of emitting
1058 // an error on the 'int' saying that you can't have two type specifiers in
1059 // the same declaration of X. Because of this, we look ahead past this
1060 // token to see if it's a type specifier. If so, we know the code is
1061 // otherwise invalid, so we can produce the expected semi error.
1062 if (!isKnownToBeTypeSpecifier(NextToken()))
1063 return true;
1064 break;
1065 case tok::r_brace: // struct bar { struct foo {...} }
1066 // Missing ';' at end of struct is accepted as an extension in C mode.
1067 if (!getLangOpts().CPlusPlus)
1068 return true;
1069 break;
Richard Smith1ac67d12013-01-19 03:48:05 +00001070 // C++11 attributes
1071 case tok::l_square: // enum E [[]] x
1072 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1073 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith52c5b872013-01-29 04:13:32 +00001074 case tok::greater:
1075 // template<class T = class X>
1076 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001077 }
1078 return false;
1079}
1080
Douglas Gregor556877c2008-04-13 21:30:24 +00001081/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1082/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1083/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001084/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001085///
1086/// class-specifier: [C++ class]
1087/// class-head '{' member-specification[opt] '}'
1088/// class-head '{' member-specification[opt] '}' attributes[opt]
1089/// class-head:
1090/// class-key identifier[opt] base-clause[opt]
1091/// class-key nested-name-specifier identifier base-clause[opt]
1092/// class-key nested-name-specifier[opt] simple-template-id
1093/// base-clause[opt]
1094/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001095/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001096/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001097/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001098/// simple-template-id base-clause[opt]
1099/// class-key:
1100/// 'class'
1101/// 'struct'
1102/// 'union'
1103///
1104/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001105/// class-key ::[opt] nested-name-specifier[opt] identifier
1106/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1107/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001108///
1109/// Note that the C++ class-specifier and elaborated-type-specifier,
1110/// together, subsume the C99 struct-or-union-specifier:
1111///
1112/// struct-or-union-specifier: [C99 6.7.2.1]
1113/// struct-or-union identifier[opt] '{' struct-contents '}'
1114/// struct-or-union identifier
1115/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1116/// '}' attributes[opt]
1117/// [GNU] struct-or-union attributes[opt] identifier
1118/// struct-or-union:
1119/// 'struct'
1120/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001121void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1122 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001123 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001124 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001125 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001126 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001127 DeclSpec::TST TagType;
1128 if (TagTokKind == tok::kw_struct)
1129 TagType = DeclSpec::TST_struct;
1130 else if (TagTokKind == tok::kw___interface)
1131 TagType = DeclSpec::TST_interface;
1132 else if (TagTokKind == tok::kw_class)
1133 TagType = DeclSpec::TST_class;
1134 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001135 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1136 TagType = DeclSpec::TST_union;
1137 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001138
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001139 if (Tok.is(tok::code_completion)) {
1140 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001141 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001142 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001143 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001144
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001145 // C++03 [temp.explicit] 14.7.2/8:
1146 // The usual access checking rules do not apply to names used to specify
1147 // explicit instantiations.
1148 //
1149 // As an extension we do not perform access checking on the names used to
1150 // specify explicit specializations either. This is important to allow
1151 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001152 //
1153 // Note that we don't suppress if this turns out to be an elaborated
1154 // type specifier.
1155 bool shouldDelayDiagsInTag =
1156 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1157 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1158 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001159
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001160 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001161 // If attributes exist after tag, parse them.
Richard Smith37a45dd2013-10-24 01:21:09 +00001162 MaybeParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +00001163
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001164 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +00001165 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +00001166 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001167
John McCall8d32c052012-05-22 21:28:12 +00001168 // Parse inheritance specifiers.
1169 if (Tok.is(tok::kw___single_inheritance) ||
1170 Tok.is(tok::kw___multiple_inheritance) ||
1171 Tok.is(tok::kw___virtual_inheritance))
Richard Smith37a45dd2013-10-24 01:21:09 +00001172 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCall8d32c052012-05-22 21:28:12 +00001173
Alexis Hunt96d5c762009-11-21 08:43:09 +00001174 // If C++0x attributes exist here, parse them.
1175 // FIXME: Are we consistent with the ordering of parsing of different
1176 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001177 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001178
Michael Han309af292013-01-07 16:57:11 +00001179 // Source location used by FIXIT to insert misplaced
1180 // C++11 attributes
1181 SourceLocation AttrFixitLoc = Tok.getLocation();
1182
Alp Toker53358e42013-12-17 14:12:30 +00001183 // GNU libstdc++ and libc++ use certain intrinsic names as the
1184 // name of struct templates, but some are keywords in GCC >= 4.3
1185 // MSVC and Clang. For compatibility, convert the token to an identifier
1186 // and issue a warning diagnostic.
1187 if (TagType == DeclSpec::TST_struct && !Tok.is(tok::identifier) &&
1188 !Tok.isAnnotation()) {
1189 const IdentifierInfo *II = Tok.getIdentifierInfo();
1190 // We rarely end up here so the following check is efficient.
1191 if (II && II->getName().startswith("__is_"))
1192 TryKeywordIdentFallback(true);
1193 }
Mike Stump11289f42009-09-09 15:08:12 +00001194
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001195 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001196 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001197 if (getLangOpts().CPlusPlus) {
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001198 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1199 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001200
Douglas Gregordf593fb2011-11-07 17:33:42 +00001201 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall413021a2010-07-30 06:26:29 +00001202 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +00001203 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001204 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Alp Tokerec543272013-12-24 09:48:30 +00001205 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001206 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001207
Douglas Gregor916462b2009-10-30 21:46:58 +00001208 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1209
Douglas Gregor67a65642009-02-17 23:15:12 +00001210 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +00001211 IdentifierInfo *Name = 0;
1212 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +00001213 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +00001214 if (Tok.is(tok::identifier)) {
1215 Name = Tok.getIdentifierInfo();
1216 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001217
David Blaikiebbafb8a2012-03-11 07:00:24 +00001218 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001219 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001220 // Eat the template argument list and try to continue parsing this as
1221 // a class (or template thereof).
1222 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001223 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +00001224 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +00001225 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001226 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001227 // We couldn't parse the template argument list at all, so don't
1228 // try to give any location information for the list.
1229 LAngleLoc = RAngleLoc = SourceLocation();
1230 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001231
Douglas Gregor916462b2009-10-30 21:46:58 +00001232 Diag(NameLoc, diag::err_explicit_spec_non_template)
Alp Toker01d65e12014-01-06 12:54:41 +00001233 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1234 << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
Joao Matose9a3ed42012-08-31 22:18:20 +00001235
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001236 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001237 // we've removed its template argument list.
1238 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1239 if (TemplateParams && TemplateParams->size() > 1) {
1240 TemplateParams->pop_back();
1241 } else {
1242 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001243 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001244 = ParsedTemplateInfo::NonTemplate;
1245 }
1246 } else if (TemplateInfo.Kind
1247 == ParsedTemplateInfo::ExplicitInstantiation) {
1248 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +00001249 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001250 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001251 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001252 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001253 = SourceLocation();
1254 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1255 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001256 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001257 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001258 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001259 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001260 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001261
Douglas Gregore7c20652011-03-02 00:47:37 +00001262 if (TemplateId->Kind != TNK_Type_template &&
1263 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001264 // The template-name in the simple-template-id refers to
1265 // something other than a class template. Give an appropriate
1266 // error message and skip to the ';'.
1267 SourceRange Range(NameLoc);
1268 if (SS.isNotEmpty())
1269 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001270
Richard Smith72bfbd82013-12-04 00:28:23 +00001271 // FIXME: Name may be null here.
Douglas Gregor7f741122009-02-25 19:37:18 +00001272 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu30f93852013-06-19 22:25:01 +00001273 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001274
Douglas Gregor7f741122009-02-25 19:37:18 +00001275 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001276 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor7f741122009-02-25 19:37:18 +00001277 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001278 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001279 }
1280
Richard Smithbfdb1082012-03-12 08:56:40 +00001281 // There are four options here.
1282 // - If we are in a trailing return type, this is always just a reference,
1283 // and we must not try to parse a definition. For instance,
1284 // [] () -> struct S { };
1285 // does not define a type.
1286 // - If we have 'struct foo {...', 'struct foo :...',
1287 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1288 // - If we have 'struct foo;', then this is either a forward declaration
1289 // or a friend declaration, which have to be treated differently.
1290 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001291 //
1292 // We also detect these erroneous cases to provide better diagnostic for
1293 // C++11 attributes parsing.
1294 // - attributes follow class name:
1295 // struct foo [[]] {};
1296 // - attributes appear before or after 'final':
1297 // struct foo [[]] final [[]] {};
1298 //
Richard Smithc5b05522012-03-12 07:56:15 +00001299 // However, in type-specifier-seq's, things look like declarations but are
1300 // just references, e.g.
1301 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001302 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001303 // &T::operator struct s;
Richard Smith649c7b062014-01-08 00:56:48 +00001304 // For these, DSC is DSC_type_specifier or DSC_alias_declaration.
Michael Han9407e502012-11-26 22:54:45 +00001305
1306 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001307 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001308
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001309 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
John McCallfaf5fb42010-08-26 23:41:50 +00001310 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001311 if (DSC == DSC_trailing)
1312 TUK = Sema::TUK_Reference;
1313 else if (Tok.is(tok::l_brace) ||
1314 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001315 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001316 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001317 if (DS.isFriendSpecified()) {
1318 // C++ [class.friend]p2:
1319 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001320 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001321 << SourceRange(DS.getFriendSpecLoc());
1322
1323 // Skip everything up to the semicolon, so that this looks like a proper
1324 // friend class (or template thereof) declaration.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001325 SkipUntil(tok::semi, StopBeforeMatch);
John McCallfaf5fb42010-08-26 23:41:50 +00001326 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001327 } else {
1328 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001329 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001330 }
Richard Smith434516c2013-02-22 06:46:23 +00001331 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1332 NextToken().is(tok::kw_alignas))) {
Michael Han9407e502012-11-26 22:54:45 +00001333 // We can't tell if this is a definition or reference
1334 // until we skipped the 'final' and C++11 attribute specifiers.
1335 TentativeParsingAction PA(*this);
1336
1337 // Skip the 'final' keyword.
1338 ConsumeToken();
1339
1340 // Skip C++11 attribute specifiers.
1341 while (true) {
1342 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1343 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001344 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001345 break;
Richard Smith434516c2013-02-22 06:46:23 +00001346 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han9407e502012-11-26 22:54:45 +00001347 ConsumeToken();
1348 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001349 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001350 break;
1351 } else {
1352 break;
1353 }
1354 }
1355
1356 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1357 TUK = Sema::TUK_Definition;
1358 else
1359 TUK = Sema::TUK_Reference;
1360
1361 PA.Revert();
Richard Smith649c7b062014-01-08 00:56:48 +00001362 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00001363 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001364 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001365 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001366 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001367 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Joao Matose9a3ed42012-08-31 22:18:20 +00001368 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00001369 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001370 DeclSpec::getSpecifierName(TagType, PPol));
Joao Matose9a3ed42012-08-31 22:18:20 +00001371 PP.EnterToken(Tok);
1372 Tok.setKind(tok::semi);
1373 }
Richard Smith369b9f92012-06-25 21:37:02 +00001374 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001375 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001376
Michael Han9407e502012-11-26 22:54:45 +00001377 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1378 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001379 if (TUK != Sema::TUK_Reference) {
1380 // If this is not a reference, then the only possible
1381 // valid place for C++11 attributes to appear here
1382 // is between class-key and class-name. If there are
1383 // any attributes after class-name, we try a fixit to move
1384 // them to the right place.
1385 SourceRange AttrRange = Attributes.Range;
1386 if (AttrRange.isValid()) {
1387 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1388 << AttrRange
1389 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1390 CharSourceRange(AttrRange, true))
1391 << FixItHint::CreateRemoval(AttrRange);
1392
1393 // Recover by adding misplaced attributes to the attribute list
1394 // of the class so they can be applied on the class later.
1395 attrs.takeAllFrom(Attributes);
1396 }
1397 }
Michael Han9407e502012-11-26 22:54:45 +00001398
John McCall6347b682012-05-07 06:16:58 +00001399 // If this is an elaborated type specifier, and we delayed
1400 // diagnostics before, just merge them into the current pool.
1401 if (shouldDelayDiagsInTag) {
1402 diagsFromTag.done();
1403 if (TUK == Sema::TUK_Reference)
1404 diagsFromTag.redelay();
1405 }
1406
John McCall413021a2010-07-30 06:26:29 +00001407 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001408 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001409 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1410 // We have a declaration or reference to an anonymous class.
1411 Diag(StartLoc, diag::err_anon_type_definition)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001412 << DeclSpec::getSpecifierName(TagType, Policy);
John McCall413021a2010-07-30 06:26:29 +00001413 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001414
David Majnemer3252fd02013-12-05 01:36:53 +00001415 // If we are parsing a definition and stop at a base-clause, continue on
1416 // until the semicolon. Continuing from the comma will just trick us into
1417 // thinking we are seeing a variable declaration.
1418 if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
1419 SkipUntil(tok::semi, StopBeforeMatch);
1420 else
1421 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor556877c2008-04-13 21:30:24 +00001422 return;
1423 }
1424
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001425 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001426 DeclResult TagOrTempResult = true; // invalid
1427 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001428
Douglas Gregord6ab8742009-05-28 23:31:59 +00001429 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +00001430 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001431 // Explicit specialization, class template partial specialization,
1432 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001433 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001434 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001435 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001436 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001437 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001438 ProhibitAttributes(attrs);
1439
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001440 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001441 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001442 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001443 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001444 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001445 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001446 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001447 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001448 TemplateId->TemplateNameLoc,
1449 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001450 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001451 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001452 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001453
1454 // Friend template-ids are treated as references unless
1455 // they have template headers, in which case they're ill-formed
1456 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1457 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001458 } else if (TUK == Sema::TUK_Reference ||
1459 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001460 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001461 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001462 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001463 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001464 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001465 TemplateId->Template,
1466 TemplateId->TemplateNameLoc,
1467 TemplateId->LAngleLoc,
1468 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001469 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001470 } else {
1471 // This is an explicit specialization or a class template
1472 // partial specialization.
1473 TemplateParameterLists FakedParamLists;
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001474 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1475 // This looks like an explicit instantiation, because we have
1476 // something like
1477 //
1478 // template class Foo<X>
1479 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001480 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001481 // meant to be an explicit specialization, but the user forgot
1482 // the '<>' after 'template'.
Richard Smith003c5e12013-11-08 19:03:29 +00001483 // It this is friend declaration however, since it cannot have a
1484 // template header, it is most likely that the user meant to
1485 // remove the 'template' keyword.
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001486 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith003c5e12013-11-08 19:03:29 +00001487 "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001488
Richard Smith003c5e12013-11-08 19:03:29 +00001489 if (TUK == Sema::TUK_Friend) {
1490 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
1491 TemplateParams = 0;
1492 } else {
1493 SourceLocation LAngleLoc =
1494 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1495 Diag(TemplateId->TemplateNameLoc,
1496 diag::err_explicit_instantiation_with_definition)
1497 << SourceRange(TemplateInfo.TemplateLoc)
1498 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1499
1500 // Create a fake template parameter list that contains only
1501 // "template<>", so that we treat this construct as a class
1502 // template specialization.
1503 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1504 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1505 LAngleLoc));
1506 TemplateParams = &FakedParamLists;
1507 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001508 }
1509
1510 // Build the class template specialization.
1511 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001512 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor3c7cd6a2011-09-09 20:53:38 +00001513 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall3e56fd42010-08-23 07:28:44 +00001514 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001515 TemplateId->TemplateNameLoc,
1516 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +00001517 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001518 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001519 attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001520 MultiTemplateParamsArg(
Douglas Gregor67a65642009-02-17 23:15:12 +00001521 TemplateParams? &(*TemplateParams)[0] : 0,
1522 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001523 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001524 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001525 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001526 // Explicit instantiation of a member of a class template
1527 // specialization, e.g.,
1528 //
1529 // template struct Outer<int>::Inner;
1530 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001531 ProhibitAttributes(attrs);
1532
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001533 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001534 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001535 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001536 TemplateInfo.TemplateLoc,
1537 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001538 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001539 } else if (TUK == Sema::TUK_Friend &&
1540 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001541 ProhibitAttributes(attrs);
1542
John McCallace48cd2010-10-19 01:40:49 +00001543 TagOrTempResult =
1544 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1545 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001546 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001547 MultiTemplateParamsArg(
John McCallace48cd2010-10-19 01:40:49 +00001548 TemplateParams? &(*TemplateParams)[0] : 0,
1549 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001550 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001551 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1552 ProhibitAttributes(attrs);
Richard Smith003c5e12013-11-08 19:03:29 +00001553
Larisse Voufo725de3e2013-06-21 00:08:46 +00001554 if (TUK == Sema::TUK_Definition &&
1555 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1556 // If the declarator-id is not a template-id, issue a diagnostic and
1557 // recover by ignoring the 'template' keyword.
1558 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1559 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001560 TemplateParams = 0;
Larisse Voufo725de3e2013-06-21 00:08:46 +00001561 }
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001562
John McCall7f41d982009-09-11 04:59:25 +00001563 bool IsDependent = false;
1564
John McCall32723e92010-10-19 18:40:57 +00001565 // Don't pass down template parameter lists if this is just a tag
1566 // reference. For example, we don't need the template parameters here:
1567 // template <class T> class A *makeA(T t);
1568 MultiTemplateParamsArg TParams;
1569 if (TUK != Sema::TUK_Reference && TemplateParams)
1570 TParams =
1571 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1572
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001573 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001574 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001575 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001576 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001577 TParams, Owned, IsDependent,
1578 SourceLocation(), false,
Richard Smith649c7b062014-01-08 00:56:48 +00001579 clang::TypeResult(),
1580 DSC == DSC_type_specifier);
John McCall7f41d982009-09-11 04:59:25 +00001581
1582 // If ActOnTag said the type was dependent, try again with the
1583 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001584 if (IsDependent) {
1585 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001586 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001587 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001588 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001589 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001590
Douglas Gregor556877c2008-04-13 21:30:24 +00001591 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001592 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001593 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001594 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001595 isCXX11FinalKeyword());
David Blaikiebbafb8a2012-03-11 07:00:24 +00001596 if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001597 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1598 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001599 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001600 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001601 }
1602
John McCallba7bf592010-08-24 05:47:05 +00001603 const char *PrevSpec = 0;
1604 unsigned DiagID;
1605 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001606 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001607 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1608 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001609 PrevSpec, DiagID, TypeResult.get(), Policy);
John McCall7f41d982009-09-11 04:59:25 +00001610 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001611 Result = DS.SetTypeSpecType(TagType, StartLoc,
1612 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001613 PrevSpec, DiagID, TagOrTempResult.get(), Owned,
1614 Policy);
John McCall7f41d982009-09-11 04:59:25 +00001615 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001616 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001617 return;
1618 }
Mike Stump11289f42009-09-09 15:08:12 +00001619
John McCallba7bf592010-08-24 05:47:05 +00001620 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001621 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001622
Chris Lattnercf251412010-02-02 01:23:29 +00001623 // At this point, we've successfully parsed a class-specifier in 'definition'
1624 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1625 // going to look at what comes after it to improve error recovery. If an
1626 // impossible token occurs next, we assume that the programmer forgot a ; at
1627 // the end of the declaration and recover that way.
1628 //
Richard Smith369b9f92012-06-25 21:37:02 +00001629 // Also enforce C++ [temp]p3:
1630 // In a template-declaration which defines a class, no declarator
1631 // is permitted.
Joao Matose9a3ed42012-08-31 22:18:20 +00001632 if (TUK == Sema::TUK_Definition &&
1633 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001634 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001635 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Alp Toker383d2c42014-01-01 03:08:43 +00001636 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001637 DeclSpec::getSpecifierName(TagType, PPol));
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001638 // Push this token back into the preprocessor and change our current token
1639 // to ';' so that the rest of the code recovers as though there were an
1640 // ';' after the definition.
1641 PP.EnterToken(Tok);
1642 Tok.setKind(tok::semi);
1643 }
Chris Lattnercf251412010-02-02 01:23:29 +00001644 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001645}
1646
Mike Stump11289f42009-09-09 15:08:12 +00001647/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001648///
1649/// base-clause : [C++ class.derived]
1650/// ':' base-specifier-list
1651/// base-specifier-list:
1652/// base-specifier '...'[opt]
1653/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001654void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001655 assert(Tok.is(tok::colon) && "Not a base clause");
1656 ConsumeToken();
1657
Douglas Gregor29a92472008-10-22 17:49:05 +00001658 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001659 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001660
Douglas Gregor556877c2008-04-13 21:30:24 +00001661 while (true) {
1662 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001663 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001664 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001665 // Skip the rest of this base specifier, up until the comma or
1666 // opening brace.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001667 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor29a92472008-10-22 17:49:05 +00001668 } else {
1669 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001670 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001671 }
1672
1673 // If the next token is a comma, consume it and keep reading
1674 // base-specifiers.
Alp Toker97650562014-01-10 11:19:30 +00001675 if (!TryConsumeToken(tok::comma))
1676 break;
Douglas Gregor556877c2008-04-13 21:30:24 +00001677 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001678
1679 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001680 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001681}
1682
1683/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1684/// one entry in the base class list of a class specifier, for example:
1685/// class foo : public bar, virtual private baz {
1686/// 'public bar' and 'virtual private baz' are each base-specifiers.
1687///
1688/// base-specifier: [C++ class.derived]
Richard Smith4c96e992013-02-19 23:47:15 +00001689/// attribute-specifier-seq[opt] base-type-specifier
1690/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1691/// base-type-specifier
1692/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1693/// base-type-specifier
John McCall48871652010-08-21 09:40:31 +00001694Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001695 bool IsVirtual = false;
1696 SourceLocation StartLoc = Tok.getLocation();
1697
Richard Smith4c96e992013-02-19 23:47:15 +00001698 ParsedAttributesWithRange Attributes(AttrFactory);
1699 MaybeParseCXX11Attributes(Attributes);
1700
Douglas Gregor556877c2008-04-13 21:30:24 +00001701 // Parse the 'virtual' keyword.
Alp Toker97650562014-01-10 11:19:30 +00001702 if (TryConsumeToken(tok::kw_virtual))
Douglas Gregor556877c2008-04-13 21:30:24 +00001703 IsVirtual = true;
Douglas Gregor556877c2008-04-13 21:30:24 +00001704
Richard Smith4c96e992013-02-19 23:47:15 +00001705 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1706
Douglas Gregor556877c2008-04-13 21:30:24 +00001707 // Parse an (optional) access specifier.
1708 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001709 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001710 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001711
Richard Smith4c96e992013-02-19 23:47:15 +00001712 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1713
Douglas Gregor556877c2008-04-13 21:30:24 +00001714 // Parse the 'virtual' keyword (again!), in case it came after the
1715 // access specifier.
1716 if (Tok.is(tok::kw_virtual)) {
1717 SourceLocation VirtualLoc = ConsumeToken();
1718 if (IsVirtual) {
1719 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001720 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001721 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001722 }
1723
1724 IsVirtual = true;
1725 }
1726
Richard Smith4c96e992013-02-19 23:47:15 +00001727 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1728
Douglas Gregor831c93f2008-11-05 20:51:48 +00001729 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001730 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00001731 SourceLocation BaseLoc;
1732 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001733 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001734 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001735
Douglas Gregor752a5952011-01-03 22:36:02 +00001736 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1737 // actually part of the base-specifier-list grammar productions, but we
1738 // parse it here for convenience.
1739 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00001740 TryConsumeToken(tok::ellipsis, EllipsisLoc);
1741
Mike Stump11289f42009-09-09 15:08:12 +00001742 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001743 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001744
Douglas Gregor556877c2008-04-13 21:30:24 +00001745 // Notify semantic analysis that we have parsed a complete
1746 // base-specifier.
Richard Smith4c96e992013-02-19 23:47:15 +00001747 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1748 Access, BaseType.get(), BaseLoc,
1749 EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001750}
1751
1752/// getAccessSpecifierIfPresent - Determine whether the next token is
1753/// a C++ access-specifier.
1754///
1755/// access-specifier: [C++ class.derived]
1756/// 'private'
1757/// 'protected'
1758/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001759AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001760 switch (Tok.getKind()) {
1761 default: return AS_none;
1762 case tok::kw_private: return AS_private;
1763 case tok::kw_protected: return AS_protected;
1764 case tok::kw_public: return AS_public;
1765 }
1766}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001767
Douglas Gregor433e0532012-04-16 18:27:27 +00001768/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith2331bbf2012-05-02 22:22:32 +00001769/// delayed, e.g., default arguments, create a late-parsed method declaration
1770/// record to handle the parsing at the end of the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00001771void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1772 Decl *ThisDecl) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001773 // We just declared a member function. If this member function
Richard Smith2331bbf2012-05-02 22:22:32 +00001774 // has any default arguments, we'll need to parse them later.
Eli Friedman3af2a772009-07-22 21:45:50 +00001775 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001776 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001777 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor433e0532012-04-16 18:27:27 +00001778
Eli Friedman3af2a772009-07-22 21:45:50 +00001779 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1780 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1781 if (!LateMethod) {
1782 // Push this method onto the stack of late-parsed method
1783 // declarations.
Douglas Gregorefc46952010-10-12 16:25:54 +00001784 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1785 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001786 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001787
1788 // Add all of the parameters prior to this one (they don't
1789 // have default arguments).
1790 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1791 for (unsigned I = 0; I < ParamIdx; ++I)
1792 LateMethod->DefaultArgs.push_back(
Douglas Gregor1d85d292010-03-02 01:29:43 +00001793 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001794 }
1795
Douglas Gregor433e0532012-04-16 18:27:27 +00001796 // Add this parameter to the list of parameters (it may or may
Eli Friedman3af2a772009-07-22 21:45:50 +00001797 // not have a default argument).
1798 LateMethod->DefaultArgs.push_back(
1799 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1800 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1801 }
1802 }
1803}
1804
Richard Smith89645bc2013-01-02 12:01:23 +00001805/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001806/// virt-specifier.
1807///
1808/// virt-specifier:
1809/// override
1810/// final
Richard Smith89645bc2013-01-02 12:01:23 +00001811VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001812 if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001813 return VirtSpecifiers::VS_None;
1814
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001815 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001816
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001817 // Initialize the contextual keywords.
1818 if (!Ident_final) {
1819 Ident_final = &PP.getIdentifierTable().get("final");
1820 if (getLangOpts().MicrosoftExt)
1821 Ident_sealed = &PP.getIdentifierTable().get("sealed");
1822 Ident_override = &PP.getIdentifierTable().get("override");
Anders Carlsson56104902011-01-17 03:05:47 +00001823 }
1824
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001825 if (II == Ident_override)
1826 return VirtSpecifiers::VS_Override;
1827
1828 if (II == Ident_sealed)
1829 return VirtSpecifiers::VS_Sealed;
1830
1831 if (II == Ident_final)
1832 return VirtSpecifiers::VS_Final;
1833
Anders Carlsson56104902011-01-17 03:05:47 +00001834 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001835}
1836
Richard Smith89645bc2013-01-02 12:01:23 +00001837/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001838///
1839/// virt-specifier-seq:
1840/// virt-specifier
1841/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00001842void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalldb632ac2012-09-25 07:32:39 +00001843 bool IsInterface) {
Anders Carlsson56104902011-01-17 03:05:47 +00001844 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00001845 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001846 if (Specifier == VirtSpecifiers::VS_None)
1847 return;
1848
1849 // C++ [class.mem]p8:
1850 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001851 const char *PrevSpec = 0;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001852 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001853 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1854 << PrevSpec
1855 << FixItHint::CreateRemoval(Tok.getLocation());
1856
David Majnemera5433082013-10-18 00:33:31 +00001857 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
1858 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalldb632ac2012-09-25 07:32:39 +00001859 Diag(Tok.getLocation(), diag::err_override_control_interface)
1860 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemera5433082013-10-18 00:33:31 +00001861 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
1862 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
John McCalldb632ac2012-09-25 07:32:39 +00001863 } else {
David Majnemera5433082013-10-18 00:33:31 +00001864 Diag(Tok.getLocation(),
1865 getLangOpts().CPlusPlus11
1866 ? diag::warn_cxx98_compat_override_control_keyword
1867 : diag::ext_override_control_keyword)
1868 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalldb632ac2012-09-25 07:32:39 +00001869 }
Anders Carlsson56104902011-01-17 03:05:47 +00001870 ConsumeToken();
1871 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001872}
1873
Richard Smith89645bc2013-01-02 12:01:23 +00001874/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001875/// 'final' or Microsoft 'sealed' contextual keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00001876bool Parser::isCXX11FinalKeyword() const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001877 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
1878 return Specifier == VirtSpecifiers::VS_Final ||
1879 Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001880}
1881
Richard Smith72553fc2014-01-23 23:53:27 +00001882/// \brief Parse a C++ member-declarator up to, but not including, the optional
1883/// brace-or-equal-initializer or pure-specifier.
1884void Parser::ParseCXXMemberDeclaratorBeforeInitializer(
1885 Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
1886 LateParsedAttrList &LateParsedAttrs) {
1887 // member-declarator:
1888 // declarator pure-specifier[opt]
1889 // declarator brace-or-equal-initializer[opt]
1890 // identifier[opt] ':' constant-expression
1891 if (Tok.isNot(tok::colon)) {
1892 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1893 // is a bitfield.
1894 // FIXME: This should only apply when parsing the id-expression (see
1895 // PR18587).
1896 ColonProtectionRAIIObject X(*this);
1897 ParseDeclarator(DeclaratorInfo);
1898 }
1899
1900 if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
1901 BitfieldSize = ParseConstantExpression();
1902 if (BitfieldSize.isInvalid())
1903 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
1904 } else
1905 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
1906
1907 // If a simple-asm-expr is present, parse it.
1908 if (Tok.is(tok::kw_asm)) {
1909 SourceLocation Loc;
1910 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1911 if (AsmLabel.isInvalid())
1912 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
1913
1914 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1915 DeclaratorInfo.SetRangeEnd(Loc);
1916 }
1917
1918 // If attributes exist after the declarator, but before an '{', parse them.
1919 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Richard Smith4b5a9492014-01-24 22:34:35 +00001920
1921 // For compatibility with code written to older Clang, also accept a
1922 // virt-specifier *after* the GNU attributes.
1923 // FIXME: If we saw any attributes that are known to GCC followed by a
1924 // virt-specifier, issue a GCC-compat warning.
1925 if (BitfieldSize.isUnset() && VS.isUnset())
1926 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith72553fc2014-01-23 23:53:27 +00001927}
1928
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001929/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1930///
1931/// member-declaration:
1932/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1933/// function-definition ';'[opt]
1934/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1935/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001936/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001937/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001938/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001939///
1940/// member-declarator-list:
1941/// member-declarator
1942/// member-declarator-list ',' member-declarator
1943///
1944/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001945/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001946/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00001947/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001948/// identifier[opt] ':' constant-expression
1949///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001950/// virt-specifier-seq:
1951/// virt-specifier
1952/// virt-specifier-seq virt-specifier
1953///
1954/// virt-specifier:
1955/// override
1956/// final
David Majnemera5433082013-10-18 00:33:31 +00001957/// [MS] sealed
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001958///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001959/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001960/// '= 0'
1961///
1962/// constant-initializer:
1963/// '=' constant-expression
1964///
Douglas Gregor3447e762009-08-20 22:52:58 +00001965void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00001966 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00001967 const ParsedTemplateInfo &TemplateInfo,
1968 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00001969 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001970 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00001971 Diag(Tok, diag::err_at_defs_cxx);
1972 else
1973 Diag(Tok, diag::err_at_in_class);
Richard Smithda35e962013-11-09 04:52:51 +00001974
Douglas Gregor23c84762011-04-14 17:21:19 +00001975 ConsumeToken();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001976 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregor23c84762011-04-14 17:21:19 +00001977 return;
1978 }
Richard Smithda35e962013-11-09 04:52:51 +00001979
John McCalla0097262009-12-11 02:10:03 +00001980 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00001981 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00001982 if (!TemplateInfo.Kind &&
Richard Smith45855df2012-05-09 08:23:23 +00001983 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1984 if (TryAnnotateCXXScopeToken())
1985 MalformedTypeSpec = true;
1986
1987 bool isAccessDecl;
1988 if (Tok.isNot(tok::annot_cxxscope))
1989 isAccessDecl = false;
1990 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00001991 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1992 else
1993 isAccessDecl = NextToken().is(tok::kw_operator);
1994
1995 if (isAccessDecl) {
1996 // Collect the scope specifier token we annotated earlier.
1997 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00001998 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1999 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00002000
2001 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00002002 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00002003 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +00002004 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
2005 TemplateKWLoc, Name)) {
John McCalla0097262009-12-11 02:10:03 +00002006 SkipUntil(tok::semi);
2007 return;
2008 }
2009
2010 // TODO: recover from mistakenly-qualified operator declarations.
Alp Toker383d2c42014-01-01 03:08:43 +00002011 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
2012 "access declaration")) {
2013 SkipUntil(tok::semi);
John McCalla0097262009-12-11 02:10:03 +00002014 return;
Alp Toker383d2c42014-01-01 03:08:43 +00002015 }
John McCalla0097262009-12-11 02:10:03 +00002016
Douglas Gregor0be31a22010-07-02 17:43:08 +00002017 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002018 /* HasUsingKeyword */ false,
2019 SourceLocation(),
John McCalla0097262009-12-11 02:10:03 +00002020 SS, Name,
2021 /* AttrList */ 0,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002022 /* HasTypenameKeyword */ false,
John McCalla0097262009-12-11 02:10:03 +00002023 SourceLocation());
2024 return;
2025 }
2026 }
2027
Anders Carlssonf24fcff62009-03-11 16:27:10 +00002028 // static_assert-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00002029 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00002030 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00002031 SourceLocation DeclEnd;
2032 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002033 return;
2034 }
Mike Stump11289f42009-09-09 15:08:12 +00002035
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002036 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00002037 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00002038 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00002039 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00002040 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002041 AS, AccessAttrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002042 return;
2043 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002044
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002045 // Handle: member-declaration ::= '__extension__' member-declaration
2046 if (Tok.is(tok::kw___extension__)) {
2047 // __extension__ silences extension warnings in the subexpression.
2048 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2049 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002050 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2051 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002052 }
Douglas Gregorfec52632009-06-20 00:51:54 +00002053
John McCall084e83d2011-03-24 11:26:52 +00002054 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00002055 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002056 // Optional C++11 attribute-specifier
2057 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00002058 // We need to keep these attributes for future diagnostic
2059 // before they are taken over by declaration specifier.
2060 FnAttrs.addAll(attrs.getList());
2061 FnAttrs.Range = attrs.Range;
2062
John McCall53fa7142010-12-24 02:08:15 +00002063 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002064
Douglas Gregorfec52632009-06-20 00:51:54 +00002065 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00002066 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002067
Douglas Gregorfec52632009-06-20 00:51:54 +00002068 // Eat 'using'.
2069 SourceLocation UsingLoc = ConsumeToken();
2070
2071 if (Tok.is(tok::kw_namespace)) {
2072 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002073 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002074 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00002075 SourceLocation DeclEnd;
Richard Smith3f1b5d02011-05-05 21:57:07 +00002076 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +00002077 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2078 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00002079 }
2080 return;
2081 }
2082
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002083 // Hold late-parsed attributes so we can attach a Decl to them later.
2084 LateParsedAttrList CommonLateParsedAttrs;
2085
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002086 // decl-specifier-seq:
2087 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00002088 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00002089 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00002090 if (MalformedTypeSpec)
2091 DS.SetTypeSpecError();
Richard Smith72553fc2014-01-23 23:53:27 +00002092
2093 {
2094 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
2095 // is a bitfield.
2096 ColonProtectionRAIIObject X(*this);
2097 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2098 &CommonLateParsedAttrs);
2099 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002100
Richard Smith404dfb42013-11-19 22:47:36 +00002101 // If we had a free-standing type definition with a missing semicolon, we
2102 // may get this far before the problem becomes obvious.
2103 if (DS.hasTagDefinition() &&
2104 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2105 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2106 &CommonLateParsedAttrs))
2107 return;
2108
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002109 MultiTemplateParamsArg TemplateParams(
John McCall11083da2009-09-16 22:47:08 +00002110 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2111 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2112
Alp Toker35d87032013-12-30 23:29:50 +00002113 if (TryConsumeToken(tok::semi)) {
Michael Handdc016d2012-11-28 23:17:40 +00002114 if (DS.isFriendSpecified())
2115 ProhibitAttributes(FnAttrs);
2116
John McCall48871652010-08-21 09:40:31 +00002117 Decl *TheDecl =
Chandler Carruth7c9856d2011-05-03 18:35:10 +00002118 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCall796c2a52010-07-16 08:13:16 +00002119 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00002120 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002121 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002122
John McCall28a6aea2009-11-04 02:18:39 +00002123 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002124 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002125
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002126 // Hold late-parsed attributes so we can attach a Decl to them later.
2127 LateParsedAttrList LateParsedAttrs;
2128
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002129 SourceLocation EqualLoc;
2130 bool HasInitializer = false;
2131 ExprResult Init;
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002132
Richard Smith72553fc2014-01-23 23:53:27 +00002133 SmallVector<Decl *, 8> DeclsInGroup;
2134 ExprResult BitfieldSize;
2135 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002136
Richard Smith72553fc2014-01-23 23:53:27 +00002137 // Parse the first declarator.
2138 ParseCXXMemberDeclaratorBeforeInitializer(DeclaratorInfo, VS, BitfieldSize,
2139 LateParsedAttrs);
Nico Weber24b2a822011-01-28 06:07:34 +00002140
Richard Smith72553fc2014-01-23 23:53:27 +00002141 // If this has neither a name nor a bit width, something has gone seriously
2142 // wrong. Skip until the semi-colon or }.
Richard Smith4b5a9492014-01-24 22:34:35 +00002143 if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002144 // If so, skip until the semi-colon or a }.
2145 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2146 TryConsumeToken(tok::semi);
2147 return;
2148 }
John Thompson5bc5cbe2009-11-25 22:58:06 +00002149
Richard Smith72553fc2014-01-23 23:53:27 +00002150 // Check for a member function definition.
Richard Smith4b5a9492014-01-24 22:34:35 +00002151 if (BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002152 // MSVC permits pure specifier on inline functions defined at class scope.
Francois Pichet3abc9b82011-05-11 02:14:46 +00002153 // Hence check for =0 before checking for function definition.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002154 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Richard Smith72553fc2014-01-23 23:53:27 +00002155 DeclaratorInfo.isFunctionDeclarator() &&
Francois Pichet3abc9b82011-05-11 02:14:46 +00002156 NextToken().is(tok::numeric_constant)) {
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002157 EqualLoc = ConsumeToken();
Francois Pichet3abc9b82011-05-11 02:14:46 +00002158 Init = ParseInitializer();
2159 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002160 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002161 else
2162 HasInitializer = true;
Francois Pichet3abc9b82011-05-11 02:14:46 +00002163 }
2164
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002165 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002166 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002167 //
2168 // In C++11, a non-function declarator followed by an open brace is a
2169 // braced-init-list for an in-class member initialization, not an
2170 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002171 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002172 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002173 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002174 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002175 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002176 } else if (Tok.is(tok::equal)) {
2177 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002178 if (KW.is(tok::kw_default))
2179 DefinitionKind = FDK_Defaulted;
2180 else if (KW.is(tok::kw_delete))
2181 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002182 }
2183 }
2184
Michael Handdc016d2012-11-28 23:17:40 +00002185 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2186 // to a friend declaration, that declaration shall be a definition.
2187 if (DeclaratorInfo.isFunctionDeclarator() &&
2188 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2189 // Diagnose attributes that appear before decl specifier:
2190 // [[]] friend int foo();
2191 ProhibitAttributes(FnAttrs);
2192 }
2193
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002194 if (DefinitionKind) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002195 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002196 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002197 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002198 SkipUntil(tok::r_brace);
Michael Handdc016d2012-11-28 23:17:40 +00002199
Douglas Gregor8a4db832011-01-19 16:41:58 +00002200 // Consume the optional ';'
Alp Toker35d87032013-12-30 23:29:50 +00002201 TryConsumeToken(tok::semi);
2202
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002203 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002204 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002205
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002206 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002207 Diag(DeclaratorInfo.getIdentifierLoc(),
2208 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002209
Richard Smith2603b092012-11-15 22:54:20 +00002210 // Recover by treating the 'typedef' as spurious.
2211 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002212 }
2213
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002214 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002215 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002216 VS, DefinitionKind, Init);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002217
David Majnemer23252a32013-08-01 04:22:55 +00002218 if (FunDecl) {
2219 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2220 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2221 }
2222 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2223 LateParsedAttrs[i]->addDecl(FunDecl);
2224 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002225 }
2226 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002227
2228 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002229 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002230 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002231
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002232 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002233 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002234 }
2235
2236 // member-declarator-list:
2237 // member-declarator
2238 // member-declarator-list ',' member-declarator
2239
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002240 while (1) {
Richard Smith2b013182012-06-10 03:12:00 +00002241 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002242 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith938f40b2011-06-11 17:19:42 +00002243 if (BitfieldSize.get()) {
2244 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002245 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith938f40b2011-06-11 17:19:42 +00002246 } else {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002247 HasInitializer = true;
Richard Smith2b013182012-06-10 03:12:00 +00002248 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2249 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smith2b013182012-06-10 03:12:00 +00002250 != DeclSpec::SCS_typedef)
2251 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002252 }
2253 }
2254
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002255 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002256 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002257 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002258
Rafael Espindola0a67e2f2013-01-08 20:44:06 +00002259 NamedDecl *ThisDecl = 0;
John McCall07e91c02009-08-06 02:15:43 +00002260 if (DS.isFriendSpecified()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002261 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
Michael Handdc016d2012-11-28 23:17:40 +00002262 // to a friend declaration, that declaration shall be a definition.
2263 //
Richard Smith72553fc2014-01-23 23:53:27 +00002264 // Diagnose attributes that appear in a friend member function declarator:
2265 // friend int foo [[]] ();
Michael Handdc016d2012-11-28 23:17:40 +00002266 SmallVector<SourceRange, 4> Ranges;
2267 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
Richard Smith72553fc2014-01-23 23:53:27 +00002268 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
2269 E = Ranges.end(); I != E; ++I)
2270 Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
Michael Handdc016d2012-11-28 23:17:40 +00002271
Richard Smith72553fc2014-01-23 23:53:27 +00002272 // TODO: handle initializers, VS, bitfields, 'delete'
Douglas Gregor0be31a22010-07-02 17:43:08 +00002273 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002274 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002275 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002276 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002277 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002278 TemplateParams,
John McCall07e91c02009-08-06 02:15:43 +00002279 BitfieldSize.release(),
Richard Smith2b013182012-06-10 03:12:00 +00002280 VS, HasInClassInit);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002281
2282 if (VarTemplateDecl *VT =
2283 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : 0)
2284 // Re-direct this decl to refer to the templated decl so that we can
2285 // initialize it.
2286 ThisDecl = VT->getTemplatedDecl();
2287
David Majnemer23252a32013-08-01 04:22:55 +00002288 if (ThisDecl && AccessAttrs)
Richard Smithf8a75c32013-08-29 00:47:48 +00002289 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00002290 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002291
Douglas Gregor728d00b2011-10-10 14:49:18 +00002292 // Handle the initializer.
David Blaikie35506f82013-01-30 01:22:18 +00002293 if (HasInClassInit != ICIS_NoInit &&
2294 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2295 DeclSpec::SCS_static) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002296 // The initializer was deferred; parse it and cache the tokens.
David Majnemer23252a32013-08-01 04:22:55 +00002297 Diag(Tok, getLangOpts().CPlusPlus11
2298 ? diag::warn_cxx98_compat_nonstatic_member_init
2299 : diag::ext_nonstatic_member_init);
Richard Smith5d164bc2011-10-15 05:09:34 +00002300
Richard Smith938f40b2011-06-11 17:19:42 +00002301 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002302 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2303 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002304 //
2305 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002306 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002307 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002308 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemer23252a32013-08-01 04:22:55 +00002309
2310 // Avoid later warnings about a class member of incomplete type.
David Blaikiecdd91db2012-02-14 09:00:46 +00002311 if (ThisDecl)
David Blaikiecdd91db2012-02-14 09:00:46 +00002312 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002313 } else
2314 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002315 } else if (HasInitializer) {
2316 // Normal initializer.
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002317 if (!Init.isUsable())
David Majnemer23252a32013-08-01 04:22:55 +00002318 Init = ParseCXXMemberInitializer(
2319 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2320
Douglas Gregor728d00b2011-10-10 14:49:18 +00002321 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002322 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002323 else if (ThisDecl)
Sebastian Redleef474c2012-02-22 10:50:08 +00002324 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smith74aeef52013-04-26 16:15:35 +00002325 DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002326 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor728d00b2011-10-10 14:49:18 +00002327 // No initializer.
Richard Smith74aeef52013-04-26 16:15:35 +00002328 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002329
Douglas Gregor728d00b2011-10-10 14:49:18 +00002330 if (ThisDecl) {
David Majnemer23252a32013-08-01 04:22:55 +00002331 if (!ThisDecl->isInvalidDecl()) {
2332 // Set the Decl for any late parsed attributes
2333 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2334 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2335
2336 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2337 LateParsedAttrs[i]->addDecl(ThisDecl);
2338 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002339 Actions.FinalizeDeclaration(ThisDecl);
2340 DeclsInGroup.push_back(ThisDecl);
David Majnemer23252a32013-08-01 04:22:55 +00002341
2342 if (DeclaratorInfo.isFunctionDeclarator() &&
2343 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2344 DeclSpec::SCS_typedef)
2345 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002346 }
David Majnemer23252a32013-08-01 04:22:55 +00002347 LateParsedAttrs.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002348
2349 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002350
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002351 // If we don't have a comma, it is either the end of the list (a ';')
2352 // or an error, bail out.
Alp Toker094e5212014-01-05 03:27:11 +00002353 SourceLocation CommaLoc;
2354 if (!TryConsumeToken(tok::comma, CommaLoc))
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002355 break;
Mike Stump11289f42009-09-09 15:08:12 +00002356
Richard Smithc8a79032012-01-09 22:31:44 +00002357 if (Tok.isAtStartOfLine() &&
2358 !MightBeDeclarator(Declarator::MemberContext)) {
2359 // This comma was followed by a line-break and something which can't be
2360 // the start of a declarator. The comma was probably a typo for a
2361 // semicolon.
2362 Diag(CommaLoc, diag::err_expected_semi_declaration)
2363 << FixItHint::CreateReplacement(CommaLoc, ";");
2364 ExpectSemi = false;
2365 break;
2366 }
Mike Stump11289f42009-09-09 15:08:12 +00002367
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002368 // Parse the next declarator.
2369 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002370 VS.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002371 BitfieldSize = true;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002372 Init = true;
2373 HasInitializer = false;
Richard Smith8d06f422012-01-12 23:53:29 +00002374 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002375
Richard Smith72553fc2014-01-23 23:53:27 +00002376 // GNU attributes are allowed before the second and subsequent declarator.
John McCall53fa7142010-12-24 02:08:15 +00002377 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002378
Richard Smith72553fc2014-01-23 23:53:27 +00002379 ParseCXXMemberDeclaratorBeforeInitializer(DeclaratorInfo, VS, BitfieldSize,
2380 LateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002381 }
2382
Richard Smithc8a79032012-01-09 22:31:44 +00002383 if (ExpectSemi &&
2384 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002385 // Skip to end of block or statement.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002386 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002387 // If we stopped at a ';', eat it.
Alp Toker35d87032013-12-30 23:29:50 +00002388 TryConsumeToken(tok::semi);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002389 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002390 }
2391
Rafael Espindolaab417692013-07-09 12:05:01 +00002392 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002393}
2394
Richard Smith938f40b2011-06-11 17:19:42 +00002395/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2396/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2397/// function definition. The location of the '=', if any, will be placed in
2398/// EqualLoc.
2399///
2400/// pure-specifier:
2401/// '= 0'
Sebastian Redleef474c2012-02-22 10:50:08 +00002402///
Richard Smith938f40b2011-06-11 17:19:42 +00002403/// brace-or-equal-initializer:
2404/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002405/// braced-init-list
2406///
Richard Smith938f40b2011-06-11 17:19:42 +00002407/// initializer-clause:
2408/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002409/// braced-init-list
2410///
Richard Smithda35e962013-11-09 04:52:51 +00002411/// defaulted/deleted function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002412/// '=' 'default'
2413/// '=' 'delete'
2414///
2415/// Prior to C++0x, the assignment-expression in an initializer-clause must
2416/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002417ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002418 SourceLocation &EqualLoc) {
2419 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2420 && "Data member initializer not starting with '=' or '{'");
2421
Douglas Gregor926410d2012-02-21 02:22:07 +00002422 EnterExpressionEvaluationContext Context(Actions,
2423 Sema::PotentiallyEvaluated,
2424 D);
Alp Toker094e5212014-01-05 03:27:11 +00002425 if (TryConsumeToken(tok::equal, EqualLoc)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002426 if (Tok.is(tok::kw_delete)) {
2427 // In principle, an initializer of '= delete p;' is legal, but it will
2428 // never type-check. It's better to diagnose it as an ill-formed expression
2429 // than as an ill-formed deleted non-function member.
2430 // An initializer of '= delete p, foo' will never be parsed, because
2431 // a top-level comma always ends the initializer expression.
2432 const Token &Next = NextToken();
2433 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
Richard Smith34f30512013-11-23 04:06:09 +00002434 Next.is(tok::eof)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002435 if (IsFunction)
2436 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2437 << 1 /* delete */;
2438 else
2439 Diag(ConsumeToken(), diag::err_deleted_non_function);
2440 return ExprResult();
2441 }
2442 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002443 if (IsFunction)
2444 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2445 << 0 /* default */;
2446 else
2447 Diag(ConsumeToken(), diag::err_default_special_members);
2448 return ExprResult();
2449 }
2450
Sebastian Redleef474c2012-02-22 10:50:08 +00002451 }
2452 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002453}
2454
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002455/// ParseCXXMemberSpecification - Parse the class definition.
2456///
2457/// member-specification:
2458/// member-declaration member-specification[opt]
2459/// access-specifier ':' member-specification[opt]
2460///
Joao Matose9a3ed42012-08-31 22:18:20 +00002461void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00002462 SourceLocation AttrFixitLoc,
Richard Smith4c96e992013-02-19 23:47:15 +00002463 ParsedAttributesWithRange &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00002464 unsigned TagType, Decl *TagDecl) {
2465 assert((TagType == DeclSpec::TST_struct ||
2466 TagType == DeclSpec::TST_interface ||
2467 TagType == DeclSpec::TST_union ||
2468 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2469
John McCallfaf5fb42010-08-26 23:41:50 +00002470 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2471 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00002472
Douglas Gregoredf8f392010-01-16 20:52:59 +00002473 // Determine whether this is a non-nested class. Note that local
2474 // classes are *not* considered to be nested classes.
2475 bool NonNestedClass = true;
2476 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002477 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002478 if (S->isClassScope()) {
2479 // We're inside a class scope, so this is a nested class.
2480 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00002481
2482 // The Microsoft extension __interface does not permit nested classes.
2483 if (getCurrentClass().IsInterface) {
2484 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2485 << /*ErrorType=*/6
2486 << (isa<NamedDecl>(TagDecl)
2487 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2488 : "<anonymous>");
2489 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00002490 break;
2491 }
2492
2493 if ((S->getFlags() & Scope::FnScope)) {
2494 // If we're in a function or function template declared in the
2495 // body of a class, then this is a local class rather than a
2496 // nested class.
2497 const Scope *Parent = S->getParent();
2498 if (Parent->isTemplateParamScope())
2499 Parent = Parent->getParent();
2500 if (Parent->isClassScope())
2501 break;
2502 }
2503 }
2504 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002505
2506 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00002507 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002508
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002509 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00002510 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2511 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002512
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002513 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002514 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002515
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002516 SourceLocation FinalLoc;
David Majnemera5433082013-10-18 00:33:31 +00002517 bool IsFinalSpelledSealed = false;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002518
2519 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002520 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemera5433082013-10-18 00:33:31 +00002521 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2522 assert((Specifier == VirtSpecifiers::VS_Final ||
2523 Specifier == VirtSpecifiers::VS_Sealed) &&
2524 "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00002525 FinalLoc = ConsumeToken();
David Majnemera5433082013-10-18 00:33:31 +00002526 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002527
David Majnemera5433082013-10-18 00:33:31 +00002528 if (TagType == DeclSpec::TST_interface)
John McCalldb632ac2012-09-25 07:32:39 +00002529 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemera5433082013-10-18 00:33:31 +00002530 << VirtSpecifiers::getSpecifierName(Specifier);
2531 else if (Specifier == VirtSpecifiers::VS_Final)
2532 Diag(FinalLoc, getLangOpts().CPlusPlus11
2533 ? diag::warn_cxx98_compat_override_control_keyword
2534 : diag::ext_override_control_keyword)
2535 << VirtSpecifiers::getSpecifierName(Specifier);
2536 else if (Specifier == VirtSpecifiers::VS_Sealed)
2537 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Michael Han9407e502012-11-26 22:54:45 +00002538
Michael Han309af292013-01-07 16:57:11 +00002539 // Parse any C++11 attributes after 'final' keyword.
2540 // These attributes are not allowed to appear here,
2541 // and the only possible place for them to appertain
2542 // to the class would be between class-key and class-name.
Richard Smith4c96e992013-02-19 23:47:15 +00002543 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002544 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002545
John McCall2d814c32009-12-19 21:48:58 +00002546 if (Tok.is(tok::colon)) {
2547 ParseBaseClause(TagDecl);
2548
2549 if (!Tok.is(tok::l_brace)) {
2550 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCall2ff380a2010-03-17 00:38:33 +00002551
2552 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002553 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002554 return;
2555 }
2556 }
2557
2558 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002559 BalancedDelimiterTracker T(*this, tok::l_brace);
2560 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00002561
John McCall08bede42010-05-28 08:11:17 +00002562 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00002563 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemera5433082013-10-18 00:33:31 +00002564 IsFinalSpelledSealed,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002565 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00002566
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002567 // C++ 11p3: Members of a class defined with the keyword class are private
2568 // by default. Members of a class defined with the keywords struct or union
2569 // are public by default.
2570 AccessSpecifier CurAS;
2571 if (TagType == DeclSpec::TST_class)
2572 CurAS = AS_private;
2573 else
2574 CurAS = AS_public;
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002575 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002576
Douglas Gregor9377c822010-06-21 22:31:09 +00002577 if (TagDecl) {
2578 // While we still have something to read, read the member-declarations.
Richard Smith34f30512013-11-23 04:06:09 +00002579 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Douglas Gregor9377c822010-06-21 22:31:09 +00002580 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002581
David Blaikiebbafb8a2012-03-11 07:00:24 +00002582 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet8f981d52011-05-25 10:19:49 +00002583 Tok.is(tok::kw___if_not_exists))) {
2584 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2585 continue;
2586 }
2587
Douglas Gregor9377c822010-06-21 22:31:09 +00002588 // Check for extraneous top-level semicolon.
2589 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002590 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor9377c822010-06-21 22:31:09 +00002591 continue;
2592 }
2593
Eli Friedmanec52f922012-02-23 23:47:16 +00002594 if (Tok.is(tok::annot_pragma_vis)) {
2595 HandlePragmaVisibility();
2596 continue;
2597 }
2598
2599 if (Tok.is(tok::annot_pragma_pack)) {
2600 HandlePragmaPack();
2601 continue;
2602 }
2603
Argyrios Kyrtzidis5c2021b2012-10-12 17:39:59 +00002604 if (Tok.is(tok::annot_pragma_align)) {
2605 HandlePragmaAlign();
2606 continue;
2607 }
2608
Alexey Bataeva769e072013-03-22 06:34:35 +00002609 if (Tok.is(tok::annot_pragma_openmp)) {
2610 ParseOpenMPDeclarativeDirective();
2611 continue;
2612 }
2613
David Majnemer4bb09802014-02-10 19:50:15 +00002614 if (Tok.is(tok::annot_pragma_ms_pointers_to_members)) {
2615 HandlePragmaMSPointersToMembers();
2616 continue;
2617 }
2618
Richard Smithda35e962013-11-09 04:52:51 +00002619 // If we see a namespace here, a close brace was missing somewhere.
2620 if (Tok.is(tok::kw_namespace)) {
Richard Smith2ac43ad2013-11-15 23:00:02 +00002621 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
Richard Smithda35e962013-11-09 04:52:51 +00002622 break;
2623 }
2624
Douglas Gregor9377c822010-06-21 22:31:09 +00002625 AccessSpecifier AS = getAccessSpecifierIfPresent();
2626 if (AS != AS_none) {
2627 // Current token is a C++ access specifier.
2628 CurAS = AS;
2629 SourceLocation ASLoc = Tok.getLocation();
David Blaikieeba32c22011-10-13 06:08:43 +00002630 unsigned TokLength = Tok.getLength();
Douglas Gregor9377c822010-06-21 22:31:09 +00002631 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002632 AccessAttrs.clear();
2633 MaybeParseGNUAttributes(AccessAttrs);
2634
David Blaikieeba32c22011-10-13 06:08:43 +00002635 SourceLocation EndLoc;
Alp Toker35d87032013-12-30 23:29:50 +00002636 if (TryConsumeToken(tok::colon, EndLoc)) {
2637 } else if (TryConsumeToken(tok::semi, EndLoc)) {
2638 Diag(EndLoc, diag::err_expected)
2639 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002640 } else {
2641 EndLoc = ASLoc.getLocWithOffset(TokLength);
Alp Toker35d87032013-12-30 23:29:50 +00002642 Diag(EndLoc, diag::err_expected)
2643 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002644 }
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002645
John McCalldb632ac2012-09-25 07:32:39 +00002646 // The Microsoft extension __interface does not permit non-public
2647 // access specifiers.
2648 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2649 Diag(ASLoc, diag::err_access_specifier_interface)
2650 << (CurAS == AS_protected);
2651 }
2652
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002653 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2654 AccessAttrs.getList())) {
2655 // found another attribute than only annotations
2656 AccessAttrs.clear();
2657 }
2658
Douglas Gregor9377c822010-06-21 22:31:09 +00002659 continue;
2660 }
2661
Douglas Gregor9377c822010-06-21 22:31:09 +00002662 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002663 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002664 }
2665
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002666 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00002667 } else {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002668 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002669 }
Mike Stump11289f42009-09-09 15:08:12 +00002670
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002671 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002672 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002673 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002674
John McCall08bede42010-05-28 08:11:17 +00002675 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002676 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002677 T.getOpenLocation(),
2678 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002679 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002680
Douglas Gregor433e0532012-04-16 18:27:27 +00002681 // C++11 [class.mem]p2:
2682 // Within the class member-specification, the class is regarded as complete
Richard Smith2331bbf2012-05-02 22:22:32 +00002683 // within function bodies, default arguments, and
Douglas Gregor433e0532012-04-16 18:27:27 +00002684 // brace-or-equal-initializers for non-static data members (including such
2685 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00002686 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002687 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00002688 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002689 // declarations and the lexed inline method definitions, along with any
2690 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00002691 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002692 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002693 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00002694
2695 // We've finished with all pending member declarations.
2696 Actions.ActOnFinishCXXMemberDecls();
2697
Richard Smith938f40b2011-06-11 17:19:42 +00002698 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002699 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00002700 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002701 }
2702
John McCall08bede42010-05-28 08:11:17 +00002703 if (TagDecl)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002704 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2705 T.getCloseLocation());
John McCall2ff380a2010-03-17 00:38:33 +00002706
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002707 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002708 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002709 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002710}
Douglas Gregore8381c02008-11-05 04:29:56 +00002711
Richard Smith2ac43ad2013-11-15 23:00:02 +00002712void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithda35e962013-11-09 04:52:51 +00002713 assert(Tok.is(tok::kw_namespace));
2714
2715 // FIXME: Suggest where the close brace should have gone by looking
2716 // at indentation changes within the definition body.
Richard Smith2ac43ad2013-11-15 23:00:02 +00002717 Diag(D->getLocation(),
2718 diag::err_missing_end_of_definition) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002719 Diag(Tok.getLocation(),
Richard Smith2ac43ad2013-11-15 23:00:02 +00002720 diag::note_missing_end_of_definition_before) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002721
2722 // Push '};' onto the token stream to recover.
2723 PP.EnterToken(Tok);
2724
2725 Tok.startToken();
2726 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
2727 Tok.setKind(tok::semi);
2728 PP.EnterToken(Tok);
2729
2730 Tok.setKind(tok::r_brace);
2731}
2732
Douglas Gregore8381c02008-11-05 04:29:56 +00002733/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2734/// which explicitly initializes the members or base classes of a
2735/// class (C++ [class.base.init]). For example, the three initializers
2736/// after the ':' in the Derived constructor below:
2737///
2738/// @code
2739/// class Base { };
2740/// class Derived : Base {
2741/// int x;
2742/// float f;
2743/// public:
2744/// Derived(float f) : Base(), x(17), f(f) { }
2745/// };
2746/// @endcode
2747///
Mike Stump11289f42009-09-09 15:08:12 +00002748/// [C++] ctor-initializer:
2749/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00002750///
Mike Stump11289f42009-09-09 15:08:12 +00002751/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00002752/// mem-initializer ...[opt]
2753/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00002754void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00002755 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2756
John Wiegley1c0675e2011-04-28 01:08:34 +00002757 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2758 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00002759 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002760
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002761 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002762 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002763
Douglas Gregore8381c02008-11-05 04:29:56 +00002764 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002765 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00002766 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2767 MemInitializers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002768 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002769 } else {
2770 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2771 if (!MemInit.isInvalid())
2772 MemInitializers.push_back(MemInit.get());
2773 else
2774 AnyErrors = true;
2775 }
2776
Douglas Gregore8381c02008-11-05 04:29:56 +00002777 if (Tok.is(tok::comma))
2778 ConsumeToken();
2779 else if (Tok.is(tok::l_brace))
2780 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00002781 // If the next token looks like a base or member initializer, assume that
2782 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00002783 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2784 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2785 Diag(Loc, diag::err_ctor_init_missing_comma)
2786 << FixItHint::CreateInsertion(Loc, ", ");
2787 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00002788 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alp Tokerec543272013-12-24 09:48:30 +00002789 Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
2790 << tok::comma;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002791 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregore8381c02008-11-05 04:29:56 +00002792 break;
2793 }
2794 } while (true);
2795
David Blaikie3fc2f912013-01-17 05:26:25 +00002796 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002797 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00002798}
2799
2800/// ParseMemInitializer - Parse a C++ member initializer, which is
2801/// part of a constructor initializer that explicitly initializes one
2802/// member or base class (C++ [class.base.init]). See
2803/// ParseConstructorInitializer for an example.
2804///
2805/// [C++] mem-initializer:
2806/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002807/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00002808///
Douglas Gregore8381c02008-11-05 04:29:56 +00002809/// [C++] mem-initializer-id:
2810/// '::'[opt] nested-name-specifier[opt] class-name
2811/// identifier
John McCall48871652010-08-21 09:40:31 +00002812Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00002813 // parse '::'[opt] nested-name-specifier[opt]
2814 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002815 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00002816 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002817 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002818 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00002819 if (TemplateId->Kind == TNK_Type_template ||
2820 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00002821 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002822 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00002823 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002824 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002825 }
David Blaikie186a8892012-01-24 06:03:59 +00002826 // Uses of decltype will already have been converted to annot_decltype by
2827 // ParseOptionalCXXScopeSpecifier at this point.
2828 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2829 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002830 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00002831 return true;
2832 }
Mike Stump11289f42009-09-09 15:08:12 +00002833
David Blaikie186a8892012-01-24 06:03:59 +00002834 IdentifierInfo *II = 0;
2835 DeclSpec DS(AttrFactory);
2836 SourceLocation IdLoc = Tok.getLocation();
2837 if (Tok.is(tok::annot_decltype)) {
2838 // Get the decltype expression, if there is one.
2839 ParseDecltypeSpecifier(DS);
2840 } else {
2841 if (Tok.is(tok::identifier))
2842 // Get the identifier. This may be a member name or a class name,
2843 // but we'll let the semantic analysis determine which it is.
2844 II = Tok.getIdentifierInfo();
2845 ConsumeToken();
2846 }
2847
Douglas Gregore8381c02008-11-05 04:29:56 +00002848
2849 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002850 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002851 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2852
Sebastian Redla74948d2011-09-24 17:48:25 +00002853 ExprResult InitList = ParseBraceInitializer();
2854 if (InitList.isInvalid())
2855 return true;
2856
2857 SourceLocation EllipsisLoc;
Alp Toker094e5212014-01-05 03:27:11 +00002858 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redla74948d2011-09-24 17:48:25 +00002859
2860 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002861 TemplateTypeTy, DS, IdLoc,
2862 InitList.take(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00002863 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002864 BalancedDelimiterTracker T(*this, tok::l_paren);
2865 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00002866
Sebastian Redl3da34892011-06-05 12:23:16 +00002867 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00002868 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00002869 CommaLocsTy CommaLocs;
2870 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002871 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00002872 return true;
2873 }
2874
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002875 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00002876
2877 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00002878 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00002879
2880 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002881 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenko139474d2013-05-09 23:51:52 +00002882 T.getOpenLocation(), ArgExprs,
2883 T.getCloseLocation(), EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00002884 }
2885
Alp Tokerec543272013-12-24 09:48:30 +00002886 if (getLangOpts().CPlusPlus11)
2887 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
2888 else
2889 return Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregore8381c02008-11-05 04:29:56 +00002890}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002891
Sebastian Redl965b0e32011-03-05 14:45:16 +00002892/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002893///
Douglas Gregor356513d2008-12-01 18:00:20 +00002894/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00002895/// dynamic-exception-specification
2896/// noexcept-specification
2897///
2898/// noexcept-specification:
2899/// 'noexcept'
2900/// 'noexcept' '(' constant-expression ')'
2901ExceptionSpecificationType
Richard Smith2331bbf2012-05-02 22:22:32 +00002902Parser::tryParseExceptionSpecification(
Douglas Gregor433e0532012-04-16 18:27:27 +00002903 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002904 SmallVectorImpl<ParsedType> &DynamicExceptions,
2905 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00002906 ExprResult &NoexceptExpr) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002907 ExceptionSpecificationType Result = EST_None;
2908
2909 // See if there's a dynamic specification.
2910 if (Tok.is(tok::kw_throw)) {
2911 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2912 DynamicExceptions,
2913 DynamicExceptionRanges);
2914 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2915 "Produced different number of exception types and ranges.");
2916 }
2917
2918 // If there's no noexcept specification, we're done.
2919 if (Tok.isNot(tok::kw_noexcept))
2920 return Result;
2921
Richard Smithb15c11c2011-10-17 23:06:20 +00002922 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2923
Sebastian Redl965b0e32011-03-05 14:45:16 +00002924 // If we already had a dynamic specification, parse the noexcept for,
2925 // recovery, but emit a diagnostic and don't store the results.
2926 SourceRange NoexceptRange;
2927 ExceptionSpecificationType NoexceptType = EST_None;
2928
2929 SourceLocation KeywordLoc = ConsumeToken();
2930 if (Tok.is(tok::l_paren)) {
2931 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002932 BalancedDelimiterTracker T(*this, tok::l_paren);
2933 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00002934 NoexceptType = EST_ComputedNoexcept;
2935 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002936 // The argument must be contextually convertible to bool. We use
2937 // ActOnBooleanCondition for this purpose.
2938 if (!NoexceptExpr.isInvalid())
2939 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2940 NoexceptExpr.get());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002941 T.consumeClose();
2942 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl965b0e32011-03-05 14:45:16 +00002943 } else {
2944 // There is no argument.
2945 NoexceptType = EST_BasicNoexcept;
2946 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2947 }
2948
2949 if (Result == EST_None) {
2950 SpecificationRange = NoexceptRange;
2951 Result = NoexceptType;
2952
2953 // If there's a dynamic specification after a noexcept specification,
2954 // parse that and ignore the results.
2955 if (Tok.is(tok::kw_throw)) {
2956 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2957 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2958 DynamicExceptionRanges);
2959 }
2960 } else {
2961 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2962 }
2963
2964 return Result;
2965}
2966
Richard Smith8ca78a12013-06-13 02:02:51 +00002967static void diagnoseDynamicExceptionSpecification(
2968 Parser &P, const SourceRange &Range, bool IsNoexcept) {
2969 if (P.getLangOpts().CPlusPlus11) {
2970 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
2971 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
2972 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
2973 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
2974 }
2975}
2976
Sebastian Redl965b0e32011-03-05 14:45:16 +00002977/// ParseDynamicExceptionSpecification - Parse a C++
2978/// dynamic-exception-specification (C++ [except.spec]).
2979///
2980/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00002981/// 'throw' '(' type-id-list [opt] ')'
2982/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00002983///
Douglas Gregor356513d2008-12-01 18:00:20 +00002984/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00002985/// type-id ... [opt]
2986/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002987///
Sebastian Redl965b0e32011-03-05 14:45:16 +00002988ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2989 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002990 SmallVectorImpl<ParsedType> &Exceptions,
2991 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002992 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00002993
Sebastian Redl965b0e32011-03-05 14:45:16 +00002994 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002995 BalancedDelimiterTracker T(*this, tok::l_paren);
2996 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002997 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2998 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002999 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003000 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003001
Douglas Gregor356513d2008-12-01 18:00:20 +00003002 // Parse throw(...), a Microsoft extension that means "this function
3003 // can throw anything".
3004 if (Tok.is(tok::ellipsis)) {
3005 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003006 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00003007 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003008 T.consumeClose();
3009 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003010 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003011 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00003012 }
3013
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003014 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00003015 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003016 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00003017 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00003018
Douglas Gregor830837d2010-12-20 23:57:46 +00003019 if (Tok.is(tok::ellipsis)) {
3020 // C++0x [temp.variadic]p5:
3021 // - In a dynamic-exception-specification (15.4); the pattern is a
3022 // type-id.
3023 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003024 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00003025 if (!Res.isInvalid())
3026 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3027 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003028
Sebastian Redld6434562009-05-29 18:02:33 +00003029 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003030 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00003031 Ranges.push_back(Range);
3032 }
Alp Toker97650562014-01-10 11:19:30 +00003033
3034 if (!TryConsumeToken(tok::comma))
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003035 break;
3036 }
3037
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003038 T.consumeClose();
3039 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003040 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3041 Exceptions.empty());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003042 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003043}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003044
Douglas Gregor7fb25412010-10-01 18:44:50 +00003045/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3046/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003047TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00003048 assert(Tok.is(tok::arrow) && "expected arrow");
3049
3050 ConsumeToken();
3051
Richard Smithbfdb1082012-03-12 08:56:40 +00003052 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00003053}
3054
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003055/// \brief We have just started parsing the definition of a new class,
3056/// so push that class onto our stack of classes that is currently
3057/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00003058Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00003059Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3060 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003061 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003062 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00003063 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00003064 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003065}
3066
3067/// \brief Deallocate the given parsed class and all of its nested
3068/// classes.
3069void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00003070 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3071 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003072 delete Class;
3073}
3074
3075/// \brief Pop the top class of the stack of classes that are
3076/// currently being parsed.
3077///
3078/// This routine should be called when we have finished parsing the
3079/// definition of a class, but have not yet popped the Scope
3080/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00003081void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003082 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00003083
John McCallc1465822011-02-14 07:13:47 +00003084 Actions.PopParsingClass(state);
3085
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003086 ParsingClass *Victim = ClassStack.top();
3087 ClassStack.pop();
3088 if (Victim->TopLevelClass) {
3089 // Deallocate all of the nested classes of this class,
3090 // recursively: we don't need to keep any of this information.
3091 DeallocateParsedClasses(Victim);
3092 return;
Mike Stump11289f42009-09-09 15:08:12 +00003093 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003094 assert(!ClassStack.empty() && "Missing top-level class?");
3095
Douglas Gregorefc46952010-10-12 16:25:54 +00003096 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003097 // The victim is a nested class, but we will not need to perform
3098 // any processing after the definition of this class since it has
3099 // no members whose handling was delayed. Therefore, we can just
3100 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00003101 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003102 return;
3103 }
3104
3105 // This nested class has some members that will need to be processed
3106 // after the top-level class is completely defined. Therefore, add
3107 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003108 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00003109 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00003110 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003111}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003112
Richard Smith3dff2512012-04-10 03:25:07 +00003113/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3114///
3115/// \return the parsed identifier on success, and 0 if the next token is not an
3116/// attribute-token.
3117///
3118/// C++11 [dcl.attr.grammar]p3:
3119/// If a keyword or an alternative token that satisfies the syntactic
3120/// requirements of an identifier is contained in an attribute-token,
3121/// it is considered an identifier.
3122IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3123 switch (Tok.getKind()) {
3124 default:
3125 // Identifiers and keywords have identifier info attached.
3126 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3127 Loc = ConsumeToken();
3128 return II;
3129 }
3130 return 0;
3131
3132 case tok::ampamp: // 'and'
3133 case tok::pipe: // 'bitor'
3134 case tok::pipepipe: // 'or'
3135 case tok::caret: // 'xor'
3136 case tok::tilde: // 'compl'
3137 case tok::amp: // 'bitand'
3138 case tok::ampequal: // 'and_eq'
3139 case tok::pipeequal: // 'or_eq'
3140 case tok::caretequal: // 'xor_eq'
3141 case tok::exclaim: // 'not'
3142 case tok::exclaimequal: // 'not_eq'
3143 // Alternative tokens do not have identifier info, but their spelling
3144 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003145 SmallString<8> SpellingBuf;
Richard Smith3dff2512012-04-10 03:25:07 +00003146 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rosea7d03842013-02-08 22:30:41 +00003147 if (isLetter(Spelling[0])) {
Richard Smith3dff2512012-04-10 03:25:07 +00003148 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003149 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003150 }
3151 return 0;
3152 }
3153}
3154
Michael Han23214e52012-10-03 01:56:22 +00003155static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3156 IdentifierInfo *ScopeName) {
3157 switch (AttributeList::getKind(AttrName, ScopeName,
3158 AttributeList::AS_CXX11)) {
3159 case AttributeList::AT_CarriesDependency:
3160 case AttributeList::AT_FallThrough:
Richard Smith10876ef2013-01-17 01:30:42 +00003161 case AttributeList::AT_CXX11NoReturn: {
Michael Han23214e52012-10-03 01:56:22 +00003162 return true;
3163 }
3164
3165 default:
3166 return false;
3167 }
3168}
3169
Richard Smith3dff2512012-04-10 03:25:07 +00003170/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003171/// only parses standard attributes.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003172///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003173/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003174/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003175/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003176///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003177/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003178/// attribute[opt]
3179/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003180/// attribute '...'
3181/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003182///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003183/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003184/// attribute-token attribute-argument-clause[opt]
3185///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003186/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003187/// identifier
3188/// attribute-scoped-token
3189///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003190/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003191/// attribute-namespace '::' identifier
3192///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003193/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003194/// identifier
3195///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003196/// [C++11] attribute-argument-clause:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003197/// '(' balanced-token-seq ')'
3198///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003199/// [C++11] balanced-token-seq:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003200/// balanced-token
3201/// balanced-token-seq balanced-token
3202///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003203/// [C++11] balanced-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003204/// '(' balanced-token-seq ')'
3205/// '[' balanced-token-seq ']'
3206/// '{' balanced-token-seq '}'
3207/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smith3dff2512012-04-10 03:25:07 +00003208void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003209 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003210 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003211 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003212 ParseAlignmentSpecifier(attrs, endLoc);
3213 return;
3214 }
3215
Alexis Hunt96d5c762009-11-21 08:43:09 +00003216 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003217 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003218
Richard Smithf679b5b2011-10-14 20:48:27 +00003219 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3220
Alexis Hunt96d5c762009-11-21 08:43:09 +00003221 ConsumeBracket();
3222 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003223
Richard Smith10876ef2013-01-17 01:30:42 +00003224 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3225
Richard Smith3dff2512012-04-10 03:25:07 +00003226 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003227 // attribute not present
Alp Toker97650562014-01-10 11:19:30 +00003228 if (TryConsumeToken(tok::comma))
Alexis Hunt96d5c762009-11-21 08:43:09 +00003229 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003230
Richard Smith3dff2512012-04-10 03:25:07 +00003231 SourceLocation ScopeLoc, AttrLoc;
3232 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3233
3234 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3235 if (!AttrName)
3236 // Break out to the "expected ']'" diagnostic.
3237 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003238
Alexis Hunt96d5c762009-11-21 08:43:09 +00003239 // scoped attribute
Alp Toker97650562014-01-10 11:19:30 +00003240 if (TryConsumeToken(tok::coloncolon)) {
Richard Smith3dff2512012-04-10 03:25:07 +00003241 ScopeName = AttrName;
3242 ScopeLoc = AttrLoc;
3243
3244 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3245 if (!AttrName) {
Alp Tokerec543272013-12-24 09:48:30 +00003246 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003247 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003248 continue;
3249 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003250 }
3251
Michael Han23214e52012-10-03 01:56:22 +00003252 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003253 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003254
Richard Smith10876ef2013-01-17 01:30:42 +00003255 if (StandardAttr &&
3256 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3257 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3258 << AttrName << SourceRange(SeenAttrs[AttrName]);
3259
Michael Han23214e52012-10-03 01:56:22 +00003260 // Parse attribute arguments
3261 if (Tok.is(tok::l_paren)) {
3262 if (ScopeName && ScopeName->getName() == "gnu") {
3263 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +00003264 ScopeName, ScopeLoc, AttributeList::AS_CXX11, 0);
Michael Han23214e52012-10-03 01:56:22 +00003265 AttrParsed = true;
3266 } else {
3267 if (StandardAttr)
3268 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3269 << AttrName->getName();
3270
3271 // FIXME: handle other formats of c++11 attribute arguments
3272 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003273 SkipUntil(tok::r_paren);
Michael Han23214e52012-10-03 01:56:22 +00003274 }
3275 }
3276
3277 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003278 attrs.addNew(AttrName,
3279 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3280 AttrLoc),
Aaron Ballman00e99962013-08-31 01:11:41 +00003281 ScopeName, ScopeLoc, 0, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003282
Alp Toker97650562014-01-10 11:19:30 +00003283 if (TryConsumeToken(tok::ellipsis))
Michael Han23214e52012-10-03 01:56:22 +00003284 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3285 << AttrName->getName();
Alexis Hunt96d5c762009-11-21 08:43:09 +00003286 }
3287
Alp Toker383d2c42014-01-01 03:08:43 +00003288 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003289 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003290 if (endLoc)
3291 *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003292 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003293 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003294}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003295
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003296/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003297///
3298/// attribute-specifier-seq:
3299/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003300void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003301 SourceLocation *endLoc) {
Richard Smith4cabd042013-02-22 09:15:49 +00003302 assert(getLangOpts().CPlusPlus11);
3303
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003304 SourceLocation StartLoc = Tok.getLocation(), Loc;
3305 if (!endLoc)
3306 endLoc = &Loc;
3307
Douglas Gregor6f981002011-10-07 20:35:25 +00003308 do {
Richard Smith3dff2512012-04-10 03:25:07 +00003309 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003310 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003311
3312 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003313}
3314
Richard Smithc2c8bb82013-10-15 01:34:54 +00003315void Parser::DiagnoseAndSkipCXX11Attributes() {
3316 if (!isCXX11AttributeSpecifier())
3317 return;
3318
3319 // Start and end location of an attribute or an attribute list.
3320 SourceLocation StartLoc = Tok.getLocation();
3321 SourceLocation EndLoc;
3322
3323 do {
3324 if (Tok.is(tok::l_square)) {
3325 BalancedDelimiterTracker T(*this, tok::l_square);
3326 T.consumeOpen();
3327 T.skipToEnd();
3328 EndLoc = T.getCloseLocation();
3329 } else {
3330 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3331 ConsumeToken();
3332 BalancedDelimiterTracker T(*this, tok::l_paren);
3333 if (!T.consumeOpen())
3334 T.skipToEnd();
3335 EndLoc = T.getCloseLocation();
3336 }
3337 } while (isCXX11AttributeSpecifier());
3338
3339 if (EndLoc.isValid()) {
3340 SourceRange Range(StartLoc, EndLoc);
3341 Diag(StartLoc, diag::err_attributes_not_allowed)
3342 << Range;
3343 }
3344}
3345
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003346/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3347///
3348/// [MS] ms-attribute:
3349/// '[' token-seq ']'
3350///
3351/// [MS] ms-attribute-seq:
3352/// ms-attribute[opt]
3353/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00003354void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3355 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003356 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3357
3358 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003359 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003360 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003361 SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
John McCall53fa7142010-12-24 02:08:15 +00003362 if (endLoc) *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003363 ExpectAndConsume(tok::r_square);
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003364 }
3365}
Francois Pichet8f981d52011-05-25 10:19:49 +00003366
3367void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3368 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00003369 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00003370 if (ParseMicrosoftIfExistsCondition(Result))
3371 return;
3372
Douglas Gregor43edb322011-10-24 22:31:10 +00003373 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3374 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00003375 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet8f981d52011-05-25 10:19:49 +00003376 return;
3377 }
Francois Pichet8f981d52011-05-25 10:19:49 +00003378
Douglas Gregor43edb322011-10-24 22:31:10 +00003379 switch (Result.Behavior) {
3380 case IEB_Parse:
3381 // Parse the declarations below.
3382 break;
3383
3384 case IEB_Dependent:
3385 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3386 << Result.IsIfExists;
3387 // Fall through to skip.
3388
3389 case IEB_Skip:
3390 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00003391 return;
3392 }
3393
Richard Smith34f30512013-11-23 04:06:09 +00003394 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003395 // __if_exists, __if_not_exists can nest.
3396 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3397 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3398 continue;
3399 }
3400
3401 // Check for extraneous top-level semicolon.
3402 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003403 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00003404 continue;
3405 }
3406
3407 AccessSpecifier AS = getAccessSpecifierIfPresent();
3408 if (AS != AS_none) {
3409 // Current token is a C++ access specifier.
3410 CurAS = AS;
3411 SourceLocation ASLoc = Tok.getLocation();
3412 ConsumeToken();
3413 if (Tok.is(tok::colon))
3414 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3415 else
Alp Toker35d87032013-12-30 23:29:50 +00003416 Diag(Tok, diag::err_expected) << tok::colon;
Francois Pichet8f981d52011-05-25 10:19:49 +00003417 ConsumeToken();
3418 continue;
3419 }
3420
3421 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00003422 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet8f981d52011-05-25 10:19:49 +00003423 }
Douglas Gregor43edb322011-10-24 22:31:10 +00003424
3425 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00003426}