blob: 85b2aa7cf2d2cec2a1d72a70a9e18c9d54717ab7 [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();
Alp Toker383d2c42014-01-01 03:08:43 +0000266 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
267 SkipUntil(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();
Alp Toker383d2c42014-01-01 03:08:43 +0000431 if (ExpectAndConsume(tok::semi,
432 GNUAttr ? diag::err_expected_semi_after_attribute_list
433 : diag::err_expected_semi_after_namespace_name))
434 SkipUntil(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();
Alp Toker383d2c42014-01-01 03:08:43 +0000591 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
592 !Attrs.empty() ? "attributes list"
593 : IsAliasDecl ? "alias declaration"
594 : "using declaration"))
595 SkipUntil(tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000596
John McCall9b72f892010-11-10 02:40:36 +0000597 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith810ad3e2013-01-29 10:02:16 +0000598 // In C++11, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000599 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000600 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-11-10 02:40:36 +0000601 SourceRange R = TemplateInfo.getSourceRange();
602 Diag(UsingLoc, diag::err_templated_using_declaration)
603 << R << FixItHint::CreateRemoval(R);
604
605 // Unfortunately, we have to bail out instead of recovering by
606 // ignoring the parameters, just in case the nested name specifier
607 // depends on the parameters.
608 return 0;
609 }
610
Douglas Gregor882a61a2011-09-26 14:30:28 +0000611 // "typename" keyword is allowed for identifiers only,
612 // because it may be a type definition.
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000613 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor882a61a2011-09-26 14:30:28 +0000614 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
615 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000616 // Proceed parsing, but reset the HasTypenameKeyword flag.
617 HasTypenameKeyword = false;
Douglas Gregor882a61a2011-09-26 14:30:28 +0000618 }
619
Richard Smith3f1b5d02011-05-05 21:57:07 +0000620 if (IsAliasDecl) {
621 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000622 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3f1b5d02011-05-05 21:57:07 +0000623 TemplateParams ? TemplateParams->data() : 0,
624 TemplateParams ? TemplateParams->size() : 0);
625 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith54ecd982013-02-20 19:22:51 +0000626 UsingLoc, Name, Attrs.getList(),
627 TypeAlias);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000628 }
Richard Smithdda56e42011-04-15 14:24:37 +0000629
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000630 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
631 /* HasUsingKeyword */ true, UsingLoc,
632 SS, Name, Attrs.getList(),
633 HasTypenameKeyword, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000634}
635
Benjamin Kramere56f3932011-12-23 17:00:35 +0000636/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000637///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000638/// [C++0x] static_assert-declaration:
639/// static_assert ( constant-expression , string-literal ) ;
640///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000641/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000642/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000643///
John McCall48871652010-08-21 09:40:31 +0000644Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000645 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
646 "Not a static_assert declaration");
647
David Blaikiebbafb8a2012-03-11 07:00:24 +0000648 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000649 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000650 if (Tok.is(tok::kw_static_assert))
651 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000652
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000653 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000654
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000655 BalancedDelimiterTracker T(*this, tok::l_paren);
656 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000657 Diag(Tok, diag::err_expected) << tok::l_paren;
Richard Smith76965712012-09-13 19:12:50 +0000658 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000659 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000660 }
Mike Stump11289f42009-09-09 15:08:12 +0000661
John McCalldadc5752010-08-24 06:29:42 +0000662 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000663 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000664 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000665 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000666 }
Mike Stump11289f42009-09-09 15:08:12 +0000667
Alp Toker383d2c42014-01-01 03:08:43 +0000668 if (ExpectAndConsume(tok::comma)) {
669 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000670 return 0;
Alp Toker383d2c42014-01-01 03:08:43 +0000671 }
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000672
Richard Smithf506eaf2012-03-05 23:20:05 +0000673 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000674 Diag(Tok, diag::err_expected_string_literal)
675 << /*Source='static_assert'*/1;
Richard Smith76965712012-09-13 19:12:50 +0000676 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000677 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000678 }
Mike Stump11289f42009-09-09 15:08:12 +0000679
John McCalldadc5752010-08-24 06:29:42 +0000680 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smithd67aea22012-03-06 03:21:47 +0000681 if (AssertMessage.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000682 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000683 return 0;
Richard Smithd67aea22012-03-06 03:21:47 +0000684 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000685
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000686 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000687
Chris Lattner49836b42009-04-02 04:16:50 +0000688 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000689 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000690
John McCallb268a282010-08-23 23:25:46 +0000691 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
692 AssertExpr.take(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000693 AssertMessage.take(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000694 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000695}
696
Richard Smith74aeef52013-04-26 16:15:35 +0000697/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson74948d02009-06-24 17:47:40 +0000698///
699/// 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +0000700/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson74948d02009-06-24 17:47:40 +0000701///
David Blaikie15a430a2011-12-04 05:04:18 +0000702SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
703 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
704 && "Not a decltype specifier");
705
David Blaikie15a430a2011-12-04 05:04:18 +0000706 ExprResult Result;
707 SourceLocation StartLoc = Tok.getLocation();
708 SourceLocation EndLoc;
709
710 if (Tok.is(tok::annot_decltype)) {
711 Result = getExprAnnotation(Tok);
712 EndLoc = Tok.getAnnotationEndLoc();
713 ConsumeToken();
714 if (Result.isInvalid()) {
715 DS.SetTypeSpecError();
716 return EndLoc;
717 }
718 } else {
Richard Smith324df552012-02-24 22:30:04 +0000719 if (Tok.getIdentifierInfo()->isStr("decltype"))
720 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000721
David Blaikie15a430a2011-12-04 05:04:18 +0000722 ConsumeToken();
723
724 BalancedDelimiterTracker T(*this, tok::l_paren);
725 if (T.expectAndConsume(diag::err_expected_lparen_after,
726 "decltype", tok::r_paren)) {
727 DS.SetTypeSpecError();
728 return T.getOpenLocation() == Tok.getLocation() ?
729 StartLoc : T.getOpenLocation();
730 }
731
Richard Smith74aeef52013-04-26 16:15:35 +0000732 // Check for C++1y 'decltype(auto)'.
733 if (Tok.is(tok::kw_auto)) {
734 // No need to disambiguate here: an expression can't start with 'auto',
735 // because the typename-specifier in a function-style cast operation can't
736 // be 'auto'.
737 Diag(Tok.getLocation(),
738 getLangOpts().CPlusPlus1y
739 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
740 : diag::ext_decltype_auto_type_specifier);
741 ConsumeToken();
742 } else {
743 // Parse the expression
David Blaikie15a430a2011-12-04 05:04:18 +0000744
Richard Smith74aeef52013-04-26 16:15:35 +0000745 // C++11 [dcl.type.simple]p4:
746 // The operand of the decltype specifier is an unevaluated operand.
747 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
748 0, /*IsDecltype=*/true);
749 Result = ParseExpression();
750 if (Result.isInvalid()) {
751 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000752 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smith74aeef52013-04-26 16:15:35 +0000753 EndLoc = ConsumeParen();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000754 } else {
Richard Smith74aeef52013-04-26 16:15:35 +0000755 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
756 // Backtrack to get the location of the last token before the semi.
757 PP.RevertCachedTokens(2);
758 ConsumeToken(); // the semi.
759 EndLoc = ConsumeAnyToken();
760 assert(Tok.is(tok::semi));
761 } else {
762 EndLoc = Tok.getLocation();
763 }
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000764 }
Richard Smith74aeef52013-04-26 16:15:35 +0000765 return EndLoc;
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000766 }
Richard Smith74aeef52013-04-26 16:15:35 +0000767
768 Result = Actions.ActOnDecltypeExpression(Result.take());
David Blaikie15a430a2011-12-04 05:04:18 +0000769 }
770
771 // Match the ')'
772 T.consumeClose();
773 if (T.getCloseLocation().isInvalid()) {
774 DS.SetTypeSpecError();
775 // FIXME: this should return the location of the last token
776 // that was consumed (by "consumeClose()")
777 return T.getCloseLocation();
778 }
779
Richard Smithfd555f62012-02-22 02:04:18 +0000780 if (Result.isInvalid()) {
781 DS.SetTypeSpecError();
782 return T.getCloseLocation();
783 }
784
David Blaikie15a430a2011-12-04 05:04:18 +0000785 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000786 }
Richard Smith74aeef52013-04-26 16:15:35 +0000787 assert(!Result.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000788
Anders Carlsson74948d02009-06-24 17:47:40 +0000789 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000790 unsigned DiagID;
Anders Carlsson74948d02009-06-24 17:47:40 +0000791 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smith74aeef52013-04-26 16:15:35 +0000792 if (Result.get()
793 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
794 DiagID, Result.release())
795 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
796 DiagID)) {
John McCall49bfce42009-08-03 20:12:06 +0000797 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-12-04 05:04:18 +0000798 DS.SetTypeSpecError();
799 }
800 return EndLoc;
801}
802
803void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
804 SourceLocation StartLoc,
805 SourceLocation EndLoc) {
806 // make sure we have a token we can turn into an annotation token
807 if (PP.isBacktrackEnabled())
808 PP.RevertCachedTokens(1);
809 else
810 PP.EnterToken(Tok);
811
812 Tok.setKind(tok::annot_decltype);
Richard Smith74aeef52013-04-26 16:15:35 +0000813 setExprAnnotation(Tok,
814 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
815 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
816 ExprError());
David Blaikie15a430a2011-12-04 05:04:18 +0000817 Tok.setAnnotationEndLoc(EndLoc);
818 Tok.setLocation(StartLoc);
819 PP.AnnotateCachedTokens(Tok);
Anders Carlsson74948d02009-06-24 17:47:40 +0000820}
821
Alexis Hunt4a257072011-05-19 05:37:45 +0000822void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
823 assert(Tok.is(tok::kw___underlying_type) &&
824 "Not an underlying type specifier");
825
826 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000827 BalancedDelimiterTracker T(*this, tok::l_paren);
828 if (T.expectAndConsume(diag::err_expected_lparen_after,
829 "__underlying_type", tok::r_paren)) {
Alexis Hunt4a257072011-05-19 05:37:45 +0000830 return;
831 }
832
833 TypeResult Result = ParseTypeName();
834 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000835 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt4a257072011-05-19 05:37:45 +0000836 return;
837 }
838
839 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000840 T.consumeClose();
841 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +0000842 return;
843
844 const char *PrevSpec = 0;
845 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000846 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Alexis Hunt4a257072011-05-19 05:37:45 +0000847 DiagID, Result.release()))
848 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanellaa90af722013-07-06 18:54:58 +0000849 DS.setTypeofParensRange(T.getRange());
Alexis Hunt4a257072011-05-19 05:37:45 +0000850}
851
David Blaikie00ee7a082011-10-25 15:01:20 +0000852/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
853/// class name or decltype-specifier. Note that we only check that the result
854/// names a type; semantic analysis will need to verify that the type names a
855/// class. The result is either a type or null, depending on whether a type
856/// name was found.
Douglas Gregor831c93f2008-11-05 20:51:48 +0000857///
Richard Smith4c96e992013-02-19 23:47:15 +0000858/// base-type-specifier: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000859/// class-or-decltype
Richard Smith4c96e992013-02-19 23:47:15 +0000860/// class-or-decltype: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000861/// nested-name-specifier[opt] class-name
862/// decltype-specifier
Richard Smith4c96e992013-02-19 23:47:15 +0000863/// class-name: [C++ class.name]
Douglas Gregor831c93f2008-11-05 20:51:48 +0000864/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000865/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000866///
Richard Smith4c96e992013-02-19 23:47:15 +0000867/// In C++98, instead of base-type-specifier, we have:
868///
869/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie1cd50022011-10-25 17:10:12 +0000870Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
871 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-10-25 18:46:41 +0000872 // Ignore attempts to use typename
873 if (Tok.is(tok::kw_typename)) {
874 Diag(Tok, diag::err_expected_class_name_not_template)
875 << FixItHint::CreateRemoval(Tok.getLocation());
876 ConsumeToken();
877 }
878
David Blaikieafa155f2011-10-25 18:17:58 +0000879 // Parse optional nested-name-specifier
880 CXXScopeSpec SS;
881 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
882
883 BaseLoc = Tok.getLocation();
884
David Blaikie1cd50022011-10-25 17:10:12 +0000885 // Parse decltype-specifier
David Blaikie15a430a2011-12-04 05:04:18 +0000886 // tok == kw_decltype is just error recovery, it can only happen when SS
887 // isn't empty
888 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikieafa155f2011-10-25 18:17:58 +0000889 if (SS.isNotEmpty())
890 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
891 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie1cd50022011-10-25 17:10:12 +0000892 // Fake up a Declarator to use with ActOnTypeName.
893 DeclSpec DS(AttrFactory);
894
David Blaikie7491e732011-12-08 04:53:15 +0000895 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +0000896
897 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
898 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
899 }
900
Douglas Gregord54dfb82009-02-25 23:52:28 +0000901 // Check whether we have a template-id that names a type.
902 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000903 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +0000904 if (TemplateId->Kind == TNK_Type_template ||
905 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000906 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000907
908 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000909 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000910 EndLocation = Tok.getAnnotationEndLoc();
911 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000912
913 if (Type)
914 return Type;
915 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000916 }
917
918 // Fall through to produce an error below.
919 }
920
Douglas Gregor831c93f2008-11-05 20:51:48 +0000921 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000922 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000923 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000924 }
925
Douglas Gregor18473f32010-01-12 21:28:44 +0000926 IdentifierInfo *Id = Tok.getIdentifierInfo();
927 SourceLocation IdLoc = ConsumeToken();
928
929 if (Tok.is(tok::less)) {
930 // It looks the user intended to write a template-id here, but the
931 // template-name was wrong. Try to fix that.
932 TemplateNameKind TNK = TNK_Type_template;
933 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000934 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000935 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000936 Diag(IdLoc, diag::err_unknown_template_name)
937 << Id;
938 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000939
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000940 if (!Template) {
941 TemplateArgList TemplateArgs;
942 SourceLocation LAngleLoc, RAngleLoc;
943 ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
944 true, LAngleLoc, TemplateArgs, RAngleLoc);
Douglas Gregor18473f32010-01-12 21:28:44 +0000945 return true;
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000946 }
Douglas Gregor18473f32010-01-12 21:28:44 +0000947
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000948 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000949 UnqualifiedId TemplateName;
950 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000951
Douglas Gregor18473f32010-01-12 21:28:44 +0000952 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +0000953 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
954 TemplateName, true))
Douglas Gregor18473f32010-01-12 21:28:44 +0000955 return true;
956 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +0000957 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000958
Douglas Gregor18473f32010-01-12 21:28:44 +0000959 // If we didn't end up with a typename token, there's nothing more we
960 // can do.
961 if (Tok.isNot(tok::annot_typename))
962 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000963
Douglas Gregor18473f32010-01-12 21:28:44 +0000964 // Retrieve the type from the annotation token, consume that token, and
965 // return.
966 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +0000967 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +0000968 ConsumeToken();
969 return Type;
970 }
971
Douglas Gregor831c93f2008-11-05 20:51:48 +0000972 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +0000973 IdentifierInfo *CorrectedII = 0;
Douglas Gregore7c20652011-03-02 00:47:37 +0000974 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +0000975 false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +0000976 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +0000977 /*NonTrivialTypeSourceInfo=*/true,
978 &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000979 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +0000980 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000981 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000982 }
983
984 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +0000985 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000986
987 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +0000988 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +0000989 DS.SetRangeStart(IdLoc);
990 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +0000991 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +0000992
993 const char *PrevSpec = 0;
994 unsigned DiagID;
995 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
996
997 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
998 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +0000999}
1000
John McCall8d32c052012-05-22 21:28:12 +00001001void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
1002 while (Tok.is(tok::kw___single_inheritance) ||
1003 Tok.is(tok::kw___multiple_inheritance) ||
1004 Tok.is(tok::kw___virtual_inheritance)) {
1005 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1006 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +00001007 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
Aaron Ballman8edb5c22013-12-18 23:44:18 +00001008 AttributeList::AS_Keyword);
John McCall8d32c052012-05-22 21:28:12 +00001009 }
1010}
1011
Richard Smith369b9f92012-06-25 21:37:02 +00001012/// Determine whether the following tokens are valid after a type-specifier
1013/// which could be a standalone declaration. This will conservatively return
1014/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith200f47c2012-07-02 19:14:01 +00001015bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-06-25 21:37:02 +00001016 // This switch enumerates the valid "follow" set for type-specifiers.
1017 switch (Tok.getKind()) {
1018 default: break;
1019 case tok::semi: // struct foo {...} ;
1020 case tok::star: // struct foo {...} * P;
1021 case tok::amp: // struct foo {...} & R = ...
Richard Smith1ac67d12013-01-19 03:48:05 +00001022 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +00001023 case tok::identifier: // struct foo {...} V ;
1024 case tok::r_paren: //(struct foo {...} ) {4}
1025 case tok::annot_cxxscope: // struct foo {...} a:: b;
1026 case tok::annot_typename: // struct foo {...} a ::b;
1027 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1028 case tok::l_paren: // struct foo {...} ( x);
1029 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +00001030 case tok::kw_operator: // struct foo operator ++() {...}
Alp Tokerd3f79c52013-11-24 20:24:54 +00001031 case tok::kw___declspec: // struct foo {...} __declspec(...)
Richard Smith369b9f92012-06-25 21:37:02 +00001032 return true;
Richard Smith200f47c2012-07-02 19:14:01 +00001033 case tok::colon:
1034 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smith369b9f92012-06-25 21:37:02 +00001035 // Type qualifiers
1036 case tok::kw_const: // struct foo {...} const x;
1037 case tok::kw_volatile: // struct foo {...} volatile x;
1038 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001039 // Function specifiers
1040 // Note, no 'explicit'. An explicit function must be either a conversion
1041 // operator or a constructor. Either way, it can't have a return type.
1042 case tok::kw_inline: // struct foo inline f();
1043 case tok::kw_virtual: // struct foo virtual f();
1044 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +00001045 // Storage-class specifiers
1046 case tok::kw_static: // struct foo {...} static x;
1047 case tok::kw_extern: // struct foo {...} extern x;
1048 case tok::kw_typedef: // struct foo {...} typedef x;
1049 case tok::kw_register: // struct foo {...} register x;
1050 case tok::kw_auto: // struct foo {...} auto x;
1051 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001052 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +00001053 case tok::kw_constexpr: // struct foo {...} constexpr x;
1054 // As shown above, type qualifiers and storage class specifiers absolutely
1055 // can occur after class specifiers according to the grammar. However,
1056 // almost no one actually writes code like this. If we see one of these,
1057 // it is much more likely that someone missed a semi colon and the
1058 // type/storage class specifier we're seeing is part of the *next*
1059 // intended declaration, as in:
1060 //
1061 // struct foo { ... }
1062 // typedef int X;
1063 //
1064 // We'd really like to emit a missing semicolon error instead of emitting
1065 // an error on the 'int' saying that you can't have two type specifiers in
1066 // the same declaration of X. Because of this, we look ahead past this
1067 // token to see if it's a type specifier. If so, we know the code is
1068 // otherwise invalid, so we can produce the expected semi error.
1069 if (!isKnownToBeTypeSpecifier(NextToken()))
1070 return true;
1071 break;
1072 case tok::r_brace: // struct bar { struct foo {...} }
1073 // Missing ';' at end of struct is accepted as an extension in C mode.
1074 if (!getLangOpts().CPlusPlus)
1075 return true;
1076 break;
Richard Smith1ac67d12013-01-19 03:48:05 +00001077 // C++11 attributes
1078 case tok::l_square: // enum E [[]] x
1079 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1080 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith52c5b872013-01-29 04:13:32 +00001081 case tok::greater:
1082 // template<class T = class X>
1083 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001084 }
1085 return false;
1086}
1087
Douglas Gregor556877c2008-04-13 21:30:24 +00001088/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1089/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1090/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001091/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001092///
1093/// class-specifier: [C++ class]
1094/// class-head '{' member-specification[opt] '}'
1095/// class-head '{' member-specification[opt] '}' attributes[opt]
1096/// class-head:
1097/// class-key identifier[opt] base-clause[opt]
1098/// class-key nested-name-specifier identifier base-clause[opt]
1099/// class-key nested-name-specifier[opt] simple-template-id
1100/// base-clause[opt]
1101/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001102/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001103/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001104/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001105/// simple-template-id base-clause[opt]
1106/// class-key:
1107/// 'class'
1108/// 'struct'
1109/// 'union'
1110///
1111/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001112/// class-key ::[opt] nested-name-specifier[opt] identifier
1113/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1114/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001115///
1116/// Note that the C++ class-specifier and elaborated-type-specifier,
1117/// together, subsume the C99 struct-or-union-specifier:
1118///
1119/// struct-or-union-specifier: [C99 6.7.2.1]
1120/// struct-or-union identifier[opt] '{' struct-contents '}'
1121/// struct-or-union identifier
1122/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1123/// '}' attributes[opt]
1124/// [GNU] struct-or-union attributes[opt] identifier
1125/// struct-or-union:
1126/// 'struct'
1127/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001128void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1129 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001130 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001131 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001132 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001133 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001134 DeclSpec::TST TagType;
1135 if (TagTokKind == tok::kw_struct)
1136 TagType = DeclSpec::TST_struct;
1137 else if (TagTokKind == tok::kw___interface)
1138 TagType = DeclSpec::TST_interface;
1139 else if (TagTokKind == tok::kw_class)
1140 TagType = DeclSpec::TST_class;
1141 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001142 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1143 TagType = DeclSpec::TST_union;
1144 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001145
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001146 if (Tok.is(tok::code_completion)) {
1147 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001148 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001149 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001150 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001151
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001152 // C++03 [temp.explicit] 14.7.2/8:
1153 // The usual access checking rules do not apply to names used to specify
1154 // explicit instantiations.
1155 //
1156 // As an extension we do not perform access checking on the names used to
1157 // specify explicit specializations either. This is important to allow
1158 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001159 //
1160 // Note that we don't suppress if this turns out to be an elaborated
1161 // type specifier.
1162 bool shouldDelayDiagsInTag =
1163 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1164 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1165 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001166
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001167 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001168 // If attributes exist after tag, parse them.
Richard Smith37a45dd2013-10-24 01:21:09 +00001169 MaybeParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +00001170
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001171 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +00001172 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +00001173 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001174
John McCall8d32c052012-05-22 21:28:12 +00001175 // Parse inheritance specifiers.
1176 if (Tok.is(tok::kw___single_inheritance) ||
1177 Tok.is(tok::kw___multiple_inheritance) ||
1178 Tok.is(tok::kw___virtual_inheritance))
Richard Smith37a45dd2013-10-24 01:21:09 +00001179 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCall8d32c052012-05-22 21:28:12 +00001180
Alexis Hunt96d5c762009-11-21 08:43:09 +00001181 // If C++0x attributes exist here, parse them.
1182 // FIXME: Are we consistent with the ordering of parsing of different
1183 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001184 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001185
Michael Han309af292013-01-07 16:57:11 +00001186 // Source location used by FIXIT to insert misplaced
1187 // C++11 attributes
1188 SourceLocation AttrFixitLoc = Tok.getLocation();
1189
Alp Toker53358e42013-12-17 14:12:30 +00001190 // GNU libstdc++ and libc++ use certain intrinsic names as the
1191 // name of struct templates, but some are keywords in GCC >= 4.3
1192 // MSVC and Clang. For compatibility, convert the token to an identifier
1193 // and issue a warning diagnostic.
1194 if (TagType == DeclSpec::TST_struct && !Tok.is(tok::identifier) &&
1195 !Tok.isAnnotation()) {
1196 const IdentifierInfo *II = Tok.getIdentifierInfo();
1197 // We rarely end up here so the following check is efficient.
1198 if (II && II->getName().startswith("__is_"))
1199 TryKeywordIdentFallback(true);
1200 }
Mike Stump11289f42009-09-09 15:08:12 +00001201
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001202 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001203 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001204 if (getLangOpts().CPlusPlus) {
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001205 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1206 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001207
Douglas Gregordf593fb2011-11-07 17:33:42 +00001208 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall413021a2010-07-30 06:26:29 +00001209 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +00001210 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001211 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Alp Tokerec543272013-12-24 09:48:30 +00001212 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001213 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001214
Douglas Gregor916462b2009-10-30 21:46:58 +00001215 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1216
Douglas Gregor67a65642009-02-17 23:15:12 +00001217 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +00001218 IdentifierInfo *Name = 0;
1219 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +00001220 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +00001221 if (Tok.is(tok::identifier)) {
1222 Name = Tok.getIdentifierInfo();
1223 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001224
David Blaikiebbafb8a2012-03-11 07:00:24 +00001225 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001226 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001227 // Eat the template argument list and try to continue parsing this as
1228 // a class (or template thereof).
1229 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001230 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +00001231 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +00001232 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001233 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001234 // We couldn't parse the template argument list at all, so don't
1235 // try to give any location information for the list.
1236 LAngleLoc = RAngleLoc = SourceLocation();
1237 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001238
Douglas Gregor916462b2009-10-30 21:46:58 +00001239 Diag(NameLoc, diag::err_explicit_spec_non_template)
Alp Toker01d65e12014-01-06 12:54:41 +00001240 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1241 << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
Joao Matose9a3ed42012-08-31 22:18:20 +00001242
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001243 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001244 // we've removed its template argument list.
1245 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1246 if (TemplateParams && TemplateParams->size() > 1) {
1247 TemplateParams->pop_back();
1248 } else {
1249 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001250 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001251 = ParsedTemplateInfo::NonTemplate;
1252 }
1253 } else if (TemplateInfo.Kind
1254 == ParsedTemplateInfo::ExplicitInstantiation) {
1255 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +00001256 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001257 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001258 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001259 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001260 = SourceLocation();
1261 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1262 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001263 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001264 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001265 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001266 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001267 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001268
Douglas Gregore7c20652011-03-02 00:47:37 +00001269 if (TemplateId->Kind != TNK_Type_template &&
1270 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001271 // The template-name in the simple-template-id refers to
1272 // something other than a class template. Give an appropriate
1273 // error message and skip to the ';'.
1274 SourceRange Range(NameLoc);
1275 if (SS.isNotEmpty())
1276 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001277
Richard Smith72bfbd82013-12-04 00:28:23 +00001278 // FIXME: Name may be null here.
Douglas Gregor7f741122009-02-25 19:37:18 +00001279 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu30f93852013-06-19 22:25:01 +00001280 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001281
Douglas Gregor7f741122009-02-25 19:37:18 +00001282 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001283 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor7f741122009-02-25 19:37:18 +00001284 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001285 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001286 }
1287
Richard Smithbfdb1082012-03-12 08:56:40 +00001288 // There are four options here.
1289 // - If we are in a trailing return type, this is always just a reference,
1290 // and we must not try to parse a definition. For instance,
1291 // [] () -> struct S { };
1292 // does not define a type.
1293 // - If we have 'struct foo {...', 'struct foo :...',
1294 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1295 // - If we have 'struct foo;', then this is either a forward declaration
1296 // or a friend declaration, which have to be treated differently.
1297 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001298 //
1299 // We also detect these erroneous cases to provide better diagnostic for
1300 // C++11 attributes parsing.
1301 // - attributes follow class name:
1302 // struct foo [[]] {};
1303 // - attributes appear before or after 'final':
1304 // struct foo [[]] final [[]] {};
1305 //
Richard Smithc5b05522012-03-12 07:56:15 +00001306 // However, in type-specifier-seq's, things look like declarations but are
1307 // just references, e.g.
1308 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001309 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001310 // &T::operator struct s;
1311 // For these, DSC is DSC_type_specifier.
Michael Han9407e502012-11-26 22:54:45 +00001312
1313 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001314 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001315
John McCallfaf5fb42010-08-26 23:41:50 +00001316 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001317 if (DSC == DSC_trailing)
1318 TUK = Sema::TUK_Reference;
1319 else if (Tok.is(tok::l_brace) ||
1320 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001321 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001322 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001323 if (DS.isFriendSpecified()) {
1324 // C++ [class.friend]p2:
1325 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001326 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001327 << SourceRange(DS.getFriendSpecLoc());
1328
1329 // Skip everything up to the semicolon, so that this looks like a proper
1330 // friend class (or template thereof) declaration.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001331 SkipUntil(tok::semi, StopBeforeMatch);
John McCallfaf5fb42010-08-26 23:41:50 +00001332 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001333 } else {
1334 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001335 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001336 }
Richard Smith434516c2013-02-22 06:46:23 +00001337 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1338 NextToken().is(tok::kw_alignas))) {
Michael Han9407e502012-11-26 22:54:45 +00001339 // We can't tell if this is a definition or reference
1340 // until we skipped the 'final' and C++11 attribute specifiers.
1341 TentativeParsingAction PA(*this);
1342
1343 // Skip the 'final' keyword.
1344 ConsumeToken();
1345
1346 // Skip C++11 attribute specifiers.
1347 while (true) {
1348 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1349 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001350 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001351 break;
Richard Smith434516c2013-02-22 06:46:23 +00001352 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han9407e502012-11-26 22:54:45 +00001353 ConsumeToken();
1354 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001355 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001356 break;
1357 } else {
1358 break;
1359 }
1360 }
1361
1362 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1363 TUK = Sema::TUK_Definition;
1364 else
1365 TUK = Sema::TUK_Reference;
1366
1367 PA.Revert();
Richard Smith369b9f92012-06-25 21:37:02 +00001368 } else if (DSC != DSC_type_specifier &&
1369 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001370 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001371 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001372 if (Tok.isNot(tok::semi)) {
1373 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00001374 ExpectAndConsume(tok::semi, diag::err_expected_after,
1375 DeclSpec::getSpecifierName(TagType));
Joao Matose9a3ed42012-08-31 22:18:20 +00001376 PP.EnterToken(Tok);
1377 Tok.setKind(tok::semi);
1378 }
Richard Smith369b9f92012-06-25 21:37:02 +00001379 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001380 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001381
Michael Han9407e502012-11-26 22:54:45 +00001382 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1383 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001384 if (TUK != Sema::TUK_Reference) {
1385 // If this is not a reference, then the only possible
1386 // valid place for C++11 attributes to appear here
1387 // is between class-key and class-name. If there are
1388 // any attributes after class-name, we try a fixit to move
1389 // them to the right place.
1390 SourceRange AttrRange = Attributes.Range;
1391 if (AttrRange.isValid()) {
1392 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1393 << AttrRange
1394 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1395 CharSourceRange(AttrRange, true))
1396 << FixItHint::CreateRemoval(AttrRange);
1397
1398 // Recover by adding misplaced attributes to the attribute list
1399 // of the class so they can be applied on the class later.
1400 attrs.takeAllFrom(Attributes);
1401 }
1402 }
Michael Han9407e502012-11-26 22:54:45 +00001403
John McCall6347b682012-05-07 06:16:58 +00001404 // If this is an elaborated type specifier, and we delayed
1405 // diagnostics before, just merge them into the current pool.
1406 if (shouldDelayDiagsInTag) {
1407 diagsFromTag.done();
1408 if (TUK == Sema::TUK_Reference)
1409 diagsFromTag.redelay();
1410 }
1411
John McCall413021a2010-07-30 06:26:29 +00001412 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001413 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001414 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1415 // We have a declaration or reference to an anonymous class.
1416 Diag(StartLoc, diag::err_anon_type_definition)
1417 << DeclSpec::getSpecifierName(TagType);
1418 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001419
David Majnemer3252fd02013-12-05 01:36:53 +00001420 // If we are parsing a definition and stop at a base-clause, continue on
1421 // until the semicolon. Continuing from the comma will just trick us into
1422 // thinking we are seeing a variable declaration.
1423 if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
1424 SkipUntil(tok::semi, StopBeforeMatch);
1425 else
1426 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor556877c2008-04-13 21:30:24 +00001427 return;
1428 }
1429
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001430 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001431 DeclResult TagOrTempResult = true; // invalid
1432 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001433
Douglas Gregord6ab8742009-05-28 23:31:59 +00001434 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +00001435 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001436 // Explicit specialization, class template partial specialization,
1437 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001438 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001439 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001440 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001441 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001442 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001443 ProhibitAttributes(attrs);
1444
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001445 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001446 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001447 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001448 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001449 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001450 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001451 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001452 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001453 TemplateId->TemplateNameLoc,
1454 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001455 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001456 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001457 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001458
1459 // Friend template-ids are treated as references unless
1460 // they have template headers, in which case they're ill-formed
1461 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1462 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001463 } else if (TUK == Sema::TUK_Reference ||
1464 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001465 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001466 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001467 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001468 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001469 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001470 TemplateId->Template,
1471 TemplateId->TemplateNameLoc,
1472 TemplateId->LAngleLoc,
1473 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001474 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001475 } else {
1476 // This is an explicit specialization or a class template
1477 // partial specialization.
1478 TemplateParameterLists FakedParamLists;
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001479 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1480 // This looks like an explicit instantiation, because we have
1481 // something like
1482 //
1483 // template class Foo<X>
1484 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001485 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001486 // meant to be an explicit specialization, but the user forgot
1487 // the '<>' after 'template'.
Richard Smith003c5e12013-11-08 19:03:29 +00001488 // It this is friend declaration however, since it cannot have a
1489 // template header, it is most likely that the user meant to
1490 // remove the 'template' keyword.
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001491 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith003c5e12013-11-08 19:03:29 +00001492 "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001493
Richard Smith003c5e12013-11-08 19:03:29 +00001494 if (TUK == Sema::TUK_Friend) {
1495 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
1496 TemplateParams = 0;
1497 } else {
1498 SourceLocation LAngleLoc =
1499 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1500 Diag(TemplateId->TemplateNameLoc,
1501 diag::err_explicit_instantiation_with_definition)
1502 << SourceRange(TemplateInfo.TemplateLoc)
1503 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1504
1505 // Create a fake template parameter list that contains only
1506 // "template<>", so that we treat this construct as a class
1507 // template specialization.
1508 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1509 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1510 LAngleLoc));
1511 TemplateParams = &FakedParamLists;
1512 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001513 }
1514
1515 // Build the class template specialization.
1516 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001517 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor3c7cd6a2011-09-09 20:53:38 +00001518 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall3e56fd42010-08-23 07:28:44 +00001519 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001520 TemplateId->TemplateNameLoc,
1521 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +00001522 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001523 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001524 attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001525 MultiTemplateParamsArg(
Douglas Gregor67a65642009-02-17 23:15:12 +00001526 TemplateParams? &(*TemplateParams)[0] : 0,
1527 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001528 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001529 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001530 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001531 // Explicit instantiation of a member of a class template
1532 // specialization, e.g.,
1533 //
1534 // template struct Outer<int>::Inner;
1535 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001536 ProhibitAttributes(attrs);
1537
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001538 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001539 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001540 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001541 TemplateInfo.TemplateLoc,
1542 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001543 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001544 } else if (TUK == Sema::TUK_Friend &&
1545 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001546 ProhibitAttributes(attrs);
1547
John McCallace48cd2010-10-19 01:40:49 +00001548 TagOrTempResult =
1549 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1550 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001551 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001552 MultiTemplateParamsArg(
John McCallace48cd2010-10-19 01:40:49 +00001553 TemplateParams? &(*TemplateParams)[0] : 0,
1554 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001555 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001556 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1557 ProhibitAttributes(attrs);
Richard Smith003c5e12013-11-08 19:03:29 +00001558
Larisse Voufo725de3e2013-06-21 00:08:46 +00001559 if (TUK == Sema::TUK_Definition &&
1560 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1561 // If the declarator-id is not a template-id, issue a diagnostic and
1562 // recover by ignoring the 'template' keyword.
1563 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1564 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001565 TemplateParams = 0;
Larisse Voufo725de3e2013-06-21 00:08:46 +00001566 }
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001567
John McCall7f41d982009-09-11 04:59:25 +00001568 bool IsDependent = false;
1569
John McCall32723e92010-10-19 18:40:57 +00001570 // Don't pass down template parameter lists if this is just a tag
1571 // reference. For example, we don't need the template parameters here:
1572 // template <class T> class A *makeA(T t);
1573 MultiTemplateParamsArg TParams;
1574 if (TUK != Sema::TUK_Reference && TemplateParams)
1575 TParams =
1576 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1577
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001578 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001579 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001580 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001581 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001582 TParams, Owned, IsDependent,
1583 SourceLocation(), false,
1584 clang::TypeResult());
John McCall7f41d982009-09-11 04:59:25 +00001585
1586 // If ActOnTag said the type was dependent, try again with the
1587 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001588 if (IsDependent) {
1589 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001590 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001591 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001592 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001593 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001594
Douglas Gregor556877c2008-04-13 21:30:24 +00001595 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001596 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001597 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001598 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001599 isCXX11FinalKeyword());
David Blaikiebbafb8a2012-03-11 07:00:24 +00001600 if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001601 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1602 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001603 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001604 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001605 }
1606
John McCallba7bf592010-08-24 05:47:05 +00001607 const char *PrevSpec = 0;
1608 unsigned DiagID;
1609 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001610 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001611 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1612 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallba7bf592010-08-24 05:47:05 +00001613 PrevSpec, DiagID, TypeResult.get());
John McCall7f41d982009-09-11 04:59:25 +00001614 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001615 Result = DS.SetTypeSpecType(TagType, StartLoc,
1616 NameLoc.isValid() ? NameLoc : StartLoc,
1617 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCall7f41d982009-09-11 04:59:25 +00001618 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001619 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001620 return;
1621 }
Mike Stump11289f42009-09-09 15:08:12 +00001622
John McCallba7bf592010-08-24 05:47:05 +00001623 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001624 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001625
Chris Lattnercf251412010-02-02 01:23:29 +00001626 // At this point, we've successfully parsed a class-specifier in 'definition'
1627 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1628 // going to look at what comes after it to improve error recovery. If an
1629 // impossible token occurs next, we assume that the programmer forgot a ; at
1630 // the end of the declaration and recover that way.
1631 //
Richard Smith369b9f92012-06-25 21:37:02 +00001632 // Also enforce C++ [temp]p3:
1633 // In a template-declaration which defines a class, no declarator
1634 // is permitted.
Joao Matose9a3ed42012-08-31 22:18:20 +00001635 if (TUK == Sema::TUK_Definition &&
1636 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001637 if (Tok.isNot(tok::semi)) {
Alp Toker383d2c42014-01-01 03:08:43 +00001638 ExpectAndConsume(tok::semi, diag::err_expected_after,
1639 DeclSpec::getSpecifierName(TagType));
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001640 // Push this token back into the preprocessor and change our current token
1641 // to ';' so that the rest of the code recovers as though there were an
1642 // ';' after the definition.
1643 PP.EnterToken(Tok);
1644 Tok.setKind(tok::semi);
1645 }
Chris Lattnercf251412010-02-02 01:23:29 +00001646 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001647}
1648
Mike Stump11289f42009-09-09 15:08:12 +00001649/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001650///
1651/// base-clause : [C++ class.derived]
1652/// ':' base-specifier-list
1653/// base-specifier-list:
1654/// base-specifier '...'[opt]
1655/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001656void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001657 assert(Tok.is(tok::colon) && "Not a base clause");
1658 ConsumeToken();
1659
Douglas Gregor29a92472008-10-22 17:49:05 +00001660 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001661 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001662
Douglas Gregor556877c2008-04-13 21:30:24 +00001663 while (true) {
1664 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001665 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001666 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001667 // Skip the rest of this base specifier, up until the comma or
1668 // opening brace.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001669 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor29a92472008-10-22 17:49:05 +00001670 } else {
1671 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001672 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001673 }
1674
1675 // If the next token is a comma, consume it and keep reading
1676 // base-specifiers.
1677 if (Tok.isNot(tok::comma)) break;
Mike Stump11289f42009-09-09 15:08:12 +00001678
Douglas Gregor556877c2008-04-13 21:30:24 +00001679 // Consume the comma.
1680 ConsumeToken();
1681 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001682
1683 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001684 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001685}
1686
1687/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1688/// one entry in the base class list of a class specifier, for example:
1689/// class foo : public bar, virtual private baz {
1690/// 'public bar' and 'virtual private baz' are each base-specifiers.
1691///
1692/// base-specifier: [C++ class.derived]
Richard Smith4c96e992013-02-19 23:47:15 +00001693/// attribute-specifier-seq[opt] base-type-specifier
1694/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1695/// base-type-specifier
1696/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1697/// base-type-specifier
John McCall48871652010-08-21 09:40:31 +00001698Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001699 bool IsVirtual = false;
1700 SourceLocation StartLoc = Tok.getLocation();
1701
Richard Smith4c96e992013-02-19 23:47:15 +00001702 ParsedAttributesWithRange Attributes(AttrFactory);
1703 MaybeParseCXX11Attributes(Attributes);
1704
Douglas Gregor556877c2008-04-13 21:30:24 +00001705 // Parse the 'virtual' keyword.
1706 if (Tok.is(tok::kw_virtual)) {
1707 ConsumeToken();
1708 IsVirtual = true;
1709 }
1710
Richard Smith4c96e992013-02-19 23:47:15 +00001711 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1712
Douglas Gregor556877c2008-04-13 21:30:24 +00001713 // Parse an (optional) access specifier.
1714 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001715 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001716 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001717
Richard Smith4c96e992013-02-19 23:47:15 +00001718 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1719
Douglas Gregor556877c2008-04-13 21:30:24 +00001720 // Parse the 'virtual' keyword (again!), in case it came after the
1721 // access specifier.
1722 if (Tok.is(tok::kw_virtual)) {
1723 SourceLocation VirtualLoc = ConsumeToken();
1724 if (IsVirtual) {
1725 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001726 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001727 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001728 }
1729
1730 IsVirtual = true;
1731 }
1732
Richard Smith4c96e992013-02-19 23:47:15 +00001733 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1734
Douglas Gregor831c93f2008-11-05 20:51:48 +00001735 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001736 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00001737 SourceLocation BaseLoc;
1738 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001739 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001740 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001741
Douglas Gregor752a5952011-01-03 22:36:02 +00001742 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1743 // actually part of the base-specifier-list grammar productions, but we
1744 // parse it here for convenience.
1745 SourceLocation EllipsisLoc;
1746 if (Tok.is(tok::ellipsis))
1747 EllipsisLoc = ConsumeToken();
1748
Mike Stump11289f42009-09-09 15:08:12 +00001749 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001750 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001751
Douglas Gregor556877c2008-04-13 21:30:24 +00001752 // Notify semantic analysis that we have parsed a complete
1753 // base-specifier.
Richard Smith4c96e992013-02-19 23:47:15 +00001754 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1755 Access, BaseType.get(), BaseLoc,
1756 EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001757}
1758
1759/// getAccessSpecifierIfPresent - Determine whether the next token is
1760/// a C++ access-specifier.
1761///
1762/// access-specifier: [C++ class.derived]
1763/// 'private'
1764/// 'protected'
1765/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001766AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001767 switch (Tok.getKind()) {
1768 default: return AS_none;
1769 case tok::kw_private: return AS_private;
1770 case tok::kw_protected: return AS_protected;
1771 case tok::kw_public: return AS_public;
1772 }
1773}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001774
Douglas Gregor433e0532012-04-16 18:27:27 +00001775/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith2331bbf2012-05-02 22:22:32 +00001776/// delayed, e.g., default arguments, create a late-parsed method declaration
1777/// record to handle the parsing at the end of the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00001778void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1779 Decl *ThisDecl) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001780 // We just declared a member function. If this member function
Richard Smith2331bbf2012-05-02 22:22:32 +00001781 // has any default arguments, we'll need to parse them later.
Eli Friedman3af2a772009-07-22 21:45:50 +00001782 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001783 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001784 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor433e0532012-04-16 18:27:27 +00001785
Eli Friedman3af2a772009-07-22 21:45:50 +00001786 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1787 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1788 if (!LateMethod) {
1789 // Push this method onto the stack of late-parsed method
1790 // declarations.
Douglas Gregorefc46952010-10-12 16:25:54 +00001791 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1792 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001793 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001794
1795 // Add all of the parameters prior to this one (they don't
1796 // have default arguments).
1797 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1798 for (unsigned I = 0; I < ParamIdx; ++I)
1799 LateMethod->DefaultArgs.push_back(
Douglas Gregor1d85d292010-03-02 01:29:43 +00001800 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001801 }
1802
Douglas Gregor433e0532012-04-16 18:27:27 +00001803 // Add this parameter to the list of parameters (it may or may
Eli Friedman3af2a772009-07-22 21:45:50 +00001804 // not have a default argument).
1805 LateMethod->DefaultArgs.push_back(
1806 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1807 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1808 }
1809 }
1810}
1811
Richard Smith89645bc2013-01-02 12:01:23 +00001812/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001813/// virt-specifier.
1814///
1815/// virt-specifier:
1816/// override
1817/// final
Richard Smith89645bc2013-01-02 12:01:23 +00001818VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001819 if (!getLangOpts().CPlusPlus)
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001820 return VirtSpecifiers::VS_None;
1821
Anders Carlsson56104902011-01-17 03:05:47 +00001822 if (Tok.is(tok::identifier)) {
1823 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001824
Anders Carlsson428803b2011-01-20 03:47:08 +00001825 // Initialize the contextual keywords.
1826 if (!Ident_final) {
1827 Ident_final = &PP.getIdentifierTable().get("final");
David Majnemera5433082013-10-18 00:33:31 +00001828 if (getLangOpts().MicrosoftExt)
1829 Ident_sealed = &PP.getIdentifierTable().get("sealed");
Anders Carlsson428803b2011-01-20 03:47:08 +00001830 Ident_override = &PP.getIdentifierTable().get("override");
1831 }
1832
Anders Carlsson56104902011-01-17 03:05:47 +00001833 if (II == Ident_override)
1834 return VirtSpecifiers::VS_Override;
1835
David Majnemera5433082013-10-18 00:33:31 +00001836 if (II == Ident_sealed)
1837 return VirtSpecifiers::VS_Sealed;
1838
Anders Carlsson56104902011-01-17 03:05:47 +00001839 if (II == Ident_final)
1840 return VirtSpecifiers::VS_Final;
1841 }
1842
1843 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001844}
1845
Richard Smith89645bc2013-01-02 12:01:23 +00001846/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001847///
1848/// virt-specifier-seq:
1849/// virt-specifier
1850/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00001851void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalldb632ac2012-09-25 07:32:39 +00001852 bool IsInterface) {
Anders Carlsson56104902011-01-17 03:05:47 +00001853 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00001854 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001855 if (Specifier == VirtSpecifiers::VS_None)
1856 return;
1857
1858 // C++ [class.mem]p8:
1859 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001860 const char *PrevSpec = 0;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001861 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001862 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1863 << PrevSpec
1864 << FixItHint::CreateRemoval(Tok.getLocation());
1865
David Majnemera5433082013-10-18 00:33:31 +00001866 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
1867 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalldb632ac2012-09-25 07:32:39 +00001868 Diag(Tok.getLocation(), diag::err_override_control_interface)
1869 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemera5433082013-10-18 00:33:31 +00001870 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
1871 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
John McCalldb632ac2012-09-25 07:32:39 +00001872 } else {
David Majnemera5433082013-10-18 00:33:31 +00001873 Diag(Tok.getLocation(),
1874 getLangOpts().CPlusPlus11
1875 ? diag::warn_cxx98_compat_override_control_keyword
1876 : diag::ext_override_control_keyword)
1877 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalldb632ac2012-09-25 07:32:39 +00001878 }
Anders Carlsson56104902011-01-17 03:05:47 +00001879 ConsumeToken();
1880 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001881}
1882
Richard Smith89645bc2013-01-02 12:01:23 +00001883/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlssoncafbab72011-03-25 14:53:29 +00001884/// contextual 'final' keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00001885bool Parser::isCXX11FinalKeyword() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001886 if (!getLangOpts().CPlusPlus)
Anders Carlssoncafbab72011-03-25 14:53:29 +00001887 return false;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001888
Anders Carlssoncafbab72011-03-25 14:53:29 +00001889 if (!Tok.is(tok::identifier))
1890 return false;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001891
Anders Carlssoncafbab72011-03-25 14:53:29 +00001892 // Initialize the contextual keywords.
1893 if (!Ident_final) {
1894 Ident_final = &PP.getIdentifierTable().get("final");
David Majnemera5433082013-10-18 00:33:31 +00001895 if (getLangOpts().MicrosoftExt)
1896 Ident_sealed = &PP.getIdentifierTable().get("sealed");
Anders Carlssoncafbab72011-03-25 14:53:29 +00001897 Ident_override = &PP.getIdentifierTable().get("override");
1898 }
David Majnemera5433082013-10-18 00:33:31 +00001899
1900 return Tok.getIdentifierInfo() == Ident_final ||
1901 Tok.getIdentifierInfo() == Ident_sealed;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001902}
1903
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001904/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1905///
1906/// member-declaration:
1907/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1908/// function-definition ';'[opt]
1909/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1910/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001911/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001912/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001913/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001914///
1915/// member-declarator-list:
1916/// member-declarator
1917/// member-declarator-list ',' member-declarator
1918///
1919/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001920/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001921/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00001922/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001923/// identifier[opt] ':' constant-expression
1924///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001925/// virt-specifier-seq:
1926/// virt-specifier
1927/// virt-specifier-seq virt-specifier
1928///
1929/// virt-specifier:
1930/// override
1931/// final
David Majnemera5433082013-10-18 00:33:31 +00001932/// [MS] sealed
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001933///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001934/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001935/// '= 0'
1936///
1937/// constant-initializer:
1938/// '=' constant-expression
1939///
Douglas Gregor3447e762009-08-20 22:52:58 +00001940void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00001941 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00001942 const ParsedTemplateInfo &TemplateInfo,
1943 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00001944 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001945 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00001946 Diag(Tok, diag::err_at_defs_cxx);
1947 else
1948 Diag(Tok, diag::err_at_in_class);
Richard Smithda35e962013-11-09 04:52:51 +00001949
Douglas Gregor23c84762011-04-14 17:21:19 +00001950 ConsumeToken();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001951 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregor23c84762011-04-14 17:21:19 +00001952 return;
1953 }
Richard Smithda35e962013-11-09 04:52:51 +00001954
John McCalla0097262009-12-11 02:10:03 +00001955 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00001956 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00001957 if (!TemplateInfo.Kind &&
Richard Smith45855df2012-05-09 08:23:23 +00001958 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1959 if (TryAnnotateCXXScopeToken())
1960 MalformedTypeSpec = true;
1961
1962 bool isAccessDecl;
1963 if (Tok.isNot(tok::annot_cxxscope))
1964 isAccessDecl = false;
1965 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00001966 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1967 else
1968 isAccessDecl = NextToken().is(tok::kw_operator);
1969
1970 if (isAccessDecl) {
1971 // Collect the scope specifier token we annotated earlier.
1972 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00001973 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1974 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00001975
1976 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001977 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00001978 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001979 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1980 TemplateKWLoc, Name)) {
John McCalla0097262009-12-11 02:10:03 +00001981 SkipUntil(tok::semi);
1982 return;
1983 }
1984
1985 // TODO: recover from mistakenly-qualified operator declarations.
Alp Toker383d2c42014-01-01 03:08:43 +00001986 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
1987 "access declaration")) {
1988 SkipUntil(tok::semi);
John McCalla0097262009-12-11 02:10:03 +00001989 return;
Alp Toker383d2c42014-01-01 03:08:43 +00001990 }
John McCalla0097262009-12-11 02:10:03 +00001991
Douglas Gregor0be31a22010-07-02 17:43:08 +00001992 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001993 /* HasUsingKeyword */ false,
1994 SourceLocation(),
John McCalla0097262009-12-11 02:10:03 +00001995 SS, Name,
1996 /* AttrList */ 0,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00001997 /* HasTypenameKeyword */ false,
John McCalla0097262009-12-11 02:10:03 +00001998 SourceLocation());
1999 return;
2000 }
2001 }
2002
Anders Carlssonf24fcff62009-03-11 16:27:10 +00002003 // static_assert-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00002004 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00002005 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00002006 SourceLocation DeclEnd;
2007 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002008 return;
2009 }
Mike Stump11289f42009-09-09 15:08:12 +00002010
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002011 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00002012 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00002013 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00002014 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00002015 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002016 AS, AccessAttrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002017 return;
2018 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002019
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002020 // Handle: member-declaration ::= '__extension__' member-declaration
2021 if (Tok.is(tok::kw___extension__)) {
2022 // __extension__ silences extension warnings in the subexpression.
2023 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2024 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002025 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2026 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002027 }
Douglas Gregorfec52632009-06-20 00:51:54 +00002028
Chris Lattnercf251412010-02-02 01:23:29 +00002029 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
2030 // is a bitfield.
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002031 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002032
John McCall084e83d2011-03-24 11:26:52 +00002033 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00002034 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002035 // Optional C++11 attribute-specifier
2036 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00002037 // We need to keep these attributes for future diagnostic
2038 // before they are taken over by declaration specifier.
2039 FnAttrs.addAll(attrs.getList());
2040 FnAttrs.Range = attrs.Range;
2041
John McCall53fa7142010-12-24 02:08:15 +00002042 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002043
Douglas Gregorfec52632009-06-20 00:51:54 +00002044 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00002045 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002046
Douglas Gregorfec52632009-06-20 00:51:54 +00002047 // Eat 'using'.
2048 SourceLocation UsingLoc = ConsumeToken();
2049
2050 if (Tok.is(tok::kw_namespace)) {
2051 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002052 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002053 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00002054 SourceLocation DeclEnd;
Richard Smith3f1b5d02011-05-05 21:57:07 +00002055 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +00002056 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2057 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00002058 }
2059 return;
2060 }
2061
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002062 // Hold late-parsed attributes so we can attach a Decl to them later.
2063 LateParsedAttrList CommonLateParsedAttrs;
2064
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002065 // decl-specifier-seq:
2066 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00002067 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00002068 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00002069 if (MalformedTypeSpec)
2070 DS.SetTypeSpecError();
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002071 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2072 &CommonLateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002073
Richard Smith404dfb42013-11-19 22:47:36 +00002074 // If we had a free-standing type definition with a missing semicolon, we
2075 // may get this far before the problem becomes obvious.
2076 if (DS.hasTagDefinition() &&
2077 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2078 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2079 &CommonLateParsedAttrs))
2080 return;
2081
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002082 MultiTemplateParamsArg TemplateParams(
John McCall11083da2009-09-16 22:47:08 +00002083 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2084 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2085
Alp Toker35d87032013-12-30 23:29:50 +00002086 if (TryConsumeToken(tok::semi)) {
Michael Handdc016d2012-11-28 23:17:40 +00002087 if (DS.isFriendSpecified())
2088 ProhibitAttributes(FnAttrs);
2089
John McCall48871652010-08-21 09:40:31 +00002090 Decl *TheDecl =
Chandler Carruth7c9856d2011-05-03 18:35:10 +00002091 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCall796c2a52010-07-16 08:13:16 +00002092 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00002093 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002094 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002095
John McCall28a6aea2009-11-04 02:18:39 +00002096 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002097 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002098
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002099 // Hold late-parsed attributes so we can attach a Decl to them later.
2100 LateParsedAttrList LateParsedAttrs;
2101
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002102 SourceLocation EqualLoc;
2103 bool HasInitializer = false;
2104 ExprResult Init;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002105 if (Tok.isNot(tok::colon)) {
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002106 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2107 ColonProtectionRAIIObject X(*this);
2108
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002109 // Parse the first declarator.
2110 ParseDeclarator(DeclaratorInfo);
Richard Smith2331bbf2012-05-02 22:22:32 +00002111 // Error parsing the declarator?
Douglas Gregor92751d42008-11-17 22:58:34 +00002112 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002113 // If so, skip until the semi-colon or a }.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002114 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002115 if (Tok.is(tok::semi))
2116 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002117 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002118 }
2119
Richard Smith89645bc2013-01-02 12:01:23 +00002120 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber24b2a822011-01-28 06:07:34 +00002121
John Thompson5bc5cbe2009-11-25 22:58:06 +00002122 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002123 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson5bc5cbe2009-11-25 22:58:06 +00002124
Francois Pichet3abc9b82011-05-11 02:14:46 +00002125 // MSVC permits pure specifier on inline functions declared at class scope.
2126 // Hence check for =0 before checking for function definition.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002127 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet3abc9b82011-05-11 02:14:46 +00002128 DeclaratorInfo.isFunctionDeclarator() &&
2129 NextToken().is(tok::numeric_constant)) {
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002130 EqualLoc = ConsumeToken();
Francois Pichet3abc9b82011-05-11 02:14:46 +00002131 Init = ParseInitializer();
2132 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002133 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002134 else
2135 HasInitializer = true;
Francois Pichet3abc9b82011-05-11 02:14:46 +00002136 }
2137
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002138 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002139 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002140 //
2141 // In C++11, a non-function declarator followed by an open brace is a
2142 // braced-init-list for an in-class member initialization, not an
2143 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002144 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002145 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002146 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002147 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002148 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002149 } else if (Tok.is(tok::equal)) {
2150 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002151 if (KW.is(tok::kw_default))
2152 DefinitionKind = FDK_Defaulted;
2153 else if (KW.is(tok::kw_delete))
2154 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002155 }
2156 }
2157
Michael Handdc016d2012-11-28 23:17:40 +00002158 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2159 // to a friend declaration, that declaration shall be a definition.
2160 if (DeclaratorInfo.isFunctionDeclarator() &&
2161 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2162 // Diagnose attributes that appear before decl specifier:
2163 // [[]] friend int foo();
2164 ProhibitAttributes(FnAttrs);
2165 }
2166
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002167 if (DefinitionKind) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002168 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002169 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002170 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002171 SkipUntil(tok::r_brace);
Michael Handdc016d2012-11-28 23:17:40 +00002172
Douglas Gregor8a4db832011-01-19 16:41:58 +00002173 // Consume the optional ';'
Alp Toker35d87032013-12-30 23:29:50 +00002174 TryConsumeToken(tok::semi);
2175
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002176 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002177 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002178
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002179 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002180 Diag(DeclaratorInfo.getIdentifierLoc(),
2181 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002182
Richard Smith2603b092012-11-15 22:54:20 +00002183 // Recover by treating the 'typedef' as spurious.
2184 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002185 }
2186
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002187 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002188 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002189 VS, DefinitionKind, Init);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002190
David Majnemer23252a32013-08-01 04:22:55 +00002191 if (FunDecl) {
2192 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2193 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2194 }
2195 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2196 LateParsedAttrs[i]->addDecl(FunDecl);
2197 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002198 }
2199 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002200
2201 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002202 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002203 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002204
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002205 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002206 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002207 }
2208
2209 // member-declarator-list:
2210 // member-declarator
2211 // member-declarator-list ',' member-declarator
2212
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002213 SmallVector<Decl *, 8> DeclsInGroup;
John McCalldadc5752010-08-24 06:29:42 +00002214 ExprResult BitfieldSize;
Richard Smithc8a79032012-01-09 22:31:44 +00002215 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002216
2217 while (1) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002218 // member-declarator:
2219 // declarator pure-specifier[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00002220 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002221 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002222 if (Tok.is(tok::colon)) {
2223 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002224 BitfieldSize = ParseConstantExpression();
2225 if (BitfieldSize.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002226 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002227 }
Mike Stump11289f42009-09-09 15:08:12 +00002228
Chris Lattnerf3d3b362010-06-13 05:34:18 +00002229 // If a simple-asm-expr is present, parse it.
2230 if (Tok.is(tok::kw_asm)) {
2231 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +00002232 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnerf3d3b362010-06-13 05:34:18 +00002233 if (AsmLabel.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002234 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Chris Lattnerf3d3b362010-06-13 05:34:18 +00002235
2236 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2237 DeclaratorInfo.SetRangeEnd(Loc);
2238 }
2239
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002240 // If attributes exist after the declarator, parse them.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002241 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002242
Richard Smith938f40b2011-06-11 17:19:42 +00002243 // FIXME: When g++ adds support for this, we'll need to check whether it
2244 // goes before or after the GNU attributes and __asm__.
Richard Smith89645bc2013-01-02 12:01:23 +00002245 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith938f40b2011-06-11 17:19:42 +00002246
Richard Smith2b013182012-06-10 03:12:00 +00002247 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002248 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith938f40b2011-06-11 17:19:42 +00002249 if (BitfieldSize.get()) {
2250 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002251 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith938f40b2011-06-11 17:19:42 +00002252 } else {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002253 HasInitializer = true;
Richard Smith2b013182012-06-10 03:12:00 +00002254 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2255 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smith2b013182012-06-10 03:12:00 +00002256 != DeclSpec::SCS_typedef)
2257 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002258 }
2259 }
2260
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002261 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002262 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002263 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002264
Rafael Espindola0a67e2f2013-01-08 20:44:06 +00002265 NamedDecl *ThisDecl = 0;
John McCall07e91c02009-08-06 02:15:43 +00002266 if (DS.isFriendSpecified()) {
Michael Handdc016d2012-11-28 23:17:40 +00002267 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2268 // to a friend declaration, that declaration shall be a definition.
2269 //
2270 // Diagnose attributes appear after friend member function declarator:
2271 // foo [[]] ();
2272 SmallVector<SourceRange, 4> Ranges;
2273 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2274 if (!Ranges.empty()) {
Craig Topper2341c0d2013-07-04 03:08:24 +00002275 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
Michael Handdc016d2012-11-28 23:17:40 +00002276 E = Ranges.end(); I != E; ++I) {
2277 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2278 << *I;
2279 }
2280 }
2281
John McCall2f212b32009-09-11 21:02:39 +00002282 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor0be31a22010-07-02 17:43:08 +00002283 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002284 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002285 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002286 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002287 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002288 TemplateParams,
John McCall07e91c02009-08-06 02:15:43 +00002289 BitfieldSize.release(),
Richard Smith2b013182012-06-10 03:12:00 +00002290 VS, HasInClassInit);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002291
2292 if (VarTemplateDecl *VT =
2293 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : 0)
2294 // Re-direct this decl to refer to the templated decl so that we can
2295 // initialize it.
2296 ThisDecl = VT->getTemplatedDecl();
2297
David Majnemer23252a32013-08-01 04:22:55 +00002298 if (ThisDecl && AccessAttrs)
Richard Smithf8a75c32013-08-29 00:47:48 +00002299 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00002300 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002301
Douglas Gregor728d00b2011-10-10 14:49:18 +00002302 // Handle the initializer.
David Blaikie35506f82013-01-30 01:22:18 +00002303 if (HasInClassInit != ICIS_NoInit &&
2304 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2305 DeclSpec::SCS_static) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002306 // The initializer was deferred; parse it and cache the tokens.
David Majnemer23252a32013-08-01 04:22:55 +00002307 Diag(Tok, getLangOpts().CPlusPlus11
2308 ? diag::warn_cxx98_compat_nonstatic_member_init
2309 : diag::ext_nonstatic_member_init);
Richard Smith5d164bc2011-10-15 05:09:34 +00002310
Richard Smith938f40b2011-06-11 17:19:42 +00002311 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002312 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2313 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002314 //
2315 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002316 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002317 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002318 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemer23252a32013-08-01 04:22:55 +00002319
2320 // Avoid later warnings about a class member of incomplete type.
David Blaikiecdd91db2012-02-14 09:00:46 +00002321 if (ThisDecl)
David Blaikiecdd91db2012-02-14 09:00:46 +00002322 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002323 } else
2324 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002325 } else if (HasInitializer) {
2326 // Normal initializer.
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002327 if (!Init.isUsable())
David Majnemer23252a32013-08-01 04:22:55 +00002328 Init = ParseCXXMemberInitializer(
2329 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2330
Douglas Gregor728d00b2011-10-10 14:49:18 +00002331 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002332 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002333 else if (ThisDecl)
Sebastian Redleef474c2012-02-22 10:50:08 +00002334 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smith74aeef52013-04-26 16:15:35 +00002335 DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002336 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor728d00b2011-10-10 14:49:18 +00002337 // No initializer.
Richard Smith74aeef52013-04-26 16:15:35 +00002338 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002339
Douglas Gregor728d00b2011-10-10 14:49:18 +00002340 if (ThisDecl) {
David Majnemer23252a32013-08-01 04:22:55 +00002341 if (!ThisDecl->isInvalidDecl()) {
2342 // Set the Decl for any late parsed attributes
2343 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2344 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2345
2346 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2347 LateParsedAttrs[i]->addDecl(ThisDecl);
2348 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002349 Actions.FinalizeDeclaration(ThisDecl);
2350 DeclsInGroup.push_back(ThisDecl);
David Majnemer23252a32013-08-01 04:22:55 +00002351
2352 if (DeclaratorInfo.isFunctionDeclarator() &&
2353 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2354 DeclSpec::SCS_typedef)
2355 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002356 }
David Majnemer23252a32013-08-01 04:22:55 +00002357 LateParsedAttrs.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002358
2359 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002360
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002361 // If we don't have a comma, it is either the end of the list (a ';')
2362 // or an error, bail out.
Alp Toker094e5212014-01-05 03:27:11 +00002363 SourceLocation CommaLoc;
2364 if (!TryConsumeToken(tok::comma, CommaLoc))
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002365 break;
Mike Stump11289f42009-09-09 15:08:12 +00002366
Richard Smithc8a79032012-01-09 22:31:44 +00002367 if (Tok.isAtStartOfLine() &&
2368 !MightBeDeclarator(Declarator::MemberContext)) {
2369 // This comma was followed by a line-break and something which can't be
2370 // the start of a declarator. The comma was probably a typo for a
2371 // semicolon.
2372 Diag(CommaLoc, diag::err_expected_semi_declaration)
2373 << FixItHint::CreateReplacement(CommaLoc, ";");
2374 ExpectSemi = false;
2375 break;
2376 }
Mike Stump11289f42009-09-09 15:08:12 +00002377
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002378 // Parse the next declarator.
2379 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002380 VS.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002381 BitfieldSize = true;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002382 Init = true;
2383 HasInitializer = false;
Richard Smith8d06f422012-01-12 23:53:29 +00002384 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002385
Bill Wendling44426052012-12-20 19:22:21 +00002386 // Attributes are only allowed on the second declarator.
John McCall53fa7142010-12-24 02:08:15 +00002387 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002388
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002389 if (Tok.isNot(tok::colon))
2390 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002391 }
2392
Richard Smithc8a79032012-01-09 22:31:44 +00002393 if (ExpectSemi &&
2394 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002395 // Skip to end of block or statement.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002396 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002397 // If we stopped at a ';', eat it.
Alp Toker35d87032013-12-30 23:29:50 +00002398 TryConsumeToken(tok::semi);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002399 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002400 }
2401
Rafael Espindolaab417692013-07-09 12:05:01 +00002402 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002403}
2404
Richard Smith938f40b2011-06-11 17:19:42 +00002405/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2406/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2407/// function definition. The location of the '=', if any, will be placed in
2408/// EqualLoc.
2409///
2410/// pure-specifier:
2411/// '= 0'
Sebastian Redleef474c2012-02-22 10:50:08 +00002412///
Richard Smith938f40b2011-06-11 17:19:42 +00002413/// brace-or-equal-initializer:
2414/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002415/// braced-init-list
2416///
Richard Smith938f40b2011-06-11 17:19:42 +00002417/// initializer-clause:
2418/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002419/// braced-init-list
2420///
Richard Smithda35e962013-11-09 04:52:51 +00002421/// defaulted/deleted function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002422/// '=' 'default'
2423/// '=' 'delete'
2424///
2425/// Prior to C++0x, the assignment-expression in an initializer-clause must
2426/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002427ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002428 SourceLocation &EqualLoc) {
2429 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2430 && "Data member initializer not starting with '=' or '{'");
2431
Douglas Gregor926410d2012-02-21 02:22:07 +00002432 EnterExpressionEvaluationContext Context(Actions,
2433 Sema::PotentiallyEvaluated,
2434 D);
Alp Toker094e5212014-01-05 03:27:11 +00002435 if (TryConsumeToken(tok::equal, EqualLoc)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002436 if (Tok.is(tok::kw_delete)) {
2437 // In principle, an initializer of '= delete p;' is legal, but it will
2438 // never type-check. It's better to diagnose it as an ill-formed expression
2439 // than as an ill-formed deleted non-function member.
2440 // An initializer of '= delete p, foo' will never be parsed, because
2441 // a top-level comma always ends the initializer expression.
2442 const Token &Next = NextToken();
2443 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
Richard Smith34f30512013-11-23 04:06:09 +00002444 Next.is(tok::eof)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002445 if (IsFunction)
2446 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2447 << 1 /* delete */;
2448 else
2449 Diag(ConsumeToken(), diag::err_deleted_non_function);
2450 return ExprResult();
2451 }
2452 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002453 if (IsFunction)
2454 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2455 << 0 /* default */;
2456 else
2457 Diag(ConsumeToken(), diag::err_default_special_members);
2458 return ExprResult();
2459 }
2460
Sebastian Redleef474c2012-02-22 10:50:08 +00002461 }
2462 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002463}
2464
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002465/// ParseCXXMemberSpecification - Parse the class definition.
2466///
2467/// member-specification:
2468/// member-declaration member-specification[opt]
2469/// access-specifier ':' member-specification[opt]
2470///
Joao Matose9a3ed42012-08-31 22:18:20 +00002471void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00002472 SourceLocation AttrFixitLoc,
Richard Smith4c96e992013-02-19 23:47:15 +00002473 ParsedAttributesWithRange &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00002474 unsigned TagType, Decl *TagDecl) {
2475 assert((TagType == DeclSpec::TST_struct ||
2476 TagType == DeclSpec::TST_interface ||
2477 TagType == DeclSpec::TST_union ||
2478 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2479
John McCallfaf5fb42010-08-26 23:41:50 +00002480 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2481 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00002482
Douglas Gregoredf8f392010-01-16 20:52:59 +00002483 // Determine whether this is a non-nested class. Note that local
2484 // classes are *not* considered to be nested classes.
2485 bool NonNestedClass = true;
2486 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002487 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002488 if (S->isClassScope()) {
2489 // We're inside a class scope, so this is a nested class.
2490 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00002491
2492 // The Microsoft extension __interface does not permit nested classes.
2493 if (getCurrentClass().IsInterface) {
2494 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2495 << /*ErrorType=*/6
2496 << (isa<NamedDecl>(TagDecl)
2497 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2498 : "<anonymous>");
2499 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00002500 break;
2501 }
2502
2503 if ((S->getFlags() & Scope::FnScope)) {
2504 // If we're in a function or function template declared in the
2505 // body of a class, then this is a local class rather than a
2506 // nested class.
2507 const Scope *Parent = S->getParent();
2508 if (Parent->isTemplateParamScope())
2509 Parent = Parent->getParent();
2510 if (Parent->isClassScope())
2511 break;
2512 }
2513 }
2514 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002515
2516 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00002517 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002518
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002519 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00002520 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2521 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002522
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002523 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002524 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002525
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002526 SourceLocation FinalLoc;
David Majnemera5433082013-10-18 00:33:31 +00002527 bool IsFinalSpelledSealed = false;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002528
2529 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002530 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemera5433082013-10-18 00:33:31 +00002531 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2532 assert((Specifier == VirtSpecifiers::VS_Final ||
2533 Specifier == VirtSpecifiers::VS_Sealed) &&
2534 "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00002535 FinalLoc = ConsumeToken();
David Majnemera5433082013-10-18 00:33:31 +00002536 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002537
David Majnemera5433082013-10-18 00:33:31 +00002538 if (TagType == DeclSpec::TST_interface)
John McCalldb632ac2012-09-25 07:32:39 +00002539 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemera5433082013-10-18 00:33:31 +00002540 << VirtSpecifiers::getSpecifierName(Specifier);
2541 else if (Specifier == VirtSpecifiers::VS_Final)
2542 Diag(FinalLoc, getLangOpts().CPlusPlus11
2543 ? diag::warn_cxx98_compat_override_control_keyword
2544 : diag::ext_override_control_keyword)
2545 << VirtSpecifiers::getSpecifierName(Specifier);
2546 else if (Specifier == VirtSpecifiers::VS_Sealed)
2547 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Michael Han9407e502012-11-26 22:54:45 +00002548
Michael Han309af292013-01-07 16:57:11 +00002549 // Parse any C++11 attributes after 'final' keyword.
2550 // These attributes are not allowed to appear here,
2551 // and the only possible place for them to appertain
2552 // to the class would be between class-key and class-name.
Richard Smith4c96e992013-02-19 23:47:15 +00002553 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002554 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002555
John McCall2d814c32009-12-19 21:48:58 +00002556 if (Tok.is(tok::colon)) {
2557 ParseBaseClause(TagDecl);
2558
2559 if (!Tok.is(tok::l_brace)) {
2560 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCall2ff380a2010-03-17 00:38:33 +00002561
2562 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002563 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002564 return;
2565 }
2566 }
2567
2568 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002569 BalancedDelimiterTracker T(*this, tok::l_brace);
2570 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00002571
John McCall08bede42010-05-28 08:11:17 +00002572 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00002573 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemera5433082013-10-18 00:33:31 +00002574 IsFinalSpelledSealed,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002575 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00002576
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002577 // C++ 11p3: Members of a class defined with the keyword class are private
2578 // by default. Members of a class defined with the keywords struct or union
2579 // are public by default.
2580 AccessSpecifier CurAS;
2581 if (TagType == DeclSpec::TST_class)
2582 CurAS = AS_private;
2583 else
2584 CurAS = AS_public;
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002585 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002586
Douglas Gregor9377c822010-06-21 22:31:09 +00002587 if (TagDecl) {
2588 // While we still have something to read, read the member-declarations.
Richard Smith34f30512013-11-23 04:06:09 +00002589 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Douglas Gregor9377c822010-06-21 22:31:09 +00002590 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002591
David Blaikiebbafb8a2012-03-11 07:00:24 +00002592 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet8f981d52011-05-25 10:19:49 +00002593 Tok.is(tok::kw___if_not_exists))) {
2594 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2595 continue;
2596 }
2597
Douglas Gregor9377c822010-06-21 22:31:09 +00002598 // Check for extraneous top-level semicolon.
2599 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002600 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor9377c822010-06-21 22:31:09 +00002601 continue;
2602 }
2603
Eli Friedmanec52f922012-02-23 23:47:16 +00002604 if (Tok.is(tok::annot_pragma_vis)) {
2605 HandlePragmaVisibility();
2606 continue;
2607 }
2608
2609 if (Tok.is(tok::annot_pragma_pack)) {
2610 HandlePragmaPack();
2611 continue;
2612 }
2613
Argyrios Kyrtzidis5c2021b2012-10-12 17:39:59 +00002614 if (Tok.is(tok::annot_pragma_align)) {
2615 HandlePragmaAlign();
2616 continue;
2617 }
2618
Alexey Bataeva769e072013-03-22 06:34:35 +00002619 if (Tok.is(tok::annot_pragma_openmp)) {
2620 ParseOpenMPDeclarativeDirective();
2621 continue;
2622 }
2623
Richard Smithda35e962013-11-09 04:52:51 +00002624 // If we see a namespace here, a close brace was missing somewhere.
2625 if (Tok.is(tok::kw_namespace)) {
Richard Smith2ac43ad2013-11-15 23:00:02 +00002626 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
Richard Smithda35e962013-11-09 04:52:51 +00002627 break;
2628 }
2629
Douglas Gregor9377c822010-06-21 22:31:09 +00002630 AccessSpecifier AS = getAccessSpecifierIfPresent();
2631 if (AS != AS_none) {
2632 // Current token is a C++ access specifier.
2633 CurAS = AS;
2634 SourceLocation ASLoc = Tok.getLocation();
David Blaikieeba32c22011-10-13 06:08:43 +00002635 unsigned TokLength = Tok.getLength();
Douglas Gregor9377c822010-06-21 22:31:09 +00002636 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002637 AccessAttrs.clear();
2638 MaybeParseGNUAttributes(AccessAttrs);
2639
David Blaikieeba32c22011-10-13 06:08:43 +00002640 SourceLocation EndLoc;
Alp Toker35d87032013-12-30 23:29:50 +00002641 if (TryConsumeToken(tok::colon, EndLoc)) {
2642 } else if (TryConsumeToken(tok::semi, EndLoc)) {
2643 Diag(EndLoc, diag::err_expected)
2644 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002645 } else {
2646 EndLoc = ASLoc.getLocWithOffset(TokLength);
Alp Toker35d87032013-12-30 23:29:50 +00002647 Diag(EndLoc, diag::err_expected)
2648 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002649 }
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002650
John McCalldb632ac2012-09-25 07:32:39 +00002651 // The Microsoft extension __interface does not permit non-public
2652 // access specifiers.
2653 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2654 Diag(ASLoc, diag::err_access_specifier_interface)
2655 << (CurAS == AS_protected);
2656 }
2657
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002658 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2659 AccessAttrs.getList())) {
2660 // found another attribute than only annotations
2661 AccessAttrs.clear();
2662 }
2663
Douglas Gregor9377c822010-06-21 22:31:09 +00002664 continue;
2665 }
2666
Douglas Gregor9377c822010-06-21 22:31:09 +00002667 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002668 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002669 }
2670
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002671 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00002672 } else {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002673 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002674 }
Mike Stump11289f42009-09-09 15:08:12 +00002675
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002676 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002677 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002678 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002679
John McCall08bede42010-05-28 08:11:17 +00002680 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002681 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002682 T.getOpenLocation(),
2683 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002684 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002685
Douglas Gregor433e0532012-04-16 18:27:27 +00002686 // C++11 [class.mem]p2:
2687 // Within the class member-specification, the class is regarded as complete
Richard Smith2331bbf2012-05-02 22:22:32 +00002688 // within function bodies, default arguments, and
Douglas Gregor433e0532012-04-16 18:27:27 +00002689 // brace-or-equal-initializers for non-static data members (including such
2690 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00002691 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002692 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00002693 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002694 // declarations and the lexed inline method definitions, along with any
2695 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00002696 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002697 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002698 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00002699
2700 // We've finished with all pending member declarations.
2701 Actions.ActOnFinishCXXMemberDecls();
2702
Richard Smith938f40b2011-06-11 17:19:42 +00002703 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002704 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00002705 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002706 }
2707
John McCall08bede42010-05-28 08:11:17 +00002708 if (TagDecl)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002709 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2710 T.getCloseLocation());
John McCall2ff380a2010-03-17 00:38:33 +00002711
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002712 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002713 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002714 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002715}
Douglas Gregore8381c02008-11-05 04:29:56 +00002716
Richard Smith2ac43ad2013-11-15 23:00:02 +00002717void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithda35e962013-11-09 04:52:51 +00002718 assert(Tok.is(tok::kw_namespace));
2719
2720 // FIXME: Suggest where the close brace should have gone by looking
2721 // at indentation changes within the definition body.
Richard Smith2ac43ad2013-11-15 23:00:02 +00002722 Diag(D->getLocation(),
2723 diag::err_missing_end_of_definition) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002724 Diag(Tok.getLocation(),
Richard Smith2ac43ad2013-11-15 23:00:02 +00002725 diag::note_missing_end_of_definition_before) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002726
2727 // Push '};' onto the token stream to recover.
2728 PP.EnterToken(Tok);
2729
2730 Tok.startToken();
2731 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
2732 Tok.setKind(tok::semi);
2733 PP.EnterToken(Tok);
2734
2735 Tok.setKind(tok::r_brace);
2736}
2737
Douglas Gregore8381c02008-11-05 04:29:56 +00002738/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2739/// which explicitly initializes the members or base classes of a
2740/// class (C++ [class.base.init]). For example, the three initializers
2741/// after the ':' in the Derived constructor below:
2742///
2743/// @code
2744/// class Base { };
2745/// class Derived : Base {
2746/// int x;
2747/// float f;
2748/// public:
2749/// Derived(float f) : Base(), x(17), f(f) { }
2750/// };
2751/// @endcode
2752///
Mike Stump11289f42009-09-09 15:08:12 +00002753/// [C++] ctor-initializer:
2754/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00002755///
Mike Stump11289f42009-09-09 15:08:12 +00002756/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00002757/// mem-initializer ...[opt]
2758/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00002759void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00002760 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2761
John Wiegley1c0675e2011-04-28 01:08:34 +00002762 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2763 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00002764 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002765
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002766 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002767 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002768
Douglas Gregore8381c02008-11-05 04:29:56 +00002769 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002770 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00002771 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2772 MemInitializers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002773 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002774 } else {
2775 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2776 if (!MemInit.isInvalid())
2777 MemInitializers.push_back(MemInit.get());
2778 else
2779 AnyErrors = true;
2780 }
2781
Douglas Gregore8381c02008-11-05 04:29:56 +00002782 if (Tok.is(tok::comma))
2783 ConsumeToken();
2784 else if (Tok.is(tok::l_brace))
2785 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00002786 // If the next token looks like a base or member initializer, assume that
2787 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00002788 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2789 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2790 Diag(Loc, diag::err_ctor_init_missing_comma)
2791 << FixItHint::CreateInsertion(Loc, ", ");
2792 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00002793 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alp Tokerec543272013-12-24 09:48:30 +00002794 Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
2795 << tok::comma;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002796 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregore8381c02008-11-05 04:29:56 +00002797 break;
2798 }
2799 } while (true);
2800
David Blaikie3fc2f912013-01-17 05:26:25 +00002801 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002802 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00002803}
2804
2805/// ParseMemInitializer - Parse a C++ member initializer, which is
2806/// part of a constructor initializer that explicitly initializes one
2807/// member or base class (C++ [class.base.init]). See
2808/// ParseConstructorInitializer for an example.
2809///
2810/// [C++] mem-initializer:
2811/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002812/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00002813///
Douglas Gregore8381c02008-11-05 04:29:56 +00002814/// [C++] mem-initializer-id:
2815/// '::'[opt] nested-name-specifier[opt] class-name
2816/// identifier
John McCall48871652010-08-21 09:40:31 +00002817Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00002818 // parse '::'[opt] nested-name-specifier[opt]
2819 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002820 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00002821 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002822 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002823 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00002824 if (TemplateId->Kind == TNK_Type_template ||
2825 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00002826 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002827 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00002828 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002829 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002830 }
David Blaikie186a8892012-01-24 06:03:59 +00002831 // Uses of decltype will already have been converted to annot_decltype by
2832 // ParseOptionalCXXScopeSpecifier at this point.
2833 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2834 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002835 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00002836 return true;
2837 }
Mike Stump11289f42009-09-09 15:08:12 +00002838
David Blaikie186a8892012-01-24 06:03:59 +00002839 IdentifierInfo *II = 0;
2840 DeclSpec DS(AttrFactory);
2841 SourceLocation IdLoc = Tok.getLocation();
2842 if (Tok.is(tok::annot_decltype)) {
2843 // Get the decltype expression, if there is one.
2844 ParseDecltypeSpecifier(DS);
2845 } else {
2846 if (Tok.is(tok::identifier))
2847 // Get the identifier. This may be a member name or a class name,
2848 // but we'll let the semantic analysis determine which it is.
2849 II = Tok.getIdentifierInfo();
2850 ConsumeToken();
2851 }
2852
Douglas Gregore8381c02008-11-05 04:29:56 +00002853
2854 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002855 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002856 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2857
Sebastian Redla74948d2011-09-24 17:48:25 +00002858 ExprResult InitList = ParseBraceInitializer();
2859 if (InitList.isInvalid())
2860 return true;
2861
2862 SourceLocation EllipsisLoc;
Alp Toker094e5212014-01-05 03:27:11 +00002863 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redla74948d2011-09-24 17:48:25 +00002864
2865 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002866 TemplateTypeTy, DS, IdLoc,
2867 InitList.take(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00002868 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002869 BalancedDelimiterTracker T(*this, tok::l_paren);
2870 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00002871
Sebastian Redl3da34892011-06-05 12:23:16 +00002872 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00002873 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00002874 CommaLocsTy CommaLocs;
2875 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002876 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00002877 return true;
2878 }
2879
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002880 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00002881
2882 SourceLocation EllipsisLoc;
2883 if (Tok.is(tok::ellipsis))
2884 EllipsisLoc = ConsumeToken();
2885
2886 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002887 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenko139474d2013-05-09 23:51:52 +00002888 T.getOpenLocation(), ArgExprs,
2889 T.getCloseLocation(), EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00002890 }
2891
Alp Tokerec543272013-12-24 09:48:30 +00002892 if (getLangOpts().CPlusPlus11)
2893 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
2894 else
2895 return Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregore8381c02008-11-05 04:29:56 +00002896}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002897
Sebastian Redl965b0e32011-03-05 14:45:16 +00002898/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002899///
Douglas Gregor356513d2008-12-01 18:00:20 +00002900/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00002901/// dynamic-exception-specification
2902/// noexcept-specification
2903///
2904/// noexcept-specification:
2905/// 'noexcept'
2906/// 'noexcept' '(' constant-expression ')'
2907ExceptionSpecificationType
Richard Smith2331bbf2012-05-02 22:22:32 +00002908Parser::tryParseExceptionSpecification(
Douglas Gregor433e0532012-04-16 18:27:27 +00002909 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002910 SmallVectorImpl<ParsedType> &DynamicExceptions,
2911 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00002912 ExprResult &NoexceptExpr) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002913 ExceptionSpecificationType Result = EST_None;
2914
2915 // See if there's a dynamic specification.
2916 if (Tok.is(tok::kw_throw)) {
2917 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2918 DynamicExceptions,
2919 DynamicExceptionRanges);
2920 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2921 "Produced different number of exception types and ranges.");
2922 }
2923
2924 // If there's no noexcept specification, we're done.
2925 if (Tok.isNot(tok::kw_noexcept))
2926 return Result;
2927
Richard Smithb15c11c2011-10-17 23:06:20 +00002928 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2929
Sebastian Redl965b0e32011-03-05 14:45:16 +00002930 // If we already had a dynamic specification, parse the noexcept for,
2931 // recovery, but emit a diagnostic and don't store the results.
2932 SourceRange NoexceptRange;
2933 ExceptionSpecificationType NoexceptType = EST_None;
2934
2935 SourceLocation KeywordLoc = ConsumeToken();
2936 if (Tok.is(tok::l_paren)) {
2937 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002938 BalancedDelimiterTracker T(*this, tok::l_paren);
2939 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00002940 NoexceptType = EST_ComputedNoexcept;
2941 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002942 // The argument must be contextually convertible to bool. We use
2943 // ActOnBooleanCondition for this purpose.
2944 if (!NoexceptExpr.isInvalid())
2945 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2946 NoexceptExpr.get());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002947 T.consumeClose();
2948 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl965b0e32011-03-05 14:45:16 +00002949 } else {
2950 // There is no argument.
2951 NoexceptType = EST_BasicNoexcept;
2952 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2953 }
2954
2955 if (Result == EST_None) {
2956 SpecificationRange = NoexceptRange;
2957 Result = NoexceptType;
2958
2959 // If there's a dynamic specification after a noexcept specification,
2960 // parse that and ignore the results.
2961 if (Tok.is(tok::kw_throw)) {
2962 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2963 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2964 DynamicExceptionRanges);
2965 }
2966 } else {
2967 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2968 }
2969
2970 return Result;
2971}
2972
Richard Smith8ca78a12013-06-13 02:02:51 +00002973static void diagnoseDynamicExceptionSpecification(
2974 Parser &P, const SourceRange &Range, bool IsNoexcept) {
2975 if (P.getLangOpts().CPlusPlus11) {
2976 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
2977 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
2978 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
2979 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
2980 }
2981}
2982
Sebastian Redl965b0e32011-03-05 14:45:16 +00002983/// ParseDynamicExceptionSpecification - Parse a C++
2984/// dynamic-exception-specification (C++ [except.spec]).
2985///
2986/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00002987/// 'throw' '(' type-id-list [opt] ')'
2988/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00002989///
Douglas Gregor356513d2008-12-01 18:00:20 +00002990/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00002991/// type-id ... [opt]
2992/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002993///
Sebastian Redl965b0e32011-03-05 14:45:16 +00002994ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2995 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002996 SmallVectorImpl<ParsedType> &Exceptions,
2997 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002998 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00002999
Sebastian Redl965b0e32011-03-05 14:45:16 +00003000 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003001 BalancedDelimiterTracker T(*this, tok::l_paren);
3002 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003003 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3004 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003005 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003006 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003007
Douglas Gregor356513d2008-12-01 18:00:20 +00003008 // Parse throw(...), a Microsoft extension that means "this function
3009 // can throw anything".
3010 if (Tok.is(tok::ellipsis)) {
3011 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003012 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00003013 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003014 T.consumeClose();
3015 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003016 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003017 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00003018 }
3019
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003020 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00003021 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003022 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00003023 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00003024
Douglas Gregor830837d2010-12-20 23:57:46 +00003025 if (Tok.is(tok::ellipsis)) {
3026 // C++0x [temp.variadic]p5:
3027 // - In a dynamic-exception-specification (15.4); the pattern is a
3028 // type-id.
3029 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003030 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00003031 if (!Res.isInvalid())
3032 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3033 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003034
Sebastian Redld6434562009-05-29 18:02:33 +00003035 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003036 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00003037 Ranges.push_back(Range);
3038 }
Douglas Gregor830837d2010-12-20 23:57:46 +00003039
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003040 if (Tok.is(tok::comma))
3041 ConsumeToken();
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003042 else
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003043 break;
3044 }
3045
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003046 T.consumeClose();
3047 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003048 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3049 Exceptions.empty());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003050 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003051}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003052
Douglas Gregor7fb25412010-10-01 18:44:50 +00003053/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3054/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003055TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00003056 assert(Tok.is(tok::arrow) && "expected arrow");
3057
3058 ConsumeToken();
3059
Richard Smithbfdb1082012-03-12 08:56:40 +00003060 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00003061}
3062
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003063/// \brief We have just started parsing the definition of a new class,
3064/// so push that class onto our stack of classes that is currently
3065/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00003066Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00003067Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3068 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003069 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003070 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00003071 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00003072 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003073}
3074
3075/// \brief Deallocate the given parsed class and all of its nested
3076/// classes.
3077void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00003078 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3079 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003080 delete Class;
3081}
3082
3083/// \brief Pop the top class of the stack of classes that are
3084/// currently being parsed.
3085///
3086/// This routine should be called when we have finished parsing the
3087/// definition of a class, but have not yet popped the Scope
3088/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00003089void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003090 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00003091
John McCallc1465822011-02-14 07:13:47 +00003092 Actions.PopParsingClass(state);
3093
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003094 ParsingClass *Victim = ClassStack.top();
3095 ClassStack.pop();
3096 if (Victim->TopLevelClass) {
3097 // Deallocate all of the nested classes of this class,
3098 // recursively: we don't need to keep any of this information.
3099 DeallocateParsedClasses(Victim);
3100 return;
Mike Stump11289f42009-09-09 15:08:12 +00003101 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003102 assert(!ClassStack.empty() && "Missing top-level class?");
3103
Douglas Gregorefc46952010-10-12 16:25:54 +00003104 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003105 // The victim is a nested class, but we will not need to perform
3106 // any processing after the definition of this class since it has
3107 // no members whose handling was delayed. Therefore, we can just
3108 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00003109 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003110 return;
3111 }
3112
3113 // This nested class has some members that will need to be processed
3114 // after the top-level class is completely defined. Therefore, add
3115 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003116 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00003117 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00003118 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003119}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003120
Richard Smith3dff2512012-04-10 03:25:07 +00003121/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3122///
3123/// \return the parsed identifier on success, and 0 if the next token is not an
3124/// attribute-token.
3125///
3126/// C++11 [dcl.attr.grammar]p3:
3127/// If a keyword or an alternative token that satisfies the syntactic
3128/// requirements of an identifier is contained in an attribute-token,
3129/// it is considered an identifier.
3130IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3131 switch (Tok.getKind()) {
3132 default:
3133 // Identifiers and keywords have identifier info attached.
3134 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3135 Loc = ConsumeToken();
3136 return II;
3137 }
3138 return 0;
3139
3140 case tok::ampamp: // 'and'
3141 case tok::pipe: // 'bitor'
3142 case tok::pipepipe: // 'or'
3143 case tok::caret: // 'xor'
3144 case tok::tilde: // 'compl'
3145 case tok::amp: // 'bitand'
3146 case tok::ampequal: // 'and_eq'
3147 case tok::pipeequal: // 'or_eq'
3148 case tok::caretequal: // 'xor_eq'
3149 case tok::exclaim: // 'not'
3150 case tok::exclaimequal: // 'not_eq'
3151 // Alternative tokens do not have identifier info, but their spelling
3152 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003153 SmallString<8> SpellingBuf;
Richard Smith3dff2512012-04-10 03:25:07 +00003154 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rosea7d03842013-02-08 22:30:41 +00003155 if (isLetter(Spelling[0])) {
Richard Smith3dff2512012-04-10 03:25:07 +00003156 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003157 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003158 }
3159 return 0;
3160 }
3161}
3162
Michael Han23214e52012-10-03 01:56:22 +00003163static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3164 IdentifierInfo *ScopeName) {
3165 switch (AttributeList::getKind(AttrName, ScopeName,
3166 AttributeList::AS_CXX11)) {
3167 case AttributeList::AT_CarriesDependency:
3168 case AttributeList::AT_FallThrough:
Richard Smith10876ef2013-01-17 01:30:42 +00003169 case AttributeList::AT_CXX11NoReturn: {
Michael Han23214e52012-10-03 01:56:22 +00003170 return true;
3171 }
3172
3173 default:
3174 return false;
3175 }
3176}
3177
Richard Smith3dff2512012-04-10 03:25:07 +00003178/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003179/// only parses standard attributes.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003180///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003181/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003182/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003183/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003184///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003185/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003186/// attribute[opt]
3187/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003188/// attribute '...'
3189/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003190///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003191/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003192/// attribute-token attribute-argument-clause[opt]
3193///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003194/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003195/// identifier
3196/// attribute-scoped-token
3197///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003198/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003199/// attribute-namespace '::' identifier
3200///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003201/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003202/// identifier
3203///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003204/// [C++11] attribute-argument-clause:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003205/// '(' balanced-token-seq ')'
3206///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003207/// [C++11] balanced-token-seq:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003208/// balanced-token
3209/// balanced-token-seq balanced-token
3210///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003211/// [C++11] balanced-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003212/// '(' balanced-token-seq ')'
3213/// '[' balanced-token-seq ']'
3214/// '{' balanced-token-seq '}'
3215/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smith3dff2512012-04-10 03:25:07 +00003216void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003217 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003218 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003219 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003220 ParseAlignmentSpecifier(attrs, endLoc);
3221 return;
3222 }
3223
Alexis Hunt96d5c762009-11-21 08:43:09 +00003224 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003225 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003226
Richard Smithf679b5b2011-10-14 20:48:27 +00003227 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3228
Alexis Hunt96d5c762009-11-21 08:43:09 +00003229 ConsumeBracket();
3230 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003231
Richard Smith10876ef2013-01-17 01:30:42 +00003232 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3233
Richard Smith3dff2512012-04-10 03:25:07 +00003234 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003235 // attribute not present
3236 if (Tok.is(tok::comma)) {
3237 ConsumeToken();
3238 continue;
3239 }
3240
Richard Smith3dff2512012-04-10 03:25:07 +00003241 SourceLocation ScopeLoc, AttrLoc;
3242 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3243
3244 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3245 if (!AttrName)
3246 // Break out to the "expected ']'" diagnostic.
3247 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003248
Alexis Hunt96d5c762009-11-21 08:43:09 +00003249 // scoped attribute
3250 if (Tok.is(tok::coloncolon)) {
3251 ConsumeToken();
3252
Richard Smith3dff2512012-04-10 03:25:07 +00003253 ScopeName = AttrName;
3254 ScopeLoc = AttrLoc;
3255
3256 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3257 if (!AttrName) {
Alp Tokerec543272013-12-24 09:48:30 +00003258 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003259 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003260 continue;
3261 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003262 }
3263
Michael Han23214e52012-10-03 01:56:22 +00003264 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003265 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003266
Richard Smith10876ef2013-01-17 01:30:42 +00003267 if (StandardAttr &&
3268 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3269 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3270 << AttrName << SourceRange(SeenAttrs[AttrName]);
3271
Michael Han23214e52012-10-03 01:56:22 +00003272 // Parse attribute arguments
3273 if (Tok.is(tok::l_paren)) {
3274 if (ScopeName && ScopeName->getName() == "gnu") {
3275 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3276 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3277 AttrParsed = true;
3278 } else {
3279 if (StandardAttr)
3280 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3281 << AttrName->getName();
3282
3283 // FIXME: handle other formats of c++11 attribute arguments
3284 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003285 SkipUntil(tok::r_paren);
Michael Han23214e52012-10-03 01:56:22 +00003286 }
3287 }
3288
3289 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003290 attrs.addNew(AttrName,
3291 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3292 AttrLoc),
Aaron Ballman00e99962013-08-31 01:11:41 +00003293 ScopeName, ScopeLoc, 0, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003294
Richard Smith3dff2512012-04-10 03:25:07 +00003295 if (Tok.is(tok::ellipsis)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003296 ConsumeToken();
Michael Han23214e52012-10-03 01:56:22 +00003297
3298 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3299 << AttrName->getName();
Richard Smith3dff2512012-04-10 03:25:07 +00003300 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003301 }
3302
Alp Toker383d2c42014-01-01 03:08:43 +00003303 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003304 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003305 if (endLoc)
3306 *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003307 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003308 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003309}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003310
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003311/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003312///
3313/// attribute-specifier-seq:
3314/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003315void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003316 SourceLocation *endLoc) {
Richard Smith4cabd042013-02-22 09:15:49 +00003317 assert(getLangOpts().CPlusPlus11);
3318
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003319 SourceLocation StartLoc = Tok.getLocation(), Loc;
3320 if (!endLoc)
3321 endLoc = &Loc;
3322
Douglas Gregor6f981002011-10-07 20:35:25 +00003323 do {
Richard Smith3dff2512012-04-10 03:25:07 +00003324 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003325 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003326
3327 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003328}
3329
Richard Smithc2c8bb82013-10-15 01:34:54 +00003330void Parser::DiagnoseAndSkipCXX11Attributes() {
3331 if (!isCXX11AttributeSpecifier())
3332 return;
3333
3334 // Start and end location of an attribute or an attribute list.
3335 SourceLocation StartLoc = Tok.getLocation();
3336 SourceLocation EndLoc;
3337
3338 do {
3339 if (Tok.is(tok::l_square)) {
3340 BalancedDelimiterTracker T(*this, tok::l_square);
3341 T.consumeOpen();
3342 T.skipToEnd();
3343 EndLoc = T.getCloseLocation();
3344 } else {
3345 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3346 ConsumeToken();
3347 BalancedDelimiterTracker T(*this, tok::l_paren);
3348 if (!T.consumeOpen())
3349 T.skipToEnd();
3350 EndLoc = T.getCloseLocation();
3351 }
3352 } while (isCXX11AttributeSpecifier());
3353
3354 if (EndLoc.isValid()) {
3355 SourceRange Range(StartLoc, EndLoc);
3356 Diag(StartLoc, diag::err_attributes_not_allowed)
3357 << Range;
3358 }
3359}
3360
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003361/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3362///
3363/// [MS] ms-attribute:
3364/// '[' token-seq ']'
3365///
3366/// [MS] ms-attribute-seq:
3367/// ms-attribute[opt]
3368/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00003369void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3370 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003371 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3372
3373 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003374 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003375 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003376 SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
John McCall53fa7142010-12-24 02:08:15 +00003377 if (endLoc) *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003378 ExpectAndConsume(tok::r_square);
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003379 }
3380}
Francois Pichet8f981d52011-05-25 10:19:49 +00003381
3382void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3383 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00003384 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00003385 if (ParseMicrosoftIfExistsCondition(Result))
3386 return;
3387
Douglas Gregor43edb322011-10-24 22:31:10 +00003388 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3389 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00003390 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet8f981d52011-05-25 10:19:49 +00003391 return;
3392 }
Francois Pichet8f981d52011-05-25 10:19:49 +00003393
Douglas Gregor43edb322011-10-24 22:31:10 +00003394 switch (Result.Behavior) {
3395 case IEB_Parse:
3396 // Parse the declarations below.
3397 break;
3398
3399 case IEB_Dependent:
3400 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3401 << Result.IsIfExists;
3402 // Fall through to skip.
3403
3404 case IEB_Skip:
3405 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00003406 return;
3407 }
3408
Richard Smith34f30512013-11-23 04:06:09 +00003409 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003410 // __if_exists, __if_not_exists can nest.
3411 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3412 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3413 continue;
3414 }
3415
3416 // Check for extraneous top-level semicolon.
3417 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003418 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00003419 continue;
3420 }
3421
3422 AccessSpecifier AS = getAccessSpecifierIfPresent();
3423 if (AS != AS_none) {
3424 // Current token is a C++ access specifier.
3425 CurAS = AS;
3426 SourceLocation ASLoc = Tok.getLocation();
3427 ConsumeToken();
3428 if (Tok.is(tok::colon))
3429 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3430 else
Alp Toker35d87032013-12-30 23:29:50 +00003431 Diag(Tok, diag::err_expected) << tok::colon;
Francois Pichet8f981d52011-05-25 10:19:49 +00003432 ConsumeToken();
3433 continue;
3434 }
3435
3436 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00003437 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet8f981d52011-05-25 10:19:49 +00003438 }
Douglas Gregor43edb322011-10-24 22:31:10 +00003439
3440 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00003441}