blob: 4d428ceea1dc28b567f36eab7705a463234a5d06 [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;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000454 bool IsTypeName;
Sean Hunt2edf0a22012-06-23 05:07:58 +0000455 ParsedAttributesWithRange attrs(AttrFactory);
456
457 // FIXME: Simply skip the attributes and diagnose, don't bother parsing them.
Richard Smith4e24f0f2013-01-02 12:01:23 +0000458 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000459 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)) {
John McCall7ba107a2009-11-18 02:36:19 +0000466 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000467 ConsumeToken();
468 IsTypeName = true;
469 }
470 else
471 IsTypeName = false;
472
473 // Parse nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000474 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
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
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000482 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000483 // destructor names and allow the action module to diagnose any semantic
484 // errors.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000485 SourceLocation TemplateKWLoc;
Douglas Gregor12c118a2009-11-04 16:30:06 +0000486 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000487 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000488 /*EnteringContext=*/false,
489 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000490 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000491 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000492 TemplateKWLoc,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000493 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000494 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000495 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000496 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000497
Richard Smith4e24f0f2013-01-02 12:01:23 +0000498 MaybeParseCXX11Attributes(attrs);
Richard Smith162e1c12011-04-15 14:24:37 +0000499
500 // Maybe this is an alias-declaration.
501 bool IsAliasDecl = Tok.is(tok::equal);
502 TypeResult TypeAlias;
503 if (IsAliasDecl) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000504 // TODO: Attribute support. C++0x attributes may appear before the equals.
505 // Where can GNU attributes appear?
Richard Smith162e1c12011-04-15 14:24:37 +0000506 ConsumeToken();
507
Richard Smith80ad52f2013-01-02 11:42:31 +0000508 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000509 diag::warn_cxx98_compat_alias_declaration :
510 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000511
Richard Smith3e4c6c42011-05-05 21:57:07 +0000512 // Type alias templates cannot be specialized.
513 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000514 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
515 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000516 SpecKind = 0;
517 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
518 SpecKind = 1;
519 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
520 SpecKind = 2;
521 if (SpecKind != -1) {
522 SourceRange Range;
523 if (SpecKind == 0)
524 Range = SourceRange(Name.TemplateId->LAngleLoc,
525 Name.TemplateId->RAngleLoc);
526 else
527 Range = TemplateInfo.getSourceRange();
528 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
529 << SpecKind << Range;
530 SkipUntil(tok::semi);
531 return 0;
532 }
533
Richard Smith162e1c12011-04-15 14:24:37 +0000534 // Name must be an identifier.
535 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
536 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
537 // No removal fixit: can't recover from this.
538 SkipUntil(tok::semi);
539 return 0;
540 } else if (IsTypeName)
541 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
542 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
543 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
544 else if (SS.isNotEmpty())
545 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
546 << FixItHint::CreateRemoval(SS.getRange());
547
Richard Smith3e4c6c42011-05-05 21:57:07 +0000548 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
549 Declarator::AliasTemplateContext :
John McCallcdda47f2011-10-01 09:56:14 +0000550 Declarator::AliasDeclContext, AS, OwnedType);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000551 } else {
552 // C++11 attributes are not allowed on a using-declaration, but GNU ones
553 // are.
554 ProhibitAttributes(attrs);
555
Richard Smith162e1c12011-04-15 14:24:37 +0000556 // Parse (optional) attributes (most likely GNU strong-using extension).
557 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000558 }
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000560 // Eat ';'.
561 DeclEnd = Tok.getLocation();
562 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith162e1c12011-04-15 14:24:37 +0000563 !attrs.empty() ? "attributes list" :
564 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000565 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000566
John McCall78b81052010-11-10 02:40:36 +0000567 // Diagnose an attempt to declare a templated using-declaration.
Richard Smithd03de6a2013-01-29 10:02:16 +0000568 // In C++11, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000569 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000570 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000571 SourceRange R = TemplateInfo.getSourceRange();
572 Diag(UsingLoc, diag::err_templated_using_declaration)
573 << R << FixItHint::CreateRemoval(R);
574
575 // Unfortunately, we have to bail out instead of recovering by
576 // ignoring the parameters, just in case the nested name specifier
577 // depends on the parameters.
578 return 0;
579 }
580
Douglas Gregor480b53c2011-09-26 14:30:28 +0000581 // "typename" keyword is allowed for identifiers only,
582 // because it may be a type definition.
583 if (IsTypeName && Name.getKind() != UnqualifiedId::IK_Identifier) {
584 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
585 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
586 // Proceed parsing, but reset the IsTypeName flag.
587 IsTypeName = false;
588 }
589
Richard Smith3e4c6c42011-05-05 21:57:07 +0000590 if (IsAliasDecl) {
591 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramer5354e772012-08-23 23:38:35 +0000592 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3e4c6c42011-05-05 21:57:07 +0000593 TemplateParams ? TemplateParams->data() : 0,
594 TemplateParams ? TemplateParams->size() : 0);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000595 // FIXME: Propagate attributes.
Richard Smith3e4c6c42011-05-05 21:57:07 +0000596 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
597 UsingLoc, Name, TypeAlias);
598 }
Richard Smith162e1c12011-04-15 14:24:37 +0000599
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000600 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000601 Name, attrs.getList(),
602 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000603}
604
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000605/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000606///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000607/// [C++0x] static_assert-declaration:
608/// static_assert ( constant-expression , string-literal ) ;
609///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000610/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000611/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000612///
John McCalld226f652010-08-21 09:40:31 +0000613Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000614 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
615 "Not a static_assert declaration");
616
David Blaikie4e4d0842012-03-11 07:00:24 +0000617 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000618 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000619 if (Tok.is(tok::kw_static_assert))
620 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000621
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000622 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000624 BalancedDelimiterTracker T(*this, tok::l_paren);
625 if (T.consumeOpen()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000626 Diag(Tok, diag::err_expected_lparen);
Richard Smith3686c712012-09-13 19:12:50 +0000627 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000628 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000629 }
Mike Stump1eb44332009-09-09 15:08:12 +0000630
John McCall60d7b3a2010-08-24 06:29:42 +0000631 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000632 if (AssertExpr.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000633 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000634 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000635 }
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Anders Carlssonad5f9602009-03-13 23:29:20 +0000637 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000638 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000639
Richard Smith0cc323c2012-03-05 23:20:05 +0000640 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000641 Diag(Tok, diag::err_expected_string_literal)
642 << /*Source='static_assert'*/1;
Richard Smith3686c712012-09-13 19:12:50 +0000643 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000644 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000645 }
Mike Stump1eb44332009-09-09 15:08:12 +0000646
John McCall60d7b3a2010-08-24 06:29:42 +0000647 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smith99831e42012-03-06 03:21:47 +0000648 if (AssertMessage.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000649 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000650 return 0;
Richard Smith99831e42012-03-06 03:21:47 +0000651 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000652
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000653 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Chris Lattner97144fc2009-04-02 04:16:50 +0000655 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000656 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000657
John McCall9ae2f072010-08-23 23:25:46 +0000658 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
659 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000660 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000661 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000662}
663
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000664/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
665///
666/// 'decltype' ( expression )
667///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000668SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
669 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
670 && "Not a decltype specifier");
671
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000672
David Blaikie42d6d0c2011-12-04 05:04:18 +0000673 ExprResult Result;
674 SourceLocation StartLoc = Tok.getLocation();
675 SourceLocation EndLoc;
676
677 if (Tok.is(tok::annot_decltype)) {
678 Result = getExprAnnotation(Tok);
679 EndLoc = Tok.getAnnotationEndLoc();
680 ConsumeToken();
681 if (Result.isInvalid()) {
682 DS.SetTypeSpecError();
683 return EndLoc;
684 }
685 } else {
Richard Smithc7b55432012-02-24 22:30:04 +0000686 if (Tok.getIdentifierInfo()->isStr("decltype"))
687 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000688
David Blaikie42d6d0c2011-12-04 05:04:18 +0000689 ConsumeToken();
690
691 BalancedDelimiterTracker T(*this, tok::l_paren);
692 if (T.expectAndConsume(diag::err_expected_lparen_after,
693 "decltype", tok::r_paren)) {
694 DS.SetTypeSpecError();
695 return T.getOpenLocation() == Tok.getLocation() ?
696 StartLoc : T.getOpenLocation();
697 }
698
699 // Parse the expression
700
701 // C++0x [dcl.type.simple]p4:
702 // The operand of the decltype specifier is an unevaluated operand.
Richard Smith76f3f692012-02-22 02:04:18 +0000703 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
704 0, /*IsDecltype=*/true);
David Blaikie42d6d0c2011-12-04 05:04:18 +0000705 Result = ParseExpression();
706 if (Result.isInvalid()) {
David Blaikie42d6d0c2011-12-04 05:04:18 +0000707 DS.SetTypeSpecError();
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000708 if (SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true)) {
709 EndLoc = ConsumeParen();
710 } else {
Richard Smith569cdc82012-12-09 04:17:57 +0000711 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000712 // Backtrack to get the location of the last token before the semi.
713 PP.RevertCachedTokens(2);
714 ConsumeToken(); // the semi.
715 EndLoc = ConsumeAnyToken();
716 assert(Tok.is(tok::semi));
717 } else {
718 EndLoc = Tok.getLocation();
719 }
720 }
721 return EndLoc;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000722 }
723
724 // Match the ')'
725 T.consumeClose();
726 if (T.getCloseLocation().isInvalid()) {
727 DS.SetTypeSpecError();
728 // FIXME: this should return the location of the last token
729 // that was consumed (by "consumeClose()")
730 return T.getCloseLocation();
731 }
732
Richard Smith76f3f692012-02-22 02:04:18 +0000733 Result = Actions.ActOnDecltypeExpression(Result.take());
734 if (Result.isInvalid()) {
735 DS.SetTypeSpecError();
736 return T.getCloseLocation();
737 }
738
David Blaikie42d6d0c2011-12-04 05:04:18 +0000739 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000740 }
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000742 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000743 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000744 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000745 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
David Blaikie42d6d0c2011-12-04 05:04:18 +0000746 DiagID, Result.release())) {
John McCallfec54012009-08-03 20:12:06 +0000747 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000748 DS.SetTypeSpecError();
749 }
750 return EndLoc;
751}
752
753void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
754 SourceLocation StartLoc,
755 SourceLocation EndLoc) {
756 // make sure we have a token we can turn into an annotation token
757 if (PP.isBacktrackEnabled())
758 PP.RevertCachedTokens(1);
759 else
760 PP.EnterToken(Tok);
761
762 Tok.setKind(tok::annot_decltype);
763 setExprAnnotation(Tok, DS.getTypeSpecType() == TST_decltype ?
764 DS.getRepAsExpr() : ExprResult());
765 Tok.setAnnotationEndLoc(EndLoc);
766 Tok.setLocation(StartLoc);
767 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000768}
769
Sean Huntdb5d44b2011-05-19 05:37:45 +0000770void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
771 assert(Tok.is(tok::kw___underlying_type) &&
772 "Not an underlying type specifier");
773
774 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000775 BalancedDelimiterTracker T(*this, tok::l_paren);
776 if (T.expectAndConsume(diag::err_expected_lparen_after,
777 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000778 return;
779 }
780
781 TypeResult Result = ParseTypeName();
782 if (Result.isInvalid()) {
783 SkipUntil(tok::r_paren);
784 return;
785 }
786
787 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000788 T.consumeClose();
789 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000790 return;
791
792 const char *PrevSpec = 0;
793 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000794 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000795 DiagID, Result.release()))
796 Diag(StartLoc, DiagID) << PrevSpec;
797}
798
David Blaikie09048df2011-10-25 15:01:20 +0000799/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
800/// class name or decltype-specifier. Note that we only check that the result
801/// names a type; semantic analysis will need to verify that the type names a
802/// class. The result is either a type or null, depending on whether a type
803/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000804///
Richard Smith05321402013-02-19 23:47:15 +0000805/// base-type-specifier: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000806/// class-or-decltype
Richard Smith05321402013-02-19 23:47:15 +0000807/// class-or-decltype: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000808/// nested-name-specifier[opt] class-name
809/// decltype-specifier
Richard Smith05321402013-02-19 23:47:15 +0000810/// class-name: [C++ class.name]
Douglas Gregor42a552f2008-11-05 20:51:48 +0000811/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000812/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000813///
Richard Smith05321402013-02-19 23:47:15 +0000814/// In C++98, instead of base-type-specifier, we have:
815///
816/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie22216eb2011-10-25 17:10:12 +0000817Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
818 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000819 // Ignore attempts to use typename
820 if (Tok.is(tok::kw_typename)) {
821 Diag(Tok, diag::err_expected_class_name_not_template)
822 << FixItHint::CreateRemoval(Tok.getLocation());
823 ConsumeToken();
824 }
825
David Blaikie152aa4b2011-10-25 18:17:58 +0000826 // Parse optional nested-name-specifier
827 CXXScopeSpec SS;
828 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
829
830 BaseLoc = Tok.getLocation();
831
David Blaikie22216eb2011-10-25 17:10:12 +0000832 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000833 // tok == kw_decltype is just error recovery, it can only happen when SS
834 // isn't empty
835 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000836 if (SS.isNotEmpty())
837 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
838 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000839 // Fake up a Declarator to use with ActOnTypeName.
840 DeclSpec DS(AttrFactory);
841
David Blaikieb5777572011-12-08 04:53:15 +0000842 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000843
844 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
845 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
846 }
847
Douglas Gregor7f43d672009-02-25 23:52:28 +0000848 // Check whether we have a template-id that names a type.
849 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000850 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000851 if (TemplateId->Kind == TNK_Type_template ||
852 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000853 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000854
855 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000856 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000857 EndLocation = Tok.getAnnotationEndLoc();
858 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000859
860 if (Type)
861 return Type;
862 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000863 }
864
865 // Fall through to produce an error below.
866 }
867
Douglas Gregor42a552f2008-11-05 20:51:48 +0000868 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000869 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000870 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000871 }
872
Douglas Gregor84d0a192010-01-12 21:28:44 +0000873 IdentifierInfo *Id = Tok.getIdentifierInfo();
874 SourceLocation IdLoc = ConsumeToken();
875
876 if (Tok.is(tok::less)) {
877 // It looks the user intended to write a template-id here, but the
878 // template-name was wrong. Try to fix that.
879 TemplateNameKind TNK = TNK_Type_template;
880 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000881 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000882 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000883 Diag(IdLoc, diag::err_unknown_template_name)
884 << Id;
885 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000886
Douglas Gregor84d0a192010-01-12 21:28:44 +0000887 if (!Template)
888 return true;
889
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000890 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000891 UnqualifiedId TemplateName;
892 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000893
Douglas Gregor84d0a192010-01-12 21:28:44 +0000894 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000895 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
896 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000897 return true;
898 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000899 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000900
Douglas Gregor84d0a192010-01-12 21:28:44 +0000901 // If we didn't end up with a typename token, there's nothing more we
902 // can do.
903 if (Tok.isNot(tok::annot_typename))
904 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000905
Douglas Gregor84d0a192010-01-12 21:28:44 +0000906 // Retrieve the type from the annotation token, consume that token, and
907 // return.
908 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000909 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000910 ConsumeToken();
911 return Type;
912 }
913
Douglas Gregor42a552f2008-11-05 20:51:48 +0000914 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000915 IdentifierInfo *CorrectedII = 0;
Douglas Gregor059101f2011-03-02 00:47:37 +0000916 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000917 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000918 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000919 /*NonTrivialTypeSourceInfo=*/true,
920 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000921 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000922 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000923 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000924 }
925
926 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000927 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000928
929 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000930 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000931 DS.SetRangeStart(IdLoc);
932 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000933 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000934
935 const char *PrevSpec = 0;
936 unsigned DiagID;
937 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
938
939 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
940 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000941}
942
John McCallc052dbb2012-05-22 21:28:12 +0000943void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
944 while (Tok.is(tok::kw___single_inheritance) ||
945 Tok.is(tok::kw___multiple_inheritance) ||
946 Tok.is(tok::kw___virtual_inheritance)) {
947 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
948 SourceLocation AttrNameLoc = ConsumeToken();
949 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000950 SourceLocation(), 0, 0, AttributeList::AS_GNU);
John McCallc052dbb2012-05-22 21:28:12 +0000951 }
952}
953
Richard Smithc9f35172012-06-25 21:37:02 +0000954/// Determine whether the following tokens are valid after a type-specifier
955/// which could be a standalone declaration. This will conservatively return
956/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith139be702012-07-02 19:14:01 +0000957bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smithc9f35172012-06-25 21:37:02 +0000958 // This switch enumerates the valid "follow" set for type-specifiers.
959 switch (Tok.getKind()) {
960 default: break;
961 case tok::semi: // struct foo {...} ;
962 case tok::star: // struct foo {...} * P;
963 case tok::amp: // struct foo {...} & R = ...
Richard Smithba65f502013-01-19 03:48:05 +0000964 case tok::ampamp: // struct foo {...} && R = ...
Richard Smithc9f35172012-06-25 21:37:02 +0000965 case tok::identifier: // struct foo {...} V ;
966 case tok::r_paren: //(struct foo {...} ) {4}
967 case tok::annot_cxxscope: // struct foo {...} a:: b;
968 case tok::annot_typename: // struct foo {...} a ::b;
969 case tok::annot_template_id: // struct foo {...} a<int> ::b;
970 case tok::l_paren: // struct foo {...} ( x);
971 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smithba65f502013-01-19 03:48:05 +0000972 case tok::kw_operator: // struct foo operator ++() {...}
Richard Smithc9f35172012-06-25 21:37:02 +0000973 return true;
Richard Smith139be702012-07-02 19:14:01 +0000974 case tok::colon:
975 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smithc9f35172012-06-25 21:37:02 +0000976 // Type qualifiers
977 case tok::kw_const: // struct foo {...} const x;
978 case tok::kw_volatile: // struct foo {...} volatile x;
979 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smithba65f502013-01-19 03:48:05 +0000980 // Function specifiers
981 // Note, no 'explicit'. An explicit function must be either a conversion
982 // operator or a constructor. Either way, it can't have a return type.
983 case tok::kw_inline: // struct foo inline f();
984 case tok::kw_virtual: // struct foo virtual f();
985 case tok::kw_friend: // struct foo friend f();
Richard Smithc9f35172012-06-25 21:37:02 +0000986 // Storage-class specifiers
987 case tok::kw_static: // struct foo {...} static x;
988 case tok::kw_extern: // struct foo {...} extern x;
989 case tok::kw_typedef: // struct foo {...} typedef x;
990 case tok::kw_register: // struct foo {...} register x;
991 case tok::kw_auto: // struct foo {...} auto x;
992 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smithba65f502013-01-19 03:48:05 +0000993 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smithc9f35172012-06-25 21:37:02 +0000994 case tok::kw_constexpr: // struct foo {...} constexpr x;
995 // As shown above, type qualifiers and storage class specifiers absolutely
996 // can occur after class specifiers according to the grammar. However,
997 // almost no one actually writes code like this. If we see one of these,
998 // it is much more likely that someone missed a semi colon and the
999 // type/storage class specifier we're seeing is part of the *next*
1000 // intended declaration, as in:
1001 //
1002 // struct foo { ... }
1003 // typedef int X;
1004 //
1005 // We'd really like to emit a missing semicolon error instead of emitting
1006 // an error on the 'int' saying that you can't have two type specifiers in
1007 // the same declaration of X. Because of this, we look ahead past this
1008 // token to see if it's a type specifier. If so, we know the code is
1009 // otherwise invalid, so we can produce the expected semi error.
1010 if (!isKnownToBeTypeSpecifier(NextToken()))
1011 return true;
1012 break;
1013 case tok::r_brace: // struct bar { struct foo {...} }
1014 // Missing ';' at end of struct is accepted as an extension in C mode.
1015 if (!getLangOpts().CPlusPlus)
1016 return true;
1017 break;
Richard Smithba65f502013-01-19 03:48:05 +00001018 // C++11 attributes
1019 case tok::l_square: // enum E [[]] x
1020 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1021 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith8338a9d2013-01-29 04:13:32 +00001022 case tok::greater:
1023 // template<class T = class X>
1024 return getLangOpts().CPlusPlus;
Richard Smithc9f35172012-06-25 21:37:02 +00001025 }
1026 return false;
1027}
1028
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001029/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1030/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1031/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +00001032/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001033///
1034/// class-specifier: [C++ class]
1035/// class-head '{' member-specification[opt] '}'
1036/// class-head '{' member-specification[opt] '}' attributes[opt]
1037/// class-head:
1038/// class-key identifier[opt] base-clause[opt]
1039/// class-key nested-name-specifier identifier base-clause[opt]
1040/// class-key nested-name-specifier[opt] simple-template-id
1041/// base-clause[opt]
1042/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001043/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001044/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001045/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001046/// simple-template-id base-clause[opt]
1047/// class-key:
1048/// 'class'
1049/// 'struct'
1050/// 'union'
1051///
1052/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +00001053/// class-key ::[opt] nested-name-specifier[opt] identifier
1054/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1055/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001056///
1057/// Note that the C++ class-specifier and elaborated-type-specifier,
1058/// together, subsume the C99 struct-or-union-specifier:
1059///
1060/// struct-or-union-specifier: [C99 6.7.2.1]
1061/// struct-or-union identifier[opt] '{' struct-contents '}'
1062/// struct-or-union identifier
1063/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1064/// '}' attributes[opt]
1065/// [GNU] struct-or-union attributes[opt] identifier
1066/// struct-or-union:
1067/// 'struct'
1068/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +00001069void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1070 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001071 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001072 AccessSpecifier AS,
Michael Han2e397132012-11-26 22:54:45 +00001073 bool EnteringContext, DeclSpecContext DSC,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001074 ParsedAttributesWithRange &Attributes) {
Joao Matos17d35c32012-08-31 22:18:20 +00001075 DeclSpec::TST TagType;
1076 if (TagTokKind == tok::kw_struct)
1077 TagType = DeclSpec::TST_struct;
1078 else if (TagTokKind == tok::kw___interface)
1079 TagType = DeclSpec::TST_interface;
1080 else if (TagTokKind == tok::kw_class)
1081 TagType = DeclSpec::TST_class;
1082 else {
Chris Lattner4c97d762009-04-12 21:49:30 +00001083 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1084 TagType = DeclSpec::TST_union;
1085 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001086
Douglas Gregor374929f2009-09-18 15:37:17 +00001087 if (Tok.is(tok::code_completion)) {
1088 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001089 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001090 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00001091 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001092
Chandler Carruth926c4b42010-06-28 08:39:25 +00001093 // C++03 [temp.explicit] 14.7.2/8:
1094 // The usual access checking rules do not apply to names used to specify
1095 // explicit instantiations.
1096 //
1097 // As an extension we do not perform access checking on the names used to
1098 // specify explicit specializations either. This is important to allow
1099 // specializing traits classes for private types.
John McCall13489672012-05-07 06:16:58 +00001100 //
1101 // Note that we don't suppress if this turns out to be an elaborated
1102 // type specifier.
1103 bool shouldDelayDiagsInTag =
1104 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1105 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1106 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001107
Sean Hunt2edf0a22012-06-23 05:07:58 +00001108 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001109 // If attributes exist after tag, parse them.
1110 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +00001111 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001112
Steve Narofff59e17e2008-12-24 20:59:21 +00001113 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001114 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001115 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001116
John McCallc052dbb2012-05-22 21:28:12 +00001117 // Parse inheritance specifiers.
1118 if (Tok.is(tok::kw___single_inheritance) ||
1119 Tok.is(tok::kw___multiple_inheritance) ||
1120 Tok.is(tok::kw___virtual_inheritance))
1121 ParseMicrosoftInheritanceClassAttributes(attrs);
1122
Sean Huntbbd37c62009-11-21 08:43:09 +00001123 // If C++0x attributes exist here, parse them.
1124 // FIXME: Are we consistent with the ordering of parsing of different
1125 // styles of attributes?
Richard Smith4e24f0f2013-01-02 12:01:23 +00001126 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Michael Han07fc1ba2013-01-07 16:57:11 +00001128 // Source location used by FIXIT to insert misplaced
1129 // C++11 attributes
1130 SourceLocation AttrFixitLoc = Tok.getLocation();
1131
John Wiegley20c0da72011-04-27 23:09:49 +00001132 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +00001133 !Tok.is(tok::identifier) &&
1134 Tok.getIdentifierInfo() &&
1135 (Tok.is(tok::kw___is_arithmetic) ||
1136 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001137 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001138 Tok.is(tok::kw___is_floating_point) ||
1139 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001140 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001141 Tok.is(tok::kw___is_integral) ||
1142 Tok.is(tok::kw___is_member_function_pointer) ||
1143 Tok.is(tok::kw___is_member_pointer) ||
1144 Tok.is(tok::kw___is_pod) ||
1145 Tok.is(tok::kw___is_pointer) ||
1146 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001147 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001148 Tok.is(tok::kw___is_signed) ||
1149 Tok.is(tok::kw___is_unsigned) ||
1150 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001151 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001152 // name of struct templates, but some are keywords in GCC >= 4.3
1153 // and Clang. Therefore, when we see the token sequence "struct
1154 // X", make X into a normal identifier rather than a keyword, to
1155 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001156 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001157 Tok.setKind(tok::identifier);
1158 }
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001160 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001161 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001162 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001163 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1164 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001165
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001166 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001167 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001168 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001169 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1170 Diag(Tok, diag::err_expected_ident);
1171 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001172
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001173 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1174
Douglas Gregorcc636682009-02-17 23:15:12 +00001175 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001176 IdentifierInfo *Name = 0;
1177 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001178 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001179 if (Tok.is(tok::identifier)) {
1180 Name = Tok.getIdentifierInfo();
1181 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001182
David Blaikie4e4d0842012-03-11 07:00:24 +00001183 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001184 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001185 // Eat the template argument list and try to continue parsing this as
1186 // a class (or template thereof).
1187 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001188 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001189 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001190 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001191 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001192 // We couldn't parse the template argument list at all, so don't
1193 // try to give any location information for the list.
1194 LAngleLoc = RAngleLoc = SourceLocation();
1195 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001196
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001197 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matos17d35c32012-08-31 22:18:20 +00001198 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1199 << (TagType == DeclSpec::TST_class? 0
1200 : TagType == DeclSpec::TST_struct? 1
1201 : TagType == DeclSpec::TST_interface? 2
1202 : 3)
1203 << Name
1204 << SourceRange(LAngleLoc, RAngleLoc);
1205
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001206 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001207 // we've removed its template argument list.
1208 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1209 if (TemplateParams && TemplateParams->size() > 1) {
1210 TemplateParams->pop_back();
1211 } else {
1212 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001213 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001214 = ParsedTemplateInfo::NonTemplate;
1215 }
1216 } else if (TemplateInfo.Kind
1217 == ParsedTemplateInfo::ExplicitInstantiation) {
1218 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001219 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001220 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001221 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001222 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001223 = SourceLocation();
1224 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1225 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001226 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001227 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001228 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001229 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001230 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001231
Douglas Gregor059101f2011-03-02 00:47:37 +00001232 if (TemplateId->Kind != TNK_Type_template &&
1233 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001234 // The template-name in the simple-template-id refers to
1235 // something other than a class template. Give an appropriate
1236 // error message and skip to the ';'.
1237 SourceRange Range(NameLoc);
1238 if (SS.isNotEmpty())
1239 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001240
Douglas Gregor39a8de12009-02-25 19:37:18 +00001241 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1242 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Douglas Gregor39a8de12009-02-25 19:37:18 +00001244 DS.SetTypeSpecError();
1245 SkipUntil(tok::semi, false, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001246 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001247 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001248 }
1249
Richard Smith7796eb52012-03-12 08:56:40 +00001250 // There are four options here.
1251 // - If we are in a trailing return type, this is always just a reference,
1252 // and we must not try to parse a definition. For instance,
1253 // [] () -> struct S { };
1254 // does not define a type.
1255 // - If we have 'struct foo {...', 'struct foo :...',
1256 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1257 // - If we have 'struct foo;', then this is either a forward declaration
1258 // or a friend declaration, which have to be treated differently.
1259 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han2e397132012-11-26 22:54:45 +00001260 //
1261 // We also detect these erroneous cases to provide better diagnostic for
1262 // C++11 attributes parsing.
1263 // - attributes follow class name:
1264 // struct foo [[]] {};
1265 // - attributes appear before or after 'final':
1266 // struct foo [[]] final [[]] {};
1267 //
Richard Smith69730c12012-03-12 07:56:15 +00001268 // However, in type-specifier-seq's, things look like declarations but are
1269 // just references, e.g.
1270 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001271 // or
Richard Smith69730c12012-03-12 07:56:15 +00001272 // &T::operator struct s;
1273 // For these, DSC is DSC_type_specifier.
Michael Han2e397132012-11-26 22:54:45 +00001274
1275 // If there are attributes after class name, parse them.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001276 MaybeParseCXX11Attributes(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00001277
John McCallf312b1e2010-08-26 23:41:50 +00001278 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001279 if (DSC == DSC_trailing)
1280 TUK = Sema::TUK_Reference;
1281 else if (Tok.is(tok::l_brace) ||
1282 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001283 (isCXX11FinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001284 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001285 if (DS.isFriendSpecified()) {
1286 // C++ [class.friend]p2:
1287 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001288 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001289 << SourceRange(DS.getFriendSpecLoc());
1290
1291 // Skip everything up to the semicolon, so that this looks like a proper
1292 // friend class (or template thereof) declaration.
1293 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001294 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001295 } else {
1296 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001297 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001298 }
Richard Smith4e24f0f2013-01-02 12:01:23 +00001299 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
Michael Han2e397132012-11-26 22:54:45 +00001300 NextToken().is(tok::kw_alignas) ||
1301 NextToken().is(tok::kw__Alignas))) {
1302 // We can't tell if this is a definition or reference
1303 // until we skipped the 'final' and C++11 attribute specifiers.
1304 TentativeParsingAction PA(*this);
1305
1306 // Skip the 'final' keyword.
1307 ConsumeToken();
1308
1309 // Skip C++11 attribute specifiers.
1310 while (true) {
1311 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1312 ConsumeBracket();
1313 if (!SkipUntil(tok::r_square))
1314 break;
1315 } else if ((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1316 NextToken().is(tok::l_paren)) {
1317 ConsumeToken();
1318 ConsumeParen();
1319 if (!SkipUntil(tok::r_paren))
1320 break;
1321 } else {
1322 break;
1323 }
1324 }
1325
1326 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1327 TUK = Sema::TUK_Definition;
1328 else
1329 TUK = Sema::TUK_Reference;
1330
1331 PA.Revert();
Richard Smithc9f35172012-06-25 21:37:02 +00001332 } else if (DSC != DSC_type_specifier &&
1333 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00001334 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallf312b1e2010-08-26 23:41:50 +00001335 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matos17d35c32012-08-31 22:18:20 +00001336 if (Tok.isNot(tok::semi)) {
1337 // A semicolon was missing after this declaration. Diagnose and recover.
1338 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1339 DeclSpec::getSpecifierName(TagType));
1340 PP.EnterToken(Tok);
1341 Tok.setKind(tok::semi);
1342 }
Richard Smithc9f35172012-06-25 21:37:02 +00001343 } else
John McCallf312b1e2010-08-26 23:41:50 +00001344 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001345
Michael Han2e397132012-11-26 22:54:45 +00001346 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1347 // to caller to handle.
Michael Han07fc1ba2013-01-07 16:57:11 +00001348 if (TUK != Sema::TUK_Reference) {
1349 // If this is not a reference, then the only possible
1350 // valid place for C++11 attributes to appear here
1351 // is between class-key and class-name. If there are
1352 // any attributes after class-name, we try a fixit to move
1353 // them to the right place.
1354 SourceRange AttrRange = Attributes.Range;
1355 if (AttrRange.isValid()) {
1356 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1357 << AttrRange
1358 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1359 CharSourceRange(AttrRange, true))
1360 << FixItHint::CreateRemoval(AttrRange);
1361
1362 // Recover by adding misplaced attributes to the attribute list
1363 // of the class so they can be applied on the class later.
1364 attrs.takeAllFrom(Attributes);
1365 }
1366 }
Michael Han2e397132012-11-26 22:54:45 +00001367
John McCall13489672012-05-07 06:16:58 +00001368 // If this is an elaborated type specifier, and we delayed
1369 // diagnostics before, just merge them into the current pool.
1370 if (shouldDelayDiagsInTag) {
1371 diagsFromTag.done();
1372 if (TUK == Sema::TUK_Reference)
1373 diagsFromTag.redelay();
1374 }
1375
John McCall207014e2010-07-30 06:26:29 +00001376 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001377 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001378 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1379 // We have a declaration or reference to an anonymous class.
1380 Diag(StartLoc, diag::err_anon_type_definition)
1381 << DeclSpec::getSpecifierName(TagType);
1382 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001383
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001384 SkipUntil(tok::comma, true);
1385 return;
1386 }
1387
Douglas Gregorddc29e12009-02-06 22:42:48 +00001388 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001389 DeclResult TagOrTempResult = true; // invalid
1390 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001391
Douglas Gregor402abb52009-05-28 23:31:59 +00001392 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001393 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001394 // Explicit specialization, class template partial specialization,
1395 // or explicit instantiation.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001396 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001397 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001398 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001399 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001400 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001401 ProhibitAttributes(attrs);
1402
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001403 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001404 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001405 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001406 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001407 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001408 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001409 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001410 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001411 TemplateId->TemplateNameLoc,
1412 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001413 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001414 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001415 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001416
1417 // Friend template-ids are treated as references unless
1418 // they have template headers, in which case they're ill-formed
1419 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1420 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001421 } else if (TUK == Sema::TUK_Reference ||
1422 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001423 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001424 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001425 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001426 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001427 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001428 TemplateId->Template,
1429 TemplateId->TemplateNameLoc,
1430 TemplateId->LAngleLoc,
1431 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001432 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001433 } else {
1434 // This is an explicit specialization or a class template
1435 // partial specialization.
1436 TemplateParameterLists FakedParamLists;
1437
1438 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1439 // This looks like an explicit instantiation, because we have
1440 // something like
1441 //
1442 // template class Foo<X>
1443 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001444 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001445 // meant to be an explicit specialization, but the user forgot
1446 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +00001447 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001448
Mike Stump1eb44332009-09-09 15:08:12 +00001449 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001450 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001451 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001452 diag::err_explicit_instantiation_with_definition)
1453 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +00001454 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001455
1456 // Create a fake template parameter list that contains only
1457 // "template<>", so that we treat this construct as a class
1458 // template specialization.
1459 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +00001460 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001461 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001462 LAngleLoc,
1463 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001464 LAngleLoc));
1465 TemplateParams = &FakedParamLists;
1466 }
1467
1468 // Build the class template specialization.
1469 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001470 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001471 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001472 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001473 TemplateId->TemplateNameLoc,
1474 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001475 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001476 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001477 attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001478 MultiTemplateParamsArg(
Douglas Gregorcc636682009-02-17 23:15:12 +00001479 TemplateParams? &(*TemplateParams)[0] : 0,
1480 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001481 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001482 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001483 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001484 // Explicit instantiation of a member of a class template
1485 // specialization, e.g.,
1486 //
1487 // template struct Outer<int>::Inner;
1488 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001489 ProhibitAttributes(attrs);
1490
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001491 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001492 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001493 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001494 TemplateInfo.TemplateLoc,
1495 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001496 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001497 } else if (TUK == Sema::TUK_Friend &&
1498 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001499 ProhibitAttributes(attrs);
1500
John McCall9a34edb2010-10-19 01:40:49 +00001501 TagOrTempResult =
1502 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1503 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001504 Name, NameLoc, attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001505 MultiTemplateParamsArg(
John McCall9a34edb2010-10-19 01:40:49 +00001506 TemplateParams? &(*TemplateParams)[0] : 0,
1507 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001508 } else {
1509 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001510 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001511 // FIXME: Diagnose this particular error.
1512 }
1513
Sean Hunt2edf0a22012-06-23 05:07:58 +00001514 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1515 ProhibitAttributes(attrs);
1516
John McCallc4e70192009-09-11 04:59:25 +00001517 bool IsDependent = false;
1518
John McCalla25c4082010-10-19 18:40:57 +00001519 // Don't pass down template parameter lists if this is just a tag
1520 // reference. For example, we don't need the template parameters here:
1521 // template <class T> class A *makeA(T t);
1522 MultiTemplateParamsArg TParams;
1523 if (TUK != Sema::TUK_Reference && TemplateParams)
1524 TParams =
1525 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1526
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001527 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001528 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001529 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001530 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001531 TParams, Owned, IsDependent,
1532 SourceLocation(), false,
1533 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001534
1535 // If ActOnTag said the type was dependent, try again with the
1536 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001537 if (IsDependent) {
1538 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001539 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001540 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001541 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001542 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001543
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001544 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001545 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001546 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001547 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001548 isCXX11FinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001549 if (getLangOpts().CPlusPlus)
Michael Han07fc1ba2013-01-07 16:57:11 +00001550 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1551 TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001552 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001553 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001554 }
1555
John McCallb3d87482010-08-24 05:47:05 +00001556 const char *PrevSpec = 0;
1557 unsigned DiagID;
1558 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001559 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001560 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1561 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001562 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001563 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001564 Result = DS.SetTypeSpecType(TagType, StartLoc,
1565 NameLoc.isValid() ? NameLoc : StartLoc,
1566 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001567 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001568 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001569 return;
1570 }
Mike Stump1eb44332009-09-09 15:08:12 +00001571
John McCallb3d87482010-08-24 05:47:05 +00001572 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001573 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001574
Chris Lattner4ed5d912010-02-02 01:23:29 +00001575 // At this point, we've successfully parsed a class-specifier in 'definition'
1576 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1577 // going to look at what comes after it to improve error recovery. If an
1578 // impossible token occurs next, we assume that the programmer forgot a ; at
1579 // the end of the declaration and recover that way.
1580 //
Richard Smithc9f35172012-06-25 21:37:02 +00001581 // Also enforce C++ [temp]p3:
1582 // In a template-declaration which defines a class, no declarator
1583 // is permitted.
Joao Matos17d35c32012-08-31 22:18:20 +00001584 if (TUK == Sema::TUK_Definition &&
1585 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidis7d033b22012-12-17 20:10:43 +00001586 if (Tok.isNot(tok::semi)) {
1587 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1588 DeclSpec::getSpecifierName(TagType));
1589 // Push this token back into the preprocessor and change our current token
1590 // to ';' so that the rest of the code recovers as though there were an
1591 // ';' after the definition.
1592 PP.EnterToken(Tok);
1593 Tok.setKind(tok::semi);
1594 }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001595 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001596}
1597
Mike Stump1eb44332009-09-09 15:08:12 +00001598/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001599///
1600/// base-clause : [C++ class.derived]
1601/// ':' base-specifier-list
1602/// base-specifier-list:
1603/// base-specifier '...'[opt]
1604/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001605void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001606 assert(Tok.is(tok::colon) && "Not a base clause");
1607 ConsumeToken();
1608
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001609 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001610 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001611
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001612 while (true) {
1613 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001614 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001615 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001616 // Skip the rest of this base specifier, up until the comma or
1617 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001618 SkipUntil(tok::comma, tok::l_brace, true, true);
1619 } else {
1620 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001621 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001622 }
1623
1624 // If the next token is a comma, consume it and keep reading
1625 // base-specifiers.
1626 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001628 // Consume the comma.
1629 ConsumeToken();
1630 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001631
1632 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001633 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001634}
1635
1636/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1637/// one entry in the base class list of a class specifier, for example:
1638/// class foo : public bar, virtual private baz {
1639/// 'public bar' and 'virtual private baz' are each base-specifiers.
1640///
1641/// base-specifier: [C++ class.derived]
Richard Smith05321402013-02-19 23:47:15 +00001642/// attribute-specifier-seq[opt] base-type-specifier
1643/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1644/// base-type-specifier
1645/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1646/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001647Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001648 bool IsVirtual = false;
1649 SourceLocation StartLoc = Tok.getLocation();
1650
Richard Smith05321402013-02-19 23:47:15 +00001651 ParsedAttributesWithRange Attributes(AttrFactory);
1652 MaybeParseCXX11Attributes(Attributes);
1653
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001654 // Parse the 'virtual' keyword.
1655 if (Tok.is(tok::kw_virtual)) {
1656 ConsumeToken();
1657 IsVirtual = true;
1658 }
1659
Richard Smith05321402013-02-19 23:47:15 +00001660 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1661
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001662 // Parse an (optional) access specifier.
1663 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001664 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001665 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001666
Richard Smith05321402013-02-19 23:47:15 +00001667 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1668
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001669 // Parse the 'virtual' keyword (again!), in case it came after the
1670 // access specifier.
1671 if (Tok.is(tok::kw_virtual)) {
1672 SourceLocation VirtualLoc = ConsumeToken();
1673 if (IsVirtual) {
1674 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001675 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001676 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001677 }
1678
1679 IsVirtual = true;
1680 }
1681
Richard Smith05321402013-02-19 23:47:15 +00001682 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1683
Douglas Gregor42a552f2008-11-05 20:51:48 +00001684 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001685 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001686 SourceLocation BaseLoc;
1687 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001688 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001689 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001691 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1692 // actually part of the base-specifier-list grammar productions, but we
1693 // parse it here for convenience.
1694 SourceLocation EllipsisLoc;
1695 if (Tok.is(tok::ellipsis))
1696 EllipsisLoc = ConsumeToken();
1697
Mike Stump1eb44332009-09-09 15:08:12 +00001698 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001699 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001701 // Notify semantic analysis that we have parsed a complete
1702 // base-specifier.
Richard Smith05321402013-02-19 23:47:15 +00001703 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1704 Access, BaseType.get(), BaseLoc,
1705 EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001706}
1707
1708/// getAccessSpecifierIfPresent - Determine whether the next token is
1709/// a C++ access-specifier.
1710///
1711/// access-specifier: [C++ class.derived]
1712/// 'private'
1713/// 'protected'
1714/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001715AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001716 switch (Tok.getKind()) {
1717 default: return AS_none;
1718 case tok::kw_private: return AS_private;
1719 case tok::kw_protected: return AS_protected;
1720 case tok::kw_public: return AS_public;
1721 }
1722}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001723
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001724/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001725/// delayed, e.g., default arguments, create a late-parsed method declaration
1726/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001727void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1728 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001729 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001730 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001731 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001732 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001733 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001734
Eli Friedmand33133c2009-07-22 21:45:50 +00001735 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1736 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1737 if (!LateMethod) {
1738 // Push this method onto the stack of late-parsed method
1739 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001740 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1741 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001742 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001743
1744 // Add all of the parameters prior to this one (they don't
1745 // have default arguments).
1746 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1747 for (unsigned I = 0; I < ParamIdx; ++I)
1748 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001749 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001750 }
1751
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001752 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001753 // not have a default argument).
1754 LateMethod->DefaultArgs.push_back(
1755 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1756 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1757 }
1758 }
1759}
1760
Richard Smith4e24f0f2013-01-02 12:01:23 +00001761/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001762/// virt-specifier.
1763///
1764/// virt-specifier:
1765/// override
1766/// final
Richard Smith4e24f0f2013-01-02 12:01:23 +00001767VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001768 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001769 return VirtSpecifiers::VS_None;
1770
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001771 if (Tok.is(tok::identifier)) {
1772 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001773
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001774 // Initialize the contextual keywords.
1775 if (!Ident_final) {
1776 Ident_final = &PP.getIdentifierTable().get("final");
1777 Ident_override = &PP.getIdentifierTable().get("override");
1778 }
1779
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001780 if (II == Ident_override)
1781 return VirtSpecifiers::VS_Override;
1782
1783 if (II == Ident_final)
1784 return VirtSpecifiers::VS_Final;
1785 }
1786
1787 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001788}
1789
Richard Smith4e24f0f2013-01-02 12:01:23 +00001790/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001791///
1792/// virt-specifier-seq:
1793/// virt-specifier
1794/// virt-specifier-seq virt-specifier
Richard Smith4e24f0f2013-01-02 12:01:23 +00001795void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalle402e722012-09-25 07:32:39 +00001796 bool IsInterface) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001797 while (true) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001798 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001799 if (Specifier == VirtSpecifiers::VS_None)
1800 return;
1801
1802 // C++ [class.mem]p8:
1803 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001804 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001805 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001806 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1807 << PrevSpec
1808 << FixItHint::CreateRemoval(Tok.getLocation());
1809
John McCalle402e722012-09-25 07:32:39 +00001810 if (IsInterface && Specifier == VirtSpecifiers::VS_Final) {
1811 Diag(Tok.getLocation(), diag::err_override_control_interface)
1812 << VirtSpecifiers::getSpecifierName(Specifier);
1813 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00001814 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00001815 diag::warn_cxx98_compat_override_control_keyword :
1816 diag::ext_override_control_keyword)
1817 << VirtSpecifiers::getSpecifierName(Specifier);
1818 }
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001819 ConsumeToken();
1820 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001821}
1822
Richard Smith4e24f0f2013-01-02 12:01:23 +00001823/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001824/// contextual 'final' keyword.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001825bool Parser::isCXX11FinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001826 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001827 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001828
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001829 if (!Tok.is(tok::identifier))
1830 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001831
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001832 // Initialize the contextual keywords.
1833 if (!Ident_final) {
1834 Ident_final = &PP.getIdentifierTable().get("final");
1835 Ident_override = &PP.getIdentifierTable().get("override");
1836 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001837
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001838 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001839}
1840
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001841/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1842///
1843/// member-declaration:
1844/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1845/// function-definition ';'[opt]
1846/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1847/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001848/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001849/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001850/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001851///
1852/// member-declarator-list:
1853/// member-declarator
1854/// member-declarator-list ',' member-declarator
1855///
1856/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001857/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001858/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001859/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001860/// identifier[opt] ':' constant-expression
1861///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001862/// virt-specifier-seq:
1863/// virt-specifier
1864/// virt-specifier-seq virt-specifier
1865///
1866/// virt-specifier:
1867/// override
1868/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001869///
Sebastian Redle2b68332009-04-12 17:16:29 +00001870/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001871/// '= 0'
1872///
1873/// constant-initializer:
1874/// '=' constant-expression
1875///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001876void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001877 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001878 const ParsedTemplateInfo &TemplateInfo,
1879 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001880 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001881 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001882 Diag(Tok, diag::err_at_defs_cxx);
1883 else
1884 Diag(Tok, diag::err_at_in_class);
1885
1886 ConsumeToken();
1887 SkipUntil(tok::r_brace);
1888 return;
1889 }
1890
John McCall60fa3cf2009-12-11 02:10:03 +00001891 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001892 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001893 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001894 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1895 if (TryAnnotateCXXScopeToken())
1896 MalformedTypeSpec = true;
1897
1898 bool isAccessDecl;
1899 if (Tok.isNot(tok::annot_cxxscope))
1900 isAccessDecl = false;
1901 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001902 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1903 else
1904 isAccessDecl = NextToken().is(tok::kw_operator);
1905
1906 if (isAccessDecl) {
1907 // Collect the scope specifier token we annotated earlier.
1908 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001909 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1910 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001911
1912 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001913 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001914 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001915 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1916 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001917 SkipUntil(tok::semi);
1918 return;
1919 }
1920
1921 // TODO: recover from mistakenly-qualified operator declarations.
1922 if (ExpectAndConsume(tok::semi,
1923 diag::err_expected_semi_after,
1924 "access declaration",
1925 tok::semi))
1926 return;
1927
Douglas Gregor23c94db2010-07-02 17:43:08 +00001928 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001929 false, SourceLocation(),
1930 SS, Name,
1931 /* AttrList */ 0,
1932 /* IsTypeName */ false,
1933 SourceLocation());
1934 return;
1935 }
1936 }
1937
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001938 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001939 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001940 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001941 SourceLocation DeclEnd;
1942 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001943 return;
1944 }
Mike Stump1eb44332009-09-09 15:08:12 +00001945
Chris Lattner682bf922009-03-29 16:50:03 +00001946 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001947 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001948 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001949 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001950 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001951 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001952 return;
1953 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001954
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001955 // Handle: member-declaration ::= '__extension__' member-declaration
1956 if (Tok.is(tok::kw___extension__)) {
1957 // __extension__ silences extension warnings in the subexpression.
1958 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1959 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001960 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1961 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001962 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001963
Chris Lattner4ed5d912010-02-02 01:23:29 +00001964 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1965 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001966 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001967
John McCall0b7e6782011-03-24 11:26:52 +00001968 ParsedAttributesWithRange attrs(AttrFactory);
Michael Han52b501c2012-11-28 23:17:40 +00001969 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001970 // Optional C++11 attribute-specifier
1971 MaybeParseCXX11Attributes(attrs);
Michael Han52b501c2012-11-28 23:17:40 +00001972 // We need to keep these attributes for future diagnostic
1973 // before they are taken over by declaration specifier.
1974 FnAttrs.addAll(attrs.getList());
1975 FnAttrs.Range = attrs.Range;
1976
John McCall7f040a92010-12-24 02:08:15 +00001977 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001978
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001979 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00001980 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001982 // Eat 'using'.
1983 SourceLocation UsingLoc = ConsumeToken();
1984
1985 if (Tok.is(tok::kw_namespace)) {
1986 Diag(UsingLoc, diag::err_using_namespace_in_class);
1987 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001988 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001989 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001990 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00001991 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1992 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001993 }
1994 return;
1995 }
1996
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001997 // Hold late-parsed attributes so we can attach a Decl to them later.
1998 LateParsedAttrList CommonLateParsedAttrs;
1999
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002000 // decl-specifier-seq:
2001 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00002002 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00002003 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00002004 if (MalformedTypeSpec)
2005 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002006 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2007 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002008
Benjamin Kramer5354e772012-08-23 23:38:35 +00002009 MultiTemplateParamsArg TemplateParams(
John McCalldd4a3b02009-09-16 22:47:08 +00002010 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2011 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2012
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002013 if (Tok.is(tok::semi)) {
2014 ConsumeToken();
Michael Han52b501c2012-11-28 23:17:40 +00002015
2016 if (DS.isFriendSpecified())
2017 ProhibitAttributes(FnAttrs);
2018
John McCalld226f652010-08-21 09:40:31 +00002019 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00002020 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00002021 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00002022 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002023 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002024
John McCall54abf7d2009-11-04 02:18:39 +00002025 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00002026 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002027
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002028 // Hold late-parsed attributes so we can attach a Decl to them later.
2029 LateParsedAttrList LateParsedAttrs;
2030
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002031 SourceLocation EqualLoc;
2032 bool HasInitializer = false;
2033 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002034 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002035 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2036 ColonProtectionRAIIObject X(*this);
2037
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002038 // Parse the first declarator.
2039 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00002040 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00002041 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002042 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00002043 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002044 if (Tok.is(tok::semi))
2045 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002046 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002047 }
2048
Richard Smith4e24f0f2013-01-02 12:01:23 +00002049 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber48673472011-01-28 06:07:34 +00002050
John Thompson1b2fc0f2009-11-25 22:58:06 +00002051 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002052 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00002053
Francois Pichet6a247472011-05-11 02:14:46 +00002054 // MSVC permits pure specifier on inline functions declared at class scope.
2055 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00002056 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00002057 DeclaratorInfo.isFunctionDeclarator() &&
2058 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002059 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00002060 Init = ParseInitializer();
2061 if (Init.isInvalid())
2062 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002063 else
2064 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00002065 }
2066
Douglas Gregor45fa5602011-11-07 20:56:01 +00002067 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002068 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00002069 //
2070 // In C++11, a non-function declarator followed by an open brace is a
2071 // braced-init-list for an in-class member initialization, not an
2072 // erroneous function definition.
Richard Smith80ad52f2013-01-02 11:42:31 +00002073 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002074 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002075 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00002076 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002077 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002078 } else if (Tok.is(tok::equal)) {
2079 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00002080 if (KW.is(tok::kw_default))
2081 DefinitionKind = FDK_Defaulted;
2082 else if (KW.is(tok::kw_delete))
2083 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00002084 }
2085 }
2086
Michael Han52b501c2012-11-28 23:17:40 +00002087 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2088 // to a friend declaration, that declaration shall be a definition.
2089 if (DeclaratorInfo.isFunctionDeclarator() &&
2090 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2091 // Diagnose attributes that appear before decl specifier:
2092 // [[]] friend int foo();
2093 ProhibitAttributes(FnAttrs);
2094 }
2095
Douglas Gregor45fa5602011-11-07 20:56:01 +00002096 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002097 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002098 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002099 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00002100 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Michael Han52b501c2012-11-28 23:17:40 +00002101
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002102 // Consume the optional ';'
2103 if (Tok.is(tok::semi))
2104 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002105 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002106 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002107
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002108 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002109 Diag(DeclaratorInfo.getIdentifierLoc(),
2110 diag::err_function_declared_typedef);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002111
Richard Smith6f9a4452012-11-15 22:54:20 +00002112 // Recover by treating the 'typedef' as spurious.
2113 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002114 }
2115
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002116 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002117 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00002118 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002119
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002120 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2121 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2122 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002123 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002124 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002125 }
2126 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00002127
2128 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002129 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00002130 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002131
Chris Lattner682bf922009-03-29 16:50:03 +00002132 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002133 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002134 }
2135
2136 // member-declarator-list:
2137 // member-declarator
2138 // member-declarator-list ',' member-declarator
2139
Chris Lattner5f9e2722011-07-23 10:55:15 +00002140 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00002141 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00002142 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002143
2144 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002145 // member-declarator:
2146 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002147 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002148 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002149 if (Tok.is(tok::colon)) {
2150 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002151 BitfieldSize = ParseConstantExpression();
2152 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002153 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002154 }
Mike Stump1eb44332009-09-09 15:08:12 +00002155
Chris Lattnere6563252010-06-13 05:34:18 +00002156 // If a simple-asm-expr is present, parse it.
2157 if (Tok.is(tok::kw_asm)) {
2158 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002159 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002160 if (AsmLabel.isInvalid())
2161 SkipUntil(tok::comma, true, true);
2162
2163 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2164 DeclaratorInfo.SetRangeEnd(Loc);
2165 }
2166
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002167 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002168 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002169
Richard Smith7a614d82011-06-11 17:19:42 +00002170 // FIXME: When g++ adds support for this, we'll need to check whether it
2171 // goes before or after the GNU attributes and __asm__.
Richard Smith4e24f0f2013-01-02 12:01:23 +00002172 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith7a614d82011-06-11 17:19:42 +00002173
Richard Smithca523302012-06-10 03:12:00 +00002174 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002175 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002176 if (BitfieldSize.get()) {
2177 Diag(Tok, diag::err_bitfield_member_init);
2178 SkipUntil(tok::comma, true, true);
2179 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002180 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002181 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2182 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smithca523302012-06-10 03:12:00 +00002183 != DeclSpec::SCS_typedef)
2184 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002185 }
2186 }
2187
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002188 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002189 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002190 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002191
Rafael Espindolafc35cbc2013-01-08 20:44:06 +00002192 NamedDecl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002193 if (DS.isFriendSpecified()) {
Michael Han52b501c2012-11-28 23:17:40 +00002194 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2195 // to a friend declaration, that declaration shall be a definition.
2196 //
2197 // Diagnose attributes appear after friend member function declarator:
2198 // foo [[]] ();
2199 SmallVector<SourceRange, 4> Ranges;
2200 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2201 if (!Ranges.empty()) {
2202 for (SmallVector<SourceRange, 4>::iterator I = Ranges.begin(),
2203 E = Ranges.end(); I != E; ++I) {
2204 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2205 << *I;
2206 }
2207 }
2208
John McCallbbbcdd92009-09-11 21:02:39 +00002209 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002210 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002211 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002212 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002213 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002214 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002215 TemplateParams,
John McCall67d1a672009-08-06 02:15:43 +00002216 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002217 VS, HasInClassInit);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002218 if (AccessAttrs)
2219 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2220 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002221 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002222
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002223 // Set the Decl for any late parsed attributes
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002224 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2225 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2226 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002227 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002228 LateParsedAttrs[i]->addDecl(ThisDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002229 }
2230 LateParsedAttrs.clear();
2231
Douglas Gregor147545d2011-10-10 14:49:18 +00002232 // Handle the initializer.
David Blaikie1d87fba2013-01-30 01:22:18 +00002233 if (HasInClassInit != ICIS_NoInit &&
2234 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2235 DeclSpec::SCS_static) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002236 // The initializer was deferred; parse it and cache the tokens.
Richard Smith80ad52f2013-01-02 11:42:31 +00002237 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00002238 diag::warn_cxx98_compat_nonstatic_member_init :
2239 diag::ext_nonstatic_member_init);
2240
Richard Smith7a614d82011-06-11 17:19:42 +00002241 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002242 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2243 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002244 //
2245 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002246 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002247 Diag(Tok, diag::err_incomplete_array_member_init);
2248 SkipUntil(tok::comma, true, true);
David Blaikie3164c142012-02-14 09:00:46 +00002249 if (ThisDecl)
2250 // Avoid later warnings about a class member of incomplete type.
2251 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002252 } else
2253 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002254 } else if (HasInitializer) {
2255 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002256 if (!Init.isUsable())
Douglas Gregor552e2992012-02-21 02:22:07 +00002257 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002258 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2259
Douglas Gregor147545d2011-10-10 14:49:18 +00002260 if (Init.isInvalid())
2261 SkipUntil(tok::comma, true, true);
2262 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002263 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002264 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor147545d2011-10-10 14:49:18 +00002265 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2266 // No initializer.
2267 Actions.ActOnUninitializedDecl(ThisDecl,
2268 DS.getTypeSpecType() == DeclSpec::TST_auto);
Richard Smith7a614d82011-06-11 17:19:42 +00002269 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002270
2271 if (ThisDecl) {
2272 Actions.FinalizeDeclaration(ThisDecl);
2273 DeclsInGroup.push_back(ThisDecl);
2274 }
2275
Richard Smithe5310012012-04-29 07:31:09 +00002276 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor147545d2011-10-10 14:49:18 +00002277 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2278 != DeclSpec::SCS_typedef) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002279 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002280 }
2281
2282 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002283
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002284 // If we don't have a comma, it is either the end of the list (a ';')
2285 // or an error, bail out.
2286 if (Tok.isNot(tok::comma))
2287 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002288
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002289 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002290 SourceLocation CommaLoc = ConsumeToken();
2291
2292 if (Tok.isAtStartOfLine() &&
2293 !MightBeDeclarator(Declarator::MemberContext)) {
2294 // This comma was followed by a line-break and something which can't be
2295 // the start of a declarator. The comma was probably a typo for a
2296 // semicolon.
2297 Diag(CommaLoc, diag::err_expected_semi_declaration)
2298 << FixItHint::CreateReplacement(CommaLoc, ";");
2299 ExpectSemi = false;
2300 break;
2301 }
Mike Stump1eb44332009-09-09 15:08:12 +00002302
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002303 // Parse the next declarator.
2304 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002305 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002306 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002307 Init = true;
2308 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002309 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002310
Bill Wendlingad017fa2012-12-20 19:22:21 +00002311 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002312 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002313
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002314 if (Tok.isNot(tok::colon))
2315 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002316 }
2317
Richard Smith1c94c162012-01-09 22:31:44 +00002318 if (ExpectSemi &&
2319 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002320 // Skip to end of block or statement.
2321 SkipUntil(tok::r_brace, true, true);
2322 // If we stopped at a ';', eat it.
2323 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002324 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002325 }
2326
Douglas Gregor23c94db2010-07-02 17:43:08 +00002327 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00002328 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002329}
2330
Richard Smith7a614d82011-06-11 17:19:42 +00002331/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2332/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2333/// function definition. The location of the '=', if any, will be placed in
2334/// EqualLoc.
2335///
2336/// pure-specifier:
2337/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002338///
Richard Smith7a614d82011-06-11 17:19:42 +00002339/// brace-or-equal-initializer:
2340/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002341/// braced-init-list
2342///
Richard Smith7a614d82011-06-11 17:19:42 +00002343/// initializer-clause:
2344/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002345/// braced-init-list
2346///
Richard Smith7a614d82011-06-11 17:19:42 +00002347/// defaulted/deleted function-definition:
2348/// '=' 'default'
2349/// '=' 'delete'
2350///
2351/// Prior to C++0x, the assignment-expression in an initializer-clause must
2352/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002353ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002354 SourceLocation &EqualLoc) {
2355 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2356 && "Data member initializer not starting with '=' or '{'");
2357
Douglas Gregor552e2992012-02-21 02:22:07 +00002358 EnterExpressionEvaluationContext Context(Actions,
2359 Sema::PotentiallyEvaluated,
2360 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002361 if (Tok.is(tok::equal)) {
2362 EqualLoc = ConsumeToken();
2363 if (Tok.is(tok::kw_delete)) {
2364 // In principle, an initializer of '= delete p;' is legal, but it will
2365 // never type-check. It's better to diagnose it as an ill-formed expression
2366 // than as an ill-formed deleted non-function member.
2367 // An initializer of '= delete p, foo' will never be parsed, because
2368 // a top-level comma always ends the initializer expression.
2369 const Token &Next = NextToken();
2370 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2371 Next.is(tok::eof)) {
2372 if (IsFunction)
2373 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2374 << 1 /* delete */;
2375 else
2376 Diag(ConsumeToken(), diag::err_deleted_non_function);
2377 return ExprResult();
2378 }
2379 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002380 if (IsFunction)
2381 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2382 << 0 /* default */;
2383 else
2384 Diag(ConsumeToken(), diag::err_default_special_members);
2385 return ExprResult();
2386 }
2387
Sebastian Redl33deb352012-02-22 10:50:08 +00002388 }
2389 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002390}
2391
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002392/// ParseCXXMemberSpecification - Parse the class definition.
2393///
2394/// member-specification:
2395/// member-declaration member-specification[opt]
2396/// access-specifier ':' member-specification[opt]
2397///
Joao Matos17d35c32012-08-31 22:18:20 +00002398void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han07fc1ba2013-01-07 16:57:11 +00002399 SourceLocation AttrFixitLoc,
Richard Smith05321402013-02-19 23:47:15 +00002400 ParsedAttributesWithRange &Attrs,
Joao Matos17d35c32012-08-31 22:18:20 +00002401 unsigned TagType, Decl *TagDecl) {
2402 assert((TagType == DeclSpec::TST_struct ||
2403 TagType == DeclSpec::TST_interface ||
2404 TagType == DeclSpec::TST_union ||
2405 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2406
John McCallf312b1e2010-08-26 23:41:50 +00002407 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2408 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002409
Douglas Gregor26997fd2010-01-16 20:52:59 +00002410 // Determine whether this is a non-nested class. Note that local
2411 // classes are *not* considered to be nested classes.
2412 bool NonNestedClass = true;
2413 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002414 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002415 if (S->isClassScope()) {
2416 // We're inside a class scope, so this is a nested class.
2417 NonNestedClass = false;
John McCalle402e722012-09-25 07:32:39 +00002418
2419 // The Microsoft extension __interface does not permit nested classes.
2420 if (getCurrentClass().IsInterface) {
2421 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2422 << /*ErrorType=*/6
2423 << (isa<NamedDecl>(TagDecl)
2424 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2425 : "<anonymous>");
2426 }
Douglas Gregor26997fd2010-01-16 20:52:59 +00002427 break;
2428 }
2429
2430 if ((S->getFlags() & Scope::FnScope)) {
2431 // If we're in a function or function template declared in the
2432 // body of a class, then this is a local class rather than a
2433 // nested class.
2434 const Scope *Parent = S->getParent();
2435 if (Parent->isTemplateParamScope())
2436 Parent = Parent->getParent();
2437 if (Parent->isClassScope())
2438 break;
2439 }
2440 }
2441 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002442
2443 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002444 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002445
Douglas Gregor6569d682009-05-27 23:11:45 +00002446 // Note that we are parsing a new (potentially-nested) class definition.
John McCalle402e722012-09-25 07:32:39 +00002447 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2448 TagType == DeclSpec::TST_interface);
Douglas Gregor6569d682009-05-27 23:11:45 +00002449
Douglas Gregorddc29e12009-02-06 22:42:48 +00002450 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002451 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002452
Anders Carlssonb184a182011-03-25 14:46:08 +00002453 SourceLocation FinalLoc;
2454
2455 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002456 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00002457 assert(isCXX11FinalKeyword() && "not a class definition");
Richard Smith8b11b5e2011-10-15 04:21:46 +00002458 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002459
John McCalle402e722012-09-25 07:32:39 +00002460 if (TagType == DeclSpec::TST_interface) {
2461 Diag(FinalLoc, diag::err_override_control_interface)
2462 << "final";
2463 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00002464 Diag(FinalLoc, getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00002465 diag::warn_cxx98_compat_override_control_keyword :
2466 diag::ext_override_control_keyword) << "final";
2467 }
Michael Han2e397132012-11-26 22:54:45 +00002468
Michael Han07fc1ba2013-01-07 16:57:11 +00002469 // Parse any C++11 attributes after 'final' keyword.
2470 // These attributes are not allowed to appear here,
2471 // and the only possible place for them to appertain
2472 // to the class would be between class-key and class-name.
Richard Smith05321402013-02-19 23:47:15 +00002473 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonb184a182011-03-25 14:46:08 +00002474 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002475
John McCallbd0dfa52009-12-19 21:48:58 +00002476 if (Tok.is(tok::colon)) {
2477 ParseBaseClause(TagDecl);
2478
2479 if (!Tok.is(tok::l_brace)) {
2480 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002481
2482 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002483 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002484 return;
2485 }
2486 }
2487
2488 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002489 BalancedDelimiterTracker T(*this, tok::l_brace);
2490 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002491
John McCall42a4f662010-05-28 08:11:17 +00002492 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002493 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002494 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002495
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002496 // C++ 11p3: Members of a class defined with the keyword class are private
2497 // by default. Members of a class defined with the keywords struct or union
2498 // are public by default.
2499 AccessSpecifier CurAS;
2500 if (TagType == DeclSpec::TST_class)
2501 CurAS = AS_private;
2502 else
2503 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002504 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002505
Douglas Gregor07976d22010-06-21 22:31:09 +00002506 if (TagDecl) {
2507 // While we still have something to read, read the member-declarations.
2508 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2509 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002510
David Blaikie4e4d0842012-03-11 07:00:24 +00002511 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002512 Tok.is(tok::kw___if_not_exists))) {
2513 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2514 continue;
2515 }
2516
Douglas Gregor07976d22010-06-21 22:31:09 +00002517 // Check for extraneous top-level semicolon.
2518 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002519 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002520 continue;
2521 }
2522
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002523 if (Tok.is(tok::annot_pragma_vis)) {
2524 HandlePragmaVisibility();
2525 continue;
2526 }
2527
2528 if (Tok.is(tok::annot_pragma_pack)) {
2529 HandlePragmaPack();
2530 continue;
2531 }
2532
Argyrios Kyrtzidisf4deaef2012-10-12 17:39:59 +00002533 if (Tok.is(tok::annot_pragma_align)) {
2534 HandlePragmaAlign();
2535 continue;
2536 }
2537
Douglas Gregor07976d22010-06-21 22:31:09 +00002538 AccessSpecifier AS = getAccessSpecifierIfPresent();
2539 if (AS != AS_none) {
2540 // Current token is a C++ access specifier.
2541 CurAS = AS;
2542 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002543 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002544 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002545 AccessAttrs.clear();
2546 MaybeParseGNUAttributes(AccessAttrs);
2547
David Blaikie13f8daf2011-10-13 06:08:43 +00002548 SourceLocation EndLoc;
2549 if (Tok.is(tok::colon)) {
2550 EndLoc = Tok.getLocation();
2551 ConsumeToken();
2552 } else if (Tok.is(tok::semi)) {
2553 EndLoc = Tok.getLocation();
2554 ConsumeToken();
2555 Diag(EndLoc, diag::err_expected_colon)
2556 << FixItHint::CreateReplacement(EndLoc, ":");
2557 } else {
2558 EndLoc = ASLoc.getLocWithOffset(TokLength);
2559 Diag(EndLoc, diag::err_expected_colon)
2560 << FixItHint::CreateInsertion(EndLoc, ":");
2561 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002562
John McCalle402e722012-09-25 07:32:39 +00002563 // The Microsoft extension __interface does not permit non-public
2564 // access specifiers.
2565 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2566 Diag(ASLoc, diag::err_access_specifier_interface)
2567 << (CurAS == AS_protected);
2568 }
2569
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002570 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2571 AccessAttrs.getList())) {
2572 // found another attribute than only annotations
2573 AccessAttrs.clear();
2574 }
2575
Douglas Gregor07976d22010-06-21 22:31:09 +00002576 continue;
2577 }
2578
2579 // FIXME: Make sure we don't have a template here.
2580
2581 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002582 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002583 }
2584
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002585 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002586 } else {
2587 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002588 }
Mike Stump1eb44332009-09-09 15:08:12 +00002589
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002590 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002591 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002592 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002593
John McCall42a4f662010-05-28 08:11:17 +00002594 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002595 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002596 T.getOpenLocation(),
2597 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002598 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002599
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002600 // C++11 [class.mem]p2:
2601 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002602 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002603 // brace-or-equal-initializers for non-static data members (including such
2604 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002605 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002606 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002607 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002608 // declarations and the lexed inline method definitions, along with any
2609 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002610 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002611 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002612 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002613
2614 // We've finished with all pending member declarations.
2615 Actions.ActOnFinishCXXMemberDecls();
2616
Richard Smith7a614d82011-06-11 17:19:42 +00002617 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002618 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002619 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002620 }
2621
John McCall42a4f662010-05-28 08:11:17 +00002622 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002623 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2624 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002625
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002626 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002627 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002628 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002629}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002630
2631/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2632/// which explicitly initializes the members or base classes of a
2633/// class (C++ [class.base.init]). For example, the three initializers
2634/// after the ':' in the Derived constructor below:
2635///
2636/// @code
2637/// class Base { };
2638/// class Derived : Base {
2639/// int x;
2640/// float f;
2641/// public:
2642/// Derived(float f) : Base(), x(17), f(f) { }
2643/// };
2644/// @endcode
2645///
Mike Stump1eb44332009-09-09 15:08:12 +00002646/// [C++] ctor-initializer:
2647/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002648///
Mike Stump1eb44332009-09-09 15:08:12 +00002649/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002650/// mem-initializer ...[opt]
2651/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002652void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002653 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2654
John Wiegley28bbe4b2011-04-28 01:08:34 +00002655 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2656 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002657 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002658
Chris Lattner5f9e2722011-07-23 10:55:15 +00002659 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002660 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002661
Douglas Gregor7ad83902008-11-05 04:29:56 +00002662 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002663 if (Tok.is(tok::code_completion)) {
2664 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2665 MemInitializers.data(),
2666 MemInitializers.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002667 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002668 } else {
2669 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2670 if (!MemInit.isInvalid())
2671 MemInitializers.push_back(MemInit.get());
2672 else
2673 AnyErrors = true;
2674 }
2675
Douglas Gregor7ad83902008-11-05 04:29:56 +00002676 if (Tok.is(tok::comma))
2677 ConsumeToken();
2678 else if (Tok.is(tok::l_brace))
2679 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002680 // If the next token looks like a base or member initializer, assume that
2681 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002682 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2683 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2684 Diag(Loc, diag::err_ctor_init_missing_comma)
2685 << FixItHint::CreateInsertion(Loc, ", ");
2686 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002687 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002688 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002689 SkipUntil(tok::l_brace, true, true);
2690 break;
2691 }
2692 } while (true);
2693
David Blaikie93c86172013-01-17 05:26:25 +00002694 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002695 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002696}
2697
2698/// ParseMemInitializer - Parse a C++ member initializer, which is
2699/// part of a constructor initializer that explicitly initializes one
2700/// member or base class (C++ [class.base.init]). See
2701/// ParseConstructorInitializer for an example.
2702///
2703/// [C++] mem-initializer:
2704/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002705/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002706///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002707/// [C++] mem-initializer-id:
2708/// '::'[opt] nested-name-specifier[opt] class-name
2709/// identifier
John McCalld226f652010-08-21 09:40:31 +00002710Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002711 // parse '::'[opt] nested-name-specifier[opt]
2712 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002713 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002714 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002715 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002716 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002717 if (TemplateId->Kind == TNK_Type_template ||
2718 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002719 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002720 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002721 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002722 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002723 }
David Blaikief2116622012-01-24 06:03:59 +00002724 // Uses of decltype will already have been converted to annot_decltype by
2725 // ParseOptionalCXXScopeSpecifier at this point.
2726 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2727 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002728 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002729 return true;
2730 }
Mike Stump1eb44332009-09-09 15:08:12 +00002731
David Blaikief2116622012-01-24 06:03:59 +00002732 IdentifierInfo *II = 0;
2733 DeclSpec DS(AttrFactory);
2734 SourceLocation IdLoc = Tok.getLocation();
2735 if (Tok.is(tok::annot_decltype)) {
2736 // Get the decltype expression, if there is one.
2737 ParseDecltypeSpecifier(DS);
2738 } else {
2739 if (Tok.is(tok::identifier))
2740 // Get the identifier. This may be a member name or a class name,
2741 // but we'll let the semantic analysis determine which it is.
2742 II = Tok.getIdentifierInfo();
2743 ConsumeToken();
2744 }
2745
Douglas Gregor7ad83902008-11-05 04:29:56 +00002746
2747 // Parse the '('.
Richard Smith80ad52f2013-01-02 11:42:31 +00002748 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002749 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2750
Sebastian Redl6df65482011-09-24 17:48:25 +00002751 ExprResult InitList = ParseBraceInitializer();
2752 if (InitList.isInvalid())
2753 return true;
2754
2755 SourceLocation EllipsisLoc;
2756 if (Tok.is(tok::ellipsis))
2757 EllipsisLoc = ConsumeToken();
2758
2759 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002760 TemplateTypeTy, DS, IdLoc,
2761 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002762 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002763 BalancedDelimiterTracker T(*this, tok::l_paren);
2764 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002765
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002766 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002767 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002768 CommaLocsTy CommaLocs;
2769 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2770 SkipUntil(tok::r_paren);
2771 return true;
2772 }
2773
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002774 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002775
2776 SourceLocation EllipsisLoc;
2777 if (Tok.is(tok::ellipsis))
2778 EllipsisLoc = ConsumeToken();
2779
2780 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002781 TemplateTypeTy, DS, IdLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002782 T.getOpenLocation(), ArgExprs.data(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002783 ArgExprs.size(), T.getCloseLocation(),
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002784 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002785 }
2786
Richard Smith80ad52f2013-01-02 11:42:31 +00002787 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002788 : diag::err_expected_lparen);
2789 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002790}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002791
Sebastian Redl7acafd02011-03-05 14:45:16 +00002792/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002793///
Douglas Gregora4745612008-12-01 18:00:20 +00002794/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002795/// dynamic-exception-specification
2796/// noexcept-specification
2797///
2798/// noexcept-specification:
2799/// 'noexcept'
2800/// 'noexcept' '(' constant-expression ')'
2801ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002802Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002803 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002804 SmallVectorImpl<ParsedType> &DynamicExceptions,
2805 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002806 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002807 ExceptionSpecificationType Result = EST_None;
2808
2809 // See if there's a dynamic specification.
2810 if (Tok.is(tok::kw_throw)) {
2811 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2812 DynamicExceptions,
2813 DynamicExceptionRanges);
2814 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2815 "Produced different number of exception types and ranges.");
2816 }
2817
2818 // If there's no noexcept specification, we're done.
2819 if (Tok.isNot(tok::kw_noexcept))
2820 return Result;
2821
Richard Smith841804b2011-10-17 23:06:20 +00002822 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2823
Sebastian Redl7acafd02011-03-05 14:45:16 +00002824 // If we already had a dynamic specification, parse the noexcept for,
2825 // recovery, but emit a diagnostic and don't store the results.
2826 SourceRange NoexceptRange;
2827 ExceptionSpecificationType NoexceptType = EST_None;
2828
2829 SourceLocation KeywordLoc = ConsumeToken();
2830 if (Tok.is(tok::l_paren)) {
2831 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002832 BalancedDelimiterTracker T(*this, tok::l_paren);
2833 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002834 NoexceptType = EST_ComputedNoexcept;
2835 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002836 // The argument must be contextually convertible to bool. We use
2837 // ActOnBooleanCondition for this purpose.
2838 if (!NoexceptExpr.isInvalid())
2839 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2840 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002841 T.consumeClose();
2842 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002843 } else {
2844 // There is no argument.
2845 NoexceptType = EST_BasicNoexcept;
2846 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2847 }
2848
2849 if (Result == EST_None) {
2850 SpecificationRange = NoexceptRange;
2851 Result = NoexceptType;
2852
2853 // If there's a dynamic specification after a noexcept specification,
2854 // parse that and ignore the results.
2855 if (Tok.is(tok::kw_throw)) {
2856 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2857 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2858 DynamicExceptionRanges);
2859 }
2860 } else {
2861 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2862 }
2863
2864 return Result;
2865}
2866
2867/// ParseDynamicExceptionSpecification - Parse a C++
2868/// dynamic-exception-specification (C++ [except.spec]).
2869///
2870/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002871/// 'throw' '(' type-id-list [opt] ')'
2872/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002873///
Douglas Gregora4745612008-12-01 18:00:20 +00002874/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002875/// type-id ... [opt]
2876/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002877///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002878ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2879 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002880 SmallVectorImpl<ParsedType> &Exceptions,
2881 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002882 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002883
Sebastian Redl7acafd02011-03-05 14:45:16 +00002884 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002885 BalancedDelimiterTracker T(*this, tok::l_paren);
2886 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002887 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2888 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002889 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002890 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002891
Douglas Gregora4745612008-12-01 18:00:20 +00002892 // Parse throw(...), a Microsoft extension that means "this function
2893 // can throw anything".
2894 if (Tok.is(tok::ellipsis)) {
2895 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002896 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002897 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002898 T.consumeClose();
2899 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002900 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002901 }
2902
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002903 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002904 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002905 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002906 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002907
Douglas Gregora04426c2010-12-20 23:57:46 +00002908 if (Tok.is(tok::ellipsis)) {
2909 // C++0x [temp.variadic]p5:
2910 // - In a dynamic-exception-specification (15.4); the pattern is a
2911 // type-id.
2912 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002913 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002914 if (!Res.isInvalid())
2915 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2916 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002917
Sebastian Redlef65f062009-05-29 18:02:33 +00002918 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002919 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002920 Ranges.push_back(Range);
2921 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002922
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002923 if (Tok.is(tok::comma))
2924 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002925 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002926 break;
2927 }
2928
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002929 T.consumeClose();
2930 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002931 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002932}
Douglas Gregor6569d682009-05-27 23:11:45 +00002933
Douglas Gregordab60ad2010-10-01 18:44:50 +00002934/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2935/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002936TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002937 assert(Tok.is(tok::arrow) && "expected arrow");
2938
2939 ConsumeToken();
2940
Richard Smith7796eb52012-03-12 08:56:40 +00002941 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002942}
2943
Douglas Gregor6569d682009-05-27 23:11:45 +00002944/// \brief We have just started parsing the definition of a new class,
2945/// so push that class onto our stack of classes that is currently
2946/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002947Sema::ParsingClassState
John McCalle402e722012-09-25 07:32:39 +00002948Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
2949 bool IsInterface) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002950 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002951 "Nested class without outer class");
John McCalle402e722012-09-25 07:32:39 +00002952 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCalleee1d542011-02-14 07:13:47 +00002953 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002954}
2955
2956/// \brief Deallocate the given parsed class and all of its nested
2957/// classes.
2958void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002959 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2960 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002961 delete Class;
2962}
2963
2964/// \brief Pop the top class of the stack of classes that are
2965/// currently being parsed.
2966///
2967/// This routine should be called when we have finished parsing the
2968/// definition of a class, but have not yet popped the Scope
2969/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00002970void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002971 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002972
John McCalleee1d542011-02-14 07:13:47 +00002973 Actions.PopParsingClass(state);
2974
Douglas Gregor6569d682009-05-27 23:11:45 +00002975 ParsingClass *Victim = ClassStack.top();
2976 ClassStack.pop();
2977 if (Victim->TopLevelClass) {
2978 // Deallocate all of the nested classes of this class,
2979 // recursively: we don't need to keep any of this information.
2980 DeallocateParsedClasses(Victim);
2981 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002982 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002983 assert(!ClassStack.empty() && "Missing top-level class?");
2984
Douglas Gregord54eb442010-10-12 16:25:54 +00002985 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002986 // The victim is a nested class, but we will not need to perform
2987 // any processing after the definition of this class since it has
2988 // no members whose handling was delayed. Therefore, we can just
2989 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002990 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002991 return;
2992 }
2993
2994 // This nested class has some members that will need to be processed
2995 // after the top-level class is completely defined. Therefore, add
2996 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002997 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002998 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002999 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00003000}
Sean Huntbbd37c62009-11-21 08:43:09 +00003001
Richard Smithc56298d2012-04-10 03:25:07 +00003002/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3003///
3004/// \return the parsed identifier on success, and 0 if the next token is not an
3005/// attribute-token.
3006///
3007/// C++11 [dcl.attr.grammar]p3:
3008/// If a keyword or an alternative token that satisfies the syntactic
3009/// requirements of an identifier is contained in an attribute-token,
3010/// it is considered an identifier.
3011IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3012 switch (Tok.getKind()) {
3013 default:
3014 // Identifiers and keywords have identifier info attached.
3015 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3016 Loc = ConsumeToken();
3017 return II;
3018 }
3019 return 0;
3020
3021 case tok::ampamp: // 'and'
3022 case tok::pipe: // 'bitor'
3023 case tok::pipepipe: // 'or'
3024 case tok::caret: // 'xor'
3025 case tok::tilde: // 'compl'
3026 case tok::amp: // 'bitand'
3027 case tok::ampequal: // 'and_eq'
3028 case tok::pipeequal: // 'or_eq'
3029 case tok::caretequal: // 'xor_eq'
3030 case tok::exclaim: // 'not'
3031 case tok::exclaimequal: // 'not_eq'
3032 // Alternative tokens do not have identifier info, but their spelling
3033 // starts with an alphabetical character.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003034 SmallString<8> SpellingBuf;
Richard Smithc56298d2012-04-10 03:25:07 +00003035 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rose3f6f51e2013-02-08 22:30:41 +00003036 if (isLetter(Spelling[0])) {
Richard Smithc56298d2012-04-10 03:25:07 +00003037 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00003038 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00003039 }
3040 return 0;
3041 }
3042}
3043
Michael Han6880f492012-10-03 01:56:22 +00003044static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3045 IdentifierInfo *ScopeName) {
3046 switch (AttributeList::getKind(AttrName, ScopeName,
3047 AttributeList::AS_CXX11)) {
3048 case AttributeList::AT_CarriesDependency:
3049 case AttributeList::AT_FallThrough:
Richard Smithcd8ab512013-01-17 01:30:42 +00003050 case AttributeList::AT_CXX11NoReturn: {
Michael Han6880f492012-10-03 01:56:22 +00003051 return true;
3052 }
3053
3054 default:
3055 return false;
3056 }
3057}
3058
Richard Smithc56298d2012-04-10 03:25:07 +00003059/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003060/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00003061///
Richard Smith6ee326a2012-04-10 01:32:12 +00003062/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00003063/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003064/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00003065///
Richard Smith6ee326a2012-04-10 01:32:12 +00003066/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00003067/// attribute[opt]
3068/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00003069/// attribute '...'
3070/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00003071///
Richard Smith6ee326a2012-04-10 01:32:12 +00003072/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00003073/// attribute-token attribute-argument-clause[opt]
3074///
Richard Smith6ee326a2012-04-10 01:32:12 +00003075/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003076/// identifier
3077/// attribute-scoped-token
3078///
Richard Smith6ee326a2012-04-10 01:32:12 +00003079/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003080/// attribute-namespace '::' identifier
3081///
Richard Smith6ee326a2012-04-10 01:32:12 +00003082/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00003083/// identifier
3084///
Richard Smith6ee326a2012-04-10 01:32:12 +00003085/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00003086/// '(' balanced-token-seq ')'
3087///
Richard Smith6ee326a2012-04-10 01:32:12 +00003088/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00003089/// balanced-token
3090/// balanced-token-seq balanced-token
3091///
Richard Smith6ee326a2012-04-10 01:32:12 +00003092/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003093/// '(' balanced-token-seq ')'
3094/// '[' balanced-token-seq ']'
3095/// '{' balanced-token-seq '}'
3096/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00003097void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003098 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003099 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00003100 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003101 ParseAlignmentSpecifier(attrs, endLoc);
3102 return;
3103 }
3104
Sean Huntbbd37c62009-11-21 08:43:09 +00003105 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00003106 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00003107
Richard Smith41be6732011-10-14 20:48:27 +00003108 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3109
Sean Huntbbd37c62009-11-21 08:43:09 +00003110 ConsumeBracket();
3111 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003112
Richard Smithcd8ab512013-01-17 01:30:42 +00003113 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3114
Richard Smithc56298d2012-04-10 03:25:07 +00003115 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003116 // attribute not present
3117 if (Tok.is(tok::comma)) {
3118 ConsumeToken();
3119 continue;
3120 }
3121
Richard Smithc56298d2012-04-10 03:25:07 +00003122 SourceLocation ScopeLoc, AttrLoc;
3123 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3124
3125 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3126 if (!AttrName)
3127 // Break out to the "expected ']'" diagnostic.
3128 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003129
Sean Huntbbd37c62009-11-21 08:43:09 +00003130 // scoped attribute
3131 if (Tok.is(tok::coloncolon)) {
3132 ConsumeToken();
3133
Richard Smithc56298d2012-04-10 03:25:07 +00003134 ScopeName = AttrName;
3135 ScopeLoc = AttrLoc;
3136
3137 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3138 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003139 Diag(Tok.getLocation(), diag::err_expected_ident);
3140 SkipUntil(tok::r_square, tok::comma, true, true);
3141 continue;
3142 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003143 }
3144
Michael Han6880f492012-10-03 01:56:22 +00003145 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Sean Huntbbd37c62009-11-21 08:43:09 +00003146 bool AttrParsed = false;
Sean Huntbbd37c62009-11-21 08:43:09 +00003147
Richard Smithcd8ab512013-01-17 01:30:42 +00003148 if (StandardAttr &&
3149 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3150 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3151 << AttrName << SourceRange(SeenAttrs[AttrName]);
3152
Michael Han6880f492012-10-03 01:56:22 +00003153 // Parse attribute arguments
3154 if (Tok.is(tok::l_paren)) {
3155 if (ScopeName && ScopeName->getName() == "gnu") {
3156 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3157 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3158 AttrParsed = true;
3159 } else {
3160 if (StandardAttr)
3161 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3162 << AttrName->getName();
3163
3164 // FIXME: handle other formats of c++11 attribute arguments
3165 ConsumeParen();
3166 SkipUntil(tok::r_paren, false);
3167 }
3168 }
3169
3170 if (!AttrParsed)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00003171 attrs.addNew(AttrName,
3172 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3173 AttrLoc),
3174 ScopeName, ScopeLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00003175 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smith6ee326a2012-04-10 01:32:12 +00003176
Richard Smithc56298d2012-04-10 03:25:07 +00003177 if (Tok.is(tok::ellipsis)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003178 ConsumeToken();
Michael Han6880f492012-10-03 01:56:22 +00003179
3180 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3181 << AttrName->getName();
Richard Smithc56298d2012-04-10 03:25:07 +00003182 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003183 }
3184
3185 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3186 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003187 if (endLoc)
3188 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00003189 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3190 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003191}
Sean Huntbbd37c62009-11-21 08:43:09 +00003192
Sean Hunt2edf0a22012-06-23 05:07:58 +00003193/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003194///
3195/// attribute-specifier-seq:
3196/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00003197void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003198 SourceLocation *endLoc) {
3199 SourceLocation StartLoc = Tok.getLocation(), Loc;
3200 if (!endLoc)
3201 endLoc = &Loc;
3202
Douglas Gregor8828ee72011-10-07 20:35:25 +00003203 do {
Richard Smithc56298d2012-04-10 03:25:07 +00003204 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003205 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003206
3207 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003208}
3209
Francois Pichet334d47e2010-10-11 12:59:39 +00003210/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3211///
3212/// [MS] ms-attribute:
3213/// '[' token-seq ']'
3214///
3215/// [MS] ms-attribute-seq:
3216/// ms-attribute[opt]
3217/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003218void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3219 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003220 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3221
3222 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003223 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003224 ConsumeBracket();
3225 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003226 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003227 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3228 }
3229}
Francois Pichet563a6452011-05-25 10:19:49 +00003230
3231void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3232 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003233 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003234 if (ParseMicrosoftIfExistsCondition(Result))
3235 return;
3236
Douglas Gregor3896fc52011-10-24 22:31:10 +00003237 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3238 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003239 Diag(Tok, diag::err_expected_lbrace);
3240 return;
3241 }
Francois Pichet563a6452011-05-25 10:19:49 +00003242
Douglas Gregor3896fc52011-10-24 22:31:10 +00003243 switch (Result.Behavior) {
3244 case IEB_Parse:
3245 // Parse the declarations below.
3246 break;
3247
3248 case IEB_Dependent:
3249 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3250 << Result.IsIfExists;
3251 // Fall through to skip.
3252
3253 case IEB_Skip:
3254 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003255 return;
3256 }
3257
Douglas Gregor3896fc52011-10-24 22:31:10 +00003258 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003259 // __if_exists, __if_not_exists can nest.
3260 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3261 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3262 continue;
3263 }
3264
3265 // Check for extraneous top-level semicolon.
3266 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003267 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003268 continue;
3269 }
3270
3271 AccessSpecifier AS = getAccessSpecifierIfPresent();
3272 if (AS != AS_none) {
3273 // Current token is a C++ access specifier.
3274 CurAS = AS;
3275 SourceLocation ASLoc = Tok.getLocation();
3276 ConsumeToken();
3277 if (Tok.is(tok::colon))
3278 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3279 else
3280 Diag(Tok, diag::err_expected_colon);
3281 ConsumeToken();
3282 continue;
3283 }
3284
3285 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003286 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003287 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003288
3289 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003290}