blob: d7f8e982aa5f0996ae01fee1336aa5b88699157c [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor1b7f8982008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/OperatorKinds.h"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
John McCall19510852010-08-20 18:27:03 +000020#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Sema/Scope.h"
John McCalle402e722012-09-25 07:32:39 +000023#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000025using namespace clang;
26
27/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000028/// may either be a top level namespace or a block-level namespace alias. If
29/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000030///
31/// namespace-definition: [C++ 7.3: basic.namespace]
32/// named-namespace-definition
33/// unnamed-namespace-definition
34///
35/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000036/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000037///
38/// named-namespace-definition:
39/// original-namespace-definition
40/// extension-namespace-definition
41///
42/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000043/// 'inline'[opt] 'namespace' identifier attributes[opt]
44/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000045///
46/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000047/// 'inline'[opt] 'namespace' original-namespace-name
48/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000049///
Chris Lattner8f08cb72007-08-25 06:57:03 +000050/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
51/// 'namespace' identifier '=' qualified-namespace-specifier ';'
52///
John McCalld226f652010-08-21 09:40:31 +000053Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000054 SourceLocation &DeclEnd,
55 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000056 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000057 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +000058 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000059
Douglas Gregor49f40bd2009-09-18 19:03:04 +000060 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000061 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000062 cutOffParsing();
63 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +000064 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000065
Chris Lattner8f08cb72007-08-25 06:57:03 +000066 SourceLocation IdentLoc;
67 IdentifierInfo *Ident = 0;
Richard Trieuf858bd82011-05-26 20:11:09 +000068 std::vector<SourceLocation> ExtraIdentLoc;
69 std::vector<IdentifierInfo*> ExtraIdent;
70 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000071
72 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattner04d66662007-10-09 17:33:22 +000074 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000075 Ident = Tok.getIdentifierInfo();
76 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieuf858bd82011-05-26 20:11:09 +000077 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
78 ExtraNamespaceLoc.push_back(ConsumeToken());
79 ExtraIdent.push_back(Tok.getIdentifierInfo());
80 ExtraIdentLoc.push_back(ConsumeToken());
81 }
Chris Lattner8f08cb72007-08-25 06:57:03 +000082 }
Mike Stump1eb44332009-09-09 15:08:12 +000083
Chris Lattner8f08cb72007-08-25 06:57:03 +000084 // Read label attributes, if present.
John McCall0b7e6782011-03-24 11:26:52 +000085 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000086 if (Tok.is(tok::kw___attribute)) {
87 attrTok = Tok;
John McCall7f040a92010-12-24 02:08:15 +000088 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000089 }
Mike Stump1eb44332009-09-09 15:08:12 +000090
Douglas Gregor6a588dd2009-06-17 19:49:00 +000091 if (Tok.is(tok::equal)) {
Nico Webere1bb3292012-10-27 23:44:27 +000092 if (Ident == 0) {
93 Diag(Tok, diag::err_expected_ident);
94 // Skip to end of the definition and eat the ';'.
95 SkipUntil(tok::semi);
96 return 0;
97 }
John McCall7f040a92010-12-24 02:08:15 +000098 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +000099 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +0000100 if (InlineLoc.isValid())
101 Diag(InlineLoc, diag::err_inline_namespace_alias)
102 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000103 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000104 }
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Richard Trieuf858bd82011-05-26 20:11:09 +0000106
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000107 BalancedDelimiterTracker T(*this, tok::l_brace);
108 if (T.consumeOpen()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000109 if (!ExtraIdent.empty()) {
110 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
111 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
112 }
Mike Stump1eb44332009-09-09 15:08:12 +0000113 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +0000114 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +0000115 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Douglas Gregor23c94db2010-07-02 17:43:08 +0000118 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
119 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
120 getCurScope()->getFnParent()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000121 if (!ExtraIdent.empty()) {
122 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
123 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
124 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000125 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Douglas Gregor95f1b152010-05-14 05:08:22 +0000126 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000127 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000128 }
129
Richard Trieuf858bd82011-05-26 20:11:09 +0000130 if (!ExtraIdent.empty()) {
131 TentativeParsingAction TPA(*this);
132 SkipUntil(tok::r_brace, /*StopAtSemi*/false, /*DontConsume*/true);
133 Token rBraceToken = Tok;
134 TPA.Revert();
135
136 if (!rBraceToken.is(tok::r_brace)) {
137 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
138 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
139 } else {
Benjamin Kramer9910df02011-05-26 21:32:30 +0000140 std::string NamespaceFix;
Richard Trieuf858bd82011-05-26 20:11:09 +0000141 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
142 E = ExtraIdent.end(); I != E; ++I) {
143 NamespaceFix += " { namespace ";
144 NamespaceFix += (*I)->getName();
145 }
Benjamin Kramer9910df02011-05-26 21:32:30 +0000146
Richard Trieuf858bd82011-05-26 20:11:09 +0000147 std::string RBraces;
Benjamin Kramer9910df02011-05-26 21:32:30 +0000148 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieuf858bd82011-05-26 20:11:09 +0000149 RBraces += "} ";
Benjamin Kramer9910df02011-05-26 21:32:30 +0000150
Richard Trieuf858bd82011-05-26 20:11:09 +0000151 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
152 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
153 ExtraIdentLoc.back()),
154 NamespaceFix)
155 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
156 }
157 }
158
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000159 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith7fe62082011-10-15 05:09:34 +0000160 if (InlineLoc.isValid())
Richard Smith80ad52f2013-01-02 11:42:31 +0000161 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000162 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000163
Chris Lattner51448322009-03-29 14:02:43 +0000164 // Enter a scope for the namespace.
165 ParseScope NamespaceScope(this, Scope::DeclScope);
166
John McCalld226f652010-08-21 09:40:31 +0000167 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000168 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000169 IdentLoc, Ident, T.getOpenLocation(),
170 attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000171
John McCallf312b1e2010-08-26 23:41:50 +0000172 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
173 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Richard Trieuf858bd82011-05-26 20:11:09 +0000175 // Parse the contents of the namespace. This includes parsing recovery on
176 // any improperly nested namespaces.
177 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000178 InlineLoc, attrs, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattner51448322009-03-29 14:02:43 +0000180 // Leave the namespace scope.
181 NamespaceScope.Exit();
182
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000183 DeclEnd = T.getCloseLocation();
184 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner51448322009-03-29 14:02:43 +0000185
186 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000187}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000188
Richard Trieuf858bd82011-05-26 20:11:09 +0000189/// ParseInnerNamespace - Parse the contents of a namespace.
190void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
191 std::vector<IdentifierInfo*>& Ident,
192 std::vector<SourceLocation>& NamespaceLoc,
193 unsigned int index, SourceLocation& InlineLoc,
Richard Trieuf858bd82011-05-26 20:11:09 +0000194 ParsedAttributes& attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000195 BalancedDelimiterTracker &Tracker) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000196 if (index == Ident.size()) {
197 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
198 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000199 MaybeParseCXX11Attributes(attrs);
Richard Trieuf858bd82011-05-26 20:11:09 +0000200 MaybeParseMicrosoftAttributes(attrs);
201 ParseExternalDeclaration(attrs);
202 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000203
204 // The caller is what called check -- we are simply calling
205 // the close for it.
206 Tracker.consumeClose();
Richard Trieuf858bd82011-05-26 20:11:09 +0000207
208 return;
209 }
210
211 // Parse improperly nested namespaces.
212 ParseScope NamespaceScope(this, Scope::DeclScope);
213 Decl *NamespcDecl =
214 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
215 NamespaceLoc[index], IdentLoc[index],
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000216 Ident[index], Tracker.getOpenLocation(),
217 attrs.getList());
Richard Trieuf858bd82011-05-26 20:11:09 +0000218
219 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000220 attrs, Tracker);
Richard Trieuf858bd82011-05-26 20:11:09 +0000221
222 NamespaceScope.Exit();
223
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000224 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieuf858bd82011-05-26 20:11:09 +0000225}
226
Anders Carlssonf67606a2009-03-28 04:07:16 +0000227/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
228/// alias definition.
229///
John McCalld226f652010-08-21 09:40:31 +0000230Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000231 SourceLocation AliasLoc,
232 IdentifierInfo *Alias,
233 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000234 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Anders Carlssonf67606a2009-03-28 04:07:16 +0000236 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000238 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000239 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000240 cutOffParsing();
241 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000242 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000243
Anders Carlssonf67606a2009-03-28 04:07:16 +0000244 CXXScopeSpec SS;
245 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000246 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000247
248 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
249 Diag(Tok, diag::err_expected_namespace_name);
250 // Skip to end of the definition and eat the ';'.
251 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000252 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000253 }
254
255 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000256 IdentifierInfo *Ident = Tok.getIdentifierInfo();
257 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Anders Carlssonf67606a2009-03-28 04:07:16 +0000259 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000260 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000261 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
262 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Douglas Gregor23c94db2010-07-02 17:43:08 +0000264 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000265 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000266}
267
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000268/// ParseLinkage - We know that the current token is a string_literal
269/// and just before that, that extern was seen.
270///
271/// linkage-specification: [C++ 7.5p2: dcl.link]
272/// 'extern' string-literal '{' declaration-seq[opt] '}'
273/// 'extern' string-literal declaration
274///
Chris Lattner7d642712010-11-09 20:15:55 +0000275Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000276 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000277 SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000278 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000279 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000280 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000281 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000282
Richard Smith99831e42012-03-06 03:21:47 +0000283 // FIXME: This is incorrect: linkage-specifiers are parsed in translation
284 // phase 7, so string-literal concatenation is supposed to occur.
285 // extern "" "C" "" "+" "+" { } is legal.
286 if (Tok.hasUDSuffix())
287 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000288 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000289
Douglas Gregor074149e2009-01-05 19:45:36 +0000290 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000291 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000292 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000293 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000294 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000295 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000296 : SourceLocation());
297
John McCall0b7e6782011-03-24 11:26:52 +0000298 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000299 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000300 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000301
Douglas Gregor074149e2009-01-05 19:45:36 +0000302 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-05-01 16:25:54 +0000303 // Reset the source range in DS, as the leading "extern"
304 // does not really belong to the inner declaration ...
305 DS.SetRangeStart(SourceLocation());
306 DS.SetRangeEnd(SourceLocation());
307 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000308 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000309 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000310 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000311 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000312 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000313
Douglas Gregor63a01132010-02-07 08:38:28 +0000314 DS.abort();
315
John McCall7f040a92010-12-24 02:08:15 +0000316 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000317
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000318 BalancedDelimiterTracker T(*this, tok::l_brace);
319 T.consumeOpen();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000320 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000321 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000322 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000323 MaybeParseMicrosoftAttributes(attrs);
324 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000325 }
326
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000327 T.consumeClose();
Chris Lattner7d642712010-11-09 20:15:55 +0000328 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000329 T.getCloseLocation());
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000330}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000331
Douglas Gregorf780abc2008-12-30 03:27:21 +0000332/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
333/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000334Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000335 const ParsedTemplateInfo &TemplateInfo,
336 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000337 ParsedAttributesWithRange &attrs,
338 Decl **OwnedType) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000339 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000340 ObjCDeclContextSwitch ObjCDC(*this);
341
Douglas Gregorf780abc2008-12-30 03:27:21 +0000342 // Eat 'using'.
343 SourceLocation UsingLoc = ConsumeToken();
344
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000345 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000346 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000347 cutOffParsing();
348 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000349 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000350
John McCall78b81052010-11-10 02:40:36 +0000351 // 'using namespace' means this is a using-directive.
352 if (Tok.is(tok::kw_namespace)) {
353 // Template parameters are always an error here.
354 if (TemplateInfo.Kind) {
355 SourceRange R = TemplateInfo.getSourceRange();
356 Diag(UsingLoc, diag::err_templated_using_directive)
357 << R << FixItHint::CreateRemoval(R);
358 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000359
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000360 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000361 }
362
Richard Smith162e1c12011-04-15 14:24:37 +0000363 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000364
365 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000366 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000367
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000368 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000369 AS_none, OwnedType);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000370}
371
372/// ParseUsingDirective - Parse C++ using-directive, assumes
373/// that current token is 'namespace' and 'using' was already parsed.
374///
375/// using-directive: [C++ 7.3.p4: namespace.udir]
376/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
377/// namespace-name ;
378/// [GNU] using-directive:
379/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
380/// namespace-name attributes[opt] ;
381///
John McCalld226f652010-08-21 09:40:31 +0000382Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000383 SourceLocation UsingLoc,
384 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000385 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000386 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
387
388 // Eat 'namespace'.
389 SourceLocation NamespcLoc = ConsumeToken();
390
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000391 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000392 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000393 cutOffParsing();
394 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000395 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000396
Douglas Gregorf780abc2008-12-30 03:27:21 +0000397 CXXScopeSpec SS;
398 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000399 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000400
Douglas Gregorf780abc2008-12-30 03:27:21 +0000401 IdentifierInfo *NamespcName = 0;
402 SourceLocation IdentLoc = SourceLocation();
403
404 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000405 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000406 Diag(Tok, diag::err_expected_namespace_name);
407 // If there was invalid namespace name, skip to end of decl, and eat ';'.
408 SkipUntil(tok::semi);
409 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000410 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Chris Lattner823c44e2009-01-06 07:27:21 +0000413 // Parse identifier.
414 NamespcName = Tok.getIdentifierInfo();
415 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner823c44e2009-01-06 07:27:21 +0000417 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000418 bool GNUAttr = false;
419 if (Tok.is(tok::kw___attribute)) {
420 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000421 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattner823c44e2009-01-06 07:27:21 +0000424 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000425 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000426 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000427 GNUAttr ? diag::err_expected_semi_after_attribute_list
428 : diag::err_expected_semi_after_namespace_name,
429 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000430
Douglas Gregor23c94db2010-07-02 17:43:08 +0000431 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000432 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000433}
434
Richard Smith162e1c12011-04-15 14:24:37 +0000435/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
436/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000437///
438/// using-declaration: [C++ 7.3.p3: namespace.udecl]
439/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000440/// unqualified-id
441/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000442///
Richard Smithd03de6a2013-01-29 10:02:16 +0000443/// alias-declaration: C++11 [dcl.dcl]p1
444/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smith162e1c12011-04-15 14:24:37 +0000445///
John McCalld226f652010-08-21 09:40:31 +0000446Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000447 const ParsedTemplateInfo &TemplateInfo,
448 SourceLocation UsingLoc,
449 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000450 AccessSpecifier AS,
451 Decl **OwnedType) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000452 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000453 SourceLocation TypenameLoc;
Richard Smith6b3d3e52013-02-20 19:22:51 +0000454 bool IsTypeName = false;
455 ParsedAttributesWithRange Attrs(AttrFactory);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000456
457 // FIXME: Simply skip the attributes and diagnose, don't bother parsing them.
Richard Smith6b3d3e52013-02-20 19:22:51 +0000458 MaybeParseCXX11Attributes(Attrs);
459 ProhibitAttributes(Attrs);
460 Attrs.clear();
461 Attrs.Range = SourceRange();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000462
463 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000464 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000465 if (Tok.is(tok::kw_typename)) {
Richard Smith6b3d3e52013-02-20 19:22:51 +0000466 TypenameLoc = ConsumeToken();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000467 IsTypeName = true;
468 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000469
470 // Parse nested-name-specifier.
Richard Smith2db075b2013-03-26 01:15:19 +0000471 IdentifierInfo *LastII = 0;
472 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
473 /*MayBePseudoDtor=*/0, /*IsTypename=*/false,
474 /*LastII=*/&LastII);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000475
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000476 // Check nested-name specifier.
477 if (SS.isInvalid()) {
478 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000479 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000480 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000481
Richard Smith2db075b2013-03-26 01:15:19 +0000482 SourceLocation TemplateKWLoc;
483 UnqualifiedId Name;
484
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000485 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000486 // destructor names and allow the action module to diagnose any semantic
487 // errors.
Richard Smith2db075b2013-03-26 01:15:19 +0000488 //
489 // C++11 [class.qual]p2:
490 // [...] in a using-declaration that is a member-declaration, if the name
491 // specified after the nested-name-specifier is the same as the identifier
492 // or the simple-template-id's template-name in the last component of the
493 // nested-name-specifier, the name is [...] considered to name the
494 // constructor.
495 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
496 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
497 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
498 !SS.getScopeRep()->getAsNamespace() &&
499 !SS.getScopeRep()->getAsNamespaceAlias()) {
500 SourceLocation IdLoc = ConsumeToken();
501 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
502 Name.setConstructorName(Type, IdLoc, IdLoc);
503 } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
504 /*AllowDestructorName=*/ true,
505 /*AllowConstructorName=*/ true, ParsedType(),
506 TemplateKWLoc, Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000507 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000508 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000509 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000510
Richard Smith6b3d3e52013-02-20 19:22:51 +0000511 MaybeParseCXX11Attributes(Attrs);
Richard Smith162e1c12011-04-15 14:24:37 +0000512
513 // Maybe this is an alias-declaration.
514 bool IsAliasDecl = Tok.is(tok::equal);
515 TypeResult TypeAlias;
516 if (IsAliasDecl) {
Richard Smith6b3d3e52013-02-20 19:22:51 +0000517 // TODO: Can GNU attributes appear here?
Richard Smith162e1c12011-04-15 14:24:37 +0000518 ConsumeToken();
519
Richard Smith80ad52f2013-01-02 11:42:31 +0000520 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000521 diag::warn_cxx98_compat_alias_declaration :
522 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000523
Richard Smith3e4c6c42011-05-05 21:57:07 +0000524 // Type alias templates cannot be specialized.
525 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000526 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
527 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000528 SpecKind = 0;
529 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
530 SpecKind = 1;
531 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
532 SpecKind = 2;
533 if (SpecKind != -1) {
534 SourceRange Range;
535 if (SpecKind == 0)
536 Range = SourceRange(Name.TemplateId->LAngleLoc,
537 Name.TemplateId->RAngleLoc);
538 else
539 Range = TemplateInfo.getSourceRange();
540 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
541 << SpecKind << Range;
542 SkipUntil(tok::semi);
543 return 0;
544 }
545
Richard Smith162e1c12011-04-15 14:24:37 +0000546 // Name must be an identifier.
547 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
548 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
549 // No removal fixit: can't recover from this.
550 SkipUntil(tok::semi);
551 return 0;
552 } else if (IsTypeName)
553 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
554 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
555 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
556 else if (SS.isNotEmpty())
557 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
558 << FixItHint::CreateRemoval(SS.getRange());
559
Richard Smith3e4c6c42011-05-05 21:57:07 +0000560 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
561 Declarator::AliasTemplateContext :
Richard Smith6b3d3e52013-02-20 19:22:51 +0000562 Declarator::AliasDeclContext, AS, OwnedType,
563 &Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000564 } else {
565 // C++11 attributes are not allowed on a using-declaration, but GNU ones
566 // are.
Richard Smith6b3d3e52013-02-20 19:22:51 +0000567 ProhibitAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000568
Richard Smith162e1c12011-04-15 14:24:37 +0000569 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith6b3d3e52013-02-20 19:22:51 +0000570 MaybeParseGNUAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000571 }
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000573 // Eat ';'.
574 DeclEnd = Tok.getLocation();
575 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000576 !Attrs.empty() ? "attributes list" :
Richard Smith162e1c12011-04-15 14:24:37 +0000577 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000578 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000579
John McCall78b81052010-11-10 02:40:36 +0000580 // Diagnose an attempt to declare a templated using-declaration.
Richard Smithd03de6a2013-01-29 10:02:16 +0000581 // In C++11, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000582 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000583 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000584 SourceRange R = TemplateInfo.getSourceRange();
585 Diag(UsingLoc, diag::err_templated_using_declaration)
586 << R << FixItHint::CreateRemoval(R);
587
588 // Unfortunately, we have to bail out instead of recovering by
589 // ignoring the parameters, just in case the nested name specifier
590 // depends on the parameters.
591 return 0;
592 }
593
Douglas Gregor480b53c2011-09-26 14:30:28 +0000594 // "typename" keyword is allowed for identifiers only,
595 // because it may be a type definition.
596 if (IsTypeName && Name.getKind() != UnqualifiedId::IK_Identifier) {
597 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
598 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
599 // Proceed parsing, but reset the IsTypeName flag.
600 IsTypeName = false;
601 }
602
Richard Smith3e4c6c42011-05-05 21:57:07 +0000603 if (IsAliasDecl) {
604 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramer5354e772012-08-23 23:38:35 +0000605 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3e4c6c42011-05-05 21:57:07 +0000606 TemplateParams ? TemplateParams->data() : 0,
607 TemplateParams ? TemplateParams->size() : 0);
608 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000609 UsingLoc, Name, Attrs.getList(),
610 TypeAlias);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000611 }
Richard Smith162e1c12011-04-15 14:24:37 +0000612
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000613 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000614 Name, Attrs.getList(),
John McCall7f040a92010-12-24 02:08:15 +0000615 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000616}
617
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000618/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000619///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000620/// [C++0x] static_assert-declaration:
621/// static_assert ( constant-expression , string-literal ) ;
622///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000623/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000624/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000625///
John McCalld226f652010-08-21 09:40:31 +0000626Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000627 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
628 "Not a static_assert declaration");
629
David Blaikie4e4d0842012-03-11 07:00:24 +0000630 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000631 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000632 if (Tok.is(tok::kw_static_assert))
633 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000634
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000635 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000637 BalancedDelimiterTracker T(*this, tok::l_paren);
638 if (T.consumeOpen()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000639 Diag(Tok, diag::err_expected_lparen);
Richard Smith3686c712012-09-13 19:12:50 +0000640 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000641 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
John McCall60d7b3a2010-08-24 06:29:42 +0000644 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000645 if (AssertExpr.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000646 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000647 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000648 }
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Anders Carlssonad5f9602009-03-13 23:29:20 +0000650 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000651 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000652
Richard Smith0cc323c2012-03-05 23:20:05 +0000653 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000654 Diag(Tok, diag::err_expected_string_literal)
655 << /*Source='static_assert'*/1;
Richard Smith3686c712012-09-13 19:12:50 +0000656 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000657 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000658 }
Mike Stump1eb44332009-09-09 15:08:12 +0000659
John McCall60d7b3a2010-08-24 06:29:42 +0000660 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smith99831e42012-03-06 03:21:47 +0000661 if (AssertMessage.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000662 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000663 return 0;
Richard Smith99831e42012-03-06 03:21:47 +0000664 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000665
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000666 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Chris Lattner97144fc2009-04-02 04:16:50 +0000668 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000669 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000670
John McCall9ae2f072010-08-23 23:25:46 +0000671 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
672 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000673 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000674 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000675}
676
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000677/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
678///
679/// 'decltype' ( expression )
680///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000681SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
682 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
683 && "Not a decltype specifier");
684
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000685
David Blaikie42d6d0c2011-12-04 05:04:18 +0000686 ExprResult Result;
687 SourceLocation StartLoc = Tok.getLocation();
688 SourceLocation EndLoc;
689
690 if (Tok.is(tok::annot_decltype)) {
691 Result = getExprAnnotation(Tok);
692 EndLoc = Tok.getAnnotationEndLoc();
693 ConsumeToken();
694 if (Result.isInvalid()) {
695 DS.SetTypeSpecError();
696 return EndLoc;
697 }
698 } else {
Richard Smithc7b55432012-02-24 22:30:04 +0000699 if (Tok.getIdentifierInfo()->isStr("decltype"))
700 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000701
David Blaikie42d6d0c2011-12-04 05:04:18 +0000702 ConsumeToken();
703
704 BalancedDelimiterTracker T(*this, tok::l_paren);
705 if (T.expectAndConsume(diag::err_expected_lparen_after,
706 "decltype", tok::r_paren)) {
707 DS.SetTypeSpecError();
708 return T.getOpenLocation() == Tok.getLocation() ?
709 StartLoc : T.getOpenLocation();
710 }
711
712 // Parse the expression
713
714 // C++0x [dcl.type.simple]p4:
715 // The operand of the decltype specifier is an unevaluated operand.
Richard Smith76f3f692012-02-22 02:04:18 +0000716 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
717 0, /*IsDecltype=*/true);
David Blaikie42d6d0c2011-12-04 05:04:18 +0000718 Result = ParseExpression();
719 if (Result.isInvalid()) {
David Blaikie42d6d0c2011-12-04 05:04:18 +0000720 DS.SetTypeSpecError();
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000721 if (SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true)) {
722 EndLoc = ConsumeParen();
723 } else {
Richard Smith569cdc82012-12-09 04:17:57 +0000724 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000725 // Backtrack to get the location of the last token before the semi.
726 PP.RevertCachedTokens(2);
727 ConsumeToken(); // the semi.
728 EndLoc = ConsumeAnyToken();
729 assert(Tok.is(tok::semi));
730 } else {
731 EndLoc = Tok.getLocation();
732 }
733 }
734 return EndLoc;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000735 }
736
737 // Match the ')'
738 T.consumeClose();
739 if (T.getCloseLocation().isInvalid()) {
740 DS.SetTypeSpecError();
741 // FIXME: this should return the location of the last token
742 // that was consumed (by "consumeClose()")
743 return T.getCloseLocation();
744 }
745
Richard Smith76f3f692012-02-22 02:04:18 +0000746 Result = Actions.ActOnDecltypeExpression(Result.take());
747 if (Result.isInvalid()) {
748 DS.SetTypeSpecError();
749 return T.getCloseLocation();
750 }
751
David Blaikie42d6d0c2011-12-04 05:04:18 +0000752 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000753 }
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000755 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000756 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000757 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000758 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
David Blaikie42d6d0c2011-12-04 05:04:18 +0000759 DiagID, Result.release())) {
John McCallfec54012009-08-03 20:12:06 +0000760 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000761 DS.SetTypeSpecError();
762 }
763 return EndLoc;
764}
765
766void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
767 SourceLocation StartLoc,
768 SourceLocation EndLoc) {
769 // make sure we have a token we can turn into an annotation token
770 if (PP.isBacktrackEnabled())
771 PP.RevertCachedTokens(1);
772 else
773 PP.EnterToken(Tok);
774
775 Tok.setKind(tok::annot_decltype);
776 setExprAnnotation(Tok, DS.getTypeSpecType() == TST_decltype ?
777 DS.getRepAsExpr() : ExprResult());
778 Tok.setAnnotationEndLoc(EndLoc);
779 Tok.setLocation(StartLoc);
780 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000781}
782
Sean Huntdb5d44b2011-05-19 05:37:45 +0000783void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
784 assert(Tok.is(tok::kw___underlying_type) &&
785 "Not an underlying type specifier");
786
787 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000788 BalancedDelimiterTracker T(*this, tok::l_paren);
789 if (T.expectAndConsume(diag::err_expected_lparen_after,
790 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000791 return;
792 }
793
794 TypeResult Result = ParseTypeName();
795 if (Result.isInvalid()) {
796 SkipUntil(tok::r_paren);
797 return;
798 }
799
800 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000801 T.consumeClose();
802 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000803 return;
804
805 const char *PrevSpec = 0;
806 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000807 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000808 DiagID, Result.release()))
809 Diag(StartLoc, DiagID) << PrevSpec;
810}
811
David Blaikie09048df2011-10-25 15:01:20 +0000812/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
813/// class name or decltype-specifier. Note that we only check that the result
814/// names a type; semantic analysis will need to verify that the type names a
815/// class. The result is either a type or null, depending on whether a type
816/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000817///
Richard Smith05321402013-02-19 23:47:15 +0000818/// base-type-specifier: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000819/// class-or-decltype
Richard Smith05321402013-02-19 23:47:15 +0000820/// class-or-decltype: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000821/// nested-name-specifier[opt] class-name
822/// decltype-specifier
Richard Smith05321402013-02-19 23:47:15 +0000823/// class-name: [C++ class.name]
Douglas Gregor42a552f2008-11-05 20:51:48 +0000824/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000825/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000826///
Richard Smith05321402013-02-19 23:47:15 +0000827/// In C++98, instead of base-type-specifier, we have:
828///
829/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie22216eb2011-10-25 17:10:12 +0000830Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
831 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000832 // Ignore attempts to use typename
833 if (Tok.is(tok::kw_typename)) {
834 Diag(Tok, diag::err_expected_class_name_not_template)
835 << FixItHint::CreateRemoval(Tok.getLocation());
836 ConsumeToken();
837 }
838
David Blaikie152aa4b2011-10-25 18:17:58 +0000839 // Parse optional nested-name-specifier
840 CXXScopeSpec SS;
841 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
842
843 BaseLoc = Tok.getLocation();
844
David Blaikie22216eb2011-10-25 17:10:12 +0000845 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000846 // tok == kw_decltype is just error recovery, it can only happen when SS
847 // isn't empty
848 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000849 if (SS.isNotEmpty())
850 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
851 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000852 // Fake up a Declarator to use with ActOnTypeName.
853 DeclSpec DS(AttrFactory);
854
David Blaikieb5777572011-12-08 04:53:15 +0000855 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000856
857 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
858 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
859 }
860
Douglas Gregor7f43d672009-02-25 23:52:28 +0000861 // Check whether we have a template-id that names a type.
862 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000863 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000864 if (TemplateId->Kind == TNK_Type_template ||
865 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000866 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000867
868 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000869 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000870 EndLocation = Tok.getAnnotationEndLoc();
871 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000872
873 if (Type)
874 return Type;
875 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000876 }
877
878 // Fall through to produce an error below.
879 }
880
Douglas Gregor42a552f2008-11-05 20:51:48 +0000881 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000882 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000883 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000884 }
885
Douglas Gregor84d0a192010-01-12 21:28:44 +0000886 IdentifierInfo *Id = Tok.getIdentifierInfo();
887 SourceLocation IdLoc = ConsumeToken();
888
889 if (Tok.is(tok::less)) {
890 // It looks the user intended to write a template-id here, but the
891 // template-name was wrong. Try to fix that.
892 TemplateNameKind TNK = TNK_Type_template;
893 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000894 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000895 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000896 Diag(IdLoc, diag::err_unknown_template_name)
897 << Id;
898 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000899
Douglas Gregor84d0a192010-01-12 21:28:44 +0000900 if (!Template)
901 return true;
902
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000903 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000904 UnqualifiedId TemplateName;
905 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000906
Douglas Gregor84d0a192010-01-12 21:28:44 +0000907 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000908 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
909 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000910 return true;
911 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000912 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000913
Douglas Gregor84d0a192010-01-12 21:28:44 +0000914 // If we didn't end up with a typename token, there's nothing more we
915 // can do.
916 if (Tok.isNot(tok::annot_typename))
917 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000918
Douglas Gregor84d0a192010-01-12 21:28:44 +0000919 // Retrieve the type from the annotation token, consume that token, and
920 // return.
921 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000922 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000923 ConsumeToken();
924 return Type;
925 }
926
Douglas Gregor42a552f2008-11-05 20:51:48 +0000927 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000928 IdentifierInfo *CorrectedII = 0;
Douglas Gregor059101f2011-03-02 00:47:37 +0000929 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000930 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000931 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000932 /*NonTrivialTypeSourceInfo=*/true,
933 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000934 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000935 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000936 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000937 }
938
939 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000940 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000941
942 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000943 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000944 DS.SetRangeStart(IdLoc);
945 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000946 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000947
948 const char *PrevSpec = 0;
949 unsigned DiagID;
950 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
951
952 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
953 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000954}
955
John McCallc052dbb2012-05-22 21:28:12 +0000956void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
957 while (Tok.is(tok::kw___single_inheritance) ||
958 Tok.is(tok::kw___multiple_inheritance) ||
959 Tok.is(tok::kw___virtual_inheritance)) {
960 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
961 SourceLocation AttrNameLoc = ConsumeToken();
962 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000963 SourceLocation(), 0, 0, AttributeList::AS_GNU);
John McCallc052dbb2012-05-22 21:28:12 +0000964 }
965}
966
Richard Smithc9f35172012-06-25 21:37:02 +0000967/// Determine whether the following tokens are valid after a type-specifier
968/// which could be a standalone declaration. This will conservatively return
969/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith139be702012-07-02 19:14:01 +0000970bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smithc9f35172012-06-25 21:37:02 +0000971 // This switch enumerates the valid "follow" set for type-specifiers.
972 switch (Tok.getKind()) {
973 default: break;
974 case tok::semi: // struct foo {...} ;
975 case tok::star: // struct foo {...} * P;
976 case tok::amp: // struct foo {...} & R = ...
Richard Smithba65f502013-01-19 03:48:05 +0000977 case tok::ampamp: // struct foo {...} && R = ...
Richard Smithc9f35172012-06-25 21:37:02 +0000978 case tok::identifier: // struct foo {...} V ;
979 case tok::r_paren: //(struct foo {...} ) {4}
980 case tok::annot_cxxscope: // struct foo {...} a:: b;
981 case tok::annot_typename: // struct foo {...} a ::b;
982 case tok::annot_template_id: // struct foo {...} a<int> ::b;
983 case tok::l_paren: // struct foo {...} ( x);
984 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smithba65f502013-01-19 03:48:05 +0000985 case tok::kw_operator: // struct foo operator ++() {...}
Richard Smithc9f35172012-06-25 21:37:02 +0000986 return true;
Richard Smith139be702012-07-02 19:14:01 +0000987 case tok::colon:
988 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smithc9f35172012-06-25 21:37:02 +0000989 // Type qualifiers
990 case tok::kw_const: // struct foo {...} const x;
991 case tok::kw_volatile: // struct foo {...} volatile x;
992 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smithba65f502013-01-19 03:48:05 +0000993 // Function specifiers
994 // Note, no 'explicit'. An explicit function must be either a conversion
995 // operator or a constructor. Either way, it can't have a return type.
996 case tok::kw_inline: // struct foo inline f();
997 case tok::kw_virtual: // struct foo virtual f();
998 case tok::kw_friend: // struct foo friend f();
Richard Smithc9f35172012-06-25 21:37:02 +0000999 // Storage-class specifiers
1000 case tok::kw_static: // struct foo {...} static x;
1001 case tok::kw_extern: // struct foo {...} extern x;
1002 case tok::kw_typedef: // struct foo {...} typedef x;
1003 case tok::kw_register: // struct foo {...} register x;
1004 case tok::kw_auto: // struct foo {...} auto x;
1005 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smithba65f502013-01-19 03:48:05 +00001006 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smithc9f35172012-06-25 21:37:02 +00001007 case tok::kw_constexpr: // struct foo {...} constexpr x;
1008 // As shown above, type qualifiers and storage class specifiers absolutely
1009 // can occur after class specifiers according to the grammar. However,
1010 // almost no one actually writes code like this. If we see one of these,
1011 // it is much more likely that someone missed a semi colon and the
1012 // type/storage class specifier we're seeing is part of the *next*
1013 // intended declaration, as in:
1014 //
1015 // struct foo { ... }
1016 // typedef int X;
1017 //
1018 // We'd really like to emit a missing semicolon error instead of emitting
1019 // an error on the 'int' saying that you can't have two type specifiers in
1020 // the same declaration of X. Because of this, we look ahead past this
1021 // token to see if it's a type specifier. If so, we know the code is
1022 // otherwise invalid, so we can produce the expected semi error.
1023 if (!isKnownToBeTypeSpecifier(NextToken()))
1024 return true;
1025 break;
1026 case tok::r_brace: // struct bar { struct foo {...} }
1027 // Missing ';' at end of struct is accepted as an extension in C mode.
1028 if (!getLangOpts().CPlusPlus)
1029 return true;
1030 break;
Richard Smithba65f502013-01-19 03:48:05 +00001031 // C++11 attributes
1032 case tok::l_square: // enum E [[]] x
1033 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1034 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith8338a9d2013-01-29 04:13:32 +00001035 case tok::greater:
1036 // template<class T = class X>
1037 return getLangOpts().CPlusPlus;
Richard Smithc9f35172012-06-25 21:37:02 +00001038 }
1039 return false;
1040}
1041
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001042/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1043/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1044/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +00001045/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001046///
1047/// class-specifier: [C++ class]
1048/// class-head '{' member-specification[opt] '}'
1049/// class-head '{' member-specification[opt] '}' attributes[opt]
1050/// class-head:
1051/// class-key identifier[opt] base-clause[opt]
1052/// class-key nested-name-specifier identifier base-clause[opt]
1053/// class-key nested-name-specifier[opt] simple-template-id
1054/// base-clause[opt]
1055/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001056/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001057/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001058/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001059/// simple-template-id base-clause[opt]
1060/// class-key:
1061/// 'class'
1062/// 'struct'
1063/// 'union'
1064///
1065/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +00001066/// class-key ::[opt] nested-name-specifier[opt] identifier
1067/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1068/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001069///
1070/// Note that the C++ class-specifier and elaborated-type-specifier,
1071/// together, subsume the C99 struct-or-union-specifier:
1072///
1073/// struct-or-union-specifier: [C99 6.7.2.1]
1074/// struct-or-union identifier[opt] '{' struct-contents '}'
1075/// struct-or-union identifier
1076/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1077/// '}' attributes[opt]
1078/// [GNU] struct-or-union attributes[opt] identifier
1079/// struct-or-union:
1080/// 'struct'
1081/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +00001082void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1083 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001084 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001085 AccessSpecifier AS,
Michael Han2e397132012-11-26 22:54:45 +00001086 bool EnteringContext, DeclSpecContext DSC,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001087 ParsedAttributesWithRange &Attributes) {
Joao Matos17d35c32012-08-31 22:18:20 +00001088 DeclSpec::TST TagType;
1089 if (TagTokKind == tok::kw_struct)
1090 TagType = DeclSpec::TST_struct;
1091 else if (TagTokKind == tok::kw___interface)
1092 TagType = DeclSpec::TST_interface;
1093 else if (TagTokKind == tok::kw_class)
1094 TagType = DeclSpec::TST_class;
1095 else {
Chris Lattner4c97d762009-04-12 21:49:30 +00001096 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1097 TagType = DeclSpec::TST_union;
1098 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001099
Douglas Gregor374929f2009-09-18 15:37:17 +00001100 if (Tok.is(tok::code_completion)) {
1101 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001102 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001103 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00001104 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001105
Chandler Carruth926c4b42010-06-28 08:39:25 +00001106 // C++03 [temp.explicit] 14.7.2/8:
1107 // The usual access checking rules do not apply to names used to specify
1108 // explicit instantiations.
1109 //
1110 // As an extension we do not perform access checking on the names used to
1111 // specify explicit specializations either. This is important to allow
1112 // specializing traits classes for private types.
John McCall13489672012-05-07 06:16:58 +00001113 //
1114 // Note that we don't suppress if this turns out to be an elaborated
1115 // type specifier.
1116 bool shouldDelayDiagsInTag =
1117 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1118 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1119 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001120
Sean Hunt2edf0a22012-06-23 05:07:58 +00001121 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001122 // If attributes exist after tag, parse them.
1123 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +00001124 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001125
Steve Narofff59e17e2008-12-24 20:59:21 +00001126 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001127 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001128 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001129
John McCallc052dbb2012-05-22 21:28:12 +00001130 // Parse inheritance specifiers.
1131 if (Tok.is(tok::kw___single_inheritance) ||
1132 Tok.is(tok::kw___multiple_inheritance) ||
1133 Tok.is(tok::kw___virtual_inheritance))
1134 ParseMicrosoftInheritanceClassAttributes(attrs);
1135
Sean Huntbbd37c62009-11-21 08:43:09 +00001136 // If C++0x attributes exist here, parse them.
1137 // FIXME: Are we consistent with the ordering of parsing of different
1138 // styles of attributes?
Richard Smith4e24f0f2013-01-02 12:01:23 +00001139 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Michael Han07fc1ba2013-01-07 16:57:11 +00001141 // Source location used by FIXIT to insert misplaced
1142 // C++11 attributes
1143 SourceLocation AttrFixitLoc = Tok.getLocation();
1144
John Wiegley20c0da72011-04-27 23:09:49 +00001145 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +00001146 !Tok.is(tok::identifier) &&
1147 Tok.getIdentifierInfo() &&
1148 (Tok.is(tok::kw___is_arithmetic) ||
1149 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001150 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001151 Tok.is(tok::kw___is_floating_point) ||
1152 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001153 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001154 Tok.is(tok::kw___is_integral) ||
1155 Tok.is(tok::kw___is_member_function_pointer) ||
1156 Tok.is(tok::kw___is_member_pointer) ||
1157 Tok.is(tok::kw___is_pod) ||
1158 Tok.is(tok::kw___is_pointer) ||
1159 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001160 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001161 Tok.is(tok::kw___is_signed) ||
1162 Tok.is(tok::kw___is_unsigned) ||
1163 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001164 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001165 // name of struct templates, but some are keywords in GCC >= 4.3
1166 // and Clang. Therefore, when we see the token sequence "struct
1167 // X", make X into a normal identifier rather than a keyword, to
1168 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001169 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001170 Tok.setKind(tok::identifier);
1171 }
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001173 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001174 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001175 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001176 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1177 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001178
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001179 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001180 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001181 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001182 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1183 Diag(Tok, diag::err_expected_ident);
1184 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001185
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001186 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1187
Douglas Gregorcc636682009-02-17 23:15:12 +00001188 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001189 IdentifierInfo *Name = 0;
1190 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001191 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001192 if (Tok.is(tok::identifier)) {
1193 Name = Tok.getIdentifierInfo();
1194 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001195
David Blaikie4e4d0842012-03-11 07:00:24 +00001196 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001197 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001198 // Eat the template argument list and try to continue parsing this as
1199 // a class (or template thereof).
1200 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001201 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001202 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001203 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001204 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001205 // We couldn't parse the template argument list at all, so don't
1206 // try to give any location information for the list.
1207 LAngleLoc = RAngleLoc = SourceLocation();
1208 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001209
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001210 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matos17d35c32012-08-31 22:18:20 +00001211 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1212 << (TagType == DeclSpec::TST_class? 0
1213 : TagType == DeclSpec::TST_struct? 1
1214 : TagType == DeclSpec::TST_interface? 2
1215 : 3)
1216 << Name
1217 << SourceRange(LAngleLoc, RAngleLoc);
1218
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001219 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001220 // we've removed its template argument list.
1221 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1222 if (TemplateParams && TemplateParams->size() > 1) {
1223 TemplateParams->pop_back();
1224 } else {
1225 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001226 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001227 = ParsedTemplateInfo::NonTemplate;
1228 }
1229 } else if (TemplateInfo.Kind
1230 == ParsedTemplateInfo::ExplicitInstantiation) {
1231 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001232 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001233 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001234 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001235 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001236 = SourceLocation();
1237 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1238 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001239 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001240 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001241 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001242 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001243 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001244
Douglas Gregor059101f2011-03-02 00:47:37 +00001245 if (TemplateId->Kind != TNK_Type_template &&
1246 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001247 // The template-name in the simple-template-id refers to
1248 // something other than a class template. Give an appropriate
1249 // error message and skip to the ';'.
1250 SourceRange Range(NameLoc);
1251 if (SS.isNotEmpty())
1252 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001253
Douglas Gregor39a8de12009-02-25 19:37:18 +00001254 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1255 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Douglas Gregor39a8de12009-02-25 19:37:18 +00001257 DS.SetTypeSpecError();
1258 SkipUntil(tok::semi, false, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001259 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001260 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001261 }
1262
Richard Smith7796eb52012-03-12 08:56:40 +00001263 // There are four options here.
1264 // - If we are in a trailing return type, this is always just a reference,
1265 // and we must not try to parse a definition. For instance,
1266 // [] () -> struct S { };
1267 // does not define a type.
1268 // - If we have 'struct foo {...', 'struct foo :...',
1269 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1270 // - If we have 'struct foo;', then this is either a forward declaration
1271 // or a friend declaration, which have to be treated differently.
1272 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han2e397132012-11-26 22:54:45 +00001273 //
1274 // We also detect these erroneous cases to provide better diagnostic for
1275 // C++11 attributes parsing.
1276 // - attributes follow class name:
1277 // struct foo [[]] {};
1278 // - attributes appear before or after 'final':
1279 // struct foo [[]] final [[]] {};
1280 //
Richard Smith69730c12012-03-12 07:56:15 +00001281 // However, in type-specifier-seq's, things look like declarations but are
1282 // just references, e.g.
1283 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001284 // or
Richard Smith69730c12012-03-12 07:56:15 +00001285 // &T::operator struct s;
1286 // For these, DSC is DSC_type_specifier.
Michael Han2e397132012-11-26 22:54:45 +00001287
1288 // If there are attributes after class name, parse them.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001289 MaybeParseCXX11Attributes(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00001290
John McCallf312b1e2010-08-26 23:41:50 +00001291 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001292 if (DSC == DSC_trailing)
1293 TUK = Sema::TUK_Reference;
1294 else if (Tok.is(tok::l_brace) ||
1295 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001296 (isCXX11FinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001297 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001298 if (DS.isFriendSpecified()) {
1299 // C++ [class.friend]p2:
1300 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001301 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001302 << SourceRange(DS.getFriendSpecLoc());
1303
1304 // Skip everything up to the semicolon, so that this looks like a proper
1305 // friend class (or template thereof) declaration.
1306 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001307 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001308 } else {
1309 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001310 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001311 }
Richard Smith150d8532013-02-22 06:46:23 +00001312 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1313 NextToken().is(tok::kw_alignas))) {
Michael Han2e397132012-11-26 22:54:45 +00001314 // We can't tell if this is a definition or reference
1315 // until we skipped the 'final' and C++11 attribute specifiers.
1316 TentativeParsingAction PA(*this);
1317
1318 // Skip the 'final' keyword.
1319 ConsumeToken();
1320
1321 // Skip C++11 attribute specifiers.
1322 while (true) {
1323 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1324 ConsumeBracket();
1325 if (!SkipUntil(tok::r_square))
1326 break;
Richard Smith150d8532013-02-22 06:46:23 +00001327 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han2e397132012-11-26 22:54:45 +00001328 ConsumeToken();
1329 ConsumeParen();
1330 if (!SkipUntil(tok::r_paren))
1331 break;
1332 } else {
1333 break;
1334 }
1335 }
1336
1337 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1338 TUK = Sema::TUK_Definition;
1339 else
1340 TUK = Sema::TUK_Reference;
1341
1342 PA.Revert();
Richard Smithc9f35172012-06-25 21:37:02 +00001343 } else if (DSC != DSC_type_specifier &&
1344 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00001345 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallf312b1e2010-08-26 23:41:50 +00001346 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matos17d35c32012-08-31 22:18:20 +00001347 if (Tok.isNot(tok::semi)) {
1348 // A semicolon was missing after this declaration. Diagnose and recover.
1349 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1350 DeclSpec::getSpecifierName(TagType));
1351 PP.EnterToken(Tok);
1352 Tok.setKind(tok::semi);
1353 }
Richard Smithc9f35172012-06-25 21:37:02 +00001354 } else
John McCallf312b1e2010-08-26 23:41:50 +00001355 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001356
Michael Han2e397132012-11-26 22:54:45 +00001357 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1358 // to caller to handle.
Michael Han07fc1ba2013-01-07 16:57:11 +00001359 if (TUK != Sema::TUK_Reference) {
1360 // If this is not a reference, then the only possible
1361 // valid place for C++11 attributes to appear here
1362 // is between class-key and class-name. If there are
1363 // any attributes after class-name, we try a fixit to move
1364 // them to the right place.
1365 SourceRange AttrRange = Attributes.Range;
1366 if (AttrRange.isValid()) {
1367 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1368 << AttrRange
1369 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1370 CharSourceRange(AttrRange, true))
1371 << FixItHint::CreateRemoval(AttrRange);
1372
1373 // Recover by adding misplaced attributes to the attribute list
1374 // of the class so they can be applied on the class later.
1375 attrs.takeAllFrom(Attributes);
1376 }
1377 }
Michael Han2e397132012-11-26 22:54:45 +00001378
John McCall13489672012-05-07 06:16:58 +00001379 // If this is an elaborated type specifier, and we delayed
1380 // diagnostics before, just merge them into the current pool.
1381 if (shouldDelayDiagsInTag) {
1382 diagsFromTag.done();
1383 if (TUK == Sema::TUK_Reference)
1384 diagsFromTag.redelay();
1385 }
1386
John McCall207014e2010-07-30 06:26:29 +00001387 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001388 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001389 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1390 // We have a declaration or reference to an anonymous class.
1391 Diag(StartLoc, diag::err_anon_type_definition)
1392 << DeclSpec::getSpecifierName(TagType);
1393 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001394
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001395 SkipUntil(tok::comma, true);
1396 return;
1397 }
1398
Douglas Gregorddc29e12009-02-06 22:42:48 +00001399 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001400 DeclResult TagOrTempResult = true; // invalid
1401 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001402
Douglas Gregor402abb52009-05-28 23:31:59 +00001403 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001404 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001405 // Explicit specialization, class template partial specialization,
1406 // or explicit instantiation.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001407 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001408 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001409 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001410 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001411 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001412 ProhibitAttributes(attrs);
1413
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001414 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001415 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001416 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001417 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001418 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001419 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001420 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001421 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001422 TemplateId->TemplateNameLoc,
1423 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001424 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001425 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001426 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001427
1428 // Friend template-ids are treated as references unless
1429 // they have template headers, in which case they're ill-formed
1430 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1431 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001432 } else if (TUK == Sema::TUK_Reference ||
1433 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001434 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001435 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001436 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001437 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001438 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001439 TemplateId->Template,
1440 TemplateId->TemplateNameLoc,
1441 TemplateId->LAngleLoc,
1442 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001443 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001444 } else {
1445 // This is an explicit specialization or a class template
1446 // partial specialization.
1447 TemplateParameterLists FakedParamLists;
1448
1449 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1450 // This looks like an explicit instantiation, because we have
1451 // something like
1452 //
1453 // template class Foo<X>
1454 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001455 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001456 // meant to be an explicit specialization, but the user forgot
1457 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +00001458 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001459
Mike Stump1eb44332009-09-09 15:08:12 +00001460 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001461 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001462 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001463 diag::err_explicit_instantiation_with_definition)
1464 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +00001465 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001466
1467 // Create a fake template parameter list that contains only
1468 // "template<>", so that we treat this construct as a class
1469 // template specialization.
1470 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +00001471 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001472 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001473 LAngleLoc,
1474 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001475 LAngleLoc));
1476 TemplateParams = &FakedParamLists;
1477 }
1478
1479 // Build the class template specialization.
1480 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001481 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001482 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001483 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001484 TemplateId->TemplateNameLoc,
1485 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001486 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001487 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001488 attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001489 MultiTemplateParamsArg(
Douglas Gregorcc636682009-02-17 23:15:12 +00001490 TemplateParams? &(*TemplateParams)[0] : 0,
1491 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001492 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001493 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001494 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001495 // Explicit instantiation of a member of a class template
1496 // specialization, e.g.,
1497 //
1498 // template struct Outer<int>::Inner;
1499 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001500 ProhibitAttributes(attrs);
1501
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001502 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001503 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001504 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001505 TemplateInfo.TemplateLoc,
1506 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001507 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001508 } else if (TUK == Sema::TUK_Friend &&
1509 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001510 ProhibitAttributes(attrs);
1511
John McCall9a34edb2010-10-19 01:40:49 +00001512 TagOrTempResult =
1513 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1514 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001515 Name, NameLoc, attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001516 MultiTemplateParamsArg(
John McCall9a34edb2010-10-19 01:40:49 +00001517 TemplateParams? &(*TemplateParams)[0] : 0,
1518 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001519 } else {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001520 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1521 ProhibitAttributes(attrs);
1522
John McCallc4e70192009-09-11 04:59:25 +00001523 bool IsDependent = false;
1524
John McCalla25c4082010-10-19 18:40:57 +00001525 // Don't pass down template parameter lists if this is just a tag
1526 // reference. For example, we don't need the template parameters here:
1527 // template <class T> class A *makeA(T t);
1528 MultiTemplateParamsArg TParams;
1529 if (TUK != Sema::TUK_Reference && TemplateParams)
1530 TParams =
1531 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1532
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001533 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001534 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001535 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001536 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001537 TParams, Owned, IsDependent,
1538 SourceLocation(), false,
1539 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001540
1541 // If ActOnTag said the type was dependent, try again with the
1542 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001543 if (IsDependent) {
1544 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001545 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001546 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001547 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001548 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001549
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001550 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001551 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001552 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001553 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001554 isCXX11FinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001555 if (getLangOpts().CPlusPlus)
Michael Han07fc1ba2013-01-07 16:57:11 +00001556 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1557 TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001558 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001559 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001560 }
1561
John McCallb3d87482010-08-24 05:47:05 +00001562 const char *PrevSpec = 0;
1563 unsigned DiagID;
1564 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001565 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001566 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1567 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001568 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001569 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001570 Result = DS.SetTypeSpecType(TagType, StartLoc,
1571 NameLoc.isValid() ? NameLoc : StartLoc,
1572 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001573 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001574 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001575 return;
1576 }
Mike Stump1eb44332009-09-09 15:08:12 +00001577
John McCallb3d87482010-08-24 05:47:05 +00001578 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001579 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001580
Chris Lattner4ed5d912010-02-02 01:23:29 +00001581 // At this point, we've successfully parsed a class-specifier in 'definition'
1582 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1583 // going to look at what comes after it to improve error recovery. If an
1584 // impossible token occurs next, we assume that the programmer forgot a ; at
1585 // the end of the declaration and recover that way.
1586 //
Richard Smithc9f35172012-06-25 21:37:02 +00001587 // Also enforce C++ [temp]p3:
1588 // In a template-declaration which defines a class, no declarator
1589 // is permitted.
Joao Matos17d35c32012-08-31 22:18:20 +00001590 if (TUK == Sema::TUK_Definition &&
1591 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidis7d033b22012-12-17 20:10:43 +00001592 if (Tok.isNot(tok::semi)) {
1593 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1594 DeclSpec::getSpecifierName(TagType));
1595 // Push this token back into the preprocessor and change our current token
1596 // to ';' so that the rest of the code recovers as though there were an
1597 // ';' after the definition.
1598 PP.EnterToken(Tok);
1599 Tok.setKind(tok::semi);
1600 }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001601 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001602}
1603
Mike Stump1eb44332009-09-09 15:08:12 +00001604/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001605///
1606/// base-clause : [C++ class.derived]
1607/// ':' base-specifier-list
1608/// base-specifier-list:
1609/// base-specifier '...'[opt]
1610/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001611void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001612 assert(Tok.is(tok::colon) && "Not a base clause");
1613 ConsumeToken();
1614
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001615 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001616 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001617
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001618 while (true) {
1619 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001620 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001621 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001622 // Skip the rest of this base specifier, up until the comma or
1623 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001624 SkipUntil(tok::comma, tok::l_brace, true, true);
1625 } else {
1626 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001627 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001628 }
1629
1630 // If the next token is a comma, consume it and keep reading
1631 // base-specifiers.
1632 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001634 // Consume the comma.
1635 ConsumeToken();
1636 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001637
1638 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001639 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001640}
1641
1642/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1643/// one entry in the base class list of a class specifier, for example:
1644/// class foo : public bar, virtual private baz {
1645/// 'public bar' and 'virtual private baz' are each base-specifiers.
1646///
1647/// base-specifier: [C++ class.derived]
Richard Smith05321402013-02-19 23:47:15 +00001648/// attribute-specifier-seq[opt] base-type-specifier
1649/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1650/// base-type-specifier
1651/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1652/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001653Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001654 bool IsVirtual = false;
1655 SourceLocation StartLoc = Tok.getLocation();
1656
Richard Smith05321402013-02-19 23:47:15 +00001657 ParsedAttributesWithRange Attributes(AttrFactory);
1658 MaybeParseCXX11Attributes(Attributes);
1659
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001660 // Parse the 'virtual' keyword.
1661 if (Tok.is(tok::kw_virtual)) {
1662 ConsumeToken();
1663 IsVirtual = true;
1664 }
1665
Richard Smith05321402013-02-19 23:47:15 +00001666 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1667
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001668 // Parse an (optional) access specifier.
1669 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001670 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001671 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Richard Smith05321402013-02-19 23:47:15 +00001673 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1674
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001675 // Parse the 'virtual' keyword (again!), in case it came after the
1676 // access specifier.
1677 if (Tok.is(tok::kw_virtual)) {
1678 SourceLocation VirtualLoc = ConsumeToken();
1679 if (IsVirtual) {
1680 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001681 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001682 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001683 }
1684
1685 IsVirtual = true;
1686 }
1687
Richard Smith05321402013-02-19 23:47:15 +00001688 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1689
Douglas Gregor42a552f2008-11-05 20:51:48 +00001690 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001691 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001692 SourceLocation BaseLoc;
1693 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001694 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001695 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001697 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1698 // actually part of the base-specifier-list grammar productions, but we
1699 // parse it here for convenience.
1700 SourceLocation EllipsisLoc;
1701 if (Tok.is(tok::ellipsis))
1702 EllipsisLoc = ConsumeToken();
1703
Mike Stump1eb44332009-09-09 15:08:12 +00001704 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001705 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001707 // Notify semantic analysis that we have parsed a complete
1708 // base-specifier.
Richard Smith05321402013-02-19 23:47:15 +00001709 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1710 Access, BaseType.get(), BaseLoc,
1711 EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001712}
1713
1714/// getAccessSpecifierIfPresent - Determine whether the next token is
1715/// a C++ access-specifier.
1716///
1717/// access-specifier: [C++ class.derived]
1718/// 'private'
1719/// 'protected'
1720/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001721AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001722 switch (Tok.getKind()) {
1723 default: return AS_none;
1724 case tok::kw_private: return AS_private;
1725 case tok::kw_protected: return AS_protected;
1726 case tok::kw_public: return AS_public;
1727 }
1728}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001729
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001730/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001731/// delayed, e.g., default arguments, create a late-parsed method declaration
1732/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001733void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1734 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001735 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001736 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001737 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001738 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001739 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001740
Eli Friedmand33133c2009-07-22 21:45:50 +00001741 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1742 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1743 if (!LateMethod) {
1744 // Push this method onto the stack of late-parsed method
1745 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001746 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1747 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001748 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001749
1750 // Add all of the parameters prior to this one (they don't
1751 // have default arguments).
1752 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1753 for (unsigned I = 0; I < ParamIdx; ++I)
1754 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001755 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001756 }
1757
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001758 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001759 // not have a default argument).
1760 LateMethod->DefaultArgs.push_back(
1761 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1762 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1763 }
1764 }
1765}
1766
Richard Smith4e24f0f2013-01-02 12:01:23 +00001767/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001768/// virt-specifier.
1769///
1770/// virt-specifier:
1771/// override
1772/// final
Richard Smith4e24f0f2013-01-02 12:01:23 +00001773VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001774 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001775 return VirtSpecifiers::VS_None;
1776
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001777 if (Tok.is(tok::identifier)) {
1778 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001779
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001780 // Initialize the contextual keywords.
1781 if (!Ident_final) {
1782 Ident_final = &PP.getIdentifierTable().get("final");
1783 Ident_override = &PP.getIdentifierTable().get("override");
1784 }
1785
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001786 if (II == Ident_override)
1787 return VirtSpecifiers::VS_Override;
1788
1789 if (II == Ident_final)
1790 return VirtSpecifiers::VS_Final;
1791 }
1792
1793 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001794}
1795
Richard Smith4e24f0f2013-01-02 12:01:23 +00001796/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001797///
1798/// virt-specifier-seq:
1799/// virt-specifier
1800/// virt-specifier-seq virt-specifier
Richard Smith4e24f0f2013-01-02 12:01:23 +00001801void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalle402e722012-09-25 07:32:39 +00001802 bool IsInterface) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001803 while (true) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001804 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001805 if (Specifier == VirtSpecifiers::VS_None)
1806 return;
1807
1808 // C++ [class.mem]p8:
1809 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001810 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001811 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001812 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1813 << PrevSpec
1814 << FixItHint::CreateRemoval(Tok.getLocation());
1815
John McCalle402e722012-09-25 07:32:39 +00001816 if (IsInterface && Specifier == VirtSpecifiers::VS_Final) {
1817 Diag(Tok.getLocation(), diag::err_override_control_interface)
1818 << VirtSpecifiers::getSpecifierName(Specifier);
1819 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00001820 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00001821 diag::warn_cxx98_compat_override_control_keyword :
1822 diag::ext_override_control_keyword)
1823 << VirtSpecifiers::getSpecifierName(Specifier);
1824 }
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001825 ConsumeToken();
1826 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001827}
1828
Richard Smith4e24f0f2013-01-02 12:01:23 +00001829/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001830/// contextual 'final' keyword.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001831bool Parser::isCXX11FinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001832 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001833 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001834
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001835 if (!Tok.is(tok::identifier))
1836 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001837
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001838 // Initialize the contextual keywords.
1839 if (!Ident_final) {
1840 Ident_final = &PP.getIdentifierTable().get("final");
1841 Ident_override = &PP.getIdentifierTable().get("override");
1842 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001843
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001844 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001845}
1846
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001847/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1848///
1849/// member-declaration:
1850/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1851/// function-definition ';'[opt]
1852/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1853/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001854/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001855/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001856/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001857///
1858/// member-declarator-list:
1859/// member-declarator
1860/// member-declarator-list ',' member-declarator
1861///
1862/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001863/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001864/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001865/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001866/// identifier[opt] ':' constant-expression
1867///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001868/// virt-specifier-seq:
1869/// virt-specifier
1870/// virt-specifier-seq virt-specifier
1871///
1872/// virt-specifier:
1873/// override
1874/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001875///
Sebastian Redle2b68332009-04-12 17:16:29 +00001876/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001877/// '= 0'
1878///
1879/// constant-initializer:
1880/// '=' constant-expression
1881///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001882void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001883 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001884 const ParsedTemplateInfo &TemplateInfo,
1885 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001886 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001887 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001888 Diag(Tok, diag::err_at_defs_cxx);
1889 else
1890 Diag(Tok, diag::err_at_in_class);
1891
1892 ConsumeToken();
1893 SkipUntil(tok::r_brace);
1894 return;
1895 }
1896
John McCall60fa3cf2009-12-11 02:10:03 +00001897 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001898 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001899 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001900 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1901 if (TryAnnotateCXXScopeToken())
1902 MalformedTypeSpec = true;
1903
1904 bool isAccessDecl;
1905 if (Tok.isNot(tok::annot_cxxscope))
1906 isAccessDecl = false;
1907 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001908 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1909 else
1910 isAccessDecl = NextToken().is(tok::kw_operator);
1911
1912 if (isAccessDecl) {
1913 // Collect the scope specifier token we annotated earlier.
1914 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001915 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1916 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001917
1918 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001919 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001920 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001921 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1922 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001923 SkipUntil(tok::semi);
1924 return;
1925 }
1926
1927 // TODO: recover from mistakenly-qualified operator declarations.
1928 if (ExpectAndConsume(tok::semi,
1929 diag::err_expected_semi_after,
1930 "access declaration",
1931 tok::semi))
1932 return;
1933
Douglas Gregor23c94db2010-07-02 17:43:08 +00001934 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001935 false, SourceLocation(),
1936 SS, Name,
1937 /* AttrList */ 0,
1938 /* IsTypeName */ false,
1939 SourceLocation());
1940 return;
1941 }
1942 }
1943
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001944 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001945 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001946 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001947 SourceLocation DeclEnd;
1948 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001949 return;
1950 }
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Chris Lattner682bf922009-03-29 16:50:03 +00001952 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001953 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001954 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001955 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001956 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001957 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001958 return;
1959 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001960
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001961 // Handle: member-declaration ::= '__extension__' member-declaration
1962 if (Tok.is(tok::kw___extension__)) {
1963 // __extension__ silences extension warnings in the subexpression.
1964 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1965 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001966 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1967 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001968 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001969
Chris Lattner4ed5d912010-02-02 01:23:29 +00001970 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1971 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001972 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001973
John McCall0b7e6782011-03-24 11:26:52 +00001974 ParsedAttributesWithRange attrs(AttrFactory);
Michael Han52b501c2012-11-28 23:17:40 +00001975 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001976 // Optional C++11 attribute-specifier
1977 MaybeParseCXX11Attributes(attrs);
Michael Han52b501c2012-11-28 23:17:40 +00001978 // We need to keep these attributes for future diagnostic
1979 // before they are taken over by declaration specifier.
1980 FnAttrs.addAll(attrs.getList());
1981 FnAttrs.Range = attrs.Range;
1982
John McCall7f040a92010-12-24 02:08:15 +00001983 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001984
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001985 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00001986 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001987
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001988 // Eat 'using'.
1989 SourceLocation UsingLoc = ConsumeToken();
1990
1991 if (Tok.is(tok::kw_namespace)) {
1992 Diag(UsingLoc, diag::err_using_namespace_in_class);
1993 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001994 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001995 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001996 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00001997 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1998 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001999 }
2000 return;
2001 }
2002
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002003 // Hold late-parsed attributes so we can attach a Decl to them later.
2004 LateParsedAttrList CommonLateParsedAttrs;
2005
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002006 // decl-specifier-seq:
2007 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00002008 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00002009 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00002010 if (MalformedTypeSpec)
2011 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002012 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2013 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002014
Benjamin Kramer5354e772012-08-23 23:38:35 +00002015 MultiTemplateParamsArg TemplateParams(
John McCalldd4a3b02009-09-16 22:47:08 +00002016 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2017 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2018
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002019 if (Tok.is(tok::semi)) {
2020 ConsumeToken();
Michael Han52b501c2012-11-28 23:17:40 +00002021
2022 if (DS.isFriendSpecified())
2023 ProhibitAttributes(FnAttrs);
2024
John McCalld226f652010-08-21 09:40:31 +00002025 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00002026 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00002027 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00002028 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002029 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002030
John McCall54abf7d2009-11-04 02:18:39 +00002031 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00002032 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002033
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002034 // Hold late-parsed attributes so we can attach a Decl to them later.
2035 LateParsedAttrList LateParsedAttrs;
2036
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002037 SourceLocation EqualLoc;
2038 bool HasInitializer = false;
2039 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002040 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002041 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2042 ColonProtectionRAIIObject X(*this);
2043
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002044 // Parse the first declarator.
2045 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00002046 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00002047 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002048 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00002049 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002050 if (Tok.is(tok::semi))
2051 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002052 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002053 }
2054
Richard Smith4e24f0f2013-01-02 12:01:23 +00002055 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber48673472011-01-28 06:07:34 +00002056
John Thompson1b2fc0f2009-11-25 22:58:06 +00002057 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002058 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00002059
Francois Pichet6a247472011-05-11 02:14:46 +00002060 // MSVC permits pure specifier on inline functions declared at class scope.
2061 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00002062 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00002063 DeclaratorInfo.isFunctionDeclarator() &&
2064 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002065 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00002066 Init = ParseInitializer();
2067 if (Init.isInvalid())
2068 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002069 else
2070 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00002071 }
2072
Douglas Gregor45fa5602011-11-07 20:56:01 +00002073 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002074 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00002075 //
2076 // In C++11, a non-function declarator followed by an open brace is a
2077 // braced-init-list for an in-class member initialization, not an
2078 // erroneous function definition.
Richard Smith80ad52f2013-01-02 11:42:31 +00002079 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002080 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002081 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00002082 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002083 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002084 } else if (Tok.is(tok::equal)) {
2085 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00002086 if (KW.is(tok::kw_default))
2087 DefinitionKind = FDK_Defaulted;
2088 else if (KW.is(tok::kw_delete))
2089 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00002090 }
2091 }
2092
Michael Han52b501c2012-11-28 23:17:40 +00002093 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2094 // to a friend declaration, that declaration shall be a definition.
2095 if (DeclaratorInfo.isFunctionDeclarator() &&
2096 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2097 // Diagnose attributes that appear before decl specifier:
2098 // [[]] friend int foo();
2099 ProhibitAttributes(FnAttrs);
2100 }
2101
Douglas Gregor45fa5602011-11-07 20:56:01 +00002102 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002103 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002104 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002105 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00002106 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Michael Han52b501c2012-11-28 23:17:40 +00002107
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002108 // Consume the optional ';'
2109 if (Tok.is(tok::semi))
2110 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002111 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002112 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002113
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002114 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002115 Diag(DeclaratorInfo.getIdentifierLoc(),
2116 diag::err_function_declared_typedef);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002117
Richard Smith6f9a4452012-11-15 22:54:20 +00002118 // Recover by treating the 'typedef' as spurious.
2119 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002120 }
2121
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002122 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002123 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00002124 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002125
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002126 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2127 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2128 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002129 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002130 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002131 }
2132 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00002133
2134 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002135 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00002136 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002137
Chris Lattner682bf922009-03-29 16:50:03 +00002138 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002139 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002140 }
2141
2142 // member-declarator-list:
2143 // member-declarator
2144 // member-declarator-list ',' member-declarator
2145
Chris Lattner5f9e2722011-07-23 10:55:15 +00002146 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00002147 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00002148 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002149
2150 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002151 // member-declarator:
2152 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002153 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002154 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002155 if (Tok.is(tok::colon)) {
2156 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002157 BitfieldSize = ParseConstantExpression();
2158 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002159 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002160 }
Mike Stump1eb44332009-09-09 15:08:12 +00002161
Chris Lattnere6563252010-06-13 05:34:18 +00002162 // If a simple-asm-expr is present, parse it.
2163 if (Tok.is(tok::kw_asm)) {
2164 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002165 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002166 if (AsmLabel.isInvalid())
2167 SkipUntil(tok::comma, true, true);
2168
2169 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2170 DeclaratorInfo.SetRangeEnd(Loc);
2171 }
2172
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002173 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002174 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002175
Richard Smith7a614d82011-06-11 17:19:42 +00002176 // FIXME: When g++ adds support for this, we'll need to check whether it
2177 // goes before or after the GNU attributes and __asm__.
Richard Smith4e24f0f2013-01-02 12:01:23 +00002178 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith7a614d82011-06-11 17:19:42 +00002179
Richard Smithca523302012-06-10 03:12:00 +00002180 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002181 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002182 if (BitfieldSize.get()) {
2183 Diag(Tok, diag::err_bitfield_member_init);
2184 SkipUntil(tok::comma, true, true);
2185 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002186 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002187 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2188 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smithca523302012-06-10 03:12:00 +00002189 != DeclSpec::SCS_typedef)
2190 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002191 }
2192 }
2193
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002194 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002195 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002196 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002197
Rafael Espindolafc35cbc2013-01-08 20:44:06 +00002198 NamedDecl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002199 if (DS.isFriendSpecified()) {
Michael Han52b501c2012-11-28 23:17:40 +00002200 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2201 // to a friend declaration, that declaration shall be a definition.
2202 //
2203 // Diagnose attributes appear after friend member function declarator:
2204 // foo [[]] ();
2205 SmallVector<SourceRange, 4> Ranges;
2206 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2207 if (!Ranges.empty()) {
2208 for (SmallVector<SourceRange, 4>::iterator I = Ranges.begin(),
2209 E = Ranges.end(); I != E; ++I) {
2210 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2211 << *I;
2212 }
2213 }
2214
John McCallbbbcdd92009-09-11 21:02:39 +00002215 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002216 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002217 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002218 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002219 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002220 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002221 TemplateParams,
John McCall67d1a672009-08-06 02:15:43 +00002222 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002223 VS, HasInClassInit);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002224 if (AccessAttrs)
2225 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2226 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002227 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002228
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002229 // Set the Decl for any late parsed attributes
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002230 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2231 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2232 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002233 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002234 LateParsedAttrs[i]->addDecl(ThisDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002235 }
2236 LateParsedAttrs.clear();
2237
Douglas Gregor147545d2011-10-10 14:49:18 +00002238 // Handle the initializer.
David Blaikie1d87fba2013-01-30 01:22:18 +00002239 if (HasInClassInit != ICIS_NoInit &&
2240 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2241 DeclSpec::SCS_static) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002242 // The initializer was deferred; parse it and cache the tokens.
Richard Smith80ad52f2013-01-02 11:42:31 +00002243 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00002244 diag::warn_cxx98_compat_nonstatic_member_init :
2245 diag::ext_nonstatic_member_init);
2246
Richard Smith7a614d82011-06-11 17:19:42 +00002247 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002248 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2249 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002250 //
2251 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002252 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002253 Diag(Tok, diag::err_incomplete_array_member_init);
2254 SkipUntil(tok::comma, true, true);
David Blaikie3164c142012-02-14 09:00:46 +00002255 if (ThisDecl)
2256 // Avoid later warnings about a class member of incomplete type.
2257 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002258 } else
2259 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002260 } else if (HasInitializer) {
2261 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002262 if (!Init.isUsable())
Douglas Gregor552e2992012-02-21 02:22:07 +00002263 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002264 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2265
Douglas Gregor147545d2011-10-10 14:49:18 +00002266 if (Init.isInvalid())
2267 SkipUntil(tok::comma, true, true);
2268 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002269 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002270 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor147545d2011-10-10 14:49:18 +00002271 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2272 // No initializer.
2273 Actions.ActOnUninitializedDecl(ThisDecl,
2274 DS.getTypeSpecType() == DeclSpec::TST_auto);
Richard Smith7a614d82011-06-11 17:19:42 +00002275 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002276
2277 if (ThisDecl) {
2278 Actions.FinalizeDeclaration(ThisDecl);
2279 DeclsInGroup.push_back(ThisDecl);
2280 }
2281
Richard Smithe5310012012-04-29 07:31:09 +00002282 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor147545d2011-10-10 14:49:18 +00002283 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2284 != DeclSpec::SCS_typedef) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002285 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002286 }
2287
2288 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002289
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002290 // If we don't have a comma, it is either the end of the list (a ';')
2291 // or an error, bail out.
2292 if (Tok.isNot(tok::comma))
2293 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002294
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002295 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002296 SourceLocation CommaLoc = ConsumeToken();
2297
2298 if (Tok.isAtStartOfLine() &&
2299 !MightBeDeclarator(Declarator::MemberContext)) {
2300 // This comma was followed by a line-break and something which can't be
2301 // the start of a declarator. The comma was probably a typo for a
2302 // semicolon.
2303 Diag(CommaLoc, diag::err_expected_semi_declaration)
2304 << FixItHint::CreateReplacement(CommaLoc, ";");
2305 ExpectSemi = false;
2306 break;
2307 }
Mike Stump1eb44332009-09-09 15:08:12 +00002308
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002309 // Parse the next declarator.
2310 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002311 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002312 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002313 Init = true;
2314 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002315 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002316
Bill Wendlingad017fa2012-12-20 19:22:21 +00002317 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002318 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002319
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002320 if (Tok.isNot(tok::colon))
2321 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002322 }
2323
Richard Smith1c94c162012-01-09 22:31:44 +00002324 if (ExpectSemi &&
2325 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002326 // Skip to end of block or statement.
2327 SkipUntil(tok::r_brace, true, true);
2328 // If we stopped at a ';', eat it.
2329 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002330 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002331 }
2332
Douglas Gregor23c94db2010-07-02 17:43:08 +00002333 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00002334 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002335}
2336
Richard Smith7a614d82011-06-11 17:19:42 +00002337/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2338/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2339/// function definition. The location of the '=', if any, will be placed in
2340/// EqualLoc.
2341///
2342/// pure-specifier:
2343/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002344///
Richard Smith7a614d82011-06-11 17:19:42 +00002345/// brace-or-equal-initializer:
2346/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002347/// braced-init-list
2348///
Richard Smith7a614d82011-06-11 17:19:42 +00002349/// initializer-clause:
2350/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002351/// braced-init-list
2352///
Richard Smith7a614d82011-06-11 17:19:42 +00002353/// defaulted/deleted function-definition:
2354/// '=' 'default'
2355/// '=' 'delete'
2356///
2357/// Prior to C++0x, the assignment-expression in an initializer-clause must
2358/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002359ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002360 SourceLocation &EqualLoc) {
2361 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2362 && "Data member initializer not starting with '=' or '{'");
2363
Douglas Gregor552e2992012-02-21 02:22:07 +00002364 EnterExpressionEvaluationContext Context(Actions,
2365 Sema::PotentiallyEvaluated,
2366 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002367 if (Tok.is(tok::equal)) {
2368 EqualLoc = ConsumeToken();
2369 if (Tok.is(tok::kw_delete)) {
2370 // In principle, an initializer of '= delete p;' is legal, but it will
2371 // never type-check. It's better to diagnose it as an ill-formed expression
2372 // than as an ill-formed deleted non-function member.
2373 // An initializer of '= delete p, foo' will never be parsed, because
2374 // a top-level comma always ends the initializer expression.
2375 const Token &Next = NextToken();
2376 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2377 Next.is(tok::eof)) {
2378 if (IsFunction)
2379 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2380 << 1 /* delete */;
2381 else
2382 Diag(ConsumeToken(), diag::err_deleted_non_function);
2383 return ExprResult();
2384 }
2385 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002386 if (IsFunction)
2387 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2388 << 0 /* default */;
2389 else
2390 Diag(ConsumeToken(), diag::err_default_special_members);
2391 return ExprResult();
2392 }
2393
Sebastian Redl33deb352012-02-22 10:50:08 +00002394 }
2395 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002396}
2397
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002398/// ParseCXXMemberSpecification - Parse the class definition.
2399///
2400/// member-specification:
2401/// member-declaration member-specification[opt]
2402/// access-specifier ':' member-specification[opt]
2403///
Joao Matos17d35c32012-08-31 22:18:20 +00002404void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han07fc1ba2013-01-07 16:57:11 +00002405 SourceLocation AttrFixitLoc,
Richard Smith05321402013-02-19 23:47:15 +00002406 ParsedAttributesWithRange &Attrs,
Joao Matos17d35c32012-08-31 22:18:20 +00002407 unsigned TagType, Decl *TagDecl) {
2408 assert((TagType == DeclSpec::TST_struct ||
2409 TagType == DeclSpec::TST_interface ||
2410 TagType == DeclSpec::TST_union ||
2411 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2412
John McCallf312b1e2010-08-26 23:41:50 +00002413 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2414 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002415
Douglas Gregor26997fd2010-01-16 20:52:59 +00002416 // Determine whether this is a non-nested class. Note that local
2417 // classes are *not* considered to be nested classes.
2418 bool NonNestedClass = true;
2419 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002420 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002421 if (S->isClassScope()) {
2422 // We're inside a class scope, so this is a nested class.
2423 NonNestedClass = false;
John McCalle402e722012-09-25 07:32:39 +00002424
2425 // The Microsoft extension __interface does not permit nested classes.
2426 if (getCurrentClass().IsInterface) {
2427 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2428 << /*ErrorType=*/6
2429 << (isa<NamedDecl>(TagDecl)
2430 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2431 : "<anonymous>");
2432 }
Douglas Gregor26997fd2010-01-16 20:52:59 +00002433 break;
2434 }
2435
2436 if ((S->getFlags() & Scope::FnScope)) {
2437 // If we're in a function or function template declared in the
2438 // body of a class, then this is a local class rather than a
2439 // nested class.
2440 const Scope *Parent = S->getParent();
2441 if (Parent->isTemplateParamScope())
2442 Parent = Parent->getParent();
2443 if (Parent->isClassScope())
2444 break;
2445 }
2446 }
2447 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002448
2449 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002450 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002451
Douglas Gregor6569d682009-05-27 23:11:45 +00002452 // Note that we are parsing a new (potentially-nested) class definition.
John McCalle402e722012-09-25 07:32:39 +00002453 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2454 TagType == DeclSpec::TST_interface);
Douglas Gregor6569d682009-05-27 23:11:45 +00002455
Douglas Gregorddc29e12009-02-06 22:42:48 +00002456 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002457 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002458
Anders Carlssonb184a182011-03-25 14:46:08 +00002459 SourceLocation FinalLoc;
2460
2461 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002462 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00002463 assert(isCXX11FinalKeyword() && "not a class definition");
Richard Smith8b11b5e2011-10-15 04:21:46 +00002464 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002465
John McCalle402e722012-09-25 07:32:39 +00002466 if (TagType == DeclSpec::TST_interface) {
2467 Diag(FinalLoc, diag::err_override_control_interface)
2468 << "final";
2469 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00002470 Diag(FinalLoc, getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00002471 diag::warn_cxx98_compat_override_control_keyword :
2472 diag::ext_override_control_keyword) << "final";
2473 }
Michael Han2e397132012-11-26 22:54:45 +00002474
Michael Han07fc1ba2013-01-07 16:57:11 +00002475 // Parse any C++11 attributes after 'final' keyword.
2476 // These attributes are not allowed to appear here,
2477 // and the only possible place for them to appertain
2478 // to the class would be between class-key and class-name.
Richard Smith05321402013-02-19 23:47:15 +00002479 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonb184a182011-03-25 14:46:08 +00002480 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002481
John McCallbd0dfa52009-12-19 21:48:58 +00002482 if (Tok.is(tok::colon)) {
2483 ParseBaseClause(TagDecl);
2484
2485 if (!Tok.is(tok::l_brace)) {
2486 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002487
2488 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002489 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002490 return;
2491 }
2492 }
2493
2494 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002495 BalancedDelimiterTracker T(*this, tok::l_brace);
2496 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002497
John McCall42a4f662010-05-28 08:11:17 +00002498 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002499 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002500 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002501
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002502 // C++ 11p3: Members of a class defined with the keyword class are private
2503 // by default. Members of a class defined with the keywords struct or union
2504 // are public by default.
2505 AccessSpecifier CurAS;
2506 if (TagType == DeclSpec::TST_class)
2507 CurAS = AS_private;
2508 else
2509 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002510 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002511
Douglas Gregor07976d22010-06-21 22:31:09 +00002512 if (TagDecl) {
2513 // While we still have something to read, read the member-declarations.
2514 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2515 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002516
David Blaikie4e4d0842012-03-11 07:00:24 +00002517 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002518 Tok.is(tok::kw___if_not_exists))) {
2519 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2520 continue;
2521 }
2522
Douglas Gregor07976d22010-06-21 22:31:09 +00002523 // Check for extraneous top-level semicolon.
2524 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002525 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002526 continue;
2527 }
2528
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002529 if (Tok.is(tok::annot_pragma_vis)) {
2530 HandlePragmaVisibility();
2531 continue;
2532 }
2533
2534 if (Tok.is(tok::annot_pragma_pack)) {
2535 HandlePragmaPack();
2536 continue;
2537 }
2538
Argyrios Kyrtzidisf4deaef2012-10-12 17:39:59 +00002539 if (Tok.is(tok::annot_pragma_align)) {
2540 HandlePragmaAlign();
2541 continue;
2542 }
2543
Alexey Bataevc6400582013-03-22 06:34:35 +00002544 if (Tok.is(tok::annot_pragma_openmp)) {
2545 ParseOpenMPDeclarativeDirective();
2546 continue;
2547 }
2548
Douglas Gregor07976d22010-06-21 22:31:09 +00002549 AccessSpecifier AS = getAccessSpecifierIfPresent();
2550 if (AS != AS_none) {
2551 // Current token is a C++ access specifier.
2552 CurAS = AS;
2553 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002554 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002555 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002556 AccessAttrs.clear();
2557 MaybeParseGNUAttributes(AccessAttrs);
2558
David Blaikie13f8daf2011-10-13 06:08:43 +00002559 SourceLocation EndLoc;
2560 if (Tok.is(tok::colon)) {
2561 EndLoc = Tok.getLocation();
2562 ConsumeToken();
2563 } else if (Tok.is(tok::semi)) {
2564 EndLoc = Tok.getLocation();
2565 ConsumeToken();
2566 Diag(EndLoc, diag::err_expected_colon)
2567 << FixItHint::CreateReplacement(EndLoc, ":");
2568 } else {
2569 EndLoc = ASLoc.getLocWithOffset(TokLength);
2570 Diag(EndLoc, diag::err_expected_colon)
2571 << FixItHint::CreateInsertion(EndLoc, ":");
2572 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002573
John McCalle402e722012-09-25 07:32:39 +00002574 // The Microsoft extension __interface does not permit non-public
2575 // access specifiers.
2576 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2577 Diag(ASLoc, diag::err_access_specifier_interface)
2578 << (CurAS == AS_protected);
2579 }
2580
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002581 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2582 AccessAttrs.getList())) {
2583 // found another attribute than only annotations
2584 AccessAttrs.clear();
2585 }
2586
Douglas Gregor07976d22010-06-21 22:31:09 +00002587 continue;
2588 }
2589
2590 // FIXME: Make sure we don't have a template here.
2591
2592 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002593 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002594 }
2595
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002596 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002597 } else {
2598 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002599 }
Mike Stump1eb44332009-09-09 15:08:12 +00002600
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002601 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002602 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002603 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002604
John McCall42a4f662010-05-28 08:11:17 +00002605 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002606 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002607 T.getOpenLocation(),
2608 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002609 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002610
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002611 // C++11 [class.mem]p2:
2612 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002613 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002614 // brace-or-equal-initializers for non-static data members (including such
2615 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002616 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002617 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002618 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002619 // declarations and the lexed inline method definitions, along with any
2620 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002621 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002622 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002623 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002624
2625 // We've finished with all pending member declarations.
2626 Actions.ActOnFinishCXXMemberDecls();
2627
Richard Smith7a614d82011-06-11 17:19:42 +00002628 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002629 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002630 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002631 }
2632
John McCall42a4f662010-05-28 08:11:17 +00002633 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002634 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2635 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002636
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002637 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002638 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002639 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002640}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002641
2642/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2643/// which explicitly initializes the members or base classes of a
2644/// class (C++ [class.base.init]). For example, the three initializers
2645/// after the ':' in the Derived constructor below:
2646///
2647/// @code
2648/// class Base { };
2649/// class Derived : Base {
2650/// int x;
2651/// float f;
2652/// public:
2653/// Derived(float f) : Base(), x(17), f(f) { }
2654/// };
2655/// @endcode
2656///
Mike Stump1eb44332009-09-09 15:08:12 +00002657/// [C++] ctor-initializer:
2658/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002659///
Mike Stump1eb44332009-09-09 15:08:12 +00002660/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002661/// mem-initializer ...[opt]
2662/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002663void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002664 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2665
John Wiegley28bbe4b2011-04-28 01:08:34 +00002666 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2667 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002668 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002669
Chris Lattner5f9e2722011-07-23 10:55:15 +00002670 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002671 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002672
Douglas Gregor7ad83902008-11-05 04:29:56 +00002673 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002674 if (Tok.is(tok::code_completion)) {
2675 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2676 MemInitializers.data(),
2677 MemInitializers.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002678 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002679 } else {
2680 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2681 if (!MemInit.isInvalid())
2682 MemInitializers.push_back(MemInit.get());
2683 else
2684 AnyErrors = true;
2685 }
2686
Douglas Gregor7ad83902008-11-05 04:29:56 +00002687 if (Tok.is(tok::comma))
2688 ConsumeToken();
2689 else if (Tok.is(tok::l_brace))
2690 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002691 // If the next token looks like a base or member initializer, assume that
2692 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002693 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2694 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2695 Diag(Loc, diag::err_ctor_init_missing_comma)
2696 << FixItHint::CreateInsertion(Loc, ", ");
2697 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002698 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002699 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002700 SkipUntil(tok::l_brace, true, true);
2701 break;
2702 }
2703 } while (true);
2704
David Blaikie93c86172013-01-17 05:26:25 +00002705 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002706 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002707}
2708
2709/// ParseMemInitializer - Parse a C++ member initializer, which is
2710/// part of a constructor initializer that explicitly initializes one
2711/// member or base class (C++ [class.base.init]). See
2712/// ParseConstructorInitializer for an example.
2713///
2714/// [C++] mem-initializer:
2715/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002716/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002717///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002718/// [C++] mem-initializer-id:
2719/// '::'[opt] nested-name-specifier[opt] class-name
2720/// identifier
John McCalld226f652010-08-21 09:40:31 +00002721Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002722 // parse '::'[opt] nested-name-specifier[opt]
2723 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002724 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002725 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002726 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002727 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002728 if (TemplateId->Kind == TNK_Type_template ||
2729 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002730 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002731 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002732 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002733 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002734 }
David Blaikief2116622012-01-24 06:03:59 +00002735 // Uses of decltype will already have been converted to annot_decltype by
2736 // ParseOptionalCXXScopeSpecifier at this point.
2737 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2738 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002739 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002740 return true;
2741 }
Mike Stump1eb44332009-09-09 15:08:12 +00002742
David Blaikief2116622012-01-24 06:03:59 +00002743 IdentifierInfo *II = 0;
2744 DeclSpec DS(AttrFactory);
2745 SourceLocation IdLoc = Tok.getLocation();
2746 if (Tok.is(tok::annot_decltype)) {
2747 // Get the decltype expression, if there is one.
2748 ParseDecltypeSpecifier(DS);
2749 } else {
2750 if (Tok.is(tok::identifier))
2751 // Get the identifier. This may be a member name or a class name,
2752 // but we'll let the semantic analysis determine which it is.
2753 II = Tok.getIdentifierInfo();
2754 ConsumeToken();
2755 }
2756
Douglas Gregor7ad83902008-11-05 04:29:56 +00002757
2758 // Parse the '('.
Richard Smith80ad52f2013-01-02 11:42:31 +00002759 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002760 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2761
Sebastian Redl6df65482011-09-24 17:48:25 +00002762 ExprResult InitList = ParseBraceInitializer();
2763 if (InitList.isInvalid())
2764 return true;
2765
2766 SourceLocation EllipsisLoc;
2767 if (Tok.is(tok::ellipsis))
2768 EllipsisLoc = ConsumeToken();
2769
2770 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002771 TemplateTypeTy, DS, IdLoc,
2772 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002773 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002774 BalancedDelimiterTracker T(*this, tok::l_paren);
2775 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002776
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002777 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002778 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002779 CommaLocsTy CommaLocs;
2780 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2781 SkipUntil(tok::r_paren);
2782 return true;
2783 }
2784
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002785 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002786
2787 SourceLocation EllipsisLoc;
2788 if (Tok.is(tok::ellipsis))
2789 EllipsisLoc = ConsumeToken();
2790
2791 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002792 TemplateTypeTy, DS, IdLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002793 T.getOpenLocation(), ArgExprs.data(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002794 ArgExprs.size(), T.getCloseLocation(),
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002795 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002796 }
2797
Richard Smith80ad52f2013-01-02 11:42:31 +00002798 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002799 : diag::err_expected_lparen);
2800 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002801}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002802
Sebastian Redl7acafd02011-03-05 14:45:16 +00002803/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002804///
Douglas Gregora4745612008-12-01 18:00:20 +00002805/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002806/// dynamic-exception-specification
2807/// noexcept-specification
2808///
2809/// noexcept-specification:
2810/// 'noexcept'
2811/// 'noexcept' '(' constant-expression ')'
2812ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002813Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002814 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002815 SmallVectorImpl<ParsedType> &DynamicExceptions,
2816 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002817 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002818 ExceptionSpecificationType Result = EST_None;
2819
2820 // See if there's a dynamic specification.
2821 if (Tok.is(tok::kw_throw)) {
2822 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2823 DynamicExceptions,
2824 DynamicExceptionRanges);
2825 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2826 "Produced different number of exception types and ranges.");
2827 }
2828
2829 // If there's no noexcept specification, we're done.
2830 if (Tok.isNot(tok::kw_noexcept))
2831 return Result;
2832
Richard Smith841804b2011-10-17 23:06:20 +00002833 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2834
Sebastian Redl7acafd02011-03-05 14:45:16 +00002835 // If we already had a dynamic specification, parse the noexcept for,
2836 // recovery, but emit a diagnostic and don't store the results.
2837 SourceRange NoexceptRange;
2838 ExceptionSpecificationType NoexceptType = EST_None;
2839
2840 SourceLocation KeywordLoc = ConsumeToken();
2841 if (Tok.is(tok::l_paren)) {
2842 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002843 BalancedDelimiterTracker T(*this, tok::l_paren);
2844 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002845 NoexceptType = EST_ComputedNoexcept;
2846 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002847 // The argument must be contextually convertible to bool. We use
2848 // ActOnBooleanCondition for this purpose.
2849 if (!NoexceptExpr.isInvalid())
2850 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2851 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002852 T.consumeClose();
2853 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002854 } else {
2855 // There is no argument.
2856 NoexceptType = EST_BasicNoexcept;
2857 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2858 }
2859
2860 if (Result == EST_None) {
2861 SpecificationRange = NoexceptRange;
2862 Result = NoexceptType;
2863
2864 // If there's a dynamic specification after a noexcept specification,
2865 // parse that and ignore the results.
2866 if (Tok.is(tok::kw_throw)) {
2867 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2868 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2869 DynamicExceptionRanges);
2870 }
2871 } else {
2872 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2873 }
2874
2875 return Result;
2876}
2877
2878/// ParseDynamicExceptionSpecification - Parse a C++
2879/// dynamic-exception-specification (C++ [except.spec]).
2880///
2881/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002882/// 'throw' '(' type-id-list [opt] ')'
2883/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002884///
Douglas Gregora4745612008-12-01 18:00:20 +00002885/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002886/// type-id ... [opt]
2887/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002888///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002889ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2890 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002891 SmallVectorImpl<ParsedType> &Exceptions,
2892 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002893 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002894
Sebastian Redl7acafd02011-03-05 14:45:16 +00002895 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002896 BalancedDelimiterTracker T(*this, tok::l_paren);
2897 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002898 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2899 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002900 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002901 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002902
Douglas Gregora4745612008-12-01 18:00:20 +00002903 // Parse throw(...), a Microsoft extension that means "this function
2904 // can throw anything".
2905 if (Tok.is(tok::ellipsis)) {
2906 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002907 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002908 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002909 T.consumeClose();
2910 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002911 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002912 }
2913
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002914 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002915 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002916 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002917 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002918
Douglas Gregora04426c2010-12-20 23:57:46 +00002919 if (Tok.is(tok::ellipsis)) {
2920 // C++0x [temp.variadic]p5:
2921 // - In a dynamic-exception-specification (15.4); the pattern is a
2922 // type-id.
2923 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002924 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002925 if (!Res.isInvalid())
2926 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2927 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002928
Sebastian Redlef65f062009-05-29 18:02:33 +00002929 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002930 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002931 Ranges.push_back(Range);
2932 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002933
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002934 if (Tok.is(tok::comma))
2935 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002936 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002937 break;
2938 }
2939
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002940 T.consumeClose();
2941 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002942 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002943}
Douglas Gregor6569d682009-05-27 23:11:45 +00002944
Douglas Gregordab60ad2010-10-01 18:44:50 +00002945/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2946/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002947TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002948 assert(Tok.is(tok::arrow) && "expected arrow");
2949
2950 ConsumeToken();
2951
Richard Smith7796eb52012-03-12 08:56:40 +00002952 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002953}
2954
Douglas Gregor6569d682009-05-27 23:11:45 +00002955/// \brief We have just started parsing the definition of a new class,
2956/// so push that class onto our stack of classes that is currently
2957/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002958Sema::ParsingClassState
John McCalle402e722012-09-25 07:32:39 +00002959Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
2960 bool IsInterface) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002961 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002962 "Nested class without outer class");
John McCalle402e722012-09-25 07:32:39 +00002963 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCalleee1d542011-02-14 07:13:47 +00002964 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002965}
2966
2967/// \brief Deallocate the given parsed class and all of its nested
2968/// classes.
2969void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002970 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2971 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002972 delete Class;
2973}
2974
2975/// \brief Pop the top class of the stack of classes that are
2976/// currently being parsed.
2977///
2978/// This routine should be called when we have finished parsing the
2979/// definition of a class, but have not yet popped the Scope
2980/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00002981void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002982 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002983
John McCalleee1d542011-02-14 07:13:47 +00002984 Actions.PopParsingClass(state);
2985
Douglas Gregor6569d682009-05-27 23:11:45 +00002986 ParsingClass *Victim = ClassStack.top();
2987 ClassStack.pop();
2988 if (Victim->TopLevelClass) {
2989 // Deallocate all of the nested classes of this class,
2990 // recursively: we don't need to keep any of this information.
2991 DeallocateParsedClasses(Victim);
2992 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002993 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002994 assert(!ClassStack.empty() && "Missing top-level class?");
2995
Douglas Gregord54eb442010-10-12 16:25:54 +00002996 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002997 // The victim is a nested class, but we will not need to perform
2998 // any processing after the definition of this class since it has
2999 // no members whose handling was delayed. Therefore, we can just
3000 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00003001 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00003002 return;
3003 }
3004
3005 // This nested class has some members that will need to be processed
3006 // after the top-level class is completely defined. Therefore, add
3007 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003008 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00003009 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00003010 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00003011}
Sean Huntbbd37c62009-11-21 08:43:09 +00003012
Richard Smithc56298d2012-04-10 03:25:07 +00003013/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3014///
3015/// \return the parsed identifier on success, and 0 if the next token is not an
3016/// attribute-token.
3017///
3018/// C++11 [dcl.attr.grammar]p3:
3019/// If a keyword or an alternative token that satisfies the syntactic
3020/// requirements of an identifier is contained in an attribute-token,
3021/// it is considered an identifier.
3022IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3023 switch (Tok.getKind()) {
3024 default:
3025 // Identifiers and keywords have identifier info attached.
3026 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3027 Loc = ConsumeToken();
3028 return II;
3029 }
3030 return 0;
3031
3032 case tok::ampamp: // 'and'
3033 case tok::pipe: // 'bitor'
3034 case tok::pipepipe: // 'or'
3035 case tok::caret: // 'xor'
3036 case tok::tilde: // 'compl'
3037 case tok::amp: // 'bitand'
3038 case tok::ampequal: // 'and_eq'
3039 case tok::pipeequal: // 'or_eq'
3040 case tok::caretequal: // 'xor_eq'
3041 case tok::exclaim: // 'not'
3042 case tok::exclaimequal: // 'not_eq'
3043 // Alternative tokens do not have identifier info, but their spelling
3044 // starts with an alphabetical character.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003045 SmallString<8> SpellingBuf;
Richard Smithc56298d2012-04-10 03:25:07 +00003046 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rose3f6f51e2013-02-08 22:30:41 +00003047 if (isLetter(Spelling[0])) {
Richard Smithc56298d2012-04-10 03:25:07 +00003048 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00003049 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00003050 }
3051 return 0;
3052 }
3053}
3054
Michael Han6880f492012-10-03 01:56:22 +00003055static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3056 IdentifierInfo *ScopeName) {
3057 switch (AttributeList::getKind(AttrName, ScopeName,
3058 AttributeList::AS_CXX11)) {
3059 case AttributeList::AT_CarriesDependency:
3060 case AttributeList::AT_FallThrough:
Richard Smithcd8ab512013-01-17 01:30:42 +00003061 case AttributeList::AT_CXX11NoReturn: {
Michael Han6880f492012-10-03 01:56:22 +00003062 return true;
3063 }
3064
3065 default:
3066 return false;
3067 }
3068}
3069
Richard Smithc56298d2012-04-10 03:25:07 +00003070/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003071/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00003072///
Richard Smith6ee326a2012-04-10 01:32:12 +00003073/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00003074/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003075/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00003076///
Richard Smith6ee326a2012-04-10 01:32:12 +00003077/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00003078/// attribute[opt]
3079/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00003080/// attribute '...'
3081/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00003082///
Richard Smith6ee326a2012-04-10 01:32:12 +00003083/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00003084/// attribute-token attribute-argument-clause[opt]
3085///
Richard Smith6ee326a2012-04-10 01:32:12 +00003086/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003087/// identifier
3088/// attribute-scoped-token
3089///
Richard Smith6ee326a2012-04-10 01:32:12 +00003090/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003091/// attribute-namespace '::' identifier
3092///
Richard Smith6ee326a2012-04-10 01:32:12 +00003093/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00003094/// identifier
3095///
Richard Smith6ee326a2012-04-10 01:32:12 +00003096/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00003097/// '(' balanced-token-seq ')'
3098///
Richard Smith6ee326a2012-04-10 01:32:12 +00003099/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00003100/// balanced-token
3101/// balanced-token-seq balanced-token
3102///
Richard Smith6ee326a2012-04-10 01:32:12 +00003103/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003104/// '(' balanced-token-seq ')'
3105/// '[' balanced-token-seq ']'
3106/// '{' balanced-token-seq '}'
3107/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00003108void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003109 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003110 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00003111 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003112 ParseAlignmentSpecifier(attrs, endLoc);
3113 return;
3114 }
3115
Sean Huntbbd37c62009-11-21 08:43:09 +00003116 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00003117 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00003118
Richard Smith41be6732011-10-14 20:48:27 +00003119 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3120
Sean Huntbbd37c62009-11-21 08:43:09 +00003121 ConsumeBracket();
3122 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003123
Richard Smithcd8ab512013-01-17 01:30:42 +00003124 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3125
Richard Smithc56298d2012-04-10 03:25:07 +00003126 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003127 // attribute not present
3128 if (Tok.is(tok::comma)) {
3129 ConsumeToken();
3130 continue;
3131 }
3132
Richard Smithc56298d2012-04-10 03:25:07 +00003133 SourceLocation ScopeLoc, AttrLoc;
3134 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3135
3136 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3137 if (!AttrName)
3138 // Break out to the "expected ']'" diagnostic.
3139 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003140
Sean Huntbbd37c62009-11-21 08:43:09 +00003141 // scoped attribute
3142 if (Tok.is(tok::coloncolon)) {
3143 ConsumeToken();
3144
Richard Smithc56298d2012-04-10 03:25:07 +00003145 ScopeName = AttrName;
3146 ScopeLoc = AttrLoc;
3147
3148 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3149 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003150 Diag(Tok.getLocation(), diag::err_expected_ident);
3151 SkipUntil(tok::r_square, tok::comma, true, true);
3152 continue;
3153 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003154 }
3155
Michael Han6880f492012-10-03 01:56:22 +00003156 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Sean Huntbbd37c62009-11-21 08:43:09 +00003157 bool AttrParsed = false;
Sean Huntbbd37c62009-11-21 08:43:09 +00003158
Richard Smithcd8ab512013-01-17 01:30:42 +00003159 if (StandardAttr &&
3160 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3161 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3162 << AttrName << SourceRange(SeenAttrs[AttrName]);
3163
Michael Han6880f492012-10-03 01:56:22 +00003164 // Parse attribute arguments
3165 if (Tok.is(tok::l_paren)) {
3166 if (ScopeName && ScopeName->getName() == "gnu") {
3167 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3168 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3169 AttrParsed = true;
3170 } else {
3171 if (StandardAttr)
3172 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3173 << AttrName->getName();
3174
3175 // FIXME: handle other formats of c++11 attribute arguments
3176 ConsumeParen();
3177 SkipUntil(tok::r_paren, false);
3178 }
3179 }
3180
3181 if (!AttrParsed)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00003182 attrs.addNew(AttrName,
3183 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3184 AttrLoc),
3185 ScopeName, ScopeLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00003186 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smith6ee326a2012-04-10 01:32:12 +00003187
Richard Smithc56298d2012-04-10 03:25:07 +00003188 if (Tok.is(tok::ellipsis)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003189 ConsumeToken();
Michael Han6880f492012-10-03 01:56:22 +00003190
3191 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3192 << AttrName->getName();
Richard Smithc56298d2012-04-10 03:25:07 +00003193 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003194 }
3195
3196 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3197 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003198 if (endLoc)
3199 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00003200 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3201 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003202}
Sean Huntbbd37c62009-11-21 08:43:09 +00003203
Sean Hunt2edf0a22012-06-23 05:07:58 +00003204/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003205///
3206/// attribute-specifier-seq:
3207/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00003208void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003209 SourceLocation *endLoc) {
Richard Smith672edb02013-02-22 09:15:49 +00003210 assert(getLangOpts().CPlusPlus11);
3211
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003212 SourceLocation StartLoc = Tok.getLocation(), Loc;
3213 if (!endLoc)
3214 endLoc = &Loc;
3215
Douglas Gregor8828ee72011-10-07 20:35:25 +00003216 do {
Richard Smithc56298d2012-04-10 03:25:07 +00003217 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003218 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003219
3220 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003221}
3222
Francois Pichet334d47e2010-10-11 12:59:39 +00003223/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3224///
3225/// [MS] ms-attribute:
3226/// '[' token-seq ']'
3227///
3228/// [MS] ms-attribute-seq:
3229/// ms-attribute[opt]
3230/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003231void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3232 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003233 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3234
3235 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003236 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003237 ConsumeBracket();
3238 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003239 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003240 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3241 }
3242}
Francois Pichet563a6452011-05-25 10:19:49 +00003243
3244void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3245 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003246 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003247 if (ParseMicrosoftIfExistsCondition(Result))
3248 return;
3249
Douglas Gregor3896fc52011-10-24 22:31:10 +00003250 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3251 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003252 Diag(Tok, diag::err_expected_lbrace);
3253 return;
3254 }
Francois Pichet563a6452011-05-25 10:19:49 +00003255
Douglas Gregor3896fc52011-10-24 22:31:10 +00003256 switch (Result.Behavior) {
3257 case IEB_Parse:
3258 // Parse the declarations below.
3259 break;
3260
3261 case IEB_Dependent:
3262 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3263 << Result.IsIfExists;
3264 // Fall through to skip.
3265
3266 case IEB_Skip:
3267 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003268 return;
3269 }
3270
Douglas Gregor3896fc52011-10-24 22:31:10 +00003271 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003272 // __if_exists, __if_not_exists can nest.
3273 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3274 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3275 continue;
3276 }
3277
3278 // Check for extraneous top-level semicolon.
3279 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003280 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003281 continue;
3282 }
3283
3284 AccessSpecifier AS = getAccessSpecifierIfPresent();
3285 if (AS != AS_none) {
3286 // Current token is a C++ access specifier.
3287 CurAS = AS;
3288 SourceLocation ASLoc = Tok.getLocation();
3289 ConsumeToken();
3290 if (Tok.is(tok::colon))
3291 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3292 else
3293 Diag(Tok, diag::err_expected_colon);
3294 ConsumeToken();
3295 continue;
3296 }
3297
3298 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003299 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003300 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003301
3302 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003303}