blob: aa2c0f512afc1c66b61616fe5df549d3c8441f3f [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor1b7f8982008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/OperatorKinds.h"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
John McCall19510852010-08-20 18:27:03 +000020#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Sema/Scope.h"
John McCalle402e722012-09-25 07:32:39 +000023#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000025using namespace clang;
26
27/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000028/// may either be a top level namespace or a block-level namespace alias. If
29/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000030///
31/// namespace-definition: [C++ 7.3: basic.namespace]
32/// named-namespace-definition
33/// unnamed-namespace-definition
34///
35/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000036/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000037///
38/// named-namespace-definition:
39/// original-namespace-definition
40/// extension-namespace-definition
41///
42/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000043/// 'inline'[opt] 'namespace' identifier attributes[opt]
44/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000045///
46/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000047/// 'inline'[opt] 'namespace' original-namespace-name
48/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000049///
Chris Lattner8f08cb72007-08-25 06:57:03 +000050/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
51/// 'namespace' identifier '=' qualified-namespace-specifier ';'
52///
John McCalld226f652010-08-21 09:40:31 +000053Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000054 SourceLocation &DeclEnd,
55 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000056 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000057 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +000058 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000059
Douglas Gregor49f40bd2009-09-18 19:03:04 +000060 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000061 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000062 cutOffParsing();
63 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +000064 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000065
Chris Lattner8f08cb72007-08-25 06:57:03 +000066 SourceLocation IdentLoc;
67 IdentifierInfo *Ident = 0;
Richard Trieuf858bd82011-05-26 20:11:09 +000068 std::vector<SourceLocation> ExtraIdentLoc;
69 std::vector<IdentifierInfo*> ExtraIdent;
70 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000071
72 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattner04d66662007-10-09 17:33:22 +000074 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000075 Ident = Tok.getIdentifierInfo();
76 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieuf858bd82011-05-26 20:11:09 +000077 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
78 ExtraNamespaceLoc.push_back(ConsumeToken());
79 ExtraIdent.push_back(Tok.getIdentifierInfo());
80 ExtraIdentLoc.push_back(ConsumeToken());
81 }
Chris Lattner8f08cb72007-08-25 06:57:03 +000082 }
Mike Stump1eb44332009-09-09 15:08:12 +000083
Chris Lattner8f08cb72007-08-25 06:57:03 +000084 // Read label attributes, if present.
John McCall0b7e6782011-03-24 11:26:52 +000085 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000086 if (Tok.is(tok::kw___attribute)) {
87 attrTok = Tok;
John McCall7f040a92010-12-24 02:08:15 +000088 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000089 }
Mike Stump1eb44332009-09-09 15:08:12 +000090
Douglas Gregor6a588dd2009-06-17 19:49:00 +000091 if (Tok.is(tok::equal)) {
Nico Webere1bb3292012-10-27 23:44:27 +000092 if (Ident == 0) {
93 Diag(Tok, diag::err_expected_ident);
94 // Skip to end of the definition and eat the ';'.
95 SkipUntil(tok::semi);
96 return 0;
97 }
John McCall7f040a92010-12-24 02:08:15 +000098 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +000099 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +0000100 if (InlineLoc.isValid())
101 Diag(InlineLoc, diag::err_inline_namespace_alias)
102 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000103 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000104 }
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Richard Trieuf858bd82011-05-26 20:11:09 +0000106
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000107 BalancedDelimiterTracker T(*this, tok::l_brace);
108 if (T.consumeOpen()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000109 if (!ExtraIdent.empty()) {
110 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
111 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
112 }
Mike Stump1eb44332009-09-09 15:08:12 +0000113 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +0000114 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +0000115 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Douglas Gregor23c94db2010-07-02 17:43:08 +0000118 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
119 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
120 getCurScope()->getFnParent()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000121 if (!ExtraIdent.empty()) {
122 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
123 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
124 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000125 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Douglas Gregor95f1b152010-05-14 05:08:22 +0000126 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000127 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000128 }
129
Richard Trieuf858bd82011-05-26 20:11:09 +0000130 if (!ExtraIdent.empty()) {
131 TentativeParsingAction TPA(*this);
132 SkipUntil(tok::r_brace, /*StopAtSemi*/false, /*DontConsume*/true);
133 Token rBraceToken = Tok;
134 TPA.Revert();
135
136 if (!rBraceToken.is(tok::r_brace)) {
137 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
138 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
139 } else {
Benjamin Kramer9910df02011-05-26 21:32:30 +0000140 std::string NamespaceFix;
Richard Trieuf858bd82011-05-26 20:11:09 +0000141 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
142 E = ExtraIdent.end(); I != E; ++I) {
143 NamespaceFix += " { namespace ";
144 NamespaceFix += (*I)->getName();
145 }
Benjamin Kramer9910df02011-05-26 21:32:30 +0000146
Richard Trieuf858bd82011-05-26 20:11:09 +0000147 std::string RBraces;
Benjamin Kramer9910df02011-05-26 21:32:30 +0000148 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieuf858bd82011-05-26 20:11:09 +0000149 RBraces += "} ";
Benjamin Kramer9910df02011-05-26 21:32:30 +0000150
Richard Trieuf858bd82011-05-26 20:11:09 +0000151 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
152 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
153 ExtraIdentLoc.back()),
154 NamespaceFix)
155 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
156 }
157 }
158
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000159 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith7fe62082011-10-15 05:09:34 +0000160 if (InlineLoc.isValid())
Richard Smith80ad52f2013-01-02 11:42:31 +0000161 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000162 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000163
Chris Lattner51448322009-03-29 14:02:43 +0000164 // Enter a scope for the namespace.
165 ParseScope NamespaceScope(this, Scope::DeclScope);
166
John McCalld226f652010-08-21 09:40:31 +0000167 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000168 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000169 IdentLoc, Ident, T.getOpenLocation(),
170 attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000171
John McCallf312b1e2010-08-26 23:41:50 +0000172 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
173 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Richard Trieuf858bd82011-05-26 20:11:09 +0000175 // Parse the contents of the namespace. This includes parsing recovery on
176 // any improperly nested namespaces.
177 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000178 InlineLoc, attrs, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattner51448322009-03-29 14:02:43 +0000180 // Leave the namespace scope.
181 NamespaceScope.Exit();
182
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000183 DeclEnd = T.getCloseLocation();
184 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner51448322009-03-29 14:02:43 +0000185
186 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000187}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000188
Richard Trieuf858bd82011-05-26 20:11:09 +0000189/// ParseInnerNamespace - Parse the contents of a namespace.
190void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
191 std::vector<IdentifierInfo*>& Ident,
192 std::vector<SourceLocation>& NamespaceLoc,
193 unsigned int index, SourceLocation& InlineLoc,
Richard Trieuf858bd82011-05-26 20:11:09 +0000194 ParsedAttributes& attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000195 BalancedDelimiterTracker &Tracker) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000196 if (index == Ident.size()) {
197 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
198 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000199 MaybeParseCXX11Attributes(attrs);
Richard Trieuf858bd82011-05-26 20:11:09 +0000200 MaybeParseMicrosoftAttributes(attrs);
201 ParseExternalDeclaration(attrs);
202 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000203
204 // The caller is what called check -- we are simply calling
205 // the close for it.
206 Tracker.consumeClose();
Richard Trieuf858bd82011-05-26 20:11:09 +0000207
208 return;
209 }
210
211 // Parse improperly nested namespaces.
212 ParseScope NamespaceScope(this, Scope::DeclScope);
213 Decl *NamespcDecl =
214 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
215 NamespaceLoc[index], IdentLoc[index],
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000216 Ident[index], Tracker.getOpenLocation(),
217 attrs.getList());
Richard Trieuf858bd82011-05-26 20:11:09 +0000218
219 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000220 attrs, Tracker);
Richard Trieuf858bd82011-05-26 20:11:09 +0000221
222 NamespaceScope.Exit();
223
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000224 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieuf858bd82011-05-26 20:11:09 +0000225}
226
Anders Carlssonf67606a2009-03-28 04:07:16 +0000227/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
228/// alias definition.
229///
John McCalld226f652010-08-21 09:40:31 +0000230Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000231 SourceLocation AliasLoc,
232 IdentifierInfo *Alias,
233 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000234 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Anders Carlssonf67606a2009-03-28 04:07:16 +0000236 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000238 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000239 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000240 cutOffParsing();
241 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000242 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000243
Anders Carlssonf67606a2009-03-28 04:07:16 +0000244 CXXScopeSpec SS;
245 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000246 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000247
248 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
249 Diag(Tok, diag::err_expected_namespace_name);
250 // Skip to end of the definition and eat the ';'.
251 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000252 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000253 }
254
255 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000256 IdentifierInfo *Ident = Tok.getIdentifierInfo();
257 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Anders Carlssonf67606a2009-03-28 04:07:16 +0000259 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000260 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000261 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
262 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Douglas Gregor23c94db2010-07-02 17:43:08 +0000264 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000265 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000266}
267
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000268/// ParseLinkage - We know that the current token is a string_literal
269/// and just before that, that extern was seen.
270///
271/// linkage-specification: [C++ 7.5p2: dcl.link]
272/// 'extern' string-literal '{' declaration-seq[opt] '}'
273/// 'extern' string-literal declaration
274///
Chris Lattner7d642712010-11-09 20:15:55 +0000275Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000276 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000277 SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000278 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000279 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000280 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000281 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000282
Richard Smith99831e42012-03-06 03:21:47 +0000283 // FIXME: This is incorrect: linkage-specifiers are parsed in translation
284 // phase 7, so string-literal concatenation is supposed to occur.
285 // extern "" "C" "" "+" "+" { } is legal.
286 if (Tok.hasUDSuffix())
287 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000288 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000289
Douglas Gregor074149e2009-01-05 19:45:36 +0000290 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000291 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000292 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000293 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000294 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000295 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000296 : SourceLocation());
297
John McCall0b7e6782011-03-24 11:26:52 +0000298 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000299 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000300 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000301
Douglas Gregor074149e2009-01-05 19:45:36 +0000302 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-05-01 16:25:54 +0000303 // Reset the source range in DS, as the leading "extern"
304 // does not really belong to the inner declaration ...
305 DS.SetRangeStart(SourceLocation());
306 DS.SetRangeEnd(SourceLocation());
307 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000308 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000309 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000310 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000311 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000312 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000313
Douglas Gregor63a01132010-02-07 08:38:28 +0000314 DS.abort();
315
John McCall7f040a92010-12-24 02:08:15 +0000316 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000317
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000318 BalancedDelimiterTracker T(*this, tok::l_brace);
319 T.consumeOpen();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000320 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000321 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000322 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000323 MaybeParseMicrosoftAttributes(attrs);
324 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000325 }
326
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000327 T.consumeClose();
Chris Lattner7d642712010-11-09 20:15:55 +0000328 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000329 T.getCloseLocation());
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000330}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000331
Douglas Gregorf780abc2008-12-30 03:27:21 +0000332/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
333/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000334Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000335 const ParsedTemplateInfo &TemplateInfo,
336 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000337 ParsedAttributesWithRange &attrs,
338 Decl **OwnedType) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000339 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000340 ObjCDeclContextSwitch ObjCDC(*this);
341
Douglas Gregorf780abc2008-12-30 03:27:21 +0000342 // Eat 'using'.
343 SourceLocation UsingLoc = ConsumeToken();
344
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000345 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000346 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000347 cutOffParsing();
348 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000349 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000350
John McCall78b81052010-11-10 02:40:36 +0000351 // 'using namespace' means this is a using-directive.
352 if (Tok.is(tok::kw_namespace)) {
353 // Template parameters are always an error here.
354 if (TemplateInfo.Kind) {
355 SourceRange R = TemplateInfo.getSourceRange();
356 Diag(UsingLoc, diag::err_templated_using_directive)
357 << R << FixItHint::CreateRemoval(R);
358 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000359
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000360 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000361 }
362
Richard Smith162e1c12011-04-15 14:24:37 +0000363 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000364
365 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000366 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000367
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000368 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000369 AS_none, OwnedType);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000370}
371
372/// ParseUsingDirective - Parse C++ using-directive, assumes
373/// that current token is 'namespace' and 'using' was already parsed.
374///
375/// using-directive: [C++ 7.3.p4: namespace.udir]
376/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
377/// namespace-name ;
378/// [GNU] using-directive:
379/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
380/// namespace-name attributes[opt] ;
381///
John McCalld226f652010-08-21 09:40:31 +0000382Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000383 SourceLocation UsingLoc,
384 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000385 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000386 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
387
388 // Eat 'namespace'.
389 SourceLocation NamespcLoc = ConsumeToken();
390
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000391 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000392 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000393 cutOffParsing();
394 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000395 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000396
Douglas Gregorf780abc2008-12-30 03:27:21 +0000397 CXXScopeSpec SS;
398 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000399 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000400
Douglas Gregorf780abc2008-12-30 03:27:21 +0000401 IdentifierInfo *NamespcName = 0;
402 SourceLocation IdentLoc = SourceLocation();
403
404 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000405 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000406 Diag(Tok, diag::err_expected_namespace_name);
407 // If there was invalid namespace name, skip to end of decl, and eat ';'.
408 SkipUntil(tok::semi);
409 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000410 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Chris Lattner823c44e2009-01-06 07:27:21 +0000413 // Parse identifier.
414 NamespcName = Tok.getIdentifierInfo();
415 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner823c44e2009-01-06 07:27:21 +0000417 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000418 bool GNUAttr = false;
419 if (Tok.is(tok::kw___attribute)) {
420 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000421 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattner823c44e2009-01-06 07:27:21 +0000424 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000425 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000426 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000427 GNUAttr ? diag::err_expected_semi_after_attribute_list
428 : diag::err_expected_semi_after_namespace_name,
429 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000430
Douglas Gregor23c94db2010-07-02 17:43:08 +0000431 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000432 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000433}
434
Richard Smith162e1c12011-04-15 14:24:37 +0000435/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
436/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000437///
438/// using-declaration: [C++ 7.3.p3: namespace.udecl]
439/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000440/// unqualified-id
441/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000442///
Richard Smithd03de6a2013-01-29 10:02:16 +0000443/// alias-declaration: C++11 [dcl.dcl]p1
444/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smith162e1c12011-04-15 14:24:37 +0000445///
John McCalld226f652010-08-21 09:40:31 +0000446Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000447 const ParsedTemplateInfo &TemplateInfo,
448 SourceLocation UsingLoc,
449 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000450 AccessSpecifier AS,
451 Decl **OwnedType) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000452 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000453 SourceLocation TypenameLoc;
Richard Smith6b3d3e52013-02-20 19:22:51 +0000454 bool IsTypeName = false;
455 ParsedAttributesWithRange Attrs(AttrFactory);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000456
457 // FIXME: Simply skip the attributes and diagnose, don't bother parsing them.
Richard Smith6b3d3e52013-02-20 19:22:51 +0000458 MaybeParseCXX11Attributes(Attrs);
459 ProhibitAttributes(Attrs);
460 Attrs.clear();
461 Attrs.Range = SourceRange();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000462
463 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000464 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000465 if (Tok.is(tok::kw_typename)) {
Richard Smith6b3d3e52013-02-20 19:22:51 +0000466 TypenameLoc = ConsumeToken();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000467 IsTypeName = true;
468 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000469
470 // Parse nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000471 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000472
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000473 // Check nested-name specifier.
474 if (SS.isInvalid()) {
475 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000476 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000477 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000478
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000479 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000480 // destructor names and allow the action module to diagnose any semantic
481 // errors.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000482 SourceLocation TemplateKWLoc;
Douglas Gregor12c118a2009-11-04 16:30:06 +0000483 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000484 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000485 /*EnteringContext=*/false,
486 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000487 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000488 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000489 TemplateKWLoc,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000490 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000491 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000492 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000493 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000494
Richard Smith6b3d3e52013-02-20 19:22:51 +0000495 MaybeParseCXX11Attributes(Attrs);
Richard Smith162e1c12011-04-15 14:24:37 +0000496
497 // Maybe this is an alias-declaration.
498 bool IsAliasDecl = Tok.is(tok::equal);
499 TypeResult TypeAlias;
500 if (IsAliasDecl) {
Richard Smith6b3d3e52013-02-20 19:22:51 +0000501 // TODO: Can GNU attributes appear here?
Richard Smith162e1c12011-04-15 14:24:37 +0000502 ConsumeToken();
503
Richard Smith80ad52f2013-01-02 11:42:31 +0000504 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000505 diag::warn_cxx98_compat_alias_declaration :
506 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000507
Richard Smith3e4c6c42011-05-05 21:57:07 +0000508 // Type alias templates cannot be specialized.
509 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000510 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
511 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000512 SpecKind = 0;
513 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
514 SpecKind = 1;
515 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
516 SpecKind = 2;
517 if (SpecKind != -1) {
518 SourceRange Range;
519 if (SpecKind == 0)
520 Range = SourceRange(Name.TemplateId->LAngleLoc,
521 Name.TemplateId->RAngleLoc);
522 else
523 Range = TemplateInfo.getSourceRange();
524 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
525 << SpecKind << Range;
526 SkipUntil(tok::semi);
527 return 0;
528 }
529
Richard Smith162e1c12011-04-15 14:24:37 +0000530 // Name must be an identifier.
531 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
532 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
533 // No removal fixit: can't recover from this.
534 SkipUntil(tok::semi);
535 return 0;
536 } else if (IsTypeName)
537 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
538 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
539 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
540 else if (SS.isNotEmpty())
541 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
542 << FixItHint::CreateRemoval(SS.getRange());
543
Richard Smith3e4c6c42011-05-05 21:57:07 +0000544 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
545 Declarator::AliasTemplateContext :
Richard Smith6b3d3e52013-02-20 19:22:51 +0000546 Declarator::AliasDeclContext, AS, OwnedType,
547 &Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000548 } else {
549 // C++11 attributes are not allowed on a using-declaration, but GNU ones
550 // are.
Richard Smith6b3d3e52013-02-20 19:22:51 +0000551 ProhibitAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000552
Richard Smith162e1c12011-04-15 14:24:37 +0000553 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith6b3d3e52013-02-20 19:22:51 +0000554 MaybeParseGNUAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000555 }
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000557 // Eat ';'.
558 DeclEnd = Tok.getLocation();
559 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000560 !Attrs.empty() ? "attributes list" :
Richard Smith162e1c12011-04-15 14:24:37 +0000561 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000562 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000563
John McCall78b81052010-11-10 02:40:36 +0000564 // Diagnose an attempt to declare a templated using-declaration.
Richard Smithd03de6a2013-01-29 10:02:16 +0000565 // In C++11, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000566 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000567 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000568 SourceRange R = TemplateInfo.getSourceRange();
569 Diag(UsingLoc, diag::err_templated_using_declaration)
570 << R << FixItHint::CreateRemoval(R);
571
572 // Unfortunately, we have to bail out instead of recovering by
573 // ignoring the parameters, just in case the nested name specifier
574 // depends on the parameters.
575 return 0;
576 }
577
Douglas Gregor480b53c2011-09-26 14:30:28 +0000578 // "typename" keyword is allowed for identifiers only,
579 // because it may be a type definition.
580 if (IsTypeName && Name.getKind() != UnqualifiedId::IK_Identifier) {
581 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
582 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
583 // Proceed parsing, but reset the IsTypeName flag.
584 IsTypeName = false;
585 }
586
Richard Smith3e4c6c42011-05-05 21:57:07 +0000587 if (IsAliasDecl) {
588 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramer5354e772012-08-23 23:38:35 +0000589 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3e4c6c42011-05-05 21:57:07 +0000590 TemplateParams ? TemplateParams->data() : 0,
591 TemplateParams ? TemplateParams->size() : 0);
592 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000593 UsingLoc, Name, Attrs.getList(),
594 TypeAlias);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000595 }
Richard Smith162e1c12011-04-15 14:24:37 +0000596
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000597 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000598 Name, Attrs.getList(),
John McCall7f040a92010-12-24 02:08:15 +0000599 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000600}
601
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000602/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000603///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000604/// [C++0x] static_assert-declaration:
605/// static_assert ( constant-expression , string-literal ) ;
606///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000607/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000608/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000609///
John McCalld226f652010-08-21 09:40:31 +0000610Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000611 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
612 "Not a static_assert declaration");
613
David Blaikie4e4d0842012-03-11 07:00:24 +0000614 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000615 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000616 if (Tok.is(tok::kw_static_assert))
617 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000618
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000619 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000621 BalancedDelimiterTracker T(*this, tok::l_paren);
622 if (T.consumeOpen()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000623 Diag(Tok, diag::err_expected_lparen);
Richard Smith3686c712012-09-13 19:12:50 +0000624 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000625 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000626 }
Mike Stump1eb44332009-09-09 15:08:12 +0000627
John McCall60d7b3a2010-08-24 06:29:42 +0000628 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000629 if (AssertExpr.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000630 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000631 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Anders Carlssonad5f9602009-03-13 23:29:20 +0000634 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000635 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000636
Richard Smith0cc323c2012-03-05 23:20:05 +0000637 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000638 Diag(Tok, diag::err_expected_string_literal)
639 << /*Source='static_assert'*/1;
Richard Smith3686c712012-09-13 19:12:50 +0000640 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000641 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
John McCall60d7b3a2010-08-24 06:29:42 +0000644 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smith99831e42012-03-06 03:21:47 +0000645 if (AssertMessage.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000646 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000647 return 0;
Richard Smith99831e42012-03-06 03:21:47 +0000648 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000649
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000650 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Chris Lattner97144fc2009-04-02 04:16:50 +0000652 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000653 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000654
John McCall9ae2f072010-08-23 23:25:46 +0000655 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
656 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000657 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000658 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000659}
660
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000661/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
662///
663/// 'decltype' ( expression )
664///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000665SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
666 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
667 && "Not a decltype specifier");
668
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000669
David Blaikie42d6d0c2011-12-04 05:04:18 +0000670 ExprResult Result;
671 SourceLocation StartLoc = Tok.getLocation();
672 SourceLocation EndLoc;
673
674 if (Tok.is(tok::annot_decltype)) {
675 Result = getExprAnnotation(Tok);
676 EndLoc = Tok.getAnnotationEndLoc();
677 ConsumeToken();
678 if (Result.isInvalid()) {
679 DS.SetTypeSpecError();
680 return EndLoc;
681 }
682 } else {
Richard Smithc7b55432012-02-24 22:30:04 +0000683 if (Tok.getIdentifierInfo()->isStr("decltype"))
684 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000685
David Blaikie42d6d0c2011-12-04 05:04:18 +0000686 ConsumeToken();
687
688 BalancedDelimiterTracker T(*this, tok::l_paren);
689 if (T.expectAndConsume(diag::err_expected_lparen_after,
690 "decltype", tok::r_paren)) {
691 DS.SetTypeSpecError();
692 return T.getOpenLocation() == Tok.getLocation() ?
693 StartLoc : T.getOpenLocation();
694 }
695
696 // Parse the expression
697
698 // C++0x [dcl.type.simple]p4:
699 // The operand of the decltype specifier is an unevaluated operand.
Richard Smith76f3f692012-02-22 02:04:18 +0000700 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
701 0, /*IsDecltype=*/true);
David Blaikie42d6d0c2011-12-04 05:04:18 +0000702 Result = ParseExpression();
703 if (Result.isInvalid()) {
David Blaikie42d6d0c2011-12-04 05:04:18 +0000704 DS.SetTypeSpecError();
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000705 if (SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true)) {
706 EndLoc = ConsumeParen();
707 } else {
Richard Smith569cdc82012-12-09 04:17:57 +0000708 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000709 // Backtrack to get the location of the last token before the semi.
710 PP.RevertCachedTokens(2);
711 ConsumeToken(); // the semi.
712 EndLoc = ConsumeAnyToken();
713 assert(Tok.is(tok::semi));
714 } else {
715 EndLoc = Tok.getLocation();
716 }
717 }
718 return EndLoc;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000719 }
720
721 // Match the ')'
722 T.consumeClose();
723 if (T.getCloseLocation().isInvalid()) {
724 DS.SetTypeSpecError();
725 // FIXME: this should return the location of the last token
726 // that was consumed (by "consumeClose()")
727 return T.getCloseLocation();
728 }
729
Richard Smith76f3f692012-02-22 02:04:18 +0000730 Result = Actions.ActOnDecltypeExpression(Result.take());
731 if (Result.isInvalid()) {
732 DS.SetTypeSpecError();
733 return T.getCloseLocation();
734 }
735
David Blaikie42d6d0c2011-12-04 05:04:18 +0000736 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000737 }
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000739 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000740 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000741 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000742 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
David Blaikie42d6d0c2011-12-04 05:04:18 +0000743 DiagID, Result.release())) {
John McCallfec54012009-08-03 20:12:06 +0000744 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000745 DS.SetTypeSpecError();
746 }
747 return EndLoc;
748}
749
750void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
751 SourceLocation StartLoc,
752 SourceLocation EndLoc) {
753 // make sure we have a token we can turn into an annotation token
754 if (PP.isBacktrackEnabled())
755 PP.RevertCachedTokens(1);
756 else
757 PP.EnterToken(Tok);
758
759 Tok.setKind(tok::annot_decltype);
760 setExprAnnotation(Tok, DS.getTypeSpecType() == TST_decltype ?
761 DS.getRepAsExpr() : ExprResult());
762 Tok.setAnnotationEndLoc(EndLoc);
763 Tok.setLocation(StartLoc);
764 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000765}
766
Sean Huntdb5d44b2011-05-19 05:37:45 +0000767void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
768 assert(Tok.is(tok::kw___underlying_type) &&
769 "Not an underlying type specifier");
770
771 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000772 BalancedDelimiterTracker T(*this, tok::l_paren);
773 if (T.expectAndConsume(diag::err_expected_lparen_after,
774 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000775 return;
776 }
777
778 TypeResult Result = ParseTypeName();
779 if (Result.isInvalid()) {
780 SkipUntil(tok::r_paren);
781 return;
782 }
783
784 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000785 T.consumeClose();
786 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000787 return;
788
789 const char *PrevSpec = 0;
790 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000791 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000792 DiagID, Result.release()))
793 Diag(StartLoc, DiagID) << PrevSpec;
794}
795
David Blaikie09048df2011-10-25 15:01:20 +0000796/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
797/// class name or decltype-specifier. Note that we only check that the result
798/// names a type; semantic analysis will need to verify that the type names a
799/// class. The result is either a type or null, depending on whether a type
800/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000801///
Richard Smith05321402013-02-19 23:47:15 +0000802/// base-type-specifier: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000803/// class-or-decltype
Richard Smith05321402013-02-19 23:47:15 +0000804/// class-or-decltype: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000805/// nested-name-specifier[opt] class-name
806/// decltype-specifier
Richard Smith05321402013-02-19 23:47:15 +0000807/// class-name: [C++ class.name]
Douglas Gregor42a552f2008-11-05 20:51:48 +0000808/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000809/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000810///
Richard Smith05321402013-02-19 23:47:15 +0000811/// In C++98, instead of base-type-specifier, we have:
812///
813/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie22216eb2011-10-25 17:10:12 +0000814Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
815 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000816 // Ignore attempts to use typename
817 if (Tok.is(tok::kw_typename)) {
818 Diag(Tok, diag::err_expected_class_name_not_template)
819 << FixItHint::CreateRemoval(Tok.getLocation());
820 ConsumeToken();
821 }
822
David Blaikie152aa4b2011-10-25 18:17:58 +0000823 // Parse optional nested-name-specifier
824 CXXScopeSpec SS;
825 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
826
827 BaseLoc = Tok.getLocation();
828
David Blaikie22216eb2011-10-25 17:10:12 +0000829 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000830 // tok == kw_decltype is just error recovery, it can only happen when SS
831 // isn't empty
832 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000833 if (SS.isNotEmpty())
834 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
835 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000836 // Fake up a Declarator to use with ActOnTypeName.
837 DeclSpec DS(AttrFactory);
838
David Blaikieb5777572011-12-08 04:53:15 +0000839 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000840
841 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
842 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
843 }
844
Douglas Gregor7f43d672009-02-25 23:52:28 +0000845 // Check whether we have a template-id that names a type.
846 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000847 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000848 if (TemplateId->Kind == TNK_Type_template ||
849 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000850 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000851
852 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000853 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000854 EndLocation = Tok.getAnnotationEndLoc();
855 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000856
857 if (Type)
858 return Type;
859 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000860 }
861
862 // Fall through to produce an error below.
863 }
864
Douglas Gregor42a552f2008-11-05 20:51:48 +0000865 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000866 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000867 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000868 }
869
Douglas Gregor84d0a192010-01-12 21:28:44 +0000870 IdentifierInfo *Id = Tok.getIdentifierInfo();
871 SourceLocation IdLoc = ConsumeToken();
872
873 if (Tok.is(tok::less)) {
874 // It looks the user intended to write a template-id here, but the
875 // template-name was wrong. Try to fix that.
876 TemplateNameKind TNK = TNK_Type_template;
877 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000878 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000879 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000880 Diag(IdLoc, diag::err_unknown_template_name)
881 << Id;
882 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000883
Douglas Gregor84d0a192010-01-12 21:28:44 +0000884 if (!Template)
885 return true;
886
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000887 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000888 UnqualifiedId TemplateName;
889 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000890
Douglas Gregor84d0a192010-01-12 21:28:44 +0000891 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000892 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
893 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000894 return true;
895 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000896 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000897
Douglas Gregor84d0a192010-01-12 21:28:44 +0000898 // If we didn't end up with a typename token, there's nothing more we
899 // can do.
900 if (Tok.isNot(tok::annot_typename))
901 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000902
Douglas Gregor84d0a192010-01-12 21:28:44 +0000903 // Retrieve the type from the annotation token, consume that token, and
904 // return.
905 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000906 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000907 ConsumeToken();
908 return Type;
909 }
910
Douglas Gregor42a552f2008-11-05 20:51:48 +0000911 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000912 IdentifierInfo *CorrectedII = 0;
Douglas Gregor059101f2011-03-02 00:47:37 +0000913 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000914 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000915 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000916 /*NonTrivialTypeSourceInfo=*/true,
917 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000918 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000919 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000920 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000921 }
922
923 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000924 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000925
926 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000927 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000928 DS.SetRangeStart(IdLoc);
929 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000930 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000931
932 const char *PrevSpec = 0;
933 unsigned DiagID;
934 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
935
936 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
937 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000938}
939
John McCallc052dbb2012-05-22 21:28:12 +0000940void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
941 while (Tok.is(tok::kw___single_inheritance) ||
942 Tok.is(tok::kw___multiple_inheritance) ||
943 Tok.is(tok::kw___virtual_inheritance)) {
944 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
945 SourceLocation AttrNameLoc = ConsumeToken();
946 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000947 SourceLocation(), 0, 0, AttributeList::AS_GNU);
John McCallc052dbb2012-05-22 21:28:12 +0000948 }
949}
950
Richard Smithc9f35172012-06-25 21:37:02 +0000951/// Determine whether the following tokens are valid after a type-specifier
952/// which could be a standalone declaration. This will conservatively return
953/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith139be702012-07-02 19:14:01 +0000954bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smithc9f35172012-06-25 21:37:02 +0000955 // This switch enumerates the valid "follow" set for type-specifiers.
956 switch (Tok.getKind()) {
957 default: break;
958 case tok::semi: // struct foo {...} ;
959 case tok::star: // struct foo {...} * P;
960 case tok::amp: // struct foo {...} & R = ...
Richard Smithba65f502013-01-19 03:48:05 +0000961 case tok::ampamp: // struct foo {...} && R = ...
Richard Smithc9f35172012-06-25 21:37:02 +0000962 case tok::identifier: // struct foo {...} V ;
963 case tok::r_paren: //(struct foo {...} ) {4}
964 case tok::annot_cxxscope: // struct foo {...} a:: b;
965 case tok::annot_typename: // struct foo {...} a ::b;
966 case tok::annot_template_id: // struct foo {...} a<int> ::b;
967 case tok::l_paren: // struct foo {...} ( x);
968 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smithba65f502013-01-19 03:48:05 +0000969 case tok::kw_operator: // struct foo operator ++() {...}
Richard Smithc9f35172012-06-25 21:37:02 +0000970 return true;
Richard Smith139be702012-07-02 19:14:01 +0000971 case tok::colon:
972 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smithc9f35172012-06-25 21:37:02 +0000973 // Type qualifiers
974 case tok::kw_const: // struct foo {...} const x;
975 case tok::kw_volatile: // struct foo {...} volatile x;
976 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smithba65f502013-01-19 03:48:05 +0000977 // Function specifiers
978 // Note, no 'explicit'. An explicit function must be either a conversion
979 // operator or a constructor. Either way, it can't have a return type.
980 case tok::kw_inline: // struct foo inline f();
981 case tok::kw_virtual: // struct foo virtual f();
982 case tok::kw_friend: // struct foo friend f();
Richard Smithc9f35172012-06-25 21:37:02 +0000983 // Storage-class specifiers
984 case tok::kw_static: // struct foo {...} static x;
985 case tok::kw_extern: // struct foo {...} extern x;
986 case tok::kw_typedef: // struct foo {...} typedef x;
987 case tok::kw_register: // struct foo {...} register x;
988 case tok::kw_auto: // struct foo {...} auto x;
989 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smithba65f502013-01-19 03:48:05 +0000990 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smithc9f35172012-06-25 21:37:02 +0000991 case tok::kw_constexpr: // struct foo {...} constexpr x;
992 // As shown above, type qualifiers and storage class specifiers absolutely
993 // can occur after class specifiers according to the grammar. However,
994 // almost no one actually writes code like this. If we see one of these,
995 // it is much more likely that someone missed a semi colon and the
996 // type/storage class specifier we're seeing is part of the *next*
997 // intended declaration, as in:
998 //
999 // struct foo { ... }
1000 // typedef int X;
1001 //
1002 // We'd really like to emit a missing semicolon error instead of emitting
1003 // an error on the 'int' saying that you can't have two type specifiers in
1004 // the same declaration of X. Because of this, we look ahead past this
1005 // token to see if it's a type specifier. If so, we know the code is
1006 // otherwise invalid, so we can produce the expected semi error.
1007 if (!isKnownToBeTypeSpecifier(NextToken()))
1008 return true;
1009 break;
1010 case tok::r_brace: // struct bar { struct foo {...} }
1011 // Missing ';' at end of struct is accepted as an extension in C mode.
1012 if (!getLangOpts().CPlusPlus)
1013 return true;
1014 break;
Richard Smithba65f502013-01-19 03:48:05 +00001015 // C++11 attributes
1016 case tok::l_square: // enum E [[]] x
1017 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1018 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith8338a9d2013-01-29 04:13:32 +00001019 case tok::greater:
1020 // template<class T = class X>
1021 return getLangOpts().CPlusPlus;
Richard Smithc9f35172012-06-25 21:37:02 +00001022 }
1023 return false;
1024}
1025
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001026/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1027/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1028/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +00001029/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001030///
1031/// class-specifier: [C++ class]
1032/// class-head '{' member-specification[opt] '}'
1033/// class-head '{' member-specification[opt] '}' attributes[opt]
1034/// class-head:
1035/// class-key identifier[opt] base-clause[opt]
1036/// class-key nested-name-specifier identifier base-clause[opt]
1037/// class-key nested-name-specifier[opt] simple-template-id
1038/// base-clause[opt]
1039/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001040/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001041/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001042/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001043/// simple-template-id base-clause[opt]
1044/// class-key:
1045/// 'class'
1046/// 'struct'
1047/// 'union'
1048///
1049/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +00001050/// class-key ::[opt] nested-name-specifier[opt] identifier
1051/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1052/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001053///
1054/// Note that the C++ class-specifier and elaborated-type-specifier,
1055/// together, subsume the C99 struct-or-union-specifier:
1056///
1057/// struct-or-union-specifier: [C99 6.7.2.1]
1058/// struct-or-union identifier[opt] '{' struct-contents '}'
1059/// struct-or-union identifier
1060/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1061/// '}' attributes[opt]
1062/// [GNU] struct-or-union attributes[opt] identifier
1063/// struct-or-union:
1064/// 'struct'
1065/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +00001066void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1067 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001068 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001069 AccessSpecifier AS,
Michael Han2e397132012-11-26 22:54:45 +00001070 bool EnteringContext, DeclSpecContext DSC,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001071 ParsedAttributesWithRange &Attributes) {
Joao Matos17d35c32012-08-31 22:18:20 +00001072 DeclSpec::TST TagType;
1073 if (TagTokKind == tok::kw_struct)
1074 TagType = DeclSpec::TST_struct;
1075 else if (TagTokKind == tok::kw___interface)
1076 TagType = DeclSpec::TST_interface;
1077 else if (TagTokKind == tok::kw_class)
1078 TagType = DeclSpec::TST_class;
1079 else {
Chris Lattner4c97d762009-04-12 21:49:30 +00001080 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1081 TagType = DeclSpec::TST_union;
1082 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001083
Douglas Gregor374929f2009-09-18 15:37:17 +00001084 if (Tok.is(tok::code_completion)) {
1085 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001086 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001087 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00001088 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001089
Chandler Carruth926c4b42010-06-28 08:39:25 +00001090 // C++03 [temp.explicit] 14.7.2/8:
1091 // The usual access checking rules do not apply to names used to specify
1092 // explicit instantiations.
1093 //
1094 // As an extension we do not perform access checking on the names used to
1095 // specify explicit specializations either. This is important to allow
1096 // specializing traits classes for private types.
John McCall13489672012-05-07 06:16:58 +00001097 //
1098 // Note that we don't suppress if this turns out to be an elaborated
1099 // type specifier.
1100 bool shouldDelayDiagsInTag =
1101 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1102 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1103 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001104
Sean Hunt2edf0a22012-06-23 05:07:58 +00001105 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001106 // If attributes exist after tag, parse them.
1107 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +00001108 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001109
Steve Narofff59e17e2008-12-24 20:59:21 +00001110 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001111 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001112 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001113
John McCallc052dbb2012-05-22 21:28:12 +00001114 // Parse inheritance specifiers.
1115 if (Tok.is(tok::kw___single_inheritance) ||
1116 Tok.is(tok::kw___multiple_inheritance) ||
1117 Tok.is(tok::kw___virtual_inheritance))
1118 ParseMicrosoftInheritanceClassAttributes(attrs);
1119
Sean Huntbbd37c62009-11-21 08:43:09 +00001120 // If C++0x attributes exist here, parse them.
1121 // FIXME: Are we consistent with the ordering of parsing of different
1122 // styles of attributes?
Richard Smith4e24f0f2013-01-02 12:01:23 +00001123 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Michael Han07fc1ba2013-01-07 16:57:11 +00001125 // Source location used by FIXIT to insert misplaced
1126 // C++11 attributes
1127 SourceLocation AttrFixitLoc = Tok.getLocation();
1128
John Wiegley20c0da72011-04-27 23:09:49 +00001129 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +00001130 !Tok.is(tok::identifier) &&
1131 Tok.getIdentifierInfo() &&
1132 (Tok.is(tok::kw___is_arithmetic) ||
1133 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001134 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001135 Tok.is(tok::kw___is_floating_point) ||
1136 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001137 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001138 Tok.is(tok::kw___is_integral) ||
1139 Tok.is(tok::kw___is_member_function_pointer) ||
1140 Tok.is(tok::kw___is_member_pointer) ||
1141 Tok.is(tok::kw___is_pod) ||
1142 Tok.is(tok::kw___is_pointer) ||
1143 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001144 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001145 Tok.is(tok::kw___is_signed) ||
1146 Tok.is(tok::kw___is_unsigned) ||
1147 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001148 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001149 // name of struct templates, but some are keywords in GCC >= 4.3
1150 // and Clang. Therefore, when we see the token sequence "struct
1151 // X", make X into a normal identifier rather than a keyword, to
1152 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001153 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001154 Tok.setKind(tok::identifier);
1155 }
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001157 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001158 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001159 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001160 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1161 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001162
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001163 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001164 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001165 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001166 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1167 Diag(Tok, diag::err_expected_ident);
1168 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001169
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001170 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1171
Douglas Gregorcc636682009-02-17 23:15:12 +00001172 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001173 IdentifierInfo *Name = 0;
1174 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001175 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001176 if (Tok.is(tok::identifier)) {
1177 Name = Tok.getIdentifierInfo();
1178 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001179
David Blaikie4e4d0842012-03-11 07:00:24 +00001180 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001181 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001182 // Eat the template argument list and try to continue parsing this as
1183 // a class (or template thereof).
1184 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001185 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001186 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001187 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001188 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001189 // We couldn't parse the template argument list at all, so don't
1190 // try to give any location information for the list.
1191 LAngleLoc = RAngleLoc = SourceLocation();
1192 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001193
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001194 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matos17d35c32012-08-31 22:18:20 +00001195 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1196 << (TagType == DeclSpec::TST_class? 0
1197 : TagType == DeclSpec::TST_struct? 1
1198 : TagType == DeclSpec::TST_interface? 2
1199 : 3)
1200 << Name
1201 << SourceRange(LAngleLoc, RAngleLoc);
1202
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001203 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001204 // we've removed its template argument list.
1205 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1206 if (TemplateParams && TemplateParams->size() > 1) {
1207 TemplateParams->pop_back();
1208 } else {
1209 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001210 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001211 = ParsedTemplateInfo::NonTemplate;
1212 }
1213 } else if (TemplateInfo.Kind
1214 == ParsedTemplateInfo::ExplicitInstantiation) {
1215 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001216 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001217 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001218 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001219 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001220 = SourceLocation();
1221 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1222 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001223 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001224 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001225 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001226 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001227 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001228
Douglas Gregor059101f2011-03-02 00:47:37 +00001229 if (TemplateId->Kind != TNK_Type_template &&
1230 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001231 // The template-name in the simple-template-id refers to
1232 // something other than a class template. Give an appropriate
1233 // error message and skip to the ';'.
1234 SourceRange Range(NameLoc);
1235 if (SS.isNotEmpty())
1236 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001237
Douglas Gregor39a8de12009-02-25 19:37:18 +00001238 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1239 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Douglas Gregor39a8de12009-02-25 19:37:18 +00001241 DS.SetTypeSpecError();
1242 SkipUntil(tok::semi, false, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001243 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001244 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001245 }
1246
Richard Smith7796eb52012-03-12 08:56:40 +00001247 // There are four options here.
1248 // - If we are in a trailing return type, this is always just a reference,
1249 // and we must not try to parse a definition. For instance,
1250 // [] () -> struct S { };
1251 // does not define a type.
1252 // - If we have 'struct foo {...', 'struct foo :...',
1253 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1254 // - If we have 'struct foo;', then this is either a forward declaration
1255 // or a friend declaration, which have to be treated differently.
1256 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han2e397132012-11-26 22:54:45 +00001257 //
1258 // We also detect these erroneous cases to provide better diagnostic for
1259 // C++11 attributes parsing.
1260 // - attributes follow class name:
1261 // struct foo [[]] {};
1262 // - attributes appear before or after 'final':
1263 // struct foo [[]] final [[]] {};
1264 //
Richard Smith69730c12012-03-12 07:56:15 +00001265 // However, in type-specifier-seq's, things look like declarations but are
1266 // just references, e.g.
1267 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001268 // or
Richard Smith69730c12012-03-12 07:56:15 +00001269 // &T::operator struct s;
1270 // For these, DSC is DSC_type_specifier.
Michael Han2e397132012-11-26 22:54:45 +00001271
1272 // If there are attributes after class name, parse them.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001273 MaybeParseCXX11Attributes(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00001274
John McCallf312b1e2010-08-26 23:41:50 +00001275 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001276 if (DSC == DSC_trailing)
1277 TUK = Sema::TUK_Reference;
1278 else if (Tok.is(tok::l_brace) ||
1279 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001280 (isCXX11FinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001281 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001282 if (DS.isFriendSpecified()) {
1283 // C++ [class.friend]p2:
1284 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001285 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001286 << SourceRange(DS.getFriendSpecLoc());
1287
1288 // Skip everything up to the semicolon, so that this looks like a proper
1289 // friend class (or template thereof) declaration.
1290 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001291 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001292 } else {
1293 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001294 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001295 }
Richard Smith150d8532013-02-22 06:46:23 +00001296 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1297 NextToken().is(tok::kw_alignas))) {
Michael Han2e397132012-11-26 22:54:45 +00001298 // We can't tell if this is a definition or reference
1299 // until we skipped the 'final' and C++11 attribute specifiers.
1300 TentativeParsingAction PA(*this);
1301
1302 // Skip the 'final' keyword.
1303 ConsumeToken();
1304
1305 // Skip C++11 attribute specifiers.
1306 while (true) {
1307 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1308 ConsumeBracket();
1309 if (!SkipUntil(tok::r_square))
1310 break;
Richard Smith150d8532013-02-22 06:46:23 +00001311 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han2e397132012-11-26 22:54:45 +00001312 ConsumeToken();
1313 ConsumeParen();
1314 if (!SkipUntil(tok::r_paren))
1315 break;
1316 } else {
1317 break;
1318 }
1319 }
1320
1321 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1322 TUK = Sema::TUK_Definition;
1323 else
1324 TUK = Sema::TUK_Reference;
1325
1326 PA.Revert();
Richard Smithc9f35172012-06-25 21:37:02 +00001327 } else if (DSC != DSC_type_specifier &&
1328 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00001329 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallf312b1e2010-08-26 23:41:50 +00001330 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matos17d35c32012-08-31 22:18:20 +00001331 if (Tok.isNot(tok::semi)) {
1332 // A semicolon was missing after this declaration. Diagnose and recover.
1333 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1334 DeclSpec::getSpecifierName(TagType));
1335 PP.EnterToken(Tok);
1336 Tok.setKind(tok::semi);
1337 }
Richard Smithc9f35172012-06-25 21:37:02 +00001338 } else
John McCallf312b1e2010-08-26 23:41:50 +00001339 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001340
Michael Han2e397132012-11-26 22:54:45 +00001341 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1342 // to caller to handle.
Michael Han07fc1ba2013-01-07 16:57:11 +00001343 if (TUK != Sema::TUK_Reference) {
1344 // If this is not a reference, then the only possible
1345 // valid place for C++11 attributes to appear here
1346 // is between class-key and class-name. If there are
1347 // any attributes after class-name, we try a fixit to move
1348 // them to the right place.
1349 SourceRange AttrRange = Attributes.Range;
1350 if (AttrRange.isValid()) {
1351 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1352 << AttrRange
1353 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1354 CharSourceRange(AttrRange, true))
1355 << FixItHint::CreateRemoval(AttrRange);
1356
1357 // Recover by adding misplaced attributes to the attribute list
1358 // of the class so they can be applied on the class later.
1359 attrs.takeAllFrom(Attributes);
1360 }
1361 }
Michael Han2e397132012-11-26 22:54:45 +00001362
John McCall13489672012-05-07 06:16:58 +00001363 // If this is an elaborated type specifier, and we delayed
1364 // diagnostics before, just merge them into the current pool.
1365 if (shouldDelayDiagsInTag) {
1366 diagsFromTag.done();
1367 if (TUK == Sema::TUK_Reference)
1368 diagsFromTag.redelay();
1369 }
1370
John McCall207014e2010-07-30 06:26:29 +00001371 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001372 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001373 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1374 // We have a declaration or reference to an anonymous class.
1375 Diag(StartLoc, diag::err_anon_type_definition)
1376 << DeclSpec::getSpecifierName(TagType);
1377 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001378
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001379 SkipUntil(tok::comma, true);
1380 return;
1381 }
1382
Douglas Gregorddc29e12009-02-06 22:42:48 +00001383 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001384 DeclResult TagOrTempResult = true; // invalid
1385 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001386
Douglas Gregor402abb52009-05-28 23:31:59 +00001387 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001388 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001389 // Explicit specialization, class template partial specialization,
1390 // or explicit instantiation.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001391 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001392 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001393 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001394 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001395 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001396 ProhibitAttributes(attrs);
1397
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001398 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001399 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001400 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001401 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001402 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001403 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001404 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001405 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001406 TemplateId->TemplateNameLoc,
1407 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001408 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001409 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001410 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001411
1412 // Friend template-ids are treated as references unless
1413 // they have template headers, in which case they're ill-formed
1414 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1415 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001416 } else if (TUK == Sema::TUK_Reference ||
1417 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001418 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001419 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001420 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001421 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001422 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001423 TemplateId->Template,
1424 TemplateId->TemplateNameLoc,
1425 TemplateId->LAngleLoc,
1426 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001427 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001428 } else {
1429 // This is an explicit specialization or a class template
1430 // partial specialization.
1431 TemplateParameterLists FakedParamLists;
1432
1433 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1434 // This looks like an explicit instantiation, because we have
1435 // something like
1436 //
1437 // template class Foo<X>
1438 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001439 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001440 // meant to be an explicit specialization, but the user forgot
1441 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +00001442 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001443
Mike Stump1eb44332009-09-09 15:08:12 +00001444 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001445 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001446 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001447 diag::err_explicit_instantiation_with_definition)
1448 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +00001449 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001450
1451 // Create a fake template parameter list that contains only
1452 // "template<>", so that we treat this construct as a class
1453 // template specialization.
1454 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +00001455 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001456 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001457 LAngleLoc,
1458 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001459 LAngleLoc));
1460 TemplateParams = &FakedParamLists;
1461 }
1462
1463 // Build the class template specialization.
1464 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001465 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001466 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001467 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001468 TemplateId->TemplateNameLoc,
1469 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001470 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001471 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001472 attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001473 MultiTemplateParamsArg(
Douglas Gregorcc636682009-02-17 23:15:12 +00001474 TemplateParams? &(*TemplateParams)[0] : 0,
1475 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001476 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001477 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001478 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001479 // Explicit instantiation of a member of a class template
1480 // specialization, e.g.,
1481 //
1482 // template struct Outer<int>::Inner;
1483 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001484 ProhibitAttributes(attrs);
1485
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001486 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001487 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001488 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001489 TemplateInfo.TemplateLoc,
1490 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001491 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001492 } else if (TUK == Sema::TUK_Friend &&
1493 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001494 ProhibitAttributes(attrs);
1495
John McCall9a34edb2010-10-19 01:40:49 +00001496 TagOrTempResult =
1497 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1498 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001499 Name, NameLoc, attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001500 MultiTemplateParamsArg(
John McCall9a34edb2010-10-19 01:40:49 +00001501 TemplateParams? &(*TemplateParams)[0] : 0,
1502 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001503 } else {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001504 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1505 ProhibitAttributes(attrs);
1506
John McCallc4e70192009-09-11 04:59:25 +00001507 bool IsDependent = false;
1508
John McCalla25c4082010-10-19 18:40:57 +00001509 // Don't pass down template parameter lists if this is just a tag
1510 // reference. For example, we don't need the template parameters here:
1511 // template <class T> class A *makeA(T t);
1512 MultiTemplateParamsArg TParams;
1513 if (TUK != Sema::TUK_Reference && TemplateParams)
1514 TParams =
1515 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1516
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001517 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001518 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001519 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001520 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001521 TParams, Owned, IsDependent,
1522 SourceLocation(), false,
1523 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001524
1525 // If ActOnTag said the type was dependent, try again with the
1526 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001527 if (IsDependent) {
1528 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001529 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001530 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001531 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001532 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001533
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001534 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001535 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001536 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001537 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001538 isCXX11FinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001539 if (getLangOpts().CPlusPlus)
Michael Han07fc1ba2013-01-07 16:57:11 +00001540 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1541 TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001542 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001543 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001544 }
1545
John McCallb3d87482010-08-24 05:47:05 +00001546 const char *PrevSpec = 0;
1547 unsigned DiagID;
1548 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001549 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001550 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1551 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001552 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001553 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001554 Result = DS.SetTypeSpecType(TagType, StartLoc,
1555 NameLoc.isValid() ? NameLoc : StartLoc,
1556 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001557 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001558 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001559 return;
1560 }
Mike Stump1eb44332009-09-09 15:08:12 +00001561
John McCallb3d87482010-08-24 05:47:05 +00001562 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001563 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001564
Chris Lattner4ed5d912010-02-02 01:23:29 +00001565 // At this point, we've successfully parsed a class-specifier in 'definition'
1566 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1567 // going to look at what comes after it to improve error recovery. If an
1568 // impossible token occurs next, we assume that the programmer forgot a ; at
1569 // the end of the declaration and recover that way.
1570 //
Richard Smithc9f35172012-06-25 21:37:02 +00001571 // Also enforce C++ [temp]p3:
1572 // In a template-declaration which defines a class, no declarator
1573 // is permitted.
Joao Matos17d35c32012-08-31 22:18:20 +00001574 if (TUK == Sema::TUK_Definition &&
1575 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidis7d033b22012-12-17 20:10:43 +00001576 if (Tok.isNot(tok::semi)) {
1577 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1578 DeclSpec::getSpecifierName(TagType));
1579 // Push this token back into the preprocessor and change our current token
1580 // to ';' so that the rest of the code recovers as though there were an
1581 // ';' after the definition.
1582 PP.EnterToken(Tok);
1583 Tok.setKind(tok::semi);
1584 }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001585 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001586}
1587
Mike Stump1eb44332009-09-09 15:08:12 +00001588/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001589///
1590/// base-clause : [C++ class.derived]
1591/// ':' base-specifier-list
1592/// base-specifier-list:
1593/// base-specifier '...'[opt]
1594/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001595void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001596 assert(Tok.is(tok::colon) && "Not a base clause");
1597 ConsumeToken();
1598
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001599 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001600 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001601
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001602 while (true) {
1603 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001604 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001605 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001606 // Skip the rest of this base specifier, up until the comma or
1607 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001608 SkipUntil(tok::comma, tok::l_brace, true, true);
1609 } else {
1610 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001611 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001612 }
1613
1614 // If the next token is a comma, consume it and keep reading
1615 // base-specifiers.
1616 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001618 // Consume the comma.
1619 ConsumeToken();
1620 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001621
1622 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001623 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001624}
1625
1626/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1627/// one entry in the base class list of a class specifier, for example:
1628/// class foo : public bar, virtual private baz {
1629/// 'public bar' and 'virtual private baz' are each base-specifiers.
1630///
1631/// base-specifier: [C++ class.derived]
Richard Smith05321402013-02-19 23:47:15 +00001632/// attribute-specifier-seq[opt] base-type-specifier
1633/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1634/// base-type-specifier
1635/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1636/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001637Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001638 bool IsVirtual = false;
1639 SourceLocation StartLoc = Tok.getLocation();
1640
Richard Smith05321402013-02-19 23:47:15 +00001641 ParsedAttributesWithRange Attributes(AttrFactory);
1642 MaybeParseCXX11Attributes(Attributes);
1643
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001644 // Parse the 'virtual' keyword.
1645 if (Tok.is(tok::kw_virtual)) {
1646 ConsumeToken();
1647 IsVirtual = true;
1648 }
1649
Richard Smith05321402013-02-19 23:47:15 +00001650 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1651
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001652 // Parse an (optional) access specifier.
1653 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001654 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001655 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Richard Smith05321402013-02-19 23:47:15 +00001657 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1658
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001659 // Parse the 'virtual' keyword (again!), in case it came after the
1660 // access specifier.
1661 if (Tok.is(tok::kw_virtual)) {
1662 SourceLocation VirtualLoc = ConsumeToken();
1663 if (IsVirtual) {
1664 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001665 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001666 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001667 }
1668
1669 IsVirtual = true;
1670 }
1671
Richard Smith05321402013-02-19 23:47:15 +00001672 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1673
Douglas Gregor42a552f2008-11-05 20:51:48 +00001674 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001675 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001676 SourceLocation BaseLoc;
1677 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001678 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001679 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001681 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1682 // actually part of the base-specifier-list grammar productions, but we
1683 // parse it here for convenience.
1684 SourceLocation EllipsisLoc;
1685 if (Tok.is(tok::ellipsis))
1686 EllipsisLoc = ConsumeToken();
1687
Mike Stump1eb44332009-09-09 15:08:12 +00001688 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001689 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001691 // Notify semantic analysis that we have parsed a complete
1692 // base-specifier.
Richard Smith05321402013-02-19 23:47:15 +00001693 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1694 Access, BaseType.get(), BaseLoc,
1695 EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001696}
1697
1698/// getAccessSpecifierIfPresent - Determine whether the next token is
1699/// a C++ access-specifier.
1700///
1701/// access-specifier: [C++ class.derived]
1702/// 'private'
1703/// 'protected'
1704/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001705AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001706 switch (Tok.getKind()) {
1707 default: return AS_none;
1708 case tok::kw_private: return AS_private;
1709 case tok::kw_protected: return AS_protected;
1710 case tok::kw_public: return AS_public;
1711 }
1712}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001713
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001714/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001715/// delayed, e.g., default arguments, create a late-parsed method declaration
1716/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001717void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1718 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001719 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001720 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001721 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001722 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001723 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001724
Eli Friedmand33133c2009-07-22 21:45:50 +00001725 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1726 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1727 if (!LateMethod) {
1728 // Push this method onto the stack of late-parsed method
1729 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001730 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1731 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001732 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001733
1734 // Add all of the parameters prior to this one (they don't
1735 // have default arguments).
1736 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1737 for (unsigned I = 0; I < ParamIdx; ++I)
1738 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001739 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001740 }
1741
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001742 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001743 // not have a default argument).
1744 LateMethod->DefaultArgs.push_back(
1745 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1746 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1747 }
1748 }
1749}
1750
Richard Smith4e24f0f2013-01-02 12:01:23 +00001751/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001752/// virt-specifier.
1753///
1754/// virt-specifier:
1755/// override
1756/// final
Richard Smith4e24f0f2013-01-02 12:01:23 +00001757VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001758 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001759 return VirtSpecifiers::VS_None;
1760
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001761 if (Tok.is(tok::identifier)) {
1762 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001763
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001764 // Initialize the contextual keywords.
1765 if (!Ident_final) {
1766 Ident_final = &PP.getIdentifierTable().get("final");
1767 Ident_override = &PP.getIdentifierTable().get("override");
1768 }
1769
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001770 if (II == Ident_override)
1771 return VirtSpecifiers::VS_Override;
1772
1773 if (II == Ident_final)
1774 return VirtSpecifiers::VS_Final;
1775 }
1776
1777 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001778}
1779
Richard Smith4e24f0f2013-01-02 12:01:23 +00001780/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001781///
1782/// virt-specifier-seq:
1783/// virt-specifier
1784/// virt-specifier-seq virt-specifier
Richard Smith4e24f0f2013-01-02 12:01:23 +00001785void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalle402e722012-09-25 07:32:39 +00001786 bool IsInterface) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001787 while (true) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001788 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001789 if (Specifier == VirtSpecifiers::VS_None)
1790 return;
1791
1792 // C++ [class.mem]p8:
1793 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001794 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001795 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001796 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1797 << PrevSpec
1798 << FixItHint::CreateRemoval(Tok.getLocation());
1799
John McCalle402e722012-09-25 07:32:39 +00001800 if (IsInterface && Specifier == VirtSpecifiers::VS_Final) {
1801 Diag(Tok.getLocation(), diag::err_override_control_interface)
1802 << VirtSpecifiers::getSpecifierName(Specifier);
1803 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00001804 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00001805 diag::warn_cxx98_compat_override_control_keyword :
1806 diag::ext_override_control_keyword)
1807 << VirtSpecifiers::getSpecifierName(Specifier);
1808 }
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001809 ConsumeToken();
1810 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001811}
1812
Richard Smith4e24f0f2013-01-02 12:01:23 +00001813/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001814/// contextual 'final' keyword.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001815bool Parser::isCXX11FinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001816 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001817 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001818
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001819 if (!Tok.is(tok::identifier))
1820 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001821
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001822 // Initialize the contextual keywords.
1823 if (!Ident_final) {
1824 Ident_final = &PP.getIdentifierTable().get("final");
1825 Ident_override = &PP.getIdentifierTable().get("override");
1826 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001827
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001828 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001829}
1830
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001831/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1832///
1833/// member-declaration:
1834/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1835/// function-definition ';'[opt]
1836/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1837/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001838/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001839/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001840/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001841///
1842/// member-declarator-list:
1843/// member-declarator
1844/// member-declarator-list ',' member-declarator
1845///
1846/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001847/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001848/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001849/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001850/// identifier[opt] ':' constant-expression
1851///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001852/// virt-specifier-seq:
1853/// virt-specifier
1854/// virt-specifier-seq virt-specifier
1855///
1856/// virt-specifier:
1857/// override
1858/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001859///
Sebastian Redle2b68332009-04-12 17:16:29 +00001860/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001861/// '= 0'
1862///
1863/// constant-initializer:
1864/// '=' constant-expression
1865///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001866void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001867 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001868 const ParsedTemplateInfo &TemplateInfo,
1869 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001870 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001871 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001872 Diag(Tok, diag::err_at_defs_cxx);
1873 else
1874 Diag(Tok, diag::err_at_in_class);
1875
1876 ConsumeToken();
1877 SkipUntil(tok::r_brace);
1878 return;
1879 }
1880
John McCall60fa3cf2009-12-11 02:10:03 +00001881 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001882 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001883 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001884 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1885 if (TryAnnotateCXXScopeToken())
1886 MalformedTypeSpec = true;
1887
1888 bool isAccessDecl;
1889 if (Tok.isNot(tok::annot_cxxscope))
1890 isAccessDecl = false;
1891 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001892 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1893 else
1894 isAccessDecl = NextToken().is(tok::kw_operator);
1895
1896 if (isAccessDecl) {
1897 // Collect the scope specifier token we annotated earlier.
1898 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001899 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1900 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001901
1902 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001903 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001904 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001905 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1906 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001907 SkipUntil(tok::semi);
1908 return;
1909 }
1910
1911 // TODO: recover from mistakenly-qualified operator declarations.
1912 if (ExpectAndConsume(tok::semi,
1913 diag::err_expected_semi_after,
1914 "access declaration",
1915 tok::semi))
1916 return;
1917
Douglas Gregor23c94db2010-07-02 17:43:08 +00001918 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001919 false, SourceLocation(),
1920 SS, Name,
1921 /* AttrList */ 0,
1922 /* IsTypeName */ false,
1923 SourceLocation());
1924 return;
1925 }
1926 }
1927
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001928 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001929 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001930 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001931 SourceLocation DeclEnd;
1932 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001933 return;
1934 }
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Chris Lattner682bf922009-03-29 16:50:03 +00001936 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001937 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001938 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001939 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001940 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001941 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001942 return;
1943 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001944
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001945 // Handle: member-declaration ::= '__extension__' member-declaration
1946 if (Tok.is(tok::kw___extension__)) {
1947 // __extension__ silences extension warnings in the subexpression.
1948 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1949 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001950 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1951 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001952 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001953
Chris Lattner4ed5d912010-02-02 01:23:29 +00001954 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1955 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001956 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001957
John McCall0b7e6782011-03-24 11:26:52 +00001958 ParsedAttributesWithRange attrs(AttrFactory);
Michael Han52b501c2012-11-28 23:17:40 +00001959 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001960 // Optional C++11 attribute-specifier
1961 MaybeParseCXX11Attributes(attrs);
Michael Han52b501c2012-11-28 23:17:40 +00001962 // We need to keep these attributes for future diagnostic
1963 // before they are taken over by declaration specifier.
1964 FnAttrs.addAll(attrs.getList());
1965 FnAttrs.Range = attrs.Range;
1966
John McCall7f040a92010-12-24 02:08:15 +00001967 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001968
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001969 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00001970 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001971
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001972 // Eat 'using'.
1973 SourceLocation UsingLoc = ConsumeToken();
1974
1975 if (Tok.is(tok::kw_namespace)) {
1976 Diag(UsingLoc, diag::err_using_namespace_in_class);
1977 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001978 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001979 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001980 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00001981 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1982 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001983 }
1984 return;
1985 }
1986
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001987 // Hold late-parsed attributes so we can attach a Decl to them later.
1988 LateParsedAttrList CommonLateParsedAttrs;
1989
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001990 // decl-specifier-seq:
1991 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001992 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00001993 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00001994 if (MalformedTypeSpec)
1995 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001996 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
1997 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001998
Benjamin Kramer5354e772012-08-23 23:38:35 +00001999 MultiTemplateParamsArg TemplateParams(
John McCalldd4a3b02009-09-16 22:47:08 +00002000 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2001 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2002
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002003 if (Tok.is(tok::semi)) {
2004 ConsumeToken();
Michael Han52b501c2012-11-28 23:17:40 +00002005
2006 if (DS.isFriendSpecified())
2007 ProhibitAttributes(FnAttrs);
2008
John McCalld226f652010-08-21 09:40:31 +00002009 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00002010 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00002011 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00002012 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002013 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002014
John McCall54abf7d2009-11-04 02:18:39 +00002015 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00002016 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002017
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002018 // Hold late-parsed attributes so we can attach a Decl to them later.
2019 LateParsedAttrList LateParsedAttrs;
2020
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002021 SourceLocation EqualLoc;
2022 bool HasInitializer = false;
2023 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002024 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002025 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2026 ColonProtectionRAIIObject X(*this);
2027
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002028 // Parse the first declarator.
2029 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00002030 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00002031 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002032 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00002033 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002034 if (Tok.is(tok::semi))
2035 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002036 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002037 }
2038
Richard Smith4e24f0f2013-01-02 12:01:23 +00002039 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber48673472011-01-28 06:07:34 +00002040
John Thompson1b2fc0f2009-11-25 22:58:06 +00002041 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002042 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00002043
Francois Pichet6a247472011-05-11 02:14:46 +00002044 // MSVC permits pure specifier on inline functions declared at class scope.
2045 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00002046 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00002047 DeclaratorInfo.isFunctionDeclarator() &&
2048 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002049 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00002050 Init = ParseInitializer();
2051 if (Init.isInvalid())
2052 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002053 else
2054 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00002055 }
2056
Douglas Gregor45fa5602011-11-07 20:56:01 +00002057 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002058 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00002059 //
2060 // In C++11, a non-function declarator followed by an open brace is a
2061 // braced-init-list for an in-class member initialization, not an
2062 // erroneous function definition.
Richard Smith80ad52f2013-01-02 11:42:31 +00002063 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002064 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002065 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00002066 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002067 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002068 } else if (Tok.is(tok::equal)) {
2069 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00002070 if (KW.is(tok::kw_default))
2071 DefinitionKind = FDK_Defaulted;
2072 else if (KW.is(tok::kw_delete))
2073 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00002074 }
2075 }
2076
Michael Han52b501c2012-11-28 23:17:40 +00002077 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2078 // to a friend declaration, that declaration shall be a definition.
2079 if (DeclaratorInfo.isFunctionDeclarator() &&
2080 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2081 // Diagnose attributes that appear before decl specifier:
2082 // [[]] friend int foo();
2083 ProhibitAttributes(FnAttrs);
2084 }
2085
Douglas Gregor45fa5602011-11-07 20:56:01 +00002086 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002087 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002088 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002089 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00002090 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Michael Han52b501c2012-11-28 23:17:40 +00002091
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002092 // Consume the optional ';'
2093 if (Tok.is(tok::semi))
2094 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002095 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002096 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002097
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002098 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002099 Diag(DeclaratorInfo.getIdentifierLoc(),
2100 diag::err_function_declared_typedef);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002101
Richard Smith6f9a4452012-11-15 22:54:20 +00002102 // Recover by treating the 'typedef' as spurious.
2103 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002104 }
2105
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002106 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002107 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00002108 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002109
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002110 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2111 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2112 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002113 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002114 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002115 }
2116 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00002117
2118 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002119 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00002120 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002121
Chris Lattner682bf922009-03-29 16:50:03 +00002122 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002123 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002124 }
2125
2126 // member-declarator-list:
2127 // member-declarator
2128 // member-declarator-list ',' member-declarator
2129
Chris Lattner5f9e2722011-07-23 10:55:15 +00002130 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00002131 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00002132 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002133
2134 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002135 // member-declarator:
2136 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002137 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002138 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002139 if (Tok.is(tok::colon)) {
2140 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002141 BitfieldSize = ParseConstantExpression();
2142 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002143 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002144 }
Mike Stump1eb44332009-09-09 15:08:12 +00002145
Chris Lattnere6563252010-06-13 05:34:18 +00002146 // If a simple-asm-expr is present, parse it.
2147 if (Tok.is(tok::kw_asm)) {
2148 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002149 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002150 if (AsmLabel.isInvalid())
2151 SkipUntil(tok::comma, true, true);
2152
2153 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2154 DeclaratorInfo.SetRangeEnd(Loc);
2155 }
2156
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002157 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002158 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002159
Richard Smith7a614d82011-06-11 17:19:42 +00002160 // FIXME: When g++ adds support for this, we'll need to check whether it
2161 // goes before or after the GNU attributes and __asm__.
Richard Smith4e24f0f2013-01-02 12:01:23 +00002162 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith7a614d82011-06-11 17:19:42 +00002163
Richard Smithca523302012-06-10 03:12:00 +00002164 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002165 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002166 if (BitfieldSize.get()) {
2167 Diag(Tok, diag::err_bitfield_member_init);
2168 SkipUntil(tok::comma, true, true);
2169 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002170 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002171 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2172 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smithca523302012-06-10 03:12:00 +00002173 != DeclSpec::SCS_typedef)
2174 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002175 }
2176 }
2177
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002178 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002179 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002180 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002181
Rafael Espindolafc35cbc2013-01-08 20:44:06 +00002182 NamedDecl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002183 if (DS.isFriendSpecified()) {
Michael Han52b501c2012-11-28 23:17:40 +00002184 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2185 // to a friend declaration, that declaration shall be a definition.
2186 //
2187 // Diagnose attributes appear after friend member function declarator:
2188 // foo [[]] ();
2189 SmallVector<SourceRange, 4> Ranges;
2190 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2191 if (!Ranges.empty()) {
2192 for (SmallVector<SourceRange, 4>::iterator I = Ranges.begin(),
2193 E = Ranges.end(); I != E; ++I) {
2194 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2195 << *I;
2196 }
2197 }
2198
John McCallbbbcdd92009-09-11 21:02:39 +00002199 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002200 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002201 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002202 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002203 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002204 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002205 TemplateParams,
John McCall67d1a672009-08-06 02:15:43 +00002206 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002207 VS, HasInClassInit);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002208 if (AccessAttrs)
2209 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2210 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002211 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002212
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002213 // Set the Decl for any late parsed attributes
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002214 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2215 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2216 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002217 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002218 LateParsedAttrs[i]->addDecl(ThisDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002219 }
2220 LateParsedAttrs.clear();
2221
Douglas Gregor147545d2011-10-10 14:49:18 +00002222 // Handle the initializer.
David Blaikie1d87fba2013-01-30 01:22:18 +00002223 if (HasInClassInit != ICIS_NoInit &&
2224 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2225 DeclSpec::SCS_static) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002226 // The initializer was deferred; parse it and cache the tokens.
Richard Smith80ad52f2013-01-02 11:42:31 +00002227 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00002228 diag::warn_cxx98_compat_nonstatic_member_init :
2229 diag::ext_nonstatic_member_init);
2230
Richard Smith7a614d82011-06-11 17:19:42 +00002231 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002232 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2233 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002234 //
2235 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002236 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002237 Diag(Tok, diag::err_incomplete_array_member_init);
2238 SkipUntil(tok::comma, true, true);
David Blaikie3164c142012-02-14 09:00:46 +00002239 if (ThisDecl)
2240 // Avoid later warnings about a class member of incomplete type.
2241 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002242 } else
2243 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002244 } else if (HasInitializer) {
2245 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002246 if (!Init.isUsable())
Douglas Gregor552e2992012-02-21 02:22:07 +00002247 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002248 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2249
Douglas Gregor147545d2011-10-10 14:49:18 +00002250 if (Init.isInvalid())
2251 SkipUntil(tok::comma, true, true);
2252 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002253 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002254 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor147545d2011-10-10 14:49:18 +00002255 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2256 // No initializer.
2257 Actions.ActOnUninitializedDecl(ThisDecl,
2258 DS.getTypeSpecType() == DeclSpec::TST_auto);
Richard Smith7a614d82011-06-11 17:19:42 +00002259 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002260
2261 if (ThisDecl) {
2262 Actions.FinalizeDeclaration(ThisDecl);
2263 DeclsInGroup.push_back(ThisDecl);
2264 }
2265
Richard Smithe5310012012-04-29 07:31:09 +00002266 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor147545d2011-10-10 14:49:18 +00002267 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2268 != DeclSpec::SCS_typedef) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002269 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002270 }
2271
2272 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002273
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002274 // If we don't have a comma, it is either the end of the list (a ';')
2275 // or an error, bail out.
2276 if (Tok.isNot(tok::comma))
2277 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002278
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002279 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002280 SourceLocation CommaLoc = ConsumeToken();
2281
2282 if (Tok.isAtStartOfLine() &&
2283 !MightBeDeclarator(Declarator::MemberContext)) {
2284 // This comma was followed by a line-break and something which can't be
2285 // the start of a declarator. The comma was probably a typo for a
2286 // semicolon.
2287 Diag(CommaLoc, diag::err_expected_semi_declaration)
2288 << FixItHint::CreateReplacement(CommaLoc, ";");
2289 ExpectSemi = false;
2290 break;
2291 }
Mike Stump1eb44332009-09-09 15:08:12 +00002292
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002293 // Parse the next declarator.
2294 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002295 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002296 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002297 Init = true;
2298 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002299 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002300
Bill Wendlingad017fa2012-12-20 19:22:21 +00002301 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002302 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002303
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002304 if (Tok.isNot(tok::colon))
2305 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002306 }
2307
Richard Smith1c94c162012-01-09 22:31:44 +00002308 if (ExpectSemi &&
2309 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002310 // Skip to end of block or statement.
2311 SkipUntil(tok::r_brace, true, true);
2312 // If we stopped at a ';', eat it.
2313 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002314 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002315 }
2316
Douglas Gregor23c94db2010-07-02 17:43:08 +00002317 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00002318 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002319}
2320
Richard Smith7a614d82011-06-11 17:19:42 +00002321/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2322/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2323/// function definition. The location of the '=', if any, will be placed in
2324/// EqualLoc.
2325///
2326/// pure-specifier:
2327/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002328///
Richard Smith7a614d82011-06-11 17:19:42 +00002329/// brace-or-equal-initializer:
2330/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002331/// braced-init-list
2332///
Richard Smith7a614d82011-06-11 17:19:42 +00002333/// initializer-clause:
2334/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002335/// braced-init-list
2336///
Richard Smith7a614d82011-06-11 17:19:42 +00002337/// defaulted/deleted function-definition:
2338/// '=' 'default'
2339/// '=' 'delete'
2340///
2341/// Prior to C++0x, the assignment-expression in an initializer-clause must
2342/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002343ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002344 SourceLocation &EqualLoc) {
2345 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2346 && "Data member initializer not starting with '=' or '{'");
2347
Douglas Gregor552e2992012-02-21 02:22:07 +00002348 EnterExpressionEvaluationContext Context(Actions,
2349 Sema::PotentiallyEvaluated,
2350 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002351 if (Tok.is(tok::equal)) {
2352 EqualLoc = ConsumeToken();
2353 if (Tok.is(tok::kw_delete)) {
2354 // In principle, an initializer of '= delete p;' is legal, but it will
2355 // never type-check. It's better to diagnose it as an ill-formed expression
2356 // than as an ill-formed deleted non-function member.
2357 // An initializer of '= delete p, foo' will never be parsed, because
2358 // a top-level comma always ends the initializer expression.
2359 const Token &Next = NextToken();
2360 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2361 Next.is(tok::eof)) {
2362 if (IsFunction)
2363 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2364 << 1 /* delete */;
2365 else
2366 Diag(ConsumeToken(), diag::err_deleted_non_function);
2367 return ExprResult();
2368 }
2369 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002370 if (IsFunction)
2371 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2372 << 0 /* default */;
2373 else
2374 Diag(ConsumeToken(), diag::err_default_special_members);
2375 return ExprResult();
2376 }
2377
Sebastian Redl33deb352012-02-22 10:50:08 +00002378 }
2379 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002380}
2381
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002382/// ParseCXXMemberSpecification - Parse the class definition.
2383///
2384/// member-specification:
2385/// member-declaration member-specification[opt]
2386/// access-specifier ':' member-specification[opt]
2387///
Joao Matos17d35c32012-08-31 22:18:20 +00002388void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han07fc1ba2013-01-07 16:57:11 +00002389 SourceLocation AttrFixitLoc,
Richard Smith05321402013-02-19 23:47:15 +00002390 ParsedAttributesWithRange &Attrs,
Joao Matos17d35c32012-08-31 22:18:20 +00002391 unsigned TagType, Decl *TagDecl) {
2392 assert((TagType == DeclSpec::TST_struct ||
2393 TagType == DeclSpec::TST_interface ||
2394 TagType == DeclSpec::TST_union ||
2395 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2396
John McCallf312b1e2010-08-26 23:41:50 +00002397 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2398 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002399
Douglas Gregor26997fd2010-01-16 20:52:59 +00002400 // Determine whether this is a non-nested class. Note that local
2401 // classes are *not* considered to be nested classes.
2402 bool NonNestedClass = true;
2403 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002404 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002405 if (S->isClassScope()) {
2406 // We're inside a class scope, so this is a nested class.
2407 NonNestedClass = false;
John McCalle402e722012-09-25 07:32:39 +00002408
2409 // The Microsoft extension __interface does not permit nested classes.
2410 if (getCurrentClass().IsInterface) {
2411 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2412 << /*ErrorType=*/6
2413 << (isa<NamedDecl>(TagDecl)
2414 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2415 : "<anonymous>");
2416 }
Douglas Gregor26997fd2010-01-16 20:52:59 +00002417 break;
2418 }
2419
2420 if ((S->getFlags() & Scope::FnScope)) {
2421 // If we're in a function or function template declared in the
2422 // body of a class, then this is a local class rather than a
2423 // nested class.
2424 const Scope *Parent = S->getParent();
2425 if (Parent->isTemplateParamScope())
2426 Parent = Parent->getParent();
2427 if (Parent->isClassScope())
2428 break;
2429 }
2430 }
2431 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002432
2433 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002434 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002435
Douglas Gregor6569d682009-05-27 23:11:45 +00002436 // Note that we are parsing a new (potentially-nested) class definition.
John McCalle402e722012-09-25 07:32:39 +00002437 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2438 TagType == DeclSpec::TST_interface);
Douglas Gregor6569d682009-05-27 23:11:45 +00002439
Douglas Gregorddc29e12009-02-06 22:42:48 +00002440 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002441 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002442
Anders Carlssonb184a182011-03-25 14:46:08 +00002443 SourceLocation FinalLoc;
2444
2445 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002446 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00002447 assert(isCXX11FinalKeyword() && "not a class definition");
Richard Smith8b11b5e2011-10-15 04:21:46 +00002448 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002449
John McCalle402e722012-09-25 07:32:39 +00002450 if (TagType == DeclSpec::TST_interface) {
2451 Diag(FinalLoc, diag::err_override_control_interface)
2452 << "final";
2453 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00002454 Diag(FinalLoc, getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00002455 diag::warn_cxx98_compat_override_control_keyword :
2456 diag::ext_override_control_keyword) << "final";
2457 }
Michael Han2e397132012-11-26 22:54:45 +00002458
Michael Han07fc1ba2013-01-07 16:57:11 +00002459 // Parse any C++11 attributes after 'final' keyword.
2460 // These attributes are not allowed to appear here,
2461 // and the only possible place for them to appertain
2462 // to the class would be between class-key and class-name.
Richard Smith05321402013-02-19 23:47:15 +00002463 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonb184a182011-03-25 14:46:08 +00002464 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002465
John McCallbd0dfa52009-12-19 21:48:58 +00002466 if (Tok.is(tok::colon)) {
2467 ParseBaseClause(TagDecl);
2468
2469 if (!Tok.is(tok::l_brace)) {
2470 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002471
2472 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002473 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002474 return;
2475 }
2476 }
2477
2478 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002479 BalancedDelimiterTracker T(*this, tok::l_brace);
2480 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002481
John McCall42a4f662010-05-28 08:11:17 +00002482 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002483 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002484 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002485
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002486 // C++ 11p3: Members of a class defined with the keyword class are private
2487 // by default. Members of a class defined with the keywords struct or union
2488 // are public by default.
2489 AccessSpecifier CurAS;
2490 if (TagType == DeclSpec::TST_class)
2491 CurAS = AS_private;
2492 else
2493 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002494 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002495
Douglas Gregor07976d22010-06-21 22:31:09 +00002496 if (TagDecl) {
2497 // While we still have something to read, read the member-declarations.
2498 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2499 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002500
David Blaikie4e4d0842012-03-11 07:00:24 +00002501 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002502 Tok.is(tok::kw___if_not_exists))) {
2503 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2504 continue;
2505 }
2506
Douglas Gregor07976d22010-06-21 22:31:09 +00002507 // Check for extraneous top-level semicolon.
2508 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002509 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002510 continue;
2511 }
2512
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002513 if (Tok.is(tok::annot_pragma_vis)) {
2514 HandlePragmaVisibility();
2515 continue;
2516 }
2517
2518 if (Tok.is(tok::annot_pragma_pack)) {
2519 HandlePragmaPack();
2520 continue;
2521 }
2522
Argyrios Kyrtzidisf4deaef2012-10-12 17:39:59 +00002523 if (Tok.is(tok::annot_pragma_align)) {
2524 HandlePragmaAlign();
2525 continue;
2526 }
2527
Alexey Bataevc6400582013-03-22 06:34:35 +00002528 if (Tok.is(tok::annot_pragma_openmp)) {
2529 ParseOpenMPDeclarativeDirective();
2530 continue;
2531 }
2532
Douglas Gregor07976d22010-06-21 22:31:09 +00002533 AccessSpecifier AS = getAccessSpecifierIfPresent();
2534 if (AS != AS_none) {
2535 // Current token is a C++ access specifier.
2536 CurAS = AS;
2537 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002538 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002539 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002540 AccessAttrs.clear();
2541 MaybeParseGNUAttributes(AccessAttrs);
2542
David Blaikie13f8daf2011-10-13 06:08:43 +00002543 SourceLocation EndLoc;
2544 if (Tok.is(tok::colon)) {
2545 EndLoc = Tok.getLocation();
2546 ConsumeToken();
2547 } else if (Tok.is(tok::semi)) {
2548 EndLoc = Tok.getLocation();
2549 ConsumeToken();
2550 Diag(EndLoc, diag::err_expected_colon)
2551 << FixItHint::CreateReplacement(EndLoc, ":");
2552 } else {
2553 EndLoc = ASLoc.getLocWithOffset(TokLength);
2554 Diag(EndLoc, diag::err_expected_colon)
2555 << FixItHint::CreateInsertion(EndLoc, ":");
2556 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002557
John McCalle402e722012-09-25 07:32:39 +00002558 // The Microsoft extension __interface does not permit non-public
2559 // access specifiers.
2560 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2561 Diag(ASLoc, diag::err_access_specifier_interface)
2562 << (CurAS == AS_protected);
2563 }
2564
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002565 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2566 AccessAttrs.getList())) {
2567 // found another attribute than only annotations
2568 AccessAttrs.clear();
2569 }
2570
Douglas Gregor07976d22010-06-21 22:31:09 +00002571 continue;
2572 }
2573
2574 // FIXME: Make sure we don't have a template here.
2575
2576 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002577 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002578 }
2579
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002580 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002581 } else {
2582 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002583 }
Mike Stump1eb44332009-09-09 15:08:12 +00002584
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002585 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002586 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002587 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002588
John McCall42a4f662010-05-28 08:11:17 +00002589 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002590 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002591 T.getOpenLocation(),
2592 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002593 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002594
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002595 // C++11 [class.mem]p2:
2596 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002597 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002598 // brace-or-equal-initializers for non-static data members (including such
2599 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002600 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002601 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002602 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002603 // declarations and the lexed inline method definitions, along with any
2604 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002605 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002606 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002607 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002608
2609 // We've finished with all pending member declarations.
2610 Actions.ActOnFinishCXXMemberDecls();
2611
Richard Smith7a614d82011-06-11 17:19:42 +00002612 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002613 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002614 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002615 }
2616
John McCall42a4f662010-05-28 08:11:17 +00002617 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002618 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2619 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002620
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002621 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002622 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002623 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002624}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002625
2626/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2627/// which explicitly initializes the members or base classes of a
2628/// class (C++ [class.base.init]). For example, the three initializers
2629/// after the ':' in the Derived constructor below:
2630///
2631/// @code
2632/// class Base { };
2633/// class Derived : Base {
2634/// int x;
2635/// float f;
2636/// public:
2637/// Derived(float f) : Base(), x(17), f(f) { }
2638/// };
2639/// @endcode
2640///
Mike Stump1eb44332009-09-09 15:08:12 +00002641/// [C++] ctor-initializer:
2642/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002643///
Mike Stump1eb44332009-09-09 15:08:12 +00002644/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002645/// mem-initializer ...[opt]
2646/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002647void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002648 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2649
John Wiegley28bbe4b2011-04-28 01:08:34 +00002650 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2651 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002652 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002653
Chris Lattner5f9e2722011-07-23 10:55:15 +00002654 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002655 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002656
Douglas Gregor7ad83902008-11-05 04:29:56 +00002657 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002658 if (Tok.is(tok::code_completion)) {
2659 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2660 MemInitializers.data(),
2661 MemInitializers.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002662 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002663 } else {
2664 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2665 if (!MemInit.isInvalid())
2666 MemInitializers.push_back(MemInit.get());
2667 else
2668 AnyErrors = true;
2669 }
2670
Douglas Gregor7ad83902008-11-05 04:29:56 +00002671 if (Tok.is(tok::comma))
2672 ConsumeToken();
2673 else if (Tok.is(tok::l_brace))
2674 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002675 // If the next token looks like a base or member initializer, assume that
2676 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002677 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2678 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2679 Diag(Loc, diag::err_ctor_init_missing_comma)
2680 << FixItHint::CreateInsertion(Loc, ", ");
2681 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002682 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002683 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002684 SkipUntil(tok::l_brace, true, true);
2685 break;
2686 }
2687 } while (true);
2688
David Blaikie93c86172013-01-17 05:26:25 +00002689 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002690 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002691}
2692
2693/// ParseMemInitializer - Parse a C++ member initializer, which is
2694/// part of a constructor initializer that explicitly initializes one
2695/// member or base class (C++ [class.base.init]). See
2696/// ParseConstructorInitializer for an example.
2697///
2698/// [C++] mem-initializer:
2699/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002700/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002701///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002702/// [C++] mem-initializer-id:
2703/// '::'[opt] nested-name-specifier[opt] class-name
2704/// identifier
John McCalld226f652010-08-21 09:40:31 +00002705Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002706 // parse '::'[opt] nested-name-specifier[opt]
2707 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002708 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002709 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002710 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002711 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002712 if (TemplateId->Kind == TNK_Type_template ||
2713 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002714 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002715 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002716 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002717 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002718 }
David Blaikief2116622012-01-24 06:03:59 +00002719 // Uses of decltype will already have been converted to annot_decltype by
2720 // ParseOptionalCXXScopeSpecifier at this point.
2721 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2722 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002723 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002724 return true;
2725 }
Mike Stump1eb44332009-09-09 15:08:12 +00002726
David Blaikief2116622012-01-24 06:03:59 +00002727 IdentifierInfo *II = 0;
2728 DeclSpec DS(AttrFactory);
2729 SourceLocation IdLoc = Tok.getLocation();
2730 if (Tok.is(tok::annot_decltype)) {
2731 // Get the decltype expression, if there is one.
2732 ParseDecltypeSpecifier(DS);
2733 } else {
2734 if (Tok.is(tok::identifier))
2735 // Get the identifier. This may be a member name or a class name,
2736 // but we'll let the semantic analysis determine which it is.
2737 II = Tok.getIdentifierInfo();
2738 ConsumeToken();
2739 }
2740
Douglas Gregor7ad83902008-11-05 04:29:56 +00002741
2742 // Parse the '('.
Richard Smith80ad52f2013-01-02 11:42:31 +00002743 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002744 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2745
Sebastian Redl6df65482011-09-24 17:48:25 +00002746 ExprResult InitList = ParseBraceInitializer();
2747 if (InitList.isInvalid())
2748 return true;
2749
2750 SourceLocation EllipsisLoc;
2751 if (Tok.is(tok::ellipsis))
2752 EllipsisLoc = ConsumeToken();
2753
2754 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002755 TemplateTypeTy, DS, IdLoc,
2756 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002757 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002758 BalancedDelimiterTracker T(*this, tok::l_paren);
2759 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002760
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002761 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002762 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002763 CommaLocsTy CommaLocs;
2764 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2765 SkipUntil(tok::r_paren);
2766 return true;
2767 }
2768
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002769 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002770
2771 SourceLocation EllipsisLoc;
2772 if (Tok.is(tok::ellipsis))
2773 EllipsisLoc = ConsumeToken();
2774
2775 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002776 TemplateTypeTy, DS, IdLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002777 T.getOpenLocation(), ArgExprs.data(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002778 ArgExprs.size(), T.getCloseLocation(),
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002779 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002780 }
2781
Richard Smith80ad52f2013-01-02 11:42:31 +00002782 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002783 : diag::err_expected_lparen);
2784 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002785}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002786
Sebastian Redl7acafd02011-03-05 14:45:16 +00002787/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002788///
Douglas Gregora4745612008-12-01 18:00:20 +00002789/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002790/// dynamic-exception-specification
2791/// noexcept-specification
2792///
2793/// noexcept-specification:
2794/// 'noexcept'
2795/// 'noexcept' '(' constant-expression ')'
2796ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002797Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002798 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002799 SmallVectorImpl<ParsedType> &DynamicExceptions,
2800 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002801 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002802 ExceptionSpecificationType Result = EST_None;
2803
2804 // See if there's a dynamic specification.
2805 if (Tok.is(tok::kw_throw)) {
2806 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2807 DynamicExceptions,
2808 DynamicExceptionRanges);
2809 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2810 "Produced different number of exception types and ranges.");
2811 }
2812
2813 // If there's no noexcept specification, we're done.
2814 if (Tok.isNot(tok::kw_noexcept))
2815 return Result;
2816
Richard Smith841804b2011-10-17 23:06:20 +00002817 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2818
Sebastian Redl7acafd02011-03-05 14:45:16 +00002819 // If we already had a dynamic specification, parse the noexcept for,
2820 // recovery, but emit a diagnostic and don't store the results.
2821 SourceRange NoexceptRange;
2822 ExceptionSpecificationType NoexceptType = EST_None;
2823
2824 SourceLocation KeywordLoc = ConsumeToken();
2825 if (Tok.is(tok::l_paren)) {
2826 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002827 BalancedDelimiterTracker T(*this, tok::l_paren);
2828 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002829 NoexceptType = EST_ComputedNoexcept;
2830 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002831 // The argument must be contextually convertible to bool. We use
2832 // ActOnBooleanCondition for this purpose.
2833 if (!NoexceptExpr.isInvalid())
2834 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2835 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002836 T.consumeClose();
2837 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002838 } else {
2839 // There is no argument.
2840 NoexceptType = EST_BasicNoexcept;
2841 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2842 }
2843
2844 if (Result == EST_None) {
2845 SpecificationRange = NoexceptRange;
2846 Result = NoexceptType;
2847
2848 // If there's a dynamic specification after a noexcept specification,
2849 // parse that and ignore the results.
2850 if (Tok.is(tok::kw_throw)) {
2851 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2852 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2853 DynamicExceptionRanges);
2854 }
2855 } else {
2856 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2857 }
2858
2859 return Result;
2860}
2861
2862/// ParseDynamicExceptionSpecification - Parse a C++
2863/// dynamic-exception-specification (C++ [except.spec]).
2864///
2865/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002866/// 'throw' '(' type-id-list [opt] ')'
2867/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002868///
Douglas Gregora4745612008-12-01 18:00:20 +00002869/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002870/// type-id ... [opt]
2871/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002872///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002873ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2874 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002875 SmallVectorImpl<ParsedType> &Exceptions,
2876 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002877 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002878
Sebastian Redl7acafd02011-03-05 14:45:16 +00002879 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002880 BalancedDelimiterTracker T(*this, tok::l_paren);
2881 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002882 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2883 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002884 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002885 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002886
Douglas Gregora4745612008-12-01 18:00:20 +00002887 // Parse throw(...), a Microsoft extension that means "this function
2888 // can throw anything".
2889 if (Tok.is(tok::ellipsis)) {
2890 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002891 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002892 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002893 T.consumeClose();
2894 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002895 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002896 }
2897
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002898 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002899 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002900 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002901 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002902
Douglas Gregora04426c2010-12-20 23:57:46 +00002903 if (Tok.is(tok::ellipsis)) {
2904 // C++0x [temp.variadic]p5:
2905 // - In a dynamic-exception-specification (15.4); the pattern is a
2906 // type-id.
2907 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002908 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002909 if (!Res.isInvalid())
2910 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2911 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002912
Sebastian Redlef65f062009-05-29 18:02:33 +00002913 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002914 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002915 Ranges.push_back(Range);
2916 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002917
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002918 if (Tok.is(tok::comma))
2919 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002920 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002921 break;
2922 }
2923
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002924 T.consumeClose();
2925 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002926 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002927}
Douglas Gregor6569d682009-05-27 23:11:45 +00002928
Douglas Gregordab60ad2010-10-01 18:44:50 +00002929/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2930/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002931TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002932 assert(Tok.is(tok::arrow) && "expected arrow");
2933
2934 ConsumeToken();
2935
Richard Smith7796eb52012-03-12 08:56:40 +00002936 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002937}
2938
Douglas Gregor6569d682009-05-27 23:11:45 +00002939/// \brief We have just started parsing the definition of a new class,
2940/// so push that class onto our stack of classes that is currently
2941/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002942Sema::ParsingClassState
John McCalle402e722012-09-25 07:32:39 +00002943Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
2944 bool IsInterface) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002945 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002946 "Nested class without outer class");
John McCalle402e722012-09-25 07:32:39 +00002947 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCalleee1d542011-02-14 07:13:47 +00002948 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002949}
2950
2951/// \brief Deallocate the given parsed class and all of its nested
2952/// classes.
2953void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002954 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2955 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002956 delete Class;
2957}
2958
2959/// \brief Pop the top class of the stack of classes that are
2960/// currently being parsed.
2961///
2962/// This routine should be called when we have finished parsing the
2963/// definition of a class, but have not yet popped the Scope
2964/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00002965void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002966 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002967
John McCalleee1d542011-02-14 07:13:47 +00002968 Actions.PopParsingClass(state);
2969
Douglas Gregor6569d682009-05-27 23:11:45 +00002970 ParsingClass *Victim = ClassStack.top();
2971 ClassStack.pop();
2972 if (Victim->TopLevelClass) {
2973 // Deallocate all of the nested classes of this class,
2974 // recursively: we don't need to keep any of this information.
2975 DeallocateParsedClasses(Victim);
2976 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002977 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002978 assert(!ClassStack.empty() && "Missing top-level class?");
2979
Douglas Gregord54eb442010-10-12 16:25:54 +00002980 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002981 // The victim is a nested class, but we will not need to perform
2982 // any processing after the definition of this class since it has
2983 // no members whose handling was delayed. Therefore, we can just
2984 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002985 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002986 return;
2987 }
2988
2989 // This nested class has some members that will need to be processed
2990 // after the top-level class is completely defined. Therefore, add
2991 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002992 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002993 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002994 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00002995}
Sean Huntbbd37c62009-11-21 08:43:09 +00002996
Richard Smithc56298d2012-04-10 03:25:07 +00002997/// \brief Try to parse an 'identifier' which appears within an attribute-token.
2998///
2999/// \return the parsed identifier on success, and 0 if the next token is not an
3000/// attribute-token.
3001///
3002/// C++11 [dcl.attr.grammar]p3:
3003/// If a keyword or an alternative token that satisfies the syntactic
3004/// requirements of an identifier is contained in an attribute-token,
3005/// it is considered an identifier.
3006IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3007 switch (Tok.getKind()) {
3008 default:
3009 // Identifiers and keywords have identifier info attached.
3010 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3011 Loc = ConsumeToken();
3012 return II;
3013 }
3014 return 0;
3015
3016 case tok::ampamp: // 'and'
3017 case tok::pipe: // 'bitor'
3018 case tok::pipepipe: // 'or'
3019 case tok::caret: // 'xor'
3020 case tok::tilde: // 'compl'
3021 case tok::amp: // 'bitand'
3022 case tok::ampequal: // 'and_eq'
3023 case tok::pipeequal: // 'or_eq'
3024 case tok::caretequal: // 'xor_eq'
3025 case tok::exclaim: // 'not'
3026 case tok::exclaimequal: // 'not_eq'
3027 // Alternative tokens do not have identifier info, but their spelling
3028 // starts with an alphabetical character.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003029 SmallString<8> SpellingBuf;
Richard Smithc56298d2012-04-10 03:25:07 +00003030 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rose3f6f51e2013-02-08 22:30:41 +00003031 if (isLetter(Spelling[0])) {
Richard Smithc56298d2012-04-10 03:25:07 +00003032 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00003033 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00003034 }
3035 return 0;
3036 }
3037}
3038
Michael Han6880f492012-10-03 01:56:22 +00003039static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3040 IdentifierInfo *ScopeName) {
3041 switch (AttributeList::getKind(AttrName, ScopeName,
3042 AttributeList::AS_CXX11)) {
3043 case AttributeList::AT_CarriesDependency:
3044 case AttributeList::AT_FallThrough:
Richard Smithcd8ab512013-01-17 01:30:42 +00003045 case AttributeList::AT_CXX11NoReturn: {
Michael Han6880f492012-10-03 01:56:22 +00003046 return true;
3047 }
3048
3049 default:
3050 return false;
3051 }
3052}
3053
Richard Smithc56298d2012-04-10 03:25:07 +00003054/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003055/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00003056///
Richard Smith6ee326a2012-04-10 01:32:12 +00003057/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00003058/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003059/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00003060///
Richard Smith6ee326a2012-04-10 01:32:12 +00003061/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00003062/// attribute[opt]
3063/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00003064/// attribute '...'
3065/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00003066///
Richard Smith6ee326a2012-04-10 01:32:12 +00003067/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00003068/// attribute-token attribute-argument-clause[opt]
3069///
Richard Smith6ee326a2012-04-10 01:32:12 +00003070/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003071/// identifier
3072/// attribute-scoped-token
3073///
Richard Smith6ee326a2012-04-10 01:32:12 +00003074/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003075/// attribute-namespace '::' identifier
3076///
Richard Smith6ee326a2012-04-10 01:32:12 +00003077/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00003078/// identifier
3079///
Richard Smith6ee326a2012-04-10 01:32:12 +00003080/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00003081/// '(' balanced-token-seq ')'
3082///
Richard Smith6ee326a2012-04-10 01:32:12 +00003083/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00003084/// balanced-token
3085/// balanced-token-seq balanced-token
3086///
Richard Smith6ee326a2012-04-10 01:32:12 +00003087/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003088/// '(' balanced-token-seq ')'
3089/// '[' balanced-token-seq ']'
3090/// '{' balanced-token-seq '}'
3091/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00003092void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003093 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003094 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00003095 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003096 ParseAlignmentSpecifier(attrs, endLoc);
3097 return;
3098 }
3099
Sean Huntbbd37c62009-11-21 08:43:09 +00003100 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00003101 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00003102
Richard Smith41be6732011-10-14 20:48:27 +00003103 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3104
Sean Huntbbd37c62009-11-21 08:43:09 +00003105 ConsumeBracket();
3106 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003107
Richard Smithcd8ab512013-01-17 01:30:42 +00003108 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3109
Richard Smithc56298d2012-04-10 03:25:07 +00003110 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003111 // attribute not present
3112 if (Tok.is(tok::comma)) {
3113 ConsumeToken();
3114 continue;
3115 }
3116
Richard Smithc56298d2012-04-10 03:25:07 +00003117 SourceLocation ScopeLoc, AttrLoc;
3118 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3119
3120 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3121 if (!AttrName)
3122 // Break out to the "expected ']'" diagnostic.
3123 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003124
Sean Huntbbd37c62009-11-21 08:43:09 +00003125 // scoped attribute
3126 if (Tok.is(tok::coloncolon)) {
3127 ConsumeToken();
3128
Richard Smithc56298d2012-04-10 03:25:07 +00003129 ScopeName = AttrName;
3130 ScopeLoc = AttrLoc;
3131
3132 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3133 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003134 Diag(Tok.getLocation(), diag::err_expected_ident);
3135 SkipUntil(tok::r_square, tok::comma, true, true);
3136 continue;
3137 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003138 }
3139
Michael Han6880f492012-10-03 01:56:22 +00003140 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Sean Huntbbd37c62009-11-21 08:43:09 +00003141 bool AttrParsed = false;
Sean Huntbbd37c62009-11-21 08:43:09 +00003142
Richard Smithcd8ab512013-01-17 01:30:42 +00003143 if (StandardAttr &&
3144 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3145 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3146 << AttrName << SourceRange(SeenAttrs[AttrName]);
3147
Michael Han6880f492012-10-03 01:56:22 +00003148 // Parse attribute arguments
3149 if (Tok.is(tok::l_paren)) {
3150 if (ScopeName && ScopeName->getName() == "gnu") {
3151 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3152 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3153 AttrParsed = true;
3154 } else {
3155 if (StandardAttr)
3156 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3157 << AttrName->getName();
3158
3159 // FIXME: handle other formats of c++11 attribute arguments
3160 ConsumeParen();
3161 SkipUntil(tok::r_paren, false);
3162 }
3163 }
3164
3165 if (!AttrParsed)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00003166 attrs.addNew(AttrName,
3167 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3168 AttrLoc),
3169 ScopeName, ScopeLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00003170 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smith6ee326a2012-04-10 01:32:12 +00003171
Richard Smithc56298d2012-04-10 03:25:07 +00003172 if (Tok.is(tok::ellipsis)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003173 ConsumeToken();
Michael Han6880f492012-10-03 01:56:22 +00003174
3175 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3176 << AttrName->getName();
Richard Smithc56298d2012-04-10 03:25:07 +00003177 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003178 }
3179
3180 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3181 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003182 if (endLoc)
3183 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00003184 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3185 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003186}
Sean Huntbbd37c62009-11-21 08:43:09 +00003187
Sean Hunt2edf0a22012-06-23 05:07:58 +00003188/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003189///
3190/// attribute-specifier-seq:
3191/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00003192void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003193 SourceLocation *endLoc) {
Richard Smith672edb02013-02-22 09:15:49 +00003194 assert(getLangOpts().CPlusPlus11);
3195
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003196 SourceLocation StartLoc = Tok.getLocation(), Loc;
3197 if (!endLoc)
3198 endLoc = &Loc;
3199
Douglas Gregor8828ee72011-10-07 20:35:25 +00003200 do {
Richard Smithc56298d2012-04-10 03:25:07 +00003201 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003202 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003203
3204 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003205}
3206
Francois Pichet334d47e2010-10-11 12:59:39 +00003207/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3208///
3209/// [MS] ms-attribute:
3210/// '[' token-seq ']'
3211///
3212/// [MS] ms-attribute-seq:
3213/// ms-attribute[opt]
3214/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003215void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3216 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003217 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3218
3219 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003220 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003221 ConsumeBracket();
3222 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003223 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003224 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3225 }
3226}
Francois Pichet563a6452011-05-25 10:19:49 +00003227
3228void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3229 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003230 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003231 if (ParseMicrosoftIfExistsCondition(Result))
3232 return;
3233
Douglas Gregor3896fc52011-10-24 22:31:10 +00003234 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3235 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003236 Diag(Tok, diag::err_expected_lbrace);
3237 return;
3238 }
Francois Pichet563a6452011-05-25 10:19:49 +00003239
Douglas Gregor3896fc52011-10-24 22:31:10 +00003240 switch (Result.Behavior) {
3241 case IEB_Parse:
3242 // Parse the declarations below.
3243 break;
3244
3245 case IEB_Dependent:
3246 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3247 << Result.IsIfExists;
3248 // Fall through to skip.
3249
3250 case IEB_Skip:
3251 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003252 return;
3253 }
3254
Douglas Gregor3896fc52011-10-24 22:31:10 +00003255 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003256 // __if_exists, __if_not_exists can nest.
3257 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3258 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3259 continue;
3260 }
3261
3262 // Check for extraneous top-level semicolon.
3263 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003264 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003265 continue;
3266 }
3267
3268 AccessSpecifier AS = getAccessSpecifierIfPresent();
3269 if (AS != AS_none) {
3270 // Current token is a C++ access specifier.
3271 CurAS = AS;
3272 SourceLocation ASLoc = Tok.getLocation();
3273 ConsumeToken();
3274 if (Tok.is(tok::colon))
3275 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3276 else
3277 Diag(Tok, diag::err_expected_colon);
3278 ConsumeToken();
3279 continue;
3280 }
3281
3282 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003283 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003284 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003285
3286 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003287}