blob: 1a1eeb94029d7021deb8f8703c2bbb516d23671f [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor1b7f8982008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/OperatorKinds.h"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
John McCall19510852010-08-20 18:27:03 +000020#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Sema/Scope.h"
John McCalle402e722012-09-25 07:32:39 +000023#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000025using namespace clang;
26
27/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000028/// may either be a top level namespace or a block-level namespace alias. If
29/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000030///
31/// namespace-definition: [C++ 7.3: basic.namespace]
32/// named-namespace-definition
33/// unnamed-namespace-definition
34///
35/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000036/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000037///
38/// named-namespace-definition:
39/// original-namespace-definition
40/// extension-namespace-definition
41///
42/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000043/// 'inline'[opt] 'namespace' identifier attributes[opt]
44/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000045///
46/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000047/// 'inline'[opt] 'namespace' original-namespace-name
48/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000049///
Chris Lattner8f08cb72007-08-25 06:57:03 +000050/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
51/// 'namespace' identifier '=' qualified-namespace-specifier ';'
52///
John McCalld226f652010-08-21 09:40:31 +000053Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000054 SourceLocation &DeclEnd,
55 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000056 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000057 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +000058 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000059
Douglas Gregor49f40bd2009-09-18 19:03:04 +000060 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000061 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000062 cutOffParsing();
63 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +000064 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000065
Chris Lattner8f08cb72007-08-25 06:57:03 +000066 SourceLocation IdentLoc;
67 IdentifierInfo *Ident = 0;
Richard Trieuf858bd82011-05-26 20:11:09 +000068 std::vector<SourceLocation> ExtraIdentLoc;
69 std::vector<IdentifierInfo*> ExtraIdent;
70 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000071
72 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattner04d66662007-10-09 17:33:22 +000074 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000075 Ident = Tok.getIdentifierInfo();
76 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieuf858bd82011-05-26 20:11:09 +000077 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
78 ExtraNamespaceLoc.push_back(ConsumeToken());
79 ExtraIdent.push_back(Tok.getIdentifierInfo());
80 ExtraIdentLoc.push_back(ConsumeToken());
81 }
Chris Lattner8f08cb72007-08-25 06:57:03 +000082 }
Mike Stump1eb44332009-09-09 15:08:12 +000083
Chris Lattner8f08cb72007-08-25 06:57:03 +000084 // Read label attributes, if present.
John McCall0b7e6782011-03-24 11:26:52 +000085 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000086 if (Tok.is(tok::kw___attribute)) {
87 attrTok = Tok;
John McCall7f040a92010-12-24 02:08:15 +000088 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000089 }
Mike Stump1eb44332009-09-09 15:08:12 +000090
Douglas Gregor6a588dd2009-06-17 19:49:00 +000091 if (Tok.is(tok::equal)) {
Nico Webere1bb3292012-10-27 23:44:27 +000092 if (Ident == 0) {
93 Diag(Tok, diag::err_expected_ident);
94 // Skip to end of the definition and eat the ';'.
95 SkipUntil(tok::semi);
96 return 0;
97 }
John McCall7f040a92010-12-24 02:08:15 +000098 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +000099 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +0000100 if (InlineLoc.isValid())
101 Diag(InlineLoc, diag::err_inline_namespace_alias)
102 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000103 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000104 }
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Richard Trieuf858bd82011-05-26 20:11:09 +0000106
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000107 BalancedDelimiterTracker T(*this, tok::l_brace);
108 if (T.consumeOpen()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000109 if (!ExtraIdent.empty()) {
110 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
111 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
112 }
Mike Stump1eb44332009-09-09 15:08:12 +0000113 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +0000114 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +0000115 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Douglas Gregor23c94db2010-07-02 17:43:08 +0000118 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
119 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
120 getCurScope()->getFnParent()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000121 if (!ExtraIdent.empty()) {
122 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
123 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
124 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000125 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Douglas Gregor95f1b152010-05-14 05:08:22 +0000126 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000127 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000128 }
129
Richard Trieuf858bd82011-05-26 20:11:09 +0000130 if (!ExtraIdent.empty()) {
131 TentativeParsingAction TPA(*this);
132 SkipUntil(tok::r_brace, /*StopAtSemi*/false, /*DontConsume*/true);
133 Token rBraceToken = Tok;
134 TPA.Revert();
135
136 if (!rBraceToken.is(tok::r_brace)) {
137 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
138 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
139 } else {
Benjamin Kramer9910df02011-05-26 21:32:30 +0000140 std::string NamespaceFix;
Richard Trieuf858bd82011-05-26 20:11:09 +0000141 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
142 E = ExtraIdent.end(); I != E; ++I) {
143 NamespaceFix += " { namespace ";
144 NamespaceFix += (*I)->getName();
145 }
Benjamin Kramer9910df02011-05-26 21:32:30 +0000146
Richard Trieuf858bd82011-05-26 20:11:09 +0000147 std::string RBraces;
Benjamin Kramer9910df02011-05-26 21:32:30 +0000148 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieuf858bd82011-05-26 20:11:09 +0000149 RBraces += "} ";
Benjamin Kramer9910df02011-05-26 21:32:30 +0000150
Richard Trieuf858bd82011-05-26 20:11:09 +0000151 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
152 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
153 ExtraIdentLoc.back()),
154 NamespaceFix)
155 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
156 }
157 }
158
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000159 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith7fe62082011-10-15 05:09:34 +0000160 if (InlineLoc.isValid())
Richard Smith80ad52f2013-01-02 11:42:31 +0000161 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000162 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000163
Chris Lattner51448322009-03-29 14:02:43 +0000164 // Enter a scope for the namespace.
165 ParseScope NamespaceScope(this, Scope::DeclScope);
166
John McCalld226f652010-08-21 09:40:31 +0000167 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000168 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000169 IdentLoc, Ident, T.getOpenLocation(),
170 attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000171
John McCallf312b1e2010-08-26 23:41:50 +0000172 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
173 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Richard Trieuf858bd82011-05-26 20:11:09 +0000175 // Parse the contents of the namespace. This includes parsing recovery on
176 // any improperly nested namespaces.
177 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000178 InlineLoc, attrs, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattner51448322009-03-29 14:02:43 +0000180 // Leave the namespace scope.
181 NamespaceScope.Exit();
182
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000183 DeclEnd = T.getCloseLocation();
184 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner51448322009-03-29 14:02:43 +0000185
186 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000187}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000188
Richard Trieuf858bd82011-05-26 20:11:09 +0000189/// ParseInnerNamespace - Parse the contents of a namespace.
190void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
191 std::vector<IdentifierInfo*>& Ident,
192 std::vector<SourceLocation>& NamespaceLoc,
193 unsigned int index, SourceLocation& InlineLoc,
Richard Trieuf858bd82011-05-26 20:11:09 +0000194 ParsedAttributes& attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000195 BalancedDelimiterTracker &Tracker) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000196 if (index == Ident.size()) {
197 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
198 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000199 MaybeParseCXX11Attributes(attrs);
Richard Trieuf858bd82011-05-26 20:11:09 +0000200 MaybeParseMicrosoftAttributes(attrs);
201 ParseExternalDeclaration(attrs);
202 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000203
204 // The caller is what called check -- we are simply calling
205 // the close for it.
206 Tracker.consumeClose();
Richard Trieuf858bd82011-05-26 20:11:09 +0000207
208 return;
209 }
210
211 // Parse improperly nested namespaces.
212 ParseScope NamespaceScope(this, Scope::DeclScope);
213 Decl *NamespcDecl =
214 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
215 NamespaceLoc[index], IdentLoc[index],
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000216 Ident[index], Tracker.getOpenLocation(),
217 attrs.getList());
Richard Trieuf858bd82011-05-26 20:11:09 +0000218
219 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000220 attrs, Tracker);
Richard Trieuf858bd82011-05-26 20:11:09 +0000221
222 NamespaceScope.Exit();
223
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000224 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieuf858bd82011-05-26 20:11:09 +0000225}
226
Anders Carlssonf67606a2009-03-28 04:07:16 +0000227/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
228/// alias definition.
229///
John McCalld226f652010-08-21 09:40:31 +0000230Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000231 SourceLocation AliasLoc,
232 IdentifierInfo *Alias,
233 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000234 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Anders Carlssonf67606a2009-03-28 04:07:16 +0000236 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000238 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000239 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000240 cutOffParsing();
241 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000242 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000243
Anders Carlssonf67606a2009-03-28 04:07:16 +0000244 CXXScopeSpec SS;
245 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000246 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000247
248 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
249 Diag(Tok, diag::err_expected_namespace_name);
250 // Skip to end of the definition and eat the ';'.
251 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000252 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000253 }
254
255 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000256 IdentifierInfo *Ident = Tok.getIdentifierInfo();
257 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Anders Carlssonf67606a2009-03-28 04:07:16 +0000259 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000260 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000261 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
262 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Douglas Gregor23c94db2010-07-02 17:43:08 +0000264 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000265 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000266}
267
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000268/// ParseLinkage - We know that the current token is a string_literal
269/// and just before that, that extern was seen.
270///
271/// linkage-specification: [C++ 7.5p2: dcl.link]
272/// 'extern' string-literal '{' declaration-seq[opt] '}'
273/// 'extern' string-literal declaration
274///
Chris Lattner7d642712010-11-09 20:15:55 +0000275Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000276 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000277 SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000278 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000279 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000280 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000281 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000282
Richard Smith99831e42012-03-06 03:21:47 +0000283 // FIXME: This is incorrect: linkage-specifiers are parsed in translation
284 // phase 7, so string-literal concatenation is supposed to occur.
285 // extern "" "C" "" "+" "+" { } is legal.
286 if (Tok.hasUDSuffix())
287 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000288 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000289
Douglas Gregor074149e2009-01-05 19:45:36 +0000290 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000291 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000292 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000293 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000294 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000295 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000296 : SourceLocation());
297
John McCall0b7e6782011-03-24 11:26:52 +0000298 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000299 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000300 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000301
Douglas Gregor074149e2009-01-05 19:45:36 +0000302 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-05-01 16:25:54 +0000303 // Reset the source range in DS, as the leading "extern"
304 // does not really belong to the inner declaration ...
305 DS.SetRangeStart(SourceLocation());
306 DS.SetRangeEnd(SourceLocation());
307 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000308 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000309 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000310 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000311 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000312 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000313
Douglas Gregor63a01132010-02-07 08:38:28 +0000314 DS.abort();
315
John McCall7f040a92010-12-24 02:08:15 +0000316 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000317
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000318 BalancedDelimiterTracker T(*this, tok::l_brace);
319 T.consumeOpen();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000320 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000321 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000322 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000323 MaybeParseMicrosoftAttributes(attrs);
324 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000325 }
326
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000327 T.consumeClose();
Chris Lattner7d642712010-11-09 20:15:55 +0000328 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000329 T.getCloseLocation());
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000330}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000331
Douglas Gregorf780abc2008-12-30 03:27:21 +0000332/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
333/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000334Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000335 const ParsedTemplateInfo &TemplateInfo,
336 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000337 ParsedAttributesWithRange &attrs,
338 Decl **OwnedType) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000339 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000340 ObjCDeclContextSwitch ObjCDC(*this);
341
Douglas Gregorf780abc2008-12-30 03:27:21 +0000342 // Eat 'using'.
343 SourceLocation UsingLoc = ConsumeToken();
344
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000345 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000346 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000347 cutOffParsing();
348 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000349 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000350
John McCall78b81052010-11-10 02:40:36 +0000351 // 'using namespace' means this is a using-directive.
352 if (Tok.is(tok::kw_namespace)) {
353 // Template parameters are always an error here.
354 if (TemplateInfo.Kind) {
355 SourceRange R = TemplateInfo.getSourceRange();
356 Diag(UsingLoc, diag::err_templated_using_directive)
357 << R << FixItHint::CreateRemoval(R);
358 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000359
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000360 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000361 }
362
Richard Smith162e1c12011-04-15 14:24:37 +0000363 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000364
365 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000366 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000367
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000368 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000369 AS_none, OwnedType);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000370}
371
372/// ParseUsingDirective - Parse C++ using-directive, assumes
373/// that current token is 'namespace' and 'using' was already parsed.
374///
375/// using-directive: [C++ 7.3.p4: namespace.udir]
376/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
377/// namespace-name ;
378/// [GNU] using-directive:
379/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
380/// namespace-name attributes[opt] ;
381///
John McCalld226f652010-08-21 09:40:31 +0000382Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000383 SourceLocation UsingLoc,
384 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000385 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000386 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
387
388 // Eat 'namespace'.
389 SourceLocation NamespcLoc = ConsumeToken();
390
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000391 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000392 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000393 cutOffParsing();
394 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000395 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000396
Douglas Gregorf780abc2008-12-30 03:27:21 +0000397 CXXScopeSpec SS;
398 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000399 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000400
Douglas Gregorf780abc2008-12-30 03:27:21 +0000401 IdentifierInfo *NamespcName = 0;
402 SourceLocation IdentLoc = SourceLocation();
403
404 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000405 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000406 Diag(Tok, diag::err_expected_namespace_name);
407 // If there was invalid namespace name, skip to end of decl, and eat ';'.
408 SkipUntil(tok::semi);
409 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000410 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Chris Lattner823c44e2009-01-06 07:27:21 +0000413 // Parse identifier.
414 NamespcName = Tok.getIdentifierInfo();
415 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner823c44e2009-01-06 07:27:21 +0000417 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000418 bool GNUAttr = false;
419 if (Tok.is(tok::kw___attribute)) {
420 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000421 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattner823c44e2009-01-06 07:27:21 +0000424 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000425 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000426 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000427 GNUAttr ? diag::err_expected_semi_after_attribute_list
428 : diag::err_expected_semi_after_namespace_name,
429 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000430
Douglas Gregor23c94db2010-07-02 17:43:08 +0000431 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000432 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000433}
434
Richard Smith162e1c12011-04-15 14:24:37 +0000435/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
436/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000437///
438/// using-declaration: [C++ 7.3.p3: namespace.udecl]
439/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000440/// unqualified-id
441/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000442///
Richard Smithd03de6a2013-01-29 10:02:16 +0000443/// alias-declaration: C++11 [dcl.dcl]p1
444/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smith162e1c12011-04-15 14:24:37 +0000445///
John McCalld226f652010-08-21 09:40:31 +0000446Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000447 const ParsedTemplateInfo &TemplateInfo,
448 SourceLocation UsingLoc,
449 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000450 AccessSpecifier AS,
451 Decl **OwnedType) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000452 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000453 SourceLocation TypenameLoc;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000454 bool IsTypeName;
Sean Hunt2edf0a22012-06-23 05:07:58 +0000455 ParsedAttributesWithRange attrs(AttrFactory);
456
457 // FIXME: Simply skip the attributes and diagnose, don't bother parsing them.
Richard Smith4e24f0f2013-01-02 12:01:23 +0000458 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000459 ProhibitAttributes(attrs);
460 attrs.clear();
461 attrs.Range = SourceRange();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000462
463 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000464 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000465 if (Tok.is(tok::kw_typename)) {
John McCall7ba107a2009-11-18 02:36:19 +0000466 TypenameLoc = Tok.getLocation();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000467 ConsumeToken();
468 IsTypeName = true;
469 }
470 else
471 IsTypeName = false;
472
473 // Parse nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000474 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000475
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000476 // Check nested-name specifier.
477 if (SS.isInvalid()) {
478 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000479 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000480 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000481
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000482 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000483 // destructor names and allow the action module to diagnose any semantic
484 // errors.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000485 SourceLocation TemplateKWLoc;
Douglas Gregor12c118a2009-11-04 16:30:06 +0000486 UnqualifiedId Name;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000487 if (ParseUnqualifiedId(SS,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000488 /*EnteringContext=*/false,
489 /*AllowDestructorName=*/true,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000490 /*AllowConstructorName=*/true,
John McCallb3d87482010-08-24 05:47:05 +0000491 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000492 TemplateKWLoc,
Douglas Gregor12c118a2009-11-04 16:30:06 +0000493 Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000494 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000495 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000496 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000497
Richard Smith4e24f0f2013-01-02 12:01:23 +0000498 MaybeParseCXX11Attributes(attrs);
Richard Smith162e1c12011-04-15 14:24:37 +0000499
500 // Maybe this is an alias-declaration.
501 bool IsAliasDecl = Tok.is(tok::equal);
502 TypeResult TypeAlias;
503 if (IsAliasDecl) {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000504 // TODO: Attribute support. C++0x attributes may appear before the equals.
505 // Where can GNU attributes appear?
Richard Smith162e1c12011-04-15 14:24:37 +0000506 ConsumeToken();
507
Richard Smith80ad52f2013-01-02 11:42:31 +0000508 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000509 diag::warn_cxx98_compat_alias_declaration :
510 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000511
Richard Smith3e4c6c42011-05-05 21:57:07 +0000512 // Type alias templates cannot be specialized.
513 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000514 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
515 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000516 SpecKind = 0;
517 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
518 SpecKind = 1;
519 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
520 SpecKind = 2;
521 if (SpecKind != -1) {
522 SourceRange Range;
523 if (SpecKind == 0)
524 Range = SourceRange(Name.TemplateId->LAngleLoc,
525 Name.TemplateId->RAngleLoc);
526 else
527 Range = TemplateInfo.getSourceRange();
528 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
529 << SpecKind << Range;
530 SkipUntil(tok::semi);
531 return 0;
532 }
533
Richard Smith162e1c12011-04-15 14:24:37 +0000534 // Name must be an identifier.
535 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
536 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
537 // No removal fixit: can't recover from this.
538 SkipUntil(tok::semi);
539 return 0;
540 } else if (IsTypeName)
541 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
542 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
543 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
544 else if (SS.isNotEmpty())
545 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
546 << FixItHint::CreateRemoval(SS.getRange());
547
Richard Smith3e4c6c42011-05-05 21:57:07 +0000548 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
549 Declarator::AliasTemplateContext :
John McCallcdda47f2011-10-01 09:56:14 +0000550 Declarator::AliasDeclContext, AS, OwnedType);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000551 } else {
552 // C++11 attributes are not allowed on a using-declaration, but GNU ones
553 // are.
554 ProhibitAttributes(attrs);
555
Richard Smith162e1c12011-04-15 14:24:37 +0000556 // Parse (optional) attributes (most likely GNU strong-using extension).
557 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000558 }
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000560 // Eat ';'.
561 DeclEnd = Tok.getLocation();
562 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith162e1c12011-04-15 14:24:37 +0000563 !attrs.empty() ? "attributes list" :
564 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000565 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000566
John McCall78b81052010-11-10 02:40:36 +0000567 // Diagnose an attempt to declare a templated using-declaration.
Richard Smithd03de6a2013-01-29 10:02:16 +0000568 // In C++11, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000569 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000570 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000571 SourceRange R = TemplateInfo.getSourceRange();
572 Diag(UsingLoc, diag::err_templated_using_declaration)
573 << R << FixItHint::CreateRemoval(R);
574
575 // Unfortunately, we have to bail out instead of recovering by
576 // ignoring the parameters, just in case the nested name specifier
577 // depends on the parameters.
578 return 0;
579 }
580
Douglas Gregor480b53c2011-09-26 14:30:28 +0000581 // "typename" keyword is allowed for identifiers only,
582 // because it may be a type definition.
583 if (IsTypeName && Name.getKind() != UnqualifiedId::IK_Identifier) {
584 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
585 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
586 // Proceed parsing, but reset the IsTypeName flag.
587 IsTypeName = false;
588 }
589
Richard Smith3e4c6c42011-05-05 21:57:07 +0000590 if (IsAliasDecl) {
591 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramer5354e772012-08-23 23:38:35 +0000592 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3e4c6c42011-05-05 21:57:07 +0000593 TemplateParams ? TemplateParams->data() : 0,
594 TemplateParams ? TemplateParams->size() : 0);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000595 // FIXME: Propagate attributes.
Richard Smith3e4c6c42011-05-05 21:57:07 +0000596 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
597 UsingLoc, Name, TypeAlias);
598 }
Richard Smith162e1c12011-04-15 14:24:37 +0000599
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000600 return Actions.ActOnUsingDeclaration(getCurScope(), AS, true, UsingLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000601 Name, attrs.getList(),
602 IsTypeName, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000603}
604
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000605/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000606///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000607/// [C++0x] static_assert-declaration:
608/// static_assert ( constant-expression , string-literal ) ;
609///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000610/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000611/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000612///
John McCalld226f652010-08-21 09:40:31 +0000613Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000614 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
615 "Not a static_assert declaration");
616
David Blaikie4e4d0842012-03-11 07:00:24 +0000617 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000618 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000619 if (Tok.is(tok::kw_static_assert))
620 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000621
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000622 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000624 BalancedDelimiterTracker T(*this, tok::l_paren);
625 if (T.consumeOpen()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000626 Diag(Tok, diag::err_expected_lparen);
Richard Smith3686c712012-09-13 19:12:50 +0000627 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000628 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000629 }
Mike Stump1eb44332009-09-09 15:08:12 +0000630
John McCall60d7b3a2010-08-24 06:29:42 +0000631 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000632 if (AssertExpr.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000633 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000634 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000635 }
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Anders Carlssonad5f9602009-03-13 23:29:20 +0000637 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000638 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000639
Richard Smith0cc323c2012-03-05 23:20:05 +0000640 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000641 Diag(Tok, diag::err_expected_string_literal)
642 << /*Source='static_assert'*/1;
Richard Smith3686c712012-09-13 19:12:50 +0000643 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000644 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000645 }
Mike Stump1eb44332009-09-09 15:08:12 +0000646
John McCall60d7b3a2010-08-24 06:29:42 +0000647 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smith99831e42012-03-06 03:21:47 +0000648 if (AssertMessage.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000649 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000650 return 0;
Richard Smith99831e42012-03-06 03:21:47 +0000651 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000652
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000653 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Chris Lattner97144fc2009-04-02 04:16:50 +0000655 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000656 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000657
John McCall9ae2f072010-08-23 23:25:46 +0000658 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
659 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000660 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000661 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000662}
663
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000664/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier.
665///
666/// 'decltype' ( expression )
667///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000668SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
669 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
670 && "Not a decltype specifier");
671
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000672
David Blaikie42d6d0c2011-12-04 05:04:18 +0000673 ExprResult Result;
674 SourceLocation StartLoc = Tok.getLocation();
675 SourceLocation EndLoc;
676
677 if (Tok.is(tok::annot_decltype)) {
678 Result = getExprAnnotation(Tok);
679 EndLoc = Tok.getAnnotationEndLoc();
680 ConsumeToken();
681 if (Result.isInvalid()) {
682 DS.SetTypeSpecError();
683 return EndLoc;
684 }
685 } else {
Richard Smithc7b55432012-02-24 22:30:04 +0000686 if (Tok.getIdentifierInfo()->isStr("decltype"))
687 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000688
David Blaikie42d6d0c2011-12-04 05:04:18 +0000689 ConsumeToken();
690
691 BalancedDelimiterTracker T(*this, tok::l_paren);
692 if (T.expectAndConsume(diag::err_expected_lparen_after,
693 "decltype", tok::r_paren)) {
694 DS.SetTypeSpecError();
695 return T.getOpenLocation() == Tok.getLocation() ?
696 StartLoc : T.getOpenLocation();
697 }
698
699 // Parse the expression
700
701 // C++0x [dcl.type.simple]p4:
702 // The operand of the decltype specifier is an unevaluated operand.
Richard Smith76f3f692012-02-22 02:04:18 +0000703 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
704 0, /*IsDecltype=*/true);
David Blaikie42d6d0c2011-12-04 05:04:18 +0000705 Result = ParseExpression();
706 if (Result.isInvalid()) {
David Blaikie42d6d0c2011-12-04 05:04:18 +0000707 DS.SetTypeSpecError();
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000708 if (SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true)) {
709 EndLoc = ConsumeParen();
710 } else {
Richard Smith569cdc82012-12-09 04:17:57 +0000711 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000712 // Backtrack to get the location of the last token before the semi.
713 PP.RevertCachedTokens(2);
714 ConsumeToken(); // the semi.
715 EndLoc = ConsumeAnyToken();
716 assert(Tok.is(tok::semi));
717 } else {
718 EndLoc = Tok.getLocation();
719 }
720 }
721 return EndLoc;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000722 }
723
724 // Match the ')'
725 T.consumeClose();
726 if (T.getCloseLocation().isInvalid()) {
727 DS.SetTypeSpecError();
728 // FIXME: this should return the location of the last token
729 // that was consumed (by "consumeClose()")
730 return T.getCloseLocation();
731 }
732
Richard Smith76f3f692012-02-22 02:04:18 +0000733 Result = Actions.ActOnDecltypeExpression(Result.take());
734 if (Result.isInvalid()) {
735 DS.SetTypeSpecError();
736 return T.getCloseLocation();
737 }
738
David Blaikie42d6d0c2011-12-04 05:04:18 +0000739 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000740 }
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000742 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000743 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000744 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Mike Stump1eb44332009-09-09 15:08:12 +0000745 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
David Blaikie42d6d0c2011-12-04 05:04:18 +0000746 DiagID, Result.release())) {
John McCallfec54012009-08-03 20:12:06 +0000747 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000748 DS.SetTypeSpecError();
749 }
750 return EndLoc;
751}
752
753void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
754 SourceLocation StartLoc,
755 SourceLocation EndLoc) {
756 // make sure we have a token we can turn into an annotation token
757 if (PP.isBacktrackEnabled())
758 PP.RevertCachedTokens(1);
759 else
760 PP.EnterToken(Tok);
761
762 Tok.setKind(tok::annot_decltype);
763 setExprAnnotation(Tok, DS.getTypeSpecType() == TST_decltype ?
764 DS.getRepAsExpr() : ExprResult());
765 Tok.setAnnotationEndLoc(EndLoc);
766 Tok.setLocation(StartLoc);
767 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000768}
769
Sean Huntdb5d44b2011-05-19 05:37:45 +0000770void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
771 assert(Tok.is(tok::kw___underlying_type) &&
772 "Not an underlying type specifier");
773
774 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000775 BalancedDelimiterTracker T(*this, tok::l_paren);
776 if (T.expectAndConsume(diag::err_expected_lparen_after,
777 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000778 return;
779 }
780
781 TypeResult Result = ParseTypeName();
782 if (Result.isInvalid()) {
783 SkipUntil(tok::r_paren);
784 return;
785 }
786
787 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000788 T.consumeClose();
789 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000790 return;
791
792 const char *PrevSpec = 0;
793 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000794 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000795 DiagID, Result.release()))
796 Diag(StartLoc, DiagID) << PrevSpec;
797}
798
David Blaikie09048df2011-10-25 15:01:20 +0000799/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
800/// class name or decltype-specifier. Note that we only check that the result
801/// names a type; semantic analysis will need to verify that the type names a
802/// class. The result is either a type or null, depending on whether a type
803/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000804///
David Blaikie09048df2011-10-25 15:01:20 +0000805/// base-type-specifier: [C++ 10.1]
806/// class-or-decltype
807/// class-or-decltype: [C++ 10.1]
808/// nested-name-specifier[opt] class-name
809/// decltype-specifier
Douglas Gregor42a552f2008-11-05 20:51:48 +0000810/// class-name: [C++ 9.1]
811/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000812/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000813///
David Blaikie22216eb2011-10-25 17:10:12 +0000814Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
815 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000816 // Ignore attempts to use typename
817 if (Tok.is(tok::kw_typename)) {
818 Diag(Tok, diag::err_expected_class_name_not_template)
819 << FixItHint::CreateRemoval(Tok.getLocation());
820 ConsumeToken();
821 }
822
David Blaikie152aa4b2011-10-25 18:17:58 +0000823 // Parse optional nested-name-specifier
824 CXXScopeSpec SS;
825 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
826
827 BaseLoc = Tok.getLocation();
828
David Blaikie22216eb2011-10-25 17:10:12 +0000829 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000830 // tok == kw_decltype is just error recovery, it can only happen when SS
831 // isn't empty
832 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000833 if (SS.isNotEmpty())
834 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
835 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000836 // Fake up a Declarator to use with ActOnTypeName.
837 DeclSpec DS(AttrFactory);
838
David Blaikieb5777572011-12-08 04:53:15 +0000839 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000840
841 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
842 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
843 }
844
Douglas Gregor7f43d672009-02-25 23:52:28 +0000845 // Check whether we have a template-id that names a type.
846 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000847 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000848 if (TemplateId->Kind == TNK_Type_template ||
849 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000850 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000851
852 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000853 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000854 EndLocation = Tok.getAnnotationEndLoc();
855 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000856
857 if (Type)
858 return Type;
859 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000860 }
861
862 // Fall through to produce an error below.
863 }
864
Douglas Gregor42a552f2008-11-05 20:51:48 +0000865 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000866 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000867 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000868 }
869
Douglas Gregor84d0a192010-01-12 21:28:44 +0000870 IdentifierInfo *Id = Tok.getIdentifierInfo();
871 SourceLocation IdLoc = ConsumeToken();
872
873 if (Tok.is(tok::less)) {
874 // It looks the user intended to write a template-id here, but the
875 // template-name was wrong. Try to fix that.
876 TemplateNameKind TNK = TNK_Type_template;
877 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000878 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000879 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000880 Diag(IdLoc, diag::err_unknown_template_name)
881 << Id;
882 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000883
Douglas Gregor84d0a192010-01-12 21:28:44 +0000884 if (!Template)
885 return true;
886
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000887 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000888 UnqualifiedId TemplateName;
889 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000890
Douglas Gregor84d0a192010-01-12 21:28:44 +0000891 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000892 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
893 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000894 return true;
895 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000896 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000897
Douglas Gregor84d0a192010-01-12 21:28:44 +0000898 // If we didn't end up with a typename token, there's nothing more we
899 // can do.
900 if (Tok.isNot(tok::annot_typename))
901 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000902
Douglas Gregor84d0a192010-01-12 21:28:44 +0000903 // Retrieve the type from the annotation token, consume that token, and
904 // return.
905 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000906 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000907 ConsumeToken();
908 return Type;
909 }
910
Douglas Gregor42a552f2008-11-05 20:51:48 +0000911 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000912 IdentifierInfo *CorrectedII = 0;
Douglas Gregor059101f2011-03-02 00:47:37 +0000913 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000914 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000915 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000916 /*NonTrivialTypeSourceInfo=*/true,
917 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000918 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000919 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000920 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000921 }
922
923 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000924 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000925
926 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000927 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000928 DS.SetRangeStart(IdLoc);
929 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000930 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000931
932 const char *PrevSpec = 0;
933 unsigned DiagID;
934 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
935
936 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
937 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000938}
939
John McCallc052dbb2012-05-22 21:28:12 +0000940void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
941 while (Tok.is(tok::kw___single_inheritance) ||
942 Tok.is(tok::kw___multiple_inheritance) ||
943 Tok.is(tok::kw___virtual_inheritance)) {
944 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
945 SourceLocation AttrNameLoc = ConsumeToken();
946 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000947 SourceLocation(), 0, 0, AttributeList::AS_GNU);
John McCallc052dbb2012-05-22 21:28:12 +0000948 }
949}
950
Richard Smithc9f35172012-06-25 21:37:02 +0000951/// Determine whether the following tokens are valid after a type-specifier
952/// which could be a standalone declaration. This will conservatively return
953/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith139be702012-07-02 19:14:01 +0000954bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smithc9f35172012-06-25 21:37:02 +0000955 // This switch enumerates the valid "follow" set for type-specifiers.
956 switch (Tok.getKind()) {
957 default: break;
958 case tok::semi: // struct foo {...} ;
959 case tok::star: // struct foo {...} * P;
960 case tok::amp: // struct foo {...} & R = ...
Richard Smithba65f502013-01-19 03:48:05 +0000961 case tok::ampamp: // struct foo {...} && R = ...
Richard Smithc9f35172012-06-25 21:37:02 +0000962 case tok::identifier: // struct foo {...} V ;
963 case tok::r_paren: //(struct foo {...} ) {4}
964 case tok::annot_cxxscope: // struct foo {...} a:: b;
965 case tok::annot_typename: // struct foo {...} a ::b;
966 case tok::annot_template_id: // struct foo {...} a<int> ::b;
967 case tok::l_paren: // struct foo {...} ( x);
968 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smithba65f502013-01-19 03:48:05 +0000969 case tok::kw_operator: // struct foo operator ++() {...}
Richard Smithc9f35172012-06-25 21:37:02 +0000970 return true;
Richard Smith139be702012-07-02 19:14:01 +0000971 case tok::colon:
972 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smithc9f35172012-06-25 21:37:02 +0000973 // Type qualifiers
974 case tok::kw_const: // struct foo {...} const x;
975 case tok::kw_volatile: // struct foo {...} volatile x;
976 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smithba65f502013-01-19 03:48:05 +0000977 // Function specifiers
978 // Note, no 'explicit'. An explicit function must be either a conversion
979 // operator or a constructor. Either way, it can't have a return type.
980 case tok::kw_inline: // struct foo inline f();
981 case tok::kw_virtual: // struct foo virtual f();
982 case tok::kw_friend: // struct foo friend f();
Richard Smithc9f35172012-06-25 21:37:02 +0000983 // Storage-class specifiers
984 case tok::kw_static: // struct foo {...} static x;
985 case tok::kw_extern: // struct foo {...} extern x;
986 case tok::kw_typedef: // struct foo {...} typedef x;
987 case tok::kw_register: // struct foo {...} register x;
988 case tok::kw_auto: // struct foo {...} auto x;
989 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smithba65f502013-01-19 03:48:05 +0000990 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smithc9f35172012-06-25 21:37:02 +0000991 case tok::kw_constexpr: // struct foo {...} constexpr x;
992 // As shown above, type qualifiers and storage class specifiers absolutely
993 // can occur after class specifiers according to the grammar. However,
994 // almost no one actually writes code like this. If we see one of these,
995 // it is much more likely that someone missed a semi colon and the
996 // type/storage class specifier we're seeing is part of the *next*
997 // intended declaration, as in:
998 //
999 // struct foo { ... }
1000 // typedef int X;
1001 //
1002 // We'd really like to emit a missing semicolon error instead of emitting
1003 // an error on the 'int' saying that you can't have two type specifiers in
1004 // the same declaration of X. Because of this, we look ahead past this
1005 // token to see if it's a type specifier. If so, we know the code is
1006 // otherwise invalid, so we can produce the expected semi error.
1007 if (!isKnownToBeTypeSpecifier(NextToken()))
1008 return true;
1009 break;
1010 case tok::r_brace: // struct bar { struct foo {...} }
1011 // Missing ';' at end of struct is accepted as an extension in C mode.
1012 if (!getLangOpts().CPlusPlus)
1013 return true;
1014 break;
Richard Smithba65f502013-01-19 03:48:05 +00001015 // C++11 attributes
1016 case tok::l_square: // enum E [[]] x
1017 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1018 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith8338a9d2013-01-29 04:13:32 +00001019 case tok::greater:
1020 // template<class T = class X>
1021 return getLangOpts().CPlusPlus;
Richard Smithc9f35172012-06-25 21:37:02 +00001022 }
1023 return false;
1024}
1025
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001026/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1027/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1028/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +00001029/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001030///
1031/// class-specifier: [C++ class]
1032/// class-head '{' member-specification[opt] '}'
1033/// class-head '{' member-specification[opt] '}' attributes[opt]
1034/// class-head:
1035/// class-key identifier[opt] base-clause[opt]
1036/// class-key nested-name-specifier identifier base-clause[opt]
1037/// class-key nested-name-specifier[opt] simple-template-id
1038/// base-clause[opt]
1039/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001040/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001041/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001042/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001043/// simple-template-id base-clause[opt]
1044/// class-key:
1045/// 'class'
1046/// 'struct'
1047/// 'union'
1048///
1049/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +00001050/// class-key ::[opt] nested-name-specifier[opt] identifier
1051/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1052/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001053///
1054/// Note that the C++ class-specifier and elaborated-type-specifier,
1055/// together, subsume the C99 struct-or-union-specifier:
1056///
1057/// struct-or-union-specifier: [C99 6.7.2.1]
1058/// struct-or-union identifier[opt] '{' struct-contents '}'
1059/// struct-or-union identifier
1060/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1061/// '}' attributes[opt]
1062/// [GNU] struct-or-union attributes[opt] identifier
1063/// struct-or-union:
1064/// 'struct'
1065/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +00001066void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1067 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001068 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001069 AccessSpecifier AS,
Michael Han2e397132012-11-26 22:54:45 +00001070 bool EnteringContext, DeclSpecContext DSC,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001071 ParsedAttributesWithRange &Attributes) {
Joao Matos17d35c32012-08-31 22:18:20 +00001072 DeclSpec::TST TagType;
1073 if (TagTokKind == tok::kw_struct)
1074 TagType = DeclSpec::TST_struct;
1075 else if (TagTokKind == tok::kw___interface)
1076 TagType = DeclSpec::TST_interface;
1077 else if (TagTokKind == tok::kw_class)
1078 TagType = DeclSpec::TST_class;
1079 else {
Chris Lattner4c97d762009-04-12 21:49:30 +00001080 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1081 TagType = DeclSpec::TST_union;
1082 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001083
Douglas Gregor374929f2009-09-18 15:37:17 +00001084 if (Tok.is(tok::code_completion)) {
1085 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001086 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001087 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00001088 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001089
Chandler Carruth926c4b42010-06-28 08:39:25 +00001090 // C++03 [temp.explicit] 14.7.2/8:
1091 // The usual access checking rules do not apply to names used to specify
1092 // explicit instantiations.
1093 //
1094 // As an extension we do not perform access checking on the names used to
1095 // specify explicit specializations either. This is important to allow
1096 // specializing traits classes for private types.
John McCall13489672012-05-07 06:16:58 +00001097 //
1098 // Note that we don't suppress if this turns out to be an elaborated
1099 // type specifier.
1100 bool shouldDelayDiagsInTag =
1101 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1102 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1103 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001104
Sean Hunt2edf0a22012-06-23 05:07:58 +00001105 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001106 // If attributes exist after tag, parse them.
1107 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +00001108 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001109
Steve Narofff59e17e2008-12-24 20:59:21 +00001110 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001111 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001112 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001113
John McCallc052dbb2012-05-22 21:28:12 +00001114 // Parse inheritance specifiers.
1115 if (Tok.is(tok::kw___single_inheritance) ||
1116 Tok.is(tok::kw___multiple_inheritance) ||
1117 Tok.is(tok::kw___virtual_inheritance))
1118 ParseMicrosoftInheritanceClassAttributes(attrs);
1119
Sean Huntbbd37c62009-11-21 08:43:09 +00001120 // If C++0x attributes exist here, parse them.
1121 // FIXME: Are we consistent with the ordering of parsing of different
1122 // styles of attributes?
Richard Smith4e24f0f2013-01-02 12:01:23 +00001123 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Michael Han07fc1ba2013-01-07 16:57:11 +00001125 // Source location used by FIXIT to insert misplaced
1126 // C++11 attributes
1127 SourceLocation AttrFixitLoc = Tok.getLocation();
1128
John Wiegley20c0da72011-04-27 23:09:49 +00001129 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +00001130 !Tok.is(tok::identifier) &&
1131 Tok.getIdentifierInfo() &&
1132 (Tok.is(tok::kw___is_arithmetic) ||
1133 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001134 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001135 Tok.is(tok::kw___is_floating_point) ||
1136 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001137 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001138 Tok.is(tok::kw___is_integral) ||
1139 Tok.is(tok::kw___is_member_function_pointer) ||
1140 Tok.is(tok::kw___is_member_pointer) ||
1141 Tok.is(tok::kw___is_pod) ||
1142 Tok.is(tok::kw___is_pointer) ||
1143 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001144 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001145 Tok.is(tok::kw___is_signed) ||
1146 Tok.is(tok::kw___is_unsigned) ||
1147 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001148 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001149 // name of struct templates, but some are keywords in GCC >= 4.3
1150 // and Clang. Therefore, when we see the token sequence "struct
1151 // X", make X into a normal identifier rather than a keyword, to
1152 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001153 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001154 Tok.setKind(tok::identifier);
1155 }
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001157 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001158 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001159 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001160 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1161 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001162
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001163 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001164 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001165 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001166 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1167 Diag(Tok, diag::err_expected_ident);
1168 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001169
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001170 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1171
Douglas Gregorcc636682009-02-17 23:15:12 +00001172 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001173 IdentifierInfo *Name = 0;
1174 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001175 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001176 if (Tok.is(tok::identifier)) {
1177 Name = Tok.getIdentifierInfo();
1178 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001179
David Blaikie4e4d0842012-03-11 07:00:24 +00001180 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001181 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001182 // Eat the template argument list and try to continue parsing this as
1183 // a class (or template thereof).
1184 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001185 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001186 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001187 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001188 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001189 // We couldn't parse the template argument list at all, so don't
1190 // try to give any location information for the list.
1191 LAngleLoc = RAngleLoc = SourceLocation();
1192 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001193
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001194 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matos17d35c32012-08-31 22:18:20 +00001195 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1196 << (TagType == DeclSpec::TST_class? 0
1197 : TagType == DeclSpec::TST_struct? 1
1198 : TagType == DeclSpec::TST_interface? 2
1199 : 3)
1200 << Name
1201 << SourceRange(LAngleLoc, RAngleLoc);
1202
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001203 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001204 // we've removed its template argument list.
1205 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1206 if (TemplateParams && TemplateParams->size() > 1) {
1207 TemplateParams->pop_back();
1208 } else {
1209 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001210 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001211 = ParsedTemplateInfo::NonTemplate;
1212 }
1213 } else if (TemplateInfo.Kind
1214 == ParsedTemplateInfo::ExplicitInstantiation) {
1215 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001216 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001217 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001218 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001219 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001220 = SourceLocation();
1221 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1222 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001223 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001224 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001225 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001226 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001227 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001228
Douglas Gregor059101f2011-03-02 00:47:37 +00001229 if (TemplateId->Kind != TNK_Type_template &&
1230 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001231 // The template-name in the simple-template-id refers to
1232 // something other than a class template. Give an appropriate
1233 // error message and skip to the ';'.
1234 SourceRange Range(NameLoc);
1235 if (SS.isNotEmpty())
1236 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001237
Douglas Gregor39a8de12009-02-25 19:37:18 +00001238 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1239 << Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Douglas Gregor39a8de12009-02-25 19:37:18 +00001241 DS.SetTypeSpecError();
1242 SkipUntil(tok::semi, false, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001243 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001244 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001245 }
1246
Richard Smith7796eb52012-03-12 08:56:40 +00001247 // There are four options here.
1248 // - If we are in a trailing return type, this is always just a reference,
1249 // and we must not try to parse a definition. For instance,
1250 // [] () -> struct S { };
1251 // does not define a type.
1252 // - If we have 'struct foo {...', 'struct foo :...',
1253 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1254 // - If we have 'struct foo;', then this is either a forward declaration
1255 // or a friend declaration, which have to be treated differently.
1256 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han2e397132012-11-26 22:54:45 +00001257 //
1258 // We also detect these erroneous cases to provide better diagnostic for
1259 // C++11 attributes parsing.
1260 // - attributes follow class name:
1261 // struct foo [[]] {};
1262 // - attributes appear before or after 'final':
1263 // struct foo [[]] final [[]] {};
1264 //
Richard Smith69730c12012-03-12 07:56:15 +00001265 // However, in type-specifier-seq's, things look like declarations but are
1266 // just references, e.g.
1267 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001268 // or
Richard Smith69730c12012-03-12 07:56:15 +00001269 // &T::operator struct s;
1270 // For these, DSC is DSC_type_specifier.
Michael Han2e397132012-11-26 22:54:45 +00001271
1272 // If there are attributes after class name, parse them.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001273 MaybeParseCXX11Attributes(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00001274
John McCallf312b1e2010-08-26 23:41:50 +00001275 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001276 if (DSC == DSC_trailing)
1277 TUK = Sema::TUK_Reference;
1278 else if (Tok.is(tok::l_brace) ||
1279 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001280 (isCXX11FinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001281 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001282 if (DS.isFriendSpecified()) {
1283 // C++ [class.friend]p2:
1284 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001285 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001286 << SourceRange(DS.getFriendSpecLoc());
1287
1288 // Skip everything up to the semicolon, so that this looks like a proper
1289 // friend class (or template thereof) declaration.
1290 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001291 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001292 } else {
1293 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001294 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001295 }
Richard Smith4e24f0f2013-01-02 12:01:23 +00001296 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
Michael Han2e397132012-11-26 22:54:45 +00001297 NextToken().is(tok::kw_alignas) ||
1298 NextToken().is(tok::kw__Alignas))) {
1299 // We can't tell if this is a definition or reference
1300 // until we skipped the 'final' and C++11 attribute specifiers.
1301 TentativeParsingAction PA(*this);
1302
1303 // Skip the 'final' keyword.
1304 ConsumeToken();
1305
1306 // Skip C++11 attribute specifiers.
1307 while (true) {
1308 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1309 ConsumeBracket();
1310 if (!SkipUntil(tok::r_square))
1311 break;
1312 } else if ((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1313 NextToken().is(tok::l_paren)) {
1314 ConsumeToken();
1315 ConsumeParen();
1316 if (!SkipUntil(tok::r_paren))
1317 break;
1318 } else {
1319 break;
1320 }
1321 }
1322
1323 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1324 TUK = Sema::TUK_Definition;
1325 else
1326 TUK = Sema::TUK_Reference;
1327
1328 PA.Revert();
Richard Smithc9f35172012-06-25 21:37:02 +00001329 } else if (DSC != DSC_type_specifier &&
1330 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00001331 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallf312b1e2010-08-26 23:41:50 +00001332 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matos17d35c32012-08-31 22:18:20 +00001333 if (Tok.isNot(tok::semi)) {
1334 // A semicolon was missing after this declaration. Diagnose and recover.
1335 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1336 DeclSpec::getSpecifierName(TagType));
1337 PP.EnterToken(Tok);
1338 Tok.setKind(tok::semi);
1339 }
Richard Smithc9f35172012-06-25 21:37:02 +00001340 } else
John McCallf312b1e2010-08-26 23:41:50 +00001341 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001342
Michael Han2e397132012-11-26 22:54:45 +00001343 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1344 // to caller to handle.
Michael Han07fc1ba2013-01-07 16:57:11 +00001345 if (TUK != Sema::TUK_Reference) {
1346 // If this is not a reference, then the only possible
1347 // valid place for C++11 attributes to appear here
1348 // is between class-key and class-name. If there are
1349 // any attributes after class-name, we try a fixit to move
1350 // them to the right place.
1351 SourceRange AttrRange = Attributes.Range;
1352 if (AttrRange.isValid()) {
1353 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1354 << AttrRange
1355 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1356 CharSourceRange(AttrRange, true))
1357 << FixItHint::CreateRemoval(AttrRange);
1358
1359 // Recover by adding misplaced attributes to the attribute list
1360 // of the class so they can be applied on the class later.
1361 attrs.takeAllFrom(Attributes);
1362 }
1363 }
Michael Han2e397132012-11-26 22:54:45 +00001364
John McCall13489672012-05-07 06:16:58 +00001365 // If this is an elaborated type specifier, and we delayed
1366 // diagnostics before, just merge them into the current pool.
1367 if (shouldDelayDiagsInTag) {
1368 diagsFromTag.done();
1369 if (TUK == Sema::TUK_Reference)
1370 diagsFromTag.redelay();
1371 }
1372
John McCall207014e2010-07-30 06:26:29 +00001373 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001374 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001375 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1376 // We have a declaration or reference to an anonymous class.
1377 Diag(StartLoc, diag::err_anon_type_definition)
1378 << DeclSpec::getSpecifierName(TagType);
1379 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001380
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001381 SkipUntil(tok::comma, true);
1382 return;
1383 }
1384
Douglas Gregorddc29e12009-02-06 22:42:48 +00001385 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001386 DeclResult TagOrTempResult = true; // invalid
1387 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001388
Douglas Gregor402abb52009-05-28 23:31:59 +00001389 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001390 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001391 // Explicit specialization, class template partial specialization,
1392 // or explicit instantiation.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001393 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001394 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001395 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001396 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001397 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001398 ProhibitAttributes(attrs);
1399
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001400 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001401 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001402 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001403 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001404 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001405 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001406 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001407 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001408 TemplateId->TemplateNameLoc,
1409 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001410 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001411 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001412 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001413
1414 // Friend template-ids are treated as references unless
1415 // they have template headers, in which case they're ill-formed
1416 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1417 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001418 } else if (TUK == Sema::TUK_Reference ||
1419 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001420 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001421 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001422 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001423 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001424 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001425 TemplateId->Template,
1426 TemplateId->TemplateNameLoc,
1427 TemplateId->LAngleLoc,
1428 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001429 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001430 } else {
1431 // This is an explicit specialization or a class template
1432 // partial specialization.
1433 TemplateParameterLists FakedParamLists;
1434
1435 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1436 // This looks like an explicit instantiation, because we have
1437 // something like
1438 //
1439 // template class Foo<X>
1440 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001441 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001442 // meant to be an explicit specialization, but the user forgot
1443 // the '<>' after 'template'.
John McCallf312b1e2010-08-26 23:41:50 +00001444 assert(TUK == Sema::TUK_Definition && "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001445
Mike Stump1eb44332009-09-09 15:08:12 +00001446 SourceLocation LAngleLoc
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001447 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001448 Diag(TemplateId->TemplateNameLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001449 diag::err_explicit_instantiation_with_definition)
1450 << SourceRange(TemplateInfo.TemplateLoc)
Douglas Gregor849b2432010-03-31 17:46:05 +00001451 << FixItHint::CreateInsertion(LAngleLoc, "<>");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001452
1453 // Create a fake template parameter list that contains only
1454 // "template<>", so that we treat this construct as a class
1455 // template specialization.
1456 FakedParamLists.push_back(
Mike Stump1eb44332009-09-09 15:08:12 +00001457 Actions.ActOnTemplateParameterList(0, SourceLocation(),
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001458 TemplateInfo.TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001459 LAngleLoc,
1460 0, 0,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001461 LAngleLoc));
1462 TemplateParams = &FakedParamLists;
1463 }
1464
1465 // Build the class template specialization.
1466 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001467 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001468 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001469 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001470 TemplateId->TemplateNameLoc,
1471 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001472 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001473 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001474 attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001475 MultiTemplateParamsArg(
Douglas Gregorcc636682009-02-17 23:15:12 +00001476 TemplateParams? &(*TemplateParams)[0] : 0,
1477 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001478 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001479 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001480 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001481 // Explicit instantiation of a member of a class template
1482 // specialization, e.g.,
1483 //
1484 // template struct Outer<int>::Inner;
1485 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001486 ProhibitAttributes(attrs);
1487
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001488 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001489 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001490 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001491 TemplateInfo.TemplateLoc,
1492 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001493 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001494 } else if (TUK == Sema::TUK_Friend &&
1495 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001496 ProhibitAttributes(attrs);
1497
John McCall9a34edb2010-10-19 01:40:49 +00001498 TagOrTempResult =
1499 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1500 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001501 Name, NameLoc, attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001502 MultiTemplateParamsArg(
John McCall9a34edb2010-10-19 01:40:49 +00001503 TemplateParams? &(*TemplateParams)[0] : 0,
1504 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001505 } else {
1506 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001507 TUK == Sema::TUK_Definition) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001508 // FIXME: Diagnose this particular error.
1509 }
1510
Sean Hunt2edf0a22012-06-23 05:07:58 +00001511 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1512 ProhibitAttributes(attrs);
1513
John McCallc4e70192009-09-11 04:59:25 +00001514 bool IsDependent = false;
1515
John McCalla25c4082010-10-19 18:40:57 +00001516 // Don't pass down template parameter lists if this is just a tag
1517 // reference. For example, we don't need the template parameters here:
1518 // template <class T> class A *makeA(T t);
1519 MultiTemplateParamsArg TParams;
1520 if (TUK != Sema::TUK_Reference && TemplateParams)
1521 TParams =
1522 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1523
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001524 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001525 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001526 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001527 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001528 TParams, Owned, IsDependent,
1529 SourceLocation(), false,
1530 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001531
1532 // If ActOnTag said the type was dependent, try again with the
1533 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001534 if (IsDependent) {
1535 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001536 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001537 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001538 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001539 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001540
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001541 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001542 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001543 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001544 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001545 isCXX11FinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001546 if (getLangOpts().CPlusPlus)
Michael Han07fc1ba2013-01-07 16:57:11 +00001547 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1548 TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001549 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001550 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001551 }
1552
John McCallb3d87482010-08-24 05:47:05 +00001553 const char *PrevSpec = 0;
1554 unsigned DiagID;
1555 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001556 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001557 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1558 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001559 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001560 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001561 Result = DS.SetTypeSpecType(TagType, StartLoc,
1562 NameLoc.isValid() ? NameLoc : StartLoc,
1563 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001564 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001565 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001566 return;
1567 }
Mike Stump1eb44332009-09-09 15:08:12 +00001568
John McCallb3d87482010-08-24 05:47:05 +00001569 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001570 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001571
Chris Lattner4ed5d912010-02-02 01:23:29 +00001572 // At this point, we've successfully parsed a class-specifier in 'definition'
1573 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1574 // going to look at what comes after it to improve error recovery. If an
1575 // impossible token occurs next, we assume that the programmer forgot a ; at
1576 // the end of the declaration and recover that way.
1577 //
Richard Smithc9f35172012-06-25 21:37:02 +00001578 // Also enforce C++ [temp]p3:
1579 // In a template-declaration which defines a class, no declarator
1580 // is permitted.
Joao Matos17d35c32012-08-31 22:18:20 +00001581 if (TUK == Sema::TUK_Definition &&
1582 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidis7d033b22012-12-17 20:10:43 +00001583 if (Tok.isNot(tok::semi)) {
1584 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1585 DeclSpec::getSpecifierName(TagType));
1586 // Push this token back into the preprocessor and change our current token
1587 // to ';' so that the rest of the code recovers as though there were an
1588 // ';' after the definition.
1589 PP.EnterToken(Tok);
1590 Tok.setKind(tok::semi);
1591 }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001592 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001593}
1594
Mike Stump1eb44332009-09-09 15:08:12 +00001595/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001596///
1597/// base-clause : [C++ class.derived]
1598/// ':' base-specifier-list
1599/// base-specifier-list:
1600/// base-specifier '...'[opt]
1601/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001602void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001603 assert(Tok.is(tok::colon) && "Not a base clause");
1604 ConsumeToken();
1605
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001606 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001607 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001608
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001609 while (true) {
1610 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001611 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001612 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001613 // Skip the rest of this base specifier, up until the comma or
1614 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001615 SkipUntil(tok::comma, tok::l_brace, true, true);
1616 } else {
1617 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001618 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001619 }
1620
1621 // If the next token is a comma, consume it and keep reading
1622 // base-specifiers.
1623 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001625 // Consume the comma.
1626 ConsumeToken();
1627 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001628
1629 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001630 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001631}
1632
1633/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1634/// one entry in the base class list of a class specifier, for example:
1635/// class foo : public bar, virtual private baz {
1636/// 'public bar' and 'virtual private baz' are each base-specifiers.
1637///
1638/// base-specifier: [C++ class.derived]
1639/// ::[opt] nested-name-specifier[opt] class-name
1640/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001641/// base-type-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001642/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
David Blaikie09048df2011-10-25 15:01:20 +00001643/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001644Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001645 bool IsVirtual = false;
1646 SourceLocation StartLoc = Tok.getLocation();
1647
1648 // Parse the 'virtual' keyword.
1649 if (Tok.is(tok::kw_virtual)) {
1650 ConsumeToken();
1651 IsVirtual = true;
1652 }
1653
1654 // Parse an (optional) access specifier.
1655 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001656 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001657 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001658
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
Douglas Gregor42a552f2008-11-05 20:51:48 +00001672 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001673 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001674 SourceLocation BaseLoc;
1675 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001676 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001677 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001679 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1680 // actually part of the base-specifier-list grammar productions, but we
1681 // parse it here for convenience.
1682 SourceLocation EllipsisLoc;
1683 if (Tok.is(tok::ellipsis))
1684 EllipsisLoc = ConsumeToken();
1685
Mike Stump1eb44332009-09-09 15:08:12 +00001686 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001687 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001689 // Notify semantic analysis that we have parsed a complete
1690 // base-specifier.
Sebastian Redla55e52c2008-11-25 22:21:31 +00001691 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001692 BaseType.get(), BaseLoc, EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001693}
1694
1695/// getAccessSpecifierIfPresent - Determine whether the next token is
1696/// a C++ access-specifier.
1697///
1698/// access-specifier: [C++ class.derived]
1699/// 'private'
1700/// 'protected'
1701/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001702AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001703 switch (Tok.getKind()) {
1704 default: return AS_none;
1705 case tok::kw_private: return AS_private;
1706 case tok::kw_protected: return AS_protected;
1707 case tok::kw_public: return AS_public;
1708 }
1709}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001710
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001711/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001712/// delayed, e.g., default arguments, create a late-parsed method declaration
1713/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001714void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1715 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001716 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001717 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001718 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001719 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001720 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001721
Eli Friedmand33133c2009-07-22 21:45:50 +00001722 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1723 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1724 if (!LateMethod) {
1725 // Push this method onto the stack of late-parsed method
1726 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001727 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1728 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001729 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001730
1731 // Add all of the parameters prior to this one (they don't
1732 // have default arguments).
1733 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1734 for (unsigned I = 0; I < ParamIdx; ++I)
1735 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001736 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001737 }
1738
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001739 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001740 // not have a default argument).
1741 LateMethod->DefaultArgs.push_back(
1742 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1743 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1744 }
1745 }
1746}
1747
Richard Smith4e24f0f2013-01-02 12:01:23 +00001748/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001749/// virt-specifier.
1750///
1751/// virt-specifier:
1752/// override
1753/// final
Richard Smith4e24f0f2013-01-02 12:01:23 +00001754VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001755 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001756 return VirtSpecifiers::VS_None;
1757
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001758 if (Tok.is(tok::identifier)) {
1759 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001760
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001761 // Initialize the contextual keywords.
1762 if (!Ident_final) {
1763 Ident_final = &PP.getIdentifierTable().get("final");
1764 Ident_override = &PP.getIdentifierTable().get("override");
1765 }
1766
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001767 if (II == Ident_override)
1768 return VirtSpecifiers::VS_Override;
1769
1770 if (II == Ident_final)
1771 return VirtSpecifiers::VS_Final;
1772 }
1773
1774 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001775}
1776
Richard Smith4e24f0f2013-01-02 12:01:23 +00001777/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001778///
1779/// virt-specifier-seq:
1780/// virt-specifier
1781/// virt-specifier-seq virt-specifier
Richard Smith4e24f0f2013-01-02 12:01:23 +00001782void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalle402e722012-09-25 07:32:39 +00001783 bool IsInterface) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001784 while (true) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001785 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001786 if (Specifier == VirtSpecifiers::VS_None)
1787 return;
1788
1789 // C++ [class.mem]p8:
1790 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001791 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001792 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001793 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1794 << PrevSpec
1795 << FixItHint::CreateRemoval(Tok.getLocation());
1796
John McCalle402e722012-09-25 07:32:39 +00001797 if (IsInterface && Specifier == VirtSpecifiers::VS_Final) {
1798 Diag(Tok.getLocation(), diag::err_override_control_interface)
1799 << VirtSpecifiers::getSpecifierName(Specifier);
1800 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00001801 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00001802 diag::warn_cxx98_compat_override_control_keyword :
1803 diag::ext_override_control_keyword)
1804 << VirtSpecifiers::getSpecifierName(Specifier);
1805 }
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001806 ConsumeToken();
1807 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001808}
1809
Richard Smith4e24f0f2013-01-02 12:01:23 +00001810/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001811/// contextual 'final' keyword.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001812bool Parser::isCXX11FinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001813 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001814 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001815
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001816 if (!Tok.is(tok::identifier))
1817 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001818
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001819 // Initialize the contextual keywords.
1820 if (!Ident_final) {
1821 Ident_final = &PP.getIdentifierTable().get("final");
1822 Ident_override = &PP.getIdentifierTable().get("override");
1823 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001824
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001825 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001826}
1827
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001828/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1829///
1830/// member-declaration:
1831/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1832/// function-definition ';'[opt]
1833/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1834/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001835/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001836/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001837/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001838///
1839/// member-declarator-list:
1840/// member-declarator
1841/// member-declarator-list ',' member-declarator
1842///
1843/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001844/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001845/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001846/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001847/// identifier[opt] ':' constant-expression
1848///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001849/// virt-specifier-seq:
1850/// virt-specifier
1851/// virt-specifier-seq virt-specifier
1852///
1853/// virt-specifier:
1854/// override
1855/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001856///
Sebastian Redle2b68332009-04-12 17:16:29 +00001857/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001858/// '= 0'
1859///
1860/// constant-initializer:
1861/// '=' constant-expression
1862///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001863void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001864 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001865 const ParsedTemplateInfo &TemplateInfo,
1866 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001867 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001868 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001869 Diag(Tok, diag::err_at_defs_cxx);
1870 else
1871 Diag(Tok, diag::err_at_in_class);
1872
1873 ConsumeToken();
1874 SkipUntil(tok::r_brace);
1875 return;
1876 }
1877
John McCall60fa3cf2009-12-11 02:10:03 +00001878 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001879 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001880 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001881 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1882 if (TryAnnotateCXXScopeToken())
1883 MalformedTypeSpec = true;
1884
1885 bool isAccessDecl;
1886 if (Tok.isNot(tok::annot_cxxscope))
1887 isAccessDecl = false;
1888 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001889 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1890 else
1891 isAccessDecl = NextToken().is(tok::kw_operator);
1892
1893 if (isAccessDecl) {
1894 // Collect the scope specifier token we annotated earlier.
1895 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001896 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1897 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001898
1899 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001900 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001901 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001902 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1903 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001904 SkipUntil(tok::semi);
1905 return;
1906 }
1907
1908 // TODO: recover from mistakenly-qualified operator declarations.
1909 if (ExpectAndConsume(tok::semi,
1910 diag::err_expected_semi_after,
1911 "access declaration",
1912 tok::semi))
1913 return;
1914
Douglas Gregor23c94db2010-07-02 17:43:08 +00001915 Actions.ActOnUsingDeclaration(getCurScope(), AS,
John McCall60fa3cf2009-12-11 02:10:03 +00001916 false, SourceLocation(),
1917 SS, Name,
1918 /* AttrList */ 0,
1919 /* IsTypeName */ false,
1920 SourceLocation());
1921 return;
1922 }
1923 }
1924
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001925 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001926 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001927 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001928 SourceLocation DeclEnd;
1929 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001930 return;
1931 }
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Chris Lattner682bf922009-03-29 16:50:03 +00001933 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001934 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00001935 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00001936 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00001937 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001938 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00001939 return;
1940 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001941
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001942 // Handle: member-declaration ::= '__extension__' member-declaration
1943 if (Tok.is(tok::kw___extension__)) {
1944 // __extension__ silences extension warnings in the subexpression.
1945 ExtensionRAIIObject O(Diags); // Use RAII to do this.
1946 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001947 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
1948 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001949 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001950
Chris Lattner4ed5d912010-02-02 01:23:29 +00001951 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1952 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001953 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001954
John McCall0b7e6782011-03-24 11:26:52 +00001955 ParsedAttributesWithRange attrs(AttrFactory);
Michael Han52b501c2012-11-28 23:17:40 +00001956 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001957 // Optional C++11 attribute-specifier
1958 MaybeParseCXX11Attributes(attrs);
Michael Han52b501c2012-11-28 23:17:40 +00001959 // We need to keep these attributes for future diagnostic
1960 // before they are taken over by declaration specifier.
1961 FnAttrs.addAll(attrs.getList());
1962 FnAttrs.Range = attrs.Range;
1963
John McCall7f040a92010-12-24 02:08:15 +00001964 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001965
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001966 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00001967 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001968
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001969 // Eat 'using'.
1970 SourceLocation UsingLoc = ConsumeToken();
1971
1972 if (Tok.is(tok::kw_namespace)) {
1973 Diag(UsingLoc, diag::err_using_namespace_in_class);
1974 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00001975 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001976 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001977 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00001978 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
1979 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00001980 }
1981 return;
1982 }
1983
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001984 // Hold late-parsed attributes so we can attach a Decl to them later.
1985 LateParsedAttrList CommonLateParsedAttrs;
1986
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001987 // decl-specifier-seq:
1988 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00001989 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00001990 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00001991 if (MalformedTypeSpec)
1992 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001993 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
1994 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001995
Benjamin Kramer5354e772012-08-23 23:38:35 +00001996 MultiTemplateParamsArg TemplateParams(
John McCalldd4a3b02009-09-16 22:47:08 +00001997 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
1998 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
1999
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002000 if (Tok.is(tok::semi)) {
2001 ConsumeToken();
Michael Han52b501c2012-11-28 23:17:40 +00002002
2003 if (DS.isFriendSpecified())
2004 ProhibitAttributes(FnAttrs);
2005
John McCalld226f652010-08-21 09:40:31 +00002006 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00002007 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00002008 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00002009 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002010 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002011
John McCall54abf7d2009-11-04 02:18:39 +00002012 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00002013 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002014
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002015 // Hold late-parsed attributes so we can attach a Decl to them later.
2016 LateParsedAttrList LateParsedAttrs;
2017
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002018 SourceLocation EqualLoc;
2019 bool HasInitializer = false;
2020 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002021 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002022 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2023 ColonProtectionRAIIObject X(*this);
2024
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002025 // Parse the first declarator.
2026 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00002027 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00002028 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002029 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00002030 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002031 if (Tok.is(tok::semi))
2032 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002033 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002034 }
2035
Richard Smith4e24f0f2013-01-02 12:01:23 +00002036 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber48673472011-01-28 06:07:34 +00002037
John Thompson1b2fc0f2009-11-25 22:58:06 +00002038 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002039 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00002040
Francois Pichet6a247472011-05-11 02:14:46 +00002041 // MSVC permits pure specifier on inline functions declared at class scope.
2042 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00002043 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00002044 DeclaratorInfo.isFunctionDeclarator() &&
2045 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002046 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00002047 Init = ParseInitializer();
2048 if (Init.isInvalid())
2049 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002050 else
2051 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00002052 }
2053
Douglas Gregor45fa5602011-11-07 20:56:01 +00002054 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002055 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00002056 //
2057 // In C++11, a non-function declarator followed by an open brace is a
2058 // braced-init-list for an in-class member initialization, not an
2059 // erroneous function definition.
Richard Smith80ad52f2013-01-02 11:42:31 +00002060 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002061 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002062 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00002063 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002064 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002065 } else if (Tok.is(tok::equal)) {
2066 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00002067 if (KW.is(tok::kw_default))
2068 DefinitionKind = FDK_Defaulted;
2069 else if (KW.is(tok::kw_delete))
2070 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00002071 }
2072 }
2073
Michael Han52b501c2012-11-28 23:17:40 +00002074 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2075 // to a friend declaration, that declaration shall be a definition.
2076 if (DeclaratorInfo.isFunctionDeclarator() &&
2077 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2078 // Diagnose attributes that appear before decl specifier:
2079 // [[]] friend int foo();
2080 ProhibitAttributes(FnAttrs);
2081 }
2082
Douglas Gregor45fa5602011-11-07 20:56:01 +00002083 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002084 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002085 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002086 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00002087 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Michael Han52b501c2012-11-28 23:17:40 +00002088
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002089 // Consume the optional ';'
2090 if (Tok.is(tok::semi))
2091 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002092 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002093 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002094
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002095 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002096 Diag(DeclaratorInfo.getIdentifierLoc(),
2097 diag::err_function_declared_typedef);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002098
Richard Smith6f9a4452012-11-15 22:54:20 +00002099 // Recover by treating the 'typedef' as spurious.
2100 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002101 }
2102
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002103 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002104 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00002105 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002106
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002107 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2108 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2109 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002110 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002111 LateParsedAttrs[i]->addDecl(FunDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002112 }
2113 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00002114
2115 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002116 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00002117 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002118
Chris Lattner682bf922009-03-29 16:50:03 +00002119 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002120 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002121 }
2122
2123 // member-declarator-list:
2124 // member-declarator
2125 // member-declarator-list ',' member-declarator
2126
Chris Lattner5f9e2722011-07-23 10:55:15 +00002127 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00002128 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00002129 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002130
2131 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002132 // member-declarator:
2133 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002134 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002135 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002136 if (Tok.is(tok::colon)) {
2137 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002138 BitfieldSize = ParseConstantExpression();
2139 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002140 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002141 }
Mike Stump1eb44332009-09-09 15:08:12 +00002142
Chris Lattnere6563252010-06-13 05:34:18 +00002143 // If a simple-asm-expr is present, parse it.
2144 if (Tok.is(tok::kw_asm)) {
2145 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002146 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002147 if (AsmLabel.isInvalid())
2148 SkipUntil(tok::comma, true, true);
2149
2150 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2151 DeclaratorInfo.SetRangeEnd(Loc);
2152 }
2153
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002154 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002155 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002156
Richard Smith7a614d82011-06-11 17:19:42 +00002157 // FIXME: When g++ adds support for this, we'll need to check whether it
2158 // goes before or after the GNU attributes and __asm__.
Richard Smith4e24f0f2013-01-02 12:01:23 +00002159 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith7a614d82011-06-11 17:19:42 +00002160
Richard Smithca523302012-06-10 03:12:00 +00002161 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002162 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002163 if (BitfieldSize.get()) {
2164 Diag(Tok, diag::err_bitfield_member_init);
2165 SkipUntil(tok::comma, true, true);
2166 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002167 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002168 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2169 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smithca523302012-06-10 03:12:00 +00002170 != DeclSpec::SCS_typedef)
2171 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002172 }
2173 }
2174
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002175 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002176 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002177 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002178
Rafael Espindolafc35cbc2013-01-08 20:44:06 +00002179 NamedDecl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002180 if (DS.isFriendSpecified()) {
Michael Han52b501c2012-11-28 23:17:40 +00002181 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2182 // to a friend declaration, that declaration shall be a definition.
2183 //
2184 // Diagnose attributes appear after friend member function declarator:
2185 // foo [[]] ();
2186 SmallVector<SourceRange, 4> Ranges;
2187 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2188 if (!Ranges.empty()) {
2189 for (SmallVector<SourceRange, 4>::iterator I = Ranges.begin(),
2190 E = Ranges.end(); I != E; ++I) {
2191 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2192 << *I;
2193 }
2194 }
2195
John McCallbbbcdd92009-09-11 21:02:39 +00002196 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002197 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002198 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002199 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002200 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002201 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002202 TemplateParams,
John McCall67d1a672009-08-06 02:15:43 +00002203 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002204 VS, HasInClassInit);
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002205 if (AccessAttrs)
2206 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2207 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002208 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002209
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002210 // Set the Decl for any late parsed attributes
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002211 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2212 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2213 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002214 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002215 LateParsedAttrs[i]->addDecl(ThisDecl);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002216 }
2217 LateParsedAttrs.clear();
2218
Douglas Gregor147545d2011-10-10 14:49:18 +00002219 // Handle the initializer.
David Blaikie1d87fba2013-01-30 01:22:18 +00002220 if (HasInClassInit != ICIS_NoInit &&
2221 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2222 DeclSpec::SCS_static) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002223 // The initializer was deferred; parse it and cache the tokens.
Richard Smith80ad52f2013-01-02 11:42:31 +00002224 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00002225 diag::warn_cxx98_compat_nonstatic_member_init :
2226 diag::ext_nonstatic_member_init);
2227
Richard Smith7a614d82011-06-11 17:19:42 +00002228 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002229 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2230 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002231 //
2232 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002233 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002234 Diag(Tok, diag::err_incomplete_array_member_init);
2235 SkipUntil(tok::comma, true, true);
David Blaikie3164c142012-02-14 09:00:46 +00002236 if (ThisDecl)
2237 // Avoid later warnings about a class member of incomplete type.
2238 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002239 } else
2240 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002241 } else if (HasInitializer) {
2242 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002243 if (!Init.isUsable())
Douglas Gregor552e2992012-02-21 02:22:07 +00002244 Init = ParseCXXMemberInitializer(ThisDecl,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002245 DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2246
Douglas Gregor147545d2011-10-10 14:49:18 +00002247 if (Init.isInvalid())
2248 SkipUntil(tok::comma, true, true);
2249 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002250 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002251 DS.getTypeSpecType() == DeclSpec::TST_auto);
Douglas Gregor147545d2011-10-10 14:49:18 +00002252 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2253 // No initializer.
2254 Actions.ActOnUninitializedDecl(ThisDecl,
2255 DS.getTypeSpecType() == DeclSpec::TST_auto);
Richard Smith7a614d82011-06-11 17:19:42 +00002256 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002257
2258 if (ThisDecl) {
2259 Actions.FinalizeDeclaration(ThisDecl);
2260 DeclsInGroup.push_back(ThisDecl);
2261 }
2262
Richard Smithe5310012012-04-29 07:31:09 +00002263 if (ThisDecl && DeclaratorInfo.isFunctionDeclarator() &&
Douglas Gregor147545d2011-10-10 14:49:18 +00002264 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
2265 != DeclSpec::SCS_typedef) {
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002266 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002267 }
2268
2269 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002270
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002271 // If we don't have a comma, it is either the end of the list (a ';')
2272 // or an error, bail out.
2273 if (Tok.isNot(tok::comma))
2274 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002275
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002276 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002277 SourceLocation CommaLoc = ConsumeToken();
2278
2279 if (Tok.isAtStartOfLine() &&
2280 !MightBeDeclarator(Declarator::MemberContext)) {
2281 // This comma was followed by a line-break and something which can't be
2282 // the start of a declarator. The comma was probably a typo for a
2283 // semicolon.
2284 Diag(CommaLoc, diag::err_expected_semi_declaration)
2285 << FixItHint::CreateReplacement(CommaLoc, ";");
2286 ExpectSemi = false;
2287 break;
2288 }
Mike Stump1eb44332009-09-09 15:08:12 +00002289
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002290 // Parse the next declarator.
2291 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002292 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002293 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002294 Init = true;
2295 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002296 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002297
Bill Wendlingad017fa2012-12-20 19:22:21 +00002298 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002299 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002300
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002301 if (Tok.isNot(tok::colon))
2302 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002303 }
2304
Richard Smith1c94c162012-01-09 22:31:44 +00002305 if (ExpectSemi &&
2306 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002307 // Skip to end of block or statement.
2308 SkipUntil(tok::r_brace, true, true);
2309 // If we stopped at a ';', eat it.
2310 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002311 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002312 }
2313
Douglas Gregor23c94db2010-07-02 17:43:08 +00002314 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup.data(),
Chris Lattnerae50d502010-02-02 00:43:15 +00002315 DeclsInGroup.size());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002316}
2317
Richard Smith7a614d82011-06-11 17:19:42 +00002318/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2319/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2320/// function definition. The location of the '=', if any, will be placed in
2321/// EqualLoc.
2322///
2323/// pure-specifier:
2324/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002325///
Richard Smith7a614d82011-06-11 17:19:42 +00002326/// brace-or-equal-initializer:
2327/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002328/// braced-init-list
2329///
Richard Smith7a614d82011-06-11 17:19:42 +00002330/// initializer-clause:
2331/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002332/// braced-init-list
2333///
Richard Smith7a614d82011-06-11 17:19:42 +00002334/// defaulted/deleted function-definition:
2335/// '=' 'default'
2336/// '=' 'delete'
2337///
2338/// Prior to C++0x, the assignment-expression in an initializer-clause must
2339/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002340ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002341 SourceLocation &EqualLoc) {
2342 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2343 && "Data member initializer not starting with '=' or '{'");
2344
Douglas Gregor552e2992012-02-21 02:22:07 +00002345 EnterExpressionEvaluationContext Context(Actions,
2346 Sema::PotentiallyEvaluated,
2347 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002348 if (Tok.is(tok::equal)) {
2349 EqualLoc = ConsumeToken();
2350 if (Tok.is(tok::kw_delete)) {
2351 // In principle, an initializer of '= delete p;' is legal, but it will
2352 // never type-check. It's better to diagnose it as an ill-formed expression
2353 // than as an ill-formed deleted non-function member.
2354 // An initializer of '= delete p, foo' will never be parsed, because
2355 // a top-level comma always ends the initializer expression.
2356 const Token &Next = NextToken();
2357 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2358 Next.is(tok::eof)) {
2359 if (IsFunction)
2360 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2361 << 1 /* delete */;
2362 else
2363 Diag(ConsumeToken(), diag::err_deleted_non_function);
2364 return ExprResult();
2365 }
2366 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002367 if (IsFunction)
2368 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2369 << 0 /* default */;
2370 else
2371 Diag(ConsumeToken(), diag::err_default_special_members);
2372 return ExprResult();
2373 }
2374
Sebastian Redl33deb352012-02-22 10:50:08 +00002375 }
2376 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002377}
2378
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002379/// ParseCXXMemberSpecification - Parse the class definition.
2380///
2381/// member-specification:
2382/// member-declaration member-specification[opt]
2383/// access-specifier ':' member-specification[opt]
2384///
Joao Matos17d35c32012-08-31 22:18:20 +00002385void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han07fc1ba2013-01-07 16:57:11 +00002386 SourceLocation AttrFixitLoc,
2387 ParsedAttributes &Attrs,
Joao Matos17d35c32012-08-31 22:18:20 +00002388 unsigned TagType, Decl *TagDecl) {
2389 assert((TagType == DeclSpec::TST_struct ||
2390 TagType == DeclSpec::TST_interface ||
2391 TagType == DeclSpec::TST_union ||
2392 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2393
John McCallf312b1e2010-08-26 23:41:50 +00002394 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2395 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Douglas Gregor26997fd2010-01-16 20:52:59 +00002397 // Determine whether this is a non-nested class. Note that local
2398 // classes are *not* considered to be nested classes.
2399 bool NonNestedClass = true;
2400 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002401 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002402 if (S->isClassScope()) {
2403 // We're inside a class scope, so this is a nested class.
2404 NonNestedClass = false;
John McCalle402e722012-09-25 07:32:39 +00002405
2406 // The Microsoft extension __interface does not permit nested classes.
2407 if (getCurrentClass().IsInterface) {
2408 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2409 << /*ErrorType=*/6
2410 << (isa<NamedDecl>(TagDecl)
2411 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2412 : "<anonymous>");
2413 }
Douglas Gregor26997fd2010-01-16 20:52:59 +00002414 break;
2415 }
2416
2417 if ((S->getFlags() & Scope::FnScope)) {
2418 // If we're in a function or function template declared in the
2419 // body of a class, then this is a local class rather than a
2420 // nested class.
2421 const Scope *Parent = S->getParent();
2422 if (Parent->isTemplateParamScope())
2423 Parent = Parent->getParent();
2424 if (Parent->isClassScope())
2425 break;
2426 }
2427 }
2428 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002429
2430 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002431 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002432
Douglas Gregor6569d682009-05-27 23:11:45 +00002433 // Note that we are parsing a new (potentially-nested) class definition.
John McCalle402e722012-09-25 07:32:39 +00002434 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2435 TagType == DeclSpec::TST_interface);
Douglas Gregor6569d682009-05-27 23:11:45 +00002436
Douglas Gregorddc29e12009-02-06 22:42:48 +00002437 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002438 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002439
Anders Carlssonb184a182011-03-25 14:46:08 +00002440 SourceLocation FinalLoc;
2441
2442 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002443 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00002444 assert(isCXX11FinalKeyword() && "not a class definition");
Richard Smith8b11b5e2011-10-15 04:21:46 +00002445 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002446
John McCalle402e722012-09-25 07:32:39 +00002447 if (TagType == DeclSpec::TST_interface) {
2448 Diag(FinalLoc, diag::err_override_control_interface)
2449 << "final";
2450 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00002451 Diag(FinalLoc, getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00002452 diag::warn_cxx98_compat_override_control_keyword :
2453 diag::ext_override_control_keyword) << "final";
2454 }
Michael Han2e397132012-11-26 22:54:45 +00002455
Michael Han07fc1ba2013-01-07 16:57:11 +00002456 // Parse any C++11 attributes after 'final' keyword.
2457 // These attributes are not allowed to appear here,
2458 // and the only possible place for them to appertain
2459 // to the class would be between class-key and class-name.
2460 ParsedAttributesWithRange Attributes(AttrFactory);
2461 MaybeParseCXX11Attributes(Attributes);
2462 SourceRange AttrRange = Attributes.Range;
2463 if (AttrRange.isValid()) {
2464 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
2465 << AttrRange
2466 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
2467 CharSourceRange(AttrRange, true))
2468 << FixItHint::CreateRemoval(AttrRange);
2469
2470 // Recover by adding attributes to the attribute list of the class
2471 // so they can be applied on the class later.
2472 Attrs.takeAllFrom(Attributes);
2473 }
Anders Carlssonb184a182011-03-25 14:46:08 +00002474 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002475
John McCallbd0dfa52009-12-19 21:48:58 +00002476 if (Tok.is(tok::colon)) {
2477 ParseBaseClause(TagDecl);
2478
2479 if (!Tok.is(tok::l_brace)) {
2480 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002481
2482 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002483 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002484 return;
2485 }
2486 }
2487
2488 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002489 BalancedDelimiterTracker T(*this, tok::l_brace);
2490 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002491
John McCall42a4f662010-05-28 08:11:17 +00002492 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002493 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002494 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002495
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002496 // C++ 11p3: Members of a class defined with the keyword class are private
2497 // by default. Members of a class defined with the keywords struct or union
2498 // are public by default.
2499 AccessSpecifier CurAS;
2500 if (TagType == DeclSpec::TST_class)
2501 CurAS = AS_private;
2502 else
2503 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002504 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002505
Douglas Gregor07976d22010-06-21 22:31:09 +00002506 if (TagDecl) {
2507 // While we still have something to read, read the member-declarations.
2508 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2509 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002510
David Blaikie4e4d0842012-03-11 07:00:24 +00002511 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002512 Tok.is(tok::kw___if_not_exists))) {
2513 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2514 continue;
2515 }
2516
Douglas Gregor07976d22010-06-21 22:31:09 +00002517 // Check for extraneous top-level semicolon.
2518 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002519 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002520 continue;
2521 }
2522
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002523 if (Tok.is(tok::annot_pragma_vis)) {
2524 HandlePragmaVisibility();
2525 continue;
2526 }
2527
2528 if (Tok.is(tok::annot_pragma_pack)) {
2529 HandlePragmaPack();
2530 continue;
2531 }
2532
Argyrios Kyrtzidisf4deaef2012-10-12 17:39:59 +00002533 if (Tok.is(tok::annot_pragma_align)) {
2534 HandlePragmaAlign();
2535 continue;
2536 }
2537
Douglas Gregor07976d22010-06-21 22:31:09 +00002538 AccessSpecifier AS = getAccessSpecifierIfPresent();
2539 if (AS != AS_none) {
2540 // Current token is a C++ access specifier.
2541 CurAS = AS;
2542 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002543 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002544 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002545 AccessAttrs.clear();
2546 MaybeParseGNUAttributes(AccessAttrs);
2547
David Blaikie13f8daf2011-10-13 06:08:43 +00002548 SourceLocation EndLoc;
2549 if (Tok.is(tok::colon)) {
2550 EndLoc = Tok.getLocation();
2551 ConsumeToken();
2552 } else if (Tok.is(tok::semi)) {
2553 EndLoc = Tok.getLocation();
2554 ConsumeToken();
2555 Diag(EndLoc, diag::err_expected_colon)
2556 << FixItHint::CreateReplacement(EndLoc, ":");
2557 } else {
2558 EndLoc = ASLoc.getLocWithOffset(TokLength);
2559 Diag(EndLoc, diag::err_expected_colon)
2560 << FixItHint::CreateInsertion(EndLoc, ":");
2561 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002562
John McCalle402e722012-09-25 07:32:39 +00002563 // The Microsoft extension __interface does not permit non-public
2564 // access specifiers.
2565 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2566 Diag(ASLoc, diag::err_access_specifier_interface)
2567 << (CurAS == AS_protected);
2568 }
2569
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002570 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2571 AccessAttrs.getList())) {
2572 // found another attribute than only annotations
2573 AccessAttrs.clear();
2574 }
2575
Douglas Gregor07976d22010-06-21 22:31:09 +00002576 continue;
2577 }
2578
2579 // FIXME: Make sure we don't have a template here.
2580
2581 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002582 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002583 }
2584
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002585 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002586 } else {
2587 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002588 }
Mike Stump1eb44332009-09-09 15:08:12 +00002589
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002590 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002591 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002592 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002593
John McCall42a4f662010-05-28 08:11:17 +00002594 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002595 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002596 T.getOpenLocation(),
2597 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002598 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002599
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002600 // C++11 [class.mem]p2:
2601 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002602 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002603 // brace-or-equal-initializers for non-static data members (including such
2604 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002605 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002606 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002607 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002608 // declarations and the lexed inline method definitions, along with any
2609 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002610 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002611 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002612 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002613
2614 // We've finished with all pending member declarations.
2615 Actions.ActOnFinishCXXMemberDecls();
2616
Richard Smith7a614d82011-06-11 17:19:42 +00002617 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002618 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002619 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002620 }
2621
John McCall42a4f662010-05-28 08:11:17 +00002622 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002623 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2624 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002625
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002626 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002627 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002628 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002629}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002630
2631/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2632/// which explicitly initializes the members or base classes of a
2633/// class (C++ [class.base.init]). For example, the three initializers
2634/// after the ':' in the Derived constructor below:
2635///
2636/// @code
2637/// class Base { };
2638/// class Derived : Base {
2639/// int x;
2640/// float f;
2641/// public:
2642/// Derived(float f) : Base(), x(17), f(f) { }
2643/// };
2644/// @endcode
2645///
Mike Stump1eb44332009-09-09 15:08:12 +00002646/// [C++] ctor-initializer:
2647/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002648///
Mike Stump1eb44332009-09-09 15:08:12 +00002649/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002650/// mem-initializer ...[opt]
2651/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002652void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002653 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2654
John Wiegley28bbe4b2011-04-28 01:08:34 +00002655 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2656 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002657 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002658
Chris Lattner5f9e2722011-07-23 10:55:15 +00002659 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002660 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002661
Douglas Gregor7ad83902008-11-05 04:29:56 +00002662 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002663 if (Tok.is(tok::code_completion)) {
2664 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2665 MemInitializers.data(),
2666 MemInitializers.size());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002667 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002668 } else {
2669 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2670 if (!MemInit.isInvalid())
2671 MemInitializers.push_back(MemInit.get());
2672 else
2673 AnyErrors = true;
2674 }
2675
Douglas Gregor7ad83902008-11-05 04:29:56 +00002676 if (Tok.is(tok::comma))
2677 ConsumeToken();
2678 else if (Tok.is(tok::l_brace))
2679 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002680 // If the next token looks like a base or member initializer, assume that
2681 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002682 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2683 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2684 Diag(Loc, diag::err_ctor_init_missing_comma)
2685 << FixItHint::CreateInsertion(Loc, ", ");
2686 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002687 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002688 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002689 SkipUntil(tok::l_brace, true, true);
2690 break;
2691 }
2692 } while (true);
2693
David Blaikie93c86172013-01-17 05:26:25 +00002694 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002695 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002696}
2697
2698/// ParseMemInitializer - Parse a C++ member initializer, which is
2699/// part of a constructor initializer that explicitly initializes one
2700/// member or base class (C++ [class.base.init]). See
2701/// ParseConstructorInitializer for an example.
2702///
2703/// [C++] mem-initializer:
2704/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002705/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002706///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002707/// [C++] mem-initializer-id:
2708/// '::'[opt] nested-name-specifier[opt] class-name
2709/// identifier
John McCalld226f652010-08-21 09:40:31 +00002710Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002711 // parse '::'[opt] nested-name-specifier[opt]
2712 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002713 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002714 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002715 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002716 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002717 if (TemplateId->Kind == TNK_Type_template ||
2718 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002719 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002720 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002721 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002722 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002723 }
David Blaikief2116622012-01-24 06:03:59 +00002724 // Uses of decltype will already have been converted to annot_decltype by
2725 // ParseOptionalCXXScopeSpecifier at this point.
2726 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2727 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002728 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002729 return true;
2730 }
Mike Stump1eb44332009-09-09 15:08:12 +00002731
David Blaikief2116622012-01-24 06:03:59 +00002732 IdentifierInfo *II = 0;
2733 DeclSpec DS(AttrFactory);
2734 SourceLocation IdLoc = Tok.getLocation();
2735 if (Tok.is(tok::annot_decltype)) {
2736 // Get the decltype expression, if there is one.
2737 ParseDecltypeSpecifier(DS);
2738 } else {
2739 if (Tok.is(tok::identifier))
2740 // Get the identifier. This may be a member name or a class name,
2741 // but we'll let the semantic analysis determine which it is.
2742 II = Tok.getIdentifierInfo();
2743 ConsumeToken();
2744 }
2745
Douglas Gregor7ad83902008-11-05 04:29:56 +00002746
2747 // Parse the '('.
Richard Smith80ad52f2013-01-02 11:42:31 +00002748 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002749 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2750
Sebastian Redl6df65482011-09-24 17:48:25 +00002751 ExprResult InitList = ParseBraceInitializer();
2752 if (InitList.isInvalid())
2753 return true;
2754
2755 SourceLocation EllipsisLoc;
2756 if (Tok.is(tok::ellipsis))
2757 EllipsisLoc = ConsumeToken();
2758
2759 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002760 TemplateTypeTy, DS, IdLoc,
2761 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002762 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002763 BalancedDelimiterTracker T(*this, tok::l_paren);
2764 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002765
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002766 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002767 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002768 CommaLocsTy CommaLocs;
2769 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2770 SkipUntil(tok::r_paren);
2771 return true;
2772 }
2773
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002774 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002775
2776 SourceLocation EllipsisLoc;
2777 if (Tok.is(tok::ellipsis))
2778 EllipsisLoc = ConsumeToken();
2779
2780 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002781 TemplateTypeTy, DS, IdLoc,
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002782 T.getOpenLocation(), ArgExprs.data(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002783 ArgExprs.size(), T.getCloseLocation(),
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002784 EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002785 }
2786
Richard Smith80ad52f2013-01-02 11:42:31 +00002787 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002788 : diag::err_expected_lparen);
2789 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002790}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002791
Sebastian Redl7acafd02011-03-05 14:45:16 +00002792/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002793///
Douglas Gregora4745612008-12-01 18:00:20 +00002794/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002795/// dynamic-exception-specification
2796/// noexcept-specification
2797///
2798/// noexcept-specification:
2799/// 'noexcept'
2800/// 'noexcept' '(' constant-expression ')'
2801ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002802Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002803 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002804 SmallVectorImpl<ParsedType> &DynamicExceptions,
2805 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002806 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002807 ExceptionSpecificationType Result = EST_None;
2808
2809 // See if there's a dynamic specification.
2810 if (Tok.is(tok::kw_throw)) {
2811 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2812 DynamicExceptions,
2813 DynamicExceptionRanges);
2814 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2815 "Produced different number of exception types and ranges.");
2816 }
2817
2818 // If there's no noexcept specification, we're done.
2819 if (Tok.isNot(tok::kw_noexcept))
2820 return Result;
2821
Richard Smith841804b2011-10-17 23:06:20 +00002822 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2823
Sebastian Redl7acafd02011-03-05 14:45:16 +00002824 // If we already had a dynamic specification, parse the noexcept for,
2825 // recovery, but emit a diagnostic and don't store the results.
2826 SourceRange NoexceptRange;
2827 ExceptionSpecificationType NoexceptType = EST_None;
2828
2829 SourceLocation KeywordLoc = ConsumeToken();
2830 if (Tok.is(tok::l_paren)) {
2831 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002832 BalancedDelimiterTracker T(*this, tok::l_paren);
2833 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002834 NoexceptType = EST_ComputedNoexcept;
2835 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002836 // The argument must be contextually convertible to bool. We use
2837 // ActOnBooleanCondition for this purpose.
2838 if (!NoexceptExpr.isInvalid())
2839 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2840 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002841 T.consumeClose();
2842 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002843 } else {
2844 // There is no argument.
2845 NoexceptType = EST_BasicNoexcept;
2846 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2847 }
2848
2849 if (Result == EST_None) {
2850 SpecificationRange = NoexceptRange;
2851 Result = NoexceptType;
2852
2853 // If there's a dynamic specification after a noexcept specification,
2854 // parse that and ignore the results.
2855 if (Tok.is(tok::kw_throw)) {
2856 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2857 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2858 DynamicExceptionRanges);
2859 }
2860 } else {
2861 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2862 }
2863
2864 return Result;
2865}
2866
2867/// ParseDynamicExceptionSpecification - Parse a C++
2868/// dynamic-exception-specification (C++ [except.spec]).
2869///
2870/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002871/// 'throw' '(' type-id-list [opt] ')'
2872/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002873///
Douglas Gregora4745612008-12-01 18:00:20 +00002874/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002875/// type-id ... [opt]
2876/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002877///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002878ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2879 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002880 SmallVectorImpl<ParsedType> &Exceptions,
2881 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002882 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002883
Sebastian Redl7acafd02011-03-05 14:45:16 +00002884 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002885 BalancedDelimiterTracker T(*this, tok::l_paren);
2886 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002887 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2888 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002889 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002890 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002891
Douglas Gregora4745612008-12-01 18:00:20 +00002892 // Parse throw(...), a Microsoft extension that means "this function
2893 // can throw anything".
2894 if (Tok.is(tok::ellipsis)) {
2895 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002896 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002897 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002898 T.consumeClose();
2899 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002900 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002901 }
2902
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002903 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002904 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002905 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002906 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002907
Douglas Gregora04426c2010-12-20 23:57:46 +00002908 if (Tok.is(tok::ellipsis)) {
2909 // C++0x [temp.variadic]p5:
2910 // - In a dynamic-exception-specification (15.4); the pattern is a
2911 // type-id.
2912 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002913 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002914 if (!Res.isInvalid())
2915 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2916 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002917
Sebastian Redlef65f062009-05-29 18:02:33 +00002918 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002919 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002920 Ranges.push_back(Range);
2921 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002922
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002923 if (Tok.is(tok::comma))
2924 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002925 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002926 break;
2927 }
2928
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002929 T.consumeClose();
2930 SpecificationRange.setEnd(T.getCloseLocation());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002931 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002932}
Douglas Gregor6569d682009-05-27 23:11:45 +00002933
Douglas Gregordab60ad2010-10-01 18:44:50 +00002934/// ParseTrailingReturnType - Parse a trailing return type on a new-style
2935/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00002936TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00002937 assert(Tok.is(tok::arrow) && "expected arrow");
2938
2939 ConsumeToken();
2940
Richard Smith7796eb52012-03-12 08:56:40 +00002941 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002942}
2943
Douglas Gregor6569d682009-05-27 23:11:45 +00002944/// \brief We have just started parsing the definition of a new class,
2945/// so push that class onto our stack of classes that is currently
2946/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00002947Sema::ParsingClassState
John McCalle402e722012-09-25 07:32:39 +00002948Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
2949 bool IsInterface) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002950 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00002951 "Nested class without outer class");
John McCalle402e722012-09-25 07:32:39 +00002952 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCalleee1d542011-02-14 07:13:47 +00002953 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00002954}
2955
2956/// \brief Deallocate the given parsed class and all of its nested
2957/// classes.
2958void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00002959 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
2960 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00002961 delete Class;
2962}
2963
2964/// \brief Pop the top class of the stack of classes that are
2965/// currently being parsed.
2966///
2967/// This routine should be called when we have finished parsing the
2968/// definition of a class, but have not yet popped the Scope
2969/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00002970void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002971 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00002972
John McCalleee1d542011-02-14 07:13:47 +00002973 Actions.PopParsingClass(state);
2974
Douglas Gregor6569d682009-05-27 23:11:45 +00002975 ParsingClass *Victim = ClassStack.top();
2976 ClassStack.pop();
2977 if (Victim->TopLevelClass) {
2978 // Deallocate all of the nested classes of this class,
2979 // recursively: we don't need to keep any of this information.
2980 DeallocateParsedClasses(Victim);
2981 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002982 }
Douglas Gregor6569d682009-05-27 23:11:45 +00002983 assert(!ClassStack.empty() && "Missing top-level class?");
2984
Douglas Gregord54eb442010-10-12 16:25:54 +00002985 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00002986 // The victim is a nested class, but we will not need to perform
2987 // any processing after the definition of this class since it has
2988 // no members whose handling was delayed. Therefore, we can just
2989 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00002990 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00002991 return;
2992 }
2993
2994 // This nested class has some members that will need to be processed
2995 // after the top-level class is completely defined. Therefore, add
2996 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002997 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00002998 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00002999 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00003000}
Sean Huntbbd37c62009-11-21 08:43:09 +00003001
Richard Smithc56298d2012-04-10 03:25:07 +00003002/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3003///
3004/// \return the parsed identifier on success, and 0 if the next token is not an
3005/// attribute-token.
3006///
3007/// C++11 [dcl.attr.grammar]p3:
3008/// If a keyword or an alternative token that satisfies the syntactic
3009/// requirements of an identifier is contained in an attribute-token,
3010/// it is considered an identifier.
3011IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3012 switch (Tok.getKind()) {
3013 default:
3014 // Identifiers and keywords have identifier info attached.
3015 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3016 Loc = ConsumeToken();
3017 return II;
3018 }
3019 return 0;
3020
3021 case tok::ampamp: // 'and'
3022 case tok::pipe: // 'bitor'
3023 case tok::pipepipe: // 'or'
3024 case tok::caret: // 'xor'
3025 case tok::tilde: // 'compl'
3026 case tok::amp: // 'bitand'
3027 case tok::ampequal: // 'and_eq'
3028 case tok::pipeequal: // 'or_eq'
3029 case tok::caretequal: // 'xor_eq'
3030 case tok::exclaim: // 'not'
3031 case tok::exclaimequal: // 'not_eq'
3032 // Alternative tokens do not have identifier info, but their spelling
3033 // starts with an alphabetical character.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003034 SmallString<8> SpellingBuf;
Richard Smithc56298d2012-04-10 03:25:07 +00003035 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rose3f6f51e2013-02-08 22:30:41 +00003036 if (isLetter(Spelling[0])) {
Richard Smithc56298d2012-04-10 03:25:07 +00003037 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00003038 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00003039 }
3040 return 0;
3041 }
3042}
3043
Michael Han6880f492012-10-03 01:56:22 +00003044static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3045 IdentifierInfo *ScopeName) {
3046 switch (AttributeList::getKind(AttrName, ScopeName,
3047 AttributeList::AS_CXX11)) {
3048 case AttributeList::AT_CarriesDependency:
3049 case AttributeList::AT_FallThrough:
Richard Smithcd8ab512013-01-17 01:30:42 +00003050 case AttributeList::AT_CXX11NoReturn: {
Michael Han6880f492012-10-03 01:56:22 +00003051 return true;
3052 }
3053
3054 default:
3055 return false;
3056 }
3057}
3058
Richard Smithc56298d2012-04-10 03:25:07 +00003059/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003060/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00003061///
Richard Smith6ee326a2012-04-10 01:32:12 +00003062/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00003063/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003064/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00003065///
Richard Smith6ee326a2012-04-10 01:32:12 +00003066/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00003067/// attribute[opt]
3068/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00003069/// attribute '...'
3070/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00003071///
Richard Smith6ee326a2012-04-10 01:32:12 +00003072/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00003073/// attribute-token attribute-argument-clause[opt]
3074///
Richard Smith6ee326a2012-04-10 01:32:12 +00003075/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003076/// identifier
3077/// attribute-scoped-token
3078///
Richard Smith6ee326a2012-04-10 01:32:12 +00003079/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003080/// attribute-namespace '::' identifier
3081///
Richard Smith6ee326a2012-04-10 01:32:12 +00003082/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00003083/// identifier
3084///
Richard Smith6ee326a2012-04-10 01:32:12 +00003085/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00003086/// '(' balanced-token-seq ')'
3087///
Richard Smith6ee326a2012-04-10 01:32:12 +00003088/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00003089/// balanced-token
3090/// balanced-token-seq balanced-token
3091///
Richard Smith6ee326a2012-04-10 01:32:12 +00003092/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003093/// '(' balanced-token-seq ')'
3094/// '[' balanced-token-seq ']'
3095/// '{' balanced-token-seq '}'
3096/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00003097void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003098 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003099 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00003100 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003101 ParseAlignmentSpecifier(attrs, endLoc);
3102 return;
3103 }
3104
Sean Huntbbd37c62009-11-21 08:43:09 +00003105 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00003106 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00003107
Richard Smith41be6732011-10-14 20:48:27 +00003108 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3109
Sean Huntbbd37c62009-11-21 08:43:09 +00003110 ConsumeBracket();
3111 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003112
Richard Smithcd8ab512013-01-17 01:30:42 +00003113 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3114
Richard Smithc56298d2012-04-10 03:25:07 +00003115 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003116 // attribute not present
3117 if (Tok.is(tok::comma)) {
3118 ConsumeToken();
3119 continue;
3120 }
3121
Richard Smithc56298d2012-04-10 03:25:07 +00003122 SourceLocation ScopeLoc, AttrLoc;
3123 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3124
3125 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3126 if (!AttrName)
3127 // Break out to the "expected ']'" diagnostic.
3128 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003129
Sean Huntbbd37c62009-11-21 08:43:09 +00003130 // scoped attribute
3131 if (Tok.is(tok::coloncolon)) {
3132 ConsumeToken();
3133
Richard Smithc56298d2012-04-10 03:25:07 +00003134 ScopeName = AttrName;
3135 ScopeLoc = AttrLoc;
3136
3137 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3138 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003139 Diag(Tok.getLocation(), diag::err_expected_ident);
3140 SkipUntil(tok::r_square, tok::comma, true, true);
3141 continue;
3142 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003143 }
3144
Michael Han6880f492012-10-03 01:56:22 +00003145 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Sean Huntbbd37c62009-11-21 08:43:09 +00003146 bool AttrParsed = false;
Sean Huntbbd37c62009-11-21 08:43:09 +00003147
Richard Smithcd8ab512013-01-17 01:30:42 +00003148 if (StandardAttr &&
3149 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3150 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3151 << AttrName << SourceRange(SeenAttrs[AttrName]);
3152
Michael Han6880f492012-10-03 01:56:22 +00003153 // Parse attribute arguments
3154 if (Tok.is(tok::l_paren)) {
3155 if (ScopeName && ScopeName->getName() == "gnu") {
3156 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3157 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3158 AttrParsed = true;
3159 } else {
3160 if (StandardAttr)
3161 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3162 << AttrName->getName();
3163
3164 // FIXME: handle other formats of c++11 attribute arguments
3165 ConsumeParen();
3166 SkipUntil(tok::r_paren, false);
3167 }
3168 }
3169
3170 if (!AttrParsed)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00003171 attrs.addNew(AttrName,
3172 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3173 AttrLoc),
3174 ScopeName, ScopeLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00003175 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smith6ee326a2012-04-10 01:32:12 +00003176
Richard Smithc56298d2012-04-10 03:25:07 +00003177 if (Tok.is(tok::ellipsis)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003178 ConsumeToken();
Michael Han6880f492012-10-03 01:56:22 +00003179
3180 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3181 << AttrName->getName();
Richard Smithc56298d2012-04-10 03:25:07 +00003182 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003183 }
3184
3185 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3186 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003187 if (endLoc)
3188 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00003189 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3190 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003191}
Sean Huntbbd37c62009-11-21 08:43:09 +00003192
Sean Hunt2edf0a22012-06-23 05:07:58 +00003193/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003194///
3195/// attribute-specifier-seq:
3196/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00003197void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003198 SourceLocation *endLoc) {
3199 SourceLocation StartLoc = Tok.getLocation(), Loc;
3200 if (!endLoc)
3201 endLoc = &Loc;
3202
Douglas Gregor8828ee72011-10-07 20:35:25 +00003203 do {
Richard Smithc56298d2012-04-10 03:25:07 +00003204 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003205 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003206
3207 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003208}
3209
Francois Pichet334d47e2010-10-11 12:59:39 +00003210/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3211///
3212/// [MS] ms-attribute:
3213/// '[' token-seq ']'
3214///
3215/// [MS] ms-attribute-seq:
3216/// ms-attribute[opt]
3217/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003218void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3219 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003220 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3221
3222 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003223 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003224 ConsumeBracket();
3225 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003226 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003227 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3228 }
3229}
Francois Pichet563a6452011-05-25 10:19:49 +00003230
3231void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3232 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003233 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003234 if (ParseMicrosoftIfExistsCondition(Result))
3235 return;
3236
Douglas Gregor3896fc52011-10-24 22:31:10 +00003237 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3238 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003239 Diag(Tok, diag::err_expected_lbrace);
3240 return;
3241 }
Francois Pichet563a6452011-05-25 10:19:49 +00003242
Douglas Gregor3896fc52011-10-24 22:31:10 +00003243 switch (Result.Behavior) {
3244 case IEB_Parse:
3245 // Parse the declarations below.
3246 break;
3247
3248 case IEB_Dependent:
3249 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3250 << Result.IsIfExists;
3251 // Fall through to skip.
3252
3253 case IEB_Skip:
3254 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003255 return;
3256 }
3257
Douglas Gregor3896fc52011-10-24 22:31:10 +00003258 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003259 // __if_exists, __if_not_exists can nest.
3260 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3261 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3262 continue;
3263 }
3264
3265 // Check for extraneous top-level semicolon.
3266 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003267 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003268 continue;
3269 }
3270
3271 AccessSpecifier AS = getAccessSpecifierIfPresent();
3272 if (AS != AS_none) {
3273 // Current token is a C++ access specifier.
3274 CurAS = AS;
3275 SourceLocation ASLoc = Tok.getLocation();
3276 ConsumeToken();
3277 if (Tok.is(tok::colon))
3278 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3279 else
3280 Diag(Tok, diag::err_expected_colon);
3281 ConsumeToken();
3282 continue;
3283 }
3284
3285 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003286 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003287 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003288
3289 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003290}