blob: 6098c0c8970f5a3262949066900bfdede5d55491 [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"
Jordan Rosea7d03842013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Basic/OperatorKinds.h"
Larisse Voufo39a1e502013-08-06 01:03:05 +000018#include "clang/AST/DeclTemplate.h"
Chris Lattner60f36222009-01-29 05:15:15 +000019#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/DeclSpec.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000022#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/Sema/Scope.h"
John McCalldb632ac2012-09-25 07:32:39 +000024#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Chris Lattnera5235172007-08-25 06:57:03 +000026using namespace clang;
27
28/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redl67667942010-08-27 23:12:46 +000029/// may either be a top level namespace or a block-level namespace alias. If
30/// there was an inline keyword, it has already been parsed.
Chris Lattnera5235172007-08-25 06:57:03 +000031///
32/// namespace-definition: [C++ 7.3: basic.namespace]
33/// named-namespace-definition
34/// unnamed-namespace-definition
35///
36/// unnamed-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000037/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000038///
39/// named-namespace-definition:
40/// original-namespace-definition
41/// extension-namespace-definition
42///
43/// original-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000044/// 'inline'[opt] 'namespace' identifier attributes[opt]
45/// '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000046///
47/// extension-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000048/// 'inline'[opt] 'namespace' original-namespace-name
49/// '{' namespace-body '}'
Mike Stump11289f42009-09-09 15:08:12 +000050///
Chris Lattnera5235172007-08-25 06:57:03 +000051/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
52/// 'namespace' identifier '=' qualified-namespace-specifier ';'
53///
John McCall48871652010-08-21 09:40:31 +000054Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redl67667942010-08-27 23:12:46 +000055 SourceLocation &DeclEnd,
56 SourceLocation InlineLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000057 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000058 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian4bf82622011-08-22 17:59:19 +000059 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000060
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000061 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000062 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000063 cutOffParsing();
64 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000065 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000066
Chris Lattnera5235172007-08-25 06:57:03 +000067 SourceLocation IdentLoc;
68 IdentifierInfo *Ident = 0;
Richard Trieu61384cb2011-05-26 20:11:09 +000069 std::vector<SourceLocation> ExtraIdentLoc;
70 std::vector<IdentifierInfo*> ExtraIdent;
71 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000072
73 Token attrTok;
Mike Stump11289f42009-09-09 15:08:12 +000074
Chris Lattner76c72282007-10-09 17:33:22 +000075 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000076 Ident = Tok.getIdentifierInfo();
77 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieu61384cb2011-05-26 20:11:09 +000078 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
79 ExtraNamespaceLoc.push_back(ConsumeToken());
80 ExtraIdent.push_back(Tok.getIdentifierInfo());
81 ExtraIdentLoc.push_back(ConsumeToken());
82 }
Chris Lattnera5235172007-08-25 06:57:03 +000083 }
Mike Stump11289f42009-09-09 15:08:12 +000084
Chris Lattnera5235172007-08-25 06:57:03 +000085 // Read label attributes, if present.
John McCall084e83d2011-03-24 11:26:52 +000086 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000087 if (Tok.is(tok::kw___attribute)) {
88 attrTok = Tok;
John McCall53fa7142010-12-24 02:08:15 +000089 ParseGNUAttributes(attrs);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000090 }
Mike Stump11289f42009-09-09 15:08:12 +000091
Douglas Gregor6b6bba42009-06-17 19:49:00 +000092 if (Tok.is(tok::equal)) {
Nico Weber729f1e22012-10-27 23:44:27 +000093 if (Ident == 0) {
Alp Tokerec543272013-12-24 09:48:30 +000094 Diag(Tok, diag::err_expected) << tok::identifier;
Nico Weber729f1e22012-10-27 23:44:27 +000095 // Skip to end of the definition and eat the ';'.
96 SkipUntil(tok::semi);
97 return 0;
98 }
John McCall53fa7142010-12-24 02:08:15 +000099 if (!attrs.empty())
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000100 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redl67667942010-08-27 23:12:46 +0000101 if (InlineLoc.isValid())
102 Diag(InlineLoc, diag::err_inline_namespace_alias)
103 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000104 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000105 }
Mike Stump11289f42009-09-09 15:08:12 +0000106
Richard Trieu61384cb2011-05-26 20:11:09 +0000107
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000108 BalancedDelimiterTracker T(*this, tok::l_brace);
109 if (T.consumeOpen()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000110 if (!ExtraIdent.empty()) {
111 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
112 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
113 }
Alp Tokerec543272013-12-24 09:48:30 +0000114
115 if (Ident)
116 Diag(Tok, diag::err_expected) << tok::l_brace;
117 else
118 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
119
John McCall48871652010-08-21 09:40:31 +0000120 return 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000121 }
Mike Stump11289f42009-09-09 15:08:12 +0000122
Douglas Gregor0be31a22010-07-02 17:43:08 +0000123 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
124 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
125 getCurScope()->getFnParent()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000126 if (!ExtraIdent.empty()) {
127 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
128 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
129 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000130 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000131 SkipUntil(tok::r_brace);
John McCall48871652010-08-21 09:40:31 +0000132 return 0;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000133 }
134
Richard Trieu61384cb2011-05-26 20:11:09 +0000135 if (!ExtraIdent.empty()) {
136 TentativeParsingAction TPA(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000137 SkipUntil(tok::r_brace, StopBeforeMatch);
Richard Trieu61384cb2011-05-26 20:11:09 +0000138 Token rBraceToken = Tok;
139 TPA.Revert();
140
141 if (!rBraceToken.is(tok::r_brace)) {
142 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
143 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
144 } else {
Benjamin Kramerf546f412011-05-26 21:32:30 +0000145 std::string NamespaceFix;
Richard Trieu61384cb2011-05-26 20:11:09 +0000146 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
147 E = ExtraIdent.end(); I != E; ++I) {
148 NamespaceFix += " { namespace ";
149 NamespaceFix += (*I)->getName();
150 }
Benjamin Kramerf546f412011-05-26 21:32:30 +0000151
Richard Trieu61384cb2011-05-26 20:11:09 +0000152 std::string RBraces;
Benjamin Kramerf546f412011-05-26 21:32:30 +0000153 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieu61384cb2011-05-26 20:11:09 +0000154 RBraces += "} ";
Benjamin Kramerf546f412011-05-26 21:32:30 +0000155
Richard Trieu61384cb2011-05-26 20:11:09 +0000156 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
157 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
158 ExtraIdentLoc.back()),
159 NamespaceFix)
160 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
161 }
162 }
163
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000164 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith5d164bc2011-10-15 05:09:34 +0000165 if (InlineLoc.isValid())
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000166 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000167 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000168
Chris Lattner4de55aa2009-03-29 14:02:43 +0000169 // Enter a scope for the namespace.
170 ParseScope NamespaceScope(this, Scope::DeclScope);
171
John McCall48871652010-08-21 09:40:31 +0000172 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000173 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000174 IdentLoc, Ident, T.getOpenLocation(),
175 attrs.getList());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000176
John McCallfaf5fb42010-08-26 23:41:50 +0000177 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
178 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000179
Richard Trieu61384cb2011-05-26 20:11:09 +0000180 // Parse the contents of the namespace. This includes parsing recovery on
181 // any improperly nested namespaces.
182 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000183 InlineLoc, attrs, T);
Mike Stump11289f42009-09-09 15:08:12 +0000184
Chris Lattner4de55aa2009-03-29 14:02:43 +0000185 // Leave the namespace scope.
186 NamespaceScope.Exit();
187
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000188 DeclEnd = T.getCloseLocation();
189 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000190
191 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000192}
Chris Lattner38376f12008-01-12 07:05:38 +0000193
Richard Trieu61384cb2011-05-26 20:11:09 +0000194/// ParseInnerNamespace - Parse the contents of a namespace.
195void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
196 std::vector<IdentifierInfo*>& Ident,
197 std::vector<SourceLocation>& NamespaceLoc,
198 unsigned int index, SourceLocation& InlineLoc,
Richard Trieu61384cb2011-05-26 20:11:09 +0000199 ParsedAttributes& attrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000200 BalancedDelimiterTracker &Tracker) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000201 if (index == Ident.size()) {
Richard Smith34f30512013-11-23 04:06:09 +0000202 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000203 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000204 MaybeParseCXX11Attributes(attrs);
Richard Trieu61384cb2011-05-26 20:11:09 +0000205 MaybeParseMicrosoftAttributes(attrs);
206 ParseExternalDeclaration(attrs);
207 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000208
209 // The caller is what called check -- we are simply calling
210 // the close for it.
211 Tracker.consumeClose();
Richard Trieu61384cb2011-05-26 20:11:09 +0000212
213 return;
214 }
215
216 // Parse improperly nested namespaces.
217 ParseScope NamespaceScope(this, Scope::DeclScope);
218 Decl *NamespcDecl =
219 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
220 NamespaceLoc[index], IdentLoc[index],
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000221 Ident[index], Tracker.getOpenLocation(),
222 attrs.getList());
Richard Trieu61384cb2011-05-26 20:11:09 +0000223
224 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000225 attrs, Tracker);
Richard Trieu61384cb2011-05-26 20:11:09 +0000226
227 NamespaceScope.Exit();
228
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000229 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieu61384cb2011-05-26 20:11:09 +0000230}
231
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000232/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
233/// alias definition.
234///
John McCall48871652010-08-21 09:40:31 +0000235Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall084e83d2011-03-24 11:26:52 +0000236 SourceLocation AliasLoc,
237 IdentifierInfo *Alias,
238 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000239 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000240
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000241 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000242
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000243 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000244 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000245 cutOffParsing();
246 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000247 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000248
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000249 CXXScopeSpec SS;
250 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000251 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000252
253 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
254 Diag(Tok, diag::err_expected_namespace_name);
255 // Skip to end of the definition and eat the ';'.
256 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000257 return 0;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000258 }
259
260 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000261 IdentifierInfo *Ident = Tok.getIdentifierInfo();
262 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000263
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000264 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000265 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000266 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
267 "", tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000268
Douglas Gregor0be31a22010-07-02 17:43:08 +0000269 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000270 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000271}
272
Chris Lattner38376f12008-01-12 07:05:38 +0000273/// ParseLinkage - We know that the current token is a string_literal
274/// and just before that, that extern was seen.
275///
276/// linkage-specification: [C++ 7.5p2: dcl.link]
277/// 'extern' string-literal '{' declaration-seq[opt] '}'
278/// 'extern' string-literal declaration
279///
Chris Lattner8ea64422010-11-09 20:15:55 +0000280Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregor15799fd2008-11-21 16:10:08 +0000281 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000282 SmallString<8> LangBuffer;
Douglas Gregordc970f02010-03-16 22:30:13 +0000283 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000284 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregordc970f02010-03-16 22:30:13 +0000285 if (Invalid)
John McCall48871652010-08-21 09:40:31 +0000286 return 0;
Chris Lattner38376f12008-01-12 07:05:38 +0000287
Richard Smithd67aea22012-03-06 03:21:47 +0000288 // FIXME: This is incorrect: linkage-specifiers are parsed in translation
289 // phase 7, so string-literal concatenation is supposed to occur.
290 // extern "" "C" "" "+" "+" { } is legal.
291 if (Tok.hasUDSuffix())
292 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattner38376f12008-01-12 07:05:38 +0000293 SourceLocation Loc = ConsumeStringToken();
Chris Lattner38376f12008-01-12 07:05:38 +0000294
Douglas Gregor07665a62009-01-05 19:45:36 +0000295 ParseScope LinkageScope(this, Scope::DeclScope);
John McCall48871652010-08-21 09:40:31 +0000296 Decl *LinkageSpec
Douglas Gregor0be31a22010-07-02 17:43:08 +0000297 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000298 DS.getSourceRange().getBegin(),
Benjamin Kramerbebee842010-05-03 13:08:54 +0000299 Loc, Lang,
Abramo Bagnaraea947882011-03-08 16:41:52 +0000300 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor07665a62009-01-05 19:45:36 +0000301 : SourceLocation());
302
John McCall084e83d2011-03-24 11:26:52 +0000303 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000304 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000305 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000306
Douglas Gregor07665a62009-01-05 19:45:36 +0000307 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara4d423992011-05-01 16:25:54 +0000308 // Reset the source range in DS, as the leading "extern"
309 // does not really belong to the inner declaration ...
310 DS.SetRangeStart(SourceLocation());
311 DS.SetRangeEnd(SourceLocation());
312 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000313 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000314 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000315 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor07665a62009-01-05 19:45:36 +0000316 SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000317 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000318
Douglas Gregorb65a9132010-02-07 08:38:28 +0000319 DS.abort();
320
John McCall53fa7142010-12-24 02:08:15 +0000321 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000322
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000323 BalancedDelimiterTracker T(*this, tok::l_brace);
324 T.consumeOpen();
Richard Smith34f30512013-11-23 04:06:09 +0000325 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
John McCall084e83d2011-03-24 11:26:52 +0000326 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000327 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000328 MaybeParseMicrosoftAttributes(attrs);
329 ParseExternalDeclaration(attrs);
Chris Lattner38376f12008-01-12 07:05:38 +0000330 }
331
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000332 T.consumeClose();
Chris Lattner8ea64422010-11-09 20:15:55 +0000333 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000334 T.getCloseLocation());
Chris Lattner38376f12008-01-12 07:05:38 +0000335}
Douglas Gregor556877c2008-04-13 21:30:24 +0000336
Douglas Gregord7c4d982008-12-30 03:27:21 +0000337/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
338/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000339Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000340 const ParsedTemplateInfo &TemplateInfo,
341 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000342 ParsedAttributesWithRange &attrs,
343 Decl **OwnedType) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000344 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000345 ObjCDeclContextSwitch ObjCDC(*this);
346
Douglas Gregord7c4d982008-12-30 03:27:21 +0000347 // Eat 'using'.
348 SourceLocation UsingLoc = ConsumeToken();
349
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000350 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000351 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000352 cutOffParsing();
353 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000354 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000355
John McCall9b72f892010-11-10 02:40:36 +0000356 // 'using namespace' means this is a using-directive.
357 if (Tok.is(tok::kw_namespace)) {
358 // Template parameters are always an error here.
359 if (TemplateInfo.Kind) {
360 SourceRange R = TemplateInfo.getSourceRange();
361 Diag(UsingLoc, diag::err_templated_using_directive)
362 << R << FixItHint::CreateRemoval(R);
363 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000364
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000365 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000366 }
367
Richard Smithdda56e42011-04-15 14:24:37 +0000368 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +0000369
370 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000371 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000372
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000373 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000374 AS_none, OwnedType);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000375}
376
377/// ParseUsingDirective - Parse C++ using-directive, assumes
378/// that current token is 'namespace' and 'using' was already parsed.
379///
380/// using-directive: [C++ 7.3.p4: namespace.udir]
381/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
382/// namespace-name ;
383/// [GNU] using-directive:
384/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
385/// namespace-name attributes[opt] ;
386///
John McCall48871652010-08-21 09:40:31 +0000387Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000388 SourceLocation UsingLoc,
389 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000390 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000391 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
392
393 // Eat 'namespace'.
394 SourceLocation NamespcLoc = ConsumeToken();
395
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000396 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000397 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000398 cutOffParsing();
399 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000400 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000401
Douglas Gregord7c4d982008-12-30 03:27:21 +0000402 CXXScopeSpec SS;
403 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000404 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000405
Douglas Gregord7c4d982008-12-30 03:27:21 +0000406 IdentifierInfo *NamespcName = 0;
407 SourceLocation IdentLoc = SourceLocation();
408
409 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000410 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000411 Diag(Tok, diag::err_expected_namespace_name);
412 // If there was invalid namespace name, skip to end of decl, and eat ';'.
413 SkipUntil(tok::semi);
414 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCall48871652010-08-21 09:40:31 +0000415 return 0;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000416 }
Mike Stump11289f42009-09-09 15:08:12 +0000417
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000418 // Parse identifier.
419 NamespcName = Tok.getIdentifierInfo();
420 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000421
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000422 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000423 bool GNUAttr = false;
424 if (Tok.is(tok::kw___attribute)) {
425 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000426 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000427 }
Mike Stump11289f42009-09-09 15:08:12 +0000428
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000429 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000430 DeclEnd = Tok.getLocation();
Chris Lattner34a95662009-06-14 00:07:48 +0000431 ExpectAndConsume(tok::semi,
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000432 GNUAttr ? diag::err_expected_semi_after_attribute_list
433 : diag::err_expected_semi_after_namespace_name,
434 "", tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000435
Douglas Gregor0be31a22010-07-02 17:43:08 +0000436 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000437 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000438}
439
Richard Smithdda56e42011-04-15 14:24:37 +0000440/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
441/// Assumes that 'using' was already seen.
Douglas Gregord7c4d982008-12-30 03:27:21 +0000442///
443/// using-declaration: [C++ 7.3.p3: namespace.udecl]
444/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000445/// unqualified-id
446/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000447///
Richard Smith810ad3e2013-01-29 10:02:16 +0000448/// alias-declaration: C++11 [dcl.dcl]p1
449/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smithdda56e42011-04-15 14:24:37 +0000450///
John McCall48871652010-08-21 09:40:31 +0000451Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000452 const ParsedTemplateInfo &TemplateInfo,
453 SourceLocation UsingLoc,
454 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000455 AccessSpecifier AS,
456 Decl **OwnedType) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000457 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000458 SourceLocation TypenameLoc;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000459 bool HasTypenameKeyword = false;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000460
Richard Smithc2c8bb82013-10-15 01:34:54 +0000461 // Check for misplaced attributes before the identifier in an
462 // alias-declaration.
463 ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
464 MaybeParseCXX11Attributes(MisplacedAttrs);
Douglas Gregorfec52632009-06-20 00:51:54 +0000465
466 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000467 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregorfec52632009-06-20 00:51:54 +0000468 if (Tok.is(tok::kw_typename)) {
Richard Smith54ecd982013-02-20 19:22:51 +0000469 TypenameLoc = ConsumeToken();
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000470 HasTypenameKeyword = true;
Douglas Gregorfec52632009-06-20 00:51:54 +0000471 }
Douglas Gregorfec52632009-06-20 00:51:54 +0000472
473 // Parse nested-name-specifier.
Richard Smith7447af42013-03-26 01:15:19 +0000474 IdentifierInfo *LastII = 0;
475 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
476 /*MayBePseudoDtor=*/0, /*IsTypename=*/false,
477 /*LastII=*/&LastII);
Douglas Gregorfec52632009-06-20 00:51:54 +0000478
Douglas Gregorfec52632009-06-20 00:51:54 +0000479 // Check nested-name specifier.
480 if (SS.isInvalid()) {
481 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000482 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000483 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000484
Richard Smith7447af42013-03-26 01:15:19 +0000485 SourceLocation TemplateKWLoc;
486 UnqualifiedId Name;
487
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000488 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000489 // destructor names and allow the action module to diagnose any semantic
490 // errors.
Richard Smith7447af42013-03-26 01:15:19 +0000491 //
492 // C++11 [class.qual]p2:
493 // [...] in a using-declaration that is a member-declaration, if the name
494 // specified after the nested-name-specifier is the same as the identifier
495 // or the simple-template-id's template-name in the last component of the
496 // nested-name-specifier, the name is [...] considered to name the
497 // constructor.
498 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
499 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
500 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
501 !SS.getScopeRep()->getAsNamespace() &&
502 !SS.getScopeRep()->getAsNamespaceAlias()) {
503 SourceLocation IdLoc = ConsumeToken();
504 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
505 Name.setConstructorName(Type, IdLoc, IdLoc);
506 } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
507 /*AllowDestructorName=*/ true,
508 /*AllowConstructorName=*/ true, ParsedType(),
509 TemplateKWLoc, Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000510 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000511 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000512 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000513
Richard Smithc2c8bb82013-10-15 01:34:54 +0000514 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smith37a45dd2013-10-24 01:21:09 +0000515 MaybeParseGNUAttributes(Attrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000516 MaybeParseCXX11Attributes(Attrs);
Richard Smithdda56e42011-04-15 14:24:37 +0000517
518 // Maybe this is an alias-declaration.
Richard Smithdda56e42011-04-15 14:24:37 +0000519 TypeResult TypeAlias;
Richard Smithc2c8bb82013-10-15 01:34:54 +0000520 bool IsAliasDecl = Tok.is(tok::equal);
Richard Smithdda56e42011-04-15 14:24:37 +0000521 if (IsAliasDecl) {
Richard Smithc2c8bb82013-10-15 01:34:54 +0000522 // If we had any misplaced attributes from earlier, this is where they
523 // should have been written.
524 if (MisplacedAttrs.Range.isValid()) {
525 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
526 << FixItHint::CreateInsertionFromRange(
527 Tok.getLocation(),
528 CharSourceRange::getTokenRange(MisplacedAttrs.Range))
529 << FixItHint::CreateRemoval(MisplacedAttrs.Range);
530 Attrs.takeAllFrom(MisplacedAttrs);
531 }
532
Richard Smithdda56e42011-04-15 14:24:37 +0000533 ConsumeToken();
534
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000535 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000536 diag::warn_cxx98_compat_alias_declaration :
537 diag::ext_alias_declaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000538
Richard Smith3f1b5d02011-05-05 21:57:07 +0000539 // Type alias templates cannot be specialized.
540 int SpecKind = -1;
Richard Smith14034022011-05-05 22:36:10 +0000541 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
542 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000543 SpecKind = 0;
544 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
545 SpecKind = 1;
546 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
547 SpecKind = 2;
548 if (SpecKind != -1) {
549 SourceRange Range;
550 if (SpecKind == 0)
551 Range = SourceRange(Name.TemplateId->LAngleLoc,
552 Name.TemplateId->RAngleLoc);
553 else
554 Range = TemplateInfo.getSourceRange();
555 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
556 << SpecKind << Range;
557 SkipUntil(tok::semi);
558 return 0;
559 }
560
Richard Smithdda56e42011-04-15 14:24:37 +0000561 // Name must be an identifier.
562 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
563 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
564 // No removal fixit: can't recover from this.
565 SkipUntil(tok::semi);
566 return 0;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000567 } else if (HasTypenameKeyword)
Richard Smithdda56e42011-04-15 14:24:37 +0000568 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
569 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
570 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
571 else if (SS.isNotEmpty())
572 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
573 << FixItHint::CreateRemoval(SS.getRange());
574
Richard Smith3f1b5d02011-05-05 21:57:07 +0000575 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
576 Declarator::AliasTemplateContext :
Richard Smith54ecd982013-02-20 19:22:51 +0000577 Declarator::AliasDeclContext, AS, OwnedType,
578 &Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000579 } else {
580 // C++11 attributes are not allowed on a using-declaration, but GNU ones
581 // are.
Richard Smithc2c8bb82013-10-15 01:34:54 +0000582 ProhibitAttributes(MisplacedAttrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000583 ProhibitAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000584
Richard Smithdda56e42011-04-15 14:24:37 +0000585 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith54ecd982013-02-20 19:22:51 +0000586 MaybeParseGNUAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000587 }
Mike Stump11289f42009-09-09 15:08:12 +0000588
Douglas Gregorfec52632009-06-20 00:51:54 +0000589 // Eat ';'.
590 DeclEnd = Tok.getLocation();
591 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith54ecd982013-02-20 19:22:51 +0000592 !Attrs.empty() ? "attributes list" :
Richard Smithdda56e42011-04-15 14:24:37 +0000593 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor220f4272009-11-04 16:30:06 +0000594 tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000595
John McCall9b72f892010-11-10 02:40:36 +0000596 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith810ad3e2013-01-29 10:02:16 +0000597 // In C++11, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000598 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000599 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-11-10 02:40:36 +0000600 SourceRange R = TemplateInfo.getSourceRange();
601 Diag(UsingLoc, diag::err_templated_using_declaration)
602 << R << FixItHint::CreateRemoval(R);
603
604 // Unfortunately, we have to bail out instead of recovering by
605 // ignoring the parameters, just in case the nested name specifier
606 // depends on the parameters.
607 return 0;
608 }
609
Douglas Gregor882a61a2011-09-26 14:30:28 +0000610 // "typename" keyword is allowed for identifiers only,
611 // because it may be a type definition.
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000612 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor882a61a2011-09-26 14:30:28 +0000613 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
614 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000615 // Proceed parsing, but reset the HasTypenameKeyword flag.
616 HasTypenameKeyword = false;
Douglas Gregor882a61a2011-09-26 14:30:28 +0000617 }
618
Richard Smith3f1b5d02011-05-05 21:57:07 +0000619 if (IsAliasDecl) {
620 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000621 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3f1b5d02011-05-05 21:57:07 +0000622 TemplateParams ? TemplateParams->data() : 0,
623 TemplateParams ? TemplateParams->size() : 0);
624 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith54ecd982013-02-20 19:22:51 +0000625 UsingLoc, Name, Attrs.getList(),
626 TypeAlias);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000627 }
Richard Smithdda56e42011-04-15 14:24:37 +0000628
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000629 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
630 /* HasUsingKeyword */ true, UsingLoc,
631 SS, Name, Attrs.getList(),
632 HasTypenameKeyword, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000633}
634
Benjamin Kramere56f3932011-12-23 17:00:35 +0000635/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000636///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000637/// [C++0x] static_assert-declaration:
638/// static_assert ( constant-expression , string-literal ) ;
639///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000640/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000641/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000642///
John McCall48871652010-08-21 09:40:31 +0000643Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000644 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
645 "Not a static_assert declaration");
646
David Blaikiebbafb8a2012-03-11 07:00:24 +0000647 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000648 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000649 if (Tok.is(tok::kw_static_assert))
650 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000651
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000652 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000653
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000654 BalancedDelimiterTracker T(*this, tok::l_paren);
655 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000656 Diag(Tok, diag::err_expected) << tok::l_paren;
Richard Smith76965712012-09-13 19:12:50 +0000657 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000658 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000659 }
Mike Stump11289f42009-09-09 15:08:12 +0000660
John McCalldadc5752010-08-24 06:29:42 +0000661 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000662 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000663 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000664 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000665 }
Mike Stump11289f42009-09-09 15:08:12 +0000666
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000667 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCall48871652010-08-21 09:40:31 +0000668 return 0;
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000669
Richard Smithf506eaf2012-03-05 23:20:05 +0000670 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000671 Diag(Tok, diag::err_expected_string_literal)
672 << /*Source='static_assert'*/1;
Richard Smith76965712012-09-13 19:12:50 +0000673 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000674 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000675 }
Mike Stump11289f42009-09-09 15:08:12 +0000676
John McCalldadc5752010-08-24 06:29:42 +0000677 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smithd67aea22012-03-06 03:21:47 +0000678 if (AssertMessage.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000679 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000680 return 0;
Richard Smithd67aea22012-03-06 03:21:47 +0000681 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000682
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000683 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000684
Chris Lattner49836b42009-04-02 04:16:50 +0000685 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000686 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000687
John McCallb268a282010-08-23 23:25:46 +0000688 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
689 AssertExpr.take(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000690 AssertMessage.take(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000691 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000692}
693
Richard Smith74aeef52013-04-26 16:15:35 +0000694/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson74948d02009-06-24 17:47:40 +0000695///
696/// 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +0000697/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson74948d02009-06-24 17:47:40 +0000698///
David Blaikie15a430a2011-12-04 05:04:18 +0000699SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
700 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
701 && "Not a decltype specifier");
702
David Blaikie15a430a2011-12-04 05:04:18 +0000703 ExprResult Result;
704 SourceLocation StartLoc = Tok.getLocation();
705 SourceLocation EndLoc;
706
707 if (Tok.is(tok::annot_decltype)) {
708 Result = getExprAnnotation(Tok);
709 EndLoc = Tok.getAnnotationEndLoc();
710 ConsumeToken();
711 if (Result.isInvalid()) {
712 DS.SetTypeSpecError();
713 return EndLoc;
714 }
715 } else {
Richard Smith324df552012-02-24 22:30:04 +0000716 if (Tok.getIdentifierInfo()->isStr("decltype"))
717 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000718
David Blaikie15a430a2011-12-04 05:04:18 +0000719 ConsumeToken();
720
721 BalancedDelimiterTracker T(*this, tok::l_paren);
722 if (T.expectAndConsume(diag::err_expected_lparen_after,
723 "decltype", tok::r_paren)) {
724 DS.SetTypeSpecError();
725 return T.getOpenLocation() == Tok.getLocation() ?
726 StartLoc : T.getOpenLocation();
727 }
728
Richard Smith74aeef52013-04-26 16:15:35 +0000729 // Check for C++1y 'decltype(auto)'.
730 if (Tok.is(tok::kw_auto)) {
731 // No need to disambiguate here: an expression can't start with 'auto',
732 // because the typename-specifier in a function-style cast operation can't
733 // be 'auto'.
734 Diag(Tok.getLocation(),
735 getLangOpts().CPlusPlus1y
736 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
737 : diag::ext_decltype_auto_type_specifier);
738 ConsumeToken();
739 } else {
740 // Parse the expression
David Blaikie15a430a2011-12-04 05:04:18 +0000741
Richard Smith74aeef52013-04-26 16:15:35 +0000742 // C++11 [dcl.type.simple]p4:
743 // The operand of the decltype specifier is an unevaluated operand.
744 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
745 0, /*IsDecltype=*/true);
746 Result = ParseExpression();
747 if (Result.isInvalid()) {
748 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000749 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smith74aeef52013-04-26 16:15:35 +0000750 EndLoc = ConsumeParen();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000751 } else {
Richard Smith74aeef52013-04-26 16:15:35 +0000752 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
753 // Backtrack to get the location of the last token before the semi.
754 PP.RevertCachedTokens(2);
755 ConsumeToken(); // the semi.
756 EndLoc = ConsumeAnyToken();
757 assert(Tok.is(tok::semi));
758 } else {
759 EndLoc = Tok.getLocation();
760 }
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000761 }
Richard Smith74aeef52013-04-26 16:15:35 +0000762 return EndLoc;
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000763 }
Richard Smith74aeef52013-04-26 16:15:35 +0000764
765 Result = Actions.ActOnDecltypeExpression(Result.take());
David Blaikie15a430a2011-12-04 05:04:18 +0000766 }
767
768 // Match the ')'
769 T.consumeClose();
770 if (T.getCloseLocation().isInvalid()) {
771 DS.SetTypeSpecError();
772 // FIXME: this should return the location of the last token
773 // that was consumed (by "consumeClose()")
774 return T.getCloseLocation();
775 }
776
Richard Smithfd555f62012-02-22 02:04:18 +0000777 if (Result.isInvalid()) {
778 DS.SetTypeSpecError();
779 return T.getCloseLocation();
780 }
781
David Blaikie15a430a2011-12-04 05:04:18 +0000782 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000783 }
Richard Smith74aeef52013-04-26 16:15:35 +0000784 assert(!Result.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000785
Anders Carlsson74948d02009-06-24 17:47:40 +0000786 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000787 unsigned DiagID;
Anders Carlsson74948d02009-06-24 17:47:40 +0000788 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smith74aeef52013-04-26 16:15:35 +0000789 if (Result.get()
790 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
791 DiagID, Result.release())
792 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
793 DiagID)) {
John McCall49bfce42009-08-03 20:12:06 +0000794 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-12-04 05:04:18 +0000795 DS.SetTypeSpecError();
796 }
797 return EndLoc;
798}
799
800void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
801 SourceLocation StartLoc,
802 SourceLocation EndLoc) {
803 // make sure we have a token we can turn into an annotation token
804 if (PP.isBacktrackEnabled())
805 PP.RevertCachedTokens(1);
806 else
807 PP.EnterToken(Tok);
808
809 Tok.setKind(tok::annot_decltype);
Richard Smith74aeef52013-04-26 16:15:35 +0000810 setExprAnnotation(Tok,
811 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
812 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
813 ExprError());
David Blaikie15a430a2011-12-04 05:04:18 +0000814 Tok.setAnnotationEndLoc(EndLoc);
815 Tok.setLocation(StartLoc);
816 PP.AnnotateCachedTokens(Tok);
Anders Carlsson74948d02009-06-24 17:47:40 +0000817}
818
Alexis Hunt4a257072011-05-19 05:37:45 +0000819void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
820 assert(Tok.is(tok::kw___underlying_type) &&
821 "Not an underlying type specifier");
822
823 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000824 BalancedDelimiterTracker T(*this, tok::l_paren);
825 if (T.expectAndConsume(diag::err_expected_lparen_after,
826 "__underlying_type", tok::r_paren)) {
Alexis Hunt4a257072011-05-19 05:37:45 +0000827 return;
828 }
829
830 TypeResult Result = ParseTypeName();
831 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000832 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt4a257072011-05-19 05:37:45 +0000833 return;
834 }
835
836 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000837 T.consumeClose();
838 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +0000839 return;
840
841 const char *PrevSpec = 0;
842 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000843 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Alexis Hunt4a257072011-05-19 05:37:45 +0000844 DiagID, Result.release()))
845 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanellaa90af722013-07-06 18:54:58 +0000846 DS.setTypeofParensRange(T.getRange());
Alexis Hunt4a257072011-05-19 05:37:45 +0000847}
848
David Blaikie00ee7a082011-10-25 15:01:20 +0000849/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
850/// class name or decltype-specifier. Note that we only check that the result
851/// names a type; semantic analysis will need to verify that the type names a
852/// class. The result is either a type or null, depending on whether a type
853/// name was found.
Douglas Gregor831c93f2008-11-05 20:51:48 +0000854///
Richard Smith4c96e992013-02-19 23:47:15 +0000855/// base-type-specifier: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000856/// class-or-decltype
Richard Smith4c96e992013-02-19 23:47:15 +0000857/// class-or-decltype: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000858/// nested-name-specifier[opt] class-name
859/// decltype-specifier
Richard Smith4c96e992013-02-19 23:47:15 +0000860/// class-name: [C++ class.name]
Douglas Gregor831c93f2008-11-05 20:51:48 +0000861/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000862/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000863///
Richard Smith4c96e992013-02-19 23:47:15 +0000864/// In C++98, instead of base-type-specifier, we have:
865///
866/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie1cd50022011-10-25 17:10:12 +0000867Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
868 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-10-25 18:46:41 +0000869 // Ignore attempts to use typename
870 if (Tok.is(tok::kw_typename)) {
871 Diag(Tok, diag::err_expected_class_name_not_template)
872 << FixItHint::CreateRemoval(Tok.getLocation());
873 ConsumeToken();
874 }
875
David Blaikieafa155f2011-10-25 18:17:58 +0000876 // Parse optional nested-name-specifier
877 CXXScopeSpec SS;
878 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
879
880 BaseLoc = Tok.getLocation();
881
David Blaikie1cd50022011-10-25 17:10:12 +0000882 // Parse decltype-specifier
David Blaikie15a430a2011-12-04 05:04:18 +0000883 // tok == kw_decltype is just error recovery, it can only happen when SS
884 // isn't empty
885 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikieafa155f2011-10-25 18:17:58 +0000886 if (SS.isNotEmpty())
887 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
888 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie1cd50022011-10-25 17:10:12 +0000889 // Fake up a Declarator to use with ActOnTypeName.
890 DeclSpec DS(AttrFactory);
891
David Blaikie7491e732011-12-08 04:53:15 +0000892 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +0000893
894 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
895 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
896 }
897
Douglas Gregord54dfb82009-02-25 23:52:28 +0000898 // Check whether we have a template-id that names a type.
899 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000900 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +0000901 if (TemplateId->Kind == TNK_Type_template ||
902 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000903 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000904
905 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000906 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000907 EndLocation = Tok.getAnnotationEndLoc();
908 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000909
910 if (Type)
911 return Type;
912 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000913 }
914
915 // Fall through to produce an error below.
916 }
917
Douglas Gregor831c93f2008-11-05 20:51:48 +0000918 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000919 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000920 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000921 }
922
Douglas Gregor18473f32010-01-12 21:28:44 +0000923 IdentifierInfo *Id = Tok.getIdentifierInfo();
924 SourceLocation IdLoc = ConsumeToken();
925
926 if (Tok.is(tok::less)) {
927 // It looks the user intended to write a template-id here, but the
928 // template-name was wrong. Try to fix that.
929 TemplateNameKind TNK = TNK_Type_template;
930 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000931 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000932 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000933 Diag(IdLoc, diag::err_unknown_template_name)
934 << Id;
935 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000936
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000937 if (!Template) {
938 TemplateArgList TemplateArgs;
939 SourceLocation LAngleLoc, RAngleLoc;
940 ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
941 true, LAngleLoc, TemplateArgs, RAngleLoc);
Douglas Gregor18473f32010-01-12 21:28:44 +0000942 return true;
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000943 }
Douglas Gregor18473f32010-01-12 21:28:44 +0000944
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000945 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000946 UnqualifiedId TemplateName;
947 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000948
Douglas Gregor18473f32010-01-12 21:28:44 +0000949 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +0000950 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
951 TemplateName, true))
Douglas Gregor18473f32010-01-12 21:28:44 +0000952 return true;
953 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +0000954 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000955
Douglas Gregor18473f32010-01-12 21:28:44 +0000956 // If we didn't end up with a typename token, there's nothing more we
957 // can do.
958 if (Tok.isNot(tok::annot_typename))
959 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000960
Douglas Gregor18473f32010-01-12 21:28:44 +0000961 // Retrieve the type from the annotation token, consume that token, and
962 // return.
963 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +0000964 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +0000965 ConsumeToken();
966 return Type;
967 }
968
Douglas Gregor831c93f2008-11-05 20:51:48 +0000969 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +0000970 IdentifierInfo *CorrectedII = 0;
Douglas Gregore7c20652011-03-02 00:47:37 +0000971 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +0000972 false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +0000973 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +0000974 /*NonTrivialTypeSourceInfo=*/true,
975 &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000976 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +0000977 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000978 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000979 }
980
981 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +0000982 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000983
984 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000985 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +0000986 DS.SetRangeStart(IdLoc);
987 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +0000988 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000989
990 const char *PrevSpec = 0;
991 unsigned DiagID;
992 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
993
994 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
995 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000996}
997
John McCall8d32c052012-05-22 21:28:12 +0000998void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
999 while (Tok.is(tok::kw___single_inheritance) ||
1000 Tok.is(tok::kw___multiple_inheritance) ||
1001 Tok.is(tok::kw___virtual_inheritance)) {
1002 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1003 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +00001004 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
Aaron Ballman8edb5c22013-12-18 23:44:18 +00001005 AttributeList::AS_Keyword);
John McCall8d32c052012-05-22 21:28:12 +00001006 }
1007}
1008
Richard Smith369b9f92012-06-25 21:37:02 +00001009/// Determine whether the following tokens are valid after a type-specifier
1010/// which could be a standalone declaration. This will conservatively return
1011/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith200f47c2012-07-02 19:14:01 +00001012bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-06-25 21:37:02 +00001013 // This switch enumerates the valid "follow" set for type-specifiers.
1014 switch (Tok.getKind()) {
1015 default: break;
1016 case tok::semi: // struct foo {...} ;
1017 case tok::star: // struct foo {...} * P;
1018 case tok::amp: // struct foo {...} & R = ...
Richard Smith1ac67d12013-01-19 03:48:05 +00001019 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +00001020 case tok::identifier: // struct foo {...} V ;
1021 case tok::r_paren: //(struct foo {...} ) {4}
1022 case tok::annot_cxxscope: // struct foo {...} a:: b;
1023 case tok::annot_typename: // struct foo {...} a ::b;
1024 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1025 case tok::l_paren: // struct foo {...} ( x);
1026 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +00001027 case tok::kw_operator: // struct foo operator ++() {...}
Alp Tokerd3f79c52013-11-24 20:24:54 +00001028 case tok::kw___declspec: // struct foo {...} __declspec(...)
Richard Smith369b9f92012-06-25 21:37:02 +00001029 return true;
Richard Smith200f47c2012-07-02 19:14:01 +00001030 case tok::colon:
1031 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smith369b9f92012-06-25 21:37:02 +00001032 // Type qualifiers
1033 case tok::kw_const: // struct foo {...} const x;
1034 case tok::kw_volatile: // struct foo {...} volatile x;
1035 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001036 // Function specifiers
1037 // Note, no 'explicit'. An explicit function must be either a conversion
1038 // operator or a constructor. Either way, it can't have a return type.
1039 case tok::kw_inline: // struct foo inline f();
1040 case tok::kw_virtual: // struct foo virtual f();
1041 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +00001042 // Storage-class specifiers
1043 case tok::kw_static: // struct foo {...} static x;
1044 case tok::kw_extern: // struct foo {...} extern x;
1045 case tok::kw_typedef: // struct foo {...} typedef x;
1046 case tok::kw_register: // struct foo {...} register x;
1047 case tok::kw_auto: // struct foo {...} auto x;
1048 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001049 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +00001050 case tok::kw_constexpr: // struct foo {...} constexpr x;
1051 // As shown above, type qualifiers and storage class specifiers absolutely
1052 // can occur after class specifiers according to the grammar. However,
1053 // almost no one actually writes code like this. If we see one of these,
1054 // it is much more likely that someone missed a semi colon and the
1055 // type/storage class specifier we're seeing is part of the *next*
1056 // intended declaration, as in:
1057 //
1058 // struct foo { ... }
1059 // typedef int X;
1060 //
1061 // We'd really like to emit a missing semicolon error instead of emitting
1062 // an error on the 'int' saying that you can't have two type specifiers in
1063 // the same declaration of X. Because of this, we look ahead past this
1064 // token to see if it's a type specifier. If so, we know the code is
1065 // otherwise invalid, so we can produce the expected semi error.
1066 if (!isKnownToBeTypeSpecifier(NextToken()))
1067 return true;
1068 break;
1069 case tok::r_brace: // struct bar { struct foo {...} }
1070 // Missing ';' at end of struct is accepted as an extension in C mode.
1071 if (!getLangOpts().CPlusPlus)
1072 return true;
1073 break;
Richard Smith1ac67d12013-01-19 03:48:05 +00001074 // C++11 attributes
1075 case tok::l_square: // enum E [[]] x
1076 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1077 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith52c5b872013-01-29 04:13:32 +00001078 case tok::greater:
1079 // template<class T = class X>
1080 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001081 }
1082 return false;
1083}
1084
Douglas Gregor556877c2008-04-13 21:30:24 +00001085/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1086/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1087/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001088/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001089///
1090/// class-specifier: [C++ class]
1091/// class-head '{' member-specification[opt] '}'
1092/// class-head '{' member-specification[opt] '}' attributes[opt]
1093/// class-head:
1094/// class-key identifier[opt] base-clause[opt]
1095/// class-key nested-name-specifier identifier base-clause[opt]
1096/// class-key nested-name-specifier[opt] simple-template-id
1097/// base-clause[opt]
1098/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001099/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001100/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001101/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001102/// simple-template-id base-clause[opt]
1103/// class-key:
1104/// 'class'
1105/// 'struct'
1106/// 'union'
1107///
1108/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001109/// class-key ::[opt] nested-name-specifier[opt] identifier
1110/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1111/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001112///
1113/// Note that the C++ class-specifier and elaborated-type-specifier,
1114/// together, subsume the C99 struct-or-union-specifier:
1115///
1116/// struct-or-union-specifier: [C99 6.7.2.1]
1117/// struct-or-union identifier[opt] '{' struct-contents '}'
1118/// struct-or-union identifier
1119/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1120/// '}' attributes[opt]
1121/// [GNU] struct-or-union attributes[opt] identifier
1122/// struct-or-union:
1123/// 'struct'
1124/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001125void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1126 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001127 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001128 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001129 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001130 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001131 DeclSpec::TST TagType;
1132 if (TagTokKind == tok::kw_struct)
1133 TagType = DeclSpec::TST_struct;
1134 else if (TagTokKind == tok::kw___interface)
1135 TagType = DeclSpec::TST_interface;
1136 else if (TagTokKind == tok::kw_class)
1137 TagType = DeclSpec::TST_class;
1138 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001139 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1140 TagType = DeclSpec::TST_union;
1141 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001142
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001143 if (Tok.is(tok::code_completion)) {
1144 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001145 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001146 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001147 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001148
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001149 // C++03 [temp.explicit] 14.7.2/8:
1150 // The usual access checking rules do not apply to names used to specify
1151 // explicit instantiations.
1152 //
1153 // As an extension we do not perform access checking on the names used to
1154 // specify explicit specializations either. This is important to allow
1155 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001156 //
1157 // Note that we don't suppress if this turns out to be an elaborated
1158 // type specifier.
1159 bool shouldDelayDiagsInTag =
1160 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1161 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1162 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001163
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001164 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001165 // If attributes exist after tag, parse them.
Richard Smith37a45dd2013-10-24 01:21:09 +00001166 MaybeParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +00001167
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001168 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +00001169 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +00001170 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001171
John McCall8d32c052012-05-22 21:28:12 +00001172 // Parse inheritance specifiers.
1173 if (Tok.is(tok::kw___single_inheritance) ||
1174 Tok.is(tok::kw___multiple_inheritance) ||
1175 Tok.is(tok::kw___virtual_inheritance))
Richard Smith37a45dd2013-10-24 01:21:09 +00001176 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCall8d32c052012-05-22 21:28:12 +00001177
Alexis Hunt96d5c762009-11-21 08:43:09 +00001178 // If C++0x attributes exist here, parse them.
1179 // FIXME: Are we consistent with the ordering of parsing of different
1180 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001181 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001182
Michael Han309af292013-01-07 16:57:11 +00001183 // Source location used by FIXIT to insert misplaced
1184 // C++11 attributes
1185 SourceLocation AttrFixitLoc = Tok.getLocation();
1186
Alp Toker53358e42013-12-17 14:12:30 +00001187 // GNU libstdc++ and libc++ use certain intrinsic names as the
1188 // name of struct templates, but some are keywords in GCC >= 4.3
1189 // MSVC and Clang. For compatibility, convert the token to an identifier
1190 // and issue a warning diagnostic.
1191 if (TagType == DeclSpec::TST_struct && !Tok.is(tok::identifier) &&
1192 !Tok.isAnnotation()) {
1193 const IdentifierInfo *II = Tok.getIdentifierInfo();
1194 // We rarely end up here so the following check is efficient.
1195 if (II && II->getName().startswith("__is_"))
1196 TryKeywordIdentFallback(true);
1197 }
Mike Stump11289f42009-09-09 15:08:12 +00001198
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001199 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001200 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001201 if (getLangOpts().CPlusPlus) {
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001202 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1203 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001204
Douglas Gregordf593fb2011-11-07 17:33:42 +00001205 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall413021a2010-07-30 06:26:29 +00001206 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +00001207 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001208 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Alp Tokerec543272013-12-24 09:48:30 +00001209 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001210 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001211
Douglas Gregor916462b2009-10-30 21:46:58 +00001212 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1213
Douglas Gregor67a65642009-02-17 23:15:12 +00001214 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +00001215 IdentifierInfo *Name = 0;
1216 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +00001217 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +00001218 if (Tok.is(tok::identifier)) {
1219 Name = Tok.getIdentifierInfo();
1220 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001221
David Blaikiebbafb8a2012-03-11 07:00:24 +00001222 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001223 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001224 // Eat the template argument list and try to continue parsing this as
1225 // a class (or template thereof).
1226 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001227 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +00001228 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +00001229 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001230 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001231 // We couldn't parse the template argument list at all, so don't
1232 // try to give any location information for the list.
1233 LAngleLoc = RAngleLoc = SourceLocation();
1234 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001235
Douglas Gregor916462b2009-10-30 21:46:58 +00001236 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matose9a3ed42012-08-31 22:18:20 +00001237 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1238 << (TagType == DeclSpec::TST_class? 0
1239 : TagType == DeclSpec::TST_struct? 1
Richard Smith68b14532013-11-08 21:51:24 +00001240 : TagType == DeclSpec::TST_union? 2
Joao Matose9a3ed42012-08-31 22:18:20 +00001241 : 3)
1242 << Name
1243 << SourceRange(LAngleLoc, RAngleLoc);
1244
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001245 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001246 // we've removed its template argument list.
1247 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1248 if (TemplateParams && TemplateParams->size() > 1) {
1249 TemplateParams->pop_back();
1250 } else {
1251 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001252 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001253 = ParsedTemplateInfo::NonTemplate;
1254 }
1255 } else if (TemplateInfo.Kind
1256 == ParsedTemplateInfo::ExplicitInstantiation) {
1257 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +00001258 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001259 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001260 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001261 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001262 = SourceLocation();
1263 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1264 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001265 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001266 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001267 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001268 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001269 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001270
Douglas Gregore7c20652011-03-02 00:47:37 +00001271 if (TemplateId->Kind != TNK_Type_template &&
1272 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001273 // The template-name in the simple-template-id refers to
1274 // something other than a class template. Give an appropriate
1275 // error message and skip to the ';'.
1276 SourceRange Range(NameLoc);
1277 if (SS.isNotEmpty())
1278 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001279
Richard Smith72bfbd82013-12-04 00:28:23 +00001280 // FIXME: Name may be null here.
Douglas Gregor7f741122009-02-25 19:37:18 +00001281 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu30f93852013-06-19 22:25:01 +00001282 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001283
Douglas Gregor7f741122009-02-25 19:37:18 +00001284 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001285 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor7f741122009-02-25 19:37:18 +00001286 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001287 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001288 }
1289
Richard Smithbfdb1082012-03-12 08:56:40 +00001290 // There are four options here.
1291 // - If we are in a trailing return type, this is always just a reference,
1292 // and we must not try to parse a definition. For instance,
1293 // [] () -> struct S { };
1294 // does not define a type.
1295 // - If we have 'struct foo {...', 'struct foo :...',
1296 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1297 // - If we have 'struct foo;', then this is either a forward declaration
1298 // or a friend declaration, which have to be treated differently.
1299 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001300 //
1301 // We also detect these erroneous cases to provide better diagnostic for
1302 // C++11 attributes parsing.
1303 // - attributes follow class name:
1304 // struct foo [[]] {};
1305 // - attributes appear before or after 'final':
1306 // struct foo [[]] final [[]] {};
1307 //
Richard Smithc5b05522012-03-12 07:56:15 +00001308 // However, in type-specifier-seq's, things look like declarations but are
1309 // just references, e.g.
1310 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001311 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001312 // &T::operator struct s;
1313 // For these, DSC is DSC_type_specifier.
Michael Han9407e502012-11-26 22:54:45 +00001314
1315 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001316 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001317
John McCallfaf5fb42010-08-26 23:41:50 +00001318 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001319 if (DSC == DSC_trailing)
1320 TUK = Sema::TUK_Reference;
1321 else if (Tok.is(tok::l_brace) ||
1322 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001323 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001324 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001325 if (DS.isFriendSpecified()) {
1326 // C++ [class.friend]p2:
1327 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001328 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001329 << SourceRange(DS.getFriendSpecLoc());
1330
1331 // Skip everything up to the semicolon, so that this looks like a proper
1332 // friend class (or template thereof) declaration.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001333 SkipUntil(tok::semi, StopBeforeMatch);
John McCallfaf5fb42010-08-26 23:41:50 +00001334 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001335 } else {
1336 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001337 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001338 }
Richard Smith434516c2013-02-22 06:46:23 +00001339 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1340 NextToken().is(tok::kw_alignas))) {
Michael Han9407e502012-11-26 22:54:45 +00001341 // We can't tell if this is a definition or reference
1342 // until we skipped the 'final' and C++11 attribute specifiers.
1343 TentativeParsingAction PA(*this);
1344
1345 // Skip the 'final' keyword.
1346 ConsumeToken();
1347
1348 // Skip C++11 attribute specifiers.
1349 while (true) {
1350 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1351 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001352 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001353 break;
Richard Smith434516c2013-02-22 06:46:23 +00001354 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han9407e502012-11-26 22:54:45 +00001355 ConsumeToken();
1356 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001357 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001358 break;
1359 } else {
1360 break;
1361 }
1362 }
1363
1364 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1365 TUK = Sema::TUK_Definition;
1366 else
1367 TUK = Sema::TUK_Reference;
1368
1369 PA.Revert();
Richard Smith369b9f92012-06-25 21:37:02 +00001370 } else if (DSC != DSC_type_specifier &&
1371 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001372 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001373 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001374 if (Tok.isNot(tok::semi)) {
1375 // A semicolon was missing after this declaration. Diagnose and recover.
1376 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1377 DeclSpec::getSpecifierName(TagType));
1378 PP.EnterToken(Tok);
1379 Tok.setKind(tok::semi);
1380 }
Richard Smith369b9f92012-06-25 21:37:02 +00001381 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001382 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001383
Michael Han9407e502012-11-26 22:54:45 +00001384 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1385 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001386 if (TUK != Sema::TUK_Reference) {
1387 // If this is not a reference, then the only possible
1388 // valid place for C++11 attributes to appear here
1389 // is between class-key and class-name. If there are
1390 // any attributes after class-name, we try a fixit to move
1391 // them to the right place.
1392 SourceRange AttrRange = Attributes.Range;
1393 if (AttrRange.isValid()) {
1394 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1395 << AttrRange
1396 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1397 CharSourceRange(AttrRange, true))
1398 << FixItHint::CreateRemoval(AttrRange);
1399
1400 // Recover by adding misplaced attributes to the attribute list
1401 // of the class so they can be applied on the class later.
1402 attrs.takeAllFrom(Attributes);
1403 }
1404 }
Michael Han9407e502012-11-26 22:54:45 +00001405
John McCall6347b682012-05-07 06:16:58 +00001406 // If this is an elaborated type specifier, and we delayed
1407 // diagnostics before, just merge them into the current pool.
1408 if (shouldDelayDiagsInTag) {
1409 diagsFromTag.done();
1410 if (TUK == Sema::TUK_Reference)
1411 diagsFromTag.redelay();
1412 }
1413
John McCall413021a2010-07-30 06:26:29 +00001414 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001415 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001416 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1417 // We have a declaration or reference to an anonymous class.
1418 Diag(StartLoc, diag::err_anon_type_definition)
1419 << DeclSpec::getSpecifierName(TagType);
1420 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001421
David Majnemer3252fd02013-12-05 01:36:53 +00001422 // If we are parsing a definition and stop at a base-clause, continue on
1423 // until the semicolon. Continuing from the comma will just trick us into
1424 // thinking we are seeing a variable declaration.
1425 if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
1426 SkipUntil(tok::semi, StopBeforeMatch);
1427 else
1428 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor556877c2008-04-13 21:30:24 +00001429 return;
1430 }
1431
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001432 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001433 DeclResult TagOrTempResult = true; // invalid
1434 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001435
Douglas Gregord6ab8742009-05-28 23:31:59 +00001436 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +00001437 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001438 // Explicit specialization, class template partial specialization,
1439 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001440 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001441 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001442 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001443 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001444 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001445 ProhibitAttributes(attrs);
1446
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001447 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001448 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001449 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001450 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001451 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001452 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001453 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001454 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001455 TemplateId->TemplateNameLoc,
1456 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001457 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001458 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001459 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001460
1461 // Friend template-ids are treated as references unless
1462 // they have template headers, in which case they're ill-formed
1463 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1464 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001465 } else if (TUK == Sema::TUK_Reference ||
1466 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001467 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001468 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001469 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001470 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001471 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001472 TemplateId->Template,
1473 TemplateId->TemplateNameLoc,
1474 TemplateId->LAngleLoc,
1475 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001476 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001477 } else {
1478 // This is an explicit specialization or a class template
1479 // partial specialization.
1480 TemplateParameterLists FakedParamLists;
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001481 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1482 // This looks like an explicit instantiation, because we have
1483 // something like
1484 //
1485 // template class Foo<X>
1486 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001487 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001488 // meant to be an explicit specialization, but the user forgot
1489 // the '<>' after 'template'.
Richard Smith003c5e12013-11-08 19:03:29 +00001490 // It this is friend declaration however, since it cannot have a
1491 // template header, it is most likely that the user meant to
1492 // remove the 'template' keyword.
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001493 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith003c5e12013-11-08 19:03:29 +00001494 "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001495
Richard Smith003c5e12013-11-08 19:03:29 +00001496 if (TUK == Sema::TUK_Friend) {
1497 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
1498 TemplateParams = 0;
1499 } else {
1500 SourceLocation LAngleLoc =
1501 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1502 Diag(TemplateId->TemplateNameLoc,
1503 diag::err_explicit_instantiation_with_definition)
1504 << SourceRange(TemplateInfo.TemplateLoc)
1505 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1506
1507 // Create a fake template parameter list that contains only
1508 // "template<>", so that we treat this construct as a class
1509 // template specialization.
1510 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1511 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1512 LAngleLoc));
1513 TemplateParams = &FakedParamLists;
1514 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001515 }
1516
1517 // Build the class template specialization.
1518 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001519 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor3c7cd6a2011-09-09 20:53:38 +00001520 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall3e56fd42010-08-23 07:28:44 +00001521 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001522 TemplateId->TemplateNameLoc,
1523 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +00001524 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001525 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001526 attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001527 MultiTemplateParamsArg(
Douglas Gregor67a65642009-02-17 23:15:12 +00001528 TemplateParams? &(*TemplateParams)[0] : 0,
1529 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001530 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001531 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001532 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001533 // Explicit instantiation of a member of a class template
1534 // specialization, e.g.,
1535 //
1536 // template struct Outer<int>::Inner;
1537 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001538 ProhibitAttributes(attrs);
1539
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001540 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001541 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001542 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001543 TemplateInfo.TemplateLoc,
1544 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001545 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001546 } else if (TUK == Sema::TUK_Friend &&
1547 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001548 ProhibitAttributes(attrs);
1549
John McCallace48cd2010-10-19 01:40:49 +00001550 TagOrTempResult =
1551 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1552 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001553 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001554 MultiTemplateParamsArg(
John McCallace48cd2010-10-19 01:40:49 +00001555 TemplateParams? &(*TemplateParams)[0] : 0,
1556 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001557 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001558 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1559 ProhibitAttributes(attrs);
Richard Smith003c5e12013-11-08 19:03:29 +00001560
Larisse Voufo725de3e2013-06-21 00:08:46 +00001561 if (TUK == Sema::TUK_Definition &&
1562 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1563 // If the declarator-id is not a template-id, issue a diagnostic and
1564 // recover by ignoring the 'template' keyword.
1565 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1566 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001567 TemplateParams = 0;
Larisse Voufo725de3e2013-06-21 00:08:46 +00001568 }
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001569
John McCall7f41d982009-09-11 04:59:25 +00001570 bool IsDependent = false;
1571
John McCall32723e92010-10-19 18:40:57 +00001572 // Don't pass down template parameter lists if this is just a tag
1573 // reference. For example, we don't need the template parameters here:
1574 // template <class T> class A *makeA(T t);
1575 MultiTemplateParamsArg TParams;
1576 if (TUK != Sema::TUK_Reference && TemplateParams)
1577 TParams =
1578 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1579
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001580 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001581 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001582 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001583 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001584 TParams, Owned, IsDependent,
1585 SourceLocation(), false,
1586 clang::TypeResult());
John McCall7f41d982009-09-11 04:59:25 +00001587
1588 // If ActOnTag said the type was dependent, try again with the
1589 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001590 if (IsDependent) {
1591 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001592 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001593 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001594 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001595 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001596
Douglas Gregor556877c2008-04-13 21:30:24 +00001597 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001598 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001599 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001600 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001601 isCXX11FinalKeyword());
David Blaikiebbafb8a2012-03-11 07:00:24 +00001602 if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001603 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1604 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001605 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001606 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001607 }
1608
John McCallba7bf592010-08-24 05:47:05 +00001609 const char *PrevSpec = 0;
1610 unsigned DiagID;
1611 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001612 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001613 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1614 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallba7bf592010-08-24 05:47:05 +00001615 PrevSpec, DiagID, TypeResult.get());
John McCall7f41d982009-09-11 04:59:25 +00001616 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001617 Result = DS.SetTypeSpecType(TagType, StartLoc,
1618 NameLoc.isValid() ? NameLoc : StartLoc,
1619 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCall7f41d982009-09-11 04:59:25 +00001620 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001621 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001622 return;
1623 }
Mike Stump11289f42009-09-09 15:08:12 +00001624
John McCallba7bf592010-08-24 05:47:05 +00001625 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001626 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001627
Chris Lattnercf251412010-02-02 01:23:29 +00001628 // At this point, we've successfully parsed a class-specifier in 'definition'
1629 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1630 // going to look at what comes after it to improve error recovery. If an
1631 // impossible token occurs next, we assume that the programmer forgot a ; at
1632 // the end of the declaration and recover that way.
1633 //
Richard Smith369b9f92012-06-25 21:37:02 +00001634 // Also enforce C++ [temp]p3:
1635 // In a template-declaration which defines a class, no declarator
1636 // is permitted.
Joao Matose9a3ed42012-08-31 22:18:20 +00001637 if (TUK == Sema::TUK_Definition &&
1638 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001639 if (Tok.isNot(tok::semi)) {
1640 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1641 DeclSpec::getSpecifierName(TagType));
1642 // Push this token back into the preprocessor and change our current token
1643 // to ';' so that the rest of the code recovers as though there were an
1644 // ';' after the definition.
1645 PP.EnterToken(Tok);
1646 Tok.setKind(tok::semi);
1647 }
Chris Lattnercf251412010-02-02 01:23:29 +00001648 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001649}
1650
Mike Stump11289f42009-09-09 15:08:12 +00001651/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001652///
1653/// base-clause : [C++ class.derived]
1654/// ':' base-specifier-list
1655/// base-specifier-list:
1656/// base-specifier '...'[opt]
1657/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001658void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001659 assert(Tok.is(tok::colon) && "Not a base clause");
1660 ConsumeToken();
1661
Douglas Gregor29a92472008-10-22 17:49:05 +00001662 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001663 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001664
Douglas Gregor556877c2008-04-13 21:30:24 +00001665 while (true) {
1666 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001667 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001668 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001669 // Skip the rest of this base specifier, up until the comma or
1670 // opening brace.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001671 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor29a92472008-10-22 17:49:05 +00001672 } else {
1673 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001674 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001675 }
1676
1677 // If the next token is a comma, consume it and keep reading
1678 // base-specifiers.
1679 if (Tok.isNot(tok::comma)) break;
Mike Stump11289f42009-09-09 15:08:12 +00001680
Douglas Gregor556877c2008-04-13 21:30:24 +00001681 // Consume the comma.
1682 ConsumeToken();
1683 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001684
1685 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001686 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001687}
1688
1689/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1690/// one entry in the base class list of a class specifier, for example:
1691/// class foo : public bar, virtual private baz {
1692/// 'public bar' and 'virtual private baz' are each base-specifiers.
1693///
1694/// base-specifier: [C++ class.derived]
Richard Smith4c96e992013-02-19 23:47:15 +00001695/// attribute-specifier-seq[opt] base-type-specifier
1696/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1697/// base-type-specifier
1698/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1699/// base-type-specifier
John McCall48871652010-08-21 09:40:31 +00001700Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001701 bool IsVirtual = false;
1702 SourceLocation StartLoc = Tok.getLocation();
1703
Richard Smith4c96e992013-02-19 23:47:15 +00001704 ParsedAttributesWithRange Attributes(AttrFactory);
1705 MaybeParseCXX11Attributes(Attributes);
1706
Douglas Gregor556877c2008-04-13 21:30:24 +00001707 // Parse the 'virtual' keyword.
1708 if (Tok.is(tok::kw_virtual)) {
1709 ConsumeToken();
1710 IsVirtual = true;
1711 }
1712
Richard Smith4c96e992013-02-19 23:47:15 +00001713 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1714
Douglas Gregor556877c2008-04-13 21:30:24 +00001715 // Parse an (optional) access specifier.
1716 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001717 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001718 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001719
Richard Smith4c96e992013-02-19 23:47:15 +00001720 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1721
Douglas Gregor556877c2008-04-13 21:30:24 +00001722 // Parse the 'virtual' keyword (again!), in case it came after the
1723 // access specifier.
1724 if (Tok.is(tok::kw_virtual)) {
1725 SourceLocation VirtualLoc = ConsumeToken();
1726 if (IsVirtual) {
1727 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001728 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001729 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001730 }
1731
1732 IsVirtual = true;
1733 }
1734
Richard Smith4c96e992013-02-19 23:47:15 +00001735 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1736
Douglas Gregor831c93f2008-11-05 20:51:48 +00001737 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001738 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00001739 SourceLocation BaseLoc;
1740 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001741 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001742 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001743
Douglas Gregor752a5952011-01-03 22:36:02 +00001744 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1745 // actually part of the base-specifier-list grammar productions, but we
1746 // parse it here for convenience.
1747 SourceLocation EllipsisLoc;
1748 if (Tok.is(tok::ellipsis))
1749 EllipsisLoc = ConsumeToken();
1750
Mike Stump11289f42009-09-09 15:08:12 +00001751 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001752 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001753
Douglas Gregor556877c2008-04-13 21:30:24 +00001754 // Notify semantic analysis that we have parsed a complete
1755 // base-specifier.
Richard Smith4c96e992013-02-19 23:47:15 +00001756 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1757 Access, BaseType.get(), BaseLoc,
1758 EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001759}
1760
1761/// getAccessSpecifierIfPresent - Determine whether the next token is
1762/// a C++ access-specifier.
1763///
1764/// access-specifier: [C++ class.derived]
1765/// 'private'
1766/// 'protected'
1767/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001768AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001769 switch (Tok.getKind()) {
1770 default: return AS_none;
1771 case tok::kw_private: return AS_private;
1772 case tok::kw_protected: return AS_protected;
1773 case tok::kw_public: return AS_public;
1774 }
1775}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001776
Douglas Gregor433e0532012-04-16 18:27:27 +00001777/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith2331bbf2012-05-02 22:22:32 +00001778/// delayed, e.g., default arguments, create a late-parsed method declaration
1779/// record to handle the parsing at the end of the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00001780void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1781 Decl *ThisDecl) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001782 // We just declared a member function. If this member function
Richard Smith2331bbf2012-05-02 22:22:32 +00001783 // has any default arguments, we'll need to parse them later.
Eli Friedman3af2a772009-07-22 21:45:50 +00001784 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001785 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001786 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor433e0532012-04-16 18:27:27 +00001787
Eli Friedman3af2a772009-07-22 21:45:50 +00001788 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1789 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1790 if (!LateMethod) {
1791 // Push this method onto the stack of late-parsed method
1792 // declarations.
Douglas Gregorefc46952010-10-12 16:25:54 +00001793 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1794 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001795 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001796
1797 // Add all of the parameters prior to this one (they don't
1798 // have default arguments).
1799 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1800 for (unsigned I = 0; I < ParamIdx; ++I)
1801 LateMethod->DefaultArgs.push_back(
Douglas Gregor1d85d292010-03-02 01:29:43 +00001802 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001803 }
1804
Douglas Gregor433e0532012-04-16 18:27:27 +00001805 // Add this parameter to the list of parameters (it may or may
Eli Friedman3af2a772009-07-22 21:45:50 +00001806 // not have a default argument).
1807 LateMethod->DefaultArgs.push_back(
1808 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1809 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1810 }
1811 }
1812}
1813
Richard Smith89645bc2013-01-02 12:01:23 +00001814/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001815/// virt-specifier.
1816///
1817/// virt-specifier:
1818/// override
1819/// final
Richard Smith89645bc2013-01-02 12:01:23 +00001820VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001821 if (!getLangOpts().CPlusPlus)
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001822 return VirtSpecifiers::VS_None;
1823
Anders Carlsson56104902011-01-17 03:05:47 +00001824 if (Tok.is(tok::identifier)) {
1825 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001826
Anders Carlsson428803b2011-01-20 03:47:08 +00001827 // Initialize the contextual keywords.
1828 if (!Ident_final) {
1829 Ident_final = &PP.getIdentifierTable().get("final");
David Majnemera5433082013-10-18 00:33:31 +00001830 if (getLangOpts().MicrosoftExt)
1831 Ident_sealed = &PP.getIdentifierTable().get("sealed");
Anders Carlsson428803b2011-01-20 03:47:08 +00001832 Ident_override = &PP.getIdentifierTable().get("override");
1833 }
1834
Anders Carlsson56104902011-01-17 03:05:47 +00001835 if (II == Ident_override)
1836 return VirtSpecifiers::VS_Override;
1837
David Majnemera5433082013-10-18 00:33:31 +00001838 if (II == Ident_sealed)
1839 return VirtSpecifiers::VS_Sealed;
1840
Anders Carlsson56104902011-01-17 03:05:47 +00001841 if (II == Ident_final)
1842 return VirtSpecifiers::VS_Final;
1843 }
1844
1845 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001846}
1847
Richard Smith89645bc2013-01-02 12:01:23 +00001848/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001849///
1850/// virt-specifier-seq:
1851/// virt-specifier
1852/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00001853void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalldb632ac2012-09-25 07:32:39 +00001854 bool IsInterface) {
Anders Carlsson56104902011-01-17 03:05:47 +00001855 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00001856 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001857 if (Specifier == VirtSpecifiers::VS_None)
1858 return;
1859
1860 // C++ [class.mem]p8:
1861 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001862 const char *PrevSpec = 0;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001863 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001864 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1865 << PrevSpec
1866 << FixItHint::CreateRemoval(Tok.getLocation());
1867
David Majnemera5433082013-10-18 00:33:31 +00001868 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
1869 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalldb632ac2012-09-25 07:32:39 +00001870 Diag(Tok.getLocation(), diag::err_override_control_interface)
1871 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemera5433082013-10-18 00:33:31 +00001872 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
1873 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
John McCalldb632ac2012-09-25 07:32:39 +00001874 } else {
David Majnemera5433082013-10-18 00:33:31 +00001875 Diag(Tok.getLocation(),
1876 getLangOpts().CPlusPlus11
1877 ? diag::warn_cxx98_compat_override_control_keyword
1878 : diag::ext_override_control_keyword)
1879 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalldb632ac2012-09-25 07:32:39 +00001880 }
Anders Carlsson56104902011-01-17 03:05:47 +00001881 ConsumeToken();
1882 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001883}
1884
Richard Smith89645bc2013-01-02 12:01:23 +00001885/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlssoncafbab72011-03-25 14:53:29 +00001886/// contextual 'final' keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00001887bool Parser::isCXX11FinalKeyword() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001888 if (!getLangOpts().CPlusPlus)
Anders Carlssoncafbab72011-03-25 14:53:29 +00001889 return false;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001890
Anders Carlssoncafbab72011-03-25 14:53:29 +00001891 if (!Tok.is(tok::identifier))
1892 return false;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001893
Anders Carlssoncafbab72011-03-25 14:53:29 +00001894 // Initialize the contextual keywords.
1895 if (!Ident_final) {
1896 Ident_final = &PP.getIdentifierTable().get("final");
David Majnemera5433082013-10-18 00:33:31 +00001897 if (getLangOpts().MicrosoftExt)
1898 Ident_sealed = &PP.getIdentifierTable().get("sealed");
Anders Carlssoncafbab72011-03-25 14:53:29 +00001899 Ident_override = &PP.getIdentifierTable().get("override");
1900 }
David Majnemera5433082013-10-18 00:33:31 +00001901
1902 return Tok.getIdentifierInfo() == Ident_final ||
1903 Tok.getIdentifierInfo() == Ident_sealed;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001904}
1905
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001906/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1907///
1908/// member-declaration:
1909/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1910/// function-definition ';'[opt]
1911/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1912/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001913/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001914/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001915/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001916///
1917/// member-declarator-list:
1918/// member-declarator
1919/// member-declarator-list ',' member-declarator
1920///
1921/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001922/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001923/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00001924/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001925/// identifier[opt] ':' constant-expression
1926///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001927/// virt-specifier-seq:
1928/// virt-specifier
1929/// virt-specifier-seq virt-specifier
1930///
1931/// virt-specifier:
1932/// override
1933/// final
David Majnemera5433082013-10-18 00:33:31 +00001934/// [MS] sealed
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001935///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001936/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001937/// '= 0'
1938///
1939/// constant-initializer:
1940/// '=' constant-expression
1941///
Douglas Gregor3447e762009-08-20 22:52:58 +00001942void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00001943 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00001944 const ParsedTemplateInfo &TemplateInfo,
1945 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00001946 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001947 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00001948 Diag(Tok, diag::err_at_defs_cxx);
1949 else
1950 Diag(Tok, diag::err_at_in_class);
Richard Smithda35e962013-11-09 04:52:51 +00001951
Douglas Gregor23c84762011-04-14 17:21:19 +00001952 ConsumeToken();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001953 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregor23c84762011-04-14 17:21:19 +00001954 return;
1955 }
Richard Smithda35e962013-11-09 04:52:51 +00001956
John McCalla0097262009-12-11 02:10:03 +00001957 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00001958 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00001959 if (!TemplateInfo.Kind &&
Richard Smith45855df2012-05-09 08:23:23 +00001960 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1961 if (TryAnnotateCXXScopeToken())
1962 MalformedTypeSpec = true;
1963
1964 bool isAccessDecl;
1965 if (Tok.isNot(tok::annot_cxxscope))
1966 isAccessDecl = false;
1967 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00001968 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1969 else
1970 isAccessDecl = NextToken().is(tok::kw_operator);
1971
1972 if (isAccessDecl) {
1973 // Collect the scope specifier token we annotated earlier.
1974 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00001975 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1976 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00001977
1978 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001979 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00001980 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001981 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1982 TemplateKWLoc, Name)) {
John McCalla0097262009-12-11 02:10:03 +00001983 SkipUntil(tok::semi);
1984 return;
1985 }
1986
1987 // TODO: recover from mistakenly-qualified operator declarations.
1988 if (ExpectAndConsume(tok::semi,
1989 diag::err_expected_semi_after,
1990 "access declaration",
1991 tok::semi))
1992 return;
1993
Douglas Gregor0be31a22010-07-02 17:43:08 +00001994 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001995 /* HasUsingKeyword */ false,
1996 SourceLocation(),
John McCalla0097262009-12-11 02:10:03 +00001997 SS, Name,
1998 /* AttrList */ 0,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001999 /* HasTypenameKeyword */ false,
John McCalla0097262009-12-11 02:10:03 +00002000 SourceLocation());
2001 return;
2002 }
2003 }
2004
Anders Carlssonf24fcff62009-03-11 16:27:10 +00002005 // static_assert-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00002006 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00002007 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00002008 SourceLocation DeclEnd;
2009 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002010 return;
2011 }
Mike Stump11289f42009-09-09 15:08:12 +00002012
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002013 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00002014 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00002015 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00002016 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00002017 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002018 AS, AccessAttrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002019 return;
2020 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002021
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002022 // Handle: member-declaration ::= '__extension__' member-declaration
2023 if (Tok.is(tok::kw___extension__)) {
2024 // __extension__ silences extension warnings in the subexpression.
2025 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2026 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002027 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2028 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002029 }
Douglas Gregorfec52632009-06-20 00:51:54 +00002030
Chris Lattnercf251412010-02-02 01:23:29 +00002031 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
2032 // is a bitfield.
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002033 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002034
John McCall084e83d2011-03-24 11:26:52 +00002035 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00002036 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002037 // Optional C++11 attribute-specifier
2038 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00002039 // We need to keep these attributes for future diagnostic
2040 // before they are taken over by declaration specifier.
2041 FnAttrs.addAll(attrs.getList());
2042 FnAttrs.Range = attrs.Range;
2043
John McCall53fa7142010-12-24 02:08:15 +00002044 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002045
Douglas Gregorfec52632009-06-20 00:51:54 +00002046 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00002047 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002048
Douglas Gregorfec52632009-06-20 00:51:54 +00002049 // Eat 'using'.
2050 SourceLocation UsingLoc = ConsumeToken();
2051
2052 if (Tok.is(tok::kw_namespace)) {
2053 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002054 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002055 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00002056 SourceLocation DeclEnd;
Richard Smith3f1b5d02011-05-05 21:57:07 +00002057 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +00002058 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2059 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00002060 }
2061 return;
2062 }
2063
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002064 // Hold late-parsed attributes so we can attach a Decl to them later.
2065 LateParsedAttrList CommonLateParsedAttrs;
2066
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002067 // decl-specifier-seq:
2068 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00002069 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00002070 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00002071 if (MalformedTypeSpec)
2072 DS.SetTypeSpecError();
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002073 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2074 &CommonLateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002075
Richard Smith404dfb42013-11-19 22:47:36 +00002076 // If we had a free-standing type definition with a missing semicolon, we
2077 // may get this far before the problem becomes obvious.
2078 if (DS.hasTagDefinition() &&
2079 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2080 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2081 &CommonLateParsedAttrs))
2082 return;
2083
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002084 MultiTemplateParamsArg TemplateParams(
John McCall11083da2009-09-16 22:47:08 +00002085 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2086 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2087
Alp Toker35d87032013-12-30 23:29:50 +00002088 if (TryConsumeToken(tok::semi)) {
Michael Handdc016d2012-11-28 23:17:40 +00002089 if (DS.isFriendSpecified())
2090 ProhibitAttributes(FnAttrs);
2091
John McCall48871652010-08-21 09:40:31 +00002092 Decl *TheDecl =
Chandler Carruth7c9856d2011-05-03 18:35:10 +00002093 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCall796c2a52010-07-16 08:13:16 +00002094 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00002095 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002096 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002097
John McCall28a6aea2009-11-04 02:18:39 +00002098 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002099 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002100
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002101 // Hold late-parsed attributes so we can attach a Decl to them later.
2102 LateParsedAttrList LateParsedAttrs;
2103
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002104 SourceLocation EqualLoc;
2105 bool HasInitializer = false;
2106 ExprResult Init;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002107 if (Tok.isNot(tok::colon)) {
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002108 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2109 ColonProtectionRAIIObject X(*this);
2110
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002111 // Parse the first declarator.
2112 ParseDeclarator(DeclaratorInfo);
Richard Smith2331bbf2012-05-02 22:22:32 +00002113 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +00002114 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002115 // If so, skip until the semi-colon or a }.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002116 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002117 if (Tok.is(tok::semi))
2118 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002119 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002120 }
2121
Richard Smith89645bc2013-01-02 12:01:23 +00002122 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber24b2a822011-01-28 06:07:34 +00002123
John Thompson5bc5cbe2009-11-25 22:58:06 +00002124 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002125 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson5bc5cbe2009-11-25 22:58:06 +00002126
Francois Pichet3abc9b82011-05-11 02:14:46 +00002127 // MSVC permits pure specifier on inline functions declared at class scope.
2128 // Hence check for =0 before checking for function definition.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002129 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet3abc9b82011-05-11 02:14:46 +00002130 DeclaratorInfo.isFunctionDeclarator() &&
2131 NextToken().is(tok::numeric_constant)) {
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002132 EqualLoc = ConsumeToken();
Francois Pichet3abc9b82011-05-11 02:14:46 +00002133 Init = ParseInitializer();
2134 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002135 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002136 else
2137 HasInitializer = true;
Francois Pichet3abc9b82011-05-11 02:14:46 +00002138 }
2139
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002140 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002141 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002142 //
2143 // In C++11, a non-function declarator followed by an open brace is a
2144 // braced-init-list for an in-class member initialization, not an
2145 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002146 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002147 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002148 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002149 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002150 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002151 } else if (Tok.is(tok::equal)) {
2152 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002153 if (KW.is(tok::kw_default))
2154 DefinitionKind = FDK_Defaulted;
2155 else if (KW.is(tok::kw_delete))
2156 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002157 }
2158 }
2159
Michael Handdc016d2012-11-28 23:17:40 +00002160 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2161 // to a friend declaration, that declaration shall be a definition.
2162 if (DeclaratorInfo.isFunctionDeclarator() &&
2163 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2164 // Diagnose attributes that appear before decl specifier:
2165 // [[]] friend int foo();
2166 ProhibitAttributes(FnAttrs);
2167 }
2168
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002169 if (DefinitionKind) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002170 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002171 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002172 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002173 SkipUntil(tok::r_brace);
Michael Handdc016d2012-11-28 23:17:40 +00002174
Douglas Gregor8a4db832011-01-19 16:41:58 +00002175 // Consume the optional ';'
Alp Toker35d87032013-12-30 23:29:50 +00002176 TryConsumeToken(tok::semi);
2177
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002178 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002179 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002180
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002181 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002182 Diag(DeclaratorInfo.getIdentifierLoc(),
2183 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002184
Richard Smith2603b092012-11-15 22:54:20 +00002185 // Recover by treating the 'typedef' as spurious.
2186 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002187 }
2188
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002189 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002190 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002191 VS, DefinitionKind, Init);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002192
David Majnemer23252a32013-08-01 04:22:55 +00002193 if (FunDecl) {
2194 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2195 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2196 }
2197 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2198 LateParsedAttrs[i]->addDecl(FunDecl);
2199 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002200 }
2201 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002202
2203 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002204 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002205 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002206
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002207 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002208 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002209 }
2210
2211 // member-declarator-list:
2212 // member-declarator
2213 // member-declarator-list ',' member-declarator
2214
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002215 SmallVector<Decl *, 8> DeclsInGroup;
John McCalldadc5752010-08-24 06:29:42 +00002216 ExprResult BitfieldSize;
Richard Smithc8a79032012-01-09 22:31:44 +00002217 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002218
2219 while (1) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002220 // member-declarator:
2221 // declarator pure-specifier[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00002222 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002223 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002224 if (Tok.is(tok::colon)) {
2225 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002226 BitfieldSize = ParseConstantExpression();
2227 if (BitfieldSize.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002228 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002229 }
Mike Stump11289f42009-09-09 15:08:12 +00002230
Chris Lattnerf3d3b362010-06-13 05:34:18 +00002231 // If a simple-asm-expr is present, parse it.
2232 if (Tok.is(tok::kw_asm)) {
2233 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00002234 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnerf3d3b362010-06-13 05:34:18 +00002235 if (AsmLabel.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002236 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Chris Lattnerf3d3b362010-06-13 05:34:18 +00002237
2238 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2239 DeclaratorInfo.SetRangeEnd(Loc);
2240 }
2241
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002242 // If attributes exist after the declarator, parse them.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002243 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002244
Richard Smith938f40b2011-06-11 17:19:42 +00002245 // FIXME: When g++ adds support for this, we'll need to check whether it
2246 // goes before or after the GNU attributes and __asm__.
Richard Smith89645bc2013-01-02 12:01:23 +00002247 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith938f40b2011-06-11 17:19:42 +00002248
Richard Smith2b013182012-06-10 03:12:00 +00002249 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002250 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith938f40b2011-06-11 17:19:42 +00002251 if (BitfieldSize.get()) {
2252 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002253 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith938f40b2011-06-11 17:19:42 +00002254 } else {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002255 HasInitializer = true;
Richard Smith2b013182012-06-10 03:12:00 +00002256 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2257 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smith2b013182012-06-10 03:12:00 +00002258 != DeclSpec::SCS_typedef)
2259 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002260 }
2261 }
2262
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002263 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002264 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002265 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002266
Rafael Espindola0a67e2f2013-01-08 20:44:06 +00002267 NamedDecl *ThisDecl = 0;
John McCall07e91c02009-08-06 02:15:43 +00002268 if (DS.isFriendSpecified()) {
Michael Handdc016d2012-11-28 23:17:40 +00002269 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2270 // to a friend declaration, that declaration shall be a definition.
2271 //
2272 // Diagnose attributes appear after friend member function declarator:
2273 // foo [[]] ();
2274 SmallVector<SourceRange, 4> Ranges;
2275 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2276 if (!Ranges.empty()) {
Craig Topper2341c0d2013-07-04 03:08:24 +00002277 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
Michael Handdc016d2012-11-28 23:17:40 +00002278 E = Ranges.end(); I != E; ++I) {
2279 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2280 << *I;
2281 }
2282 }
2283
John McCall2f212b32009-09-11 21:02:39 +00002284 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor0be31a22010-07-02 17:43:08 +00002285 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002286 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002287 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002288 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002289 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002290 TemplateParams,
John McCall07e91c02009-08-06 02:15:43 +00002291 BitfieldSize.release(),
Richard Smith2b013182012-06-10 03:12:00 +00002292 VS, HasInClassInit);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002293
2294 if (VarTemplateDecl *VT =
2295 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : 0)
2296 // Re-direct this decl to refer to the templated decl so that we can
2297 // initialize it.
2298 ThisDecl = VT->getTemplatedDecl();
2299
David Majnemer23252a32013-08-01 04:22:55 +00002300 if (ThisDecl && AccessAttrs)
Richard Smithf8a75c32013-08-29 00:47:48 +00002301 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00002302 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002303
Douglas Gregor728d00b2011-10-10 14:49:18 +00002304 // Handle the initializer.
David Blaikie35506f82013-01-30 01:22:18 +00002305 if (HasInClassInit != ICIS_NoInit &&
2306 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2307 DeclSpec::SCS_static) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002308 // The initializer was deferred; parse it and cache the tokens.
David Majnemer23252a32013-08-01 04:22:55 +00002309 Diag(Tok, getLangOpts().CPlusPlus11
2310 ? diag::warn_cxx98_compat_nonstatic_member_init
2311 : diag::ext_nonstatic_member_init);
Richard Smith5d164bc2011-10-15 05:09:34 +00002312
Richard Smith938f40b2011-06-11 17:19:42 +00002313 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002314 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2315 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002316 //
2317 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002318 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002319 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002320 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemer23252a32013-08-01 04:22:55 +00002321
2322 // Avoid later warnings about a class member of incomplete type.
David Blaikiecdd91db2012-02-14 09:00:46 +00002323 if (ThisDecl)
David Blaikiecdd91db2012-02-14 09:00:46 +00002324 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002325 } else
2326 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002327 } else if (HasInitializer) {
2328 // Normal initializer.
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002329 if (!Init.isUsable())
David Majnemer23252a32013-08-01 04:22:55 +00002330 Init = ParseCXXMemberInitializer(
2331 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2332
Douglas Gregor728d00b2011-10-10 14:49:18 +00002333 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002334 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002335 else if (ThisDecl)
Sebastian Redleef474c2012-02-22 10:50:08 +00002336 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smith74aeef52013-04-26 16:15:35 +00002337 DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002338 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor728d00b2011-10-10 14:49:18 +00002339 // No initializer.
Richard Smith74aeef52013-04-26 16:15:35 +00002340 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002341
Douglas Gregor728d00b2011-10-10 14:49:18 +00002342 if (ThisDecl) {
David Majnemer23252a32013-08-01 04:22:55 +00002343 if (!ThisDecl->isInvalidDecl()) {
2344 // Set the Decl for any late parsed attributes
2345 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2346 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2347
2348 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2349 LateParsedAttrs[i]->addDecl(ThisDecl);
2350 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002351 Actions.FinalizeDeclaration(ThisDecl);
2352 DeclsInGroup.push_back(ThisDecl);
David Majnemer23252a32013-08-01 04:22:55 +00002353
2354 if (DeclaratorInfo.isFunctionDeclarator() &&
2355 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2356 DeclSpec::SCS_typedef)
2357 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002358 }
David Majnemer23252a32013-08-01 04:22:55 +00002359 LateParsedAttrs.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002360
2361 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002362
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002363 // If we don't have a comma, it is either the end of the list (a ';')
2364 // or an error, bail out.
2365 if (Tok.isNot(tok::comma))
2366 break;
Mike Stump11289f42009-09-09 15:08:12 +00002367
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002368 // Consume the comma.
Richard Smithc8a79032012-01-09 22:31:44 +00002369 SourceLocation CommaLoc = ConsumeToken();
2370
2371 if (Tok.isAtStartOfLine() &&
2372 !MightBeDeclarator(Declarator::MemberContext)) {
2373 // This comma was followed by a line-break and something which can't be
2374 // the start of a declarator. The comma was probably a typo for a
2375 // semicolon.
2376 Diag(CommaLoc, diag::err_expected_semi_declaration)
2377 << FixItHint::CreateReplacement(CommaLoc, ";");
2378 ExpectSemi = false;
2379 break;
2380 }
Mike Stump11289f42009-09-09 15:08:12 +00002381
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002382 // Parse the next declarator.
2383 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002384 VS.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002385 BitfieldSize = true;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002386 Init = true;
2387 HasInitializer = false;
Richard Smith8d06f422012-01-12 23:53:29 +00002388 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002389
Bill Wendling44426052012-12-20 19:22:21 +00002390 // Attributes are only allowed on the second declarator.
John McCall53fa7142010-12-24 02:08:15 +00002391 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002392
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002393 if (Tok.isNot(tok::colon))
2394 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002395 }
2396
Richard Smithc8a79032012-01-09 22:31:44 +00002397 if (ExpectSemi &&
2398 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002399 // Skip to end of block or statement.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002400 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002401 // If we stopped at a ';', eat it.
Alp Toker35d87032013-12-30 23:29:50 +00002402 TryConsumeToken(tok::semi);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002403 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002404 }
2405
Rafael Espindolaab417692013-07-09 12:05:01 +00002406 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002407}
2408
Richard Smith938f40b2011-06-11 17:19:42 +00002409/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2410/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2411/// function definition. The location of the '=', if any, will be placed in
2412/// EqualLoc.
2413///
2414/// pure-specifier:
2415/// '= 0'
Sebastian Redleef474c2012-02-22 10:50:08 +00002416///
Richard Smith938f40b2011-06-11 17:19:42 +00002417/// brace-or-equal-initializer:
2418/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002419/// braced-init-list
2420///
Richard Smith938f40b2011-06-11 17:19:42 +00002421/// initializer-clause:
2422/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002423/// braced-init-list
2424///
Richard Smithda35e962013-11-09 04:52:51 +00002425/// defaulted/deleted function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002426/// '=' 'default'
2427/// '=' 'delete'
2428///
2429/// Prior to C++0x, the assignment-expression in an initializer-clause must
2430/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002431ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002432 SourceLocation &EqualLoc) {
2433 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2434 && "Data member initializer not starting with '=' or '{'");
2435
Douglas Gregor926410d2012-02-21 02:22:07 +00002436 EnterExpressionEvaluationContext Context(Actions,
2437 Sema::PotentiallyEvaluated,
2438 D);
Richard Smith938f40b2011-06-11 17:19:42 +00002439 if (Tok.is(tok::equal)) {
2440 EqualLoc = ConsumeToken();
2441 if (Tok.is(tok::kw_delete)) {
2442 // In principle, an initializer of '= delete p;' is legal, but it will
2443 // never type-check. It's better to diagnose it as an ill-formed expression
2444 // than as an ill-formed deleted non-function member.
2445 // An initializer of '= delete p, foo' will never be parsed, because
2446 // a top-level comma always ends the initializer expression.
2447 const Token &Next = NextToken();
2448 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
Richard Smith34f30512013-11-23 04:06:09 +00002449 Next.is(tok::eof)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002450 if (IsFunction)
2451 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2452 << 1 /* delete */;
2453 else
2454 Diag(ConsumeToken(), diag::err_deleted_non_function);
2455 return ExprResult();
2456 }
2457 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002458 if (IsFunction)
2459 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2460 << 0 /* default */;
2461 else
2462 Diag(ConsumeToken(), diag::err_default_special_members);
2463 return ExprResult();
2464 }
2465
Sebastian Redleef474c2012-02-22 10:50:08 +00002466 }
2467 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002468}
2469
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002470/// ParseCXXMemberSpecification - Parse the class definition.
2471///
2472/// member-specification:
2473/// member-declaration member-specification[opt]
2474/// access-specifier ':' member-specification[opt]
2475///
Joao Matose9a3ed42012-08-31 22:18:20 +00002476void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00002477 SourceLocation AttrFixitLoc,
Richard Smith4c96e992013-02-19 23:47:15 +00002478 ParsedAttributesWithRange &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00002479 unsigned TagType, Decl *TagDecl) {
2480 assert((TagType == DeclSpec::TST_struct ||
2481 TagType == DeclSpec::TST_interface ||
2482 TagType == DeclSpec::TST_union ||
2483 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2484
John McCallfaf5fb42010-08-26 23:41:50 +00002485 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2486 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00002487
Douglas Gregoredf8f392010-01-16 20:52:59 +00002488 // Determine whether this is a non-nested class. Note that local
2489 // classes are *not* considered to be nested classes.
2490 bool NonNestedClass = true;
2491 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002492 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002493 if (S->isClassScope()) {
2494 // We're inside a class scope, so this is a nested class.
2495 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00002496
2497 // The Microsoft extension __interface does not permit nested classes.
2498 if (getCurrentClass().IsInterface) {
2499 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2500 << /*ErrorType=*/6
2501 << (isa<NamedDecl>(TagDecl)
2502 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2503 : "<anonymous>");
2504 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00002505 break;
2506 }
2507
2508 if ((S->getFlags() & Scope::FnScope)) {
2509 // If we're in a function or function template declared in the
2510 // body of a class, then this is a local class rather than a
2511 // nested class.
2512 const Scope *Parent = S->getParent();
2513 if (Parent->isTemplateParamScope())
2514 Parent = Parent->getParent();
2515 if (Parent->isClassScope())
2516 break;
2517 }
2518 }
2519 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002520
2521 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00002522 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002523
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002524 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00002525 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2526 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002527
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002528 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002529 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002530
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002531 SourceLocation FinalLoc;
David Majnemera5433082013-10-18 00:33:31 +00002532 bool IsFinalSpelledSealed = false;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002533
2534 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002535 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemera5433082013-10-18 00:33:31 +00002536 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2537 assert((Specifier == VirtSpecifiers::VS_Final ||
2538 Specifier == VirtSpecifiers::VS_Sealed) &&
2539 "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00002540 FinalLoc = ConsumeToken();
David Majnemera5433082013-10-18 00:33:31 +00002541 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002542
David Majnemera5433082013-10-18 00:33:31 +00002543 if (TagType == DeclSpec::TST_interface)
John McCalldb632ac2012-09-25 07:32:39 +00002544 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemera5433082013-10-18 00:33:31 +00002545 << VirtSpecifiers::getSpecifierName(Specifier);
2546 else if (Specifier == VirtSpecifiers::VS_Final)
2547 Diag(FinalLoc, getLangOpts().CPlusPlus11
2548 ? diag::warn_cxx98_compat_override_control_keyword
2549 : diag::ext_override_control_keyword)
2550 << VirtSpecifiers::getSpecifierName(Specifier);
2551 else if (Specifier == VirtSpecifiers::VS_Sealed)
2552 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Michael Han9407e502012-11-26 22:54:45 +00002553
Michael Han309af292013-01-07 16:57:11 +00002554 // Parse any C++11 attributes after 'final' keyword.
2555 // These attributes are not allowed to appear here,
2556 // and the only possible place for them to appertain
2557 // to the class would be between class-key and class-name.
Richard Smith4c96e992013-02-19 23:47:15 +00002558 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002559 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002560
John McCall2d814c32009-12-19 21:48:58 +00002561 if (Tok.is(tok::colon)) {
2562 ParseBaseClause(TagDecl);
2563
2564 if (!Tok.is(tok::l_brace)) {
2565 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCall2ff380a2010-03-17 00:38:33 +00002566
2567 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002568 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002569 return;
2570 }
2571 }
2572
2573 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002574 BalancedDelimiterTracker T(*this, tok::l_brace);
2575 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00002576
John McCall08bede42010-05-28 08:11:17 +00002577 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00002578 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemera5433082013-10-18 00:33:31 +00002579 IsFinalSpelledSealed,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002580 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00002581
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002582 // C++ 11p3: Members of a class defined with the keyword class are private
2583 // by default. Members of a class defined with the keywords struct or union
2584 // are public by default.
2585 AccessSpecifier CurAS;
2586 if (TagType == DeclSpec::TST_class)
2587 CurAS = AS_private;
2588 else
2589 CurAS = AS_public;
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002590 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002591
Douglas Gregor9377c822010-06-21 22:31:09 +00002592 if (TagDecl) {
2593 // While we still have something to read, read the member-declarations.
Richard Smith34f30512013-11-23 04:06:09 +00002594 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Douglas Gregor9377c822010-06-21 22:31:09 +00002595 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002596
David Blaikiebbafb8a2012-03-11 07:00:24 +00002597 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet8f981d52011-05-25 10:19:49 +00002598 Tok.is(tok::kw___if_not_exists))) {
2599 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2600 continue;
2601 }
2602
Douglas Gregor9377c822010-06-21 22:31:09 +00002603 // Check for extraneous top-level semicolon.
2604 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002605 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor9377c822010-06-21 22:31:09 +00002606 continue;
2607 }
2608
Eli Friedmanec52f922012-02-23 23:47:16 +00002609 if (Tok.is(tok::annot_pragma_vis)) {
2610 HandlePragmaVisibility();
2611 continue;
2612 }
2613
2614 if (Tok.is(tok::annot_pragma_pack)) {
2615 HandlePragmaPack();
2616 continue;
2617 }
2618
Argyrios Kyrtzidis5c2021b2012-10-12 17:39:59 +00002619 if (Tok.is(tok::annot_pragma_align)) {
2620 HandlePragmaAlign();
2621 continue;
2622 }
2623
Alexey Bataeva769e072013-03-22 06:34:35 +00002624 if (Tok.is(tok::annot_pragma_openmp)) {
2625 ParseOpenMPDeclarativeDirective();
2626 continue;
2627 }
2628
Richard Smithda35e962013-11-09 04:52:51 +00002629 // If we see a namespace here, a close brace was missing somewhere.
2630 if (Tok.is(tok::kw_namespace)) {
Richard Smith2ac43ad2013-11-15 23:00:02 +00002631 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
Richard Smithda35e962013-11-09 04:52:51 +00002632 break;
2633 }
2634
Douglas Gregor9377c822010-06-21 22:31:09 +00002635 AccessSpecifier AS = getAccessSpecifierIfPresent();
2636 if (AS != AS_none) {
2637 // Current token is a C++ access specifier.
2638 CurAS = AS;
2639 SourceLocation ASLoc = Tok.getLocation();
David Blaikieeba32c22011-10-13 06:08:43 +00002640 unsigned TokLength = Tok.getLength();
Douglas Gregor9377c822010-06-21 22:31:09 +00002641 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002642 AccessAttrs.clear();
2643 MaybeParseGNUAttributes(AccessAttrs);
2644
David Blaikieeba32c22011-10-13 06:08:43 +00002645 SourceLocation EndLoc;
Alp Toker35d87032013-12-30 23:29:50 +00002646 if (TryConsumeToken(tok::colon, EndLoc)) {
2647 } else if (TryConsumeToken(tok::semi, EndLoc)) {
2648 Diag(EndLoc, diag::err_expected)
2649 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002650 } else {
2651 EndLoc = ASLoc.getLocWithOffset(TokLength);
Alp Toker35d87032013-12-30 23:29:50 +00002652 Diag(EndLoc, diag::err_expected)
2653 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002654 }
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002655
John McCalldb632ac2012-09-25 07:32:39 +00002656 // The Microsoft extension __interface does not permit non-public
2657 // access specifiers.
2658 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2659 Diag(ASLoc, diag::err_access_specifier_interface)
2660 << (CurAS == AS_protected);
2661 }
2662
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002663 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2664 AccessAttrs.getList())) {
2665 // found another attribute than only annotations
2666 AccessAttrs.clear();
2667 }
2668
Douglas Gregor9377c822010-06-21 22:31:09 +00002669 continue;
2670 }
2671
Douglas Gregor9377c822010-06-21 22:31:09 +00002672 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002673 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002674 }
2675
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002676 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00002677 } else {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002678 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002679 }
Mike Stump11289f42009-09-09 15:08:12 +00002680
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002681 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002682 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002683 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002684
John McCall08bede42010-05-28 08:11:17 +00002685 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002686 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002687 T.getOpenLocation(),
2688 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002689 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002690
Douglas Gregor433e0532012-04-16 18:27:27 +00002691 // C++11 [class.mem]p2:
2692 // Within the class member-specification, the class is regarded as complete
Richard Smith2331bbf2012-05-02 22:22:32 +00002693 // within function bodies, default arguments, and
Douglas Gregor433e0532012-04-16 18:27:27 +00002694 // brace-or-equal-initializers for non-static data members (including such
2695 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00002696 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002697 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00002698 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002699 // declarations and the lexed inline method definitions, along with any
2700 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00002701 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002702 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002703 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00002704
2705 // We've finished with all pending member declarations.
2706 Actions.ActOnFinishCXXMemberDecls();
2707
Richard Smith938f40b2011-06-11 17:19:42 +00002708 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002709 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00002710 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002711 }
2712
John McCall08bede42010-05-28 08:11:17 +00002713 if (TagDecl)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002714 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2715 T.getCloseLocation());
John McCall2ff380a2010-03-17 00:38:33 +00002716
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002717 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002718 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002719 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002720}
Douglas Gregore8381c02008-11-05 04:29:56 +00002721
Richard Smith2ac43ad2013-11-15 23:00:02 +00002722void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithda35e962013-11-09 04:52:51 +00002723 assert(Tok.is(tok::kw_namespace));
2724
2725 // FIXME: Suggest where the close brace should have gone by looking
2726 // at indentation changes within the definition body.
Richard Smith2ac43ad2013-11-15 23:00:02 +00002727 Diag(D->getLocation(),
2728 diag::err_missing_end_of_definition) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002729 Diag(Tok.getLocation(),
Richard Smith2ac43ad2013-11-15 23:00:02 +00002730 diag::note_missing_end_of_definition_before) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002731
2732 // Push '};' onto the token stream to recover.
2733 PP.EnterToken(Tok);
2734
2735 Tok.startToken();
2736 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
2737 Tok.setKind(tok::semi);
2738 PP.EnterToken(Tok);
2739
2740 Tok.setKind(tok::r_brace);
2741}
2742
Douglas Gregore8381c02008-11-05 04:29:56 +00002743/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2744/// which explicitly initializes the members or base classes of a
2745/// class (C++ [class.base.init]). For example, the three initializers
2746/// after the ':' in the Derived constructor below:
2747///
2748/// @code
2749/// class Base { };
2750/// class Derived : Base {
2751/// int x;
2752/// float f;
2753/// public:
2754/// Derived(float f) : Base(), x(17), f(f) { }
2755/// };
2756/// @endcode
2757///
Mike Stump11289f42009-09-09 15:08:12 +00002758/// [C++] ctor-initializer:
2759/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00002760///
Mike Stump11289f42009-09-09 15:08:12 +00002761/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00002762/// mem-initializer ...[opt]
2763/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00002764void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00002765 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2766
John Wiegley1c0675e2011-04-28 01:08:34 +00002767 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2768 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00002769 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002770
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002771 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002772 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002773
Douglas Gregore8381c02008-11-05 04:29:56 +00002774 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002775 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00002776 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2777 MemInitializers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002778 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002779 } else {
2780 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2781 if (!MemInit.isInvalid())
2782 MemInitializers.push_back(MemInit.get());
2783 else
2784 AnyErrors = true;
2785 }
2786
Douglas Gregore8381c02008-11-05 04:29:56 +00002787 if (Tok.is(tok::comma))
2788 ConsumeToken();
2789 else if (Tok.is(tok::l_brace))
2790 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00002791 // If the next token looks like a base or member initializer, assume that
2792 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00002793 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2794 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2795 Diag(Loc, diag::err_ctor_init_missing_comma)
2796 << FixItHint::CreateInsertion(Loc, ", ");
2797 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00002798 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alp Tokerec543272013-12-24 09:48:30 +00002799 Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
2800 << tok::comma;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002801 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregore8381c02008-11-05 04:29:56 +00002802 break;
2803 }
2804 } while (true);
2805
David Blaikie3fc2f912013-01-17 05:26:25 +00002806 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002807 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00002808}
2809
2810/// ParseMemInitializer - Parse a C++ member initializer, which is
2811/// part of a constructor initializer that explicitly initializes one
2812/// member or base class (C++ [class.base.init]). See
2813/// ParseConstructorInitializer for an example.
2814///
2815/// [C++] mem-initializer:
2816/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002817/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00002818///
Douglas Gregore8381c02008-11-05 04:29:56 +00002819/// [C++] mem-initializer-id:
2820/// '::'[opt] nested-name-specifier[opt] class-name
2821/// identifier
John McCall48871652010-08-21 09:40:31 +00002822Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00002823 // parse '::'[opt] nested-name-specifier[opt]
2824 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002825 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00002826 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002827 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002828 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00002829 if (TemplateId->Kind == TNK_Type_template ||
2830 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00002831 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002832 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00002833 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002834 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002835 }
David Blaikie186a8892012-01-24 06:03:59 +00002836 // Uses of decltype will already have been converted to annot_decltype by
2837 // ParseOptionalCXXScopeSpecifier at this point.
2838 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2839 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002840 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00002841 return true;
2842 }
Mike Stump11289f42009-09-09 15:08:12 +00002843
David Blaikie186a8892012-01-24 06:03:59 +00002844 IdentifierInfo *II = 0;
2845 DeclSpec DS(AttrFactory);
2846 SourceLocation IdLoc = Tok.getLocation();
2847 if (Tok.is(tok::annot_decltype)) {
2848 // Get the decltype expression, if there is one.
2849 ParseDecltypeSpecifier(DS);
2850 } else {
2851 if (Tok.is(tok::identifier))
2852 // Get the identifier. This may be a member name or a class name,
2853 // but we'll let the semantic analysis determine which it is.
2854 II = Tok.getIdentifierInfo();
2855 ConsumeToken();
2856 }
2857
Douglas Gregore8381c02008-11-05 04:29:56 +00002858
2859 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002860 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002861 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2862
Sebastian Redla74948d2011-09-24 17:48:25 +00002863 ExprResult InitList = ParseBraceInitializer();
2864 if (InitList.isInvalid())
2865 return true;
2866
2867 SourceLocation EllipsisLoc;
2868 if (Tok.is(tok::ellipsis))
2869 EllipsisLoc = ConsumeToken();
2870
2871 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002872 TemplateTypeTy, DS, IdLoc,
2873 InitList.take(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00002874 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002875 BalancedDelimiterTracker T(*this, tok::l_paren);
2876 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00002877
Sebastian Redl3da34892011-06-05 12:23:16 +00002878 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00002879 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00002880 CommaLocsTy CommaLocs;
2881 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002882 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00002883 return true;
2884 }
2885
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002886 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00002887
2888 SourceLocation EllipsisLoc;
2889 if (Tok.is(tok::ellipsis))
2890 EllipsisLoc = ConsumeToken();
2891
2892 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002893 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenko139474d2013-05-09 23:51:52 +00002894 T.getOpenLocation(), ArgExprs,
2895 T.getCloseLocation(), EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00002896 }
2897
Alp Tokerec543272013-12-24 09:48:30 +00002898 if (getLangOpts().CPlusPlus11)
2899 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
2900 else
2901 return Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregore8381c02008-11-05 04:29:56 +00002902}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002903
Sebastian Redl965b0e32011-03-05 14:45:16 +00002904/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002905///
Douglas Gregor356513d2008-12-01 18:00:20 +00002906/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00002907/// dynamic-exception-specification
2908/// noexcept-specification
2909///
2910/// noexcept-specification:
2911/// 'noexcept'
2912/// 'noexcept' '(' constant-expression ')'
2913ExceptionSpecificationType
Richard Smith2331bbf2012-05-02 22:22:32 +00002914Parser::tryParseExceptionSpecification(
Douglas Gregor433e0532012-04-16 18:27:27 +00002915 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002916 SmallVectorImpl<ParsedType> &DynamicExceptions,
2917 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00002918 ExprResult &NoexceptExpr) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002919 ExceptionSpecificationType Result = EST_None;
2920
2921 // See if there's a dynamic specification.
2922 if (Tok.is(tok::kw_throw)) {
2923 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2924 DynamicExceptions,
2925 DynamicExceptionRanges);
2926 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2927 "Produced different number of exception types and ranges.");
2928 }
2929
2930 // If there's no noexcept specification, we're done.
2931 if (Tok.isNot(tok::kw_noexcept))
2932 return Result;
2933
Richard Smithb15c11c2011-10-17 23:06:20 +00002934 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2935
Sebastian Redl965b0e32011-03-05 14:45:16 +00002936 // If we already had a dynamic specification, parse the noexcept for,
2937 // recovery, but emit a diagnostic and don't store the results.
2938 SourceRange NoexceptRange;
2939 ExceptionSpecificationType NoexceptType = EST_None;
2940
2941 SourceLocation KeywordLoc = ConsumeToken();
2942 if (Tok.is(tok::l_paren)) {
2943 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002944 BalancedDelimiterTracker T(*this, tok::l_paren);
2945 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00002946 NoexceptType = EST_ComputedNoexcept;
2947 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002948 // The argument must be contextually convertible to bool. We use
2949 // ActOnBooleanCondition for this purpose.
2950 if (!NoexceptExpr.isInvalid())
2951 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2952 NoexceptExpr.get());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002953 T.consumeClose();
2954 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl965b0e32011-03-05 14:45:16 +00002955 } else {
2956 // There is no argument.
2957 NoexceptType = EST_BasicNoexcept;
2958 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2959 }
2960
2961 if (Result == EST_None) {
2962 SpecificationRange = NoexceptRange;
2963 Result = NoexceptType;
2964
2965 // If there's a dynamic specification after a noexcept specification,
2966 // parse that and ignore the results.
2967 if (Tok.is(tok::kw_throw)) {
2968 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2969 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2970 DynamicExceptionRanges);
2971 }
2972 } else {
2973 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2974 }
2975
2976 return Result;
2977}
2978
Richard Smith8ca78a12013-06-13 02:02:51 +00002979static void diagnoseDynamicExceptionSpecification(
2980 Parser &P, const SourceRange &Range, bool IsNoexcept) {
2981 if (P.getLangOpts().CPlusPlus11) {
2982 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
2983 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
2984 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
2985 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
2986 }
2987}
2988
Sebastian Redl965b0e32011-03-05 14:45:16 +00002989/// ParseDynamicExceptionSpecification - Parse a C++
2990/// dynamic-exception-specification (C++ [except.spec]).
2991///
2992/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00002993/// 'throw' '(' type-id-list [opt] ')'
2994/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00002995///
Douglas Gregor356513d2008-12-01 18:00:20 +00002996/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00002997/// type-id ... [opt]
2998/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002999///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003000ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
3001 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003002 SmallVectorImpl<ParsedType> &Exceptions,
3003 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003004 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00003005
Sebastian Redl965b0e32011-03-05 14:45:16 +00003006 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003007 BalancedDelimiterTracker T(*this, tok::l_paren);
3008 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003009 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3010 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003011 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003012 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003013
Douglas Gregor356513d2008-12-01 18:00:20 +00003014 // Parse throw(...), a Microsoft extension that means "this function
3015 // can throw anything".
3016 if (Tok.is(tok::ellipsis)) {
3017 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003018 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00003019 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003020 T.consumeClose();
3021 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003022 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003023 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00003024 }
3025
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003026 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00003027 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003028 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00003029 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00003030
Douglas Gregor830837d2010-12-20 23:57:46 +00003031 if (Tok.is(tok::ellipsis)) {
3032 // C++0x [temp.variadic]p5:
3033 // - In a dynamic-exception-specification (15.4); the pattern is a
3034 // type-id.
3035 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003036 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00003037 if (!Res.isInvalid())
3038 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3039 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003040
Sebastian Redld6434562009-05-29 18:02:33 +00003041 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003042 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00003043 Ranges.push_back(Range);
3044 }
Douglas Gregor830837d2010-12-20 23:57:46 +00003045
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003046 if (Tok.is(tok::comma))
3047 ConsumeToken();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003048 else
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003049 break;
3050 }
3051
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003052 T.consumeClose();
3053 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003054 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3055 Exceptions.empty());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003056 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003057}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003058
Douglas Gregor7fb25412010-10-01 18:44:50 +00003059/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3060/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003061TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00003062 assert(Tok.is(tok::arrow) && "expected arrow");
3063
3064 ConsumeToken();
3065
Richard Smithbfdb1082012-03-12 08:56:40 +00003066 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00003067}
3068
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003069/// \brief We have just started parsing the definition of a new class,
3070/// so push that class onto our stack of classes that is currently
3071/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00003072Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00003073Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3074 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003075 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003076 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00003077 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00003078 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003079}
3080
3081/// \brief Deallocate the given parsed class and all of its nested
3082/// classes.
3083void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00003084 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3085 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003086 delete Class;
3087}
3088
3089/// \brief Pop the top class of the stack of classes that are
3090/// currently being parsed.
3091///
3092/// This routine should be called when we have finished parsing the
3093/// definition of a class, but have not yet popped the Scope
3094/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00003095void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003096 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00003097
John McCallc1465822011-02-14 07:13:47 +00003098 Actions.PopParsingClass(state);
3099
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003100 ParsingClass *Victim = ClassStack.top();
3101 ClassStack.pop();
3102 if (Victim->TopLevelClass) {
3103 // Deallocate all of the nested classes of this class,
3104 // recursively: we don't need to keep any of this information.
3105 DeallocateParsedClasses(Victim);
3106 return;
Mike Stump11289f42009-09-09 15:08:12 +00003107 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003108 assert(!ClassStack.empty() && "Missing top-level class?");
3109
Douglas Gregorefc46952010-10-12 16:25:54 +00003110 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003111 // The victim is a nested class, but we will not need to perform
3112 // any processing after the definition of this class since it has
3113 // no members whose handling was delayed. Therefore, we can just
3114 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00003115 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003116 return;
3117 }
3118
3119 // This nested class has some members that will need to be processed
3120 // after the top-level class is completely defined. Therefore, add
3121 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003122 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00003123 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00003124 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003125}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003126
Richard Smith3dff2512012-04-10 03:25:07 +00003127/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3128///
3129/// \return the parsed identifier on success, and 0 if the next token is not an
3130/// attribute-token.
3131///
3132/// C++11 [dcl.attr.grammar]p3:
3133/// If a keyword or an alternative token that satisfies the syntactic
3134/// requirements of an identifier is contained in an attribute-token,
3135/// it is considered an identifier.
3136IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3137 switch (Tok.getKind()) {
3138 default:
3139 // Identifiers and keywords have identifier info attached.
3140 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3141 Loc = ConsumeToken();
3142 return II;
3143 }
3144 return 0;
3145
3146 case tok::ampamp: // 'and'
3147 case tok::pipe: // 'bitor'
3148 case tok::pipepipe: // 'or'
3149 case tok::caret: // 'xor'
3150 case tok::tilde: // 'compl'
3151 case tok::amp: // 'bitand'
3152 case tok::ampequal: // 'and_eq'
3153 case tok::pipeequal: // 'or_eq'
3154 case tok::caretequal: // 'xor_eq'
3155 case tok::exclaim: // 'not'
3156 case tok::exclaimequal: // 'not_eq'
3157 // Alternative tokens do not have identifier info, but their spelling
3158 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003159 SmallString<8> SpellingBuf;
Richard Smith3dff2512012-04-10 03:25:07 +00003160 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rosea7d03842013-02-08 22:30:41 +00003161 if (isLetter(Spelling[0])) {
Richard Smith3dff2512012-04-10 03:25:07 +00003162 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003163 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003164 }
3165 return 0;
3166 }
3167}
3168
Michael Han23214e52012-10-03 01:56:22 +00003169static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3170 IdentifierInfo *ScopeName) {
3171 switch (AttributeList::getKind(AttrName, ScopeName,
3172 AttributeList::AS_CXX11)) {
3173 case AttributeList::AT_CarriesDependency:
3174 case AttributeList::AT_FallThrough:
Richard Smith10876ef2013-01-17 01:30:42 +00003175 case AttributeList::AT_CXX11NoReturn: {
Michael Han23214e52012-10-03 01:56:22 +00003176 return true;
3177 }
3178
3179 default:
3180 return false;
3181 }
3182}
3183
Richard Smith3dff2512012-04-10 03:25:07 +00003184/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003185/// only parses standard attributes.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003186///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003187/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003188/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003189/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003190///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003191/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003192/// attribute[opt]
3193/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003194/// attribute '...'
3195/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003196///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003197/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003198/// attribute-token attribute-argument-clause[opt]
3199///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003200/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003201/// identifier
3202/// attribute-scoped-token
3203///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003204/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003205/// attribute-namespace '::' identifier
3206///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003207/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003208/// identifier
3209///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003210/// [C++11] attribute-argument-clause:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003211/// '(' balanced-token-seq ')'
3212///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003213/// [C++11] balanced-token-seq:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003214/// balanced-token
3215/// balanced-token-seq balanced-token
3216///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003217/// [C++11] balanced-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003218/// '(' balanced-token-seq ')'
3219/// '[' balanced-token-seq ']'
3220/// '{' balanced-token-seq '}'
3221/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smith3dff2512012-04-10 03:25:07 +00003222void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003223 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003224 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003225 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003226 ParseAlignmentSpecifier(attrs, endLoc);
3227 return;
3228 }
3229
Alexis Hunt96d5c762009-11-21 08:43:09 +00003230 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003231 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003232
Richard Smithf679b5b2011-10-14 20:48:27 +00003233 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3234
Alexis Hunt96d5c762009-11-21 08:43:09 +00003235 ConsumeBracket();
3236 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003237
Richard Smith10876ef2013-01-17 01:30:42 +00003238 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3239
Richard Smith3dff2512012-04-10 03:25:07 +00003240 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003241 // attribute not present
3242 if (Tok.is(tok::comma)) {
3243 ConsumeToken();
3244 continue;
3245 }
3246
Richard Smith3dff2512012-04-10 03:25:07 +00003247 SourceLocation ScopeLoc, AttrLoc;
3248 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3249
3250 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3251 if (!AttrName)
3252 // Break out to the "expected ']'" diagnostic.
3253 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003254
Alexis Hunt96d5c762009-11-21 08:43:09 +00003255 // scoped attribute
3256 if (Tok.is(tok::coloncolon)) {
3257 ConsumeToken();
3258
Richard Smith3dff2512012-04-10 03:25:07 +00003259 ScopeName = AttrName;
3260 ScopeLoc = AttrLoc;
3261
3262 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3263 if (!AttrName) {
Alp Tokerec543272013-12-24 09:48:30 +00003264 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003265 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003266 continue;
3267 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003268 }
3269
Michael Han23214e52012-10-03 01:56:22 +00003270 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003271 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003272
Richard Smith10876ef2013-01-17 01:30:42 +00003273 if (StandardAttr &&
3274 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3275 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3276 << AttrName << SourceRange(SeenAttrs[AttrName]);
3277
Michael Han23214e52012-10-03 01:56:22 +00003278 // Parse attribute arguments
3279 if (Tok.is(tok::l_paren)) {
3280 if (ScopeName && ScopeName->getName() == "gnu") {
3281 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3282 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3283 AttrParsed = true;
3284 } else {
3285 if (StandardAttr)
3286 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3287 << AttrName->getName();
3288
3289 // FIXME: handle other formats of c++11 attribute arguments
3290 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003291 SkipUntil(tok::r_paren);
Michael Han23214e52012-10-03 01:56:22 +00003292 }
3293 }
3294
3295 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003296 attrs.addNew(AttrName,
3297 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3298 AttrLoc),
Aaron Ballman00e99962013-08-31 01:11:41 +00003299 ScopeName, ScopeLoc, 0, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003300
Richard Smith3dff2512012-04-10 03:25:07 +00003301 if (Tok.is(tok::ellipsis)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003302 ConsumeToken();
Michael Han23214e52012-10-03 01:56:22 +00003303
3304 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3305 << AttrName->getName();
Richard Smith3dff2512012-04-10 03:25:07 +00003306 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003307 }
3308
3309 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003310 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003311 if (endLoc)
3312 *endLoc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00003313 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003314 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003315}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003316
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003317/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003318///
3319/// attribute-specifier-seq:
3320/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003321void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003322 SourceLocation *endLoc) {
Richard Smith4cabd042013-02-22 09:15:49 +00003323 assert(getLangOpts().CPlusPlus11);
3324
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003325 SourceLocation StartLoc = Tok.getLocation(), Loc;
3326 if (!endLoc)
3327 endLoc = &Loc;
3328
Douglas Gregor6f981002011-10-07 20:35:25 +00003329 do {
Richard Smith3dff2512012-04-10 03:25:07 +00003330 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003331 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003332
3333 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003334}
3335
Richard Smithc2c8bb82013-10-15 01:34:54 +00003336void Parser::DiagnoseAndSkipCXX11Attributes() {
3337 if (!isCXX11AttributeSpecifier())
3338 return;
3339
3340 // Start and end location of an attribute or an attribute list.
3341 SourceLocation StartLoc = Tok.getLocation();
3342 SourceLocation EndLoc;
3343
3344 do {
3345 if (Tok.is(tok::l_square)) {
3346 BalancedDelimiterTracker T(*this, tok::l_square);
3347 T.consumeOpen();
3348 T.skipToEnd();
3349 EndLoc = T.getCloseLocation();
3350 } else {
3351 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3352 ConsumeToken();
3353 BalancedDelimiterTracker T(*this, tok::l_paren);
3354 if (!T.consumeOpen())
3355 T.skipToEnd();
3356 EndLoc = T.getCloseLocation();
3357 }
3358 } while (isCXX11AttributeSpecifier());
3359
3360 if (EndLoc.isValid()) {
3361 SourceRange Range(StartLoc, EndLoc);
3362 Diag(StartLoc, diag::err_attributes_not_allowed)
3363 << Range;
3364 }
3365}
3366
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003367/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3368///
3369/// [MS] ms-attribute:
3370/// '[' token-seq ']'
3371///
3372/// [MS] ms-attribute-seq:
3373/// ms-attribute[opt]
3374/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00003375void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3376 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003377 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3378
3379 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003380 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003381 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003382 SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
John McCall53fa7142010-12-24 02:08:15 +00003383 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003384 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3385 }
3386}
Francois Pichet8f981d52011-05-25 10:19:49 +00003387
3388void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3389 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00003390 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00003391 if (ParseMicrosoftIfExistsCondition(Result))
3392 return;
3393
Douglas Gregor43edb322011-10-24 22:31:10 +00003394 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3395 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00003396 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet8f981d52011-05-25 10:19:49 +00003397 return;
3398 }
Francois Pichet8f981d52011-05-25 10:19:49 +00003399
Douglas Gregor43edb322011-10-24 22:31:10 +00003400 switch (Result.Behavior) {
3401 case IEB_Parse:
3402 // Parse the declarations below.
3403 break;
3404
3405 case IEB_Dependent:
3406 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3407 << Result.IsIfExists;
3408 // Fall through to skip.
3409
3410 case IEB_Skip:
3411 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00003412 return;
3413 }
3414
Richard Smith34f30512013-11-23 04:06:09 +00003415 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003416 // __if_exists, __if_not_exists can nest.
3417 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3418 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3419 continue;
3420 }
3421
3422 // Check for extraneous top-level semicolon.
3423 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003424 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00003425 continue;
3426 }
3427
3428 AccessSpecifier AS = getAccessSpecifierIfPresent();
3429 if (AS != AS_none) {
3430 // Current token is a C++ access specifier.
3431 CurAS = AS;
3432 SourceLocation ASLoc = Tok.getLocation();
3433 ConsumeToken();
3434 if (Tok.is(tok::colon))
3435 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3436 else
Alp Toker35d87032013-12-30 23:29:50 +00003437 Diag(Tok, diag::err_expected) << tok::colon;
Francois Pichet8f981d52011-05-25 10:19:49 +00003438 ConsumeToken();
3439 continue;
3440 }
3441
3442 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00003443 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet8f981d52011-05-25 10:19:49 +00003444 }
Douglas Gregor43edb322011-10-24 22:31:10 +00003445
3446 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00003447}