blob: 22f5863e5f6acc4415f64511ad174d5cd7c1b4cd [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor1b7f8982008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/OperatorKinds.h"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
John McCall19510852010-08-20 18:27:03 +000020#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Sema/Scope.h"
John McCalle402e722012-09-25 07:32:39 +000023#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000025using namespace clang;
26
27/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000028/// may either be a top level namespace or a block-level namespace alias. If
29/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000030///
31/// namespace-definition: [C++ 7.3: basic.namespace]
32/// named-namespace-definition
33/// unnamed-namespace-definition
34///
35/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000036/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000037///
38/// named-namespace-definition:
39/// original-namespace-definition
40/// extension-namespace-definition
41///
42/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000043/// 'inline'[opt] 'namespace' identifier attributes[opt]
44/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000045///
46/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000047/// 'inline'[opt] 'namespace' original-namespace-name
48/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000049///
Chris Lattner8f08cb72007-08-25 06:57:03 +000050/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
51/// 'namespace' identifier '=' qualified-namespace-specifier ';'
52///
John McCalld226f652010-08-21 09:40:31 +000053Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000054 SourceLocation &DeclEnd,
55 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000056 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000057 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +000058 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000059
Douglas Gregor49f40bd2009-09-18 19:03:04 +000060 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000061 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000062 cutOffParsing();
63 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +000064 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000065
Chris Lattner8f08cb72007-08-25 06:57:03 +000066 SourceLocation IdentLoc;
67 IdentifierInfo *Ident = 0;
Richard Trieuf858bd82011-05-26 20:11:09 +000068 std::vector<SourceLocation> ExtraIdentLoc;
69 std::vector<IdentifierInfo*> ExtraIdent;
70 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000071
72 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattner04d66662007-10-09 17:33:22 +000074 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000075 Ident = Tok.getIdentifierInfo();
76 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieuf858bd82011-05-26 20:11:09 +000077 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
78 ExtraNamespaceLoc.push_back(ConsumeToken());
79 ExtraIdent.push_back(Tok.getIdentifierInfo());
80 ExtraIdentLoc.push_back(ConsumeToken());
81 }
Chris Lattner8f08cb72007-08-25 06:57:03 +000082 }
Mike Stump1eb44332009-09-09 15:08:12 +000083
Chris Lattner8f08cb72007-08-25 06:57:03 +000084 // Read label attributes, if present.
John McCall0b7e6782011-03-24 11:26:52 +000085 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000086 if (Tok.is(tok::kw___attribute)) {
87 attrTok = Tok;
John McCall7f040a92010-12-24 02:08:15 +000088 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000089 }
Mike Stump1eb44332009-09-09 15:08:12 +000090
Douglas Gregor6a588dd2009-06-17 19:49:00 +000091 if (Tok.is(tok::equal)) {
Nico Webere1bb3292012-10-27 23:44:27 +000092 if (Ident == 0) {
93 Diag(Tok, diag::err_expected_ident);
94 // Skip to end of the definition and eat the ';'.
95 SkipUntil(tok::semi);
96 return 0;
97 }
John McCall7f040a92010-12-24 02:08:15 +000098 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +000099 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +0000100 if (InlineLoc.isValid())
101 Diag(InlineLoc, diag::err_inline_namespace_alias)
102 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000103 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000104 }
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Richard Trieuf858bd82011-05-26 20:11:09 +0000106
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000107 BalancedDelimiterTracker T(*this, tok::l_brace);
108 if (T.consumeOpen()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000109 if (!ExtraIdent.empty()) {
110 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
111 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
112 }
Mike Stump1eb44332009-09-09 15:08:12 +0000113 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +0000114 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +0000115 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Douglas Gregor23c94db2010-07-02 17:43:08 +0000118 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
119 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
120 getCurScope()->getFnParent()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000121 if (!ExtraIdent.empty()) {
122 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
123 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
124 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000125 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Douglas Gregor95f1b152010-05-14 05:08:22 +0000126 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000127 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000128 }
129
Richard Trieuf858bd82011-05-26 20:11:09 +0000130 if (!ExtraIdent.empty()) {
131 TentativeParsingAction TPA(*this);
132 SkipUntil(tok::r_brace, /*StopAtSemi*/false, /*DontConsume*/true);
133 Token rBraceToken = Tok;
134 TPA.Revert();
135
136 if (!rBraceToken.is(tok::r_brace)) {
137 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
138 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
139 } else {
Benjamin Kramer9910df02011-05-26 21:32:30 +0000140 std::string NamespaceFix;
Richard Trieuf858bd82011-05-26 20:11:09 +0000141 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
142 E = ExtraIdent.end(); I != E; ++I) {
143 NamespaceFix += " { namespace ";
144 NamespaceFix += (*I)->getName();
145 }
Benjamin Kramer9910df02011-05-26 21:32:30 +0000146
Richard Trieuf858bd82011-05-26 20:11:09 +0000147 std::string RBraces;
Benjamin Kramer9910df02011-05-26 21:32:30 +0000148 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieuf858bd82011-05-26 20:11:09 +0000149 RBraces += "} ";
Benjamin Kramer9910df02011-05-26 21:32:30 +0000150
Richard Trieuf858bd82011-05-26 20:11:09 +0000151 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
152 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
153 ExtraIdentLoc.back()),
154 NamespaceFix)
155 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
156 }
157 }
158
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000159 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith7fe62082011-10-15 05:09:34 +0000160 if (InlineLoc.isValid())
Richard Smith80ad52f2013-01-02 11:42:31 +0000161 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000162 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000163
Chris Lattner51448322009-03-29 14:02:43 +0000164 // Enter a scope for the namespace.
165 ParseScope NamespaceScope(this, Scope::DeclScope);
166
John McCalld226f652010-08-21 09:40:31 +0000167 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000168 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000169 IdentLoc, Ident, T.getOpenLocation(),
170 attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000171
John McCallf312b1e2010-08-26 23:41:50 +0000172 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
173 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Richard Trieuf858bd82011-05-26 20:11:09 +0000175 // Parse the contents of the namespace. This includes parsing recovery on
176 // any improperly nested namespaces.
177 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000178 InlineLoc, attrs, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattner51448322009-03-29 14:02:43 +0000180 // Leave the namespace scope.
181 NamespaceScope.Exit();
182
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000183 DeclEnd = T.getCloseLocation();
184 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner51448322009-03-29 14:02:43 +0000185
186 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000187}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000188
Richard Trieuf858bd82011-05-26 20:11:09 +0000189/// ParseInnerNamespace - Parse the contents of a namespace.
190void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
191 std::vector<IdentifierInfo*>& Ident,
192 std::vector<SourceLocation>& NamespaceLoc,
193 unsigned int index, SourceLocation& InlineLoc,
Richard Trieuf858bd82011-05-26 20:11:09 +0000194 ParsedAttributes& attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000195 BalancedDelimiterTracker &Tracker) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000196 if (index == Ident.size()) {
197 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
198 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000199 MaybeParseCXX11Attributes(attrs);
Richard Trieuf858bd82011-05-26 20:11:09 +0000200 MaybeParseMicrosoftAttributes(attrs);
201 ParseExternalDeclaration(attrs);
202 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000203
204 // The caller is what called check -- we are simply calling
205 // the close for it.
206 Tracker.consumeClose();
Richard Trieuf858bd82011-05-26 20:11:09 +0000207
208 return;
209 }
210
211 // Parse improperly nested namespaces.
212 ParseScope NamespaceScope(this, Scope::DeclScope);
213 Decl *NamespcDecl =
214 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
215 NamespaceLoc[index], IdentLoc[index],
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000216 Ident[index], Tracker.getOpenLocation(),
217 attrs.getList());
Richard Trieuf858bd82011-05-26 20:11:09 +0000218
219 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000220 attrs, Tracker);
Richard Trieuf858bd82011-05-26 20:11:09 +0000221
222 NamespaceScope.Exit();
223
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000224 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieuf858bd82011-05-26 20:11:09 +0000225}
226
Anders Carlssonf67606a2009-03-28 04:07:16 +0000227/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
228/// alias definition.
229///
John McCalld226f652010-08-21 09:40:31 +0000230Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000231 SourceLocation AliasLoc,
232 IdentifierInfo *Alias,
233 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000234 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Anders Carlssonf67606a2009-03-28 04:07:16 +0000236 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000238 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000239 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000240 cutOffParsing();
241 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000242 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000243
Anders Carlssonf67606a2009-03-28 04:07:16 +0000244 CXXScopeSpec SS;
245 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000246 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000247
248 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
249 Diag(Tok, diag::err_expected_namespace_name);
250 // Skip to end of the definition and eat the ';'.
251 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000252 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000253 }
254
255 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000256 IdentifierInfo *Ident = Tok.getIdentifierInfo();
257 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Anders Carlssonf67606a2009-03-28 04:07:16 +0000259 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000260 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000261 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
262 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Douglas Gregor23c94db2010-07-02 17:43:08 +0000264 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000265 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000266}
267
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000268/// ParseLinkage - We know that the current token is a string_literal
269/// and just before that, that extern was seen.
270///
271/// linkage-specification: [C++ 7.5p2: dcl.link]
272/// 'extern' string-literal '{' declaration-seq[opt] '}'
273/// 'extern' string-literal declaration
274///
Chris Lattner7d642712010-11-09 20:15:55 +0000275Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000276 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000277 SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000278 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000279 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000280 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000281 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000282
Richard Smith99831e42012-03-06 03:21:47 +0000283 // FIXME: This is incorrect: linkage-specifiers are parsed in translation
284 // phase 7, so string-literal concatenation is supposed to occur.
285 // extern "" "C" "" "+" "+" { } is legal.
286 if (Tok.hasUDSuffix())
287 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000288 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000289
Douglas Gregor074149e2009-01-05 19:45:36 +0000290 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000291 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000292 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000293 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000294 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000295 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000296 : SourceLocation());
297
John McCall0b7e6782011-03-24 11:26:52 +0000298 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000299 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000300 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000301
Douglas Gregor074149e2009-01-05 19:45:36 +0000302 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-05-01 16:25:54 +0000303 // Reset the source range in DS, as the leading "extern"
304 // does not really belong to the inner declaration ...
305 DS.SetRangeStart(SourceLocation());
306 DS.SetRangeEnd(SourceLocation());
307 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000308 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000309 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000310 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000311 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000312 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000313
Douglas Gregor63a01132010-02-07 08:38:28 +0000314 DS.abort();
315
John McCall7f040a92010-12-24 02:08:15 +0000316 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000317
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000318 BalancedDelimiterTracker T(*this, tok::l_brace);
319 T.consumeOpen();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000320 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000321 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000322 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000323 MaybeParseMicrosoftAttributes(attrs);
324 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000325 }
326
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000327 T.consumeClose();
Chris Lattner7d642712010-11-09 20:15:55 +0000328 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000329 T.getCloseLocation());
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000330}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000331
Douglas Gregorf780abc2008-12-30 03:27:21 +0000332/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
333/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000334Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000335 const ParsedTemplateInfo &TemplateInfo,
336 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000337 ParsedAttributesWithRange &attrs,
338 Decl **OwnedType) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000339 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000340 ObjCDeclContextSwitch ObjCDC(*this);
341
Douglas Gregorf780abc2008-12-30 03:27:21 +0000342 // Eat 'using'.
343 SourceLocation UsingLoc = ConsumeToken();
344
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000345 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000346 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000347 cutOffParsing();
348 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000349 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000350
John McCall78b81052010-11-10 02:40:36 +0000351 // 'using namespace' means this is a using-directive.
352 if (Tok.is(tok::kw_namespace)) {
353 // Template parameters are always an error here.
354 if (TemplateInfo.Kind) {
355 SourceRange R = TemplateInfo.getSourceRange();
356 Diag(UsingLoc, diag::err_templated_using_directive)
357 << R << FixItHint::CreateRemoval(R);
358 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000359
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000360 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000361 }
362
Richard Smith162e1c12011-04-15 14:24:37 +0000363 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000364
365 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000366 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000367
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000368 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000369 AS_none, OwnedType);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000370}
371
372/// ParseUsingDirective - Parse C++ using-directive, assumes
373/// that current token is 'namespace' and 'using' was already parsed.
374///
375/// using-directive: [C++ 7.3.p4: namespace.udir]
376/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
377/// namespace-name ;
378/// [GNU] using-directive:
379/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
380/// namespace-name attributes[opt] ;
381///
John McCalld226f652010-08-21 09:40:31 +0000382Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000383 SourceLocation UsingLoc,
384 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000385 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000386 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
387
388 // Eat 'namespace'.
389 SourceLocation NamespcLoc = ConsumeToken();
390
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000391 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000392 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000393 cutOffParsing();
394 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000395 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000396
Douglas Gregorf780abc2008-12-30 03:27:21 +0000397 CXXScopeSpec SS;
398 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000399 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000400
Douglas Gregorf780abc2008-12-30 03:27:21 +0000401 IdentifierInfo *NamespcName = 0;
402 SourceLocation IdentLoc = SourceLocation();
403
404 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000405 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000406 Diag(Tok, diag::err_expected_namespace_name);
407 // If there was invalid namespace name, skip to end of decl, and eat ';'.
408 SkipUntil(tok::semi);
409 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000410 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Chris Lattner823c44e2009-01-06 07:27:21 +0000413 // Parse identifier.
414 NamespcName = Tok.getIdentifierInfo();
415 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner823c44e2009-01-06 07:27:21 +0000417 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000418 bool GNUAttr = false;
419 if (Tok.is(tok::kw___attribute)) {
420 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000421 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattner823c44e2009-01-06 07:27:21 +0000424 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000425 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000426 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000427 GNUAttr ? diag::err_expected_semi_after_attribute_list
428 : diag::err_expected_semi_after_namespace_name,
429 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000430
Douglas Gregor23c94db2010-07-02 17:43:08 +0000431 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000432 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000433}
434
Richard Smith162e1c12011-04-15 14:24:37 +0000435/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
436/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000437///
438/// using-declaration: [C++ 7.3.p3: namespace.udecl]
439/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000440/// unqualified-id
441/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000442///
Richard Smithd03de6a2013-01-29 10:02:16 +0000443/// alias-declaration: C++11 [dcl.dcl]p1
444/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smith162e1c12011-04-15 14:24:37 +0000445///
John McCalld226f652010-08-21 09:40:31 +0000446Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000447 const ParsedTemplateInfo &TemplateInfo,
448 SourceLocation UsingLoc,
449 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000450 AccessSpecifier AS,
451 Decl **OwnedType) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000452 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000453 SourceLocation TypenameLoc;
Richard Smith6b3d3e52013-02-20 19:22:51 +0000454 bool IsTypeName = false;
455 ParsedAttributesWithRange Attrs(AttrFactory);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000456
457 // FIXME: Simply skip the attributes and diagnose, don't bother parsing them.
Richard Smith6b3d3e52013-02-20 19:22:51 +0000458 MaybeParseCXX11Attributes(Attrs);
459 ProhibitAttributes(Attrs);
460 Attrs.clear();
461 Attrs.Range = SourceRange();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000462
463 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000464 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000465 if (Tok.is(tok::kw_typename)) {
Richard Smith6b3d3e52013-02-20 19:22:51 +0000466 TypenameLoc = ConsumeToken();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000467 IsTypeName = true;
468 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000469
470 // Parse nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000471 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000472
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000473 // Check nested-name specifier.
474 if (SS.isInvalid()) {
475 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000476 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000477 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000478
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000479 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000480 // destructor names and allow the action module to diagnose any semantic
481 // errors.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000482 SourceLocation TemplateKWLoc;
Douglas Gregor12c118a2009-11-04 16:30:06 +0000483 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000484 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000485 /*EnteringContext=*/false,
486 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000487 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000488 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000489 TemplateKWLoc,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000490 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000491 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000492 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000493 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000494
Richard Smith6b3d3e52013-02-20 19:22:51 +0000495 MaybeParseCXX11Attributes(Attrs);
Richard Smith162e1c12011-04-15 14:24:37 +0000496
497 // Maybe this is an alias-declaration.
498 bool IsAliasDecl = Tok.is(tok::equal);
499 TypeResult TypeAlias;
500 if (IsAliasDecl) {
Richard Smith6b3d3e52013-02-20 19:22:51 +0000501 // TODO: Can GNU attributes appear here?
Richard Smith162e1c12011-04-15 14:24:37 +0000502 ConsumeToken();
503
Richard Smith80ad52f2013-01-02 11:42:31 +0000504 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000505 diag::warn_cxx98_compat_alias_declaration :
506 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000507
Richard Smith3e4c6c42011-05-05 21:57:07 +0000508 // Type alias templates cannot be specialized.
509 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000510 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
511 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000512 SpecKind = 0;
513 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
514 SpecKind = 1;
515 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
516 SpecKind = 2;
517 if (SpecKind != -1) {
518 SourceRange Range;
519 if (SpecKind == 0)
520 Range = SourceRange(Name.TemplateId->LAngleLoc,
521 Name.TemplateId->RAngleLoc);
522 else
523 Range = TemplateInfo.getSourceRange();
524 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
525 << SpecKind << Range;
526 SkipUntil(tok::semi);
527 return 0;
528 }
529
Richard Smith162e1c12011-04-15 14:24:37 +0000530 // Name must be an identifier.
531 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
532 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
533 // No removal fixit: can't recover from this.
534 SkipUntil(tok::semi);
535 return 0;
536 } else if (IsTypeName)
537 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
538 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
539 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
540 else if (SS.isNotEmpty())
541 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
542 << FixItHint::CreateRemoval(SS.getRange());
543
Richard Smith3e4c6c42011-05-05 21:57:07 +0000544 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
545 Declarator::AliasTemplateContext :
Richard Smith6b3d3e52013-02-20 19:22:51 +0000546 Declarator::AliasDeclContext, AS, OwnedType,
547 &Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000548 } else {
549 // C++11 attributes are not allowed on a using-declaration, but GNU ones
550 // are.
Richard Smith6b3d3e52013-02-20 19:22:51 +0000551 ProhibitAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000552
Richard Smith162e1c12011-04-15 14:24:37 +0000553 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith6b3d3e52013-02-20 19:22:51 +0000554 MaybeParseGNUAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000555 }
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000557 // Eat ';'.
558 DeclEnd = Tok.getLocation();
559 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000560 !Attrs.empty() ? "attributes list" :
Richard Smith162e1c12011-04-15 14:24:37 +0000561 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000562 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000563
John McCall78b81052010-11-10 02:40:36 +0000564 // Diagnose an attempt to declare a templated using-declaration.
Richard Smithd03de6a2013-01-29 10:02:16 +0000565 // In C++11, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000566 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000567 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000568 SourceRange R = TemplateInfo.getSourceRange();
569 Diag(UsingLoc, diag::err_templated_using_declaration)
570 << R << FixItHint::CreateRemoval(R);
571
572 // Unfortunately, we have to bail out instead of recovering by
573 // ignoring the parameters, just in case the nested name specifier
574 // depends on the parameters.
575 return 0;
576 }
577
Douglas Gregor480b53c2011-09-26 14:30:28 +0000578 // "typename" keyword is allowed for identifiers only,
579 // because it may be a type definition.
580 if (IsTypeName && Name.getKind() != UnqualifiedId::IK_Identifier) {
581 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
582 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
583 // Proceed parsing, but reset the IsTypeName flag.
584 IsTypeName = false;
585 }
586
Richard Smith3e4c6c42011-05-05 21:57:07 +0000587 if (IsAliasDecl) {
588 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramer5354e772012-08-23 23:38:35 +0000589 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3e4c6c42011-05-05 21:57:07 +0000590 TemplateParams ? TemplateParams->data() : 0,
591 TemplateParams ? TemplateParams->size() : 0);
592 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000593 UsingLoc, Name, Attrs.getList(),
594 TypeAlias);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000595 }
Richard Smith162e1c12011-04-15 14:24:37 +0000596
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000597 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000598 Name, Attrs.getList(),
John McCall7f040a92010-12-24 02:08:15 +0000599 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000600}
601
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000602/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000603///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000604/// [C++0x] static_assert-declaration:
605/// static_assert ( constant-expression , string-literal ) ;
606///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000607/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000608/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000609///
John McCalld226f652010-08-21 09:40:31 +0000610Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000611 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
612 "Not a static_assert declaration");
613
David Blaikie4e4d0842012-03-11 07:00:24 +0000614 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000615 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000616 if (Tok.is(tok::kw_static_assert))
617 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000618
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000619 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000621 BalancedDelimiterTracker T(*this, tok::l_paren);
622 if (T.consumeOpen()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000623 Diag(Tok, diag::err_expected_lparen);
Richard Smith3686c712012-09-13 19:12:50 +0000624 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000625 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000626 }
Mike Stump1eb44332009-09-09 15:08:12 +0000627
John McCall60d7b3a2010-08-24 06:29:42 +0000628 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000629 if (AssertExpr.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000630 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000631 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Anders Carlssonad5f9602009-03-13 23:29:20 +0000634 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000635 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000636
Richard Smith0cc323c2012-03-05 23:20:05 +0000637 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000638 Diag(Tok, diag::err_expected_string_literal)
639 << /*Source='static_assert'*/1;
Richard Smith3686c712012-09-13 19:12:50 +0000640 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000641 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
John McCall60d7b3a2010-08-24 06:29:42 +0000644 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smith99831e42012-03-06 03:21:47 +0000645 if (AssertMessage.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000646 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000647 return 0;
Richard Smith99831e42012-03-06 03:21:47 +0000648 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000649
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000650 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Chris Lattner97144fc2009-04-02 04:16:50 +0000652 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000653 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000654
John McCall9ae2f072010-08-23 23:25:46 +0000655 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
656 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000657 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000658 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000659}
660
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000661/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
662///
663/// 'decltype' ( expression )
664///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000665SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
666 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
667 && "Not a decltype specifier");
668
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000669
David Blaikie42d6d0c2011-12-04 05:04:18 +0000670 ExprResult Result;
671 SourceLocation StartLoc = Tok.getLocation();
672 SourceLocation EndLoc;
673
674 if (Tok.is(tok::annot_decltype)) {
675 Result = getExprAnnotation(Tok);
676 EndLoc = Tok.getAnnotationEndLoc();
677 ConsumeToken();
678 if (Result.isInvalid()) {
679 DS.SetTypeSpecError();
680 return EndLoc;
681 }
682 } else {
Richard Smithc7b55432012-02-24 22:30:04 +0000683 if (Tok.getIdentifierInfo()->isStr("decltype"))
684 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000685
David Blaikie42d6d0c2011-12-04 05:04:18 +0000686 ConsumeToken();
687
688 BalancedDelimiterTracker T(*this, tok::l_paren);
689 if (T.expectAndConsume(diag::err_expected_lparen_after,
690 "decltype", tok::r_paren)) {
691 DS.SetTypeSpecError();
692 return T.getOpenLocation() == Tok.getLocation() ?
693 StartLoc : T.getOpenLocation();
694 }
695
696 // Parse the expression
697
698 // C++0x [dcl.type.simple]p4:
699 // The operand of the decltype specifier is an unevaluated operand.
Richard Smith76f3f692012-02-22 02:04:18 +0000700 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
701 0, /*IsDecltype=*/true);
David Blaikie42d6d0c2011-12-04 05:04:18 +0000702 Result = ParseExpression();
703 if (Result.isInvalid()) {
David Blaikie42d6d0c2011-12-04 05:04:18 +0000704 DS.SetTypeSpecError();
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000705 if (SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true)) {
706 EndLoc = ConsumeParen();
707 } else {
Richard Smith569cdc82012-12-09 04:17:57 +0000708 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000709 // Backtrack to get the location of the last token before the semi.
710 PP.RevertCachedTokens(2);
711 ConsumeToken(); // the semi.
712 EndLoc = ConsumeAnyToken();
713 assert(Tok.is(tok::semi));
714 } else {
715 EndLoc = Tok.getLocation();
716 }
717 }
718 return EndLoc;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000719 }
720
721 // Match the ')'
722 T.consumeClose();
723 if (T.getCloseLocation().isInvalid()) {
724 DS.SetTypeSpecError();
725 // FIXME: this should return the location of the last token
726 // that was consumed (by "consumeClose()")
727 return T.getCloseLocation();
728 }
729
Richard Smith76f3f692012-02-22 02:04:18 +0000730 Result = Actions.ActOnDecltypeExpression(Result.take());
731 if (Result.isInvalid()) {
732 DS.SetTypeSpecError();
733 return T.getCloseLocation();
734 }
735
David Blaikie42d6d0c2011-12-04 05:04:18 +0000736 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000737 }
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000739 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000740 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000741 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000742 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
David Blaikie42d6d0c2011-12-04 05:04:18 +0000743 DiagID, Result.release())) {
John McCallfec54012009-08-03 20:12:06 +0000744 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000745 DS.SetTypeSpecError();
746 }
747 return EndLoc;
748}
749
750void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
751 SourceLocation StartLoc,
752 SourceLocation EndLoc) {
753 // make sure we have a token we can turn into an annotation token
754 if (PP.isBacktrackEnabled())
755 PP.RevertCachedTokens(1);
756 else
757 PP.EnterToken(Tok);
758
759 Tok.setKind(tok::annot_decltype);
760 setExprAnnotation(Tok, DS.getTypeSpecType() == TST_decltype ?
761 DS.getRepAsExpr() : ExprResult());
762 Tok.setAnnotationEndLoc(EndLoc);
763 Tok.setLocation(StartLoc);
764 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000765}
766
Sean Huntdb5d44b2011-05-19 05:37:45 +0000767void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
768 assert(Tok.is(tok::kw___underlying_type) &&
769 "Not an underlying type specifier");
770
771 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000772 BalancedDelimiterTracker T(*this, tok::l_paren);
773 if (T.expectAndConsume(diag::err_expected_lparen_after,
774 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000775 return;
776 }
777
778 TypeResult Result = ParseTypeName();
779 if (Result.isInvalid()) {
780 SkipUntil(tok::r_paren);
781 return;
782 }
783
784 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000785 T.consumeClose();
786 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000787 return;
788
789 const char *PrevSpec = 0;
790 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000791 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000792 DiagID, Result.release()))
793 Diag(StartLoc, DiagID) << PrevSpec;
794}
795
David Blaikie09048df2011-10-25 15:01:20 +0000796/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
797/// class name or decltype-specifier. Note that we only check that the result
798/// names a type; semantic analysis will need to verify that the type names a
799/// class. The result is either a type or null, depending on whether a type
800/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000801///
Richard Smith05321402013-02-19 23:47:15 +0000802/// base-type-specifier: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000803/// class-or-decltype
Richard Smith05321402013-02-19 23:47:15 +0000804/// class-or-decltype: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000805/// nested-name-specifier[opt] class-name
806/// decltype-specifier
Richard Smith05321402013-02-19 23:47:15 +0000807/// class-name: [C++ class.name]
Douglas Gregor42a552f2008-11-05 20:51:48 +0000808/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000809/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000810///
Richard Smith05321402013-02-19 23:47:15 +0000811/// In C++98, instead of base-type-specifier, we have:
812///
813/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie22216eb2011-10-25 17:10:12 +0000814Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
815 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000816 // Ignore attempts to use typename
817 if (Tok.is(tok::kw_typename)) {
818 Diag(Tok, diag::err_expected_class_name_not_template)
819 << FixItHint::CreateRemoval(Tok.getLocation());
820 ConsumeToken();
821 }
822
David Blaikie152aa4b2011-10-25 18:17:58 +0000823 // Parse optional nested-name-specifier
824 CXXScopeSpec SS;
825 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
826
827 BaseLoc = Tok.getLocation();
828
David Blaikie22216eb2011-10-25 17:10:12 +0000829 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000830 // tok == kw_decltype is just error recovery, it can only happen when SS
831 // isn't empty
832 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000833 if (SS.isNotEmpty())
834 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
835 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000836 // Fake up a Declarator to use with ActOnTypeName.
837 DeclSpec DS(AttrFactory);
838
David Blaikieb5777572011-12-08 04:53:15 +0000839 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000840
841 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
842 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
843 }
844
Douglas Gregor7f43d672009-02-25 23:52:28 +0000845 // Check whether we have a template-id that names a type.
846 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000847 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000848 if (TemplateId->Kind == TNK_Type_template ||
849 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000850 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000851
852 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000853 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000854 EndLocation = Tok.getAnnotationEndLoc();
855 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000856
857 if (Type)
858 return Type;
859 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000860 }
861
862 // Fall through to produce an error below.
863 }
864
Douglas Gregor42a552f2008-11-05 20:51:48 +0000865 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000866 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000867 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000868 }
869
Douglas Gregor84d0a192010-01-12 21:28:44 +0000870 IdentifierInfo *Id = Tok.getIdentifierInfo();
871 SourceLocation IdLoc = ConsumeToken();
872
873 if (Tok.is(tok::less)) {
874 // It looks the user intended to write a template-id here, but the
875 // template-name was wrong. Try to fix that.
876 TemplateNameKind TNK = TNK_Type_template;
877 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000878 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000879 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000880 Diag(IdLoc, diag::err_unknown_template_name)
881 << Id;
882 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000883
Douglas Gregor84d0a192010-01-12 21:28:44 +0000884 if (!Template)
885 return true;
886
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000887 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000888 UnqualifiedId TemplateName;
889 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000890
Douglas Gregor84d0a192010-01-12 21:28:44 +0000891 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000892 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
893 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000894 return true;
895 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000896 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000897
Douglas Gregor84d0a192010-01-12 21:28:44 +0000898 // If we didn't end up with a typename token, there's nothing more we
899 // can do.
900 if (Tok.isNot(tok::annot_typename))
901 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000902
Douglas Gregor84d0a192010-01-12 21:28:44 +0000903 // Retrieve the type from the annotation token, consume that token, and
904 // return.
905 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000906 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000907 ConsumeToken();
908 return Type;
909 }
910
Douglas Gregor42a552f2008-11-05 20:51:48 +0000911 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000912 IdentifierInfo *CorrectedII = 0;
Douglas Gregor059101f2011-03-02 00:47:37 +0000913 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000914 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000915 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000916 /*NonTrivialTypeSourceInfo=*/true,
917 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000918 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000919 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000920 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000921 }
922
923 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000924 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000925
926 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000927 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000928 DS.SetRangeStart(IdLoc);
929 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000930 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000931
932 const char *PrevSpec = 0;
933 unsigned DiagID;
934 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
935
936 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
937 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000938}
939
John McCallc052dbb2012-05-22 21:28:12 +0000940void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
941 while (Tok.is(tok::kw___single_inheritance) ||
942 Tok.is(tok::kw___multiple_inheritance) ||
943 Tok.is(tok::kw___virtual_inheritance)) {
944 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
945 SourceLocation AttrNameLoc = ConsumeToken();
946 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000947 SourceLocation(), 0, 0, AttributeList::AS_GNU);
John McCallc052dbb2012-05-22 21:28:12 +0000948 }
949}
950
Richard Smithc9f35172012-06-25 21:37:02 +0000951/// Determine whether the following tokens are valid after a type-specifier
952/// which could be a standalone declaration. This will conservatively return
953/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith139be702012-07-02 19:14:01 +0000954bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smithc9f35172012-06-25 21:37:02 +0000955 // This switch enumerates the valid "follow" set for type-specifiers.
956 switch (Tok.getKind()) {
957 default: break;
958 case tok::semi: // struct foo {...} ;
959 case tok::star: // struct foo {...} * P;
960 case tok::amp: // struct foo {...} & R = ...
Richard Smithba65f502013-01-19 03:48:05 +0000961 case tok::ampamp: // struct foo {...} && R = ...
Richard Smithc9f35172012-06-25 21:37:02 +0000962 case tok::identifier: // struct foo {...} V ;
963 case tok::r_paren: //(struct foo {...} ) {4}
964 case tok::annot_cxxscope: // struct foo {...} a:: b;
965 case tok::annot_typename: // struct foo {...} a ::b;
966 case tok::annot_template_id: // struct foo {...} a<int> ::b;
967 case tok::l_paren: // struct foo {...} ( x);
968 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smithba65f502013-01-19 03:48:05 +0000969 case tok::kw_operator: // struct foo operator ++() {...}
Richard Smithc9f35172012-06-25 21:37:02 +0000970 return true;
Richard Smith139be702012-07-02 19:14:01 +0000971 case tok::colon:
972 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smithc9f35172012-06-25 21:37:02 +0000973 // Type qualifiers
974 case tok::kw_const: // struct foo {...} const x;
975 case tok::kw_volatile: // struct foo {...} volatile x;
976 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smithba65f502013-01-19 03:48:05 +0000977 // Function specifiers
978 // Note, no 'explicit'. An explicit function must be either a conversion
979 // operator or a constructor. Either way, it can't have a return type.
980 case tok::kw_inline: // struct foo inline f();
981 case tok::kw_virtual: // struct foo virtual f();
982 case tok::kw_friend: // struct foo friend f();
Richard Smithc9f35172012-06-25 21:37:02 +0000983 // Storage-class specifiers
984 case tok::kw_static: // struct foo {...} static x;
985 case tok::kw_extern: // struct foo {...} extern x;
986 case tok::kw_typedef: // struct foo {...} typedef x;
987 case tok::kw_register: // struct foo {...} register x;
988 case tok::kw_auto: // struct foo {...} auto x;
989 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smithba65f502013-01-19 03:48:05 +0000990 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smithc9f35172012-06-25 21:37:02 +0000991 case tok::kw_constexpr: // struct foo {...} constexpr x;
992 // As shown above, type qualifiers and storage class specifiers absolutely
993 // can occur after class specifiers according to the grammar. However,
994 // almost no one actually writes code like this. If we see one of these,
995 // it is much more likely that someone missed a semi colon and the
996 // type/storage class specifier we're seeing is part of the *next*
997 // intended declaration, as in:
998 //
999 // struct foo { ... }
1000 // typedef int X;
1001 //
1002 // We'd really like to emit a missing semicolon error instead of emitting
1003 // an error on the 'int' saying that you can't have two type specifiers in
1004 // the same declaration of X. Because of this, we look ahead past this
1005 // token to see if it's a type specifier. If so, we know the code is
1006 // otherwise invalid, so we can produce the expected semi error.
1007 if (!isKnownToBeTypeSpecifier(NextToken()))
1008 return true;
1009 break;
1010 case tok::r_brace: // struct bar { struct foo {...} }
1011 // Missing ';' at end of struct is accepted as an extension in C mode.
1012 if (!getLangOpts().CPlusPlus)
1013 return true;
1014 break;
Richard Smithba65f502013-01-19 03:48:05 +00001015 // C++11 attributes
1016 case tok::l_square: // enum E [[]] x
1017 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1018 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith8338a9d2013-01-29 04:13:32 +00001019 case tok::greater:
1020 // template<class T = class X>
1021 return getLangOpts().CPlusPlus;
Richard Smithc9f35172012-06-25 21:37:02 +00001022 }
1023 return false;
1024}
1025
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001026/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1027/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1028/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +00001029/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001030///
1031/// class-specifier: [C++ class]
1032/// class-head '{' member-specification[opt] '}'
1033/// class-head '{' member-specification[opt] '}' attributes[opt]
1034/// class-head:
1035/// class-key identifier[opt] base-clause[opt]
1036/// class-key nested-name-specifier identifier base-clause[opt]
1037/// class-key nested-name-specifier[opt] simple-template-id
1038/// base-clause[opt]
1039/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001040/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001041/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001042/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001043/// simple-template-id base-clause[opt]
1044/// class-key:
1045/// 'class'
1046/// 'struct'
1047/// 'union'
1048///
1049/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +00001050/// class-key ::[opt] nested-name-specifier[opt] identifier
1051/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1052/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001053///
1054/// Note that the C++ class-specifier and elaborated-type-specifier,
1055/// together, subsume the C99 struct-or-union-specifier:
1056///
1057/// struct-or-union-specifier: [C99 6.7.2.1]
1058/// struct-or-union identifier[opt] '{' struct-contents '}'
1059/// struct-or-union identifier
1060/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1061/// '}' attributes[opt]
1062/// [GNU] struct-or-union attributes[opt] identifier
1063/// struct-or-union:
1064/// 'struct'
1065/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +00001066void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1067 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001068 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001069 AccessSpecifier AS,
Michael Han2e397132012-11-26 22:54:45 +00001070 bool EnteringContext, DeclSpecContext DSC,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001071 ParsedAttributesWithRange &Attributes) {
Joao Matos17d35c32012-08-31 22:18:20 +00001072 DeclSpec::TST TagType;
1073 if (TagTokKind == tok::kw_struct)
1074 TagType = DeclSpec::TST_struct;
1075 else if (TagTokKind == tok::kw___interface)
1076 TagType = DeclSpec::TST_interface;
1077 else if (TagTokKind == tok::kw_class)
1078 TagType = DeclSpec::TST_class;
1079 else {
Chris Lattner4c97d762009-04-12 21:49:30 +00001080 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1081 TagType = DeclSpec::TST_union;
1082 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001083
Douglas Gregor374929f2009-09-18 15:37:17 +00001084 if (Tok.is(tok::code_completion)) {
1085 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001086 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001087 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00001088 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001089
Chandler Carruth926c4b42010-06-28 08:39:25 +00001090 // C++03 [temp.explicit] 14.7.2/8:
1091 // The usual access checking rules do not apply to names used to specify
1092 // explicit instantiations.
1093 //
1094 // As an extension we do not perform access checking on the names used to
1095 // specify explicit specializations either. This is important to allow
1096 // specializing traits classes for private types.
John McCall13489672012-05-07 06:16:58 +00001097 //
1098 // Note that we don't suppress if this turns out to be an elaborated
1099 // type specifier.
1100 bool shouldDelayDiagsInTag =
1101 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1102 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1103 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001104
Sean Hunt2edf0a22012-06-23 05:07:58 +00001105 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001106 // If attributes exist after tag, parse them.
1107 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +00001108 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001109
Steve Narofff59e17e2008-12-24 20:59:21 +00001110 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001111 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001112 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001113
John McCallc052dbb2012-05-22 21:28:12 +00001114 // Parse inheritance specifiers.
1115 if (Tok.is(tok::kw___single_inheritance) ||
1116 Tok.is(tok::kw___multiple_inheritance) ||
1117 Tok.is(tok::kw___virtual_inheritance))
1118 ParseMicrosoftInheritanceClassAttributes(attrs);
1119
Sean Huntbbd37c62009-11-21 08:43:09 +00001120 // If C++0x attributes exist here, parse them.
1121 // FIXME: Are we consistent with the ordering of parsing of different
1122 // styles of attributes?
Richard Smith4e24f0f2013-01-02 12:01:23 +00001123 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Michael Han07fc1ba2013-01-07 16:57:11 +00001125 // Source location used by FIXIT to insert misplaced
1126 // C++11 attributes
1127 SourceLocation AttrFixitLoc = Tok.getLocation();
1128
John Wiegley20c0da72011-04-27 23:09:49 +00001129 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +00001130 !Tok.is(tok::identifier) &&
1131 Tok.getIdentifierInfo() &&
1132 (Tok.is(tok::kw___is_arithmetic) ||
1133 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001134 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001135 Tok.is(tok::kw___is_floating_point) ||
1136 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001137 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001138 Tok.is(tok::kw___is_integral) ||
1139 Tok.is(tok::kw___is_member_function_pointer) ||
1140 Tok.is(tok::kw___is_member_pointer) ||
1141 Tok.is(tok::kw___is_pod) ||
1142 Tok.is(tok::kw___is_pointer) ||
1143 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001144 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001145 Tok.is(tok::kw___is_signed) ||
1146 Tok.is(tok::kw___is_unsigned) ||
1147 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001148 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001149 // name of struct templates, but some are keywords in GCC >= 4.3
1150 // and Clang. Therefore, when we see the token sequence "struct
1151 // X", make X into a normal identifier rather than a keyword, to
1152 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001153 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001154 Tok.setKind(tok::identifier);
1155 }
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001157 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001158 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001159 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001160 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1161 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001162
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001163 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001164 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001165 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001166 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1167 Diag(Tok, diag::err_expected_ident);
1168 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001169
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001170 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1171
Douglas Gregorcc636682009-02-17 23:15:12 +00001172 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001173 IdentifierInfo *Name = 0;
1174 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001175 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001176 if (Tok.is(tok::identifier)) {
1177 Name = Tok.getIdentifierInfo();
1178 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001179
David Blaikie4e4d0842012-03-11 07:00:24 +00001180 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001181 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001182 // Eat the template argument list and try to continue parsing this as
1183 // a class (or template thereof).
1184 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001185 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001186 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001187 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001188 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001189 // We couldn't parse the template argument list at all, so don't
1190 // try to give any location information for the list.
1191 LAngleLoc = RAngleLoc = SourceLocation();
1192 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001193
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001194 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matos17d35c32012-08-31 22:18:20 +00001195 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1196 << (TagType == DeclSpec::TST_class? 0
1197 : TagType == DeclSpec::TST_struct? 1
1198 : TagType == DeclSpec::TST_interface? 2
1199 : 3)
1200 << Name
1201 << SourceRange(LAngleLoc, RAngleLoc);
1202
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001203 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001204 // we've removed its template argument list.
1205 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1206 if (TemplateParams && TemplateParams->size() > 1) {
1207 TemplateParams->pop_back();
1208 } else {
1209 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001210 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001211 = ParsedTemplateInfo::NonTemplate;
1212 }
1213 } else if (TemplateInfo.Kind
1214 == ParsedTemplateInfo::ExplicitInstantiation) {
1215 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001216 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001217 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001218 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001219 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001220 = SourceLocation();
1221 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1222 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001223 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001224 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001225 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001226 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001227 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001228
Douglas Gregor059101f2011-03-02 00:47:37 +00001229 if (TemplateId->Kind != TNK_Type_template &&
1230 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001231 // The template-name in the simple-template-id refers to
1232 // something other than a class template. Give an appropriate
1233 // error message and skip to the ';'.
1234 SourceRange Range(NameLoc);
1235 if (SS.isNotEmpty())
1236 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001237
Douglas Gregor39a8de12009-02-25 19:37:18 +00001238 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1239 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Douglas Gregor39a8de12009-02-25 19:37:18 +00001241 DS.SetTypeSpecError();
1242 SkipUntil(tok::semi, false, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001243 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001244 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001245 }
1246
Richard Smith7796eb52012-03-12 08:56:40 +00001247 // There are four options here.
1248 // - If we are in a trailing return type, this is always just a reference,
1249 // and we must not try to parse a definition. For instance,
1250 // [] () -> struct S { };
1251 // does not define a type.
1252 // - If we have 'struct foo {...', 'struct foo :...',
1253 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1254 // - If we have 'struct foo;', then this is either a forward declaration
1255 // or a friend declaration, which have to be treated differently.
1256 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han2e397132012-11-26 22:54:45 +00001257 //
1258 // We also detect these erroneous cases to provide better diagnostic for
1259 // C++11 attributes parsing.
1260 // - attributes follow class name:
1261 // struct foo [[]] {};
1262 // - attributes appear before or after 'final':
1263 // struct foo [[]] final [[]] {};
1264 //
Richard Smith69730c12012-03-12 07:56:15 +00001265 // However, in type-specifier-seq's, things look like declarations but are
1266 // just references, e.g.
1267 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001268 // or
Richard Smith69730c12012-03-12 07:56:15 +00001269 // &T::operator struct s;
1270 // For these, DSC is DSC_type_specifier.
Michael Han2e397132012-11-26 22:54:45 +00001271
1272 // If there are attributes after class name, parse them.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001273 MaybeParseCXX11Attributes(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00001274
John McCallf312b1e2010-08-26 23:41:50 +00001275 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001276 if (DSC == DSC_trailing)
1277 TUK = Sema::TUK_Reference;
1278 else if (Tok.is(tok::l_brace) ||
1279 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001280 (isCXX11FinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001281 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001282 if (DS.isFriendSpecified()) {
1283 // C++ [class.friend]p2:
1284 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001285 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001286 << SourceRange(DS.getFriendSpecLoc());
1287
1288 // Skip everything up to the semicolon, so that this looks like a proper
1289 // friend class (or template thereof) declaration.
1290 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001291 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001292 } else {
1293 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001294 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001295 }
Richard Smith4e24f0f2013-01-02 12:01:23 +00001296 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
Michael Han2e397132012-11-26 22:54:45 +00001297 NextToken().is(tok::kw_alignas) ||
1298 NextToken().is(tok::kw__Alignas))) {
1299 // We can't tell if this is a definition or reference
1300 // until we skipped the 'final' and C++11 attribute specifiers.
1301 TentativeParsingAction PA(*this);
1302
1303 // Skip the 'final' keyword.
1304 ConsumeToken();
1305
1306 // Skip C++11 attribute specifiers.
1307 while (true) {
1308 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1309 ConsumeBracket();
1310 if (!SkipUntil(tok::r_square))
1311 break;
1312 } else if ((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1313 NextToken().is(tok::l_paren)) {
1314 ConsumeToken();
1315 ConsumeParen();
1316 if (!SkipUntil(tok::r_paren))
1317 break;
1318 } else {
1319 break;
1320 }
1321 }
1322
1323 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1324 TUK = Sema::TUK_Definition;
1325 else
1326 TUK = Sema::TUK_Reference;
1327
1328 PA.Revert();
Richard Smithc9f35172012-06-25 21:37:02 +00001329 } else if (DSC != DSC_type_specifier &&
1330 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00001331 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallf312b1e2010-08-26 23:41:50 +00001332 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matos17d35c32012-08-31 22:18:20 +00001333 if (Tok.isNot(tok::semi)) {
1334 // A semicolon was missing after this declaration. Diagnose and recover.
1335 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1336 DeclSpec::getSpecifierName(TagType));
1337 PP.EnterToken(Tok);
1338 Tok.setKind(tok::semi);
1339 }
Richard Smithc9f35172012-06-25 21:37:02 +00001340 } else
John McCallf312b1e2010-08-26 23:41:50 +00001341 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001342
Michael Han2e397132012-11-26 22:54:45 +00001343 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1344 // to caller to handle.
Michael Han07fc1ba2013-01-07 16:57:11 +00001345 if (TUK != Sema::TUK_Reference) {
1346 // If this is not a reference, then the only possible
1347 // valid place for C++11 attributes to appear here
1348 // is between class-key and class-name. If there are
1349 // any attributes after class-name, we try a fixit to move
1350 // them to the right place.
1351 SourceRange AttrRange = Attributes.Range;
1352 if (AttrRange.isValid()) {
1353 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1354 << AttrRange
1355 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1356 CharSourceRange(AttrRange, true))
1357 << FixItHint::CreateRemoval(AttrRange);
1358
1359 // Recover by adding misplaced attributes to the attribute list
1360 // of the class so they can be applied on the class later.
1361 attrs.takeAllFrom(Attributes);
1362 }
1363 }
Michael Han2e397132012-11-26 22:54:45 +00001364
John McCall13489672012-05-07 06:16:58 +00001365 // If this is an elaborated type specifier, and we delayed
1366 // diagnostics before, just merge them into the current pool.
1367 if (shouldDelayDiagsInTag) {
1368 diagsFromTag.done();
1369 if (TUK == Sema::TUK_Reference)
1370 diagsFromTag.redelay();
1371 }
1372
John McCall207014e2010-07-30 06:26:29 +00001373 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001374 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001375 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1376 // We have a declaration or reference to an anonymous class.
1377 Diag(StartLoc, diag::err_anon_type_definition)
1378 << DeclSpec::getSpecifierName(TagType);
1379 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001380
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001381 SkipUntil(tok::comma, true);
1382 return;
1383 }
1384
Douglas Gregorddc29e12009-02-06 22:42:48 +00001385 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001386 DeclResult TagOrTempResult = true; // invalid
1387 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001388
Douglas Gregor402abb52009-05-28 23:31:59 +00001389 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001390 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001391 // Explicit specialization, class template partial specialization,
1392 // or explicit instantiation.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001393 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001394 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001395 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001396 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001397 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001398 ProhibitAttributes(attrs);
1399
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001400 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001401 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001402 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001403 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001404 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001405 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001406 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001407 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001408 TemplateId->TemplateNameLoc,
1409 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001410 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001411 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001412 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001413
1414 // Friend template-ids are treated as references unless
1415 // they have template headers, in which case they're ill-formed
1416 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1417 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001418 } else if (TUK == Sema::TUK_Reference ||
1419 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001420 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001421 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001422 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001423 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001424 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001425 TemplateId->Template,
1426 TemplateId->TemplateNameLoc,
1427 TemplateId->LAngleLoc,
1428 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001429 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001430 } else {
1431 // This is an explicit specialization or a class template
1432 // partial specialization.
1433 TemplateParameterLists FakedParamLists;
1434
1435 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1436 // This looks like an explicit instantiation, because we have
1437 // something like
1438 //
1439 // template class Foo<X>
1440 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001441 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001442 // meant to be an explicit specialization, but the user forgot
1443 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +00001444 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001445
Mike Stump1eb44332009-09-09 15:08:12 +00001446 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001447 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001448 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001449 diag::err_explicit_instantiation_with_definition)
1450 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +00001451 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001452
1453 // Create a fake template parameter list that contains only
1454 // "template<>", so that we treat this construct as a class
1455 // template specialization.
1456 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +00001457 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001458 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001459 LAngleLoc,
1460 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001461 LAngleLoc));
1462 TemplateParams = &FakedParamLists;
1463 }
1464
1465 // Build the class template specialization.
1466 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001467 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001468 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001469 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001470 TemplateId->TemplateNameLoc,
1471 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001472 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001473 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001474 attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001475 MultiTemplateParamsArg(
Douglas Gregorcc636682009-02-17 23:15:12 +00001476 TemplateParams? &(*TemplateParams)[0] : 0,
1477 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001478 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001479 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001480 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001481 // Explicit instantiation of a member of a class template
1482 // specialization, e.g.,
1483 //
1484 // template struct Outer<int>::Inner;
1485 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001486 ProhibitAttributes(attrs);
1487
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001488 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001489 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001490 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001491 TemplateInfo.TemplateLoc,
1492 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001493 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001494 } else if (TUK == Sema::TUK_Friend &&
1495 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001496 ProhibitAttributes(attrs);
1497
John McCall9a34edb2010-10-19 01:40:49 +00001498 TagOrTempResult =
1499 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1500 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001501 Name, NameLoc, attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001502 MultiTemplateParamsArg(
John McCall9a34edb2010-10-19 01:40:49 +00001503 TemplateParams? &(*TemplateParams)[0] : 0,
1504 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001505 } else {
1506 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001507 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001508 // FIXME: Diagnose this particular error.
1509 }
1510
Sean Hunt2edf0a22012-06-23 05:07:58 +00001511 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1512 ProhibitAttributes(attrs);
1513
John McCallc4e70192009-09-11 04:59:25 +00001514 bool IsDependent = false;
1515
John McCalla25c4082010-10-19 18:40:57 +00001516 // Don't pass down template parameter lists if this is just a tag
1517 // reference. For example, we don't need the template parameters here:
1518 // template <class T> class A *makeA(T t);
1519 MultiTemplateParamsArg TParams;
1520 if (TUK != Sema::TUK_Reference && TemplateParams)
1521 TParams =
1522 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1523
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001524 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001525 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001526 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001527 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001528 TParams, Owned, IsDependent,
1529 SourceLocation(), false,
1530 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001531
1532 // If ActOnTag said the type was dependent, try again with the
1533 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001534 if (IsDependent) {
1535 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001536 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001537 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001538 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001539 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001540
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001541 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001542 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001543 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001544 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001545 isCXX11FinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001546 if (getLangOpts().CPlusPlus)
Michael Han07fc1ba2013-01-07 16:57:11 +00001547 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1548 TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001549 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001550 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001551 }
1552
John McCallb3d87482010-08-24 05:47:05 +00001553 const char *PrevSpec = 0;
1554 unsigned DiagID;
1555 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001556 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001557 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1558 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001559 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001560 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001561 Result = DS.SetTypeSpecType(TagType, StartLoc,
1562 NameLoc.isValid() ? NameLoc : StartLoc,
1563 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001564 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001565 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001566 return;
1567 }
Mike Stump1eb44332009-09-09 15:08:12 +00001568
John McCallb3d87482010-08-24 05:47:05 +00001569 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001570 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001571
Chris Lattner4ed5d912010-02-02 01:23:29 +00001572 // At this point, we've successfully parsed a class-specifier in 'definition'
1573 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1574 // going to look at what comes after it to improve error recovery. If an
1575 // impossible token occurs next, we assume that the programmer forgot a ; at
1576 // the end of the declaration and recover that way.
1577 //
Richard Smithc9f35172012-06-25 21:37:02 +00001578 // Also enforce C++ [temp]p3:
1579 // In a template-declaration which defines a class, no declarator
1580 // is permitted.
Joao Matos17d35c32012-08-31 22:18:20 +00001581 if (TUK == Sema::TUK_Definition &&
1582 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidis7d033b22012-12-17 20:10:43 +00001583 if (Tok.isNot(tok::semi)) {
1584 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1585 DeclSpec::getSpecifierName(TagType));
1586 // Push this token back into the preprocessor and change our current token
1587 // to ';' so that the rest of the code recovers as though there were an
1588 // ';' after the definition.
1589 PP.EnterToken(Tok);
1590 Tok.setKind(tok::semi);
1591 }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001592 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001593}
1594
Mike Stump1eb44332009-09-09 15:08:12 +00001595/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001596///
1597/// base-clause : [C++ class.derived]
1598/// ':' base-specifier-list
1599/// base-specifier-list:
1600/// base-specifier '...'[opt]
1601/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001602void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001603 assert(Tok.is(tok::colon) && "Not a base clause");
1604 ConsumeToken();
1605
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001606 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001607 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001608
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001609 while (true) {
1610 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001611 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001612 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001613 // Skip the rest of this base specifier, up until the comma or
1614 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001615 SkipUntil(tok::comma, tok::l_brace, true, true);
1616 } else {
1617 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001618 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001619 }
1620
1621 // If the next token is a comma, consume it and keep reading
1622 // base-specifiers.
1623 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001625 // Consume the comma.
1626 ConsumeToken();
1627 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001628
1629 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001630 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001631}
1632
1633/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1634/// one entry in the base class list of a class specifier, for example:
1635/// class foo : public bar, virtual private baz {
1636/// 'public bar' and 'virtual private baz' are each base-specifiers.
1637///
1638/// base-specifier: [C++ class.derived]
Richard Smith05321402013-02-19 23:47:15 +00001639/// attribute-specifier-seq[opt] base-type-specifier
1640/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1641/// base-type-specifier
1642/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1643/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001644Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001645 bool IsVirtual = false;
1646 SourceLocation StartLoc = Tok.getLocation();
1647
Richard Smith05321402013-02-19 23:47:15 +00001648 ParsedAttributesWithRange Attributes(AttrFactory);
1649 MaybeParseCXX11Attributes(Attributes);
1650
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001651 // Parse the 'virtual' keyword.
1652 if (Tok.is(tok::kw_virtual)) {
1653 ConsumeToken();
1654 IsVirtual = true;
1655 }
1656
Richard Smith05321402013-02-19 23:47:15 +00001657 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1658
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001659 // Parse an (optional) access specifier.
1660 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001661 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001662 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Richard Smith05321402013-02-19 23:47:15 +00001664 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1665
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001666 // Parse the 'virtual' keyword (again!), in case it came after the
1667 // access specifier.
1668 if (Tok.is(tok::kw_virtual)) {
1669 SourceLocation VirtualLoc = ConsumeToken();
1670 if (IsVirtual) {
1671 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001672 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001673 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001674 }
1675
1676 IsVirtual = true;
1677 }
1678
Richard Smith05321402013-02-19 23:47:15 +00001679 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1680
Douglas Gregor42a552f2008-11-05 20:51:48 +00001681 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001682 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001683 SourceLocation BaseLoc;
1684 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001685 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001686 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001688 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1689 // actually part of the base-specifier-list grammar productions, but we
1690 // parse it here for convenience.
1691 SourceLocation EllipsisLoc;
1692 if (Tok.is(tok::ellipsis))
1693 EllipsisLoc = ConsumeToken();
1694
Mike Stump1eb44332009-09-09 15:08:12 +00001695 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001696 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001697
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001698 // Notify semantic analysis that we have parsed a complete
1699 // base-specifier.
Richard Smith05321402013-02-19 23:47:15 +00001700 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1701 Access, BaseType.get(), BaseLoc,
1702 EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001703}
1704
1705/// getAccessSpecifierIfPresent - Determine whether the next token is
1706/// a C++ access-specifier.
1707///
1708/// access-specifier: [C++ class.derived]
1709/// 'private'
1710/// 'protected'
1711/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001712AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001713 switch (Tok.getKind()) {
1714 default: return AS_none;
1715 case tok::kw_private: return AS_private;
1716 case tok::kw_protected: return AS_protected;
1717 case tok::kw_public: return AS_public;
1718 }
1719}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001720
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001721/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001722/// delayed, e.g., default arguments, create a late-parsed method declaration
1723/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001724void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1725 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001726 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001727 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001728 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001729 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001730 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001731
Eli Friedmand33133c2009-07-22 21:45:50 +00001732 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1733 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1734 if (!LateMethod) {
1735 // Push this method onto the stack of late-parsed method
1736 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001737 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1738 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001739 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001740
1741 // Add all of the parameters prior to this one (they don't
1742 // have default arguments).
1743 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1744 for (unsigned I = 0; I < ParamIdx; ++I)
1745 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001746 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001747 }
1748
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001749 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001750 // not have a default argument).
1751 LateMethod->DefaultArgs.push_back(
1752 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1753 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1754 }
1755 }
1756}
1757
Richard Smith4e24f0f2013-01-02 12:01:23 +00001758/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001759/// virt-specifier.
1760///
1761/// virt-specifier:
1762/// override
1763/// final
Richard Smith4e24f0f2013-01-02 12:01:23 +00001764VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001765 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001766 return VirtSpecifiers::VS_None;
1767
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001768 if (Tok.is(tok::identifier)) {
1769 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001770
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001771 // Initialize the contextual keywords.
1772 if (!Ident_final) {
1773 Ident_final = &PP.getIdentifierTable().get("final");
1774 Ident_override = &PP.getIdentifierTable().get("override");
1775 }
1776
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001777 if (II == Ident_override)
1778 return VirtSpecifiers::VS_Override;
1779
1780 if (II == Ident_final)
1781 return VirtSpecifiers::VS_Final;
1782 }
1783
1784 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001785}
1786
Richard Smith4e24f0f2013-01-02 12:01:23 +00001787/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001788///
1789/// virt-specifier-seq:
1790/// virt-specifier
1791/// virt-specifier-seq virt-specifier
Richard Smith4e24f0f2013-01-02 12:01:23 +00001792void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalle402e722012-09-25 07:32:39 +00001793 bool IsInterface) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001794 while (true) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001795 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001796 if (Specifier == VirtSpecifiers::VS_None)
1797 return;
1798
1799 // C++ [class.mem]p8:
1800 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001801 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001802 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001803 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1804 << PrevSpec
1805 << FixItHint::CreateRemoval(Tok.getLocation());
1806
John McCalle402e722012-09-25 07:32:39 +00001807 if (IsInterface && Specifier == VirtSpecifiers::VS_Final) {
1808 Diag(Tok.getLocation(), diag::err_override_control_interface)
1809 << VirtSpecifiers::getSpecifierName(Specifier);
1810 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00001811 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00001812 diag::warn_cxx98_compat_override_control_keyword :
1813 diag::ext_override_control_keyword)
1814 << VirtSpecifiers::getSpecifierName(Specifier);
1815 }
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001816 ConsumeToken();
1817 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001818}
1819
Richard Smith4e24f0f2013-01-02 12:01:23 +00001820/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001821/// contextual 'final' keyword.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001822bool Parser::isCXX11FinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001823 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001824 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001825
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001826 if (!Tok.is(tok::identifier))
1827 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001828
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001829 // Initialize the contextual keywords.
1830 if (!Ident_final) {
1831 Ident_final = &PP.getIdentifierTable().get("final");
1832 Ident_override = &PP.getIdentifierTable().get("override");
1833 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001834
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001835 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001836}
1837
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001838/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1839///
1840/// member-declaration:
1841/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1842/// function-definition ';'[opt]
1843/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1844/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001845/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001846/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001847/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001848///
1849/// member-declarator-list:
1850/// member-declarator
1851/// member-declarator-list ',' member-declarator
1852///
1853/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001854/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001855/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001856/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001857/// identifier[opt] ':' constant-expression
1858///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001859/// virt-specifier-seq:
1860/// virt-specifier
1861/// virt-specifier-seq virt-specifier
1862///
1863/// virt-specifier:
1864/// override
1865/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001866///
Sebastian Redle2b68332009-04-12 17:16:29 +00001867/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001868/// '= 0'
1869///
1870/// constant-initializer:
1871/// '=' constant-expression
1872///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001873void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001874 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001875 const ParsedTemplateInfo &TemplateInfo,
1876 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001877 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001878 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001879 Diag(Tok, diag::err_at_defs_cxx);
1880 else
1881 Diag(Tok, diag::err_at_in_class);
1882
1883 ConsumeToken();
1884 SkipUntil(tok::r_brace);
1885 return;
1886 }
1887
John McCall60fa3cf2009-12-11 02:10:03 +00001888 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001889 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001890 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001891 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1892 if (TryAnnotateCXXScopeToken())
1893 MalformedTypeSpec = true;
1894
1895 bool isAccessDecl;
1896 if (Tok.isNot(tok::annot_cxxscope))
1897 isAccessDecl = false;
1898 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001899 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1900 else
1901 isAccessDecl = NextToken().is(tok::kw_operator);
1902
1903 if (isAccessDecl) {
1904 // Collect the scope specifier token we annotated earlier.
1905 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001906 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1907 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001908
1909 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001910 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001911 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001912 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1913 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001914 SkipUntil(tok::semi);
1915 return;
1916 }
1917
1918 // TODO: recover from mistakenly-qualified operator declarations.
1919 if (ExpectAndConsume(tok::semi,
1920 diag::err_expected_semi_after,
1921 "access declaration",
1922 tok::semi))
1923 return;
1924
Douglas Gregor23c94db2010-07-02 17:43:08 +00001925 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001926 false, SourceLocation(),
1927 SS, Name,
1928 /* AttrList */ 0,
1929 /* IsTypeName */ false,
1930 SourceLocation());
1931 return;
1932 }
1933 }
1934
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001935 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001936 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001937 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001938 SourceLocation DeclEnd;
1939 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001940 return;
1941 }
Mike Stump1eb44332009-09-09 15:08:12 +00001942
Chris Lattner682bf922009-03-29 16:50:03 +00001943 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001944 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001945 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001946 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001947 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001948 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001949 return;
1950 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001951
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001952 // Handle: member-declaration ::= '__extension__' member-declaration
1953 if (Tok.is(tok::kw___extension__)) {
1954 // __extension__ silences extension warnings in the subexpression.
1955 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1956 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001957 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1958 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001959 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001960
Chris Lattner4ed5d912010-02-02 01:23:29 +00001961 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1962 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001963 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001964
John McCall0b7e6782011-03-24 11:26:52 +00001965 ParsedAttributesWithRange attrs(AttrFactory);
Michael Han52b501c2012-11-28 23:17:40 +00001966 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001967 // Optional C++11 attribute-specifier
1968 MaybeParseCXX11Attributes(attrs);
Michael Han52b501c2012-11-28 23:17:40 +00001969 // We need to keep these attributes for future diagnostic
1970 // before they are taken over by declaration specifier.
1971 FnAttrs.addAll(attrs.getList());
1972 FnAttrs.Range = attrs.Range;
1973
John McCall7f040a92010-12-24 02:08:15 +00001974 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001975
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001976 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00001977 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001978
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001979 // Eat 'using'.
1980 SourceLocation UsingLoc = ConsumeToken();
1981
1982 if (Tok.is(tok::kw_namespace)) {
1983 Diag(UsingLoc, diag::err_using_namespace_in_class);
1984 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001985 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001986 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001987 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00001988 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1989 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001990 }
1991 return;
1992 }
1993
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001994 // Hold late-parsed attributes so we can attach a Decl to them later.
1995 LateParsedAttrList CommonLateParsedAttrs;
1996
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001997 // decl-specifier-seq:
1998 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001999 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00002000 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00002001 if (MalformedTypeSpec)
2002 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002003 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2004 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002005
Benjamin Kramer5354e772012-08-23 23:38:35 +00002006 MultiTemplateParamsArg TemplateParams(
John McCalldd4a3b02009-09-16 22:47:08 +00002007 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2008 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2009
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002010 if (Tok.is(tok::semi)) {
2011 ConsumeToken();
Michael Han52b501c2012-11-28 23:17:40 +00002012
2013 if (DS.isFriendSpecified())
2014 ProhibitAttributes(FnAttrs);
2015
John McCalld226f652010-08-21 09:40:31 +00002016 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00002017 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00002018 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00002019 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002020 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002021
John McCall54abf7d2009-11-04 02:18:39 +00002022 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00002023 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002024
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002025 // Hold late-parsed attributes so we can attach a Decl to them later.
2026 LateParsedAttrList LateParsedAttrs;
2027
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002028 SourceLocation EqualLoc;
2029 bool HasInitializer = false;
2030 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002031 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002032 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2033 ColonProtectionRAIIObject X(*this);
2034
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002035 // Parse the first declarator.
2036 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00002037 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00002038 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002039 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00002040 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002041 if (Tok.is(tok::semi))
2042 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002043 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002044 }
2045
Richard Smith4e24f0f2013-01-02 12:01:23 +00002046 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber48673472011-01-28 06:07:34 +00002047
John Thompson1b2fc0f2009-11-25 22:58:06 +00002048 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002049 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00002050
Francois Pichet6a247472011-05-11 02:14:46 +00002051 // MSVC permits pure specifier on inline functions declared at class scope.
2052 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00002053 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00002054 DeclaratorInfo.isFunctionDeclarator() &&
2055 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002056 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00002057 Init = ParseInitializer();
2058 if (Init.isInvalid())
2059 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002060 else
2061 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00002062 }
2063
Douglas Gregor45fa5602011-11-07 20:56:01 +00002064 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002065 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00002066 //
2067 // In C++11, a non-function declarator followed by an open brace is a
2068 // braced-init-list for an in-class member initialization, not an
2069 // erroneous function definition.
Richard Smith80ad52f2013-01-02 11:42:31 +00002070 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002071 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002072 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00002073 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002074 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002075 } else if (Tok.is(tok::equal)) {
2076 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00002077 if (KW.is(tok::kw_default))
2078 DefinitionKind = FDK_Defaulted;
2079 else if (KW.is(tok::kw_delete))
2080 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00002081 }
2082 }
2083
Michael Han52b501c2012-11-28 23:17:40 +00002084 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2085 // to a friend declaration, that declaration shall be a definition.
2086 if (DeclaratorInfo.isFunctionDeclarator() &&
2087 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2088 // Diagnose attributes that appear before decl specifier:
2089 // [[]] friend int foo();
2090 ProhibitAttributes(FnAttrs);
2091 }
2092
Douglas Gregor45fa5602011-11-07 20:56:01 +00002093 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002094 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002095 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002096 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00002097 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Michael Han52b501c2012-11-28 23:17:40 +00002098
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002099 // Consume the optional ';'
2100 if (Tok.is(tok::semi))
2101 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002102 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002103 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002104
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002105 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002106 Diag(DeclaratorInfo.getIdentifierLoc(),
2107 diag::err_function_declared_typedef);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002108
Richard Smith6f9a4452012-11-15 22:54:20 +00002109 // Recover by treating the 'typedef' as spurious.
2110 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002111 }
2112
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002113 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002114 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00002115 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002116
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002117 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2118 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2119 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002120 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002121 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002122 }
2123 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00002124
2125 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002126 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00002127 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002128
Chris Lattner682bf922009-03-29 16:50:03 +00002129 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002130 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002131 }
2132
2133 // member-declarator-list:
2134 // member-declarator
2135 // member-declarator-list ',' member-declarator
2136
Chris Lattner5f9e2722011-07-23 10:55:15 +00002137 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00002138 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00002139 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002140
2141 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002142 // member-declarator:
2143 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002144 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002145 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002146 if (Tok.is(tok::colon)) {
2147 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002148 BitfieldSize = ParseConstantExpression();
2149 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002150 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002151 }
Mike Stump1eb44332009-09-09 15:08:12 +00002152
Chris Lattnere6563252010-06-13 05:34:18 +00002153 // If a simple-asm-expr is present, parse it.
2154 if (Tok.is(tok::kw_asm)) {
2155 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002156 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002157 if (AsmLabel.isInvalid())
2158 SkipUntil(tok::comma, true, true);
2159
2160 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2161 DeclaratorInfo.SetRangeEnd(Loc);
2162 }
2163
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002164 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002165 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002166
Richard Smith7a614d82011-06-11 17:19:42 +00002167 // FIXME: When g++ adds support for this, we'll need to check whether it
2168 // goes before or after the GNU attributes and __asm__.
Richard Smith4e24f0f2013-01-02 12:01:23 +00002169 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith7a614d82011-06-11 17:19:42 +00002170
Richard Smithca523302012-06-10 03:12:00 +00002171 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002172 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002173 if (BitfieldSize.get()) {
2174 Diag(Tok, diag::err_bitfield_member_init);
2175 SkipUntil(tok::comma, true, true);
2176 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002177 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002178 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2179 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smithca523302012-06-10 03:12:00 +00002180 != DeclSpec::SCS_typedef)
2181 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002182 }
2183 }
2184
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002185 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002186 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002187 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002188
Rafael Espindolafc35cbc2013-01-08 20:44:06 +00002189 NamedDecl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002190 if (DS.isFriendSpecified()) {
Michael Han52b501c2012-11-28 23:17:40 +00002191 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2192 // to a friend declaration, that declaration shall be a definition.
2193 //
2194 // Diagnose attributes appear after friend member function declarator:
2195 // foo [[]] ();
2196 SmallVector<SourceRange, 4> Ranges;
2197 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2198 if (!Ranges.empty()) {
2199 for (SmallVector<SourceRange, 4>::iterator I = Ranges.begin(),
2200 E = Ranges.end(); I != E; ++I) {
2201 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2202 << *I;
2203 }
2204 }
2205
John McCallbbbcdd92009-09-11 21:02:39 +00002206 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002207 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002208 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002209 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002210 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002211 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002212 TemplateParams,
John McCall67d1a672009-08-06 02:15:43 +00002213 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002214 VS, HasInClassInit);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002215 if (AccessAttrs)
2216 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2217 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002218 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002219
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002220 // Set the Decl for any late parsed attributes
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002221 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2222 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2223 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002224 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002225 LateParsedAttrs[i]->addDecl(ThisDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002226 }
2227 LateParsedAttrs.clear();
2228
Douglas Gregor147545d2011-10-10 14:49:18 +00002229 // Handle the initializer.
David Blaikie1d87fba2013-01-30 01:22:18 +00002230 if (HasInClassInit != ICIS_NoInit &&
2231 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2232 DeclSpec::SCS_static) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002233 // The initializer was deferred; parse it and cache the tokens.
Richard Smith80ad52f2013-01-02 11:42:31 +00002234 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00002235 diag::warn_cxx98_compat_nonstatic_member_init :
2236 diag::ext_nonstatic_member_init);
2237
Richard Smith7a614d82011-06-11 17:19:42 +00002238 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002239 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2240 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002241 //
2242 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002243 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002244 Diag(Tok, diag::err_incomplete_array_member_init);
2245 SkipUntil(tok::comma, true, true);
David Blaikie3164c142012-02-14 09:00:46 +00002246 if (ThisDecl)
2247 // Avoid later warnings about a class member of incomplete type.
2248 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002249 } else
2250 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002251 } else if (HasInitializer) {
2252 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002253 if (!Init.isUsable())
Douglas Gregor552e2992012-02-21 02:22:07 +00002254 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002255 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2256
Douglas Gregor147545d2011-10-10 14:49:18 +00002257 if (Init.isInvalid())
2258 SkipUntil(tok::comma, true, true);
2259 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002260 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002261 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor147545d2011-10-10 14:49:18 +00002262 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2263 // No initializer.
2264 Actions.ActOnUninitializedDecl(ThisDecl,
2265 DS.getTypeSpecType() == DeclSpec::TST_auto);
Richard Smith7a614d82011-06-11 17:19:42 +00002266 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002267
2268 if (ThisDecl) {
2269 Actions.FinalizeDeclaration(ThisDecl);
2270 DeclsInGroup.push_back(ThisDecl);
2271 }
2272
Richard Smithe5310012012-04-29 07:31:09 +00002273 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor147545d2011-10-10 14:49:18 +00002274 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2275 != DeclSpec::SCS_typedef) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002276 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002277 }
2278
2279 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002280
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002281 // If we don't have a comma, it is either the end of the list (a ';')
2282 // or an error, bail out.
2283 if (Tok.isNot(tok::comma))
2284 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002285
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002286 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002287 SourceLocation CommaLoc = ConsumeToken();
2288
2289 if (Tok.isAtStartOfLine() &&
2290 !MightBeDeclarator(Declarator::MemberContext)) {
2291 // This comma was followed by a line-break and something which can't be
2292 // the start of a declarator. The comma was probably a typo for a
2293 // semicolon.
2294 Diag(CommaLoc, diag::err_expected_semi_declaration)
2295 << FixItHint::CreateReplacement(CommaLoc, ";");
2296 ExpectSemi = false;
2297 break;
2298 }
Mike Stump1eb44332009-09-09 15:08:12 +00002299
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002300 // Parse the next declarator.
2301 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002302 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002303 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002304 Init = true;
2305 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002306 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002307
Bill Wendlingad017fa2012-12-20 19:22:21 +00002308 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002309 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002310
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002311 if (Tok.isNot(tok::colon))
2312 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002313 }
2314
Richard Smith1c94c162012-01-09 22:31:44 +00002315 if (ExpectSemi &&
2316 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002317 // Skip to end of block or statement.
2318 SkipUntil(tok::r_brace, true, true);
2319 // If we stopped at a ';', eat it.
2320 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002321 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002322 }
2323
Douglas Gregor23c94db2010-07-02 17:43:08 +00002324 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00002325 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002326}
2327
Richard Smith7a614d82011-06-11 17:19:42 +00002328/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2329/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2330/// function definition. The location of the '=', if any, will be placed in
2331/// EqualLoc.
2332///
2333/// pure-specifier:
2334/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002335///
Richard Smith7a614d82011-06-11 17:19:42 +00002336/// brace-or-equal-initializer:
2337/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002338/// braced-init-list
2339///
Richard Smith7a614d82011-06-11 17:19:42 +00002340/// initializer-clause:
2341/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002342/// braced-init-list
2343///
Richard Smith7a614d82011-06-11 17:19:42 +00002344/// defaulted/deleted function-definition:
2345/// '=' 'default'
2346/// '=' 'delete'
2347///
2348/// Prior to C++0x, the assignment-expression in an initializer-clause must
2349/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002350ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002351 SourceLocation &EqualLoc) {
2352 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2353 && "Data member initializer not starting with '=' or '{'");
2354
Douglas Gregor552e2992012-02-21 02:22:07 +00002355 EnterExpressionEvaluationContext Context(Actions,
2356 Sema::PotentiallyEvaluated,
2357 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002358 if (Tok.is(tok::equal)) {
2359 EqualLoc = ConsumeToken();
2360 if (Tok.is(tok::kw_delete)) {
2361 // In principle, an initializer of '= delete p;' is legal, but it will
2362 // never type-check. It's better to diagnose it as an ill-formed expression
2363 // than as an ill-formed deleted non-function member.
2364 // An initializer of '= delete p, foo' will never be parsed, because
2365 // a top-level comma always ends the initializer expression.
2366 const Token &Next = NextToken();
2367 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2368 Next.is(tok::eof)) {
2369 if (IsFunction)
2370 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2371 << 1 /* delete */;
2372 else
2373 Diag(ConsumeToken(), diag::err_deleted_non_function);
2374 return ExprResult();
2375 }
2376 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002377 if (IsFunction)
2378 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2379 << 0 /* default */;
2380 else
2381 Diag(ConsumeToken(), diag::err_default_special_members);
2382 return ExprResult();
2383 }
2384
Sebastian Redl33deb352012-02-22 10:50:08 +00002385 }
2386 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002387}
2388
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002389/// ParseCXXMemberSpecification - Parse the class definition.
2390///
2391/// member-specification:
2392/// member-declaration member-specification[opt]
2393/// access-specifier ':' member-specification[opt]
2394///
Joao Matos17d35c32012-08-31 22:18:20 +00002395void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han07fc1ba2013-01-07 16:57:11 +00002396 SourceLocation AttrFixitLoc,
Richard Smith05321402013-02-19 23:47:15 +00002397 ParsedAttributesWithRange &Attrs,
Joao Matos17d35c32012-08-31 22:18:20 +00002398 unsigned TagType, Decl *TagDecl) {
2399 assert((TagType == DeclSpec::TST_struct ||
2400 TagType == DeclSpec::TST_interface ||
2401 TagType == DeclSpec::TST_union ||
2402 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2403
John McCallf312b1e2010-08-26 23:41:50 +00002404 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2405 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002406
Douglas Gregor26997fd2010-01-16 20:52:59 +00002407 // Determine whether this is a non-nested class. Note that local
2408 // classes are *not* considered to be nested classes.
2409 bool NonNestedClass = true;
2410 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002411 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002412 if (S->isClassScope()) {
2413 // We're inside a class scope, so this is a nested class.
2414 NonNestedClass = false;
John McCalle402e722012-09-25 07:32:39 +00002415
2416 // The Microsoft extension __interface does not permit nested classes.
2417 if (getCurrentClass().IsInterface) {
2418 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2419 << /*ErrorType=*/6
2420 << (isa<NamedDecl>(TagDecl)
2421 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2422 : "<anonymous>");
2423 }
Douglas Gregor26997fd2010-01-16 20:52:59 +00002424 break;
2425 }
2426
2427 if ((S->getFlags() & Scope::FnScope)) {
2428 // If we're in a function or function template declared in the
2429 // body of a class, then this is a local class rather than a
2430 // nested class.
2431 const Scope *Parent = S->getParent();
2432 if (Parent->isTemplateParamScope())
2433 Parent = Parent->getParent();
2434 if (Parent->isClassScope())
2435 break;
2436 }
2437 }
2438 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002439
2440 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002441 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002442
Douglas Gregor6569d682009-05-27 23:11:45 +00002443 // Note that we are parsing a new (potentially-nested) class definition.
John McCalle402e722012-09-25 07:32:39 +00002444 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2445 TagType == DeclSpec::TST_interface);
Douglas Gregor6569d682009-05-27 23:11:45 +00002446
Douglas Gregorddc29e12009-02-06 22:42:48 +00002447 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002448 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002449
Anders Carlssonb184a182011-03-25 14:46:08 +00002450 SourceLocation FinalLoc;
2451
2452 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002453 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00002454 assert(isCXX11FinalKeyword() && "not a class definition");
Richard Smith8b11b5e2011-10-15 04:21:46 +00002455 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002456
John McCalle402e722012-09-25 07:32:39 +00002457 if (TagType == DeclSpec::TST_interface) {
2458 Diag(FinalLoc, diag::err_override_control_interface)
2459 << "final";
2460 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00002461 Diag(FinalLoc, getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00002462 diag::warn_cxx98_compat_override_control_keyword :
2463 diag::ext_override_control_keyword) << "final";
2464 }
Michael Han2e397132012-11-26 22:54:45 +00002465
Michael Han07fc1ba2013-01-07 16:57:11 +00002466 // Parse any C++11 attributes after 'final' keyword.
2467 // These attributes are not allowed to appear here,
2468 // and the only possible place for them to appertain
2469 // to the class would be between class-key and class-name.
Richard Smith05321402013-02-19 23:47:15 +00002470 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonb184a182011-03-25 14:46:08 +00002471 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002472
John McCallbd0dfa52009-12-19 21:48:58 +00002473 if (Tok.is(tok::colon)) {
2474 ParseBaseClause(TagDecl);
2475
2476 if (!Tok.is(tok::l_brace)) {
2477 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002478
2479 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002480 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002481 return;
2482 }
2483 }
2484
2485 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002486 BalancedDelimiterTracker T(*this, tok::l_brace);
2487 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002488
John McCall42a4f662010-05-28 08:11:17 +00002489 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002490 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002491 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002492
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002493 // C++ 11p3: Members of a class defined with the keyword class are private
2494 // by default. Members of a class defined with the keywords struct or union
2495 // are public by default.
2496 AccessSpecifier CurAS;
2497 if (TagType == DeclSpec::TST_class)
2498 CurAS = AS_private;
2499 else
2500 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002501 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002502
Douglas Gregor07976d22010-06-21 22:31:09 +00002503 if (TagDecl) {
2504 // While we still have something to read, read the member-declarations.
2505 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2506 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002507
David Blaikie4e4d0842012-03-11 07:00:24 +00002508 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002509 Tok.is(tok::kw___if_not_exists))) {
2510 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2511 continue;
2512 }
2513
Douglas Gregor07976d22010-06-21 22:31:09 +00002514 // Check for extraneous top-level semicolon.
2515 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002516 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002517 continue;
2518 }
2519
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002520 if (Tok.is(tok::annot_pragma_vis)) {
2521 HandlePragmaVisibility();
2522 continue;
2523 }
2524
2525 if (Tok.is(tok::annot_pragma_pack)) {
2526 HandlePragmaPack();
2527 continue;
2528 }
2529
Argyrios Kyrtzidisf4deaef2012-10-12 17:39:59 +00002530 if (Tok.is(tok::annot_pragma_align)) {
2531 HandlePragmaAlign();
2532 continue;
2533 }
2534
Douglas Gregor07976d22010-06-21 22:31:09 +00002535 AccessSpecifier AS = getAccessSpecifierIfPresent();
2536 if (AS != AS_none) {
2537 // Current token is a C++ access specifier.
2538 CurAS = AS;
2539 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002540 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002541 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002542 AccessAttrs.clear();
2543 MaybeParseGNUAttributes(AccessAttrs);
2544
David Blaikie13f8daf2011-10-13 06:08:43 +00002545 SourceLocation EndLoc;
2546 if (Tok.is(tok::colon)) {
2547 EndLoc = Tok.getLocation();
2548 ConsumeToken();
2549 } else if (Tok.is(tok::semi)) {
2550 EndLoc = Tok.getLocation();
2551 ConsumeToken();
2552 Diag(EndLoc, diag::err_expected_colon)
2553 << FixItHint::CreateReplacement(EndLoc, ":");
2554 } else {
2555 EndLoc = ASLoc.getLocWithOffset(TokLength);
2556 Diag(EndLoc, diag::err_expected_colon)
2557 << FixItHint::CreateInsertion(EndLoc, ":");
2558 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002559
John McCalle402e722012-09-25 07:32:39 +00002560 // The Microsoft extension __interface does not permit non-public
2561 // access specifiers.
2562 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2563 Diag(ASLoc, diag::err_access_specifier_interface)
2564 << (CurAS == AS_protected);
2565 }
2566
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002567 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2568 AccessAttrs.getList())) {
2569 // found another attribute than only annotations
2570 AccessAttrs.clear();
2571 }
2572
Douglas Gregor07976d22010-06-21 22:31:09 +00002573 continue;
2574 }
2575
2576 // FIXME: Make sure we don't have a template here.
2577
2578 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002579 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002580 }
2581
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002582 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002583 } else {
2584 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002585 }
Mike Stump1eb44332009-09-09 15:08:12 +00002586
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002587 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002588 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002589 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002590
John McCall42a4f662010-05-28 08:11:17 +00002591 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002592 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002593 T.getOpenLocation(),
2594 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002595 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002596
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002597 // C++11 [class.mem]p2:
2598 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002599 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002600 // brace-or-equal-initializers for non-static data members (including such
2601 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002602 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002603 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002604 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002605 // declarations and the lexed inline method definitions, along with any
2606 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002607 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002608 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002609 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002610
2611 // We've finished with all pending member declarations.
2612 Actions.ActOnFinishCXXMemberDecls();
2613
Richard Smith7a614d82011-06-11 17:19:42 +00002614 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002615 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002616 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002617 }
2618
John McCall42a4f662010-05-28 08:11:17 +00002619 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002620 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2621 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002622
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002623 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002624 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002625 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002626}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002627
2628/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2629/// which explicitly initializes the members or base classes of a
2630/// class (C++ [class.base.init]). For example, the three initializers
2631/// after the ':' in the Derived constructor below:
2632///
2633/// @code
2634/// class Base { };
2635/// class Derived : Base {
2636/// int x;
2637/// float f;
2638/// public:
2639/// Derived(float f) : Base(), x(17), f(f) { }
2640/// };
2641/// @endcode
2642///
Mike Stump1eb44332009-09-09 15:08:12 +00002643/// [C++] ctor-initializer:
2644/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002645///
Mike Stump1eb44332009-09-09 15:08:12 +00002646/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002647/// mem-initializer ...[opt]
2648/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002649void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002650 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2651
John Wiegley28bbe4b2011-04-28 01:08:34 +00002652 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2653 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002654 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002655
Chris Lattner5f9e2722011-07-23 10:55:15 +00002656 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002657 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002658
Douglas Gregor7ad83902008-11-05 04:29:56 +00002659 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002660 if (Tok.is(tok::code_completion)) {
2661 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2662 MemInitializers.data(),
2663 MemInitializers.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002664 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002665 } else {
2666 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2667 if (!MemInit.isInvalid())
2668 MemInitializers.push_back(MemInit.get());
2669 else
2670 AnyErrors = true;
2671 }
2672
Douglas Gregor7ad83902008-11-05 04:29:56 +00002673 if (Tok.is(tok::comma))
2674 ConsumeToken();
2675 else if (Tok.is(tok::l_brace))
2676 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002677 // If the next token looks like a base or member initializer, assume that
2678 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002679 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2680 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2681 Diag(Loc, diag::err_ctor_init_missing_comma)
2682 << FixItHint::CreateInsertion(Loc, ", ");
2683 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002684 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002685 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002686 SkipUntil(tok::l_brace, true, true);
2687 break;
2688 }
2689 } while (true);
2690
David Blaikie93c86172013-01-17 05:26:25 +00002691 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002692 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002693}
2694
2695/// ParseMemInitializer - Parse a C++ member initializer, which is
2696/// part of a constructor initializer that explicitly initializes one
2697/// member or base class (C++ [class.base.init]). See
2698/// ParseConstructorInitializer for an example.
2699///
2700/// [C++] mem-initializer:
2701/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002702/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002703///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002704/// [C++] mem-initializer-id:
2705/// '::'[opt] nested-name-specifier[opt] class-name
2706/// identifier
John McCalld226f652010-08-21 09:40:31 +00002707Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002708 // parse '::'[opt] nested-name-specifier[opt]
2709 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002710 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002711 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002712 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002713 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002714 if (TemplateId->Kind == TNK_Type_template ||
2715 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002716 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002717 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002718 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002719 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002720 }
David Blaikief2116622012-01-24 06:03:59 +00002721 // Uses of decltype will already have been converted to annot_decltype by
2722 // ParseOptionalCXXScopeSpecifier at this point.
2723 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2724 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002725 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002726 return true;
2727 }
Mike Stump1eb44332009-09-09 15:08:12 +00002728
David Blaikief2116622012-01-24 06:03:59 +00002729 IdentifierInfo *II = 0;
2730 DeclSpec DS(AttrFactory);
2731 SourceLocation IdLoc = Tok.getLocation();
2732 if (Tok.is(tok::annot_decltype)) {
2733 // Get the decltype expression, if there is one.
2734 ParseDecltypeSpecifier(DS);
2735 } else {
2736 if (Tok.is(tok::identifier))
2737 // Get the identifier. This may be a member name or a class name,
2738 // but we'll let the semantic analysis determine which it is.
2739 II = Tok.getIdentifierInfo();
2740 ConsumeToken();
2741 }
2742
Douglas Gregor7ad83902008-11-05 04:29:56 +00002743
2744 // Parse the '('.
Richard Smith80ad52f2013-01-02 11:42:31 +00002745 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002746 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2747
Sebastian Redl6df65482011-09-24 17:48:25 +00002748 ExprResult InitList = ParseBraceInitializer();
2749 if (InitList.isInvalid())
2750 return true;
2751
2752 SourceLocation EllipsisLoc;
2753 if (Tok.is(tok::ellipsis))
2754 EllipsisLoc = ConsumeToken();
2755
2756 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002757 TemplateTypeTy, DS, IdLoc,
2758 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002759 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002760 BalancedDelimiterTracker T(*this, tok::l_paren);
2761 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002762
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002763 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002764 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002765 CommaLocsTy CommaLocs;
2766 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2767 SkipUntil(tok::r_paren);
2768 return true;
2769 }
2770
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002771 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002772
2773 SourceLocation EllipsisLoc;
2774 if (Tok.is(tok::ellipsis))
2775 EllipsisLoc = ConsumeToken();
2776
2777 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002778 TemplateTypeTy, DS, IdLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002779 T.getOpenLocation(), ArgExprs.data(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002780 ArgExprs.size(), T.getCloseLocation(),
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002781 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002782 }
2783
Richard Smith80ad52f2013-01-02 11:42:31 +00002784 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002785 : diag::err_expected_lparen);
2786 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002787}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002788
Sebastian Redl7acafd02011-03-05 14:45:16 +00002789/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002790///
Douglas Gregora4745612008-12-01 18:00:20 +00002791/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002792/// dynamic-exception-specification
2793/// noexcept-specification
2794///
2795/// noexcept-specification:
2796/// 'noexcept'
2797/// 'noexcept' '(' constant-expression ')'
2798ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002799Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002800 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002801 SmallVectorImpl<ParsedType> &DynamicExceptions,
2802 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002803 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002804 ExceptionSpecificationType Result = EST_None;
2805
2806 // See if there's a dynamic specification.
2807 if (Tok.is(tok::kw_throw)) {
2808 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2809 DynamicExceptions,
2810 DynamicExceptionRanges);
2811 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2812 "Produced different number of exception types and ranges.");
2813 }
2814
2815 // If there's no noexcept specification, we're done.
2816 if (Tok.isNot(tok::kw_noexcept))
2817 return Result;
2818
Richard Smith841804b2011-10-17 23:06:20 +00002819 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2820
Sebastian Redl7acafd02011-03-05 14:45:16 +00002821 // If we already had a dynamic specification, parse the noexcept for,
2822 // recovery, but emit a diagnostic and don't store the results.
2823 SourceRange NoexceptRange;
2824 ExceptionSpecificationType NoexceptType = EST_None;
2825
2826 SourceLocation KeywordLoc = ConsumeToken();
2827 if (Tok.is(tok::l_paren)) {
2828 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002829 BalancedDelimiterTracker T(*this, tok::l_paren);
2830 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002831 NoexceptType = EST_ComputedNoexcept;
2832 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002833 // The argument must be contextually convertible to bool. We use
2834 // ActOnBooleanCondition for this purpose.
2835 if (!NoexceptExpr.isInvalid())
2836 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2837 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002838 T.consumeClose();
2839 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002840 } else {
2841 // There is no argument.
2842 NoexceptType = EST_BasicNoexcept;
2843 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2844 }
2845
2846 if (Result == EST_None) {
2847 SpecificationRange = NoexceptRange;
2848 Result = NoexceptType;
2849
2850 // If there's a dynamic specification after a noexcept specification,
2851 // parse that and ignore the results.
2852 if (Tok.is(tok::kw_throw)) {
2853 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2854 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2855 DynamicExceptionRanges);
2856 }
2857 } else {
2858 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2859 }
2860
2861 return Result;
2862}
2863
2864/// ParseDynamicExceptionSpecification - Parse a C++
2865/// dynamic-exception-specification (C++ [except.spec]).
2866///
2867/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002868/// 'throw' '(' type-id-list [opt] ')'
2869/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002870///
Douglas Gregora4745612008-12-01 18:00:20 +00002871/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002872/// type-id ... [opt]
2873/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002874///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002875ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2876 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002877 SmallVectorImpl<ParsedType> &Exceptions,
2878 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002879 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002880
Sebastian Redl7acafd02011-03-05 14:45:16 +00002881 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002882 BalancedDelimiterTracker T(*this, tok::l_paren);
2883 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002884 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2885 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002886 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002887 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002888
Douglas Gregora4745612008-12-01 18:00:20 +00002889 // Parse throw(...), a Microsoft extension that means "this function
2890 // can throw anything".
2891 if (Tok.is(tok::ellipsis)) {
2892 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002893 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002894 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002895 T.consumeClose();
2896 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002897 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002898 }
2899
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002900 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002901 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002902 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002903 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002904
Douglas Gregora04426c2010-12-20 23:57:46 +00002905 if (Tok.is(tok::ellipsis)) {
2906 // C++0x [temp.variadic]p5:
2907 // - In a dynamic-exception-specification (15.4); the pattern is a
2908 // type-id.
2909 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002910 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002911 if (!Res.isInvalid())
2912 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2913 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002914
Sebastian Redlef65f062009-05-29 18:02:33 +00002915 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002916 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002917 Ranges.push_back(Range);
2918 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002919
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002920 if (Tok.is(tok::comma))
2921 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002922 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002923 break;
2924 }
2925
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002926 T.consumeClose();
2927 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002928 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002929}
Douglas Gregor6569d682009-05-27 23:11:45 +00002930
Douglas Gregordab60ad2010-10-01 18:44:50 +00002931/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2932/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002933TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002934 assert(Tok.is(tok::arrow) && "expected arrow");
2935
2936 ConsumeToken();
2937
Richard Smith7796eb52012-03-12 08:56:40 +00002938 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002939}
2940
Douglas Gregor6569d682009-05-27 23:11:45 +00002941/// \brief We have just started parsing the definition of a new class,
2942/// so push that class onto our stack of classes that is currently
2943/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002944Sema::ParsingClassState
John McCalle402e722012-09-25 07:32:39 +00002945Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
2946 bool IsInterface) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002947 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002948 "Nested class without outer class");
John McCalle402e722012-09-25 07:32:39 +00002949 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCalleee1d542011-02-14 07:13:47 +00002950 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002951}
2952
2953/// \brief Deallocate the given parsed class and all of its nested
2954/// classes.
2955void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002956 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2957 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002958 delete Class;
2959}
2960
2961/// \brief Pop the top class of the stack of classes that are
2962/// currently being parsed.
2963///
2964/// This routine should be called when we have finished parsing the
2965/// definition of a class, but have not yet popped the Scope
2966/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00002967void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002968 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002969
John McCalleee1d542011-02-14 07:13:47 +00002970 Actions.PopParsingClass(state);
2971
Douglas Gregor6569d682009-05-27 23:11:45 +00002972 ParsingClass *Victim = ClassStack.top();
2973 ClassStack.pop();
2974 if (Victim->TopLevelClass) {
2975 // Deallocate all of the nested classes of this class,
2976 // recursively: we don't need to keep any of this information.
2977 DeallocateParsedClasses(Victim);
2978 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002979 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002980 assert(!ClassStack.empty() && "Missing top-level class?");
2981
Douglas Gregord54eb442010-10-12 16:25:54 +00002982 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002983 // The victim is a nested class, but we will not need to perform
2984 // any processing after the definition of this class since it has
2985 // no members whose handling was delayed. Therefore, we can just
2986 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002987 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002988 return;
2989 }
2990
2991 // This nested class has some members that will need to be processed
2992 // after the top-level class is completely defined. Therefore, add
2993 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002994 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002995 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002996 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00002997}
Sean Huntbbd37c62009-11-21 08:43:09 +00002998
Richard Smithc56298d2012-04-10 03:25:07 +00002999/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3000///
3001/// \return the parsed identifier on success, and 0 if the next token is not an
3002/// attribute-token.
3003///
3004/// C++11 [dcl.attr.grammar]p3:
3005/// If a keyword or an alternative token that satisfies the syntactic
3006/// requirements of an identifier is contained in an attribute-token,
3007/// it is considered an identifier.
3008IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3009 switch (Tok.getKind()) {
3010 default:
3011 // Identifiers and keywords have identifier info attached.
3012 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3013 Loc = ConsumeToken();
3014 return II;
3015 }
3016 return 0;
3017
3018 case tok::ampamp: // 'and'
3019 case tok::pipe: // 'bitor'
3020 case tok::pipepipe: // 'or'
3021 case tok::caret: // 'xor'
3022 case tok::tilde: // 'compl'
3023 case tok::amp: // 'bitand'
3024 case tok::ampequal: // 'and_eq'
3025 case tok::pipeequal: // 'or_eq'
3026 case tok::caretequal: // 'xor_eq'
3027 case tok::exclaim: // 'not'
3028 case tok::exclaimequal: // 'not_eq'
3029 // Alternative tokens do not have identifier info, but their spelling
3030 // starts with an alphabetical character.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003031 SmallString<8> SpellingBuf;
Richard Smithc56298d2012-04-10 03:25:07 +00003032 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rose3f6f51e2013-02-08 22:30:41 +00003033 if (isLetter(Spelling[0])) {
Richard Smithc56298d2012-04-10 03:25:07 +00003034 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00003035 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00003036 }
3037 return 0;
3038 }
3039}
3040
Michael Han6880f492012-10-03 01:56:22 +00003041static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3042 IdentifierInfo *ScopeName) {
3043 switch (AttributeList::getKind(AttrName, ScopeName,
3044 AttributeList::AS_CXX11)) {
3045 case AttributeList::AT_CarriesDependency:
3046 case AttributeList::AT_FallThrough:
Richard Smithcd8ab512013-01-17 01:30:42 +00003047 case AttributeList::AT_CXX11NoReturn: {
Michael Han6880f492012-10-03 01:56:22 +00003048 return true;
3049 }
3050
3051 default:
3052 return false;
3053 }
3054}
3055
Richard Smithc56298d2012-04-10 03:25:07 +00003056/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003057/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00003058///
Richard Smith6ee326a2012-04-10 01:32:12 +00003059/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00003060/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003061/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00003062///
Richard Smith6ee326a2012-04-10 01:32:12 +00003063/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00003064/// attribute[opt]
3065/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00003066/// attribute '...'
3067/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00003068///
Richard Smith6ee326a2012-04-10 01:32:12 +00003069/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00003070/// attribute-token attribute-argument-clause[opt]
3071///
Richard Smith6ee326a2012-04-10 01:32:12 +00003072/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003073/// identifier
3074/// attribute-scoped-token
3075///
Richard Smith6ee326a2012-04-10 01:32:12 +00003076/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003077/// attribute-namespace '::' identifier
3078///
Richard Smith6ee326a2012-04-10 01:32:12 +00003079/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00003080/// identifier
3081///
Richard Smith6ee326a2012-04-10 01:32:12 +00003082/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00003083/// '(' balanced-token-seq ')'
3084///
Richard Smith6ee326a2012-04-10 01:32:12 +00003085/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00003086/// balanced-token
3087/// balanced-token-seq balanced-token
3088///
Richard Smith6ee326a2012-04-10 01:32:12 +00003089/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003090/// '(' balanced-token-seq ')'
3091/// '[' balanced-token-seq ']'
3092/// '{' balanced-token-seq '}'
3093/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00003094void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003095 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003096 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00003097 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003098 ParseAlignmentSpecifier(attrs, endLoc);
3099 return;
3100 }
3101
Sean Huntbbd37c62009-11-21 08:43:09 +00003102 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00003103 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00003104
Richard Smith41be6732011-10-14 20:48:27 +00003105 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3106
Sean Huntbbd37c62009-11-21 08:43:09 +00003107 ConsumeBracket();
3108 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003109
Richard Smithcd8ab512013-01-17 01:30:42 +00003110 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3111
Richard Smithc56298d2012-04-10 03:25:07 +00003112 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003113 // attribute not present
3114 if (Tok.is(tok::comma)) {
3115 ConsumeToken();
3116 continue;
3117 }
3118
Richard Smithc56298d2012-04-10 03:25:07 +00003119 SourceLocation ScopeLoc, AttrLoc;
3120 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3121
3122 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3123 if (!AttrName)
3124 // Break out to the "expected ']'" diagnostic.
3125 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003126
Sean Huntbbd37c62009-11-21 08:43:09 +00003127 // scoped attribute
3128 if (Tok.is(tok::coloncolon)) {
3129 ConsumeToken();
3130
Richard Smithc56298d2012-04-10 03:25:07 +00003131 ScopeName = AttrName;
3132 ScopeLoc = AttrLoc;
3133
3134 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3135 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003136 Diag(Tok.getLocation(), diag::err_expected_ident);
3137 SkipUntil(tok::r_square, tok::comma, true, true);
3138 continue;
3139 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003140 }
3141
Michael Han6880f492012-10-03 01:56:22 +00003142 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Sean Huntbbd37c62009-11-21 08:43:09 +00003143 bool AttrParsed = false;
Sean Huntbbd37c62009-11-21 08:43:09 +00003144
Richard Smithcd8ab512013-01-17 01:30:42 +00003145 if (StandardAttr &&
3146 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3147 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3148 << AttrName << SourceRange(SeenAttrs[AttrName]);
3149
Michael Han6880f492012-10-03 01:56:22 +00003150 // Parse attribute arguments
3151 if (Tok.is(tok::l_paren)) {
3152 if (ScopeName && ScopeName->getName() == "gnu") {
3153 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3154 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3155 AttrParsed = true;
3156 } else {
3157 if (StandardAttr)
3158 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3159 << AttrName->getName();
3160
3161 // FIXME: handle other formats of c++11 attribute arguments
3162 ConsumeParen();
3163 SkipUntil(tok::r_paren, false);
3164 }
3165 }
3166
3167 if (!AttrParsed)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00003168 attrs.addNew(AttrName,
3169 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3170 AttrLoc),
3171 ScopeName, ScopeLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00003172 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smith6ee326a2012-04-10 01:32:12 +00003173
Richard Smithc56298d2012-04-10 03:25:07 +00003174 if (Tok.is(tok::ellipsis)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003175 ConsumeToken();
Michael Han6880f492012-10-03 01:56:22 +00003176
3177 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3178 << AttrName->getName();
Richard Smithc56298d2012-04-10 03:25:07 +00003179 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003180 }
3181
3182 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3183 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003184 if (endLoc)
3185 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00003186 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3187 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003188}
Sean Huntbbd37c62009-11-21 08:43:09 +00003189
Sean Hunt2edf0a22012-06-23 05:07:58 +00003190/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003191///
3192/// attribute-specifier-seq:
3193/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00003194void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003195 SourceLocation *endLoc) {
3196 SourceLocation StartLoc = Tok.getLocation(), Loc;
3197 if (!endLoc)
3198 endLoc = &Loc;
3199
Douglas Gregor8828ee72011-10-07 20:35:25 +00003200 do {
Richard Smithc56298d2012-04-10 03:25:07 +00003201 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003202 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003203
3204 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003205}
3206
Francois Pichet334d47e2010-10-11 12:59:39 +00003207/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3208///
3209/// [MS] ms-attribute:
3210/// '[' token-seq ']'
3211///
3212/// [MS] ms-attribute-seq:
3213/// ms-attribute[opt]
3214/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003215void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3216 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003217 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3218
3219 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003220 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003221 ConsumeBracket();
3222 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003223 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003224 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3225 }
3226}
Francois Pichet563a6452011-05-25 10:19:49 +00003227
3228void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3229 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003230 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003231 if (ParseMicrosoftIfExistsCondition(Result))
3232 return;
3233
Douglas Gregor3896fc52011-10-24 22:31:10 +00003234 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3235 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003236 Diag(Tok, diag::err_expected_lbrace);
3237 return;
3238 }
Francois Pichet563a6452011-05-25 10:19:49 +00003239
Douglas Gregor3896fc52011-10-24 22:31:10 +00003240 switch (Result.Behavior) {
3241 case IEB_Parse:
3242 // Parse the declarations below.
3243 break;
3244
3245 case IEB_Dependent:
3246 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3247 << Result.IsIfExists;
3248 // Fall through to skip.
3249
3250 case IEB_Skip:
3251 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003252 return;
3253 }
3254
Douglas Gregor3896fc52011-10-24 22:31:10 +00003255 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003256 // __if_exists, __if_not_exists can nest.
3257 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3258 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3259 continue;
3260 }
3261
3262 // Check for extraneous top-level semicolon.
3263 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003264 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003265 continue;
3266 }
3267
3268 AccessSpecifier AS = getAccessSpecifierIfPresent();
3269 if (AS != AS_none) {
3270 // Current token is a C++ access specifier.
3271 CurAS = AS;
3272 SourceLocation ASLoc = Tok.getLocation();
3273 ConsumeToken();
3274 if (Tok.is(tok::colon))
3275 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3276 else
3277 Diag(Tok, diag::err_expected_colon);
3278 ConsumeToken();
3279 continue;
3280 }
3281
3282 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003283 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003284 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003285
3286 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003287}