blob: f09aaf5c0bbffce196f265f0d093990820c2d286 [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor1b7f8982008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/OperatorKinds.h"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
John McCall19510852010-08-20 18:27:03 +000020#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Sema/Scope.h"
John McCalle402e722012-09-25 07:32:39 +000023#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000025using namespace clang;
26
27/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000028/// may either be a top level namespace or a block-level namespace alias. If
29/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000030///
31/// namespace-definition: [C++ 7.3: basic.namespace]
32/// named-namespace-definition
33/// unnamed-namespace-definition
34///
35/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000036/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000037///
38/// named-namespace-definition:
39/// original-namespace-definition
40/// extension-namespace-definition
41///
42/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000043/// 'inline'[opt] 'namespace' identifier attributes[opt]
44/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000045///
46/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000047/// 'inline'[opt] 'namespace' original-namespace-name
48/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000049///
Chris Lattner8f08cb72007-08-25 06:57:03 +000050/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
51/// 'namespace' identifier '=' qualified-namespace-specifier ';'
52///
John McCalld226f652010-08-21 09:40:31 +000053Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000054 SourceLocation &DeclEnd,
55 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000056 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000057 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +000058 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000059
Douglas Gregor49f40bd2009-09-18 19:03:04 +000060 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000061 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000062 cutOffParsing();
63 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +000064 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000065
Chris Lattner8f08cb72007-08-25 06:57:03 +000066 SourceLocation IdentLoc;
67 IdentifierInfo *Ident = 0;
Richard Trieuf858bd82011-05-26 20:11:09 +000068 std::vector<SourceLocation> ExtraIdentLoc;
69 std::vector<IdentifierInfo*> ExtraIdent;
70 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000071
72 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattner04d66662007-10-09 17:33:22 +000074 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000075 Ident = Tok.getIdentifierInfo();
76 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieuf858bd82011-05-26 20:11:09 +000077 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
78 ExtraNamespaceLoc.push_back(ConsumeToken());
79 ExtraIdent.push_back(Tok.getIdentifierInfo());
80 ExtraIdentLoc.push_back(ConsumeToken());
81 }
Chris Lattner8f08cb72007-08-25 06:57:03 +000082 }
Mike Stump1eb44332009-09-09 15:08:12 +000083
Chris Lattner8f08cb72007-08-25 06:57:03 +000084 // Read label attributes, if present.
John McCall0b7e6782011-03-24 11:26:52 +000085 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000086 if (Tok.is(tok::kw___attribute)) {
87 attrTok = Tok;
John McCall7f040a92010-12-24 02:08:15 +000088 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000089 }
Mike Stump1eb44332009-09-09 15:08:12 +000090
Douglas Gregor6a588dd2009-06-17 19:49:00 +000091 if (Tok.is(tok::equal)) {
Nico Webere1bb3292012-10-27 23:44:27 +000092 if (Ident == 0) {
93 Diag(Tok, diag::err_expected_ident);
94 // Skip to end of the definition and eat the ';'.
95 SkipUntil(tok::semi);
96 return 0;
97 }
John McCall7f040a92010-12-24 02:08:15 +000098 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +000099 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +0000100 if (InlineLoc.isValid())
101 Diag(InlineLoc, diag::err_inline_namespace_alias)
102 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000103 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000104 }
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Richard Trieuf858bd82011-05-26 20:11:09 +0000106
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000107 BalancedDelimiterTracker T(*this, tok::l_brace);
108 if (T.consumeOpen()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000109 if (!ExtraIdent.empty()) {
110 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
111 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
112 }
Mike Stump1eb44332009-09-09 15:08:12 +0000113 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +0000114 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +0000115 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Douglas Gregor23c94db2010-07-02 17:43:08 +0000118 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
119 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
120 getCurScope()->getFnParent()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000121 if (!ExtraIdent.empty()) {
122 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
123 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
124 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000125 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Douglas Gregor95f1b152010-05-14 05:08:22 +0000126 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000127 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000128 }
129
Richard Trieuf858bd82011-05-26 20:11:09 +0000130 if (!ExtraIdent.empty()) {
131 TentativeParsingAction TPA(*this);
132 SkipUntil(tok::r_brace, /*StopAtSemi*/false, /*DontConsume*/true);
133 Token rBraceToken = Tok;
134 TPA.Revert();
135
136 if (!rBraceToken.is(tok::r_brace)) {
137 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
138 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
139 } else {
Benjamin Kramer9910df02011-05-26 21:32:30 +0000140 std::string NamespaceFix;
Richard Trieuf858bd82011-05-26 20:11:09 +0000141 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
142 E = ExtraIdent.end(); I != E; ++I) {
143 NamespaceFix += " { namespace ";
144 NamespaceFix += (*I)->getName();
145 }
Benjamin Kramer9910df02011-05-26 21:32:30 +0000146
Richard Trieuf858bd82011-05-26 20:11:09 +0000147 std::string RBraces;
Benjamin Kramer9910df02011-05-26 21:32:30 +0000148 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieuf858bd82011-05-26 20:11:09 +0000149 RBraces += "} ";
Benjamin Kramer9910df02011-05-26 21:32:30 +0000150
Richard Trieuf858bd82011-05-26 20:11:09 +0000151 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
152 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
153 ExtraIdentLoc.back()),
154 NamespaceFix)
155 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
156 }
157 }
158
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000159 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith7fe62082011-10-15 05:09:34 +0000160 if (InlineLoc.isValid())
Richard Smith80ad52f2013-01-02 11:42:31 +0000161 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000162 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000163
Chris Lattner51448322009-03-29 14:02:43 +0000164 // Enter a scope for the namespace.
165 ParseScope NamespaceScope(this, Scope::DeclScope);
166
John McCalld226f652010-08-21 09:40:31 +0000167 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000168 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000169 IdentLoc, Ident, T.getOpenLocation(),
170 attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000171
John McCallf312b1e2010-08-26 23:41:50 +0000172 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
173 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Richard Trieuf858bd82011-05-26 20:11:09 +0000175 // Parse the contents of the namespace. This includes parsing recovery on
176 // any improperly nested namespaces.
177 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000178 InlineLoc, attrs, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattner51448322009-03-29 14:02:43 +0000180 // Leave the namespace scope.
181 NamespaceScope.Exit();
182
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000183 DeclEnd = T.getCloseLocation();
184 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner51448322009-03-29 14:02:43 +0000185
186 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000187}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000188
Richard Trieuf858bd82011-05-26 20:11:09 +0000189/// ParseInnerNamespace - Parse the contents of a namespace.
190void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
191 std::vector<IdentifierInfo*>& Ident,
192 std::vector<SourceLocation>& NamespaceLoc,
193 unsigned int index, SourceLocation& InlineLoc,
Richard Trieuf858bd82011-05-26 20:11:09 +0000194 ParsedAttributes& attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000195 BalancedDelimiterTracker &Tracker) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000196 if (index == Ident.size()) {
197 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
198 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000199 MaybeParseCXX11Attributes(attrs);
Richard Trieuf858bd82011-05-26 20:11:09 +0000200 MaybeParseMicrosoftAttributes(attrs);
201 ParseExternalDeclaration(attrs);
202 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000203
204 // The caller is what called check -- we are simply calling
205 // the close for it.
206 Tracker.consumeClose();
Richard Trieuf858bd82011-05-26 20:11:09 +0000207
208 return;
209 }
210
211 // Parse improperly nested namespaces.
212 ParseScope NamespaceScope(this, Scope::DeclScope);
213 Decl *NamespcDecl =
214 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
215 NamespaceLoc[index], IdentLoc[index],
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000216 Ident[index], Tracker.getOpenLocation(),
217 attrs.getList());
Richard Trieuf858bd82011-05-26 20:11:09 +0000218
219 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000220 attrs, Tracker);
Richard Trieuf858bd82011-05-26 20:11:09 +0000221
222 NamespaceScope.Exit();
223
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000224 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieuf858bd82011-05-26 20:11:09 +0000225}
226
Anders Carlssonf67606a2009-03-28 04:07:16 +0000227/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
228/// alias definition.
229///
John McCalld226f652010-08-21 09:40:31 +0000230Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000231 SourceLocation AliasLoc,
232 IdentifierInfo *Alias,
233 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000234 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Anders Carlssonf67606a2009-03-28 04:07:16 +0000236 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000238 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000239 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000240 cutOffParsing();
241 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000242 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000243
Anders Carlssonf67606a2009-03-28 04:07:16 +0000244 CXXScopeSpec SS;
245 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000246 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000247
248 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
249 Diag(Tok, diag::err_expected_namespace_name);
250 // Skip to end of the definition and eat the ';'.
251 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000252 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000253 }
254
255 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000256 IdentifierInfo *Ident = Tok.getIdentifierInfo();
257 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Anders Carlssonf67606a2009-03-28 04:07:16 +0000259 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000260 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000261 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
262 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Douglas Gregor23c94db2010-07-02 17:43:08 +0000264 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000265 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000266}
267
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000268/// ParseLinkage - We know that the current token is a string_literal
269/// and just before that, that extern was seen.
270///
271/// linkage-specification: [C++ 7.5p2: dcl.link]
272/// 'extern' string-literal '{' declaration-seq[opt] '}'
273/// 'extern' string-literal declaration
274///
Chris Lattner7d642712010-11-09 20:15:55 +0000275Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000276 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000277 SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000278 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000279 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000280 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000281 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000282
Richard Smith99831e42012-03-06 03:21:47 +0000283 // FIXME: This is incorrect: linkage-specifiers are parsed in translation
284 // phase 7, so string-literal concatenation is supposed to occur.
285 // extern "" "C" "" "+" "+" { } is legal.
286 if (Tok.hasUDSuffix())
287 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000288 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000289
Douglas Gregor074149e2009-01-05 19:45:36 +0000290 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000291 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000292 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000293 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000294 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000295 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000296 : SourceLocation());
297
John McCall0b7e6782011-03-24 11:26:52 +0000298 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000299 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000300 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000301
Douglas Gregor074149e2009-01-05 19:45:36 +0000302 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-05-01 16:25:54 +0000303 // Reset the source range in DS, as the leading "extern"
304 // does not really belong to the inner declaration ...
305 DS.SetRangeStart(SourceLocation());
306 DS.SetRangeEnd(SourceLocation());
307 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000308 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000309 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000310 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000311 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000312 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000313
Douglas Gregor63a01132010-02-07 08:38:28 +0000314 DS.abort();
315
John McCall7f040a92010-12-24 02:08:15 +0000316 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000317
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000318 BalancedDelimiterTracker T(*this, tok::l_brace);
319 T.consumeOpen();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000320 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000321 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000322 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000323 MaybeParseMicrosoftAttributes(attrs);
324 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000325 }
326
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000327 T.consumeClose();
Chris Lattner7d642712010-11-09 20:15:55 +0000328 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000329 T.getCloseLocation());
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000330}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000331
Douglas Gregorf780abc2008-12-30 03:27:21 +0000332/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
333/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000334Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000335 const ParsedTemplateInfo &TemplateInfo,
336 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000337 ParsedAttributesWithRange &attrs,
338 Decl **OwnedType) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000339 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000340 ObjCDeclContextSwitch ObjCDC(*this);
341
Douglas Gregorf780abc2008-12-30 03:27:21 +0000342 // Eat 'using'.
343 SourceLocation UsingLoc = ConsumeToken();
344
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000345 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000346 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000347 cutOffParsing();
348 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000349 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000350
John McCall78b81052010-11-10 02:40:36 +0000351 // 'using namespace' means this is a using-directive.
352 if (Tok.is(tok::kw_namespace)) {
353 // Template parameters are always an error here.
354 if (TemplateInfo.Kind) {
355 SourceRange R = TemplateInfo.getSourceRange();
356 Diag(UsingLoc, diag::err_templated_using_directive)
357 << R << FixItHint::CreateRemoval(R);
358 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000359
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000360 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000361 }
362
Richard Smith162e1c12011-04-15 14:24:37 +0000363 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000364
365 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000366 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000367
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000368 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000369 AS_none, OwnedType);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000370}
371
372/// ParseUsingDirective - Parse C++ using-directive, assumes
373/// that current token is 'namespace' and 'using' was already parsed.
374///
375/// using-directive: [C++ 7.3.p4: namespace.udir]
376/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
377/// namespace-name ;
378/// [GNU] using-directive:
379/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
380/// namespace-name attributes[opt] ;
381///
John McCalld226f652010-08-21 09:40:31 +0000382Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000383 SourceLocation UsingLoc,
384 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000385 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000386 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
387
388 // Eat 'namespace'.
389 SourceLocation NamespcLoc = ConsumeToken();
390
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000391 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000392 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000393 cutOffParsing();
394 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000395 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000396
Douglas Gregorf780abc2008-12-30 03:27:21 +0000397 CXXScopeSpec SS;
398 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000399 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000400
Douglas Gregorf780abc2008-12-30 03:27:21 +0000401 IdentifierInfo *NamespcName = 0;
402 SourceLocation IdentLoc = SourceLocation();
403
404 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000405 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000406 Diag(Tok, diag::err_expected_namespace_name);
407 // If there was invalid namespace name, skip to end of decl, and eat ';'.
408 SkipUntil(tok::semi);
409 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000410 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Chris Lattner823c44e2009-01-06 07:27:21 +0000413 // Parse identifier.
414 NamespcName = Tok.getIdentifierInfo();
415 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner823c44e2009-01-06 07:27:21 +0000417 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000418 bool GNUAttr = false;
419 if (Tok.is(tok::kw___attribute)) {
420 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000421 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattner823c44e2009-01-06 07:27:21 +0000424 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000425 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000426 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000427 GNUAttr ? diag::err_expected_semi_after_attribute_list
428 : diag::err_expected_semi_after_namespace_name,
429 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000430
Douglas Gregor23c94db2010-07-02 17:43:08 +0000431 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000432 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000433}
434
Richard Smith162e1c12011-04-15 14:24:37 +0000435/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
436/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000437///
438/// using-declaration: [C++ 7.3.p3: namespace.udecl]
439/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000440/// unqualified-id
441/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000442///
Richard Smithd03de6a2013-01-29 10:02:16 +0000443/// alias-declaration: C++11 [dcl.dcl]p1
444/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smith162e1c12011-04-15 14:24:37 +0000445///
John McCalld226f652010-08-21 09:40:31 +0000446Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000447 const ParsedTemplateInfo &TemplateInfo,
448 SourceLocation UsingLoc,
449 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000450 AccessSpecifier AS,
451 Decl **OwnedType) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000452 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000453 SourceLocation TypenameLoc;
Richard Smith6b3d3e52013-02-20 19:22:51 +0000454 bool IsTypeName = false;
455 ParsedAttributesWithRange Attrs(AttrFactory);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000456
457 // FIXME: Simply skip the attributes and diagnose, don't bother parsing them.
Richard Smith6b3d3e52013-02-20 19:22:51 +0000458 MaybeParseCXX11Attributes(Attrs);
459 ProhibitAttributes(Attrs);
460 Attrs.clear();
461 Attrs.Range = SourceRange();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000462
463 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000464 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000465 if (Tok.is(tok::kw_typename)) {
Richard Smith6b3d3e52013-02-20 19:22:51 +0000466 TypenameLoc = ConsumeToken();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000467 IsTypeName = true;
468 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000469
470 // Parse nested-name-specifier.
Richard Smith2db075b2013-03-26 01:15:19 +0000471 IdentifierInfo *LastII = 0;
472 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
473 /*MayBePseudoDtor=*/0, /*IsTypename=*/false,
474 /*LastII=*/&LastII);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000475
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000476 // Check nested-name specifier.
477 if (SS.isInvalid()) {
478 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000479 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000480 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000481
Richard Smith2db075b2013-03-26 01:15:19 +0000482 SourceLocation TemplateKWLoc;
483 UnqualifiedId Name;
484
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000485 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000486 // destructor names and allow the action module to diagnose any semantic
487 // errors.
Richard Smith2db075b2013-03-26 01:15:19 +0000488 //
489 // C++11 [class.qual]p2:
490 // [...] in a using-declaration that is a member-declaration, if the name
491 // specified after the nested-name-specifier is the same as the identifier
492 // or the simple-template-id's template-name in the last component of the
493 // nested-name-specifier, the name is [...] considered to name the
494 // constructor.
495 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
496 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
497 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
498 !SS.getScopeRep()->getAsNamespace() &&
499 !SS.getScopeRep()->getAsNamespaceAlias()) {
500 SourceLocation IdLoc = ConsumeToken();
501 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
502 Name.setConstructorName(Type, IdLoc, IdLoc);
503 } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
504 /*AllowDestructorName=*/ true,
505 /*AllowConstructorName=*/ true, ParsedType(),
506 TemplateKWLoc, Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000507 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000508 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000509 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000510
Richard Smith6b3d3e52013-02-20 19:22:51 +0000511 MaybeParseCXX11Attributes(Attrs);
Richard Smith162e1c12011-04-15 14:24:37 +0000512
513 // Maybe this is an alias-declaration.
514 bool IsAliasDecl = Tok.is(tok::equal);
515 TypeResult TypeAlias;
516 if (IsAliasDecl) {
Richard Smith6b3d3e52013-02-20 19:22:51 +0000517 // TODO: Can GNU attributes appear here?
Richard Smith162e1c12011-04-15 14:24:37 +0000518 ConsumeToken();
519
Richard Smith80ad52f2013-01-02 11:42:31 +0000520 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000521 diag::warn_cxx98_compat_alias_declaration :
522 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000523
Richard Smith3e4c6c42011-05-05 21:57:07 +0000524 // Type alias templates cannot be specialized.
525 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000526 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
527 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000528 SpecKind = 0;
529 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
530 SpecKind = 1;
531 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
532 SpecKind = 2;
533 if (SpecKind != -1) {
534 SourceRange Range;
535 if (SpecKind == 0)
536 Range = SourceRange(Name.TemplateId->LAngleLoc,
537 Name.TemplateId->RAngleLoc);
538 else
539 Range = TemplateInfo.getSourceRange();
540 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
541 << SpecKind << Range;
542 SkipUntil(tok::semi);
543 return 0;
544 }
545
Richard Smith162e1c12011-04-15 14:24:37 +0000546 // Name must be an identifier.
547 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
548 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
549 // No removal fixit: can't recover from this.
550 SkipUntil(tok::semi);
551 return 0;
552 } else if (IsTypeName)
553 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
554 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
555 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
556 else if (SS.isNotEmpty())
557 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
558 << FixItHint::CreateRemoval(SS.getRange());
559
Richard Smith3e4c6c42011-05-05 21:57:07 +0000560 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
561 Declarator::AliasTemplateContext :
Richard Smith6b3d3e52013-02-20 19:22:51 +0000562 Declarator::AliasDeclContext, AS, OwnedType,
563 &Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000564 } else {
565 // C++11 attributes are not allowed on a using-declaration, but GNU ones
566 // are.
Richard Smith6b3d3e52013-02-20 19:22:51 +0000567 ProhibitAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000568
Richard Smith162e1c12011-04-15 14:24:37 +0000569 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith6b3d3e52013-02-20 19:22:51 +0000570 MaybeParseGNUAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000571 }
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000573 // Eat ';'.
574 DeclEnd = Tok.getLocation();
575 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000576 !Attrs.empty() ? "attributes list" :
Richard Smith162e1c12011-04-15 14:24:37 +0000577 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000578 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000579
John McCall78b81052010-11-10 02:40:36 +0000580 // Diagnose an attempt to declare a templated using-declaration.
Richard Smithd03de6a2013-01-29 10:02:16 +0000581 // In C++11, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000582 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000583 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000584 SourceRange R = TemplateInfo.getSourceRange();
585 Diag(UsingLoc, diag::err_templated_using_declaration)
586 << R << FixItHint::CreateRemoval(R);
587
588 // Unfortunately, we have to bail out instead of recovering by
589 // ignoring the parameters, just in case the nested name specifier
590 // depends on the parameters.
591 return 0;
592 }
593
Douglas Gregor480b53c2011-09-26 14:30:28 +0000594 // "typename" keyword is allowed for identifiers only,
595 // because it may be a type definition.
596 if (IsTypeName && Name.getKind() != UnqualifiedId::IK_Identifier) {
597 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
598 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
599 // Proceed parsing, but reset the IsTypeName flag.
600 IsTypeName = false;
601 }
602
Richard Smith3e4c6c42011-05-05 21:57:07 +0000603 if (IsAliasDecl) {
604 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramer5354e772012-08-23 23:38:35 +0000605 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3e4c6c42011-05-05 21:57:07 +0000606 TemplateParams ? TemplateParams->data() : 0,
607 TemplateParams ? TemplateParams->size() : 0);
608 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000609 UsingLoc, Name, Attrs.getList(),
610 TypeAlias);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000611 }
Richard Smith162e1c12011-04-15 14:24:37 +0000612
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000613 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000614 Name, Attrs.getList(),
John McCall7f040a92010-12-24 02:08:15 +0000615 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000616}
617
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000618/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000619///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000620/// [C++0x] static_assert-declaration:
621/// static_assert ( constant-expression , string-literal ) ;
622///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000623/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000624/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000625///
John McCalld226f652010-08-21 09:40:31 +0000626Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000627 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
628 "Not a static_assert declaration");
629
David Blaikie4e4d0842012-03-11 07:00:24 +0000630 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000631 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000632 if (Tok.is(tok::kw_static_assert))
633 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000634
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000635 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000637 BalancedDelimiterTracker T(*this, tok::l_paren);
638 if (T.consumeOpen()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000639 Diag(Tok, diag::err_expected_lparen);
Richard Smith3686c712012-09-13 19:12:50 +0000640 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000641 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
John McCall60d7b3a2010-08-24 06:29:42 +0000644 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000645 if (AssertExpr.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000646 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000647 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000648 }
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Anders Carlssonad5f9602009-03-13 23:29:20 +0000650 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000651 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000652
Richard Smith0cc323c2012-03-05 23:20:05 +0000653 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000654 Diag(Tok, diag::err_expected_string_literal)
655 << /*Source='static_assert'*/1;
Richard Smith3686c712012-09-13 19:12:50 +0000656 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000657 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000658 }
Mike Stump1eb44332009-09-09 15:08:12 +0000659
John McCall60d7b3a2010-08-24 06:29:42 +0000660 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smith99831e42012-03-06 03:21:47 +0000661 if (AssertMessage.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000662 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000663 return 0;
Richard Smith99831e42012-03-06 03:21:47 +0000664 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000665
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000666 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Chris Lattner97144fc2009-04-02 04:16:50 +0000668 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000669 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000670
John McCall9ae2f072010-08-23 23:25:46 +0000671 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
672 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000673 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000674 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000675}
676
Richard Smitha2c36462013-04-26 16:15:35 +0000677/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000678///
679/// 'decltype' ( expression )
Richard Smitha2c36462013-04-26 16:15:35 +0000680/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000681///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000682SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
683 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
684 && "Not a decltype specifier");
685
David Blaikie42d6d0c2011-12-04 05:04:18 +0000686 ExprResult Result;
687 SourceLocation StartLoc = Tok.getLocation();
688 SourceLocation EndLoc;
689
690 if (Tok.is(tok::annot_decltype)) {
691 Result = getExprAnnotation(Tok);
692 EndLoc = Tok.getAnnotationEndLoc();
693 ConsumeToken();
694 if (Result.isInvalid()) {
695 DS.SetTypeSpecError();
696 return EndLoc;
697 }
698 } else {
Richard Smithc7b55432012-02-24 22:30:04 +0000699 if (Tok.getIdentifierInfo()->isStr("decltype"))
700 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000701
David Blaikie42d6d0c2011-12-04 05:04:18 +0000702 ConsumeToken();
703
704 BalancedDelimiterTracker T(*this, tok::l_paren);
705 if (T.expectAndConsume(diag::err_expected_lparen_after,
706 "decltype", tok::r_paren)) {
707 DS.SetTypeSpecError();
708 return T.getOpenLocation() == Tok.getLocation() ?
709 StartLoc : T.getOpenLocation();
710 }
711
Richard Smitha2c36462013-04-26 16:15:35 +0000712 // Check for C++1y 'decltype(auto)'.
713 if (Tok.is(tok::kw_auto)) {
714 // No need to disambiguate here: an expression can't start with 'auto',
715 // because the typename-specifier in a function-style cast operation can't
716 // be 'auto'.
717 Diag(Tok.getLocation(),
718 getLangOpts().CPlusPlus1y
719 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
720 : diag::ext_decltype_auto_type_specifier);
721 ConsumeToken();
722 } else {
723 // Parse the expression
David Blaikie42d6d0c2011-12-04 05:04:18 +0000724
Richard Smitha2c36462013-04-26 16:15:35 +0000725 // C++11 [dcl.type.simple]p4:
726 // The operand of the decltype specifier is an unevaluated operand.
727 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
728 0, /*IsDecltype=*/true);
729 Result = ParseExpression();
730 if (Result.isInvalid()) {
731 DS.SetTypeSpecError();
732 if (SkipUntil(tok::r_paren, /*StopAtSemi=*/true,
733 /*DontConsume=*/true)) {
734 EndLoc = ConsumeParen();
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000735 } else {
Richard Smitha2c36462013-04-26 16:15:35 +0000736 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
737 // Backtrack to get the location of the last token before the semi.
738 PP.RevertCachedTokens(2);
739 ConsumeToken(); // the semi.
740 EndLoc = ConsumeAnyToken();
741 assert(Tok.is(tok::semi));
742 } else {
743 EndLoc = Tok.getLocation();
744 }
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000745 }
Richard Smitha2c36462013-04-26 16:15:35 +0000746 return EndLoc;
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000747 }
Richard Smitha2c36462013-04-26 16:15:35 +0000748
749 Result = Actions.ActOnDecltypeExpression(Result.take());
David Blaikie42d6d0c2011-12-04 05:04:18 +0000750 }
751
752 // Match the ')'
753 T.consumeClose();
754 if (T.getCloseLocation().isInvalid()) {
755 DS.SetTypeSpecError();
756 // FIXME: this should return the location of the last token
757 // that was consumed (by "consumeClose()")
758 return T.getCloseLocation();
759 }
760
Richard Smith76f3f692012-02-22 02:04:18 +0000761 if (Result.isInvalid()) {
762 DS.SetTypeSpecError();
763 return T.getCloseLocation();
764 }
765
David Blaikie42d6d0c2011-12-04 05:04:18 +0000766 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000767 }
Richard Smitha2c36462013-04-26 16:15:35 +0000768 assert(!Result.isInvalid());
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000770 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000771 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000772 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smitha2c36462013-04-26 16:15:35 +0000773 if (Result.get()
774 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
775 DiagID, Result.release())
776 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
777 DiagID)) {
John McCallfec54012009-08-03 20:12:06 +0000778 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000779 DS.SetTypeSpecError();
780 }
781 return EndLoc;
782}
783
784void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
785 SourceLocation StartLoc,
786 SourceLocation EndLoc) {
787 // make sure we have a token we can turn into an annotation token
788 if (PP.isBacktrackEnabled())
789 PP.RevertCachedTokens(1);
790 else
791 PP.EnterToken(Tok);
792
793 Tok.setKind(tok::annot_decltype);
Richard Smitha2c36462013-04-26 16:15:35 +0000794 setExprAnnotation(Tok,
795 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
796 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
797 ExprError());
David Blaikie42d6d0c2011-12-04 05:04:18 +0000798 Tok.setAnnotationEndLoc(EndLoc);
799 Tok.setLocation(StartLoc);
800 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000801}
802
Sean Huntdb5d44b2011-05-19 05:37:45 +0000803void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
804 assert(Tok.is(tok::kw___underlying_type) &&
805 "Not an underlying type specifier");
806
807 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000808 BalancedDelimiterTracker T(*this, tok::l_paren);
809 if (T.expectAndConsume(diag::err_expected_lparen_after,
810 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000811 return;
812 }
813
814 TypeResult Result = ParseTypeName();
815 if (Result.isInvalid()) {
816 SkipUntil(tok::r_paren);
817 return;
818 }
819
820 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000821 T.consumeClose();
822 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000823 return;
824
825 const char *PrevSpec = 0;
826 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000827 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000828 DiagID, Result.release()))
829 Diag(StartLoc, DiagID) << PrevSpec;
830}
831
David Blaikie09048df2011-10-25 15:01:20 +0000832/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
833/// class name or decltype-specifier. Note that we only check that the result
834/// names a type; semantic analysis will need to verify that the type names a
835/// class. The result is either a type or null, depending on whether a type
836/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000837///
Richard Smith05321402013-02-19 23:47:15 +0000838/// base-type-specifier: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000839/// class-or-decltype
Richard Smith05321402013-02-19 23:47:15 +0000840/// class-or-decltype: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000841/// nested-name-specifier[opt] class-name
842/// decltype-specifier
Richard Smith05321402013-02-19 23:47:15 +0000843/// class-name: [C++ class.name]
Douglas Gregor42a552f2008-11-05 20:51:48 +0000844/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000845/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000846///
Richard Smith05321402013-02-19 23:47:15 +0000847/// In C++98, instead of base-type-specifier, we have:
848///
849/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie22216eb2011-10-25 17:10:12 +0000850Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
851 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000852 // Ignore attempts to use typename
853 if (Tok.is(tok::kw_typename)) {
854 Diag(Tok, diag::err_expected_class_name_not_template)
855 << FixItHint::CreateRemoval(Tok.getLocation());
856 ConsumeToken();
857 }
858
David Blaikie152aa4b2011-10-25 18:17:58 +0000859 // Parse optional nested-name-specifier
860 CXXScopeSpec SS;
861 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
862
863 BaseLoc = Tok.getLocation();
864
David Blaikie22216eb2011-10-25 17:10:12 +0000865 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000866 // tok == kw_decltype is just error recovery, it can only happen when SS
867 // isn't empty
868 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000869 if (SS.isNotEmpty())
870 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
871 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000872 // Fake up a Declarator to use with ActOnTypeName.
873 DeclSpec DS(AttrFactory);
874
David Blaikieb5777572011-12-08 04:53:15 +0000875 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000876
877 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
878 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
879 }
880
Douglas Gregor7f43d672009-02-25 23:52:28 +0000881 // Check whether we have a template-id that names a type.
882 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000883 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000884 if (TemplateId->Kind == TNK_Type_template ||
885 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000886 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000887
888 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000889 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000890 EndLocation = Tok.getAnnotationEndLoc();
891 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000892
893 if (Type)
894 return Type;
895 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000896 }
897
898 // Fall through to produce an error below.
899 }
900
Douglas Gregor42a552f2008-11-05 20:51:48 +0000901 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000902 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000903 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000904 }
905
Douglas Gregor84d0a192010-01-12 21:28:44 +0000906 IdentifierInfo *Id = Tok.getIdentifierInfo();
907 SourceLocation IdLoc = ConsumeToken();
908
909 if (Tok.is(tok::less)) {
910 // It looks the user intended to write a template-id here, but the
911 // template-name was wrong. Try to fix that.
912 TemplateNameKind TNK = TNK_Type_template;
913 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000914 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000915 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000916 Diag(IdLoc, diag::err_unknown_template_name)
917 << Id;
918 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000919
Douglas Gregor84d0a192010-01-12 21:28:44 +0000920 if (!Template)
921 return true;
922
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000923 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000924 UnqualifiedId TemplateName;
925 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000926
Douglas Gregor84d0a192010-01-12 21:28:44 +0000927 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000928 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
929 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000930 return true;
931 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000932 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000933
Douglas Gregor84d0a192010-01-12 21:28:44 +0000934 // If we didn't end up with a typename token, there's nothing more we
935 // can do.
936 if (Tok.isNot(tok::annot_typename))
937 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000938
Douglas Gregor84d0a192010-01-12 21:28:44 +0000939 // Retrieve the type from the annotation token, consume that token, and
940 // return.
941 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000942 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000943 ConsumeToken();
944 return Type;
945 }
946
Douglas Gregor42a552f2008-11-05 20:51:48 +0000947 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000948 IdentifierInfo *CorrectedII = 0;
Douglas Gregor059101f2011-03-02 00:47:37 +0000949 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000950 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000951 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000952 /*NonTrivialTypeSourceInfo=*/true,
953 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000954 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000955 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000956 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000957 }
958
959 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000960 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000961
962 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000963 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000964 DS.SetRangeStart(IdLoc);
965 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000966 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000967
968 const char *PrevSpec = 0;
969 unsigned DiagID;
970 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
971
972 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
973 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000974}
975
John McCallc052dbb2012-05-22 21:28:12 +0000976void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
977 while (Tok.is(tok::kw___single_inheritance) ||
978 Tok.is(tok::kw___multiple_inheritance) ||
979 Tok.is(tok::kw___virtual_inheritance)) {
980 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
981 SourceLocation AttrNameLoc = ConsumeToken();
982 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000983 SourceLocation(), 0, 0, AttributeList::AS_GNU);
John McCallc052dbb2012-05-22 21:28:12 +0000984 }
985}
986
Richard Smithc9f35172012-06-25 21:37:02 +0000987/// Determine whether the following tokens are valid after a type-specifier
988/// which could be a standalone declaration. This will conservatively return
989/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith139be702012-07-02 19:14:01 +0000990bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smithc9f35172012-06-25 21:37:02 +0000991 // This switch enumerates the valid "follow" set for type-specifiers.
992 switch (Tok.getKind()) {
993 default: break;
994 case tok::semi: // struct foo {...} ;
995 case tok::star: // struct foo {...} * P;
996 case tok::amp: // struct foo {...} & R = ...
Richard Smithba65f502013-01-19 03:48:05 +0000997 case tok::ampamp: // struct foo {...} && R = ...
Richard Smithc9f35172012-06-25 21:37:02 +0000998 case tok::identifier: // struct foo {...} V ;
999 case tok::r_paren: //(struct foo {...} ) {4}
1000 case tok::annot_cxxscope: // struct foo {...} a:: b;
1001 case tok::annot_typename: // struct foo {...} a ::b;
1002 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1003 case tok::l_paren: // struct foo {...} ( x);
1004 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smithba65f502013-01-19 03:48:05 +00001005 case tok::kw_operator: // struct foo operator ++() {...}
Richard Smithc9f35172012-06-25 21:37:02 +00001006 return true;
Richard Smith139be702012-07-02 19:14:01 +00001007 case tok::colon:
1008 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smithc9f35172012-06-25 21:37:02 +00001009 // Type qualifiers
1010 case tok::kw_const: // struct foo {...} const x;
1011 case tok::kw_volatile: // struct foo {...} volatile x;
1012 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smithba65f502013-01-19 03:48:05 +00001013 // Function specifiers
1014 // Note, no 'explicit'. An explicit function must be either a conversion
1015 // operator or a constructor. Either way, it can't have a return type.
1016 case tok::kw_inline: // struct foo inline f();
1017 case tok::kw_virtual: // struct foo virtual f();
1018 case tok::kw_friend: // struct foo friend f();
Richard Smithc9f35172012-06-25 21:37:02 +00001019 // Storage-class specifiers
1020 case tok::kw_static: // struct foo {...} static x;
1021 case tok::kw_extern: // struct foo {...} extern x;
1022 case tok::kw_typedef: // struct foo {...} typedef x;
1023 case tok::kw_register: // struct foo {...} register x;
1024 case tok::kw_auto: // struct foo {...} auto x;
1025 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smithba65f502013-01-19 03:48:05 +00001026 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smithc9f35172012-06-25 21:37:02 +00001027 case tok::kw_constexpr: // struct foo {...} constexpr x;
1028 // As shown above, type qualifiers and storage class specifiers absolutely
1029 // can occur after class specifiers according to the grammar. However,
1030 // almost no one actually writes code like this. If we see one of these,
1031 // it is much more likely that someone missed a semi colon and the
1032 // type/storage class specifier we're seeing is part of the *next*
1033 // intended declaration, as in:
1034 //
1035 // struct foo { ... }
1036 // typedef int X;
1037 //
1038 // We'd really like to emit a missing semicolon error instead of emitting
1039 // an error on the 'int' saying that you can't have two type specifiers in
1040 // the same declaration of X. Because of this, we look ahead past this
1041 // token to see if it's a type specifier. If so, we know the code is
1042 // otherwise invalid, so we can produce the expected semi error.
1043 if (!isKnownToBeTypeSpecifier(NextToken()))
1044 return true;
1045 break;
1046 case tok::r_brace: // struct bar { struct foo {...} }
1047 // Missing ';' at end of struct is accepted as an extension in C mode.
1048 if (!getLangOpts().CPlusPlus)
1049 return true;
1050 break;
Richard Smithba65f502013-01-19 03:48:05 +00001051 // C++11 attributes
1052 case tok::l_square: // enum E [[]] x
1053 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1054 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith8338a9d2013-01-29 04:13:32 +00001055 case tok::greater:
1056 // template<class T = class X>
1057 return getLangOpts().CPlusPlus;
Richard Smithc9f35172012-06-25 21:37:02 +00001058 }
1059 return false;
1060}
1061
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001062/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1063/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1064/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +00001065/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001066///
1067/// class-specifier: [C++ class]
1068/// class-head '{' member-specification[opt] '}'
1069/// class-head '{' member-specification[opt] '}' attributes[opt]
1070/// class-head:
1071/// class-key identifier[opt] base-clause[opt]
1072/// class-key nested-name-specifier identifier base-clause[opt]
1073/// class-key nested-name-specifier[opt] simple-template-id
1074/// base-clause[opt]
1075/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001076/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001077/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001078/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001079/// simple-template-id base-clause[opt]
1080/// class-key:
1081/// 'class'
1082/// 'struct'
1083/// 'union'
1084///
1085/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +00001086/// class-key ::[opt] nested-name-specifier[opt] identifier
1087/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1088/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001089///
1090/// Note that the C++ class-specifier and elaborated-type-specifier,
1091/// together, subsume the C99 struct-or-union-specifier:
1092///
1093/// struct-or-union-specifier: [C99 6.7.2.1]
1094/// struct-or-union identifier[opt] '{' struct-contents '}'
1095/// struct-or-union identifier
1096/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1097/// '}' attributes[opt]
1098/// [GNU] struct-or-union attributes[opt] identifier
1099/// struct-or-union:
1100/// 'struct'
1101/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +00001102void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1103 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001104 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001105 AccessSpecifier AS,
Michael Han2e397132012-11-26 22:54:45 +00001106 bool EnteringContext, DeclSpecContext DSC,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001107 ParsedAttributesWithRange &Attributes) {
Joao Matos17d35c32012-08-31 22:18:20 +00001108 DeclSpec::TST TagType;
1109 if (TagTokKind == tok::kw_struct)
1110 TagType = DeclSpec::TST_struct;
1111 else if (TagTokKind == tok::kw___interface)
1112 TagType = DeclSpec::TST_interface;
1113 else if (TagTokKind == tok::kw_class)
1114 TagType = DeclSpec::TST_class;
1115 else {
Chris Lattner4c97d762009-04-12 21:49:30 +00001116 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1117 TagType = DeclSpec::TST_union;
1118 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001119
Douglas Gregor374929f2009-09-18 15:37:17 +00001120 if (Tok.is(tok::code_completion)) {
1121 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001122 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001123 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00001124 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001125
Chandler Carruth926c4b42010-06-28 08:39:25 +00001126 // C++03 [temp.explicit] 14.7.2/8:
1127 // The usual access checking rules do not apply to names used to specify
1128 // explicit instantiations.
1129 //
1130 // As an extension we do not perform access checking on the names used to
1131 // specify explicit specializations either. This is important to allow
1132 // specializing traits classes for private types.
John McCall13489672012-05-07 06:16:58 +00001133 //
1134 // Note that we don't suppress if this turns out to be an elaborated
1135 // type specifier.
1136 bool shouldDelayDiagsInTag =
1137 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1138 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1139 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001140
Sean Hunt2edf0a22012-06-23 05:07:58 +00001141 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001142 // If attributes exist after tag, parse them.
1143 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +00001144 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001145
Steve Narofff59e17e2008-12-24 20:59:21 +00001146 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001147 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001148 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001149
John McCallc052dbb2012-05-22 21:28:12 +00001150 // Parse inheritance specifiers.
1151 if (Tok.is(tok::kw___single_inheritance) ||
1152 Tok.is(tok::kw___multiple_inheritance) ||
1153 Tok.is(tok::kw___virtual_inheritance))
1154 ParseMicrosoftInheritanceClassAttributes(attrs);
1155
Sean Huntbbd37c62009-11-21 08:43:09 +00001156 // If C++0x attributes exist here, parse them.
1157 // FIXME: Are we consistent with the ordering of parsing of different
1158 // styles of attributes?
Richard Smith4e24f0f2013-01-02 12:01:23 +00001159 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Michael Han07fc1ba2013-01-07 16:57:11 +00001161 // Source location used by FIXIT to insert misplaced
1162 // C++11 attributes
1163 SourceLocation AttrFixitLoc = Tok.getLocation();
1164
John Wiegley20c0da72011-04-27 23:09:49 +00001165 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +00001166 !Tok.is(tok::identifier) &&
1167 Tok.getIdentifierInfo() &&
1168 (Tok.is(tok::kw___is_arithmetic) ||
1169 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001170 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001171 Tok.is(tok::kw___is_floating_point) ||
1172 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001173 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001174 Tok.is(tok::kw___is_integral) ||
1175 Tok.is(tok::kw___is_member_function_pointer) ||
1176 Tok.is(tok::kw___is_member_pointer) ||
1177 Tok.is(tok::kw___is_pod) ||
1178 Tok.is(tok::kw___is_pointer) ||
1179 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001180 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001181 Tok.is(tok::kw___is_signed) ||
1182 Tok.is(tok::kw___is_unsigned) ||
1183 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001184 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001185 // name of struct templates, but some are keywords in GCC >= 4.3
1186 // and Clang. Therefore, when we see the token sequence "struct
1187 // X", make X into a normal identifier rather than a keyword, to
1188 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001189 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001190 Tok.setKind(tok::identifier);
1191 }
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001193 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001194 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001195 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001196 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1197 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001198
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001199 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001200 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001201 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001202 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1203 Diag(Tok, diag::err_expected_ident);
1204 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001205
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001206 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1207
Douglas Gregorcc636682009-02-17 23:15:12 +00001208 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001209 IdentifierInfo *Name = 0;
1210 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001211 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001212 if (Tok.is(tok::identifier)) {
1213 Name = Tok.getIdentifierInfo();
1214 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001215
David Blaikie4e4d0842012-03-11 07:00:24 +00001216 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001217 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001218 // Eat the template argument list and try to continue parsing this as
1219 // a class (or template thereof).
1220 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001221 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001222 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001223 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001224 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001225 // We couldn't parse the template argument list at all, so don't
1226 // try to give any location information for the list.
1227 LAngleLoc = RAngleLoc = SourceLocation();
1228 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001229
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001230 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matos17d35c32012-08-31 22:18:20 +00001231 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1232 << (TagType == DeclSpec::TST_class? 0
1233 : TagType == DeclSpec::TST_struct? 1
1234 : TagType == DeclSpec::TST_interface? 2
1235 : 3)
1236 << Name
1237 << SourceRange(LAngleLoc, RAngleLoc);
1238
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001239 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001240 // we've removed its template argument list.
1241 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1242 if (TemplateParams && TemplateParams->size() > 1) {
1243 TemplateParams->pop_back();
1244 } else {
1245 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001246 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001247 = ParsedTemplateInfo::NonTemplate;
1248 }
1249 } else if (TemplateInfo.Kind
1250 == ParsedTemplateInfo::ExplicitInstantiation) {
1251 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001252 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001253 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001254 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001255 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001256 = SourceLocation();
1257 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1258 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001259 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001260 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001261 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001262 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001263 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001264
Douglas Gregor059101f2011-03-02 00:47:37 +00001265 if (TemplateId->Kind != TNK_Type_template &&
1266 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001267 // The template-name in the simple-template-id refers to
1268 // something other than a class template. Give an appropriate
1269 // error message and skip to the ';'.
1270 SourceRange Range(NameLoc);
1271 if (SS.isNotEmpty())
1272 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001273
Douglas Gregor39a8de12009-02-25 19:37:18 +00001274 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu6e91f4b2013-06-19 22:25:01 +00001275 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Douglas Gregor39a8de12009-02-25 19:37:18 +00001277 DS.SetTypeSpecError();
1278 SkipUntil(tok::semi, false, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001279 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001280 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001281 }
1282
Richard Smith7796eb52012-03-12 08:56:40 +00001283 // There are four options here.
1284 // - If we are in a trailing return type, this is always just a reference,
1285 // and we must not try to parse a definition. For instance,
1286 // [] () -> struct S { };
1287 // does not define a type.
1288 // - If we have 'struct foo {...', 'struct foo :...',
1289 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1290 // - If we have 'struct foo;', then this is either a forward declaration
1291 // or a friend declaration, which have to be treated differently.
1292 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han2e397132012-11-26 22:54:45 +00001293 //
1294 // We also detect these erroneous cases to provide better diagnostic for
1295 // C++11 attributes parsing.
1296 // - attributes follow class name:
1297 // struct foo [[]] {};
1298 // - attributes appear before or after 'final':
1299 // struct foo [[]] final [[]] {};
1300 //
Richard Smith69730c12012-03-12 07:56:15 +00001301 // However, in type-specifier-seq's, things look like declarations but are
1302 // just references, e.g.
1303 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001304 // or
Richard Smith69730c12012-03-12 07:56:15 +00001305 // &T::operator struct s;
1306 // For these, DSC is DSC_type_specifier.
Michael Han2e397132012-11-26 22:54:45 +00001307
1308 // If there are attributes after class name, parse them.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001309 MaybeParseCXX11Attributes(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00001310
John McCallf312b1e2010-08-26 23:41:50 +00001311 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001312 if (DSC == DSC_trailing)
1313 TUK = Sema::TUK_Reference;
1314 else if (Tok.is(tok::l_brace) ||
1315 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001316 (isCXX11FinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001317 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001318 if (DS.isFriendSpecified()) {
1319 // C++ [class.friend]p2:
1320 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001321 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001322 << SourceRange(DS.getFriendSpecLoc());
1323
1324 // Skip everything up to the semicolon, so that this looks like a proper
1325 // friend class (or template thereof) declaration.
1326 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001327 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001328 } else {
1329 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001330 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001331 }
Richard Smith150d8532013-02-22 06:46:23 +00001332 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1333 NextToken().is(tok::kw_alignas))) {
Michael Han2e397132012-11-26 22:54:45 +00001334 // We can't tell if this is a definition or reference
1335 // until we skipped the 'final' and C++11 attribute specifiers.
1336 TentativeParsingAction PA(*this);
1337
1338 // Skip the 'final' keyword.
1339 ConsumeToken();
1340
1341 // Skip C++11 attribute specifiers.
1342 while (true) {
1343 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1344 ConsumeBracket();
1345 if (!SkipUntil(tok::r_square))
1346 break;
Richard Smith150d8532013-02-22 06:46:23 +00001347 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han2e397132012-11-26 22:54:45 +00001348 ConsumeToken();
1349 ConsumeParen();
1350 if (!SkipUntil(tok::r_paren))
1351 break;
1352 } else {
1353 break;
1354 }
1355 }
1356
1357 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1358 TUK = Sema::TUK_Definition;
1359 else
1360 TUK = Sema::TUK_Reference;
1361
1362 PA.Revert();
Richard Smithc9f35172012-06-25 21:37:02 +00001363 } else if (DSC != DSC_type_specifier &&
1364 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00001365 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallf312b1e2010-08-26 23:41:50 +00001366 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matos17d35c32012-08-31 22:18:20 +00001367 if (Tok.isNot(tok::semi)) {
1368 // A semicolon was missing after this declaration. Diagnose and recover.
1369 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1370 DeclSpec::getSpecifierName(TagType));
1371 PP.EnterToken(Tok);
1372 Tok.setKind(tok::semi);
1373 }
Richard Smithc9f35172012-06-25 21:37:02 +00001374 } else
John McCallf312b1e2010-08-26 23:41:50 +00001375 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001376
Michael Han2e397132012-11-26 22:54:45 +00001377 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1378 // to caller to handle.
Michael Han07fc1ba2013-01-07 16:57:11 +00001379 if (TUK != Sema::TUK_Reference) {
1380 // If this is not a reference, then the only possible
1381 // valid place for C++11 attributes to appear here
1382 // is between class-key and class-name. If there are
1383 // any attributes after class-name, we try a fixit to move
1384 // them to the right place.
1385 SourceRange AttrRange = Attributes.Range;
1386 if (AttrRange.isValid()) {
1387 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1388 << AttrRange
1389 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1390 CharSourceRange(AttrRange, true))
1391 << FixItHint::CreateRemoval(AttrRange);
1392
1393 // Recover by adding misplaced attributes to the attribute list
1394 // of the class so they can be applied on the class later.
1395 attrs.takeAllFrom(Attributes);
1396 }
1397 }
Michael Han2e397132012-11-26 22:54:45 +00001398
John McCall13489672012-05-07 06:16:58 +00001399 // If this is an elaborated type specifier, and we delayed
1400 // diagnostics before, just merge them into the current pool.
1401 if (shouldDelayDiagsInTag) {
1402 diagsFromTag.done();
1403 if (TUK == Sema::TUK_Reference)
1404 diagsFromTag.redelay();
1405 }
1406
John McCall207014e2010-07-30 06:26:29 +00001407 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001408 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001409 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1410 // We have a declaration or reference to an anonymous class.
1411 Diag(StartLoc, diag::err_anon_type_definition)
1412 << DeclSpec::getSpecifierName(TagType);
1413 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001414
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001415 SkipUntil(tok::comma, true);
1416 return;
1417 }
1418
Douglas Gregorddc29e12009-02-06 22:42:48 +00001419 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001420 DeclResult TagOrTempResult = true; // invalid
1421 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001422
Douglas Gregor402abb52009-05-28 23:31:59 +00001423 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001424 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001425 // Explicit specialization, class template partial specialization,
1426 // or explicit instantiation.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001427 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001428 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001429 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001430 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001431 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001432 ProhibitAttributes(attrs);
1433
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001434 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001435 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001436 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001437 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001438 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001439 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001440 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001441 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001442 TemplateId->TemplateNameLoc,
1443 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001444 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001445 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001446 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001447
1448 // Friend template-ids are treated as references unless
1449 // they have template headers, in which case they're ill-formed
1450 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1451 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001452 } else if (TUK == Sema::TUK_Reference ||
1453 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001454 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001455 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001456 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001457 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001458 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001459 TemplateId->Template,
1460 TemplateId->TemplateNameLoc,
1461 TemplateId->LAngleLoc,
1462 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001463 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001464 } else {
1465 // This is an explicit specialization or a class template
1466 // partial specialization.
1467 TemplateParameterLists FakedParamLists;
1468
1469 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1470 // This looks like an explicit instantiation, because we have
1471 // something like
1472 //
1473 // template class Foo<X>
1474 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001475 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001476 // meant to be an explicit specialization, but the user forgot
1477 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +00001478 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001479
Mike Stump1eb44332009-09-09 15:08:12 +00001480 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001481 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001482 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001483 diag::err_explicit_instantiation_with_definition)
1484 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +00001485 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001486
1487 // Create a fake template parameter list that contains only
1488 // "template<>", so that we treat this construct as a class
1489 // template specialization.
1490 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +00001491 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001492 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001493 LAngleLoc,
1494 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001495 LAngleLoc));
1496 TemplateParams = &FakedParamLists;
1497 }
1498
1499 // Build the class template specialization.
1500 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001501 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001502 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001503 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001504 TemplateId->TemplateNameLoc,
1505 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001506 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001507 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001508 attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001509 MultiTemplateParamsArg(
Douglas Gregorcc636682009-02-17 23:15:12 +00001510 TemplateParams? &(*TemplateParams)[0] : 0,
1511 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001512 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001513 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001514 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001515 // Explicit instantiation of a member of a class template
1516 // specialization, e.g.,
1517 //
1518 // template struct Outer<int>::Inner;
1519 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001520 ProhibitAttributes(attrs);
1521
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001522 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001523 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001524 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001525 TemplateInfo.TemplateLoc,
1526 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001527 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001528 } else if (TUK == Sema::TUK_Friend &&
1529 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001530 ProhibitAttributes(attrs);
1531
John McCall9a34edb2010-10-19 01:40:49 +00001532 TagOrTempResult =
1533 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1534 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001535 Name, NameLoc, attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001536 MultiTemplateParamsArg(
John McCall9a34edb2010-10-19 01:40:49 +00001537 TemplateParams? &(*TemplateParams)[0] : 0,
1538 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001539 } else {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001540 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1541 ProhibitAttributes(attrs);
1542
John McCallc4e70192009-09-11 04:59:25 +00001543 bool IsDependent = false;
1544
John McCalla25c4082010-10-19 18:40:57 +00001545 // Don't pass down template parameter lists if this is just a tag
1546 // reference. For example, we don't need the template parameters here:
1547 // template <class T> class A *makeA(T t);
1548 MultiTemplateParamsArg TParams;
1549 if (TUK != Sema::TUK_Reference && TemplateParams)
1550 TParams =
1551 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1552
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001553 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001554 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001555 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001556 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001557 TParams, Owned, IsDependent,
1558 SourceLocation(), false,
1559 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001560
1561 // If ActOnTag said the type was dependent, try again with the
1562 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001563 if (IsDependent) {
1564 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001565 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001566 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001567 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001568 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001569
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001570 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001571 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001572 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001573 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001574 isCXX11FinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001575 if (getLangOpts().CPlusPlus)
Michael Han07fc1ba2013-01-07 16:57:11 +00001576 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1577 TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001578 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001579 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001580 }
1581
John McCallb3d87482010-08-24 05:47:05 +00001582 const char *PrevSpec = 0;
1583 unsigned DiagID;
1584 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001585 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001586 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1587 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001588 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001589 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001590 Result = DS.SetTypeSpecType(TagType, StartLoc,
1591 NameLoc.isValid() ? NameLoc : StartLoc,
1592 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001593 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001594 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001595 return;
1596 }
Mike Stump1eb44332009-09-09 15:08:12 +00001597
John McCallb3d87482010-08-24 05:47:05 +00001598 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001599 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001600
Chris Lattner4ed5d912010-02-02 01:23:29 +00001601 // At this point, we've successfully parsed a class-specifier in 'definition'
1602 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1603 // going to look at what comes after it to improve error recovery. If an
1604 // impossible token occurs next, we assume that the programmer forgot a ; at
1605 // the end of the declaration and recover that way.
1606 //
Richard Smithc9f35172012-06-25 21:37:02 +00001607 // Also enforce C++ [temp]p3:
1608 // In a template-declaration which defines a class, no declarator
1609 // is permitted.
Joao Matos17d35c32012-08-31 22:18:20 +00001610 if (TUK == Sema::TUK_Definition &&
1611 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidis7d033b22012-12-17 20:10:43 +00001612 if (Tok.isNot(tok::semi)) {
1613 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1614 DeclSpec::getSpecifierName(TagType));
1615 // Push this token back into the preprocessor and change our current token
1616 // to ';' so that the rest of the code recovers as though there were an
1617 // ';' after the definition.
1618 PP.EnterToken(Tok);
1619 Tok.setKind(tok::semi);
1620 }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001621 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001622}
1623
Mike Stump1eb44332009-09-09 15:08:12 +00001624/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001625///
1626/// base-clause : [C++ class.derived]
1627/// ':' base-specifier-list
1628/// base-specifier-list:
1629/// base-specifier '...'[opt]
1630/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001631void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001632 assert(Tok.is(tok::colon) && "Not a base clause");
1633 ConsumeToken();
1634
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001635 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001636 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001637
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001638 while (true) {
1639 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001640 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001641 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001642 // Skip the rest of this base specifier, up until the comma or
1643 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001644 SkipUntil(tok::comma, tok::l_brace, true, true);
1645 } else {
1646 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001647 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001648 }
1649
1650 // If the next token is a comma, consume it and keep reading
1651 // base-specifiers.
1652 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001654 // Consume the comma.
1655 ConsumeToken();
1656 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001657
1658 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001659 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001660}
1661
1662/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1663/// one entry in the base class list of a class specifier, for example:
1664/// class foo : public bar, virtual private baz {
1665/// 'public bar' and 'virtual private baz' are each base-specifiers.
1666///
1667/// base-specifier: [C++ class.derived]
Richard Smith05321402013-02-19 23:47:15 +00001668/// attribute-specifier-seq[opt] base-type-specifier
1669/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1670/// base-type-specifier
1671/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1672/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001673Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001674 bool IsVirtual = false;
1675 SourceLocation StartLoc = Tok.getLocation();
1676
Richard Smith05321402013-02-19 23:47:15 +00001677 ParsedAttributesWithRange Attributes(AttrFactory);
1678 MaybeParseCXX11Attributes(Attributes);
1679
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001680 // Parse the 'virtual' keyword.
1681 if (Tok.is(tok::kw_virtual)) {
1682 ConsumeToken();
1683 IsVirtual = true;
1684 }
1685
Richard Smith05321402013-02-19 23:47:15 +00001686 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1687
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001688 // Parse an (optional) access specifier.
1689 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001690 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001691 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001692
Richard Smith05321402013-02-19 23:47:15 +00001693 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1694
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001695 // Parse the 'virtual' keyword (again!), in case it came after the
1696 // access specifier.
1697 if (Tok.is(tok::kw_virtual)) {
1698 SourceLocation VirtualLoc = ConsumeToken();
1699 if (IsVirtual) {
1700 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001701 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001702 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001703 }
1704
1705 IsVirtual = true;
1706 }
1707
Richard Smith05321402013-02-19 23:47:15 +00001708 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1709
Douglas Gregor42a552f2008-11-05 20:51:48 +00001710 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001711 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001712 SourceLocation BaseLoc;
1713 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001714 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001715 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001716
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001717 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1718 // actually part of the base-specifier-list grammar productions, but we
1719 // parse it here for convenience.
1720 SourceLocation EllipsisLoc;
1721 if (Tok.is(tok::ellipsis))
1722 EllipsisLoc = ConsumeToken();
1723
Mike Stump1eb44332009-09-09 15:08:12 +00001724 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001725 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001726
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001727 // Notify semantic analysis that we have parsed a complete
1728 // base-specifier.
Richard Smith05321402013-02-19 23:47:15 +00001729 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1730 Access, BaseType.get(), BaseLoc,
1731 EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001732}
1733
1734/// getAccessSpecifierIfPresent - Determine whether the next token is
1735/// a C++ access-specifier.
1736///
1737/// access-specifier: [C++ class.derived]
1738/// 'private'
1739/// 'protected'
1740/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001741AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001742 switch (Tok.getKind()) {
1743 default: return AS_none;
1744 case tok::kw_private: return AS_private;
1745 case tok::kw_protected: return AS_protected;
1746 case tok::kw_public: return AS_public;
1747 }
1748}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001749
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001750/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001751/// delayed, e.g., default arguments, create a late-parsed method declaration
1752/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001753void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1754 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001755 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001756 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001757 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001758 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001759 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001760
Eli Friedmand33133c2009-07-22 21:45:50 +00001761 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1762 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1763 if (!LateMethod) {
1764 // Push this method onto the stack of late-parsed method
1765 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001766 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1767 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001768 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001769
1770 // Add all of the parameters prior to this one (they don't
1771 // have default arguments).
1772 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1773 for (unsigned I = 0; I < ParamIdx; ++I)
1774 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001775 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001776 }
1777
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001778 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001779 // not have a default argument).
1780 LateMethod->DefaultArgs.push_back(
1781 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1782 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1783 }
1784 }
1785}
1786
Richard Smith4e24f0f2013-01-02 12:01:23 +00001787/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001788/// virt-specifier.
1789///
1790/// virt-specifier:
1791/// override
1792/// final
Richard Smith4e24f0f2013-01-02 12:01:23 +00001793VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001794 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001795 return VirtSpecifiers::VS_None;
1796
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001797 if (Tok.is(tok::identifier)) {
1798 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001799
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001800 // Initialize the contextual keywords.
1801 if (!Ident_final) {
1802 Ident_final = &PP.getIdentifierTable().get("final");
1803 Ident_override = &PP.getIdentifierTable().get("override");
1804 }
1805
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001806 if (II == Ident_override)
1807 return VirtSpecifiers::VS_Override;
1808
1809 if (II == Ident_final)
1810 return VirtSpecifiers::VS_Final;
1811 }
1812
1813 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001814}
1815
Richard Smith4e24f0f2013-01-02 12:01:23 +00001816/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001817///
1818/// virt-specifier-seq:
1819/// virt-specifier
1820/// virt-specifier-seq virt-specifier
Richard Smith4e24f0f2013-01-02 12:01:23 +00001821void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalle402e722012-09-25 07:32:39 +00001822 bool IsInterface) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001823 while (true) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001824 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001825 if (Specifier == VirtSpecifiers::VS_None)
1826 return;
1827
1828 // C++ [class.mem]p8:
1829 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001830 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001831 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001832 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1833 << PrevSpec
1834 << FixItHint::CreateRemoval(Tok.getLocation());
1835
John McCalle402e722012-09-25 07:32:39 +00001836 if (IsInterface && Specifier == VirtSpecifiers::VS_Final) {
1837 Diag(Tok.getLocation(), diag::err_override_control_interface)
1838 << VirtSpecifiers::getSpecifierName(Specifier);
1839 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00001840 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00001841 diag::warn_cxx98_compat_override_control_keyword :
1842 diag::ext_override_control_keyword)
1843 << VirtSpecifiers::getSpecifierName(Specifier);
1844 }
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001845 ConsumeToken();
1846 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001847}
1848
Richard Smith4e24f0f2013-01-02 12:01:23 +00001849/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001850/// contextual 'final' keyword.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001851bool Parser::isCXX11FinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001852 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001853 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001854
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001855 if (!Tok.is(tok::identifier))
1856 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001857
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001858 // Initialize the contextual keywords.
1859 if (!Ident_final) {
1860 Ident_final = &PP.getIdentifierTable().get("final");
1861 Ident_override = &PP.getIdentifierTable().get("override");
1862 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001863
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001864 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001865}
1866
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001867/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1868///
1869/// member-declaration:
1870/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1871/// function-definition ';'[opt]
1872/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1873/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001874/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001875/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001876/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001877///
1878/// member-declarator-list:
1879/// member-declarator
1880/// member-declarator-list ',' member-declarator
1881///
1882/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001883/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001884/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001885/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001886/// identifier[opt] ':' constant-expression
1887///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001888/// virt-specifier-seq:
1889/// virt-specifier
1890/// virt-specifier-seq virt-specifier
1891///
1892/// virt-specifier:
1893/// override
1894/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001895///
Sebastian Redle2b68332009-04-12 17:16:29 +00001896/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001897/// '= 0'
1898///
1899/// constant-initializer:
1900/// '=' constant-expression
1901///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001902void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001903 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001904 const ParsedTemplateInfo &TemplateInfo,
1905 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001906 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001907 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001908 Diag(Tok, diag::err_at_defs_cxx);
1909 else
1910 Diag(Tok, diag::err_at_in_class);
1911
1912 ConsumeToken();
1913 SkipUntil(tok::r_brace);
1914 return;
1915 }
1916
John McCall60fa3cf2009-12-11 02:10:03 +00001917 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001918 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001919 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001920 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1921 if (TryAnnotateCXXScopeToken())
1922 MalformedTypeSpec = true;
1923
1924 bool isAccessDecl;
1925 if (Tok.isNot(tok::annot_cxxscope))
1926 isAccessDecl = false;
1927 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001928 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1929 else
1930 isAccessDecl = NextToken().is(tok::kw_operator);
1931
1932 if (isAccessDecl) {
1933 // Collect the scope specifier token we annotated earlier.
1934 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001935 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1936 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001937
1938 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001939 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001940 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001941 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1942 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001943 SkipUntil(tok::semi);
1944 return;
1945 }
1946
1947 // TODO: recover from mistakenly-qualified operator declarations.
1948 if (ExpectAndConsume(tok::semi,
1949 diag::err_expected_semi_after,
1950 "access declaration",
1951 tok::semi))
1952 return;
1953
Douglas Gregor23c94db2010-07-02 17:43:08 +00001954 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001955 false, SourceLocation(),
1956 SS, Name,
1957 /* AttrList */ 0,
1958 /* IsTypeName */ false,
1959 SourceLocation());
1960 return;
1961 }
1962 }
1963
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001964 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001965 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001966 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001967 SourceLocation DeclEnd;
1968 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001969 return;
1970 }
Mike Stump1eb44332009-09-09 15:08:12 +00001971
Chris Lattner682bf922009-03-29 16:50:03 +00001972 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001973 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001974 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001975 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001976 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001977 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001978 return;
1979 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001980
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001981 // Handle: member-declaration ::= '__extension__' member-declaration
1982 if (Tok.is(tok::kw___extension__)) {
1983 // __extension__ silences extension warnings in the subexpression.
1984 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1985 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001986 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1987 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001988 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001989
Chris Lattner4ed5d912010-02-02 01:23:29 +00001990 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1991 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001992 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001993
John McCall0b7e6782011-03-24 11:26:52 +00001994 ParsedAttributesWithRange attrs(AttrFactory);
Michael Han52b501c2012-11-28 23:17:40 +00001995 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001996 // Optional C++11 attribute-specifier
1997 MaybeParseCXX11Attributes(attrs);
Michael Han52b501c2012-11-28 23:17:40 +00001998 // We need to keep these attributes for future diagnostic
1999 // before they are taken over by declaration specifier.
2000 FnAttrs.addAll(attrs.getList());
2001 FnAttrs.Range = attrs.Range;
2002
John McCall7f040a92010-12-24 02:08:15 +00002003 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002004
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002005 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00002006 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002007
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002008 // Eat 'using'.
2009 SourceLocation UsingLoc = ConsumeToken();
2010
2011 if (Tok.is(tok::kw_namespace)) {
2012 Diag(UsingLoc, diag::err_using_namespace_in_class);
2013 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00002014 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002015 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00002016 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00002017 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2018 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002019 }
2020 return;
2021 }
2022
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002023 // Hold late-parsed attributes so we can attach a Decl to them later.
2024 LateParsedAttrList CommonLateParsedAttrs;
2025
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002026 // decl-specifier-seq:
2027 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00002028 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00002029 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00002030 if (MalformedTypeSpec)
2031 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002032 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2033 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002034
Benjamin Kramer5354e772012-08-23 23:38:35 +00002035 MultiTemplateParamsArg TemplateParams(
John McCalldd4a3b02009-09-16 22:47:08 +00002036 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2037 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2038
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002039 if (Tok.is(tok::semi)) {
2040 ConsumeToken();
Michael Han52b501c2012-11-28 23:17:40 +00002041
2042 if (DS.isFriendSpecified())
2043 ProhibitAttributes(FnAttrs);
2044
John McCalld226f652010-08-21 09:40:31 +00002045 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00002046 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00002047 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00002048 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002049 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002050
John McCall54abf7d2009-11-04 02:18:39 +00002051 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00002052 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002053
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002054 // Hold late-parsed attributes so we can attach a Decl to them later.
2055 LateParsedAttrList LateParsedAttrs;
2056
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002057 SourceLocation EqualLoc;
2058 bool HasInitializer = false;
2059 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002060 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002061 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2062 ColonProtectionRAIIObject X(*this);
2063
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002064 // Parse the first declarator.
2065 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00002066 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00002067 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002068 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00002069 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002070 if (Tok.is(tok::semi))
2071 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002072 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002073 }
2074
Richard Smith4e24f0f2013-01-02 12:01:23 +00002075 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber48673472011-01-28 06:07:34 +00002076
John Thompson1b2fc0f2009-11-25 22:58:06 +00002077 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002078 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00002079
Francois Pichet6a247472011-05-11 02:14:46 +00002080 // MSVC permits pure specifier on inline functions declared at class scope.
2081 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00002082 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00002083 DeclaratorInfo.isFunctionDeclarator() &&
2084 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002085 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00002086 Init = ParseInitializer();
2087 if (Init.isInvalid())
2088 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002089 else
2090 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00002091 }
2092
Douglas Gregor45fa5602011-11-07 20:56:01 +00002093 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002094 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00002095 //
2096 // In C++11, a non-function declarator followed by an open brace is a
2097 // braced-init-list for an in-class member initialization, not an
2098 // erroneous function definition.
Richard Smith80ad52f2013-01-02 11:42:31 +00002099 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002100 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002101 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00002102 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002103 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002104 } else if (Tok.is(tok::equal)) {
2105 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00002106 if (KW.is(tok::kw_default))
2107 DefinitionKind = FDK_Defaulted;
2108 else if (KW.is(tok::kw_delete))
2109 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00002110 }
2111 }
2112
Michael Han52b501c2012-11-28 23:17:40 +00002113 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2114 // to a friend declaration, that declaration shall be a definition.
2115 if (DeclaratorInfo.isFunctionDeclarator() &&
2116 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2117 // Diagnose attributes that appear before decl specifier:
2118 // [[]] friend int foo();
2119 ProhibitAttributes(FnAttrs);
2120 }
2121
Douglas Gregor45fa5602011-11-07 20:56:01 +00002122 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002123 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002124 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002125 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00002126 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Michael Han52b501c2012-11-28 23:17:40 +00002127
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002128 // Consume the optional ';'
2129 if (Tok.is(tok::semi))
2130 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002131 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002132 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002133
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002134 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002135 Diag(DeclaratorInfo.getIdentifierLoc(),
2136 diag::err_function_declared_typedef);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002137
Richard Smith6f9a4452012-11-15 22:54:20 +00002138 // Recover by treating the 'typedef' as spurious.
2139 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002140 }
2141
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002142 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002143 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00002144 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002145
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002146 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2147 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2148 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002149 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002150 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002151 }
2152 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00002153
2154 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002155 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00002156 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002157
Chris Lattner682bf922009-03-29 16:50:03 +00002158 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002159 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002160 }
2161
2162 // member-declarator-list:
2163 // member-declarator
2164 // member-declarator-list ',' member-declarator
2165
Chris Lattner5f9e2722011-07-23 10:55:15 +00002166 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00002167 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00002168 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002169
2170 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002171 // member-declarator:
2172 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002173 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002174 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002175 if (Tok.is(tok::colon)) {
2176 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002177 BitfieldSize = ParseConstantExpression();
2178 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002179 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002180 }
Mike Stump1eb44332009-09-09 15:08:12 +00002181
Chris Lattnere6563252010-06-13 05:34:18 +00002182 // If a simple-asm-expr is present, parse it.
2183 if (Tok.is(tok::kw_asm)) {
2184 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002185 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002186 if (AsmLabel.isInvalid())
2187 SkipUntil(tok::comma, true, true);
2188
2189 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2190 DeclaratorInfo.SetRangeEnd(Loc);
2191 }
2192
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002193 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002194 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002195
Richard Smith7a614d82011-06-11 17:19:42 +00002196 // FIXME: When g++ adds support for this, we'll need to check whether it
2197 // goes before or after the GNU attributes and __asm__.
Richard Smith4e24f0f2013-01-02 12:01:23 +00002198 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith7a614d82011-06-11 17:19:42 +00002199
Richard Smithca523302012-06-10 03:12:00 +00002200 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002201 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002202 if (BitfieldSize.get()) {
2203 Diag(Tok, diag::err_bitfield_member_init);
2204 SkipUntil(tok::comma, true, true);
2205 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002206 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002207 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2208 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smithca523302012-06-10 03:12:00 +00002209 != DeclSpec::SCS_typedef)
2210 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002211 }
2212 }
2213
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002214 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002215 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002216 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002217
Rafael Espindolafc35cbc2013-01-08 20:44:06 +00002218 NamedDecl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002219 if (DS.isFriendSpecified()) {
Michael Han52b501c2012-11-28 23:17:40 +00002220 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2221 // to a friend declaration, that declaration shall be a definition.
2222 //
2223 // Diagnose attributes appear after friend member function declarator:
2224 // foo [[]] ();
2225 SmallVector<SourceRange, 4> Ranges;
2226 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2227 if (!Ranges.empty()) {
2228 for (SmallVector<SourceRange, 4>::iterator I = Ranges.begin(),
2229 E = Ranges.end(); I != E; ++I) {
2230 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2231 << *I;
2232 }
2233 }
2234
John McCallbbbcdd92009-09-11 21:02:39 +00002235 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002236 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002237 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002238 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002239 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002240 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002241 TemplateParams,
John McCall67d1a672009-08-06 02:15:43 +00002242 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002243 VS, HasInClassInit);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002244 if (AccessAttrs)
2245 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2246 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002247 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002248
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002249 // Set the Decl for any late parsed attributes
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002250 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2251 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2252 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002253 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002254 LateParsedAttrs[i]->addDecl(ThisDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002255 }
2256 LateParsedAttrs.clear();
2257
Douglas Gregor147545d2011-10-10 14:49:18 +00002258 // Handle the initializer.
David Blaikie1d87fba2013-01-30 01:22:18 +00002259 if (HasInClassInit != ICIS_NoInit &&
2260 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2261 DeclSpec::SCS_static) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002262 // The initializer was deferred; parse it and cache the tokens.
Richard Smith80ad52f2013-01-02 11:42:31 +00002263 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00002264 diag::warn_cxx98_compat_nonstatic_member_init :
2265 diag::ext_nonstatic_member_init);
2266
Richard Smith7a614d82011-06-11 17:19:42 +00002267 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002268 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2269 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002270 //
2271 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002272 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002273 Diag(Tok, diag::err_incomplete_array_member_init);
2274 SkipUntil(tok::comma, true, true);
David Blaikie3164c142012-02-14 09:00:46 +00002275 if (ThisDecl)
2276 // Avoid later warnings about a class member of incomplete type.
2277 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002278 } else
2279 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002280 } else if (HasInitializer) {
2281 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002282 if (!Init.isUsable())
Douglas Gregor552e2992012-02-21 02:22:07 +00002283 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002284 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2285
Douglas Gregor147545d2011-10-10 14:49:18 +00002286 if (Init.isInvalid())
2287 SkipUntil(tok::comma, true, true);
2288 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002289 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smitha2c36462013-04-26 16:15:35 +00002290 DS.containsPlaceholderType());
Douglas Gregor147545d2011-10-10 14:49:18 +00002291 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2292 // No initializer.
Richard Smitha2c36462013-04-26 16:15:35 +00002293 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
Richard Smith7a614d82011-06-11 17:19:42 +00002294 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002295
2296 if (ThisDecl) {
2297 Actions.FinalizeDeclaration(ThisDecl);
2298 DeclsInGroup.push_back(ThisDecl);
2299 }
2300
Richard Smithe5310012012-04-29 07:31:09 +00002301 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor147545d2011-10-10 14:49:18 +00002302 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2303 != DeclSpec::SCS_typedef) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002304 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002305 }
2306
2307 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002308
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002309 // If we don't have a comma, it is either the end of the list (a ';')
2310 // or an error, bail out.
2311 if (Tok.isNot(tok::comma))
2312 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002313
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002314 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002315 SourceLocation CommaLoc = ConsumeToken();
2316
2317 if (Tok.isAtStartOfLine() &&
2318 !MightBeDeclarator(Declarator::MemberContext)) {
2319 // This comma was followed by a line-break and something which can't be
2320 // the start of a declarator. The comma was probably a typo for a
2321 // semicolon.
2322 Diag(CommaLoc, diag::err_expected_semi_declaration)
2323 << FixItHint::CreateReplacement(CommaLoc, ";");
2324 ExpectSemi = false;
2325 break;
2326 }
Mike Stump1eb44332009-09-09 15:08:12 +00002327
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002328 // Parse the next declarator.
2329 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002330 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002331 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002332 Init = true;
2333 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002334 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002335
Bill Wendlingad017fa2012-12-20 19:22:21 +00002336 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002337 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002338
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002339 if (Tok.isNot(tok::colon))
2340 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002341 }
2342
Richard Smith1c94c162012-01-09 22:31:44 +00002343 if (ExpectSemi &&
2344 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002345 // Skip to end of block or statement.
2346 SkipUntil(tok::r_brace, true, true);
2347 // If we stopped at a ';', eat it.
2348 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002349 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002350 }
2351
Douglas Gregor23c94db2010-07-02 17:43:08 +00002352 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00002353 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002354}
2355
Richard Smith7a614d82011-06-11 17:19:42 +00002356/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2357/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2358/// function definition. The location of the '=', if any, will be placed in
2359/// EqualLoc.
2360///
2361/// pure-specifier:
2362/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002363///
Richard Smith7a614d82011-06-11 17:19:42 +00002364/// brace-or-equal-initializer:
2365/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002366/// braced-init-list
2367///
Richard Smith7a614d82011-06-11 17:19:42 +00002368/// initializer-clause:
2369/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002370/// braced-init-list
2371///
Richard Smith7a614d82011-06-11 17:19:42 +00002372/// defaulted/deleted function-definition:
2373/// '=' 'default'
2374/// '=' 'delete'
2375///
2376/// Prior to C++0x, the assignment-expression in an initializer-clause must
2377/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002378ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002379 SourceLocation &EqualLoc) {
2380 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2381 && "Data member initializer not starting with '=' or '{'");
2382
Douglas Gregor552e2992012-02-21 02:22:07 +00002383 EnterExpressionEvaluationContext Context(Actions,
2384 Sema::PotentiallyEvaluated,
2385 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002386 if (Tok.is(tok::equal)) {
2387 EqualLoc = ConsumeToken();
2388 if (Tok.is(tok::kw_delete)) {
2389 // In principle, an initializer of '= delete p;' is legal, but it will
2390 // never type-check. It's better to diagnose it as an ill-formed expression
2391 // than as an ill-formed deleted non-function member.
2392 // An initializer of '= delete p, foo' will never be parsed, because
2393 // a top-level comma always ends the initializer expression.
2394 const Token &Next = NextToken();
2395 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2396 Next.is(tok::eof)) {
2397 if (IsFunction)
2398 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2399 << 1 /* delete */;
2400 else
2401 Diag(ConsumeToken(), diag::err_deleted_non_function);
2402 return ExprResult();
2403 }
2404 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002405 if (IsFunction)
2406 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2407 << 0 /* default */;
2408 else
2409 Diag(ConsumeToken(), diag::err_default_special_members);
2410 return ExprResult();
2411 }
2412
Sebastian Redl33deb352012-02-22 10:50:08 +00002413 }
2414 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002415}
2416
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002417/// ParseCXXMemberSpecification - Parse the class definition.
2418///
2419/// member-specification:
2420/// member-declaration member-specification[opt]
2421/// access-specifier ':' member-specification[opt]
2422///
Joao Matos17d35c32012-08-31 22:18:20 +00002423void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han07fc1ba2013-01-07 16:57:11 +00002424 SourceLocation AttrFixitLoc,
Richard Smith05321402013-02-19 23:47:15 +00002425 ParsedAttributesWithRange &Attrs,
Joao Matos17d35c32012-08-31 22:18:20 +00002426 unsigned TagType, Decl *TagDecl) {
2427 assert((TagType == DeclSpec::TST_struct ||
2428 TagType == DeclSpec::TST_interface ||
2429 TagType == DeclSpec::TST_union ||
2430 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2431
John McCallf312b1e2010-08-26 23:41:50 +00002432 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2433 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002434
Douglas Gregor26997fd2010-01-16 20:52:59 +00002435 // Determine whether this is a non-nested class. Note that local
2436 // classes are *not* considered to be nested classes.
2437 bool NonNestedClass = true;
2438 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002439 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002440 if (S->isClassScope()) {
2441 // We're inside a class scope, so this is a nested class.
2442 NonNestedClass = false;
John McCalle402e722012-09-25 07:32:39 +00002443
2444 // The Microsoft extension __interface does not permit nested classes.
2445 if (getCurrentClass().IsInterface) {
2446 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2447 << /*ErrorType=*/6
2448 << (isa<NamedDecl>(TagDecl)
2449 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2450 : "<anonymous>");
2451 }
Douglas Gregor26997fd2010-01-16 20:52:59 +00002452 break;
2453 }
2454
2455 if ((S->getFlags() & Scope::FnScope)) {
2456 // If we're in a function or function template declared in the
2457 // body of a class, then this is a local class rather than a
2458 // nested class.
2459 const Scope *Parent = S->getParent();
2460 if (Parent->isTemplateParamScope())
2461 Parent = Parent->getParent();
2462 if (Parent->isClassScope())
2463 break;
2464 }
2465 }
2466 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002467
2468 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002469 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002470
Douglas Gregor6569d682009-05-27 23:11:45 +00002471 // Note that we are parsing a new (potentially-nested) class definition.
John McCalle402e722012-09-25 07:32:39 +00002472 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2473 TagType == DeclSpec::TST_interface);
Douglas Gregor6569d682009-05-27 23:11:45 +00002474
Douglas Gregorddc29e12009-02-06 22:42:48 +00002475 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002476 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002477
Anders Carlssonb184a182011-03-25 14:46:08 +00002478 SourceLocation FinalLoc;
2479
2480 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002481 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00002482 assert(isCXX11FinalKeyword() && "not a class definition");
Richard Smith8b11b5e2011-10-15 04:21:46 +00002483 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002484
John McCalle402e722012-09-25 07:32:39 +00002485 if (TagType == DeclSpec::TST_interface) {
2486 Diag(FinalLoc, diag::err_override_control_interface)
2487 << "final";
2488 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00002489 Diag(FinalLoc, getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00002490 diag::warn_cxx98_compat_override_control_keyword :
2491 diag::ext_override_control_keyword) << "final";
2492 }
Michael Han2e397132012-11-26 22:54:45 +00002493
Michael Han07fc1ba2013-01-07 16:57:11 +00002494 // Parse any C++11 attributes after 'final' keyword.
2495 // These attributes are not allowed to appear here,
2496 // and the only possible place for them to appertain
2497 // to the class would be between class-key and class-name.
Richard Smith05321402013-02-19 23:47:15 +00002498 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonb184a182011-03-25 14:46:08 +00002499 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002500
John McCallbd0dfa52009-12-19 21:48:58 +00002501 if (Tok.is(tok::colon)) {
2502 ParseBaseClause(TagDecl);
2503
2504 if (!Tok.is(tok::l_brace)) {
2505 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002506
2507 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002508 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002509 return;
2510 }
2511 }
2512
2513 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002514 BalancedDelimiterTracker T(*this, tok::l_brace);
2515 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002516
John McCall42a4f662010-05-28 08:11:17 +00002517 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002518 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002519 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002520
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002521 // C++ 11p3: Members of a class defined with the keyword class are private
2522 // by default. Members of a class defined with the keywords struct or union
2523 // are public by default.
2524 AccessSpecifier CurAS;
2525 if (TagType == DeclSpec::TST_class)
2526 CurAS = AS_private;
2527 else
2528 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002529 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002530
Douglas Gregor07976d22010-06-21 22:31:09 +00002531 if (TagDecl) {
2532 // While we still have something to read, read the member-declarations.
2533 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2534 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002535
David Blaikie4e4d0842012-03-11 07:00:24 +00002536 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002537 Tok.is(tok::kw___if_not_exists))) {
2538 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2539 continue;
2540 }
2541
Douglas Gregor07976d22010-06-21 22:31:09 +00002542 // Check for extraneous top-level semicolon.
2543 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002544 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002545 continue;
2546 }
2547
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002548 if (Tok.is(tok::annot_pragma_vis)) {
2549 HandlePragmaVisibility();
2550 continue;
2551 }
2552
2553 if (Tok.is(tok::annot_pragma_pack)) {
2554 HandlePragmaPack();
2555 continue;
2556 }
2557
Argyrios Kyrtzidisf4deaef2012-10-12 17:39:59 +00002558 if (Tok.is(tok::annot_pragma_align)) {
2559 HandlePragmaAlign();
2560 continue;
2561 }
2562
Alexey Bataevc6400582013-03-22 06:34:35 +00002563 if (Tok.is(tok::annot_pragma_openmp)) {
2564 ParseOpenMPDeclarativeDirective();
2565 continue;
2566 }
2567
Douglas Gregor07976d22010-06-21 22:31:09 +00002568 AccessSpecifier AS = getAccessSpecifierIfPresent();
2569 if (AS != AS_none) {
2570 // Current token is a C++ access specifier.
2571 CurAS = AS;
2572 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002573 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002574 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002575 AccessAttrs.clear();
2576 MaybeParseGNUAttributes(AccessAttrs);
2577
David Blaikie13f8daf2011-10-13 06:08:43 +00002578 SourceLocation EndLoc;
2579 if (Tok.is(tok::colon)) {
2580 EndLoc = Tok.getLocation();
2581 ConsumeToken();
2582 } else if (Tok.is(tok::semi)) {
2583 EndLoc = Tok.getLocation();
2584 ConsumeToken();
2585 Diag(EndLoc, diag::err_expected_colon)
2586 << FixItHint::CreateReplacement(EndLoc, ":");
2587 } else {
2588 EndLoc = ASLoc.getLocWithOffset(TokLength);
2589 Diag(EndLoc, diag::err_expected_colon)
2590 << FixItHint::CreateInsertion(EndLoc, ":");
2591 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002592
John McCalle402e722012-09-25 07:32:39 +00002593 // The Microsoft extension __interface does not permit non-public
2594 // access specifiers.
2595 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2596 Diag(ASLoc, diag::err_access_specifier_interface)
2597 << (CurAS == AS_protected);
2598 }
2599
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002600 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2601 AccessAttrs.getList())) {
2602 // found another attribute than only annotations
2603 AccessAttrs.clear();
2604 }
2605
Douglas Gregor07976d22010-06-21 22:31:09 +00002606 continue;
2607 }
2608
2609 // FIXME: Make sure we don't have a template here.
2610
2611 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002612 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002613 }
2614
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002615 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002616 } else {
2617 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002618 }
Mike Stump1eb44332009-09-09 15:08:12 +00002619
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002620 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002621 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002622 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002623
John McCall42a4f662010-05-28 08:11:17 +00002624 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002625 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002626 T.getOpenLocation(),
2627 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002628 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002629
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002630 // C++11 [class.mem]p2:
2631 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002632 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002633 // brace-or-equal-initializers for non-static data members (including such
2634 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002635 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002636 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002637 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002638 // declarations and the lexed inline method definitions, along with any
2639 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002640 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002641 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002642 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002643
2644 // We've finished with all pending member declarations.
2645 Actions.ActOnFinishCXXMemberDecls();
2646
Richard Smith7a614d82011-06-11 17:19:42 +00002647 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002648 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002649 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002650 }
2651
John McCall42a4f662010-05-28 08:11:17 +00002652 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002653 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2654 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002655
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002656 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002657 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002658 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002659}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002660
2661/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2662/// which explicitly initializes the members or base classes of a
2663/// class (C++ [class.base.init]). For example, the three initializers
2664/// after the ':' in the Derived constructor below:
2665///
2666/// @code
2667/// class Base { };
2668/// class Derived : Base {
2669/// int x;
2670/// float f;
2671/// public:
2672/// Derived(float f) : Base(), x(17), f(f) { }
2673/// };
2674/// @endcode
2675///
Mike Stump1eb44332009-09-09 15:08:12 +00002676/// [C++] ctor-initializer:
2677/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002678///
Mike Stump1eb44332009-09-09 15:08:12 +00002679/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002680/// mem-initializer ...[opt]
2681/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002682void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002683 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2684
John Wiegley28bbe4b2011-04-28 01:08:34 +00002685 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2686 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002687 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002688
Chris Lattner5f9e2722011-07-23 10:55:15 +00002689 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002690 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002691
Douglas Gregor7ad83902008-11-05 04:29:56 +00002692 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002693 if (Tok.is(tok::code_completion)) {
2694 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2695 MemInitializers.data(),
2696 MemInitializers.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002697 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002698 } else {
2699 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2700 if (!MemInit.isInvalid())
2701 MemInitializers.push_back(MemInit.get());
2702 else
2703 AnyErrors = true;
2704 }
2705
Douglas Gregor7ad83902008-11-05 04:29:56 +00002706 if (Tok.is(tok::comma))
2707 ConsumeToken();
2708 else if (Tok.is(tok::l_brace))
2709 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002710 // If the next token looks like a base or member initializer, assume that
2711 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002712 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2713 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2714 Diag(Loc, diag::err_ctor_init_missing_comma)
2715 << FixItHint::CreateInsertion(Loc, ", ");
2716 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002717 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002718 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002719 SkipUntil(tok::l_brace, true, true);
2720 break;
2721 }
2722 } while (true);
2723
David Blaikie93c86172013-01-17 05:26:25 +00002724 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002725 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002726}
2727
2728/// ParseMemInitializer - Parse a C++ member initializer, which is
2729/// part of a constructor initializer that explicitly initializes one
2730/// member or base class (C++ [class.base.init]). See
2731/// ParseConstructorInitializer for an example.
2732///
2733/// [C++] mem-initializer:
2734/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002735/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002736///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002737/// [C++] mem-initializer-id:
2738/// '::'[opt] nested-name-specifier[opt] class-name
2739/// identifier
John McCalld226f652010-08-21 09:40:31 +00002740Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002741 // parse '::'[opt] nested-name-specifier[opt]
2742 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002743 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002744 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002745 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002746 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002747 if (TemplateId->Kind == TNK_Type_template ||
2748 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002749 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002750 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002751 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002752 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002753 }
David Blaikief2116622012-01-24 06:03:59 +00002754 // Uses of decltype will already have been converted to annot_decltype by
2755 // ParseOptionalCXXScopeSpecifier at this point.
2756 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2757 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002758 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002759 return true;
2760 }
Mike Stump1eb44332009-09-09 15:08:12 +00002761
David Blaikief2116622012-01-24 06:03:59 +00002762 IdentifierInfo *II = 0;
2763 DeclSpec DS(AttrFactory);
2764 SourceLocation IdLoc = Tok.getLocation();
2765 if (Tok.is(tok::annot_decltype)) {
2766 // Get the decltype expression, if there is one.
2767 ParseDecltypeSpecifier(DS);
2768 } else {
2769 if (Tok.is(tok::identifier))
2770 // Get the identifier. This may be a member name or a class name,
2771 // but we'll let the semantic analysis determine which it is.
2772 II = Tok.getIdentifierInfo();
2773 ConsumeToken();
2774 }
2775
Douglas Gregor7ad83902008-11-05 04:29:56 +00002776
2777 // Parse the '('.
Richard Smith80ad52f2013-01-02 11:42:31 +00002778 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002779 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2780
Sebastian Redl6df65482011-09-24 17:48:25 +00002781 ExprResult InitList = ParseBraceInitializer();
2782 if (InitList.isInvalid())
2783 return true;
2784
2785 SourceLocation EllipsisLoc;
2786 if (Tok.is(tok::ellipsis))
2787 EllipsisLoc = ConsumeToken();
2788
2789 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002790 TemplateTypeTy, DS, IdLoc,
2791 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002792 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002793 BalancedDelimiterTracker T(*this, tok::l_paren);
2794 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002795
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002796 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002797 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002798 CommaLocsTy CommaLocs;
2799 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2800 SkipUntil(tok::r_paren);
2801 return true;
2802 }
2803
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002804 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002805
2806 SourceLocation EllipsisLoc;
2807 if (Tok.is(tok::ellipsis))
2808 EllipsisLoc = ConsumeToken();
2809
2810 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002811 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenkoa36bbac2013-05-09 23:51:52 +00002812 T.getOpenLocation(), ArgExprs,
2813 T.getCloseLocation(), EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002814 }
2815
Richard Smith80ad52f2013-01-02 11:42:31 +00002816 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002817 : diag::err_expected_lparen);
2818 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002819}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002820
Sebastian Redl7acafd02011-03-05 14:45:16 +00002821/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002822///
Douglas Gregora4745612008-12-01 18:00:20 +00002823/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002824/// dynamic-exception-specification
2825/// noexcept-specification
2826///
2827/// noexcept-specification:
2828/// 'noexcept'
2829/// 'noexcept' '(' constant-expression ')'
2830ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002831Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002832 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002833 SmallVectorImpl<ParsedType> &DynamicExceptions,
2834 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002835 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002836 ExceptionSpecificationType Result = EST_None;
2837
2838 // See if there's a dynamic specification.
2839 if (Tok.is(tok::kw_throw)) {
2840 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2841 DynamicExceptions,
2842 DynamicExceptionRanges);
2843 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2844 "Produced different number of exception types and ranges.");
2845 }
2846
2847 // If there's no noexcept specification, we're done.
2848 if (Tok.isNot(tok::kw_noexcept))
2849 return Result;
2850
Richard Smith841804b2011-10-17 23:06:20 +00002851 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2852
Sebastian Redl7acafd02011-03-05 14:45:16 +00002853 // If we already had a dynamic specification, parse the noexcept for,
2854 // recovery, but emit a diagnostic and don't store the results.
2855 SourceRange NoexceptRange;
2856 ExceptionSpecificationType NoexceptType = EST_None;
2857
2858 SourceLocation KeywordLoc = ConsumeToken();
2859 if (Tok.is(tok::l_paren)) {
2860 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002861 BalancedDelimiterTracker T(*this, tok::l_paren);
2862 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002863 NoexceptType = EST_ComputedNoexcept;
2864 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002865 // The argument must be contextually convertible to bool. We use
2866 // ActOnBooleanCondition for this purpose.
2867 if (!NoexceptExpr.isInvalid())
2868 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2869 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002870 T.consumeClose();
2871 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002872 } else {
2873 // There is no argument.
2874 NoexceptType = EST_BasicNoexcept;
2875 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2876 }
2877
2878 if (Result == EST_None) {
2879 SpecificationRange = NoexceptRange;
2880 Result = NoexceptType;
2881
2882 // If there's a dynamic specification after a noexcept specification,
2883 // parse that and ignore the results.
2884 if (Tok.is(tok::kw_throw)) {
2885 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2886 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2887 DynamicExceptionRanges);
2888 }
2889 } else {
2890 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2891 }
2892
2893 return Result;
2894}
2895
Richard Smith79f4bb72013-06-13 02:02:51 +00002896static void diagnoseDynamicExceptionSpecification(
2897 Parser &P, const SourceRange &Range, bool IsNoexcept) {
2898 if (P.getLangOpts().CPlusPlus11) {
2899 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
2900 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
2901 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
2902 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
2903 }
2904}
2905
Sebastian Redl7acafd02011-03-05 14:45:16 +00002906/// ParseDynamicExceptionSpecification - Parse a C++
2907/// dynamic-exception-specification (C++ [except.spec]).
2908///
2909/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002910/// 'throw' '(' type-id-list [opt] ')'
2911/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002912///
Douglas Gregora4745612008-12-01 18:00:20 +00002913/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002914/// type-id ... [opt]
2915/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002916///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002917ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2918 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002919 SmallVectorImpl<ParsedType> &Exceptions,
2920 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002921 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002922
Sebastian Redl7acafd02011-03-05 14:45:16 +00002923 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002924 BalancedDelimiterTracker T(*this, tok::l_paren);
2925 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002926 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2927 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002928 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002929 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002930
Douglas Gregora4745612008-12-01 18:00:20 +00002931 // Parse throw(...), a Microsoft extension that means "this function
2932 // can throw anything".
2933 if (Tok.is(tok::ellipsis)) {
2934 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002935 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002936 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002937 T.consumeClose();
2938 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith79f4bb72013-06-13 02:02:51 +00002939 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redl60618fa2011-03-12 11:50:43 +00002940 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002941 }
2942
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002943 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002944 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002945 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002946 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002947
Douglas Gregora04426c2010-12-20 23:57:46 +00002948 if (Tok.is(tok::ellipsis)) {
2949 // C++0x [temp.variadic]p5:
2950 // - In a dynamic-exception-specification (15.4); the pattern is a
2951 // type-id.
2952 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002953 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002954 if (!Res.isInvalid())
2955 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2956 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002957
Sebastian Redlef65f062009-05-29 18:02:33 +00002958 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002959 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002960 Ranges.push_back(Range);
2961 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002962
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002963 if (Tok.is(tok::comma))
2964 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002965 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002966 break;
2967 }
2968
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002969 T.consumeClose();
2970 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith79f4bb72013-06-13 02:02:51 +00002971 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
2972 Exceptions.empty());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002973 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002974}
Douglas Gregor6569d682009-05-27 23:11:45 +00002975
Douglas Gregordab60ad2010-10-01 18:44:50 +00002976/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2977/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002978TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002979 assert(Tok.is(tok::arrow) && "expected arrow");
2980
2981 ConsumeToken();
2982
Richard Smith7796eb52012-03-12 08:56:40 +00002983 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002984}
2985
Douglas Gregor6569d682009-05-27 23:11:45 +00002986/// \brief We have just started parsing the definition of a new class,
2987/// so push that class onto our stack of classes that is currently
2988/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002989Sema::ParsingClassState
John McCalle402e722012-09-25 07:32:39 +00002990Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
2991 bool IsInterface) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002992 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002993 "Nested class without outer class");
John McCalle402e722012-09-25 07:32:39 +00002994 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCalleee1d542011-02-14 07:13:47 +00002995 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002996}
2997
2998/// \brief Deallocate the given parsed class and all of its nested
2999/// classes.
3000void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00003001 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3002 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00003003 delete Class;
3004}
3005
3006/// \brief Pop the top class of the stack of classes that are
3007/// currently being parsed.
3008///
3009/// This routine should be called when we have finished parsing the
3010/// definition of a class, but have not yet popped the Scope
3011/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00003012void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00003013 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00003014
John McCalleee1d542011-02-14 07:13:47 +00003015 Actions.PopParsingClass(state);
3016
Douglas Gregor6569d682009-05-27 23:11:45 +00003017 ParsingClass *Victim = ClassStack.top();
3018 ClassStack.pop();
3019 if (Victim->TopLevelClass) {
3020 // Deallocate all of the nested classes of this class,
3021 // recursively: we don't need to keep any of this information.
3022 DeallocateParsedClasses(Victim);
3023 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003024 }
Douglas Gregor6569d682009-05-27 23:11:45 +00003025 assert(!ClassStack.empty() && "Missing top-level class?");
3026
Douglas Gregord54eb442010-10-12 16:25:54 +00003027 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00003028 // The victim is a nested class, but we will not need to perform
3029 // any processing after the definition of this class since it has
3030 // no members whose handling was delayed. Therefore, we can just
3031 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00003032 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00003033 return;
3034 }
3035
3036 // This nested class has some members that will need to be processed
3037 // after the top-level class is completely defined. Therefore, add
3038 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003039 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00003040 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00003041 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00003042}
Sean Huntbbd37c62009-11-21 08:43:09 +00003043
Richard Smithc56298d2012-04-10 03:25:07 +00003044/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3045///
3046/// \return the parsed identifier on success, and 0 if the next token is not an
3047/// attribute-token.
3048///
3049/// C++11 [dcl.attr.grammar]p3:
3050/// If a keyword or an alternative token that satisfies the syntactic
3051/// requirements of an identifier is contained in an attribute-token,
3052/// it is considered an identifier.
3053IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3054 switch (Tok.getKind()) {
3055 default:
3056 // Identifiers and keywords have identifier info attached.
3057 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3058 Loc = ConsumeToken();
3059 return II;
3060 }
3061 return 0;
3062
3063 case tok::ampamp: // 'and'
3064 case tok::pipe: // 'bitor'
3065 case tok::pipepipe: // 'or'
3066 case tok::caret: // 'xor'
3067 case tok::tilde: // 'compl'
3068 case tok::amp: // 'bitand'
3069 case tok::ampequal: // 'and_eq'
3070 case tok::pipeequal: // 'or_eq'
3071 case tok::caretequal: // 'xor_eq'
3072 case tok::exclaim: // 'not'
3073 case tok::exclaimequal: // 'not_eq'
3074 // Alternative tokens do not have identifier info, but their spelling
3075 // starts with an alphabetical character.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003076 SmallString<8> SpellingBuf;
Richard Smithc56298d2012-04-10 03:25:07 +00003077 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rose3f6f51e2013-02-08 22:30:41 +00003078 if (isLetter(Spelling[0])) {
Richard Smithc56298d2012-04-10 03:25:07 +00003079 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00003080 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00003081 }
3082 return 0;
3083 }
3084}
3085
Michael Han6880f492012-10-03 01:56:22 +00003086static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3087 IdentifierInfo *ScopeName) {
3088 switch (AttributeList::getKind(AttrName, ScopeName,
3089 AttributeList::AS_CXX11)) {
3090 case AttributeList::AT_CarriesDependency:
3091 case AttributeList::AT_FallThrough:
Richard Smithcd8ab512013-01-17 01:30:42 +00003092 case AttributeList::AT_CXX11NoReturn: {
Michael Han6880f492012-10-03 01:56:22 +00003093 return true;
3094 }
3095
3096 default:
3097 return false;
3098 }
3099}
3100
Richard Smithc56298d2012-04-10 03:25:07 +00003101/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003102/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00003103///
Richard Smith6ee326a2012-04-10 01:32:12 +00003104/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00003105/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003106/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00003107///
Richard Smith6ee326a2012-04-10 01:32:12 +00003108/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00003109/// attribute[opt]
3110/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00003111/// attribute '...'
3112/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00003113///
Richard Smith6ee326a2012-04-10 01:32:12 +00003114/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00003115/// attribute-token attribute-argument-clause[opt]
3116///
Richard Smith6ee326a2012-04-10 01:32:12 +00003117/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003118/// identifier
3119/// attribute-scoped-token
3120///
Richard Smith6ee326a2012-04-10 01:32:12 +00003121/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003122/// attribute-namespace '::' identifier
3123///
Richard Smith6ee326a2012-04-10 01:32:12 +00003124/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00003125/// identifier
3126///
Richard Smith6ee326a2012-04-10 01:32:12 +00003127/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00003128/// '(' balanced-token-seq ')'
3129///
Richard Smith6ee326a2012-04-10 01:32:12 +00003130/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00003131/// balanced-token
3132/// balanced-token-seq balanced-token
3133///
Richard Smith6ee326a2012-04-10 01:32:12 +00003134/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003135/// '(' balanced-token-seq ')'
3136/// '[' balanced-token-seq ']'
3137/// '{' balanced-token-seq '}'
3138/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00003139void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003140 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003141 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00003142 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003143 ParseAlignmentSpecifier(attrs, endLoc);
3144 return;
3145 }
3146
Sean Huntbbd37c62009-11-21 08:43:09 +00003147 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00003148 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00003149
Richard Smith41be6732011-10-14 20:48:27 +00003150 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3151
Sean Huntbbd37c62009-11-21 08:43:09 +00003152 ConsumeBracket();
3153 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003154
Richard Smithcd8ab512013-01-17 01:30:42 +00003155 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3156
Richard Smithc56298d2012-04-10 03:25:07 +00003157 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003158 // attribute not present
3159 if (Tok.is(tok::comma)) {
3160 ConsumeToken();
3161 continue;
3162 }
3163
Richard Smithc56298d2012-04-10 03:25:07 +00003164 SourceLocation ScopeLoc, AttrLoc;
3165 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3166
3167 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3168 if (!AttrName)
3169 // Break out to the "expected ']'" diagnostic.
3170 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003171
Sean Huntbbd37c62009-11-21 08:43:09 +00003172 // scoped attribute
3173 if (Tok.is(tok::coloncolon)) {
3174 ConsumeToken();
3175
Richard Smithc56298d2012-04-10 03:25:07 +00003176 ScopeName = AttrName;
3177 ScopeLoc = AttrLoc;
3178
3179 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3180 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003181 Diag(Tok.getLocation(), diag::err_expected_ident);
3182 SkipUntil(tok::r_square, tok::comma, true, true);
3183 continue;
3184 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003185 }
3186
Michael Han6880f492012-10-03 01:56:22 +00003187 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Sean Huntbbd37c62009-11-21 08:43:09 +00003188 bool AttrParsed = false;
Sean Huntbbd37c62009-11-21 08:43:09 +00003189
Richard Smithcd8ab512013-01-17 01:30:42 +00003190 if (StandardAttr &&
3191 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3192 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3193 << AttrName << SourceRange(SeenAttrs[AttrName]);
3194
Michael Han6880f492012-10-03 01:56:22 +00003195 // Parse attribute arguments
3196 if (Tok.is(tok::l_paren)) {
3197 if (ScopeName && ScopeName->getName() == "gnu") {
3198 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3199 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3200 AttrParsed = true;
3201 } else {
3202 if (StandardAttr)
3203 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3204 << AttrName->getName();
3205
3206 // FIXME: handle other formats of c++11 attribute arguments
3207 ConsumeParen();
3208 SkipUntil(tok::r_paren, false);
3209 }
3210 }
3211
3212 if (!AttrParsed)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00003213 attrs.addNew(AttrName,
3214 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3215 AttrLoc),
3216 ScopeName, ScopeLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00003217 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smith6ee326a2012-04-10 01:32:12 +00003218
Richard Smithc56298d2012-04-10 03:25:07 +00003219 if (Tok.is(tok::ellipsis)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003220 ConsumeToken();
Michael Han6880f492012-10-03 01:56:22 +00003221
3222 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3223 << AttrName->getName();
Richard Smithc56298d2012-04-10 03:25:07 +00003224 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003225 }
3226
3227 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3228 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003229 if (endLoc)
3230 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00003231 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3232 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003233}
Sean Huntbbd37c62009-11-21 08:43:09 +00003234
Sean Hunt2edf0a22012-06-23 05:07:58 +00003235/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003236///
3237/// attribute-specifier-seq:
3238/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00003239void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003240 SourceLocation *endLoc) {
Richard Smith672edb02013-02-22 09:15:49 +00003241 assert(getLangOpts().CPlusPlus11);
3242
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003243 SourceLocation StartLoc = Tok.getLocation(), Loc;
3244 if (!endLoc)
3245 endLoc = &Loc;
3246
Douglas Gregor8828ee72011-10-07 20:35:25 +00003247 do {
Richard Smithc56298d2012-04-10 03:25:07 +00003248 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003249 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003250
3251 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003252}
3253
Francois Pichet334d47e2010-10-11 12:59:39 +00003254/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3255///
3256/// [MS] ms-attribute:
3257/// '[' token-seq ']'
3258///
3259/// [MS] ms-attribute-seq:
3260/// ms-attribute[opt]
3261/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003262void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3263 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003264 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3265
3266 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003267 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003268 ConsumeBracket();
3269 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003270 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003271 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3272 }
3273}
Francois Pichet563a6452011-05-25 10:19:49 +00003274
3275void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3276 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003277 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003278 if (ParseMicrosoftIfExistsCondition(Result))
3279 return;
3280
Douglas Gregor3896fc52011-10-24 22:31:10 +00003281 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3282 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003283 Diag(Tok, diag::err_expected_lbrace);
3284 return;
3285 }
Francois Pichet563a6452011-05-25 10:19:49 +00003286
Douglas Gregor3896fc52011-10-24 22:31:10 +00003287 switch (Result.Behavior) {
3288 case IEB_Parse:
3289 // Parse the declarations below.
3290 break;
3291
3292 case IEB_Dependent:
3293 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3294 << Result.IsIfExists;
3295 // Fall through to skip.
3296
3297 case IEB_Skip:
3298 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003299 return;
3300 }
3301
Douglas Gregor3896fc52011-10-24 22:31:10 +00003302 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003303 // __if_exists, __if_not_exists can nest.
3304 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3305 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3306 continue;
3307 }
3308
3309 // Check for extraneous top-level semicolon.
3310 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003311 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003312 continue;
3313 }
3314
3315 AccessSpecifier AS = getAccessSpecifierIfPresent();
3316 if (AS != AS_none) {
3317 // Current token is a C++ access specifier.
3318 CurAS = AS;
3319 SourceLocation ASLoc = Tok.getLocation();
3320 ConsumeToken();
3321 if (Tok.is(tok::colon))
3322 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3323 else
3324 Diag(Tok, diag::err_expected_colon);
3325 ConsumeToken();
3326 continue;
3327 }
3328
3329 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003330 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003331 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003332
3333 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003334}