blob: 64465bf66e2d0fbbf88594c1b4b5f9b3a3a9af33 [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"
Larisse Voufoef4579c2013-08-06 01:03:05 +000018#include "clang/AST/DeclTemplate.h"
Chris Lattner500d3292009-01-29 05:15:15 +000019#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000020#include "clang/Sema/DeclSpec.h"
John McCall19510852010-08-20 18:27:03 +000021#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000022#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Sema/Scope.h"
John McCalle402e722012-09-25 07:32:39 +000024#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000026using namespace clang;
27
28/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000029/// may either be a top level namespace or a block-level namespace alias. If
30/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000031///
32/// namespace-definition: [C++ 7.3: basic.namespace]
33/// named-namespace-definition
34/// unnamed-namespace-definition
35///
36/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000037/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000038///
39/// named-namespace-definition:
40/// original-namespace-definition
41/// extension-namespace-definition
42///
43/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000044/// 'inline'[opt] 'namespace' identifier attributes[opt]
45/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000046///
47/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000048/// 'inline'[opt] 'namespace' original-namespace-name
49/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000050///
Chris Lattner8f08cb72007-08-25 06:57:03 +000051/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
52/// 'namespace' identifier '=' qualified-namespace-specifier ';'
53///
John McCalld226f652010-08-21 09:40:31 +000054Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000055 SourceLocation &DeclEnd,
56 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000057 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000058 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +000059 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000060
Douglas Gregor49f40bd2009-09-18 19:03:04 +000061 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000062 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000063 cutOffParsing();
64 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +000065 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000066
Chris Lattner8f08cb72007-08-25 06:57:03 +000067 SourceLocation IdentLoc;
68 IdentifierInfo *Ident = 0;
Richard Trieuf858bd82011-05-26 20:11:09 +000069 std::vector<SourceLocation> ExtraIdentLoc;
70 std::vector<IdentifierInfo*> ExtraIdent;
71 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000072
73 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000074
Chris Lattner04d66662007-10-09 17:33:22 +000075 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000076 Ident = Tok.getIdentifierInfo();
77 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieuf858bd82011-05-26 20:11:09 +000078 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
79 ExtraNamespaceLoc.push_back(ConsumeToken());
80 ExtraIdent.push_back(Tok.getIdentifierInfo());
81 ExtraIdentLoc.push_back(ConsumeToken());
82 }
Chris Lattner8f08cb72007-08-25 06:57:03 +000083 }
Mike Stump1eb44332009-09-09 15:08:12 +000084
Chris Lattner8f08cb72007-08-25 06:57:03 +000085 // Read label attributes, if present.
John McCall0b7e6782011-03-24 11:26:52 +000086 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000087 if (Tok.is(tok::kw___attribute)) {
88 attrTok = Tok;
John McCall7f040a92010-12-24 02:08:15 +000089 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000090 }
Mike Stump1eb44332009-09-09 15:08:12 +000091
Douglas Gregor6a588dd2009-06-17 19:49:00 +000092 if (Tok.is(tok::equal)) {
Nico Webere1bb3292012-10-27 23:44:27 +000093 if (Ident == 0) {
94 Diag(Tok, diag::err_expected_ident);
95 // Skip to end of the definition and eat the ';'.
96 SkipUntil(tok::semi);
97 return 0;
98 }
John McCall7f040a92010-12-24 02:08:15 +000099 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000100 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +0000101 if (InlineLoc.isValid())
102 Diag(InlineLoc, diag::err_inline_namespace_alias)
103 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000104 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Richard Trieuf858bd82011-05-26 20:11:09 +0000107
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000108 BalancedDelimiterTracker T(*this, tok::l_brace);
109 if (T.consumeOpen()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000110 if (!ExtraIdent.empty()) {
111 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
112 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
113 }
Mike Stump1eb44332009-09-09 15:08:12 +0000114 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +0000115 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +0000116 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000117 }
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Douglas Gregor23c94db2010-07-02 17:43:08 +0000119 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
120 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
121 getCurScope()->getFnParent()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000122 if (!ExtraIdent.empty()) {
123 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
124 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
125 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000126 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Douglas Gregor95f1b152010-05-14 05:08:22 +0000127 SkipUntil(tok::r_brace, false);
John McCalld226f652010-08-21 09:40:31 +0000128 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000129 }
130
Richard Trieuf858bd82011-05-26 20:11:09 +0000131 if (!ExtraIdent.empty()) {
132 TentativeParsingAction TPA(*this);
133 SkipUntil(tok::r_brace, /*StopAtSemi*/false, /*DontConsume*/true);
134 Token rBraceToken = Tok;
135 TPA.Revert();
136
137 if (!rBraceToken.is(tok::r_brace)) {
138 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
139 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
140 } else {
Benjamin Kramer9910df02011-05-26 21:32:30 +0000141 std::string NamespaceFix;
Richard Trieuf858bd82011-05-26 20:11:09 +0000142 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
143 E = ExtraIdent.end(); I != E; ++I) {
144 NamespaceFix += " { namespace ";
145 NamespaceFix += (*I)->getName();
146 }
Benjamin Kramer9910df02011-05-26 21:32:30 +0000147
Richard Trieuf858bd82011-05-26 20:11:09 +0000148 std::string RBraces;
Benjamin Kramer9910df02011-05-26 21:32:30 +0000149 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieuf858bd82011-05-26 20:11:09 +0000150 RBraces += "} ";
Benjamin Kramer9910df02011-05-26 21:32:30 +0000151
Richard Trieuf858bd82011-05-26 20:11:09 +0000152 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
153 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
154 ExtraIdentLoc.back()),
155 NamespaceFix)
156 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
157 }
158 }
159
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000160 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith7fe62082011-10-15 05:09:34 +0000161 if (InlineLoc.isValid())
Richard Smith80ad52f2013-01-02 11:42:31 +0000162 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000163 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000164
Chris Lattner51448322009-03-29 14:02:43 +0000165 // Enter a scope for the namespace.
166 ParseScope NamespaceScope(this, Scope::DeclScope);
167
John McCalld226f652010-08-21 09:40:31 +0000168 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000169 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000170 IdentLoc, Ident, T.getOpenLocation(),
171 attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000172
John McCallf312b1e2010-08-26 23:41:50 +0000173 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
174 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Richard Trieuf858bd82011-05-26 20:11:09 +0000176 // Parse the contents of the namespace. This includes parsing recovery on
177 // any improperly nested namespaces.
178 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000179 InlineLoc, attrs, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattner51448322009-03-29 14:02:43 +0000181 // Leave the namespace scope.
182 NamespaceScope.Exit();
183
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000184 DeclEnd = T.getCloseLocation();
185 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner51448322009-03-29 14:02:43 +0000186
187 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000188}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000189
Richard Trieuf858bd82011-05-26 20:11:09 +0000190/// ParseInnerNamespace - Parse the contents of a namespace.
191void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
192 std::vector<IdentifierInfo*>& Ident,
193 std::vector<SourceLocation>& NamespaceLoc,
194 unsigned int index, SourceLocation& InlineLoc,
Richard Trieuf858bd82011-05-26 20:11:09 +0000195 ParsedAttributes& attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000196 BalancedDelimiterTracker &Tracker) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000197 if (index == Ident.size()) {
198 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
199 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000200 MaybeParseCXX11Attributes(attrs);
Richard Trieuf858bd82011-05-26 20:11:09 +0000201 MaybeParseMicrosoftAttributes(attrs);
202 ParseExternalDeclaration(attrs);
203 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000204
205 // The caller is what called check -- we are simply calling
206 // the close for it.
207 Tracker.consumeClose();
Richard Trieuf858bd82011-05-26 20:11:09 +0000208
209 return;
210 }
211
212 // Parse improperly nested namespaces.
213 ParseScope NamespaceScope(this, Scope::DeclScope);
214 Decl *NamespcDecl =
215 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
216 NamespaceLoc[index], IdentLoc[index],
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000217 Ident[index], Tracker.getOpenLocation(),
218 attrs.getList());
Richard Trieuf858bd82011-05-26 20:11:09 +0000219
220 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000221 attrs, Tracker);
Richard Trieuf858bd82011-05-26 20:11:09 +0000222
223 NamespaceScope.Exit();
224
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000225 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieuf858bd82011-05-26 20:11:09 +0000226}
227
Anders Carlssonf67606a2009-03-28 04:07:16 +0000228/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
229/// alias definition.
230///
John McCalld226f652010-08-21 09:40:31 +0000231Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000232 SourceLocation AliasLoc,
233 IdentifierInfo *Alias,
234 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000235 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Anders Carlssonf67606a2009-03-28 04:07:16 +0000237 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000239 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000240 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000241 cutOffParsing();
242 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000243 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000244
Anders Carlssonf67606a2009-03-28 04:07:16 +0000245 CXXScopeSpec SS;
246 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000247 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000248
249 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
250 Diag(Tok, diag::err_expected_namespace_name);
251 // Skip to end of the definition and eat the ';'.
252 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000253 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000254 }
255
256 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000257 IdentifierInfo *Ident = Tok.getIdentifierInfo();
258 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Anders Carlssonf67606a2009-03-28 04:07:16 +0000260 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000261 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000262 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
263 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Douglas Gregor23c94db2010-07-02 17:43:08 +0000265 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000266 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000267}
268
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000269/// ParseLinkage - We know that the current token is a string_literal
270/// and just before that, that extern was seen.
271///
272/// linkage-specification: [C++ 7.5p2: dcl.link]
273/// 'extern' string-literal '{' declaration-seq[opt] '}'
274/// 'extern' string-literal declaration
275///
Chris Lattner7d642712010-11-09 20:15:55 +0000276Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000277 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000278 SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000279 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000280 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000281 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000282 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000283
Richard Smith99831e42012-03-06 03:21:47 +0000284 // FIXME: This is incorrect: linkage-specifiers are parsed in translation
285 // phase 7, so string-literal concatenation is supposed to occur.
286 // extern "" "C" "" "+" "+" { } is legal.
287 if (Tok.hasUDSuffix())
288 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000289 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000290
Douglas Gregor074149e2009-01-05 19:45:36 +0000291 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000292 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000293 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000294 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000295 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000296 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000297 : SourceLocation());
298
John McCall0b7e6782011-03-24 11:26:52 +0000299 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000300 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000301 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000302
Douglas Gregor074149e2009-01-05 19:45:36 +0000303 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-05-01 16:25:54 +0000304 // Reset the source range in DS, as the leading "extern"
305 // does not really belong to the inner declaration ...
306 DS.SetRangeStart(SourceLocation());
307 DS.SetRangeEnd(SourceLocation());
308 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000309 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000310 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000311 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000312 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000313 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000314
Douglas Gregor63a01132010-02-07 08:38:28 +0000315 DS.abort();
316
John McCall7f040a92010-12-24 02:08:15 +0000317 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000318
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000319 BalancedDelimiterTracker T(*this, tok::l_brace);
320 T.consumeOpen();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000321 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000322 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000323 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000324 MaybeParseMicrosoftAttributes(attrs);
325 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000326 }
327
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000328 T.consumeClose();
Chris Lattner7d642712010-11-09 20:15:55 +0000329 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000330 T.getCloseLocation());
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000331}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000332
Douglas Gregorf780abc2008-12-30 03:27:21 +0000333/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
334/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000335Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000336 const ParsedTemplateInfo &TemplateInfo,
337 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000338 ParsedAttributesWithRange &attrs,
339 Decl **OwnedType) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000340 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000341 ObjCDeclContextSwitch ObjCDC(*this);
342
Douglas Gregorf780abc2008-12-30 03:27:21 +0000343 // Eat 'using'.
344 SourceLocation UsingLoc = ConsumeToken();
345
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000346 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000347 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000348 cutOffParsing();
349 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000350 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000351
John McCall78b81052010-11-10 02:40:36 +0000352 // 'using namespace' means this is a using-directive.
353 if (Tok.is(tok::kw_namespace)) {
354 // Template parameters are always an error here.
355 if (TemplateInfo.Kind) {
356 SourceRange R = TemplateInfo.getSourceRange();
357 Diag(UsingLoc, diag::err_templated_using_directive)
358 << R << FixItHint::CreateRemoval(R);
359 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000360
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000361 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000362 }
363
Richard Smith162e1c12011-04-15 14:24:37 +0000364 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000365
366 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000367 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000368
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000369 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000370 AS_none, OwnedType);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000371}
372
373/// ParseUsingDirective - Parse C++ using-directive, assumes
374/// that current token is 'namespace' and 'using' was already parsed.
375///
376/// using-directive: [C++ 7.3.p4: namespace.udir]
377/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
378/// namespace-name ;
379/// [GNU] using-directive:
380/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
381/// namespace-name attributes[opt] ;
382///
John McCalld226f652010-08-21 09:40:31 +0000383Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000384 SourceLocation UsingLoc,
385 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000386 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000387 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
388
389 // Eat 'namespace'.
390 SourceLocation NamespcLoc = ConsumeToken();
391
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000392 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000393 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000394 cutOffParsing();
395 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000396 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000397
Douglas Gregorf780abc2008-12-30 03:27:21 +0000398 CXXScopeSpec SS;
399 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000400 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000401
Douglas Gregorf780abc2008-12-30 03:27:21 +0000402 IdentifierInfo *NamespcName = 0;
403 SourceLocation IdentLoc = SourceLocation();
404
405 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000406 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000407 Diag(Tok, diag::err_expected_namespace_name);
408 // If there was invalid namespace name, skip to end of decl, and eat ';'.
409 SkipUntil(tok::semi);
410 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000411 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000412 }
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Chris Lattner823c44e2009-01-06 07:27:21 +0000414 // Parse identifier.
415 NamespcName = Tok.getIdentifierInfo();
416 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Chris Lattner823c44e2009-01-06 07:27:21 +0000418 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000419 bool GNUAttr = false;
420 if (Tok.is(tok::kw___attribute)) {
421 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000422 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000423 }
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattner823c44e2009-01-06 07:27:21 +0000425 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000426 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000427 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000428 GNUAttr ? diag::err_expected_semi_after_attribute_list
429 : diag::err_expected_semi_after_namespace_name,
430 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000431
Douglas Gregor23c94db2010-07-02 17:43:08 +0000432 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000433 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000434}
435
Richard Smith162e1c12011-04-15 14:24:37 +0000436/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
437/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000438///
439/// using-declaration: [C++ 7.3.p3: namespace.udecl]
440/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000441/// unqualified-id
442/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000443///
Richard Smithd03de6a2013-01-29 10:02:16 +0000444/// alias-declaration: C++11 [dcl.dcl]p1
445/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smith162e1c12011-04-15 14:24:37 +0000446///
John McCalld226f652010-08-21 09:40:31 +0000447Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000448 const ParsedTemplateInfo &TemplateInfo,
449 SourceLocation UsingLoc,
450 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000451 AccessSpecifier AS,
452 Decl **OwnedType) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000453 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000454 SourceLocation TypenameLoc;
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000455 bool HasTypenameKeyword = false;
Richard Smith6b3d3e52013-02-20 19:22:51 +0000456 ParsedAttributesWithRange Attrs(AttrFactory);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000457
458 // FIXME: Simply skip the attributes and diagnose, don't bother parsing them.
Richard Smith6b3d3e52013-02-20 19:22:51 +0000459 MaybeParseCXX11Attributes(Attrs);
460 ProhibitAttributes(Attrs);
461 Attrs.clear();
462 Attrs.Range = SourceRange();
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000463
464 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000465 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000466 if (Tok.is(tok::kw_typename)) {
Richard Smith6b3d3e52013-02-20 19:22:51 +0000467 TypenameLoc = ConsumeToken();
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000468 HasTypenameKeyword = true;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000469 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000470
471 // Parse nested-name-specifier.
Richard Smith2db075b2013-03-26 01:15:19 +0000472 IdentifierInfo *LastII = 0;
473 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
474 /*MayBePseudoDtor=*/0, /*IsTypename=*/false,
475 /*LastII=*/&LastII);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000476
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000477 // Check nested-name specifier.
478 if (SS.isInvalid()) {
479 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000480 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000481 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000482
Richard Smith2db075b2013-03-26 01:15:19 +0000483 SourceLocation TemplateKWLoc;
484 UnqualifiedId Name;
485
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000486 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000487 // destructor names and allow the action module to diagnose any semantic
488 // errors.
Richard Smith2db075b2013-03-26 01:15:19 +0000489 //
490 // C++11 [class.qual]p2:
491 // [...] in a using-declaration that is a member-declaration, if the name
492 // specified after the nested-name-specifier is the same as the identifier
493 // or the simple-template-id's template-name in the last component of the
494 // nested-name-specifier, the name is [...] considered to name the
495 // constructor.
496 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
497 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
498 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
499 !SS.getScopeRep()->getAsNamespace() &&
500 !SS.getScopeRep()->getAsNamespaceAlias()) {
501 SourceLocation IdLoc = ConsumeToken();
502 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
503 Name.setConstructorName(Type, IdLoc, IdLoc);
504 } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
505 /*AllowDestructorName=*/ true,
506 /*AllowConstructorName=*/ true, ParsedType(),
507 TemplateKWLoc, Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000508 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000509 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000510 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000511
Richard Smith6b3d3e52013-02-20 19:22:51 +0000512 MaybeParseCXX11Attributes(Attrs);
Richard Smith162e1c12011-04-15 14:24:37 +0000513
514 // Maybe this is an alias-declaration.
515 bool IsAliasDecl = Tok.is(tok::equal);
516 TypeResult TypeAlias;
517 if (IsAliasDecl) {
Richard Smith6b3d3e52013-02-20 19:22:51 +0000518 // TODO: Can GNU attributes appear here?
Richard Smith162e1c12011-04-15 14:24:37 +0000519 ConsumeToken();
520
Richard Smith80ad52f2013-01-02 11:42:31 +0000521 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000522 diag::warn_cxx98_compat_alias_declaration :
523 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000524
Richard Smith3e4c6c42011-05-05 21:57:07 +0000525 // Type alias templates cannot be specialized.
526 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000527 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
528 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000529 SpecKind = 0;
530 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
531 SpecKind = 1;
532 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
533 SpecKind = 2;
534 if (SpecKind != -1) {
535 SourceRange Range;
536 if (SpecKind == 0)
537 Range = SourceRange(Name.TemplateId->LAngleLoc,
538 Name.TemplateId->RAngleLoc);
539 else
540 Range = TemplateInfo.getSourceRange();
541 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
542 << SpecKind << Range;
543 SkipUntil(tok::semi);
544 return 0;
545 }
546
Richard Smith162e1c12011-04-15 14:24:37 +0000547 // Name must be an identifier.
548 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
549 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
550 // No removal fixit: can't recover from this.
551 SkipUntil(tok::semi);
552 return 0;
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000553 } else if (HasTypenameKeyword)
Richard Smith162e1c12011-04-15 14:24:37 +0000554 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
555 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
556 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
557 else if (SS.isNotEmpty())
558 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
559 << FixItHint::CreateRemoval(SS.getRange());
560
Richard Smith3e4c6c42011-05-05 21:57:07 +0000561 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
562 Declarator::AliasTemplateContext :
Richard Smith6b3d3e52013-02-20 19:22:51 +0000563 Declarator::AliasDeclContext, AS, OwnedType,
564 &Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000565 } else {
566 // C++11 attributes are not allowed on a using-declaration, but GNU ones
567 // are.
Richard Smith6b3d3e52013-02-20 19:22:51 +0000568 ProhibitAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000569
Richard Smith162e1c12011-04-15 14:24:37 +0000570 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith6b3d3e52013-02-20 19:22:51 +0000571 MaybeParseGNUAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000572 }
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000574 // Eat ';'.
575 DeclEnd = Tok.getLocation();
576 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000577 !Attrs.empty() ? "attributes list" :
Richard Smith162e1c12011-04-15 14:24:37 +0000578 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000579 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000580
John McCall78b81052010-11-10 02:40:36 +0000581 // Diagnose an attempt to declare a templated using-declaration.
Richard Smithd03de6a2013-01-29 10:02:16 +0000582 // In C++11, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000583 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000584 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000585 SourceRange R = TemplateInfo.getSourceRange();
586 Diag(UsingLoc, diag::err_templated_using_declaration)
587 << R << FixItHint::CreateRemoval(R);
588
589 // Unfortunately, we have to bail out instead of recovering by
590 // ignoring the parameters, just in case the nested name specifier
591 // depends on the parameters.
592 return 0;
593 }
594
Douglas Gregor480b53c2011-09-26 14:30:28 +0000595 // "typename" keyword is allowed for identifiers only,
596 // because it may be a type definition.
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000597 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor480b53c2011-09-26 14:30:28 +0000598 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
599 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000600 // Proceed parsing, but reset the HasTypenameKeyword flag.
601 HasTypenameKeyword = false;
Douglas Gregor480b53c2011-09-26 14:30:28 +0000602 }
603
Richard Smith3e4c6c42011-05-05 21:57:07 +0000604 if (IsAliasDecl) {
605 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramer5354e772012-08-23 23:38:35 +0000606 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3e4c6c42011-05-05 21:57:07 +0000607 TemplateParams ? TemplateParams->data() : 0,
608 TemplateParams ? TemplateParams->size() : 0);
609 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000610 UsingLoc, Name, Attrs.getList(),
611 TypeAlias);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000612 }
Richard Smith162e1c12011-04-15 14:24:37 +0000613
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000614 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
615 /* HasUsingKeyword */ true, UsingLoc,
616 SS, Name, Attrs.getList(),
617 HasTypenameKeyword, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000618}
619
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000620/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000621///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000622/// [C++0x] static_assert-declaration:
623/// static_assert ( constant-expression , string-literal ) ;
624///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000625/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000626/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000627///
John McCalld226f652010-08-21 09:40:31 +0000628Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000629 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
630 "Not a static_assert declaration");
631
David Blaikie4e4d0842012-03-11 07:00:24 +0000632 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000633 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000634 if (Tok.is(tok::kw_static_assert))
635 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000636
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000637 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000639 BalancedDelimiterTracker T(*this, tok::l_paren);
640 if (T.consumeOpen()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000641 Diag(Tok, diag::err_expected_lparen);
Richard Smith3686c712012-09-13 19:12:50 +0000642 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000643 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000644 }
Mike Stump1eb44332009-09-09 15:08:12 +0000645
John McCall60d7b3a2010-08-24 06:29:42 +0000646 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000647 if (AssertExpr.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000648 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000649 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000650 }
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Anders Carlssonad5f9602009-03-13 23:29:20 +0000652 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000653 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000654
Richard Smith0cc323c2012-03-05 23:20:05 +0000655 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000656 Diag(Tok, diag::err_expected_string_literal)
657 << /*Source='static_assert'*/1;
Richard Smith3686c712012-09-13 19:12:50 +0000658 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000659 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000660 }
Mike Stump1eb44332009-09-09 15:08:12 +0000661
John McCall60d7b3a2010-08-24 06:29:42 +0000662 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smith99831e42012-03-06 03:21:47 +0000663 if (AssertMessage.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000664 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000665 return 0;
Richard Smith99831e42012-03-06 03:21:47 +0000666 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000667
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000668 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Chris Lattner97144fc2009-04-02 04:16:50 +0000670 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000671 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000672
John McCall9ae2f072010-08-23 23:25:46 +0000673 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
674 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000675 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000676 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000677}
678
Richard Smitha2c36462013-04-26 16:15:35 +0000679/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000680///
681/// 'decltype' ( expression )
Richard Smitha2c36462013-04-26 16:15:35 +0000682/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000683///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000684SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
685 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
686 && "Not a decltype specifier");
687
David Blaikie42d6d0c2011-12-04 05:04:18 +0000688 ExprResult Result;
689 SourceLocation StartLoc = Tok.getLocation();
690 SourceLocation EndLoc;
691
692 if (Tok.is(tok::annot_decltype)) {
693 Result = getExprAnnotation(Tok);
694 EndLoc = Tok.getAnnotationEndLoc();
695 ConsumeToken();
696 if (Result.isInvalid()) {
697 DS.SetTypeSpecError();
698 return EndLoc;
699 }
700 } else {
Richard Smithc7b55432012-02-24 22:30:04 +0000701 if (Tok.getIdentifierInfo()->isStr("decltype"))
702 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000703
David Blaikie42d6d0c2011-12-04 05:04:18 +0000704 ConsumeToken();
705
706 BalancedDelimiterTracker T(*this, tok::l_paren);
707 if (T.expectAndConsume(diag::err_expected_lparen_after,
708 "decltype", tok::r_paren)) {
709 DS.SetTypeSpecError();
710 return T.getOpenLocation() == Tok.getLocation() ?
711 StartLoc : T.getOpenLocation();
712 }
713
Richard Smitha2c36462013-04-26 16:15:35 +0000714 // Check for C++1y 'decltype(auto)'.
715 if (Tok.is(tok::kw_auto)) {
716 // No need to disambiguate here: an expression can't start with 'auto',
717 // because the typename-specifier in a function-style cast operation can't
718 // be 'auto'.
719 Diag(Tok.getLocation(),
720 getLangOpts().CPlusPlus1y
721 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
722 : diag::ext_decltype_auto_type_specifier);
723 ConsumeToken();
724 } else {
725 // Parse the expression
David Blaikie42d6d0c2011-12-04 05:04:18 +0000726
Richard Smitha2c36462013-04-26 16:15:35 +0000727 // C++11 [dcl.type.simple]p4:
728 // The operand of the decltype specifier is an unevaluated operand.
729 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
730 0, /*IsDecltype=*/true);
731 Result = ParseExpression();
732 if (Result.isInvalid()) {
733 DS.SetTypeSpecError();
734 if (SkipUntil(tok::r_paren, /*StopAtSemi=*/true,
735 /*DontConsume=*/true)) {
736 EndLoc = ConsumeParen();
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000737 } else {
Richard Smitha2c36462013-04-26 16:15:35 +0000738 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
739 // Backtrack to get the location of the last token before the semi.
740 PP.RevertCachedTokens(2);
741 ConsumeToken(); // the semi.
742 EndLoc = ConsumeAnyToken();
743 assert(Tok.is(tok::semi));
744 } else {
745 EndLoc = Tok.getLocation();
746 }
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000747 }
Richard Smitha2c36462013-04-26 16:15:35 +0000748 return EndLoc;
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000749 }
Richard Smitha2c36462013-04-26 16:15:35 +0000750
751 Result = Actions.ActOnDecltypeExpression(Result.take());
David Blaikie42d6d0c2011-12-04 05:04:18 +0000752 }
753
754 // Match the ')'
755 T.consumeClose();
756 if (T.getCloseLocation().isInvalid()) {
757 DS.SetTypeSpecError();
758 // FIXME: this should return the location of the last token
759 // that was consumed (by "consumeClose()")
760 return T.getCloseLocation();
761 }
762
Richard Smith76f3f692012-02-22 02:04:18 +0000763 if (Result.isInvalid()) {
764 DS.SetTypeSpecError();
765 return T.getCloseLocation();
766 }
767
David Blaikie42d6d0c2011-12-04 05:04:18 +0000768 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000769 }
Richard Smitha2c36462013-04-26 16:15:35 +0000770 assert(!Result.isInvalid());
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000772 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000773 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000774 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smitha2c36462013-04-26 16:15:35 +0000775 if (Result.get()
776 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
777 DiagID, Result.release())
778 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
779 DiagID)) {
John McCallfec54012009-08-03 20:12:06 +0000780 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000781 DS.SetTypeSpecError();
782 }
783 return EndLoc;
784}
785
786void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
787 SourceLocation StartLoc,
788 SourceLocation EndLoc) {
789 // make sure we have a token we can turn into an annotation token
790 if (PP.isBacktrackEnabled())
791 PP.RevertCachedTokens(1);
792 else
793 PP.EnterToken(Tok);
794
795 Tok.setKind(tok::annot_decltype);
Richard Smitha2c36462013-04-26 16:15:35 +0000796 setExprAnnotation(Tok,
797 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
798 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
799 ExprError());
David Blaikie42d6d0c2011-12-04 05:04:18 +0000800 Tok.setAnnotationEndLoc(EndLoc);
801 Tok.setLocation(StartLoc);
802 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000803}
804
Sean Huntdb5d44b2011-05-19 05:37:45 +0000805void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
806 assert(Tok.is(tok::kw___underlying_type) &&
807 "Not an underlying type specifier");
808
809 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000810 BalancedDelimiterTracker T(*this, tok::l_paren);
811 if (T.expectAndConsume(diag::err_expected_lparen_after,
812 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000813 return;
814 }
815
816 TypeResult Result = ParseTypeName();
817 if (Result.isInvalid()) {
818 SkipUntil(tok::r_paren);
819 return;
820 }
821
822 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000823 T.consumeClose();
824 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000825 return;
826
827 const char *PrevSpec = 0;
828 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000829 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000830 DiagID, Result.release()))
831 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanella2d776342013-07-06 18:54:58 +0000832 DS.setTypeofParensRange(T.getRange());
Sean Huntdb5d44b2011-05-19 05:37:45 +0000833}
834
David Blaikie09048df2011-10-25 15:01:20 +0000835/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
836/// class name or decltype-specifier. Note that we only check that the result
837/// names a type; semantic analysis will need to verify that the type names a
838/// class. The result is either a type or null, depending on whether a type
839/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000840///
Richard Smith05321402013-02-19 23:47:15 +0000841/// base-type-specifier: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000842/// class-or-decltype
Richard Smith05321402013-02-19 23:47:15 +0000843/// class-or-decltype: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000844/// nested-name-specifier[opt] class-name
845/// decltype-specifier
Richard Smith05321402013-02-19 23:47:15 +0000846/// class-name: [C++ class.name]
Douglas Gregor42a552f2008-11-05 20:51:48 +0000847/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000848/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000849///
Richard Smith05321402013-02-19 23:47:15 +0000850/// In C++98, instead of base-type-specifier, we have:
851///
852/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie22216eb2011-10-25 17:10:12 +0000853Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
854 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000855 // Ignore attempts to use typename
856 if (Tok.is(tok::kw_typename)) {
857 Diag(Tok, diag::err_expected_class_name_not_template)
858 << FixItHint::CreateRemoval(Tok.getLocation());
859 ConsumeToken();
860 }
861
David Blaikie152aa4b2011-10-25 18:17:58 +0000862 // Parse optional nested-name-specifier
863 CXXScopeSpec SS;
864 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
865
866 BaseLoc = Tok.getLocation();
867
David Blaikie22216eb2011-10-25 17:10:12 +0000868 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000869 // tok == kw_decltype is just error recovery, it can only happen when SS
870 // isn't empty
871 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000872 if (SS.isNotEmpty())
873 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
874 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000875 // Fake up a Declarator to use with ActOnTypeName.
876 DeclSpec DS(AttrFactory);
877
David Blaikieb5777572011-12-08 04:53:15 +0000878 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000879
880 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
881 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
882 }
883
Douglas Gregor7f43d672009-02-25 23:52:28 +0000884 // Check whether we have a template-id that names a type.
885 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000886 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000887 if (TemplateId->Kind == TNK_Type_template ||
888 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000889 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000890
891 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000892 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000893 EndLocation = Tok.getAnnotationEndLoc();
894 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000895
896 if (Type)
897 return Type;
898 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000899 }
900
901 // Fall through to produce an error below.
902 }
903
Douglas Gregor42a552f2008-11-05 20:51:48 +0000904 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000905 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000906 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000907 }
908
Douglas Gregor84d0a192010-01-12 21:28:44 +0000909 IdentifierInfo *Id = Tok.getIdentifierInfo();
910 SourceLocation IdLoc = ConsumeToken();
911
912 if (Tok.is(tok::less)) {
913 // It looks the user intended to write a template-id here, but the
914 // template-name was wrong. Try to fix that.
915 TemplateNameKind TNK = TNK_Type_template;
916 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000917 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000918 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000919 Diag(IdLoc, diag::err_unknown_template_name)
920 << Id;
921 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000922
Serge Pavlov62f675c2013-08-10 05:54:47 +0000923 if (!Template) {
924 TemplateArgList TemplateArgs;
925 SourceLocation LAngleLoc, RAngleLoc;
926 ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
927 true, LAngleLoc, TemplateArgs, RAngleLoc);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000928 return true;
Serge Pavlov62f675c2013-08-10 05:54:47 +0000929 }
Douglas Gregor84d0a192010-01-12 21:28:44 +0000930
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000931 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000932 UnqualifiedId TemplateName;
933 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000934
Douglas Gregor84d0a192010-01-12 21:28:44 +0000935 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000936 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
937 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000938 return true;
939 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000940 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000941
Douglas Gregor84d0a192010-01-12 21:28:44 +0000942 // If we didn't end up with a typename token, there's nothing more we
943 // can do.
944 if (Tok.isNot(tok::annot_typename))
945 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000946
Douglas Gregor84d0a192010-01-12 21:28:44 +0000947 // Retrieve the type from the annotation token, consume that token, and
948 // return.
949 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000950 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000951 ConsumeToken();
952 return Type;
953 }
954
Douglas Gregor42a552f2008-11-05 20:51:48 +0000955 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000956 IdentifierInfo *CorrectedII = 0;
Douglas Gregor059101f2011-03-02 00:47:37 +0000957 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000958 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000959 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000960 /*NonTrivialTypeSourceInfo=*/true,
961 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000962 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000963 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000964 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000965 }
966
967 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000968 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000969
970 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000971 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000972 DS.SetRangeStart(IdLoc);
973 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000974 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000975
976 const char *PrevSpec = 0;
977 unsigned DiagID;
978 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
979
980 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
981 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000982}
983
John McCallc052dbb2012-05-22 21:28:12 +0000984void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
985 while (Tok.is(tok::kw___single_inheritance) ||
986 Tok.is(tok::kw___multiple_inheritance) ||
987 Tok.is(tok::kw___virtual_inheritance)) {
988 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
989 SourceLocation AttrNameLoc = ConsumeToken();
990 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000991 SourceLocation(), 0, 0, AttributeList::AS_GNU);
John McCallc052dbb2012-05-22 21:28:12 +0000992 }
993}
994
Richard Smithc9f35172012-06-25 21:37:02 +0000995/// Determine whether the following tokens are valid after a type-specifier
996/// which could be a standalone declaration. This will conservatively return
997/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith139be702012-07-02 19:14:01 +0000998bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smithc9f35172012-06-25 21:37:02 +0000999 // This switch enumerates the valid "follow" set for type-specifiers.
1000 switch (Tok.getKind()) {
1001 default: break;
1002 case tok::semi: // struct foo {...} ;
1003 case tok::star: // struct foo {...} * P;
1004 case tok::amp: // struct foo {...} & R = ...
Richard Smithba65f502013-01-19 03:48:05 +00001005 case tok::ampamp: // struct foo {...} && R = ...
Richard Smithc9f35172012-06-25 21:37:02 +00001006 case tok::identifier: // struct foo {...} V ;
1007 case tok::r_paren: //(struct foo {...} ) {4}
1008 case tok::annot_cxxscope: // struct foo {...} a:: b;
1009 case tok::annot_typename: // struct foo {...} a ::b;
1010 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1011 case tok::l_paren: // struct foo {...} ( x);
1012 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smithba65f502013-01-19 03:48:05 +00001013 case tok::kw_operator: // struct foo operator ++() {...}
Richard Smithc9f35172012-06-25 21:37:02 +00001014 return true;
Richard Smith139be702012-07-02 19:14:01 +00001015 case tok::colon:
1016 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smithc9f35172012-06-25 21:37:02 +00001017 // Type qualifiers
1018 case tok::kw_const: // struct foo {...} const x;
1019 case tok::kw_volatile: // struct foo {...} volatile x;
1020 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smithba65f502013-01-19 03:48:05 +00001021 // Function specifiers
1022 // Note, no 'explicit'. An explicit function must be either a conversion
1023 // operator or a constructor. Either way, it can't have a return type.
1024 case tok::kw_inline: // struct foo inline f();
1025 case tok::kw_virtual: // struct foo virtual f();
1026 case tok::kw_friend: // struct foo friend f();
Richard Smithc9f35172012-06-25 21:37:02 +00001027 // Storage-class specifiers
1028 case tok::kw_static: // struct foo {...} static x;
1029 case tok::kw_extern: // struct foo {...} extern x;
1030 case tok::kw_typedef: // struct foo {...} typedef x;
1031 case tok::kw_register: // struct foo {...} register x;
1032 case tok::kw_auto: // struct foo {...} auto x;
1033 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smithba65f502013-01-19 03:48:05 +00001034 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smithc9f35172012-06-25 21:37:02 +00001035 case tok::kw_constexpr: // struct foo {...} constexpr x;
1036 // As shown above, type qualifiers and storage class specifiers absolutely
1037 // can occur after class specifiers according to the grammar. However,
1038 // almost no one actually writes code like this. If we see one of these,
1039 // it is much more likely that someone missed a semi colon and the
1040 // type/storage class specifier we're seeing is part of the *next*
1041 // intended declaration, as in:
1042 //
1043 // struct foo { ... }
1044 // typedef int X;
1045 //
1046 // We'd really like to emit a missing semicolon error instead of emitting
1047 // an error on the 'int' saying that you can't have two type specifiers in
1048 // the same declaration of X. Because of this, we look ahead past this
1049 // token to see if it's a type specifier. If so, we know the code is
1050 // otherwise invalid, so we can produce the expected semi error.
1051 if (!isKnownToBeTypeSpecifier(NextToken()))
1052 return true;
1053 break;
1054 case tok::r_brace: // struct bar { struct foo {...} }
1055 // Missing ';' at end of struct is accepted as an extension in C mode.
1056 if (!getLangOpts().CPlusPlus)
1057 return true;
1058 break;
Richard Smithba65f502013-01-19 03:48:05 +00001059 // C++11 attributes
1060 case tok::l_square: // enum E [[]] x
1061 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1062 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith8338a9d2013-01-29 04:13:32 +00001063 case tok::greater:
1064 // template<class T = class X>
1065 return getLangOpts().CPlusPlus;
Richard Smithc9f35172012-06-25 21:37:02 +00001066 }
1067 return false;
1068}
1069
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001070/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1071/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1072/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +00001073/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001074///
1075/// class-specifier: [C++ class]
1076/// class-head '{' member-specification[opt] '}'
1077/// class-head '{' member-specification[opt] '}' attributes[opt]
1078/// class-head:
1079/// class-key identifier[opt] base-clause[opt]
1080/// class-key nested-name-specifier identifier base-clause[opt]
1081/// class-key nested-name-specifier[opt] simple-template-id
1082/// base-clause[opt]
1083/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001084/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001085/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001086/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001087/// simple-template-id base-clause[opt]
1088/// class-key:
1089/// 'class'
1090/// 'struct'
1091/// 'union'
1092///
1093/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +00001094/// class-key ::[opt] nested-name-specifier[opt] identifier
1095/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1096/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001097///
1098/// Note that the C++ class-specifier and elaborated-type-specifier,
1099/// together, subsume the C99 struct-or-union-specifier:
1100///
1101/// struct-or-union-specifier: [C99 6.7.2.1]
1102/// struct-or-union identifier[opt] '{' struct-contents '}'
1103/// struct-or-union identifier
1104/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1105/// '}' attributes[opt]
1106/// [GNU] struct-or-union attributes[opt] identifier
1107/// struct-or-union:
1108/// 'struct'
1109/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +00001110void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1111 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001112 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001113 AccessSpecifier AS,
Michael Han2e397132012-11-26 22:54:45 +00001114 bool EnteringContext, DeclSpecContext DSC,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001115 ParsedAttributesWithRange &Attributes) {
Joao Matos17d35c32012-08-31 22:18:20 +00001116 DeclSpec::TST TagType;
1117 if (TagTokKind == tok::kw_struct)
1118 TagType = DeclSpec::TST_struct;
1119 else if (TagTokKind == tok::kw___interface)
1120 TagType = DeclSpec::TST_interface;
1121 else if (TagTokKind == tok::kw_class)
1122 TagType = DeclSpec::TST_class;
1123 else {
Chris Lattner4c97d762009-04-12 21:49:30 +00001124 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1125 TagType = DeclSpec::TST_union;
1126 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001127
Douglas Gregor374929f2009-09-18 15:37:17 +00001128 if (Tok.is(tok::code_completion)) {
1129 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001130 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001131 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00001132 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001133
Chandler Carruth926c4b42010-06-28 08:39:25 +00001134 // C++03 [temp.explicit] 14.7.2/8:
1135 // The usual access checking rules do not apply to names used to specify
1136 // explicit instantiations.
1137 //
1138 // As an extension we do not perform access checking on the names used to
1139 // specify explicit specializations either. This is important to allow
1140 // specializing traits classes for private types.
John McCall13489672012-05-07 06:16:58 +00001141 //
1142 // Note that we don't suppress if this turns out to be an elaborated
1143 // type specifier.
1144 bool shouldDelayDiagsInTag =
1145 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1146 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1147 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001148
Sean Hunt2edf0a22012-06-23 05:07:58 +00001149 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001150 // If attributes exist after tag, parse them.
1151 if (Tok.is(tok::kw___attribute))
John McCall7f040a92010-12-24 02:08:15 +00001152 ParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001153
Steve Narofff59e17e2008-12-24 20:59:21 +00001154 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001155 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001156 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001157
John McCallc052dbb2012-05-22 21:28:12 +00001158 // Parse inheritance specifiers.
1159 if (Tok.is(tok::kw___single_inheritance) ||
1160 Tok.is(tok::kw___multiple_inheritance) ||
1161 Tok.is(tok::kw___virtual_inheritance))
1162 ParseMicrosoftInheritanceClassAttributes(attrs);
1163
Sean Huntbbd37c62009-11-21 08:43:09 +00001164 // If C++0x attributes exist here, parse them.
1165 // FIXME: Are we consistent with the ordering of parsing of different
1166 // styles of attributes?
Richard Smith4e24f0f2013-01-02 12:01:23 +00001167 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Michael Han07fc1ba2013-01-07 16:57:11 +00001169 // Source location used by FIXIT to insert misplaced
1170 // C++11 attributes
1171 SourceLocation AttrFixitLoc = Tok.getLocation();
1172
John Wiegley20c0da72011-04-27 23:09:49 +00001173 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +00001174 !Tok.is(tok::identifier) &&
1175 Tok.getIdentifierInfo() &&
1176 (Tok.is(tok::kw___is_arithmetic) ||
1177 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001178 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001179 Tok.is(tok::kw___is_floating_point) ||
1180 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001181 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001182 Tok.is(tok::kw___is_integral) ||
1183 Tok.is(tok::kw___is_member_function_pointer) ||
1184 Tok.is(tok::kw___is_member_pointer) ||
1185 Tok.is(tok::kw___is_pod) ||
1186 Tok.is(tok::kw___is_pointer) ||
1187 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001188 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001189 Tok.is(tok::kw___is_signed) ||
1190 Tok.is(tok::kw___is_unsigned) ||
1191 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001192 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001193 // name of struct templates, but some are keywords in GCC >= 4.3
1194 // and Clang. Therefore, when we see the token sequence "struct
1195 // X", make X into a normal identifier rather than a keyword, to
1196 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001197 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001198 Tok.setKind(tok::identifier);
1199 }
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001201 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001202 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001203 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001204 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1205 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001206
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001207 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001208 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001209 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001210 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1211 Diag(Tok, diag::err_expected_ident);
1212 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001213
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001214 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1215
Douglas Gregorcc636682009-02-17 23:15:12 +00001216 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001217 IdentifierInfo *Name = 0;
1218 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001219 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001220 if (Tok.is(tok::identifier)) {
1221 Name = Tok.getIdentifierInfo();
1222 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001223
David Blaikie4e4d0842012-03-11 07:00:24 +00001224 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001225 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001226 // Eat the template argument list and try to continue parsing this as
1227 // a class (or template thereof).
1228 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001229 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001230 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001231 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001232 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001233 // We couldn't parse the template argument list at all, so don't
1234 // try to give any location information for the list.
1235 LAngleLoc = RAngleLoc = SourceLocation();
1236 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001237
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001238 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matos17d35c32012-08-31 22:18:20 +00001239 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1240 << (TagType == DeclSpec::TST_class? 0
1241 : TagType == DeclSpec::TST_struct? 1
1242 : TagType == DeclSpec::TST_interface? 2
1243 : 3)
1244 << Name
1245 << SourceRange(LAngleLoc, RAngleLoc);
1246
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001247 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001248 // we've removed its template argument list.
1249 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1250 if (TemplateParams && TemplateParams->size() > 1) {
1251 TemplateParams->pop_back();
1252 } else {
1253 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001254 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001255 = ParsedTemplateInfo::NonTemplate;
1256 }
1257 } else if (TemplateInfo.Kind
1258 == ParsedTemplateInfo::ExplicitInstantiation) {
1259 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001260 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001261 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001262 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001263 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001264 = SourceLocation();
1265 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1266 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001267 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001268 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001269 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001270 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001271 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001272
Douglas Gregor059101f2011-03-02 00:47:37 +00001273 if (TemplateId->Kind != TNK_Type_template &&
1274 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001275 // The template-name in the simple-template-id refers to
1276 // something other than a class template. Give an appropriate
1277 // error message and skip to the ';'.
1278 SourceRange Range(NameLoc);
1279 if (SS.isNotEmpty())
1280 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001281
Douglas Gregor39a8de12009-02-25 19:37:18 +00001282 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu6e91f4b2013-06-19 22:25:01 +00001283 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Douglas Gregor39a8de12009-02-25 19:37:18 +00001285 DS.SetTypeSpecError();
1286 SkipUntil(tok::semi, false, true);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001287 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001288 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001289 }
1290
Richard Smith7796eb52012-03-12 08:56:40 +00001291 // There are four options here.
1292 // - If we are in a trailing return type, this is always just a reference,
1293 // and we must not try to parse a definition. For instance,
1294 // [] () -> struct S { };
1295 // does not define a type.
1296 // - If we have 'struct foo {...', 'struct foo :...',
1297 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1298 // - If we have 'struct foo;', then this is either a forward declaration
1299 // or a friend declaration, which have to be treated differently.
1300 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han2e397132012-11-26 22:54:45 +00001301 //
1302 // We also detect these erroneous cases to provide better diagnostic for
1303 // C++11 attributes parsing.
1304 // - attributes follow class name:
1305 // struct foo [[]] {};
1306 // - attributes appear before or after 'final':
1307 // struct foo [[]] final [[]] {};
1308 //
Richard Smith69730c12012-03-12 07:56:15 +00001309 // However, in type-specifier-seq's, things look like declarations but are
1310 // just references, e.g.
1311 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001312 // or
Richard Smith69730c12012-03-12 07:56:15 +00001313 // &T::operator struct s;
1314 // For these, DSC is DSC_type_specifier.
Michael Han2e397132012-11-26 22:54:45 +00001315
1316 // If there are attributes after class name, parse them.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001317 MaybeParseCXX11Attributes(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00001318
John McCallf312b1e2010-08-26 23:41:50 +00001319 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001320 if (DSC == DSC_trailing)
1321 TUK = Sema::TUK_Reference;
1322 else if (Tok.is(tok::l_brace) ||
1323 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001324 (isCXX11FinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001325 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001326 if (DS.isFriendSpecified()) {
1327 // C++ [class.friend]p2:
1328 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001329 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001330 << SourceRange(DS.getFriendSpecLoc());
1331
1332 // Skip everything up to the semicolon, so that this looks like a proper
1333 // friend class (or template thereof) declaration.
1334 SkipUntil(tok::semi, true, true);
John McCallf312b1e2010-08-26 23:41:50 +00001335 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001336 } else {
1337 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001338 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001339 }
Richard Smith150d8532013-02-22 06:46:23 +00001340 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1341 NextToken().is(tok::kw_alignas))) {
Michael Han2e397132012-11-26 22:54:45 +00001342 // We can't tell if this is a definition or reference
1343 // until we skipped the 'final' and C++11 attribute specifiers.
1344 TentativeParsingAction PA(*this);
1345
1346 // Skip the 'final' keyword.
1347 ConsumeToken();
1348
1349 // Skip C++11 attribute specifiers.
1350 while (true) {
1351 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1352 ConsumeBracket();
1353 if (!SkipUntil(tok::r_square))
1354 break;
Richard Smith150d8532013-02-22 06:46:23 +00001355 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han2e397132012-11-26 22:54:45 +00001356 ConsumeToken();
1357 ConsumeParen();
1358 if (!SkipUntil(tok::r_paren))
1359 break;
1360 } else {
1361 break;
1362 }
1363 }
1364
1365 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1366 TUK = Sema::TUK_Definition;
1367 else
1368 TUK = Sema::TUK_Reference;
1369
1370 PA.Revert();
Richard Smithc9f35172012-06-25 21:37:02 +00001371 } else if (DSC != DSC_type_specifier &&
1372 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00001373 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallf312b1e2010-08-26 23:41:50 +00001374 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matos17d35c32012-08-31 22:18:20 +00001375 if (Tok.isNot(tok::semi)) {
1376 // A semicolon was missing after this declaration. Diagnose and recover.
1377 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1378 DeclSpec::getSpecifierName(TagType));
1379 PP.EnterToken(Tok);
1380 Tok.setKind(tok::semi);
1381 }
Richard Smithc9f35172012-06-25 21:37:02 +00001382 } else
John McCallf312b1e2010-08-26 23:41:50 +00001383 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001384
Michael Han2e397132012-11-26 22:54:45 +00001385 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1386 // to caller to handle.
Michael Han07fc1ba2013-01-07 16:57:11 +00001387 if (TUK != Sema::TUK_Reference) {
1388 // If this is not a reference, then the only possible
1389 // valid place for C++11 attributes to appear here
1390 // is between class-key and class-name. If there are
1391 // any attributes after class-name, we try a fixit to move
1392 // them to the right place.
1393 SourceRange AttrRange = Attributes.Range;
1394 if (AttrRange.isValid()) {
1395 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1396 << AttrRange
1397 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1398 CharSourceRange(AttrRange, true))
1399 << FixItHint::CreateRemoval(AttrRange);
1400
1401 // Recover by adding misplaced attributes to the attribute list
1402 // of the class so they can be applied on the class later.
1403 attrs.takeAllFrom(Attributes);
1404 }
1405 }
Michael Han2e397132012-11-26 22:54:45 +00001406
John McCall13489672012-05-07 06:16:58 +00001407 // If this is an elaborated type specifier, and we delayed
1408 // diagnostics before, just merge them into the current pool.
1409 if (shouldDelayDiagsInTag) {
1410 diagsFromTag.done();
1411 if (TUK == Sema::TUK_Reference)
1412 diagsFromTag.redelay();
1413 }
1414
John McCall207014e2010-07-30 06:26:29 +00001415 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001416 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001417 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1418 // We have a declaration or reference to an anonymous class.
1419 Diag(StartLoc, diag::err_anon_type_definition)
1420 << DeclSpec::getSpecifierName(TagType);
1421 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001422
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001423 SkipUntil(tok::comma, true);
1424 return;
1425 }
1426
Douglas Gregorddc29e12009-02-06 22:42:48 +00001427 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001428 DeclResult TagOrTempResult = true; // invalid
1429 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001430
Douglas Gregor402abb52009-05-28 23:31:59 +00001431 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001432 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001433 // Explicit specialization, class template partial specialization,
1434 // or explicit instantiation.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001435 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001436 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001437 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001438 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001439 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001440 ProhibitAttributes(attrs);
1441
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001442 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001443 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001444 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001445 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001446 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001447 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001448 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001449 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001450 TemplateId->TemplateNameLoc,
1451 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001452 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001453 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001454 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001455
1456 // Friend template-ids are treated as references unless
1457 // they have template headers, in which case they're ill-formed
1458 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1459 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001460 } else if (TUK == Sema::TUK_Reference ||
1461 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001462 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001463 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001464 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001465 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001466 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001467 TemplateId->Template,
1468 TemplateId->TemplateNameLoc,
1469 TemplateId->LAngleLoc,
1470 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001471 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001472 } else {
1473 // This is an explicit specialization or a class template
1474 // partial specialization.
1475 TemplateParameterLists FakedParamLists;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001476 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1477 // This looks like an explicit instantiation, because we have
1478 // something like
1479 //
1480 // template class Foo<X>
1481 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001482 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001483 // meant to be an explicit specialization, but the user forgot
1484 // the '<>' after 'template'.
Larisse Voufo49854292013-06-22 13:56:11 +00001485 // It this is friend declaration however, since it cannot have a
1486 // template header, it is most likely that the user meant to
1487 // remove the 'template' keyword.
1488 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
1489 "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001490
Larisse Voufo49854292013-06-22 13:56:11 +00001491 if (TUK == Sema::TUK_Friend) {
1492 Diag(DS.getFriendSpecLoc(),
1493 diag::err_friend_explicit_instantiation);
1494 TemplateParams = 0;
1495 } else {
1496 SourceLocation LAngleLoc
1497 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1498 Diag(TemplateId->TemplateNameLoc,
1499 diag::err_explicit_instantiation_with_definition)
1500 << SourceRange(TemplateInfo.TemplateLoc)
1501 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1502
1503 // Create a fake template parameter list that contains only
1504 // "template<>", so that we treat this construct as a class
1505 // template specialization.
1506 FakedParamLists.push_back(
1507 Actions.ActOnTemplateParameterList(0, SourceLocation(),
1508 TemplateInfo.TemplateLoc,
1509 LAngleLoc,
1510 0, 0,
1511 LAngleLoc));
1512 TemplateParams = &FakedParamLists;
1513 }
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001514 }
1515
1516 // Build the class template specialization.
1517 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001518 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001519 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001520 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001521 TemplateId->TemplateNameLoc,
1522 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001523 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001524 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001525 attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001526 MultiTemplateParamsArg(
Douglas Gregorcc636682009-02-17 23:15:12 +00001527 TemplateParams? &(*TemplateParams)[0] : 0,
1528 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001529 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001530 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001531 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001532 // Explicit instantiation of a member of a class template
1533 // specialization, e.g.,
1534 //
1535 // template struct Outer<int>::Inner;
1536 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001537 ProhibitAttributes(attrs);
1538
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001539 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001540 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001541 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001542 TemplateInfo.TemplateLoc,
1543 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001544 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001545 } else if (TUK == Sema::TUK_Friend &&
1546 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001547 ProhibitAttributes(attrs);
1548
John McCall9a34edb2010-10-19 01:40:49 +00001549 TagOrTempResult =
1550 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1551 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001552 Name, NameLoc, attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001553 MultiTemplateParamsArg(
John McCall9a34edb2010-10-19 01:40:49 +00001554 TemplateParams? &(*TemplateParams)[0] : 0,
1555 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001556 } else {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001557 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1558 ProhibitAttributes(attrs);
Larisse Voufo7c64ef02013-06-21 00:08:46 +00001559
1560 if (TUK == Sema::TUK_Definition &&
1561 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1562 // If the declarator-id is not a template-id, issue a diagnostic and
1563 // recover by ignoring the 'template' keyword.
1564 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1565 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Larisse Voufo49854292013-06-22 13:56:11 +00001566 TemplateParams = 0;
Larisse Voufo7c64ef02013-06-21 00:08:46 +00001567 }
Sean Hunt2edf0a22012-06-23 05:07:58 +00001568
John McCallc4e70192009-09-11 04:59:25 +00001569 bool IsDependent = false;
1570
John McCalla25c4082010-10-19 18:40:57 +00001571 // Don't pass down template parameter lists if this is just a tag
1572 // reference. For example, we don't need the template parameters here:
1573 // template <class T> class A *makeA(T t);
1574 MultiTemplateParamsArg TParams;
1575 if (TUK != Sema::TUK_Reference && TemplateParams)
1576 TParams =
1577 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1578
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001579 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001580 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001581 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001582 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001583 TParams, Owned, IsDependent,
1584 SourceLocation(), false,
1585 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001586
1587 // If ActOnTag said the type was dependent, try again with the
1588 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001589 if (IsDependent) {
1590 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001591 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001592 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001593 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001594 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001595
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001596 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001597 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001598 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001599 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001600 isCXX11FinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001601 if (getLangOpts().CPlusPlus)
Michael Han07fc1ba2013-01-07 16:57:11 +00001602 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1603 TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001604 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001605 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001606 }
1607
John McCallb3d87482010-08-24 05:47:05 +00001608 const char *PrevSpec = 0;
1609 unsigned DiagID;
1610 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001611 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001612 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1613 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001614 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001615 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001616 Result = DS.SetTypeSpecType(TagType, StartLoc,
1617 NameLoc.isValid() ? NameLoc : StartLoc,
1618 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001619 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001620 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001621 return;
1622 }
Mike Stump1eb44332009-09-09 15:08:12 +00001623
John McCallb3d87482010-08-24 05:47:05 +00001624 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001625 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001626
Chris Lattner4ed5d912010-02-02 01:23:29 +00001627 // At this point, we've successfully parsed a class-specifier in 'definition'
1628 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1629 // going to look at what comes after it to improve error recovery. If an
1630 // impossible token occurs next, we assume that the programmer forgot a ; at
1631 // the end of the declaration and recover that way.
1632 //
Richard Smithc9f35172012-06-25 21:37:02 +00001633 // Also enforce C++ [temp]p3:
1634 // In a template-declaration which defines a class, no declarator
1635 // is permitted.
Joao Matos17d35c32012-08-31 22:18:20 +00001636 if (TUK == Sema::TUK_Definition &&
1637 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidis7d033b22012-12-17 20:10:43 +00001638 if (Tok.isNot(tok::semi)) {
1639 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1640 DeclSpec::getSpecifierName(TagType));
1641 // Push this token back into the preprocessor and change our current token
1642 // to ';' so that the rest of the code recovers as though there were an
1643 // ';' after the definition.
1644 PP.EnterToken(Tok);
1645 Tok.setKind(tok::semi);
1646 }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001647 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001648}
1649
Mike Stump1eb44332009-09-09 15:08:12 +00001650/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001651///
1652/// base-clause : [C++ class.derived]
1653/// ':' base-specifier-list
1654/// base-specifier-list:
1655/// base-specifier '...'[opt]
1656/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001657void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001658 assert(Tok.is(tok::colon) && "Not a base clause");
1659 ConsumeToken();
1660
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001661 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001662 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001663
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001664 while (true) {
1665 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001666 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001667 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001668 // Skip the rest of this base specifier, up until the comma or
1669 // opening brace.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001670 SkipUntil(tok::comma, tok::l_brace, true, true);
1671 } else {
1672 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001673 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001674 }
1675
1676 // If the next token is a comma, consume it and keep reading
1677 // base-specifiers.
1678 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001679
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001680 // Consume the comma.
1681 ConsumeToken();
1682 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001683
1684 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001685 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001686}
1687
1688/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1689/// one entry in the base class list of a class specifier, for example:
1690/// class foo : public bar, virtual private baz {
1691/// 'public bar' and 'virtual private baz' are each base-specifiers.
1692///
1693/// base-specifier: [C++ class.derived]
Richard Smith05321402013-02-19 23:47:15 +00001694/// attribute-specifier-seq[opt] base-type-specifier
1695/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1696/// base-type-specifier
1697/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1698/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001699Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001700 bool IsVirtual = false;
1701 SourceLocation StartLoc = Tok.getLocation();
1702
Richard Smith05321402013-02-19 23:47:15 +00001703 ParsedAttributesWithRange Attributes(AttrFactory);
1704 MaybeParseCXX11Attributes(Attributes);
1705
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001706 // Parse the 'virtual' keyword.
1707 if (Tok.is(tok::kw_virtual)) {
1708 ConsumeToken();
1709 IsVirtual = true;
1710 }
1711
Richard Smith05321402013-02-19 23:47:15 +00001712 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1713
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001714 // Parse an (optional) access specifier.
1715 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001716 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001717 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Richard Smith05321402013-02-19 23:47:15 +00001719 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1720
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001721 // Parse the 'virtual' keyword (again!), in case it came after the
1722 // access specifier.
1723 if (Tok.is(tok::kw_virtual)) {
1724 SourceLocation VirtualLoc = ConsumeToken();
1725 if (IsVirtual) {
1726 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001727 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001728 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001729 }
1730
1731 IsVirtual = true;
1732 }
1733
Richard Smith05321402013-02-19 23:47:15 +00001734 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1735
Douglas Gregor42a552f2008-11-05 20:51:48 +00001736 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001737 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001738 SourceLocation BaseLoc;
1739 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001740 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001741 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001743 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1744 // actually part of the base-specifier-list grammar productions, but we
1745 // parse it here for convenience.
1746 SourceLocation EllipsisLoc;
1747 if (Tok.is(tok::ellipsis))
1748 EllipsisLoc = ConsumeToken();
1749
Mike Stump1eb44332009-09-09 15:08:12 +00001750 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001751 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001752
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001753 // Notify semantic analysis that we have parsed a complete
1754 // base-specifier.
Richard Smith05321402013-02-19 23:47:15 +00001755 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1756 Access, BaseType.get(), BaseLoc,
1757 EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001758}
1759
1760/// getAccessSpecifierIfPresent - Determine whether the next token is
1761/// a C++ access-specifier.
1762///
1763/// access-specifier: [C++ class.derived]
1764/// 'private'
1765/// 'protected'
1766/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001767AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001768 switch (Tok.getKind()) {
1769 default: return AS_none;
1770 case tok::kw_private: return AS_private;
1771 case tok::kw_protected: return AS_protected;
1772 case tok::kw_public: return AS_public;
1773 }
1774}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001775
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001776/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001777/// delayed, e.g., default arguments, create a late-parsed method declaration
1778/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001779void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1780 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001781 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001782 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001783 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001784 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001785 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001786
Eli Friedmand33133c2009-07-22 21:45:50 +00001787 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1788 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1789 if (!LateMethod) {
1790 // Push this method onto the stack of late-parsed method
1791 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001792 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1793 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001794 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001795
1796 // Add all of the parameters prior to this one (they don't
1797 // have default arguments).
1798 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1799 for (unsigned I = 0; I < ParamIdx; ++I)
1800 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001801 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001802 }
1803
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001804 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001805 // not have a default argument).
1806 LateMethod->DefaultArgs.push_back(
1807 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1808 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1809 }
1810 }
1811}
1812
Richard Smith4e24f0f2013-01-02 12:01:23 +00001813/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001814/// virt-specifier.
1815///
1816/// virt-specifier:
1817/// override
1818/// final
Richard Smith4e24f0f2013-01-02 12:01:23 +00001819VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001820 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001821 return VirtSpecifiers::VS_None;
1822
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001823 if (Tok.is(tok::identifier)) {
1824 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001825
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001826 // Initialize the contextual keywords.
1827 if (!Ident_final) {
1828 Ident_final = &PP.getIdentifierTable().get("final");
1829 Ident_override = &PP.getIdentifierTable().get("override");
1830 }
1831
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001832 if (II == Ident_override)
1833 return VirtSpecifiers::VS_Override;
1834
1835 if (II == Ident_final)
1836 return VirtSpecifiers::VS_Final;
1837 }
1838
1839 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001840}
1841
Richard Smith4e24f0f2013-01-02 12:01:23 +00001842/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001843///
1844/// virt-specifier-seq:
1845/// virt-specifier
1846/// virt-specifier-seq virt-specifier
Richard Smith4e24f0f2013-01-02 12:01:23 +00001847void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalle402e722012-09-25 07:32:39 +00001848 bool IsInterface) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001849 while (true) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001850 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001851 if (Specifier == VirtSpecifiers::VS_None)
1852 return;
1853
1854 // C++ [class.mem]p8:
1855 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001856 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001857 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001858 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1859 << PrevSpec
1860 << FixItHint::CreateRemoval(Tok.getLocation());
1861
John McCalle402e722012-09-25 07:32:39 +00001862 if (IsInterface && Specifier == VirtSpecifiers::VS_Final) {
1863 Diag(Tok.getLocation(), diag::err_override_control_interface)
1864 << VirtSpecifiers::getSpecifierName(Specifier);
1865 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00001866 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00001867 diag::warn_cxx98_compat_override_control_keyword :
1868 diag::ext_override_control_keyword)
1869 << VirtSpecifiers::getSpecifierName(Specifier);
1870 }
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001871 ConsumeToken();
1872 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001873}
1874
Richard Smith4e24f0f2013-01-02 12:01:23 +00001875/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001876/// contextual 'final' keyword.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001877bool Parser::isCXX11FinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001878 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001879 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001880
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001881 if (!Tok.is(tok::identifier))
1882 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001883
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001884 // Initialize the contextual keywords.
1885 if (!Ident_final) {
1886 Ident_final = &PP.getIdentifierTable().get("final");
1887 Ident_override = &PP.getIdentifierTable().get("override");
1888 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00001889
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001890 return Tok.getIdentifierInfo() == Ident_final;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001891}
1892
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001893/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1894///
1895/// member-declaration:
1896/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1897/// function-definition ';'[opt]
1898/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1899/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001900/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001901/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001902/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001903///
1904/// member-declarator-list:
1905/// member-declarator
1906/// member-declarator-list ',' member-declarator
1907///
1908/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001909/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001910/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001911/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001912/// identifier[opt] ':' constant-expression
1913///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001914/// virt-specifier-seq:
1915/// virt-specifier
1916/// virt-specifier-seq virt-specifier
1917///
1918/// virt-specifier:
1919/// override
1920/// final
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001921///
Sebastian Redle2b68332009-04-12 17:16:29 +00001922/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001923/// '= 0'
1924///
1925/// constant-initializer:
1926/// '=' constant-expression
1927///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001928void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001929 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001930 const ParsedTemplateInfo &TemplateInfo,
1931 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001932 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001933 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001934 Diag(Tok, diag::err_at_defs_cxx);
1935 else
1936 Diag(Tok, diag::err_at_in_class);
1937
1938 ConsumeToken();
1939 SkipUntil(tok::r_brace);
1940 return;
1941 }
1942
John McCall60fa3cf2009-12-11 02:10:03 +00001943 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001944 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001945 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001946 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1947 if (TryAnnotateCXXScopeToken())
1948 MalformedTypeSpec = true;
1949
1950 bool isAccessDecl;
1951 if (Tok.isNot(tok::annot_cxxscope))
1952 isAccessDecl = false;
1953 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001954 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1955 else
1956 isAccessDecl = NextToken().is(tok::kw_operator);
1957
1958 if (isAccessDecl) {
1959 // Collect the scope specifier token we annotated earlier.
1960 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001961 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1962 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001963
1964 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001965 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001966 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001967 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1968 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001969 SkipUntil(tok::semi);
1970 return;
1971 }
1972
1973 // TODO: recover from mistakenly-qualified operator declarations.
1974 if (ExpectAndConsume(tok::semi,
1975 diag::err_expected_semi_after,
1976 "access declaration",
1977 tok::semi))
1978 return;
1979
Douglas Gregor23c94db2010-07-02 17:43:08 +00001980 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanella8d030c72013-07-22 10:54:09 +00001981 /* HasUsingKeyword */ false,
1982 SourceLocation(),
John McCall60fa3cf2009-12-11 02:10:03 +00001983 SS, Name,
1984 /* AttrList */ 0,
Enea Zaffanella8d030c72013-07-22 10:54:09 +00001985 /* HasTypenameKeyword */ false,
John McCall60fa3cf2009-12-11 02:10:03 +00001986 SourceLocation());
1987 return;
1988 }
1989 }
1990
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001991 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001992 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00001993 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00001994 SourceLocation DeclEnd;
1995 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001996 return;
1997 }
Mike Stump1eb44332009-09-09 15:08:12 +00001998
Chris Lattner682bf922009-03-29 16:50:03 +00001999 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002000 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00002001 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00002002 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00002003 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002004 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00002005 return;
2006 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00002007
Chris Lattnerbc8d5642008-12-18 01:12:00 +00002008 // Handle: member-declaration ::= '__extension__' member-declaration
2009 if (Tok.is(tok::kw___extension__)) {
2010 // __extension__ silences extension warnings in the subexpression.
2011 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2012 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002013 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2014 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00002015 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002016
Chris Lattner4ed5d912010-02-02 01:23:29 +00002017 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
2018 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002019 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002020
John McCall0b7e6782011-03-24 11:26:52 +00002021 ParsedAttributesWithRange attrs(AttrFactory);
Michael Han52b501c2012-11-28 23:17:40 +00002022 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00002023 // Optional C++11 attribute-specifier
2024 MaybeParseCXX11Attributes(attrs);
Michael Han52b501c2012-11-28 23:17:40 +00002025 // We need to keep these attributes for future diagnostic
2026 // before they are taken over by declaration specifier.
2027 FnAttrs.addAll(attrs.getList());
2028 FnAttrs.Range = attrs.Range;
2029
John McCall7f040a92010-12-24 02:08:15 +00002030 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002031
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002032 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00002033 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002034
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002035 // Eat 'using'.
2036 SourceLocation UsingLoc = ConsumeToken();
2037
2038 if (Tok.is(tok::kw_namespace)) {
2039 Diag(UsingLoc, diag::err_using_namespace_in_class);
2040 SkipUntil(tok::semi, true, true);
Chris Lattnerae50d502010-02-02 00:43:15 +00002041 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002042 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00002043 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00002044 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2045 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002046 }
2047 return;
2048 }
2049
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002050 // Hold late-parsed attributes so we can attach a Decl to them later.
2051 LateParsedAttrList CommonLateParsedAttrs;
2052
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002053 // decl-specifier-seq:
2054 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00002055 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00002056 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00002057 if (MalformedTypeSpec)
2058 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002059 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2060 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002061
Benjamin Kramer5354e772012-08-23 23:38:35 +00002062 MultiTemplateParamsArg TemplateParams(
John McCalldd4a3b02009-09-16 22:47:08 +00002063 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2064 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2065
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002066 if (Tok.is(tok::semi)) {
2067 ConsumeToken();
Michael Han52b501c2012-11-28 23:17:40 +00002068
2069 if (DS.isFriendSpecified())
2070 ProhibitAttributes(FnAttrs);
2071
John McCalld226f652010-08-21 09:40:31 +00002072 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00002073 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00002074 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00002075 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002076 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002077
John McCall54abf7d2009-11-04 02:18:39 +00002078 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00002079 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002080
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002081 // Hold late-parsed attributes so we can attach a Decl to them later.
2082 LateParsedAttrList LateParsedAttrs;
2083
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002084 SourceLocation EqualLoc;
2085 bool HasInitializer = false;
2086 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002087 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002088 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2089 ColonProtectionRAIIObject X(*this);
2090
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002091 // Parse the first declarator.
2092 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00002093 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00002094 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002095 // If so, skip until the semi-colon or a }.
Sebastian Redld941fa42011-04-24 16:27:48 +00002096 SkipUntil(tok::r_brace, true, true);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002097 if (Tok.is(tok::semi))
2098 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002099 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002100 }
2101
Richard Smith4e24f0f2013-01-02 12:01:23 +00002102 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber48673472011-01-28 06:07:34 +00002103
John Thompson1b2fc0f2009-11-25 22:58:06 +00002104 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002105 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00002106
Francois Pichet6a247472011-05-11 02:14:46 +00002107 // MSVC permits pure specifier on inline functions declared at class scope.
2108 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00002109 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00002110 DeclaratorInfo.isFunctionDeclarator() &&
2111 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002112 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00002113 Init = ParseInitializer();
2114 if (Init.isInvalid())
2115 SkipUntil(tok::comma, true, true);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002116 else
2117 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00002118 }
2119
Douglas Gregor45fa5602011-11-07 20:56:01 +00002120 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002121 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00002122 //
2123 // In C++11, a non-function declarator followed by an open brace is a
2124 // braced-init-list for an in-class member initialization, not an
2125 // erroneous function definition.
Richard Smith80ad52f2013-01-02 11:42:31 +00002126 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002127 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002128 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00002129 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002130 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002131 } else if (Tok.is(tok::equal)) {
2132 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00002133 if (KW.is(tok::kw_default))
2134 DefinitionKind = FDK_Defaulted;
2135 else if (KW.is(tok::kw_delete))
2136 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00002137 }
2138 }
2139
Michael Han52b501c2012-11-28 23:17:40 +00002140 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2141 // to a friend declaration, that declaration shall be a definition.
2142 if (DeclaratorInfo.isFunctionDeclarator() &&
2143 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2144 // Diagnose attributes that appear before decl specifier:
2145 // [[]] friend int foo();
2146 ProhibitAttributes(FnAttrs);
2147 }
2148
Douglas Gregor45fa5602011-11-07 20:56:01 +00002149 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002150 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002151 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002152 ConsumeBrace();
Richard Trieu65ba9482012-01-21 02:59:18 +00002153 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
Michael Han52b501c2012-11-28 23:17:40 +00002154
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002155 // Consume the optional ';'
2156 if (Tok.is(tok::semi))
2157 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002158 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002159 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002160
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002161 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002162 Diag(DeclaratorInfo.getIdentifierLoc(),
2163 diag::err_function_declared_typedef);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002164
Richard Smith6f9a4452012-11-15 22:54:20 +00002165 // Recover by treating the 'typedef' as spurious.
2166 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002167 }
2168
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002169 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002170 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00002171 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002172
David Majnemerfcbe2082013-08-01 04:22:55 +00002173 if (FunDecl) {
2174 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2175 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2176 }
2177 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2178 LateParsedAttrs[i]->addDecl(FunDecl);
2179 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002180 }
2181 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00002182
2183 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002184 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00002185 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002186
Chris Lattner682bf922009-03-29 16:50:03 +00002187 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002188 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002189 }
2190
2191 // member-declarator-list:
2192 // member-declarator
2193 // member-declarator-list ',' member-declarator
2194
Chris Lattner5f9e2722011-07-23 10:55:15 +00002195 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00002196 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00002197 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002198
2199 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002200 // member-declarator:
2201 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002202 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002203 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002204 if (Tok.is(tok::colon)) {
2205 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002206 BitfieldSize = ParseConstantExpression();
2207 if (BitfieldSize.isInvalid())
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002208 SkipUntil(tok::comma, true, true);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002209 }
Mike Stump1eb44332009-09-09 15:08:12 +00002210
Chris Lattnere6563252010-06-13 05:34:18 +00002211 // If a simple-asm-expr is present, parse it.
2212 if (Tok.is(tok::kw_asm)) {
2213 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002214 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002215 if (AsmLabel.isInvalid())
2216 SkipUntil(tok::comma, true, true);
2217
2218 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2219 DeclaratorInfo.SetRangeEnd(Loc);
2220 }
2221
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002222 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002223 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002224
Richard Smith7a614d82011-06-11 17:19:42 +00002225 // FIXME: When g++ adds support for this, we'll need to check whether it
2226 // goes before or after the GNU attributes and __asm__.
Richard Smith4e24f0f2013-01-02 12:01:23 +00002227 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith7a614d82011-06-11 17:19:42 +00002228
Richard Smithca523302012-06-10 03:12:00 +00002229 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002230 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002231 if (BitfieldSize.get()) {
2232 Diag(Tok, diag::err_bitfield_member_init);
2233 SkipUntil(tok::comma, true, true);
2234 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002235 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002236 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2237 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smithca523302012-06-10 03:12:00 +00002238 != DeclSpec::SCS_typedef)
2239 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002240 }
2241 }
2242
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002243 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002244 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002245 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002246
Rafael Espindolafc35cbc2013-01-08 20:44:06 +00002247 NamedDecl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002248 if (DS.isFriendSpecified()) {
Michael Han52b501c2012-11-28 23:17:40 +00002249 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2250 // to a friend declaration, that declaration shall be a definition.
2251 //
2252 // Diagnose attributes appear after friend member function declarator:
2253 // foo [[]] ();
2254 SmallVector<SourceRange, 4> Ranges;
2255 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2256 if (!Ranges.empty()) {
Craig Topper09d19ef2013-07-04 03:08:24 +00002257 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
Michael Han52b501c2012-11-28 23:17:40 +00002258 E = Ranges.end(); I != E; ++I) {
2259 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2260 << *I;
2261 }
2262 }
2263
John McCallbbbcdd92009-09-11 21:02:39 +00002264 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002265 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002266 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002267 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002268 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002269 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002270 TemplateParams,
John McCall67d1a672009-08-06 02:15:43 +00002271 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002272 VS, HasInClassInit);
Larisse Voufoef4579c2013-08-06 01:03:05 +00002273
2274 if (VarTemplateDecl *VT =
2275 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : 0)
2276 // Re-direct this decl to refer to the templated decl so that we can
2277 // initialize it.
2278 ThisDecl = VT->getTemplatedDecl();
2279
David Majnemerfcbe2082013-08-01 04:22:55 +00002280 if (ThisDecl && AccessAttrs)
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002281 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs,
2282 false, true);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002283 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002284
Douglas Gregor147545d2011-10-10 14:49:18 +00002285 // Handle the initializer.
David Blaikie1d87fba2013-01-30 01:22:18 +00002286 if (HasInClassInit != ICIS_NoInit &&
2287 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2288 DeclSpec::SCS_static) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002289 // The initializer was deferred; parse it and cache the tokens.
David Majnemerfcbe2082013-08-01 04:22:55 +00002290 Diag(Tok, getLangOpts().CPlusPlus11
2291 ? diag::warn_cxx98_compat_nonstatic_member_init
2292 : diag::ext_nonstatic_member_init);
Richard Smith7fe62082011-10-15 05:09:34 +00002293
Richard Smith7a614d82011-06-11 17:19:42 +00002294 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002295 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2296 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002297 //
2298 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002299 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002300 Diag(Tok, diag::err_incomplete_array_member_init);
2301 SkipUntil(tok::comma, true, true);
David Majnemerfcbe2082013-08-01 04:22:55 +00002302
2303 // Avoid later warnings about a class member of incomplete type.
David Blaikie3164c142012-02-14 09:00:46 +00002304 if (ThisDecl)
David Blaikie3164c142012-02-14 09:00:46 +00002305 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002306 } else
2307 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002308 } else if (HasInitializer) {
2309 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002310 if (!Init.isUsable())
David Majnemerfcbe2082013-08-01 04:22:55 +00002311 Init = ParseCXXMemberInitializer(
2312 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2313
Douglas Gregor147545d2011-10-10 14:49:18 +00002314 if (Init.isInvalid())
2315 SkipUntil(tok::comma, true, true);
2316 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002317 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smitha2c36462013-04-26 16:15:35 +00002318 DS.containsPlaceholderType());
David Majnemerfcbe2082013-08-01 04:22:55 +00002319 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor147545d2011-10-10 14:49:18 +00002320 // No initializer.
Richard Smitha2c36462013-04-26 16:15:35 +00002321 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemerfcbe2082013-08-01 04:22:55 +00002322
Douglas Gregor147545d2011-10-10 14:49:18 +00002323 if (ThisDecl) {
David Majnemerfcbe2082013-08-01 04:22:55 +00002324 if (!ThisDecl->isInvalidDecl()) {
2325 // Set the Decl for any late parsed attributes
2326 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2327 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2328
2329 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2330 LateParsedAttrs[i]->addDecl(ThisDecl);
2331 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002332 Actions.FinalizeDeclaration(ThisDecl);
2333 DeclsInGroup.push_back(ThisDecl);
David Majnemerfcbe2082013-08-01 04:22:55 +00002334
2335 if (DeclaratorInfo.isFunctionDeclarator() &&
2336 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2337 DeclSpec::SCS_typedef)
2338 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002339 }
David Majnemerfcbe2082013-08-01 04:22:55 +00002340 LateParsedAttrs.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002341
2342 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002343
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002344 // If we don't have a comma, it is either the end of the list (a ';')
2345 // or an error, bail out.
2346 if (Tok.isNot(tok::comma))
2347 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002348
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002349 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002350 SourceLocation CommaLoc = ConsumeToken();
2351
2352 if (Tok.isAtStartOfLine() &&
2353 !MightBeDeclarator(Declarator::MemberContext)) {
2354 // This comma was followed by a line-break and something which can't be
2355 // the start of a declarator. The comma was probably a typo for a
2356 // semicolon.
2357 Diag(CommaLoc, diag::err_expected_semi_declaration)
2358 << FixItHint::CreateReplacement(CommaLoc, ";");
2359 ExpectSemi = false;
2360 break;
2361 }
Mike Stump1eb44332009-09-09 15:08:12 +00002362
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002363 // Parse the next declarator.
2364 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002365 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002366 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002367 Init = true;
2368 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002369 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002370
Bill Wendlingad017fa2012-12-20 19:22:21 +00002371 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002372 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002373
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002374 if (Tok.isNot(tok::colon))
2375 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002376 }
2377
Richard Smith1c94c162012-01-09 22:31:44 +00002378 if (ExpectSemi &&
2379 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002380 // Skip to end of block or statement.
2381 SkipUntil(tok::r_brace, true, true);
2382 // If we stopped at a ';', eat it.
2383 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002384 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002385 }
2386
Rafael Espindola4549d7f2013-07-09 12:05:01 +00002387 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002388}
2389
Richard Smith7a614d82011-06-11 17:19:42 +00002390/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2391/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2392/// function definition. The location of the '=', if any, will be placed in
2393/// EqualLoc.
2394///
2395/// pure-specifier:
2396/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002397///
Richard Smith7a614d82011-06-11 17:19:42 +00002398/// brace-or-equal-initializer:
2399/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002400/// braced-init-list
2401///
Richard Smith7a614d82011-06-11 17:19:42 +00002402/// initializer-clause:
2403/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002404/// braced-init-list
2405///
Richard Smith7a614d82011-06-11 17:19:42 +00002406/// defaulted/deleted function-definition:
2407/// '=' 'default'
2408/// '=' 'delete'
2409///
2410/// Prior to C++0x, the assignment-expression in an initializer-clause must
2411/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002412ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002413 SourceLocation &EqualLoc) {
2414 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2415 && "Data member initializer not starting with '=' or '{'");
2416
Douglas Gregor552e2992012-02-21 02:22:07 +00002417 EnterExpressionEvaluationContext Context(Actions,
2418 Sema::PotentiallyEvaluated,
2419 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002420 if (Tok.is(tok::equal)) {
2421 EqualLoc = ConsumeToken();
2422 if (Tok.is(tok::kw_delete)) {
2423 // In principle, an initializer of '= delete p;' is legal, but it will
2424 // never type-check. It's better to diagnose it as an ill-formed expression
2425 // than as an ill-formed deleted non-function member.
2426 // An initializer of '= delete p, foo' will never be parsed, because
2427 // a top-level comma always ends the initializer expression.
2428 const Token &Next = NextToken();
2429 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2430 Next.is(tok::eof)) {
2431 if (IsFunction)
2432 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2433 << 1 /* delete */;
2434 else
2435 Diag(ConsumeToken(), diag::err_deleted_non_function);
2436 return ExprResult();
2437 }
2438 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002439 if (IsFunction)
2440 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2441 << 0 /* default */;
2442 else
2443 Diag(ConsumeToken(), diag::err_default_special_members);
2444 return ExprResult();
2445 }
2446
Sebastian Redl33deb352012-02-22 10:50:08 +00002447 }
2448 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002449}
2450
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002451/// ParseCXXMemberSpecification - Parse the class definition.
2452///
2453/// member-specification:
2454/// member-declaration member-specification[opt]
2455/// access-specifier ':' member-specification[opt]
2456///
Joao Matos17d35c32012-08-31 22:18:20 +00002457void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han07fc1ba2013-01-07 16:57:11 +00002458 SourceLocation AttrFixitLoc,
Richard Smith05321402013-02-19 23:47:15 +00002459 ParsedAttributesWithRange &Attrs,
Joao Matos17d35c32012-08-31 22:18:20 +00002460 unsigned TagType, Decl *TagDecl) {
2461 assert((TagType == DeclSpec::TST_struct ||
2462 TagType == DeclSpec::TST_interface ||
2463 TagType == DeclSpec::TST_union ||
2464 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2465
John McCallf312b1e2010-08-26 23:41:50 +00002466 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2467 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002468
Douglas Gregor26997fd2010-01-16 20:52:59 +00002469 // Determine whether this is a non-nested class. Note that local
2470 // classes are *not* considered to be nested classes.
2471 bool NonNestedClass = true;
2472 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002473 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002474 if (S->isClassScope()) {
2475 // We're inside a class scope, so this is a nested class.
2476 NonNestedClass = false;
John McCalle402e722012-09-25 07:32:39 +00002477
2478 // The Microsoft extension __interface does not permit nested classes.
2479 if (getCurrentClass().IsInterface) {
2480 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2481 << /*ErrorType=*/6
2482 << (isa<NamedDecl>(TagDecl)
2483 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2484 : "<anonymous>");
2485 }
Douglas Gregor26997fd2010-01-16 20:52:59 +00002486 break;
2487 }
2488
2489 if ((S->getFlags() & Scope::FnScope)) {
2490 // If we're in a function or function template declared in the
2491 // body of a class, then this is a local class rather than a
2492 // nested class.
2493 const Scope *Parent = S->getParent();
2494 if (Parent->isTemplateParamScope())
2495 Parent = Parent->getParent();
2496 if (Parent->isClassScope())
2497 break;
2498 }
2499 }
2500 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002501
2502 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002503 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002504
Douglas Gregor6569d682009-05-27 23:11:45 +00002505 // Note that we are parsing a new (potentially-nested) class definition.
John McCalle402e722012-09-25 07:32:39 +00002506 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2507 TagType == DeclSpec::TST_interface);
Douglas Gregor6569d682009-05-27 23:11:45 +00002508
Douglas Gregorddc29e12009-02-06 22:42:48 +00002509 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002510 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002511
Anders Carlssonb184a182011-03-25 14:46:08 +00002512 SourceLocation FinalLoc;
2513
2514 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002515 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00002516 assert(isCXX11FinalKeyword() && "not a class definition");
Richard Smith8b11b5e2011-10-15 04:21:46 +00002517 FinalLoc = ConsumeToken();
Anders Carlssonb184a182011-03-25 14:46:08 +00002518
John McCalle402e722012-09-25 07:32:39 +00002519 if (TagType == DeclSpec::TST_interface) {
2520 Diag(FinalLoc, diag::err_override_control_interface)
2521 << "final";
2522 } else {
Richard Smith80ad52f2013-01-02 11:42:31 +00002523 Diag(FinalLoc, getLangOpts().CPlusPlus11 ?
John McCalle402e722012-09-25 07:32:39 +00002524 diag::warn_cxx98_compat_override_control_keyword :
2525 diag::ext_override_control_keyword) << "final";
2526 }
Michael Han2e397132012-11-26 22:54:45 +00002527
Michael Han07fc1ba2013-01-07 16:57:11 +00002528 // Parse any C++11 attributes after 'final' keyword.
2529 // These attributes are not allowed to appear here,
2530 // and the only possible place for them to appertain
2531 // to the class would be between class-key and class-name.
Richard Smith05321402013-02-19 23:47:15 +00002532 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonb184a182011-03-25 14:46:08 +00002533 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002534
John McCallbd0dfa52009-12-19 21:48:58 +00002535 if (Tok.is(tok::colon)) {
2536 ParseBaseClause(TagDecl);
2537
2538 if (!Tok.is(tok::l_brace)) {
2539 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002540
2541 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002542 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002543 return;
2544 }
2545 }
2546
2547 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002548 BalancedDelimiterTracker T(*this, tok::l_brace);
2549 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002550
John McCall42a4f662010-05-28 08:11:17 +00002551 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002552 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002553 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002554
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002555 // C++ 11p3: Members of a class defined with the keyword class are private
2556 // by default. Members of a class defined with the keywords struct or union
2557 // are public by default.
2558 AccessSpecifier CurAS;
2559 if (TagType == DeclSpec::TST_class)
2560 CurAS = AS_private;
2561 else
2562 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002563 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002564
Douglas Gregor07976d22010-06-21 22:31:09 +00002565 if (TagDecl) {
2566 // While we still have something to read, read the member-declarations.
2567 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2568 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002569
David Blaikie4e4d0842012-03-11 07:00:24 +00002570 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002571 Tok.is(tok::kw___if_not_exists))) {
2572 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2573 continue;
2574 }
2575
Douglas Gregor07976d22010-06-21 22:31:09 +00002576 // Check for extraneous top-level semicolon.
2577 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002578 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002579 continue;
2580 }
2581
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002582 if (Tok.is(tok::annot_pragma_vis)) {
2583 HandlePragmaVisibility();
2584 continue;
2585 }
2586
2587 if (Tok.is(tok::annot_pragma_pack)) {
2588 HandlePragmaPack();
2589 continue;
2590 }
2591
Argyrios Kyrtzidisf4deaef2012-10-12 17:39:59 +00002592 if (Tok.is(tok::annot_pragma_align)) {
2593 HandlePragmaAlign();
2594 continue;
2595 }
2596
Alexey Bataevc6400582013-03-22 06:34:35 +00002597 if (Tok.is(tok::annot_pragma_openmp)) {
2598 ParseOpenMPDeclarativeDirective();
2599 continue;
2600 }
2601
Douglas Gregor07976d22010-06-21 22:31:09 +00002602 AccessSpecifier AS = getAccessSpecifierIfPresent();
2603 if (AS != AS_none) {
2604 // Current token is a C++ access specifier.
2605 CurAS = AS;
2606 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002607 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002608 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002609 AccessAttrs.clear();
2610 MaybeParseGNUAttributes(AccessAttrs);
2611
David Blaikie13f8daf2011-10-13 06:08:43 +00002612 SourceLocation EndLoc;
2613 if (Tok.is(tok::colon)) {
2614 EndLoc = Tok.getLocation();
2615 ConsumeToken();
2616 } else if (Tok.is(tok::semi)) {
2617 EndLoc = Tok.getLocation();
2618 ConsumeToken();
2619 Diag(EndLoc, diag::err_expected_colon)
2620 << FixItHint::CreateReplacement(EndLoc, ":");
2621 } else {
2622 EndLoc = ASLoc.getLocWithOffset(TokLength);
2623 Diag(EndLoc, diag::err_expected_colon)
2624 << FixItHint::CreateInsertion(EndLoc, ":");
2625 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002626
John McCalle402e722012-09-25 07:32:39 +00002627 // The Microsoft extension __interface does not permit non-public
2628 // access specifiers.
2629 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2630 Diag(ASLoc, diag::err_access_specifier_interface)
2631 << (CurAS == AS_protected);
2632 }
2633
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002634 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2635 AccessAttrs.getList())) {
2636 // found another attribute than only annotations
2637 AccessAttrs.clear();
2638 }
2639
Douglas Gregor07976d22010-06-21 22:31:09 +00002640 continue;
2641 }
2642
2643 // FIXME: Make sure we don't have a template here.
2644
2645 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002646 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002647 }
2648
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002649 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002650 } else {
2651 SkipUntil(tok::r_brace, false, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002652 }
Mike Stump1eb44332009-09-09 15:08:12 +00002653
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002654 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002655 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002656 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002657
John McCall42a4f662010-05-28 08:11:17 +00002658 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002659 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002660 T.getOpenLocation(),
2661 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002662 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002663
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002664 // C++11 [class.mem]p2:
2665 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002666 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002667 // brace-or-equal-initializers for non-static data members (including such
2668 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002669 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002670 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002671 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002672 // declarations and the lexed inline method definitions, along with any
2673 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002674 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002675 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002676 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002677
2678 // We've finished with all pending member declarations.
2679 Actions.ActOnFinishCXXMemberDecls();
2680
Richard Smith7a614d82011-06-11 17:19:42 +00002681 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002682 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002683 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002684 }
2685
John McCall42a4f662010-05-28 08:11:17 +00002686 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002687 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2688 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002689
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002690 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002691 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002692 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002693}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002694
2695/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2696/// which explicitly initializes the members or base classes of a
2697/// class (C++ [class.base.init]). For example, the three initializers
2698/// after the ':' in the Derived constructor below:
2699///
2700/// @code
2701/// class Base { };
2702/// class Derived : Base {
2703/// int x;
2704/// float f;
2705/// public:
2706/// Derived(float f) : Base(), x(17), f(f) { }
2707/// };
2708/// @endcode
2709///
Mike Stump1eb44332009-09-09 15:08:12 +00002710/// [C++] ctor-initializer:
2711/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002712///
Mike Stump1eb44332009-09-09 15:08:12 +00002713/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002714/// mem-initializer ...[opt]
2715/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002716void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002717 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2718
John Wiegley28bbe4b2011-04-28 01:08:34 +00002719 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2720 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002721 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002722
Chris Lattner5f9e2722011-07-23 10:55:15 +00002723 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002724 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002725
Douglas Gregor7ad83902008-11-05 04:29:56 +00002726 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002727 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko572cf582013-06-23 22:58:02 +00002728 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2729 MemInitializers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002730 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002731 } else {
2732 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2733 if (!MemInit.isInvalid())
2734 MemInitializers.push_back(MemInit.get());
2735 else
2736 AnyErrors = true;
2737 }
2738
Douglas Gregor7ad83902008-11-05 04:29:56 +00002739 if (Tok.is(tok::comma))
2740 ConsumeToken();
2741 else if (Tok.is(tok::l_brace))
2742 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002743 // If the next token looks like a base or member initializer, assume that
2744 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002745 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2746 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2747 Diag(Loc, diag::err_ctor_init_missing_comma)
2748 << FixItHint::CreateInsertion(Loc, ", ");
2749 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002750 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002751 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002752 SkipUntil(tok::l_brace, true, true);
2753 break;
2754 }
2755 } while (true);
2756
David Blaikie93c86172013-01-17 05:26:25 +00002757 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002758 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002759}
2760
2761/// ParseMemInitializer - Parse a C++ member initializer, which is
2762/// part of a constructor initializer that explicitly initializes one
2763/// member or base class (C++ [class.base.init]). See
2764/// ParseConstructorInitializer for an example.
2765///
2766/// [C++] mem-initializer:
2767/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002768/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002769///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002770/// [C++] mem-initializer-id:
2771/// '::'[opt] nested-name-specifier[opt] class-name
2772/// identifier
John McCalld226f652010-08-21 09:40:31 +00002773Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002774 // parse '::'[opt] nested-name-specifier[opt]
2775 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002776 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002777 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002778 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002779 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002780 if (TemplateId->Kind == TNK_Type_template ||
2781 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002782 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002783 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002784 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002785 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002786 }
David Blaikief2116622012-01-24 06:03:59 +00002787 // Uses of decltype will already have been converted to annot_decltype by
2788 // ParseOptionalCXXScopeSpecifier at this point.
2789 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2790 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002791 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002792 return true;
2793 }
Mike Stump1eb44332009-09-09 15:08:12 +00002794
David Blaikief2116622012-01-24 06:03:59 +00002795 IdentifierInfo *II = 0;
2796 DeclSpec DS(AttrFactory);
2797 SourceLocation IdLoc = Tok.getLocation();
2798 if (Tok.is(tok::annot_decltype)) {
2799 // Get the decltype expression, if there is one.
2800 ParseDecltypeSpecifier(DS);
2801 } else {
2802 if (Tok.is(tok::identifier))
2803 // Get the identifier. This may be a member name or a class name,
2804 // but we'll let the semantic analysis determine which it is.
2805 II = Tok.getIdentifierInfo();
2806 ConsumeToken();
2807 }
2808
Douglas Gregor7ad83902008-11-05 04:29:56 +00002809
2810 // Parse the '('.
Richard Smith80ad52f2013-01-02 11:42:31 +00002811 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002812 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2813
Sebastian Redl6df65482011-09-24 17:48:25 +00002814 ExprResult InitList = ParseBraceInitializer();
2815 if (InitList.isInvalid())
2816 return true;
2817
2818 SourceLocation EllipsisLoc;
2819 if (Tok.is(tok::ellipsis))
2820 EllipsisLoc = ConsumeToken();
2821
2822 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002823 TemplateTypeTy, DS, IdLoc,
2824 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002825 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002826 BalancedDelimiterTracker T(*this, tok::l_paren);
2827 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002828
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002829 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002830 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002831 CommaLocsTy CommaLocs;
2832 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
2833 SkipUntil(tok::r_paren);
2834 return true;
2835 }
2836
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002837 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002838
2839 SourceLocation EllipsisLoc;
2840 if (Tok.is(tok::ellipsis))
2841 EllipsisLoc = ConsumeToken();
2842
2843 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002844 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenkoa36bbac2013-05-09 23:51:52 +00002845 T.getOpenLocation(), ArgExprs,
2846 T.getCloseLocation(), EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002847 }
2848
Richard Smith80ad52f2013-01-02 11:42:31 +00002849 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002850 : diag::err_expected_lparen);
2851 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002852}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002853
Sebastian Redl7acafd02011-03-05 14:45:16 +00002854/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002855///
Douglas Gregora4745612008-12-01 18:00:20 +00002856/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002857/// dynamic-exception-specification
2858/// noexcept-specification
2859///
2860/// noexcept-specification:
2861/// 'noexcept'
2862/// 'noexcept' '(' constant-expression ')'
2863ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002864Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002865 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002866 SmallVectorImpl<ParsedType> &DynamicExceptions,
2867 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002868 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002869 ExceptionSpecificationType Result = EST_None;
2870
2871 // See if there's a dynamic specification.
2872 if (Tok.is(tok::kw_throw)) {
2873 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2874 DynamicExceptions,
2875 DynamicExceptionRanges);
2876 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2877 "Produced different number of exception types and ranges.");
2878 }
2879
2880 // If there's no noexcept specification, we're done.
2881 if (Tok.isNot(tok::kw_noexcept))
2882 return Result;
2883
Richard Smith841804b2011-10-17 23:06:20 +00002884 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2885
Sebastian Redl7acafd02011-03-05 14:45:16 +00002886 // If we already had a dynamic specification, parse the noexcept for,
2887 // recovery, but emit a diagnostic and don't store the results.
2888 SourceRange NoexceptRange;
2889 ExceptionSpecificationType NoexceptType = EST_None;
2890
2891 SourceLocation KeywordLoc = ConsumeToken();
2892 if (Tok.is(tok::l_paren)) {
2893 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002894 BalancedDelimiterTracker T(*this, tok::l_paren);
2895 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002896 NoexceptType = EST_ComputedNoexcept;
2897 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002898 // The argument must be contextually convertible to bool. We use
2899 // ActOnBooleanCondition for this purpose.
2900 if (!NoexceptExpr.isInvalid())
2901 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2902 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002903 T.consumeClose();
2904 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002905 } else {
2906 // There is no argument.
2907 NoexceptType = EST_BasicNoexcept;
2908 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2909 }
2910
2911 if (Result == EST_None) {
2912 SpecificationRange = NoexceptRange;
2913 Result = NoexceptType;
2914
2915 // If there's a dynamic specification after a noexcept specification,
2916 // parse that and ignore the results.
2917 if (Tok.is(tok::kw_throw)) {
2918 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2919 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2920 DynamicExceptionRanges);
2921 }
2922 } else {
2923 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2924 }
2925
2926 return Result;
2927}
2928
Richard Smith79f4bb72013-06-13 02:02:51 +00002929static void diagnoseDynamicExceptionSpecification(
2930 Parser &P, const SourceRange &Range, bool IsNoexcept) {
2931 if (P.getLangOpts().CPlusPlus11) {
2932 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
2933 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
2934 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
2935 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
2936 }
2937}
2938
Sebastian Redl7acafd02011-03-05 14:45:16 +00002939/// ParseDynamicExceptionSpecification - Parse a C++
2940/// dynamic-exception-specification (C++ [except.spec]).
2941///
2942/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002943/// 'throw' '(' type-id-list [opt] ')'
2944/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002945///
Douglas Gregora4745612008-12-01 18:00:20 +00002946/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002947/// type-id ... [opt]
2948/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002949///
Sebastian Redl7acafd02011-03-05 14:45:16 +00002950ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
2951 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002952 SmallVectorImpl<ParsedType> &Exceptions,
2953 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002954 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00002955
Sebastian Redl7acafd02011-03-05 14:45:16 +00002956 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002957 BalancedDelimiterTracker T(*this, tok::l_paren);
2958 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002959 Diag(Tok, diag::err_expected_lparen_after) << "throw";
2960 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00002961 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002962 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002963
Douglas Gregora4745612008-12-01 18:00:20 +00002964 // Parse throw(...), a Microsoft extension that means "this function
2965 // can throw anything".
2966 if (Tok.is(tok::ellipsis)) {
2967 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00002968 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00002969 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002970 T.consumeClose();
2971 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith79f4bb72013-06-13 02:02:51 +00002972 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redl60618fa2011-03-12 11:50:43 +00002973 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00002974 }
2975
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002976 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00002977 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002978 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00002979 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00002980
Douglas Gregora04426c2010-12-20 23:57:46 +00002981 if (Tok.is(tok::ellipsis)) {
2982 // C++0x [temp.variadic]p5:
2983 // - In a dynamic-exception-specification (15.4); the pattern is a
2984 // type-id.
2985 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002986 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00002987 if (!Res.isInvalid())
2988 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
2989 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00002990
Sebastian Redlef65f062009-05-29 18:02:33 +00002991 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00002992 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00002993 Ranges.push_back(Range);
2994 }
Douglas Gregora04426c2010-12-20 23:57:46 +00002995
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002996 if (Tok.is(tok::comma))
2997 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00002998 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002999 break;
3000 }
3001
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003002 T.consumeClose();
3003 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith79f4bb72013-06-13 02:02:51 +00003004 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3005 Exceptions.empty());
Sebastian Redl60618fa2011-03-12 11:50:43 +00003006 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003007}
Douglas Gregor6569d682009-05-27 23:11:45 +00003008
Douglas Gregordab60ad2010-10-01 18:44:50 +00003009/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3010/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00003011TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00003012 assert(Tok.is(tok::arrow) && "expected arrow");
3013
3014 ConsumeToken();
3015
Richard Smith7796eb52012-03-12 08:56:40 +00003016 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00003017}
3018
Douglas Gregor6569d682009-05-27 23:11:45 +00003019/// \brief We have just started parsing the definition of a new class,
3020/// so push that class onto our stack of classes that is currently
3021/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00003022Sema::ParsingClassState
John McCalle402e722012-09-25 07:32:39 +00003023Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3024 bool IsInterface) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00003025 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00003026 "Nested class without outer class");
John McCalle402e722012-09-25 07:32:39 +00003027 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCalleee1d542011-02-14 07:13:47 +00003028 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00003029}
3030
3031/// \brief Deallocate the given parsed class and all of its nested
3032/// classes.
3033void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00003034 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3035 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00003036 delete Class;
3037}
3038
3039/// \brief Pop the top class of the stack of classes that are
3040/// currently being parsed.
3041///
3042/// This routine should be called when we have finished parsing the
3043/// definition of a class, but have not yet popped the Scope
3044/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00003045void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00003046 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00003047
John McCalleee1d542011-02-14 07:13:47 +00003048 Actions.PopParsingClass(state);
3049
Douglas Gregor6569d682009-05-27 23:11:45 +00003050 ParsingClass *Victim = ClassStack.top();
3051 ClassStack.pop();
3052 if (Victim->TopLevelClass) {
3053 // Deallocate all of the nested classes of this class,
3054 // recursively: we don't need to keep any of this information.
3055 DeallocateParsedClasses(Victim);
3056 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003057 }
Douglas Gregor6569d682009-05-27 23:11:45 +00003058 assert(!ClassStack.empty() && "Missing top-level class?");
3059
Douglas Gregord54eb442010-10-12 16:25:54 +00003060 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00003061 // The victim is a nested class, but we will not need to perform
3062 // any processing after the definition of this class since it has
3063 // no members whose handling was delayed. Therefore, we can just
3064 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00003065 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00003066 return;
3067 }
3068
3069 // This nested class has some members that will need to be processed
3070 // after the top-level class is completely defined. Therefore, add
3071 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003072 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00003073 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00003074 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00003075}
Sean Huntbbd37c62009-11-21 08:43:09 +00003076
Richard Smithc56298d2012-04-10 03:25:07 +00003077/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3078///
3079/// \return the parsed identifier on success, and 0 if the next token is not an
3080/// attribute-token.
3081///
3082/// C++11 [dcl.attr.grammar]p3:
3083/// If a keyword or an alternative token that satisfies the syntactic
3084/// requirements of an identifier is contained in an attribute-token,
3085/// it is considered an identifier.
3086IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3087 switch (Tok.getKind()) {
3088 default:
3089 // Identifiers and keywords have identifier info attached.
3090 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3091 Loc = ConsumeToken();
3092 return II;
3093 }
3094 return 0;
3095
3096 case tok::ampamp: // 'and'
3097 case tok::pipe: // 'bitor'
3098 case tok::pipepipe: // 'or'
3099 case tok::caret: // 'xor'
3100 case tok::tilde: // 'compl'
3101 case tok::amp: // 'bitand'
3102 case tok::ampequal: // 'and_eq'
3103 case tok::pipeequal: // 'or_eq'
3104 case tok::caretequal: // 'xor_eq'
3105 case tok::exclaim: // 'not'
3106 case tok::exclaimequal: // 'not_eq'
3107 // Alternative tokens do not have identifier info, but their spelling
3108 // starts with an alphabetical character.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003109 SmallString<8> SpellingBuf;
Richard Smithc56298d2012-04-10 03:25:07 +00003110 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rose3f6f51e2013-02-08 22:30:41 +00003111 if (isLetter(Spelling[0])) {
Richard Smithc56298d2012-04-10 03:25:07 +00003112 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00003113 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00003114 }
3115 return 0;
3116 }
3117}
3118
Michael Han6880f492012-10-03 01:56:22 +00003119static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3120 IdentifierInfo *ScopeName) {
3121 switch (AttributeList::getKind(AttrName, ScopeName,
3122 AttributeList::AS_CXX11)) {
3123 case AttributeList::AT_CarriesDependency:
3124 case AttributeList::AT_FallThrough:
Richard Smithcd8ab512013-01-17 01:30:42 +00003125 case AttributeList::AT_CXX11NoReturn: {
Michael Han6880f492012-10-03 01:56:22 +00003126 return true;
3127 }
3128
3129 default:
3130 return false;
3131 }
3132}
3133
Richard Smithc56298d2012-04-10 03:25:07 +00003134/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003135/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00003136///
Richard Smith6ee326a2012-04-10 01:32:12 +00003137/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00003138/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003139/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00003140///
Richard Smith6ee326a2012-04-10 01:32:12 +00003141/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00003142/// attribute[opt]
3143/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00003144/// attribute '...'
3145/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00003146///
Richard Smith6ee326a2012-04-10 01:32:12 +00003147/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00003148/// attribute-token attribute-argument-clause[opt]
3149///
Richard Smith6ee326a2012-04-10 01:32:12 +00003150/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003151/// identifier
3152/// attribute-scoped-token
3153///
Richard Smith6ee326a2012-04-10 01:32:12 +00003154/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003155/// attribute-namespace '::' identifier
3156///
Richard Smith6ee326a2012-04-10 01:32:12 +00003157/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00003158/// identifier
3159///
Richard Smith6ee326a2012-04-10 01:32:12 +00003160/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00003161/// '(' balanced-token-seq ')'
3162///
Richard Smith6ee326a2012-04-10 01:32:12 +00003163/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00003164/// balanced-token
3165/// balanced-token-seq balanced-token
3166///
Richard Smith6ee326a2012-04-10 01:32:12 +00003167/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003168/// '(' balanced-token-seq ')'
3169/// '[' balanced-token-seq ']'
3170/// '{' balanced-token-seq '}'
3171/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00003172void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003173 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003174 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00003175 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003176 ParseAlignmentSpecifier(attrs, endLoc);
3177 return;
3178 }
3179
Sean Huntbbd37c62009-11-21 08:43:09 +00003180 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00003181 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00003182
Richard Smith41be6732011-10-14 20:48:27 +00003183 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3184
Sean Huntbbd37c62009-11-21 08:43:09 +00003185 ConsumeBracket();
3186 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003187
Richard Smithcd8ab512013-01-17 01:30:42 +00003188 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3189
Richard Smithc56298d2012-04-10 03:25:07 +00003190 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003191 // attribute not present
3192 if (Tok.is(tok::comma)) {
3193 ConsumeToken();
3194 continue;
3195 }
3196
Richard Smithc56298d2012-04-10 03:25:07 +00003197 SourceLocation ScopeLoc, AttrLoc;
3198 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3199
3200 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3201 if (!AttrName)
3202 // Break out to the "expected ']'" diagnostic.
3203 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003204
Sean Huntbbd37c62009-11-21 08:43:09 +00003205 // scoped attribute
3206 if (Tok.is(tok::coloncolon)) {
3207 ConsumeToken();
3208
Richard Smithc56298d2012-04-10 03:25:07 +00003209 ScopeName = AttrName;
3210 ScopeLoc = AttrLoc;
3211
3212 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3213 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003214 Diag(Tok.getLocation(), diag::err_expected_ident);
3215 SkipUntil(tok::r_square, tok::comma, true, true);
3216 continue;
3217 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003218 }
3219
Michael Han6880f492012-10-03 01:56:22 +00003220 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Sean Huntbbd37c62009-11-21 08:43:09 +00003221 bool AttrParsed = false;
Sean Huntbbd37c62009-11-21 08:43:09 +00003222
Richard Smithcd8ab512013-01-17 01:30:42 +00003223 if (StandardAttr &&
3224 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3225 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3226 << AttrName << SourceRange(SeenAttrs[AttrName]);
3227
Michael Han6880f492012-10-03 01:56:22 +00003228 // Parse attribute arguments
3229 if (Tok.is(tok::l_paren)) {
3230 if (ScopeName && ScopeName->getName() == "gnu") {
3231 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3232 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3233 AttrParsed = true;
3234 } else {
3235 if (StandardAttr)
3236 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3237 << AttrName->getName();
3238
3239 // FIXME: handle other formats of c++11 attribute arguments
3240 ConsumeParen();
3241 SkipUntil(tok::r_paren, false);
3242 }
3243 }
3244
3245 if (!AttrParsed)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00003246 attrs.addNew(AttrName,
3247 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3248 AttrLoc),
3249 ScopeName, ScopeLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00003250 SourceLocation(), 0, 0, AttributeList::AS_CXX11);
Richard Smith6ee326a2012-04-10 01:32:12 +00003251
Richard Smithc56298d2012-04-10 03:25:07 +00003252 if (Tok.is(tok::ellipsis)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003253 ConsumeToken();
Michael Han6880f492012-10-03 01:56:22 +00003254
3255 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3256 << AttrName->getName();
Richard Smithc56298d2012-04-10 03:25:07 +00003257 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003258 }
3259
3260 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3261 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003262 if (endLoc)
3263 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00003264 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
3265 SkipUntil(tok::r_square, false);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003266}
Sean Huntbbd37c62009-11-21 08:43:09 +00003267
Sean Hunt2edf0a22012-06-23 05:07:58 +00003268/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003269///
3270/// attribute-specifier-seq:
3271/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00003272void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003273 SourceLocation *endLoc) {
Richard Smith672edb02013-02-22 09:15:49 +00003274 assert(getLangOpts().CPlusPlus11);
3275
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003276 SourceLocation StartLoc = Tok.getLocation(), Loc;
3277 if (!endLoc)
3278 endLoc = &Loc;
3279
Douglas Gregor8828ee72011-10-07 20:35:25 +00003280 do {
Richard Smithc56298d2012-04-10 03:25:07 +00003281 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003282 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003283
3284 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003285}
3286
Francois Pichet334d47e2010-10-11 12:59:39 +00003287/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3288///
3289/// [MS] ms-attribute:
3290/// '[' token-seq ']'
3291///
3292/// [MS] ms-attribute-seq:
3293/// ms-attribute[opt]
3294/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003295void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3296 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003297 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3298
3299 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003300 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003301 ConsumeBracket();
3302 SkipUntil(tok::r_square, true, true);
John McCall7f040a92010-12-24 02:08:15 +00003303 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003304 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3305 }
3306}
Francois Pichet563a6452011-05-25 10:19:49 +00003307
3308void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3309 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003310 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003311 if (ParseMicrosoftIfExistsCondition(Result))
3312 return;
3313
Douglas Gregor3896fc52011-10-24 22:31:10 +00003314 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3315 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003316 Diag(Tok, diag::err_expected_lbrace);
3317 return;
3318 }
Francois Pichet563a6452011-05-25 10:19:49 +00003319
Douglas Gregor3896fc52011-10-24 22:31:10 +00003320 switch (Result.Behavior) {
3321 case IEB_Parse:
3322 // Parse the declarations below.
3323 break;
3324
3325 case IEB_Dependent:
3326 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3327 << Result.IsIfExists;
3328 // Fall through to skip.
3329
3330 case IEB_Skip:
3331 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003332 return;
3333 }
3334
Douglas Gregor3896fc52011-10-24 22:31:10 +00003335 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003336 // __if_exists, __if_not_exists can nest.
3337 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3338 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3339 continue;
3340 }
3341
3342 // Check for extraneous top-level semicolon.
3343 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003344 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003345 continue;
3346 }
3347
3348 AccessSpecifier AS = getAccessSpecifierIfPresent();
3349 if (AS != AS_none) {
3350 // Current token is a C++ access specifier.
3351 CurAS = AS;
3352 SourceLocation ASLoc = Tok.getLocation();
3353 ConsumeToken();
3354 if (Tok.is(tok::colon))
3355 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3356 else
3357 Diag(Tok, diag::err_expected_colon);
3358 ConsumeToken();
3359 continue;
3360 }
3361
3362 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003363 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003364 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003365
3366 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003367}