blob: 68d8e6ba4d0fc1bbff6d155b5dd3802ec114eec9 [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);
Larisse Voufo7c64ef02013-06-21 00:08:46 +00001542
1543 if (TUK == Sema::TUK_Definition &&
1544 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1545 // If the declarator-id is not a template-id, issue a diagnostic and
1546 // recover by ignoring the 'template' keyword.
1547 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1548 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1549 }
Sean Hunt2edf0a22012-06-23 05:07:58 +00001550
John McCallc4e70192009-09-11 04:59:25 +00001551 bool IsDependent = false;
1552
John McCalla25c4082010-10-19 18:40:57 +00001553 // Don't pass down template parameter lists if this is just a tag
1554 // reference. For example, we don't need the template parameters here:
1555 // template <class T> class A *makeA(T t);
1556 MultiTemplateParamsArg TParams;
1557 if (TUK != Sema::TUK_Reference && TemplateParams)
1558 TParams =
1559 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1560
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001561 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001562 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001563 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001564 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001565 TParams, Owned, IsDependent,
1566 SourceLocation(), false,
1567 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001568
1569 // If ActOnTag said the type was dependent, try again with the
1570 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001571 if (IsDependent) {
1572 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001573 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001574 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001575 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001576 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001577
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001578 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001579 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001580 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001581 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001582 isCXX11FinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001583 if (getLangOpts().CPlusPlus)
Michael Han07fc1ba2013-01-07 16:57:11 +00001584 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1585 TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001586 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001587 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001588 }
1589
John McCallb3d87482010-08-24 05:47:05 +00001590 const char *PrevSpec = 0;
1591 unsigned DiagID;
1592 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001593 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001594 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1595 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001596 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001597 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001598 Result = DS.SetTypeSpecType(TagType, StartLoc,
1599 NameLoc.isValid() ? NameLoc : StartLoc,
1600 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001601 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001602 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001603 return;
1604 }
Mike Stump1eb44332009-09-09 15:08:12 +00001605
John McCallb3d87482010-08-24 05:47:05 +00001606 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001607 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001608
Chris Lattner4ed5d912010-02-02 01:23:29 +00001609 // At this point, we've successfully parsed a class-specifier in 'definition'
1610 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1611 // going to look at what comes after it to improve error recovery. If an
1612 // impossible token occurs next, we assume that the programmer forgot a ; at
1613 // the end of the declaration and recover that way.
1614 //
Richard Smithc9f35172012-06-25 21:37:02 +00001615 // Also enforce C++ [temp]p3:
1616 // In a template-declaration which defines a class, no declarator
1617 // is permitted.
Joao Matos17d35c32012-08-31 22:18:20 +00001618 if (TUK == Sema::TUK_Definition &&
1619 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidis7d033b22012-12-17 20:10:43 +00001620 if (Tok.isNot(tok::semi)) {
1621 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1622 DeclSpec::getSpecifierName(TagType));
1623 // Push this token back into the preprocessor and change our current token
1624 // to ';' so that the rest of the code recovers as though there were an
1625 // ';' after the definition.
1626 PP.EnterToken(Tok);
1627 Tok.setKind(tok::semi);
1628 }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001629 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001630}
1631
Mike Stump1eb44332009-09-09 15:08:12 +00001632/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001633///
1634/// base-clause : [C++ class.derived]
1635/// ':' base-specifier-list
1636/// base-specifier-list:
1637/// base-specifier '...'[opt]
1638/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001639void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001640 assert(Tok.is(tok::colon) && "Not a base clause");
1641 ConsumeToken();
1642
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001643 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001644 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001645
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001646 while (true) {
1647 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001648 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001649 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001650 // Skip the rest of this base specifier, up until the comma or
1651 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001652 SkipUntil(tok::comma, tok::l_brace, true, true);
1653 } else {
1654 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001655 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001656 }
1657
1658 // If the next token is a comma, consume it and keep reading
1659 // base-specifiers.
1660 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001662 // Consume the comma.
1663 ConsumeToken();
1664 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001665
1666 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001667 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001668}
1669
1670/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1671/// one entry in the base class list of a class specifier, for example:
1672/// class foo : public bar, virtual private baz {
1673/// 'public bar' and 'virtual private baz' are each base-specifiers.
1674///
1675/// base-specifier: [C++ class.derived]
Richard Smith05321402013-02-19 23:47:15 +00001676/// attribute-specifier-seq[opt] base-type-specifier
1677/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1678/// base-type-specifier
1679/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1680/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001681Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001682 bool IsVirtual = false;
1683 SourceLocation StartLoc = Tok.getLocation();
1684
Richard Smith05321402013-02-19 23:47:15 +00001685 ParsedAttributesWithRange Attributes(AttrFactory);
1686 MaybeParseCXX11Attributes(Attributes);
1687
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001688 // Parse the 'virtual' keyword.
1689 if (Tok.is(tok::kw_virtual)) {
1690 ConsumeToken();
1691 IsVirtual = true;
1692 }
1693
Richard Smith05321402013-02-19 23:47:15 +00001694 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1695
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001696 // Parse an (optional) access specifier.
1697 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001698 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001699 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Richard Smith05321402013-02-19 23:47:15 +00001701 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1702
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001703 // Parse the 'virtual' keyword (again!), in case it came after the
1704 // access specifier.
1705 if (Tok.is(tok::kw_virtual)) {
1706 SourceLocation VirtualLoc = ConsumeToken();
1707 if (IsVirtual) {
1708 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001709 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001710 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001711 }
1712
1713 IsVirtual = true;
1714 }
1715
Richard Smith05321402013-02-19 23:47:15 +00001716 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1717
Douglas Gregor42a552f2008-11-05 20:51:48 +00001718 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001719 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001720 SourceLocation BaseLoc;
1721 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001722 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001723 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001724
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001725 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1726 // actually part of the base-specifier-list grammar productions, but we
1727 // parse it here for convenience.
1728 SourceLocation EllipsisLoc;
1729 if (Tok.is(tok::ellipsis))
1730 EllipsisLoc = ConsumeToken();
1731
Mike Stump1eb44332009-09-09 15:08:12 +00001732 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001733 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001734
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001735 // Notify semantic analysis that we have parsed a complete
1736 // base-specifier.
Richard Smith05321402013-02-19 23:47:15 +00001737 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1738 Access, BaseType.get(), BaseLoc,
1739 EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001740}
1741
1742/// getAccessSpecifierIfPresent - Determine whether the next token is
1743/// a C++ access-specifier.
1744///
1745/// access-specifier: [C++ class.derived]
1746/// 'private'
1747/// 'protected'
1748/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001749AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001750 switch (Tok.getKind()) {
1751 default: return AS_none;
1752 case tok::kw_private: return AS_private;
1753 case tok::kw_protected: return AS_protected;
1754 case tok::kw_public: return AS_public;
1755 }
1756}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001757
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001758/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001759/// delayed, e.g., default arguments, create a late-parsed method declaration
1760/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001761void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1762 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001763 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001764 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001765 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001766 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001767 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001768
Eli Friedmand33133c2009-07-22 21:45:50 +00001769 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1770 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1771 if (!LateMethod) {
1772 // Push this method onto the stack of late-parsed method
1773 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001774 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1775 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001776 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001777
1778 // Add all of the parameters prior to this one (they don't
1779 // have default arguments).
1780 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1781 for (unsigned I = 0; I < ParamIdx; ++I)
1782 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001783 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001784 }
1785
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001786 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001787 // not have a default argument).
1788 LateMethod->DefaultArgs.push_back(
1789 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1790 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1791 }
1792 }
1793}
1794
Richard Smith4e24f0f2013-01-02 12:01:23 +00001795/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001796/// virt-specifier.
1797///
1798/// virt-specifier:
1799/// override
1800/// final
Richard Smith4e24f0f2013-01-02 12:01:23 +00001801VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001802 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001803 return VirtSpecifiers::VS_None;
1804
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001805 if (Tok.is(tok::identifier)) {
1806 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001807
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001808 // Initialize the contextual keywords.
1809 if (!Ident_final) {
1810 Ident_final = &PP.getIdentifierTable().get("final");
1811 Ident_override = &PP.getIdentifierTable().get("override");
1812 }
1813
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001814 if (II == Ident_override)
1815 return VirtSpecifiers::VS_Override;
1816
1817 if (II == Ident_final)
1818 return VirtSpecifiers::VS_Final;
1819 }
1820
1821 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001822}
1823
Richard Smith4e24f0f2013-01-02 12:01:23 +00001824/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001825///
1826/// virt-specifier-seq:
1827/// virt-specifier
1828/// virt-specifier-seq virt-specifier
Richard Smith4e24f0f2013-01-02 12:01:23 +00001829void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalle402e722012-09-25 07:32:39 +00001830 bool IsInterface) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001831 while (true) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001832 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001833 if (Specifier == VirtSpecifiers::VS_None)
1834 return;
1835
1836 // C++ [class.mem]p8:
1837 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001838 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001839 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001840 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1841 << PrevSpec
1842 << FixItHint::CreateRemoval(Tok.getLocation());
1843
John McCalle402e722012-09-25 07:32:39 +00001844 if (IsInterface && Specifier == VirtSpecifiers::VS_Final) {
1845 Diag(Tok.getLocation(), diag::err_override_control_interface)
1846 << VirtSpecifiers::getSpecifierName(Specifier);
1847 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00001848 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00001849 diag::warn_cxx98_compat_override_control_keyword :
1850 diag::ext_override_control_keyword)
1851 << VirtSpecifiers::getSpecifierName(Specifier);
1852 }
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001853 ConsumeToken();
1854 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001855}
1856
Richard Smith4e24f0f2013-01-02 12:01:23 +00001857/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001858/// contextual 'final' keyword.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001859bool Parser::isCXX11FinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001860 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001861 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001862
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001863 if (!Tok.is(tok::identifier))
1864 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001865
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001866 // Initialize the contextual keywords.
1867 if (!Ident_final) {
1868 Ident_final = &PP.getIdentifierTable().get("final");
1869 Ident_override = &PP.getIdentifierTable().get("override");
1870 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001871
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001872 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001873}
1874
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001875/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1876///
1877/// member-declaration:
1878/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1879/// function-definition ';'[opt]
1880/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1881/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001882/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001883/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001884/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001885///
1886/// member-declarator-list:
1887/// member-declarator
1888/// member-declarator-list ',' member-declarator
1889///
1890/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001891/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001892/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001893/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001894/// identifier[opt] ':' constant-expression
1895///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001896/// virt-specifier-seq:
1897/// virt-specifier
1898/// virt-specifier-seq virt-specifier
1899///
1900/// virt-specifier:
1901/// override
1902/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001903///
Sebastian Redle2b68332009-04-12 17:16:29 +00001904/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001905/// '= 0'
1906///
1907/// constant-initializer:
1908/// '=' constant-expression
1909///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001910void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001911 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001912 const ParsedTemplateInfo &TemplateInfo,
1913 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001914 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001915 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001916 Diag(Tok, diag::err_at_defs_cxx);
1917 else
1918 Diag(Tok, diag::err_at_in_class);
1919
1920 ConsumeToken();
1921 SkipUntil(tok::r_brace);
1922 return;
1923 }
1924
John McCall60fa3cf2009-12-11 02:10:03 +00001925 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001926 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001927 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001928 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1929 if (TryAnnotateCXXScopeToken())
1930 MalformedTypeSpec = true;
1931
1932 bool isAccessDecl;
1933 if (Tok.isNot(tok::annot_cxxscope))
1934 isAccessDecl = false;
1935 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001936 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1937 else
1938 isAccessDecl = NextToken().is(tok::kw_operator);
1939
1940 if (isAccessDecl) {
1941 // Collect the scope specifier token we annotated earlier.
1942 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001943 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1944 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001945
1946 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001947 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001948 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001949 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1950 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001951 SkipUntil(tok::semi);
1952 return;
1953 }
1954
1955 // TODO: recover from mistakenly-qualified operator declarations.
1956 if (ExpectAndConsume(tok::semi,
1957 diag::err_expected_semi_after,
1958 "access declaration",
1959 tok::semi))
1960 return;
1961
Douglas Gregor23c94db2010-07-02 17:43:08 +00001962 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001963 false, SourceLocation(),
1964 SS, Name,
1965 /* AttrList */ 0,
1966 /* IsTypeName */ false,
1967 SourceLocation());
1968 return;
1969 }
1970 }
1971
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001972 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001973 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001974 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001975 SourceLocation DeclEnd;
1976 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001977 return;
1978 }
Mike Stump1eb44332009-09-09 15:08:12 +00001979
Chris Lattner682bf922009-03-29 16:50:03 +00001980 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001981 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001982 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001983 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001984 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001985 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001986 return;
1987 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001988
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001989 // Handle: member-declaration ::= '__extension__' member-declaration
1990 if (Tok.is(tok::kw___extension__)) {
1991 // __extension__ silences extension warnings in the subexpression.
1992 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1993 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001994 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1995 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001996 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001997
Chris Lattner4ed5d912010-02-02 01:23:29 +00001998 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1999 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002000 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002001
John McCall0b7e6782011-03-24 11:26:52 +00002002 ParsedAttributesWithRange attrs(AttrFactory);
Michael Han52b501c2012-11-28 23:17:40 +00002003 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00002004 // Optional C++11 attribute-specifier
2005 MaybeParseCXX11Attributes(attrs);
Michael Han52b501c2012-11-28 23:17:40 +00002006 // We need to keep these attributes for future diagnostic
2007 // before they are taken over by declaration specifier.
2008 FnAttrs.addAll(attrs.getList());
2009 FnAttrs.Range = attrs.Range;
2010
John McCall7f040a92010-12-24 02:08:15 +00002011 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002012
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002013 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00002014 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002015
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002016 // Eat 'using'.
2017 SourceLocation UsingLoc = ConsumeToken();
2018
2019 if (Tok.is(tok::kw_namespace)) {
2020 Diag(UsingLoc, diag::err_using_namespace_in_class);
2021 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00002022 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002023 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00002024 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00002025 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2026 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002027 }
2028 return;
2029 }
2030
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002031 // Hold late-parsed attributes so we can attach a Decl to them later.
2032 LateParsedAttrList CommonLateParsedAttrs;
2033
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002034 // decl-specifier-seq:
2035 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00002036 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00002037 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00002038 if (MalformedTypeSpec)
2039 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002040 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2041 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002042
Benjamin Kramer5354e772012-08-23 23:38:35 +00002043 MultiTemplateParamsArg TemplateParams(
John McCalldd4a3b02009-09-16 22:47:08 +00002044 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2045 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2046
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002047 if (Tok.is(tok::semi)) {
2048 ConsumeToken();
Michael Han52b501c2012-11-28 23:17:40 +00002049
2050 if (DS.isFriendSpecified())
2051 ProhibitAttributes(FnAttrs);
2052
John McCalld226f652010-08-21 09:40:31 +00002053 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00002054 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00002055 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00002056 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002057 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002058
John McCall54abf7d2009-11-04 02:18:39 +00002059 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00002060 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002061
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002062 // Hold late-parsed attributes so we can attach a Decl to them later.
2063 LateParsedAttrList LateParsedAttrs;
2064
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002065 SourceLocation EqualLoc;
2066 bool HasInitializer = false;
2067 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002068 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002069 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2070 ColonProtectionRAIIObject X(*this);
2071
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002072 // Parse the first declarator.
2073 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00002074 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00002075 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002076 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00002077 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002078 if (Tok.is(tok::semi))
2079 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002080 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002081 }
2082
Richard Smith4e24f0f2013-01-02 12:01:23 +00002083 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber48673472011-01-28 06:07:34 +00002084
John Thompson1b2fc0f2009-11-25 22:58:06 +00002085 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002086 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00002087
Francois Pichet6a247472011-05-11 02:14:46 +00002088 // MSVC permits pure specifier on inline functions declared at class scope.
2089 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00002090 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00002091 DeclaratorInfo.isFunctionDeclarator() &&
2092 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002093 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00002094 Init = ParseInitializer();
2095 if (Init.isInvalid())
2096 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002097 else
2098 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00002099 }
2100
Douglas Gregor45fa5602011-11-07 20:56:01 +00002101 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002102 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00002103 //
2104 // In C++11, a non-function declarator followed by an open brace is a
2105 // braced-init-list for an in-class member initialization, not an
2106 // erroneous function definition.
Richard Smith80ad52f2013-01-02 11:42:31 +00002107 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002108 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002109 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00002110 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002111 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002112 } else if (Tok.is(tok::equal)) {
2113 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00002114 if (KW.is(tok::kw_default))
2115 DefinitionKind = FDK_Defaulted;
2116 else if (KW.is(tok::kw_delete))
2117 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00002118 }
2119 }
2120
Michael Han52b501c2012-11-28 23:17:40 +00002121 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2122 // to a friend declaration, that declaration shall be a definition.
2123 if (DeclaratorInfo.isFunctionDeclarator() &&
2124 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2125 // Diagnose attributes that appear before decl specifier:
2126 // [[]] friend int foo();
2127 ProhibitAttributes(FnAttrs);
2128 }
2129
Douglas Gregor45fa5602011-11-07 20:56:01 +00002130 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002131 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002132 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002133 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00002134 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Michael Han52b501c2012-11-28 23:17:40 +00002135
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002136 // Consume the optional ';'
2137 if (Tok.is(tok::semi))
2138 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002139 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002140 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002141
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002142 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002143 Diag(DeclaratorInfo.getIdentifierLoc(),
2144 diag::err_function_declared_typedef);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002145
Richard Smith6f9a4452012-11-15 22:54:20 +00002146 // Recover by treating the 'typedef' as spurious.
2147 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002148 }
2149
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002150 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002151 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00002152 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002153
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002154 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2155 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2156 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002157 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002158 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002159 }
2160 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00002161
2162 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002163 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00002164 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002165
Chris Lattner682bf922009-03-29 16:50:03 +00002166 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002167 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002168 }
2169
2170 // member-declarator-list:
2171 // member-declarator
2172 // member-declarator-list ',' member-declarator
2173
Chris Lattner5f9e2722011-07-23 10:55:15 +00002174 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00002175 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00002176 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002177
2178 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002179 // member-declarator:
2180 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002181 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002182 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002183 if (Tok.is(tok::colon)) {
2184 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002185 BitfieldSize = ParseConstantExpression();
2186 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002187 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002188 }
Mike Stump1eb44332009-09-09 15:08:12 +00002189
Chris Lattnere6563252010-06-13 05:34:18 +00002190 // If a simple-asm-expr is present, parse it.
2191 if (Tok.is(tok::kw_asm)) {
2192 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002193 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002194 if (AsmLabel.isInvalid())
2195 SkipUntil(tok::comma, true, true);
2196
2197 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2198 DeclaratorInfo.SetRangeEnd(Loc);
2199 }
2200
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002201 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002202 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002203
Richard Smith7a614d82011-06-11 17:19:42 +00002204 // FIXME: When g++ adds support for this, we'll need to check whether it
2205 // goes before or after the GNU attributes and __asm__.
Richard Smith4e24f0f2013-01-02 12:01:23 +00002206 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith7a614d82011-06-11 17:19:42 +00002207
Richard Smithca523302012-06-10 03:12:00 +00002208 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002209 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002210 if (BitfieldSize.get()) {
2211 Diag(Tok, diag::err_bitfield_member_init);
2212 SkipUntil(tok::comma, true, true);
2213 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002214 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002215 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2216 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smithca523302012-06-10 03:12:00 +00002217 != DeclSpec::SCS_typedef)
2218 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002219 }
2220 }
2221
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002222 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002223 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002224 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002225
Rafael Espindolafc35cbc2013-01-08 20:44:06 +00002226 NamedDecl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002227 if (DS.isFriendSpecified()) {
Michael Han52b501c2012-11-28 23:17:40 +00002228 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2229 // to a friend declaration, that declaration shall be a definition.
2230 //
2231 // Diagnose attributes appear after friend member function declarator:
2232 // foo [[]] ();
2233 SmallVector<SourceRange, 4> Ranges;
2234 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2235 if (!Ranges.empty()) {
2236 for (SmallVector<SourceRange, 4>::iterator I = Ranges.begin(),
2237 E = Ranges.end(); I != E; ++I) {
2238 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2239 << *I;
2240 }
2241 }
2242
John McCallbbbcdd92009-09-11 21:02:39 +00002243 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002244 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002245 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002246 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002247 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002248 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002249 TemplateParams,
John McCall67d1a672009-08-06 02:15:43 +00002250 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002251 VS, HasInClassInit);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002252 if (AccessAttrs)
2253 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2254 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002255 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002256
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002257 // Set the Decl for any late parsed attributes
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002258 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2259 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2260 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002261 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002262 LateParsedAttrs[i]->addDecl(ThisDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002263 }
2264 LateParsedAttrs.clear();
2265
Douglas Gregor147545d2011-10-10 14:49:18 +00002266 // Handle the initializer.
David Blaikie1d87fba2013-01-30 01:22:18 +00002267 if (HasInClassInit != ICIS_NoInit &&
2268 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2269 DeclSpec::SCS_static) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002270 // The initializer was deferred; parse it and cache the tokens.
Richard Smith80ad52f2013-01-02 11:42:31 +00002271 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00002272 diag::warn_cxx98_compat_nonstatic_member_init :
2273 diag::ext_nonstatic_member_init);
2274
Richard Smith7a614d82011-06-11 17:19:42 +00002275 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002276 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2277 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002278 //
2279 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002280 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002281 Diag(Tok, diag::err_incomplete_array_member_init);
2282 SkipUntil(tok::comma, true, true);
David Blaikie3164c142012-02-14 09:00:46 +00002283 if (ThisDecl)
2284 // Avoid later warnings about a class member of incomplete type.
2285 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002286 } else
2287 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002288 } else if (HasInitializer) {
2289 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002290 if (!Init.isUsable())
Douglas Gregor552e2992012-02-21 02:22:07 +00002291 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002292 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2293
Douglas Gregor147545d2011-10-10 14:49:18 +00002294 if (Init.isInvalid())
2295 SkipUntil(tok::comma, true, true);
2296 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002297 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smitha2c36462013-04-26 16:15:35 +00002298 DS.containsPlaceholderType());
Douglas Gregor147545d2011-10-10 14:49:18 +00002299 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2300 // No initializer.
Richard Smitha2c36462013-04-26 16:15:35 +00002301 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
Richard Smith7a614d82011-06-11 17:19:42 +00002302 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002303
2304 if (ThisDecl) {
2305 Actions.FinalizeDeclaration(ThisDecl);
2306 DeclsInGroup.push_back(ThisDecl);
2307 }
2308
Richard Smithe5310012012-04-29 07:31:09 +00002309 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor147545d2011-10-10 14:49:18 +00002310 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2311 != DeclSpec::SCS_typedef) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002312 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002313 }
2314
2315 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002316
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002317 // If we don't have a comma, it is either the end of the list (a ';')
2318 // or an error, bail out.
2319 if (Tok.isNot(tok::comma))
2320 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002321
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002322 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002323 SourceLocation CommaLoc = ConsumeToken();
2324
2325 if (Tok.isAtStartOfLine() &&
2326 !MightBeDeclarator(Declarator::MemberContext)) {
2327 // This comma was followed by a line-break and something which can't be
2328 // the start of a declarator. The comma was probably a typo for a
2329 // semicolon.
2330 Diag(CommaLoc, diag::err_expected_semi_declaration)
2331 << FixItHint::CreateReplacement(CommaLoc, ";");
2332 ExpectSemi = false;
2333 break;
2334 }
Mike Stump1eb44332009-09-09 15:08:12 +00002335
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002336 // Parse the next declarator.
2337 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002338 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002339 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002340 Init = true;
2341 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002342 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002343
Bill Wendlingad017fa2012-12-20 19:22:21 +00002344 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002345 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002346
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002347 if (Tok.isNot(tok::colon))
2348 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002349 }
2350
Richard Smith1c94c162012-01-09 22:31:44 +00002351 if (ExpectSemi &&
2352 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002353 // Skip to end of block or statement.
2354 SkipUntil(tok::r_brace, true, true);
2355 // If we stopped at a ';', eat it.
2356 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002357 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002358 }
2359
Douglas Gregor23c94db2010-07-02 17:43:08 +00002360 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00002361 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002362}
2363
Richard Smith7a614d82011-06-11 17:19:42 +00002364/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2365/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2366/// function definition. The location of the '=', if any, will be placed in
2367/// EqualLoc.
2368///
2369/// pure-specifier:
2370/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002371///
Richard Smith7a614d82011-06-11 17:19:42 +00002372/// brace-or-equal-initializer:
2373/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002374/// braced-init-list
2375///
Richard Smith7a614d82011-06-11 17:19:42 +00002376/// initializer-clause:
2377/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002378/// braced-init-list
2379///
Richard Smith7a614d82011-06-11 17:19:42 +00002380/// defaulted/deleted function-definition:
2381/// '=' 'default'
2382/// '=' 'delete'
2383///
2384/// Prior to C++0x, the assignment-expression in an initializer-clause must
2385/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002386ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002387 SourceLocation &EqualLoc) {
2388 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2389 && "Data member initializer not starting with '=' or '{'");
2390
Douglas Gregor552e2992012-02-21 02:22:07 +00002391 EnterExpressionEvaluationContext Context(Actions,
2392 Sema::PotentiallyEvaluated,
2393 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002394 if (Tok.is(tok::equal)) {
2395 EqualLoc = ConsumeToken();
2396 if (Tok.is(tok::kw_delete)) {
2397 // In principle, an initializer of '= delete p;' is legal, but it will
2398 // never type-check. It's better to diagnose it as an ill-formed expression
2399 // than as an ill-formed deleted non-function member.
2400 // An initializer of '= delete p, foo' will never be parsed, because
2401 // a top-level comma always ends the initializer expression.
2402 const Token &Next = NextToken();
2403 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2404 Next.is(tok::eof)) {
2405 if (IsFunction)
2406 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2407 << 1 /* delete */;
2408 else
2409 Diag(ConsumeToken(), diag::err_deleted_non_function);
2410 return ExprResult();
2411 }
2412 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002413 if (IsFunction)
2414 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2415 << 0 /* default */;
2416 else
2417 Diag(ConsumeToken(), diag::err_default_special_members);
2418 return ExprResult();
2419 }
2420
Sebastian Redl33deb352012-02-22 10:50:08 +00002421 }
2422 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002423}
2424
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002425/// ParseCXXMemberSpecification - Parse the class definition.
2426///
2427/// member-specification:
2428/// member-declaration member-specification[opt]
2429/// access-specifier ':' member-specification[opt]
2430///
Joao Matos17d35c32012-08-31 22:18:20 +00002431void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han07fc1ba2013-01-07 16:57:11 +00002432 SourceLocation AttrFixitLoc,
Richard Smith05321402013-02-19 23:47:15 +00002433 ParsedAttributesWithRange &Attrs,
Joao Matos17d35c32012-08-31 22:18:20 +00002434 unsigned TagType, Decl *TagDecl) {
2435 assert((TagType == DeclSpec::TST_struct ||
2436 TagType == DeclSpec::TST_interface ||
2437 TagType == DeclSpec::TST_union ||
2438 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2439
John McCallf312b1e2010-08-26 23:41:50 +00002440 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2441 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002442
Douglas Gregor26997fd2010-01-16 20:52:59 +00002443 // Determine whether this is a non-nested class. Note that local
2444 // classes are *not* considered to be nested classes.
2445 bool NonNestedClass = true;
2446 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002447 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002448 if (S->isClassScope()) {
2449 // We're inside a class scope, so this is a nested class.
2450 NonNestedClass = false;
John McCalle402e722012-09-25 07:32:39 +00002451
2452 // The Microsoft extension __interface does not permit nested classes.
2453 if (getCurrentClass().IsInterface) {
2454 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2455 << /*ErrorType=*/6
2456 << (isa<NamedDecl>(TagDecl)
2457 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2458 : "<anonymous>");
2459 }
Douglas Gregor26997fd2010-01-16 20:52:59 +00002460 break;
2461 }
2462
2463 if ((S->getFlags() & Scope::FnScope)) {
2464 // If we're in a function or function template declared in the
2465 // body of a class, then this is a local class rather than a
2466 // nested class.
2467 const Scope *Parent = S->getParent();
2468 if (Parent->isTemplateParamScope())
2469 Parent = Parent->getParent();
2470 if (Parent->isClassScope())
2471 break;
2472 }
2473 }
2474 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002475
2476 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002477 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002478
Douglas Gregor6569d682009-05-27 23:11:45 +00002479 // Note that we are parsing a new (potentially-nested) class definition.
John McCalle402e722012-09-25 07:32:39 +00002480 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2481 TagType == DeclSpec::TST_interface);
Douglas Gregor6569d682009-05-27 23:11:45 +00002482
Douglas Gregorddc29e12009-02-06 22:42:48 +00002483 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002484 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002485
Anders Carlssonb184a182011-03-25 14:46:08 +00002486 SourceLocation FinalLoc;
2487
2488 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002489 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00002490 assert(isCXX11FinalKeyword() && "not a class definition");
Richard Smith8b11b5e2011-10-15 04:21:46 +00002491 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002492
John McCalle402e722012-09-25 07:32:39 +00002493 if (TagType == DeclSpec::TST_interface) {
2494 Diag(FinalLoc, diag::err_override_control_interface)
2495 << "final";
2496 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00002497 Diag(FinalLoc, getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00002498 diag::warn_cxx98_compat_override_control_keyword :
2499 diag::ext_override_control_keyword) << "final";
2500 }
Michael Han2e397132012-11-26 22:54:45 +00002501
Michael Han07fc1ba2013-01-07 16:57:11 +00002502 // Parse any C++11 attributes after 'final' keyword.
2503 // These attributes are not allowed to appear here,
2504 // and the only possible place for them to appertain
2505 // to the class would be between class-key and class-name.
Richard Smith05321402013-02-19 23:47:15 +00002506 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonb184a182011-03-25 14:46:08 +00002507 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002508
John McCallbd0dfa52009-12-19 21:48:58 +00002509 if (Tok.is(tok::colon)) {
2510 ParseBaseClause(TagDecl);
2511
2512 if (!Tok.is(tok::l_brace)) {
2513 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002514
2515 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002516 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002517 return;
2518 }
2519 }
2520
2521 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002522 BalancedDelimiterTracker T(*this, tok::l_brace);
2523 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002524
John McCall42a4f662010-05-28 08:11:17 +00002525 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002526 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002527 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002528
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002529 // C++ 11p3: Members of a class defined with the keyword class are private
2530 // by default. Members of a class defined with the keywords struct or union
2531 // are public by default.
2532 AccessSpecifier CurAS;
2533 if (TagType == DeclSpec::TST_class)
2534 CurAS = AS_private;
2535 else
2536 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002537 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002538
Douglas Gregor07976d22010-06-21 22:31:09 +00002539 if (TagDecl) {
2540 // While we still have something to read, read the member-declarations.
2541 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2542 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002543
David Blaikie4e4d0842012-03-11 07:00:24 +00002544 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002545 Tok.is(tok::kw___if_not_exists))) {
2546 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2547 continue;
2548 }
2549
Douglas Gregor07976d22010-06-21 22:31:09 +00002550 // Check for extraneous top-level semicolon.
2551 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002552 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002553 continue;
2554 }
2555
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002556 if (Tok.is(tok::annot_pragma_vis)) {
2557 HandlePragmaVisibility();
2558 continue;
2559 }
2560
2561 if (Tok.is(tok::annot_pragma_pack)) {
2562 HandlePragmaPack();
2563 continue;
2564 }
2565
Argyrios Kyrtzidisf4deaef2012-10-12 17:39:59 +00002566 if (Tok.is(tok::annot_pragma_align)) {
2567 HandlePragmaAlign();
2568 continue;
2569 }
2570
Alexey Bataevc6400582013-03-22 06:34:35 +00002571 if (Tok.is(tok::annot_pragma_openmp)) {
2572 ParseOpenMPDeclarativeDirective();
2573 continue;
2574 }
2575
Douglas Gregor07976d22010-06-21 22:31:09 +00002576 AccessSpecifier AS = getAccessSpecifierIfPresent();
2577 if (AS != AS_none) {
2578 // Current token is a C++ access specifier.
2579 CurAS = AS;
2580 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002581 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002582 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002583 AccessAttrs.clear();
2584 MaybeParseGNUAttributes(AccessAttrs);
2585
David Blaikie13f8daf2011-10-13 06:08:43 +00002586 SourceLocation EndLoc;
2587 if (Tok.is(tok::colon)) {
2588 EndLoc = Tok.getLocation();
2589 ConsumeToken();
2590 } else if (Tok.is(tok::semi)) {
2591 EndLoc = Tok.getLocation();
2592 ConsumeToken();
2593 Diag(EndLoc, diag::err_expected_colon)
2594 << FixItHint::CreateReplacement(EndLoc, ":");
2595 } else {
2596 EndLoc = ASLoc.getLocWithOffset(TokLength);
2597 Diag(EndLoc, diag::err_expected_colon)
2598 << FixItHint::CreateInsertion(EndLoc, ":");
2599 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002600
John McCalle402e722012-09-25 07:32:39 +00002601 // The Microsoft extension __interface does not permit non-public
2602 // access specifiers.
2603 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2604 Diag(ASLoc, diag::err_access_specifier_interface)
2605 << (CurAS == AS_protected);
2606 }
2607
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002608 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2609 AccessAttrs.getList())) {
2610 // found another attribute than only annotations
2611 AccessAttrs.clear();
2612 }
2613
Douglas Gregor07976d22010-06-21 22:31:09 +00002614 continue;
2615 }
2616
2617 // FIXME: Make sure we don't have a template here.
2618
2619 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002620 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002621 }
2622
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002623 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002624 } else {
2625 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002626 }
Mike Stump1eb44332009-09-09 15:08:12 +00002627
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002628 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002629 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002630 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002631
John McCall42a4f662010-05-28 08:11:17 +00002632 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002633 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002634 T.getOpenLocation(),
2635 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002636 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002637
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002638 // C++11 [class.mem]p2:
2639 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002640 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002641 // brace-or-equal-initializers for non-static data members (including such
2642 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002643 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002644 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002645 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002646 // declarations and the lexed inline method definitions, along with any
2647 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002648 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002649 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002650 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002651
2652 // We've finished with all pending member declarations.
2653 Actions.ActOnFinishCXXMemberDecls();
2654
Richard Smith7a614d82011-06-11 17:19:42 +00002655 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002656 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002657 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002658 }
2659
John McCall42a4f662010-05-28 08:11:17 +00002660 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002661 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2662 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002663
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002664 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002665 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002666 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002667}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002668
2669/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2670/// which explicitly initializes the members or base classes of a
2671/// class (C++ [class.base.init]). For example, the three initializers
2672/// after the ':' in the Derived constructor below:
2673///
2674/// @code
2675/// class Base { };
2676/// class Derived : Base {
2677/// int x;
2678/// float f;
2679/// public:
2680/// Derived(float f) : Base(), x(17), f(f) { }
2681/// };
2682/// @endcode
2683///
Mike Stump1eb44332009-09-09 15:08:12 +00002684/// [C++] ctor-initializer:
2685/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002686///
Mike Stump1eb44332009-09-09 15:08:12 +00002687/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002688/// mem-initializer ...[opt]
2689/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002690void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002691 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2692
John Wiegley28bbe4b2011-04-28 01:08:34 +00002693 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2694 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002695 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002696
Chris Lattner5f9e2722011-07-23 10:55:15 +00002697 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002698 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002699
Douglas Gregor7ad83902008-11-05 04:29:56 +00002700 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002701 if (Tok.is(tok::code_completion)) {
2702 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2703 MemInitializers.data(),
2704 MemInitializers.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002705 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002706 } else {
2707 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2708 if (!MemInit.isInvalid())
2709 MemInitializers.push_back(MemInit.get());
2710 else
2711 AnyErrors = true;
2712 }
2713
Douglas Gregor7ad83902008-11-05 04:29:56 +00002714 if (Tok.is(tok::comma))
2715 ConsumeToken();
2716 else if (Tok.is(tok::l_brace))
2717 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002718 // If the next token looks like a base or member initializer, assume that
2719 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002720 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2721 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2722 Diag(Loc, diag::err_ctor_init_missing_comma)
2723 << FixItHint::CreateInsertion(Loc, ", ");
2724 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002725 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002726 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002727 SkipUntil(tok::l_brace, true, true);
2728 break;
2729 }
2730 } while (true);
2731
David Blaikie93c86172013-01-17 05:26:25 +00002732 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002733 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002734}
2735
2736/// ParseMemInitializer - Parse a C++ member initializer, which is
2737/// part of a constructor initializer that explicitly initializes one
2738/// member or base class (C++ [class.base.init]). See
2739/// ParseConstructorInitializer for an example.
2740///
2741/// [C++] mem-initializer:
2742/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002743/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002744///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002745/// [C++] mem-initializer-id:
2746/// '::'[opt] nested-name-specifier[opt] class-name
2747/// identifier
John McCalld226f652010-08-21 09:40:31 +00002748Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002749 // parse '::'[opt] nested-name-specifier[opt]
2750 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002751 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002752 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002753 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002754 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002755 if (TemplateId->Kind == TNK_Type_template ||
2756 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002757 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002758 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002759 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002760 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002761 }
David Blaikief2116622012-01-24 06:03:59 +00002762 // Uses of decltype will already have been converted to annot_decltype by
2763 // ParseOptionalCXXScopeSpecifier at this point.
2764 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2765 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002766 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002767 return true;
2768 }
Mike Stump1eb44332009-09-09 15:08:12 +00002769
David Blaikief2116622012-01-24 06:03:59 +00002770 IdentifierInfo *II = 0;
2771 DeclSpec DS(AttrFactory);
2772 SourceLocation IdLoc = Tok.getLocation();
2773 if (Tok.is(tok::annot_decltype)) {
2774 // Get the decltype expression, if there is one.
2775 ParseDecltypeSpecifier(DS);
2776 } else {
2777 if (Tok.is(tok::identifier))
2778 // Get the identifier. This may be a member name or a class name,
2779 // but we'll let the semantic analysis determine which it is.
2780 II = Tok.getIdentifierInfo();
2781 ConsumeToken();
2782 }
2783
Douglas Gregor7ad83902008-11-05 04:29:56 +00002784
2785 // Parse the '('.
Richard Smith80ad52f2013-01-02 11:42:31 +00002786 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002787 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2788
Sebastian Redl6df65482011-09-24 17:48:25 +00002789 ExprResult InitList = ParseBraceInitializer();
2790 if (InitList.isInvalid())
2791 return true;
2792
2793 SourceLocation EllipsisLoc;
2794 if (Tok.is(tok::ellipsis))
2795 EllipsisLoc = ConsumeToken();
2796
2797 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002798 TemplateTypeTy, DS, IdLoc,
2799 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002800 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002801 BalancedDelimiterTracker T(*this, tok::l_paren);
2802 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002803
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002804 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002805 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002806 CommaLocsTy CommaLocs;
2807 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2808 SkipUntil(tok::r_paren);
2809 return true;
2810 }
2811
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002812 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002813
2814 SourceLocation EllipsisLoc;
2815 if (Tok.is(tok::ellipsis))
2816 EllipsisLoc = ConsumeToken();
2817
2818 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002819 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenkoa36bbac2013-05-09 23:51:52 +00002820 T.getOpenLocation(), ArgExprs,
2821 T.getCloseLocation(), EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002822 }
2823
Richard Smith80ad52f2013-01-02 11:42:31 +00002824 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002825 : diag::err_expected_lparen);
2826 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002827}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002828
Sebastian Redl7acafd02011-03-05 14:45:16 +00002829/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002830///
Douglas Gregora4745612008-12-01 18:00:20 +00002831/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002832/// dynamic-exception-specification
2833/// noexcept-specification
2834///
2835/// noexcept-specification:
2836/// 'noexcept'
2837/// 'noexcept' '(' constant-expression ')'
2838ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002839Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002840 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002841 SmallVectorImpl<ParsedType> &DynamicExceptions,
2842 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002843 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002844 ExceptionSpecificationType Result = EST_None;
2845
2846 // See if there's a dynamic specification.
2847 if (Tok.is(tok::kw_throw)) {
2848 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2849 DynamicExceptions,
2850 DynamicExceptionRanges);
2851 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2852 "Produced different number of exception types and ranges.");
2853 }
2854
2855 // If there's no noexcept specification, we're done.
2856 if (Tok.isNot(tok::kw_noexcept))
2857 return Result;
2858
Richard Smith841804b2011-10-17 23:06:20 +00002859 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2860
Sebastian Redl7acafd02011-03-05 14:45:16 +00002861 // If we already had a dynamic specification, parse the noexcept for,
2862 // recovery, but emit a diagnostic and don't store the results.
2863 SourceRange NoexceptRange;
2864 ExceptionSpecificationType NoexceptType = EST_None;
2865
2866 SourceLocation KeywordLoc = ConsumeToken();
2867 if (Tok.is(tok::l_paren)) {
2868 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002869 BalancedDelimiterTracker T(*this, tok::l_paren);
2870 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002871 NoexceptType = EST_ComputedNoexcept;
2872 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002873 // The argument must be contextually convertible to bool. We use
2874 // ActOnBooleanCondition for this purpose.
2875 if (!NoexceptExpr.isInvalid())
2876 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2877 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002878 T.consumeClose();
2879 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002880 } else {
2881 // There is no argument.
2882 NoexceptType = EST_BasicNoexcept;
2883 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2884 }
2885
2886 if (Result == EST_None) {
2887 SpecificationRange = NoexceptRange;
2888 Result = NoexceptType;
2889
2890 // If there's a dynamic specification after a noexcept specification,
2891 // parse that and ignore the results.
2892 if (Tok.is(tok::kw_throw)) {
2893 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2894 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2895 DynamicExceptionRanges);
2896 }
2897 } else {
2898 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2899 }
2900
2901 return Result;
2902}
2903
Richard Smith79f4bb72013-06-13 02:02:51 +00002904static void diagnoseDynamicExceptionSpecification(
2905 Parser &P, const SourceRange &Range, bool IsNoexcept) {
2906 if (P.getLangOpts().CPlusPlus11) {
2907 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
2908 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
2909 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
2910 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
2911 }
2912}
2913
Sebastian Redl7acafd02011-03-05 14:45:16 +00002914/// ParseDynamicExceptionSpecification - Parse a C++
2915/// dynamic-exception-specification (C++ [except.spec]).
2916///
2917/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002918/// 'throw' '(' type-id-list [opt] ')'
2919/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002920///
Douglas Gregora4745612008-12-01 18:00:20 +00002921/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002922/// type-id ... [opt]
2923/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002924///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002925ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2926 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002927 SmallVectorImpl<ParsedType> &Exceptions,
2928 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002929 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002930
Sebastian Redl7acafd02011-03-05 14:45:16 +00002931 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002932 BalancedDelimiterTracker T(*this, tok::l_paren);
2933 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002934 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2935 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002936 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002937 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002938
Douglas Gregora4745612008-12-01 18:00:20 +00002939 // Parse throw(...), a Microsoft extension that means "this function
2940 // can throw anything".
2941 if (Tok.is(tok::ellipsis)) {
2942 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002943 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002944 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002945 T.consumeClose();
2946 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith79f4bb72013-06-13 02:02:51 +00002947 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redl60618fa2011-03-12 11:50:43 +00002948 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002949 }
2950
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002951 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002952 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002953 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002954 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002955
Douglas Gregora04426c2010-12-20 23:57:46 +00002956 if (Tok.is(tok::ellipsis)) {
2957 // C++0x [temp.variadic]p5:
2958 // - In a dynamic-exception-specification (15.4); the pattern is a
2959 // type-id.
2960 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002961 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002962 if (!Res.isInvalid())
2963 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2964 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002965
Sebastian Redlef65f062009-05-29 18:02:33 +00002966 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002967 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002968 Ranges.push_back(Range);
2969 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002970
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002971 if (Tok.is(tok::comma))
2972 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002973 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002974 break;
2975 }
2976
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002977 T.consumeClose();
2978 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith79f4bb72013-06-13 02:02:51 +00002979 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
2980 Exceptions.empty());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002981 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002982}
Douglas Gregor6569d682009-05-27 23:11:45 +00002983
Douglas Gregordab60ad2010-10-01 18:44:50 +00002984/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2985/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002986TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002987 assert(Tok.is(tok::arrow) && "expected arrow");
2988
2989 ConsumeToken();
2990
Richard Smith7796eb52012-03-12 08:56:40 +00002991 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002992}
2993
Douglas Gregor6569d682009-05-27 23:11:45 +00002994/// \brief We have just started parsing the definition of a new class,
2995/// so push that class onto our stack of classes that is currently
2996/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002997Sema::ParsingClassState
John McCalle402e722012-09-25 07:32:39 +00002998Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
2999 bool IsInterface) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00003000 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00003001 "Nested class without outer class");
John McCalle402e722012-09-25 07:32:39 +00003002 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCalleee1d542011-02-14 07:13:47 +00003003 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00003004}
3005
3006/// \brief Deallocate the given parsed class and all of its nested
3007/// classes.
3008void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00003009 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3010 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00003011 delete Class;
3012}
3013
3014/// \brief Pop the top class of the stack of classes that are
3015/// currently being parsed.
3016///
3017/// This routine should be called when we have finished parsing the
3018/// definition of a class, but have not yet popped the Scope
3019/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00003020void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00003021 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00003022
John McCalleee1d542011-02-14 07:13:47 +00003023 Actions.PopParsingClass(state);
3024
Douglas Gregor6569d682009-05-27 23:11:45 +00003025 ParsingClass *Victim = ClassStack.top();
3026 ClassStack.pop();
3027 if (Victim->TopLevelClass) {
3028 // Deallocate all of the nested classes of this class,
3029 // recursively: we don't need to keep any of this information.
3030 DeallocateParsedClasses(Victim);
3031 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003032 }
Douglas Gregor6569d682009-05-27 23:11:45 +00003033 assert(!ClassStack.empty() && "Missing top-level class?");
3034
Douglas Gregord54eb442010-10-12 16:25:54 +00003035 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00003036 // The victim is a nested class, but we will not need to perform
3037 // any processing after the definition of this class since it has
3038 // no members whose handling was delayed. Therefore, we can just
3039 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00003040 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00003041 return;
3042 }
3043
3044 // This nested class has some members that will need to be processed
3045 // after the top-level class is completely defined. Therefore, add
3046 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003047 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00003048 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00003049 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00003050}
Sean Huntbbd37c62009-11-21 08:43:09 +00003051
Richard Smithc56298d2012-04-10 03:25:07 +00003052/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3053///
3054/// \return the parsed identifier on success, and 0 if the next token is not an
3055/// attribute-token.
3056///
3057/// C++11 [dcl.attr.grammar]p3:
3058/// If a keyword or an alternative token that satisfies the syntactic
3059/// requirements of an identifier is contained in an attribute-token,
3060/// it is considered an identifier.
3061IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3062 switch (Tok.getKind()) {
3063 default:
3064 // Identifiers and keywords have identifier info attached.
3065 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3066 Loc = ConsumeToken();
3067 return II;
3068 }
3069 return 0;
3070
3071 case tok::ampamp: // 'and'
3072 case tok::pipe: // 'bitor'
3073 case tok::pipepipe: // 'or'
3074 case tok::caret: // 'xor'
3075 case tok::tilde: // 'compl'
3076 case tok::amp: // 'bitand'
3077 case tok::ampequal: // 'and_eq'
3078 case tok::pipeequal: // 'or_eq'
3079 case tok::caretequal: // 'xor_eq'
3080 case tok::exclaim: // 'not'
3081 case tok::exclaimequal: // 'not_eq'
3082 // Alternative tokens do not have identifier info, but their spelling
3083 // starts with an alphabetical character.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003084 SmallString<8> SpellingBuf;
Richard Smithc56298d2012-04-10 03:25:07 +00003085 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rose3f6f51e2013-02-08 22:30:41 +00003086 if (isLetter(Spelling[0])) {
Richard Smithc56298d2012-04-10 03:25:07 +00003087 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00003088 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00003089 }
3090 return 0;
3091 }
3092}
3093
Michael Han6880f492012-10-03 01:56:22 +00003094static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3095 IdentifierInfo *ScopeName) {
3096 switch (AttributeList::getKind(AttrName, ScopeName,
3097 AttributeList::AS_CXX11)) {
3098 case AttributeList::AT_CarriesDependency:
3099 case AttributeList::AT_FallThrough:
Richard Smithcd8ab512013-01-17 01:30:42 +00003100 case AttributeList::AT_CXX11NoReturn: {
Michael Han6880f492012-10-03 01:56:22 +00003101 return true;
3102 }
3103
3104 default:
3105 return false;
3106 }
3107}
3108
Richard Smithc56298d2012-04-10 03:25:07 +00003109/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003110/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00003111///
Richard Smith6ee326a2012-04-10 01:32:12 +00003112/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00003113/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003114/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00003115///
Richard Smith6ee326a2012-04-10 01:32:12 +00003116/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00003117/// attribute[opt]
3118/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00003119/// attribute '...'
3120/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00003121///
Richard Smith6ee326a2012-04-10 01:32:12 +00003122/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00003123/// attribute-token attribute-argument-clause[opt]
3124///
Richard Smith6ee326a2012-04-10 01:32:12 +00003125/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003126/// identifier
3127/// attribute-scoped-token
3128///
Richard Smith6ee326a2012-04-10 01:32:12 +00003129/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003130/// attribute-namespace '::' identifier
3131///
Richard Smith6ee326a2012-04-10 01:32:12 +00003132/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00003133/// identifier
3134///
Richard Smith6ee326a2012-04-10 01:32:12 +00003135/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00003136/// '(' balanced-token-seq ')'
3137///
Richard Smith6ee326a2012-04-10 01:32:12 +00003138/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00003139/// balanced-token
3140/// balanced-token-seq balanced-token
3141///
Richard Smith6ee326a2012-04-10 01:32:12 +00003142/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003143/// '(' balanced-token-seq ')'
3144/// '[' balanced-token-seq ']'
3145/// '{' balanced-token-seq '}'
3146/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00003147void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003148 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003149 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00003150 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003151 ParseAlignmentSpecifier(attrs, endLoc);
3152 return;
3153 }
3154
Sean Huntbbd37c62009-11-21 08:43:09 +00003155 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00003156 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00003157
Richard Smith41be6732011-10-14 20:48:27 +00003158 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3159
Sean Huntbbd37c62009-11-21 08:43:09 +00003160 ConsumeBracket();
3161 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003162
Richard Smithcd8ab512013-01-17 01:30:42 +00003163 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3164
Richard Smithc56298d2012-04-10 03:25:07 +00003165 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003166 // attribute not present
3167 if (Tok.is(tok::comma)) {
3168 ConsumeToken();
3169 continue;
3170 }
3171
Richard Smithc56298d2012-04-10 03:25:07 +00003172 SourceLocation ScopeLoc, AttrLoc;
3173 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3174
3175 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3176 if (!AttrName)
3177 // Break out to the "expected ']'" diagnostic.
3178 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003179
Sean Huntbbd37c62009-11-21 08:43:09 +00003180 // scoped attribute
3181 if (Tok.is(tok::coloncolon)) {
3182 ConsumeToken();
3183
Richard Smithc56298d2012-04-10 03:25:07 +00003184 ScopeName = AttrName;
3185 ScopeLoc = AttrLoc;
3186
3187 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3188 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003189 Diag(Tok.getLocation(), diag::err_expected_ident);
3190 SkipUntil(tok::r_square, tok::comma, true, true);
3191 continue;
3192 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003193 }
3194
Michael Han6880f492012-10-03 01:56:22 +00003195 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Sean Huntbbd37c62009-11-21 08:43:09 +00003196 bool AttrParsed = false;
Sean Huntbbd37c62009-11-21 08:43:09 +00003197
Richard Smithcd8ab512013-01-17 01:30:42 +00003198 if (StandardAttr &&
3199 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3200 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3201 << AttrName << SourceRange(SeenAttrs[AttrName]);
3202
Michael Han6880f492012-10-03 01:56:22 +00003203 // Parse attribute arguments
3204 if (Tok.is(tok::l_paren)) {
3205 if (ScopeName && ScopeName->getName() == "gnu") {
3206 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3207 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3208 AttrParsed = true;
3209 } else {
3210 if (StandardAttr)
3211 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3212 << AttrName->getName();
3213
3214 // FIXME: handle other formats of c++11 attribute arguments
3215 ConsumeParen();
3216 SkipUntil(tok::r_paren, false);
3217 }
3218 }
3219
3220 if (!AttrParsed)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00003221 attrs.addNew(AttrName,
3222 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3223 AttrLoc),
3224 ScopeName, ScopeLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00003225 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smith6ee326a2012-04-10 01:32:12 +00003226
Richard Smithc56298d2012-04-10 03:25:07 +00003227 if (Tok.is(tok::ellipsis)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003228 ConsumeToken();
Michael Han6880f492012-10-03 01:56:22 +00003229
3230 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3231 << AttrName->getName();
Richard Smithc56298d2012-04-10 03:25:07 +00003232 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003233 }
3234
3235 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3236 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003237 if (endLoc)
3238 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00003239 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3240 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003241}
Sean Huntbbd37c62009-11-21 08:43:09 +00003242
Sean Hunt2edf0a22012-06-23 05:07:58 +00003243/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003244///
3245/// attribute-specifier-seq:
3246/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00003247void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003248 SourceLocation *endLoc) {
Richard Smith672edb02013-02-22 09:15:49 +00003249 assert(getLangOpts().CPlusPlus11);
3250
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003251 SourceLocation StartLoc = Tok.getLocation(), Loc;
3252 if (!endLoc)
3253 endLoc = &Loc;
3254
Douglas Gregor8828ee72011-10-07 20:35:25 +00003255 do {
Richard Smithc56298d2012-04-10 03:25:07 +00003256 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003257 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003258
3259 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003260}
3261
Francois Pichet334d47e2010-10-11 12:59:39 +00003262/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3263///
3264/// [MS] ms-attribute:
3265/// '[' token-seq ']'
3266///
3267/// [MS] ms-attribute-seq:
3268/// ms-attribute[opt]
3269/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003270void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3271 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003272 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3273
3274 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003275 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003276 ConsumeBracket();
3277 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003278 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003279 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3280 }
3281}
Francois Pichet563a6452011-05-25 10:19:49 +00003282
3283void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3284 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003285 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003286 if (ParseMicrosoftIfExistsCondition(Result))
3287 return;
3288
Douglas Gregor3896fc52011-10-24 22:31:10 +00003289 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3290 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003291 Diag(Tok, diag::err_expected_lbrace);
3292 return;
3293 }
Francois Pichet563a6452011-05-25 10:19:49 +00003294
Douglas Gregor3896fc52011-10-24 22:31:10 +00003295 switch (Result.Behavior) {
3296 case IEB_Parse:
3297 // Parse the declarations below.
3298 break;
3299
3300 case IEB_Dependent:
3301 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3302 << Result.IsIfExists;
3303 // Fall through to skip.
3304
3305 case IEB_Skip:
3306 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003307 return;
3308 }
3309
Douglas Gregor3896fc52011-10-24 22:31:10 +00003310 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003311 // __if_exists, __if_not_exists can nest.
3312 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3313 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3314 continue;
3315 }
3316
3317 // Check for extraneous top-level semicolon.
3318 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003319 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003320 continue;
3321 }
3322
3323 AccessSpecifier AS = getAccessSpecifierIfPresent();
3324 if (AS != AS_none) {
3325 // Current token is a C++ access specifier.
3326 CurAS = AS;
3327 SourceLocation ASLoc = Tok.getLocation();
3328 ConsumeToken();
3329 if (Tok.is(tok::colon))
3330 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3331 else
3332 Diag(Tok, diag::err_expected_colon);
3333 ConsumeToken();
3334 continue;
3335 }
3336
3337 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003338 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003339 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003340
3341 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003342}